A concise Kotlin DSL wrapper for Google's OR-Tools Linear and Mixed Integer Programming Solver.
Optimization Kotlin DSL is a expressive Kotlin Domain-Specific Language (DSL) for defining and
solving optimization problems using Google OR-Tools.
It simplifies model creation by providing idiomatic Kotlin syntax while supporting:
- Linear Programming (LP)
- Integer Programming (IP)
- Mixed Integer Programming (MIP)
Inspired by io.justdevit:simplex-kotlin-dsl, this library extends
its concept to cover a wider range of solver types.
val (status, config) = optimize {
solver(SolverType.SCIP_MIXED_INTEGER_PROGRAMMING)
val x = intVar("x")
val y = numVar("y")
val z = boolVar("z")
// OBJECTIVE
x * 2 + y * 3 + 4 * z to Goal.MAX
// CONSTRAINTS
x + y le 3
y - 1 le 2
5 * y eq (x + 3) * 2
val variables = listOf(x, y, z)
for (variable in variables) {
variable le 1.5
}
variables.sum() le y
solve()
}
println("OBJECTIVE")
println("Optimal objective value = ${config.objective.value()}")
println("VARIABLES")
config.variables.forEach { variable ->
println("${variable.name()} = ${variable.solutionValue()}")
}Output:
OBJECTIVE
Optimal objective value = 4.3999999999999995
VARIABLES
x = -1.0
y = 0.7999999999999999
z = 1.0val (status, config) = optimize {
solver(SolverType.GLOP_LINEAR_PROGRAMMING)
val totalCost = numVar("totalCost", lowerBound = 0.0)
val flows = tensorNumVar(
tensorKeys = listOf(nodes, nodes),
namePrefix = "flow",
lowerBound = 0,
)
min {
totalCost
}
"total cost constraint" {
totalCost eq nodes.flatMap { f ->
nodes.map { t ->
costTensor[f, t] * flows[f, t]
}
}.sum()
}
"initial flow constraint" {
nodes.map { z -> flows["s", z] }.sum() eq fGiven
}
for (v in nodesWithout) {
"Kirchhoff's law constraint - $v" {
nodes.map { z -> flows[v, z] }.sum() eq
nodes.map { u -> flows[u, v] }.sum()
}
}
for (f in nodes) {
for (t in nodes) {
"max flow constraint - $f $t" {
flows[f, t] le capacityMinCostTensor[f, t]
}
}
}
solve()
}Add the dependency to your Maven or Gradle project.
<dependency>
<groupId>io.github.bartlomiejkrawczyk</groupId>
<artifactId>optimization-kotlin-dsl</artifactId>
<version>${version}</version>
</dependency>dependencies {
implementation("io.github.bartlomiejkrawczyk:optimization-kotlin-dsl:${version}")
}This project is built with Gradle and configured for publication to GitHub Packages. To build and test locally:
./gradlew clean buildTo publish to GitHub Packages repository:
./gradlew publishMake sure to set the following environment variables or Gradle properties:
GITHUB_ACTOR=your_github_username
GITHUB_TOKEN=your_personal_access_tokenThis project is licensed under the MIT License.
Contributions are welcome!
If you'd like to contribute:
- Fork the repository
- Create a new branch (
feature/my-featureorfix/my-bug) - Commit your changes
- Open a Pull Request
Issues, suggestions, and improvements are highly appreciated.