|
| 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 Demo13 { |
| 16 | + data class Dealer( |
| 17 | + val demand: UInt64 |
| 18 | + ) : AutoIndexed(Dealer::class) |
| 19 | + |
| 20 | + data class DistributionCenter( |
| 21 | + val supply: UInt64, |
| 22 | + val distance: Map<Dealer, UInt64> |
| 23 | + ) : AutoIndexed(DistributionCenter::class) |
| 24 | + |
| 25 | + val carCapacity = UInt64(18) |
| 26 | + |
| 27 | + val dealers = listOf( |
| 28 | + Dealer(UInt64(100)), |
| 29 | + Dealer(UInt64(200)), |
| 30 | + Dealer(UInt64(150)), |
| 31 | + Dealer(UInt64(160)), |
| 32 | + Dealer(UInt64(140)) |
| 33 | + ) |
| 34 | + |
| 35 | + val distributionCenters = listOf( |
| 36 | + DistributionCenter( |
| 37 | + UInt64(400), mapOf( |
| 38 | + dealers[0] to UInt64(100), |
| 39 | + dealers[1] to UInt64(150), |
| 40 | + dealers[2] to UInt64(200), |
| 41 | + dealers[3] to UInt64(140), |
| 42 | + dealers[4] to UInt64(35) |
| 43 | + ) |
| 44 | + ), |
| 45 | + DistributionCenter( |
| 46 | + UInt64(200), mapOf( |
| 47 | + dealers[0] to UInt64(50), |
| 48 | + dealers[1] to UInt64(70), |
| 49 | + dealers[2] to UInt64(60), |
| 50 | + dealers[3] to UInt64(65), |
| 51 | + dealers[4] to UInt64(80) |
| 52 | + ) |
| 53 | + ), |
| 54 | + DistributionCenter( |
| 55 | + UInt64(150), mapOf( |
| 56 | + dealers[0] to UInt64(40), |
| 57 | + dealers[1] to UInt64(90), |
| 58 | + dealers[2] to UInt64(100), |
| 59 | + dealers[3] to UInt64(150), |
| 60 | + dealers[4] to UInt64(130) |
| 61 | + ) |
| 62 | + ) |
| 63 | + ) |
| 64 | + |
| 65 | + lateinit var x: UIntVariable2 |
| 66 | + lateinit var y: UIntVariable2 |
| 67 | + |
| 68 | + lateinit var trans: LinearSymbols1 |
| 69 | + lateinit var receive: LinearSymbols1 |
| 70 | + lateinit var cost: LinearSymbol |
| 71 | + |
| 72 | + val metaModel = LinearMetaModel("demo13") |
| 73 | + |
| 74 | + private val subProcesses = listOf( |
| 75 | + Demo13::initVariable, |
| 76 | + Demo13::initSymbol, |
| 77 | + Demo13::initObject, |
| 78 | + Demo13::initConstraint, |
| 79 | + Demo13::solve, |
| 80 | + Demo13::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 = UIntVariable2("x", Shape2(dealers.size, distributionCenters.size)) |
| 98 | + metaModel.add(x) |
| 99 | + y = UIntVariable2("y", Shape2(dealers.size, distributionCenters.size)) |
| 100 | + metaModel.add(y) |
| 101 | + return ok |
| 102 | + } |
| 103 | + |
| 104 | + private suspend fun initSymbol(): Try { |
| 105 | + trans = LinearSymbols1("trans", Shape1(distributionCenters.size)) { i, _ -> |
| 106 | + val distributionCenter = distributionCenters[i] |
| 107 | + LinearExpressionSymbol(sum(x[_a, distributionCenter]), "trans_${distributionCenter.index}") |
| 108 | + } |
| 109 | + metaModel.add(trans) |
| 110 | + receive = LinearSymbols1("receive", Shape1(dealers.size)) { i, _ -> |
| 111 | + val dealer = dealers[i] |
| 112 | + LinearExpressionSymbol(sum(x[dealer, _a]), "receive_${dealer.index}") |
| 113 | + } |
| 114 | + metaModel.add(receive) |
| 115 | + cost = LinearExpressionSymbol(sum(dealers.flatMap { dealer -> |
| 116 | + distributionCenters.mapNotNull { distributionCenter -> |
| 117 | + val distance = distributionCenter.distance[dealer] ?: return@mapNotNull null |
| 118 | + distance * y[dealer, distributionCenter] |
| 119 | + } |
| 120 | + }), "cost") |
| 121 | + metaModel.add(cost) |
| 122 | + return ok |
| 123 | + } |
| 124 | + |
| 125 | + private suspend fun initObject(): Try { |
| 126 | + metaModel.minimize(cost, "cost") |
| 127 | + return ok |
| 128 | + } |
| 129 | + |
| 130 | + private suspend fun initConstraint(): Try { |
| 131 | + for (distributionCenter in distributionCenters) { |
| 132 | + metaModel.addConstraint( |
| 133 | + trans[distributionCenter] leq distributionCenter.supply, |
| 134 | + "supply_${distributionCenter.index}" |
| 135 | + ) |
| 136 | + } |
| 137 | + for (dealer in dealers) { |
| 138 | + metaModel.addConstraint( |
| 139 | + receive[dealer] geq dealer.demand, |
| 140 | + "demand_${dealer.index}" |
| 141 | + ) |
| 142 | + } |
| 143 | + for (dealer in dealers) { |
| 144 | + for (distributionCenter in distributionCenters) { |
| 145 | + metaModel.addConstraint( |
| 146 | + x[dealer, distributionCenter] leq carCapacity * y[dealer, distributionCenter], |
| 147 | + "car_limit_${dealer.index}_${distributionCenter.index}" |
| 148 | + ) |
| 149 | + } |
| 150 | + } |
| 151 | + return ok |
| 152 | + } |
| 153 | + |
| 154 | + private suspend fun solve(): Try { |
| 155 | + val solver = ScipLinearSolver() |
| 156 | + when (val ret = solver(metaModel)) { |
| 157 | + is Ok -> { |
| 158 | + metaModel.tokens.setSolution(ret.value.solution) |
| 159 | + } |
| 160 | + |
| 161 | + is Failed -> { |
| 162 | + return Failed(ret.error) |
| 163 | + } |
| 164 | + } |
| 165 | + return ok |
| 166 | + } |
| 167 | + |
| 168 | + private suspend fun analyzeSolution(): Try { |
| 169 | + val trans: MutableMap<DistributionCenter, MutableMap<Dealer, UInt64>> = hashMapOf() |
| 170 | + for (token in metaModel.tokens.tokens) { |
| 171 | + if (token.result!! geq Flt64.one && token.variable belongsTo x) { |
| 172 | + val vector = token.variable.vectorView |
| 173 | + val dealer = dealers[vector[0]] |
| 174 | + val distributionCenter = distributionCenters[vector[1]] |
| 175 | + trans.getOrPut(distributionCenter) { hashMapOf() }[dealer] = token.result!!.round().toUInt64() |
| 176 | + } |
| 177 | + } |
| 178 | + return ok |
| 179 | + } |
| 180 | +} |
0 commit comments