|
| 1 | +package fuookami.ospf.kotlin.example.column_generation_demo.demo1 |
| 2 | + |
| 3 | +import java.util.* |
| 4 | +import fuookami.ospf.kotlin.utils.math.* |
| 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.cplex.* |
| 14 | +import fuookami.ospf.kotlin.core.backend.plugins.gurobi.* |
| 15 | +import fuookami.ospf.kotlin.framework.model.* |
| 16 | +import fuookami.ospf.kotlin.framework.solver.* |
| 17 | + |
| 18 | +data class ProductDemandShadowPriceKey( |
| 19 | + val product: Product |
| 20 | +) : ShadowPriceKey(ProductDemandShadowPriceKey::class) |
| 21 | + |
| 22 | +class RMP( |
| 23 | + private val length: UInt64, |
| 24 | + private val products: List<Product>, |
| 25 | + initialCuttingPlans: List<CuttingPlan> |
| 26 | +) { |
| 27 | + private val cuttingPlans: MutableList<CuttingPlan> = ArrayList() |
| 28 | + private val x: MutableList<UIntVar> = ArrayList() |
| 29 | + private val rest = LinearExpressionSymbol(MutableLinearPolynomial(), "rest") |
| 30 | + private val yield = LinearExpressionSymbols1("output", Shape1(products.size)) { v -> |
| 31 | + LinearExpressionSymbol(MutableLinearPolynomial(), "output_${v.second[0]}") |
| 32 | + } |
| 33 | + private val metaModel = LinearMetaModel("demo1") |
| 34 | + private val solver: ColumnGenerationSolver = |
| 35 | + if (System.getProperty("os.name").lowercase(Locale.getDefault()).contains("win")) { |
| 36 | + CplexColumnGenerationSolver() |
| 37 | + } else { |
| 38 | + GurobiColumnGenerationSolver() |
| 39 | + } |
| 40 | + |
| 41 | + init { |
| 42 | + metaModel.addSymbol(rest) |
| 43 | + metaModel.addSymbols(yield) |
| 44 | + |
| 45 | + metaModel.minimize(rest) |
| 46 | + metaModel.registerConstraintGroup("product_demand") |
| 47 | + |
| 48 | + for (product in products) { |
| 49 | + metaModel.addConstraint(yield[product] geq product.demand, "product_demand_${product.index}") |
| 50 | + } |
| 51 | + |
| 52 | + addColumns(initialCuttingPlans) |
| 53 | + } |
| 54 | + |
| 55 | + fun addColumn(cuttingPlan: CuttingPlan, flush: Boolean = true): Boolean { |
| 56 | + if (cuttingPlans.find { it.products == cuttingPlan.products } != null) { |
| 57 | + return false |
| 58 | + } |
| 59 | + |
| 60 | + cuttingPlans.add(cuttingPlan) |
| 61 | + val x = UIntVar("x_${cuttingPlan.index}") |
| 62 | + x.range.leq(cuttingPlan.products.maxOf { (product, amount) -> product.demand / amount + UInt64.one }) |
| 63 | + this.x.add(x) |
| 64 | + metaModel.addVar(x) |
| 65 | + |
| 66 | + rest.asMutable() += (length - cuttingPlan.products.sumOf { it.key.length * it.value }) * x |
| 67 | + rest.flush() |
| 68 | + for ((product, amount) in cuttingPlan.products) { |
| 69 | + yield[product].asMutable() += amount * x |
| 70 | + yield[product].flush() |
| 71 | + } |
| 72 | + if (flush) { |
| 73 | + metaModel.flush() |
| 74 | + } |
| 75 | + return true |
| 76 | + } |
| 77 | + |
| 78 | + fun addColumns(cuttingPlans: List<CuttingPlan>) { |
| 79 | + for (cuttingPlan in cuttingPlans) { |
| 80 | + addColumn(cuttingPlan, false) |
| 81 | + } |
| 82 | + metaModel.flush() |
| 83 | + } |
| 84 | + |
| 85 | + // solve lp |
| 86 | + suspend operator fun invoke(iteration: UInt64): Ret<SPM> { |
| 87 | + return when (val result = solver.solveLP("demo1-rmp-$iteration", metaModel, true)) { |
| 88 | + is Ok -> { |
| 89 | + Ok(extractShadowPriceMap(result.value.dualSolution)) |
| 90 | + } |
| 91 | + |
| 92 | + is Failed -> { |
| 93 | + Failed(result.error) |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // solve ip |
| 99 | + suspend operator fun invoke(): Ret<Map<CuttingPlan, UInt64>> { |
| 100 | + return when (val result = solver.solveMILP("demo1-rmp-ip", metaModel)) { |
| 101 | + is Ok -> { |
| 102 | + Ok(analyzeSolution(result.value.solution)) |
| 103 | + } |
| 104 | + |
| 105 | + is Failed -> { |
| 106 | + Failed(result.error) |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private fun extractShadowPriceMap(dualResult: List<Flt64>): SPM { |
| 112 | + val ret = SPM() |
| 113 | + |
| 114 | + for ((i, j) in metaModel.indicesOfConstraintGroup("product_demand")!!.withIndex()) { |
| 115 | + ret.put(ShadowPrice(ProductDemandShadowPriceKey(products[i]), dualResult[j])) |
| 116 | + } |
| 117 | + ret.put { map, args -> |
| 118 | + map.map[ProductDemandShadowPriceKey(args)]?.price ?: Flt64.zero |
| 119 | + } |
| 120 | + |
| 121 | + return ret |
| 122 | + } |
| 123 | + |
| 124 | + private fun analyzeSolution(result: List<Flt64>): Map<CuttingPlan, UInt64> { |
| 125 | + val solution = HashMap<CuttingPlan, UInt64>() |
| 126 | + for (token in metaModel.tokens.tokens) { |
| 127 | + if (result[token.solverIndex] geq Flt64.one) { |
| 128 | + for (i in cuttingPlans.indices) { |
| 129 | + if (token.variable.belongsTo(x[i])) { |
| 130 | + solution[cuttingPlans[i]] = result[token.solverIndex].toUInt64() |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + return solution |
| 136 | + } |
| 137 | +} |
0 commit comments