Skip to content

Instantly share code, notes, and snippets.

View thomasnield's full-sized avatar

Thomas Nield thomasnield

  • Frisco, Texas (United States)
View GitHub Profile
@thomasnield
thomasnield / pulp_optimization_1.py
Created October 17, 2017 00:53
Profit Maximization Problem with Pulp
import pulp
# Code for this walkthrough: http://benalexkeen.com/linear-programming-with-python-and-pulp-part-3/
model = pulp.LpProblem("Profit maximising problem", pulp.LpMaximize)
# Declare variables
A = pulp.LpVariable('A', lowBound=0, cat='Integer')
B = pulp.LpVariable('B', lowBound=0, cat='Integer')
@thomasnield
thomasnield / linear_optimization.kt
Created October 17, 2017 01:35
Linear Optimization with Kotlin and Apache Commons Math
package org.nield.kotlinstatistics
import org.apache.commons.math3.optim.MaxIter
import org.apache.commons.math3.optim.linear.*
import org.apache.commons.math3.optim.nonlinear.scalar.GoalType
/*
Linear optimization with Apache Commons Math and Kotlin
Original problem: http://benalexkeen.com/linear-programming-with-python-and-pulp-part-3/
*/
@thomasnield
thomasnield / ojalgo_diet_problem.kt
Created October 17, 2017 19:36
ojAlgo - The Diet Problem
import org.ojalgo.netio.BasicLogger
import org.ojalgo.optimisation.ExpressionsBasedModel
import org.ojalgo.optimisation.Variable
// https://github.com/optimatika/ojAlgo/wiki/The-Diet-Problem
fun main(args: Array<String>) {
BasicLogger.debug()
BasicLogger.debug(OjAlgoUtils.getTitle())
@thomasnield
thomasnield / ojalgo_sausage_problem.kt
Last active October 18, 2017 01:51
ojAlgo - The Sausage Problem
import org.ojalgo.optimisation.ExpressionsBasedModel
import org.ojalgo.optimisation.Variable
import java.math.RoundingMode
// PROBLEM DESCRIBED HERE: http://benalexkeen.com/linear-programming-with-python-and-pulp-part-4/
fun main(args: Array<String>) {
val model = ExpressionsBasedModel()
@thomasnield
thomasnield / integer_programming_abstract_example.kt
Last active December 25, 2017 04:04
Integer programming abstract example
package org.nield
import org.ojalgo.optimisation.ExpressionsBasedModel
import org.ojalgo.optimisation.Variable
import java.util.concurrent.atomic.AtomicInteger
// declare ojAlgo Model
val model = ExpressionsBasedModel()
// custom DSL for model expression inputs, eliminate naming and adding
@thomasnield
thomasnield / tuples.kt
Created January 17, 2018 20:00
Kotlin Tuples
/**
* Try to use explicit data classes, but if you want to quickly create tuples these will help
*/
data class Tuple2<out A, out B>(val a: A, val b: B)
data class Tuple3<out A, out B, out C>(val a: A, val b: B, val c: C)
data class Tuple4<out A, out B, out C, out D>(val a: A, val b: B, val c: C, val d: D)
data class Tuple5<out A, out B, out C, out D, out E>(val a: A, val b: B, val c: C, val d: D, val e: E)
ds,y
2015-01-12 06:26:00,101
2015-01-12 06:11:00,85
2015-01-12 05:23:00,67
2015-01-12 07:40:00,87
2015-01-12 10:10:00,81
2015-01-12 03:24:00,76
2015-01-12 01:41:00,75
2015-01-12 02:37:00,66
2015-01-12 08:29:00,110
fun <T,R> Observable<T>.switchReplaySingle(cachedSingle: (T) -> Single<R>) =
map { cachedSingle(it).toObservable().replay(1).autoConnect().firstOrError() }
.replay(1)
.autoConnect()
.take(1)
.flatMapSingle { it }
.firstOrError()
fun <T,R> Flowable<T>.switchReplaySingle(cachedSingle: (T) -> Single<R>) =
map { cachedSingle(it).toObservable().replay(1).autoConnect().firstOrError() }
@thomasnield
thomasnield / test.kt
Last active May 4, 2018 21:00
test.kt
fun main(args: Array<String>) {
val markets = listOf(
OD("ABQ","PHX"),
OD("DAL", "HOU"),
OD("HOU", "PHX")
)
@thomasnield
thomasnield / LazyTimedCache.kt
Last active May 30, 2018 15:51
Kotlin LazyTimedCache Delegate
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicReference
import kotlin.reflect.KProperty
/**