|
| 1 | +package fuookami.ospf.kotlin.example.core_demo |
| 2 | + |
| 3 | +import fuookami.ospf.kotlin.core.backend.plugins.scip.SCIPLinearSolver |
| 4 | +import fuookami.ospf.kotlin.utils.concept.* |
| 5 | +import fuookami.ospf.kotlin.utils.error.* |
| 6 | +import fuookami.ospf.kotlin.utils.functional.* |
| 7 | +import fuookami.ospf.kotlin.utils.multi_array.* |
| 8 | +import fuookami.ospf.kotlin.utils.math.* |
| 9 | +import fuookami.ospf.kotlin.core.frontend.variable.* |
| 10 | +import fuookami.ospf.kotlin.core.frontend.expression.monomial.* |
| 11 | +import fuookami.ospf.kotlin.core.frontend.expression.polynomial.* |
| 12 | +import fuookami.ospf.kotlin.core.frontend.expression.symbol.* |
| 13 | +import fuookami.ospf.kotlin.core.frontend.inequality.* |
| 14 | +import fuookami.ospf.kotlin.core.frontend.model.mechanism.* |
| 15 | + |
| 16 | +data object Demo5 { |
| 17 | + data class Cargo( |
| 18 | + val weight: UInt64, |
| 19 | + val value: UInt64 |
| 20 | + ) : AutoIndexed(Cargo::class) |
| 21 | + |
| 22 | + private val cargos = listOf( |
| 23 | + Cargo(UInt64(2U), UInt64(6U)), |
| 24 | + Cargo(UInt64(2U), UInt64(3U)), |
| 25 | + Cargo(UInt64(6U), UInt64(5U)), |
| 26 | + Cargo(UInt64(5U), UInt64(4U)), |
| 27 | + Cargo(UInt64(4U), UInt64(6U)) |
| 28 | + ) |
| 29 | + private val maxWeight = UInt64(10U) |
| 30 | + |
| 31 | + private lateinit var x: BinVariable1 |
| 32 | + private lateinit var cargoWeight: LinearSymbol |
| 33 | + private lateinit var cargoValue: LinearSymbol |
| 34 | + |
| 35 | + private val metaModel: LinearMetaModel = LinearMetaModel("demo5") |
| 36 | + |
| 37 | + private val subProcesses = listOf( |
| 38 | + Demo5::initVariable, |
| 39 | + Demo5::initSymbol, |
| 40 | + Demo5::initObject, |
| 41 | + Demo5::initConstraint, |
| 42 | + Demo5::solve, |
| 43 | + Demo5::analyzeSolution |
| 44 | + ) |
| 45 | + |
| 46 | + suspend operator fun invoke(): Try { |
| 47 | + for (process in subProcesses) { |
| 48 | + when (val result = process()) { |
| 49 | + is Ok -> {} |
| 50 | + |
| 51 | + is Failed -> { |
| 52 | + return Failed(result.error) |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + return ok |
| 57 | + } |
| 58 | + |
| 59 | + private suspend fun initVariable(): Try { |
| 60 | + x = BinVariable1("x", Shape1(cargos.size)) |
| 61 | + for (c in cargos) { |
| 62 | + x[c].name = "${x.name}_${c.index}" |
| 63 | + } |
| 64 | + metaModel.addVars(x) |
| 65 | + return ok |
| 66 | + } |
| 67 | + |
| 68 | + private suspend fun initSymbol(): Try { |
| 69 | + cargoValue = LinearExpressionSymbol(sum(cargos) { c -> c.value * x[c] }, "value") |
| 70 | + metaModel.addSymbol(cargoValue) |
| 71 | + |
| 72 | + cargoWeight = LinearExpressionSymbol(sum(cargos) { c -> c.weight * x[c] }, "weight") |
| 73 | + metaModel.addSymbol(cargoWeight) |
| 74 | + return ok |
| 75 | + } |
| 76 | + |
| 77 | + private suspend fun initObject(): Try { |
| 78 | + metaModel.maximize(LinearPolynomial(cargoValue),"value") |
| 79 | + return ok |
| 80 | + } |
| 81 | + |
| 82 | + private suspend fun initConstraint(): Try { |
| 83 | + metaModel.addConstraint( |
| 84 | + cargoWeight leq maxWeight,"weight" |
| 85 | + ) |
| 86 | + return ok |
| 87 | + } |
| 88 | + |
| 89 | + private suspend fun solve(): Try { |
| 90 | + val solver = SCIPLinearSolver() |
| 91 | + when (val ret = solver(metaModel)) { |
| 92 | + is Ok -> { |
| 93 | + metaModel.tokens.setSolution(ret.value.solution) |
| 94 | + } |
| 95 | + |
| 96 | + is Failed -> { |
| 97 | + return Failed(ret.error) |
| 98 | + } |
| 99 | + } |
| 100 | + return ok |
| 101 | + } |
| 102 | + |
| 103 | + private suspend fun analyzeSolution(): Try { |
| 104 | + val ret = HashSet<Cargo>() |
| 105 | + for (token in metaModel.tokens.tokens) { |
| 106 | + if (token.result!! eq Flt64.one |
| 107 | + && token.variable.belongsTo(x) |
| 108 | + ) { |
| 109 | + ret.add(cargos[token.variable.vectorView[0]]) |
| 110 | + } |
| 111 | + } |
| 112 | + return ok |
| 113 | + } |
| 114 | +} |
0 commit comments