Skip to content

Commit ec4be3e

Browse files
committed
feature: impl demos.
1 parent 59a3e8b commit ec4be3e

File tree

23 files changed

+1254
-24
lines changed

23 files changed

+1254
-24
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,4 @@ dist/
374374
package-lock.json
375375
Cargo.lock
376376
.VSCodeCounter/
377+
*.ilp

examples/ospf-kotlin-example/src/main/fuookami/ospf/kotlin/example/core_demo/Demo1.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@ data object Demo1 {
1919
val profit: Flt64
2020
) : AutoIndexed(Company::class)
2121

22-
private val companies: List<Company> = listOf(
22+
private val companies = listOf(
2323
Company(Flt64(3.48), Flt64(1.28), Flt64(5400.0)),
2424
Company(Flt64(5.62), Flt64(2.53), Flt64(2300.0)),
2525
Company(Flt64(7.33), Flt64(1.02), Flt64(4600.0)),
2626
Company(Flt64(6.27), Flt64(3.55), Flt64(3300.0)),
2727
Company(Flt64(2.14), Flt64(0.53), Flt64(980.0))
2828
)
29-
private val minCapital: Flt64 = Flt64(10.0)
30-
private val maxLiability: Flt64 = Flt64(5.0)
29+
private val minCapital = Flt64(10.0)
30+
private val maxLiability = Flt64(5.0)
3131

3232
private lateinit var x: BinVariable1
3333
private lateinit var capital: LinearExpressionSymbol
3434
private lateinit var liability: LinearExpressionSymbol
3535
private lateinit var profit: LinearExpressionSymbol
3636

37-
private val metaModel: LinearMetaModel = LinearMetaModel("demo1")
37+
private val metaModel = LinearMetaModel("demo1")
3838

3939
private val subProcesses = listOf(
4040
Demo1::initVariable,

examples/ospf-kotlin-example/src/main/fuookami/ospf/kotlin/example/core_demo/Demo10.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ data object Demo10 {
6262
lateinit var depart: LinearSymbols1
6363
lateinit var reached: LinearSymbols1
6464

65-
private val metaModel: LinearMetaModel = LinearMetaModel("demo10")
65+
private val metaModel = LinearMetaModel("demo10")
6666

6767
private val subProcesses = listOf(
6868
Demo10::initVariable,
@@ -95,7 +95,7 @@ data object Demo10 {
9595
if (city1 != city2) {
9696
metaModel.add(xi)
9797
} else {
98-
xi.range.eq(UInt8.zero)
98+
xi.range.eq(false)
9999
}
100100
}
101101
}

examples/ospf-kotlin-example/src/main/fuookami/ospf/kotlin/example/core_demo/Demo11.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ data object Demo11 {
6060
lateinit var flowIn: LinearSymbols1
6161
lateinit var flowOut: LinearSymbols1
6262

63-
val metaModel: LinearMetaModel = LinearMetaModel("demo11")
63+
val metaModel = LinearMetaModel("demo11")
6464

6565
private val subProcesses = listOf(
6666
Demo11::initVariable,

examples/ospf-kotlin-example/src/main/fuookami/ospf/kotlin/example/core_demo/Demo12.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ data object Demo12 {
3535
lateinit var premium: LinearSymbols1
3636
lateinit var yield: LinearSymbol
3737

38-
val metaModel: LinearMetaModel = LinearMetaModel("demo12")
38+
val metaModel = LinearMetaModel("demo12")
3939

4040
private val subProcesses = listOf(
4141
Demo12::initVariable,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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

Comments
 (0)