-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
143 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
examples/ospf-kotlin-example/src/main/fuookami/ospf/kotlin/example/core_demo/Demo5.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package fuookami.ospf.kotlin.example.core_demo | ||
|
||
import fuookami.ospf.kotlin.core.backend.plugins.scip.SCIPLinearSolver | ||
import fuookami.ospf.kotlin.utils.concept.* | ||
import fuookami.ospf.kotlin.utils.error.* | ||
import fuookami.ospf.kotlin.utils.functional.* | ||
import fuookami.ospf.kotlin.utils.multi_array.* | ||
import fuookami.ospf.kotlin.utils.math.* | ||
import fuookami.ospf.kotlin.core.frontend.variable.* | ||
import fuookami.ospf.kotlin.core.frontend.expression.monomial.* | ||
import fuookami.ospf.kotlin.core.frontend.expression.polynomial.* | ||
import fuookami.ospf.kotlin.core.frontend.expression.symbol.* | ||
import fuookami.ospf.kotlin.core.frontend.inequality.* | ||
import fuookami.ospf.kotlin.core.frontend.model.mechanism.* | ||
|
||
data object Demo5 { | ||
data class Cargo( | ||
val weight: UInt64, | ||
val value: UInt64 | ||
) : AutoIndexed(Cargo::class) | ||
|
||
private val cargos = listOf( | ||
Cargo(UInt64(2U), UInt64(6U)), | ||
Cargo(UInt64(2U), UInt64(3U)), | ||
Cargo(UInt64(6U), UInt64(5U)), | ||
Cargo(UInt64(5U), UInt64(4U)), | ||
Cargo(UInt64(4U), UInt64(6U)) | ||
) | ||
private val maxWeight = UInt64(10U) | ||
|
||
private lateinit var x: BinVariable1 | ||
private lateinit var cargoWeight: LinearSymbol | ||
private lateinit var cargoValue: LinearSymbol | ||
|
||
private val metaModel: LinearMetaModel = LinearMetaModel("demo5") | ||
|
||
private val subProcesses = listOf( | ||
Demo5::initVariable, | ||
Demo5::initSymbol, | ||
Demo5::initObject, | ||
Demo5::initConstraint, | ||
Demo5::solve, | ||
Demo5::analyzeSolution | ||
) | ||
|
||
suspend operator fun invoke(): Try { | ||
for (process in subProcesses) { | ||
when (val result = process()) { | ||
is Ok -> {} | ||
|
||
is Failed -> { | ||
return Failed(result.error) | ||
} | ||
} | ||
} | ||
return ok | ||
} | ||
|
||
private suspend fun initVariable(): Try { | ||
x = BinVariable1("x", Shape1(cargos.size)) | ||
for (c in cargos) { | ||
x[c].name = "${x.name}_${c.index}" | ||
} | ||
metaModel.addVars(x) | ||
return ok | ||
} | ||
|
||
private suspend fun initSymbol(): Try { | ||
cargoValue = LinearExpressionSymbol(sum(cargos) { c -> c.value * x[c] }, "value") | ||
metaModel.addSymbol(cargoValue) | ||
|
||
cargoWeight = LinearExpressionSymbol(sum(cargos) { c -> c.weight * x[c] }, "weight") | ||
metaModel.addSymbol(cargoWeight) | ||
return ok | ||
} | ||
|
||
private suspend fun initObject(): Try { | ||
metaModel.maximize(LinearPolynomial(cargoValue),"value") | ||
return ok | ||
} | ||
|
||
private suspend fun initConstraint(): Try { | ||
metaModel.addConstraint( | ||
cargoWeight leq maxWeight,"weight" | ||
) | ||
return ok | ||
} | ||
|
||
private suspend fun solve(): Try { | ||
val solver = SCIPLinearSolver() | ||
when (val ret = solver(metaModel)) { | ||
is Ok -> { | ||
metaModel.tokens.setSolution(ret.value.solution) | ||
} | ||
|
||
is Failed -> { | ||
return Failed(ret.error) | ||
} | ||
} | ||
return ok | ||
} | ||
|
||
private suspend fun analyzeSolution(): Try { | ||
val ret = HashSet<Cargo>() | ||
for (token in metaModel.tokens.tokens) { | ||
if (token.result!! eq Flt64.one | ||
&& token.variable.belongsTo(x) | ||
) { | ||
ret.add(cargos[token.variable.vectorView[0]]) | ||
} | ||
} | ||
return ok | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters