Koptim (for Kotlin Optimization) is a Kotlin DSL for Linear Programming. It uses the Apache Commons Mathematics Library and its Simplex implementation (see org.apache.commons.math3.optim.linear.SimplexSolver) to solve the LP instances.
Koptim is an open source project licensed under the Apache License, Version 2.0. Gradle is used for building and dependency management. The only compile time dependency is the Apache Commons Mathematics Library, while testing is done with Speck accompanied with Strikt.
val lp = LPSolver()
lp.max {
15.0 * x(0) + 10*x(1) + 7.0
} subjectTo {
x(0) <= 2
x(1) <= 3
x(0) + x(1) <= 4
-x(0) - x(1) <= -4
}
// To print the solution:
lp.x.forEachIndexed { index, lpVar -> println("x$index: ${lpVar.value}")}
println("objFun: ${lp.objectiveValue}")
To run the example, use ./gradlew run
from the project directory.
While Koptim targets the LP canonical form, some flexibility beyond the canonical form is offered:
- you can define min instead of max to minimize the objective function;
- constants are allowed in the objective function;
- variables are allowed to be negative (if you want one to be strictly non-negative, you need to make this explicit with a constraint).
Koptim strictly adheres to the canonical form for constraints. All the constraints are deemed "less than or equals to", no matter what the comparison symbol is ('<=' or '>='), while '==' is a compilation error.