|
| 1 | +package fuookami.ospf.kotlin.example.core_demo |
| 2 | + |
| 3 | +import fuookami.ospf.kotlin.utils.math.* |
| 4 | +import fuookami.ospf.kotlin.utils.concept.* |
| 5 | +import fuookami.ospf.kotlin.utils.functional.* |
| 6 | +import fuookami.ospf.kotlin.utils.multi_array.* |
| 7 | +import fuookami.ospf.kotlin.core.frontend.variable.* |
| 8 | +import fuookami.ospf.kotlin.core.frontend.expression.monomial.* |
| 9 | +import fuookami.ospf.kotlin.core.frontend.expression.polynomial.* |
| 10 | +import fuookami.ospf.kotlin.core.frontend.expression.symbol.* |
| 11 | +import fuookami.ospf.kotlin.core.frontend.inequality.* |
| 12 | +import fuookami.ospf.kotlin.core.frontend.model.mechanism.* |
| 13 | +import fuookami.ospf.kotlin.core.backend.plugins.scip.* |
| 14 | + |
| 15 | +data object Demo8 { |
| 16 | + data class Product( |
| 17 | + val profit: Flt64 |
| 18 | + ) : AutoIndexed(Product::class) |
| 19 | + |
| 20 | + data class Equipment( |
| 21 | + val amount: UInt64, |
| 22 | + val manHours: Map<Product, Flt64> |
| 23 | + ) : AutoIndexed(Equipment::class) |
| 24 | + |
| 25 | + private val maxManHours = Flt64(2000) |
| 26 | + private val products = listOf( |
| 27 | + Product(Flt64(123.0)), |
| 28 | + Product(Flt64(94.0)), |
| 29 | + Product(Flt64(105.0)), |
| 30 | + Product(Flt64(132.0)), |
| 31 | + Product(Flt64(118.0)), |
| 32 | + ) |
| 33 | + private val equipments = listOf( |
| 34 | + Equipment( |
| 35 | + UInt64(12), mapOf( |
| 36 | + products[0] to Flt64(0.23), |
| 37 | + products[1] to Flt64(0.44), |
| 38 | + products[2] to Flt64(0.17), |
| 39 | + products[3] to Flt64(0.08), |
| 40 | + products[4] to Flt64(0.36), |
| 41 | + ) |
| 42 | + ), |
| 43 | + Equipment( |
| 44 | + UInt64(14), mapOf( |
| 45 | + products[0] to Flt64(0.13), |
| 46 | + products[2] to Flt64(0.20), |
| 47 | + products[3] to Flt64(0.37), |
| 48 | + products[4] to Flt64(0.19), |
| 49 | + ) |
| 50 | + ), |
| 51 | + Equipment( |
| 52 | + UInt64(8), mapOf( |
| 53 | + products[1] to Flt64(0.25), |
| 54 | + products[2] to Flt64(0.34), |
| 55 | + products[4] to Flt64(0.18), |
| 56 | + ) |
| 57 | + ), |
| 58 | + Equipment( |
| 59 | + UInt64(6), mapOf( |
| 60 | + products[0] to Flt64(0.55), |
| 61 | + products[1] to Flt64(0.72), |
| 62 | + products[3] to Flt64(0.61) |
| 63 | + ) |
| 64 | + ) |
| 65 | + ) |
| 66 | + |
| 67 | + private lateinit var x: UIntVariable1 |
| 68 | + |
| 69 | + private lateinit var profit: LinearSymbol |
| 70 | + private lateinit var manHours: LinearSymbols1 |
| 71 | + |
| 72 | + private val metaModel: LinearMetaModel = LinearMetaModel("demo8") |
| 73 | + |
| 74 | + private val subProcesses = listOf( |
| 75 | + Demo8::initVariable, |
| 76 | + Demo8::initSymbol, |
| 77 | + Demo8::initObject, |
| 78 | + Demo8::initConstraint, |
| 79 | + Demo8::solve, |
| 80 | + Demo8::analyzeSolution |
| 81 | + ) |
| 82 | + |
| 83 | + suspend operator fun invoke(): Try { |
| 84 | + for (process in subProcesses) { |
| 85 | + when (val result = process()) { |
| 86 | + is Ok -> {} |
| 87 | + |
| 88 | + is Failed -> { |
| 89 | + return Failed(result.error) |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + return ok |
| 94 | + } |
| 95 | + |
| 96 | + private suspend fun initVariable(): Try { |
| 97 | + x = UIntVariable1("x", Shape1(products.size)) |
| 98 | + for (p in products) { |
| 99 | + x[p].name = "${x.name}_${p.index}" |
| 100 | + } |
| 101 | + metaModel.add(x) |
| 102 | + return ok |
| 103 | + } |
| 104 | + |
| 105 | + private suspend fun initSymbol(): Try { |
| 106 | + profit = LinearExpressionSymbol(sum(products.map { p -> |
| 107 | + p.profit * x[p] |
| 108 | + }), "profit") |
| 109 | + metaModel.add(profit) |
| 110 | + |
| 111 | + manHours = LinearSymbols1( |
| 112 | + "man_hours", |
| 113 | + Shape1(equipments.size) |
| 114 | + ) { i, _ -> |
| 115 | + val e = equipments[i] |
| 116 | + LinearExpressionSymbol( |
| 117 | + sum(products.mapNotNull { p -> e.manHours[p]?.let { it * x[p] } }), |
| 118 | + "man_hours_${e.index}" |
| 119 | + ) |
| 120 | + } |
| 121 | + metaModel.add(manHours) |
| 122 | + |
| 123 | + return ok |
| 124 | + } |
| 125 | + |
| 126 | + private suspend fun initObject(): Try { |
| 127 | + metaModel.maximize(profit, "profit") |
| 128 | + return ok |
| 129 | + } |
| 130 | + |
| 131 | + private suspend fun initConstraint(): Try { |
| 132 | + for (e in equipments) { |
| 133 | + metaModel.addConstraint( |
| 134 | + manHours[e] leq e.amount.toFlt64() * maxManHours, |
| 135 | + "eq_man_hours_${e.index}" |
| 136 | + ) |
| 137 | + } |
| 138 | + return ok |
| 139 | + } |
| 140 | + |
| 141 | + private suspend fun solve(): Try { |
| 142 | + val solver = ScipLinearSolver() |
| 143 | + when (val ret = solver(metaModel)) { |
| 144 | + is Ok -> { |
| 145 | + metaModel.tokens.setSolution(ret.value.solution) |
| 146 | + } |
| 147 | + |
| 148 | + is Failed -> { |
| 149 | + return Failed(ret.error) |
| 150 | + } |
| 151 | + } |
| 152 | + return ok |
| 153 | + } |
| 154 | + |
| 155 | + private suspend fun analyzeSolution(): Try { |
| 156 | + val ret = HashMap<Product, UInt64>() |
| 157 | + for (token in metaModel.tokens.tokens) { |
| 158 | + if (token.result!! neq Flt64.one |
| 159 | + && token.variable.belongsTo(x) |
| 160 | + ) { |
| 161 | + ret[products[token.variable.vectorView[0]]] = token.result!!.round().toUInt64() |
| 162 | + } |
| 163 | + } |
| 164 | + return ok |
| 165 | + } |
| 166 | +} |
0 commit comments