Skip to content

Commit 57c05f4

Browse files
committed
feature: add core demo5.
1 parent e5ee373 commit 57c05f4

File tree

6 files changed

+143
-33
lines changed

6 files changed

+143
-33
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ data object Demo1 {
4848
suspend operator fun invoke(): Try {
4949
for (process in subProcesses) {
5050
when (val result = process()) {
51+
is Ok -> {}
52+
5153
is Failed -> {
5254
return Failed(result.error)
5355
}
54-
55-
else -> {}
5656
}
5757
}
5858
return ok

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ data object Demo2 {
7474
suspend operator fun invoke(): Try {
7575
for (process in subProcesses) {
7676
when (val result = process()) {
77+
is Ok -> {}
78+
7779
is Failed -> {
7880
return Failed(result.error)
7981
}
80-
81-
else -> {}
8282
}
8383
}
8484
return ok

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

+11-15
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,39 @@ import fuookami.ospf.kotlin.core.frontend.model.mechanism.*
1414
import fuookami.ospf.kotlin.core.backend.plugins.scip.*
1515

1616
data object Demo3 {
17-
data class Product(
18-
override val index: Int,
19-
val minYield: Flt64
20-
) : Indexed
17+
data class Product(val minYield: Flt64) : AutoIndexed(Product::class)
2118

2219
data class Material(
23-
override val index: Int,
2420
val cost: Flt64,
2521
val yieldValue: Map<Product, Flt64>
26-
) : Indexed
22+
) : AutoIndexed(Material::class)
2723

2824
private val products = listOf(
29-
Product(0, Flt64(15000.0)),
30-
Product(1, Flt64(15000.0)),
31-
Product(2, Flt64(10000.0))
25+
Product(Flt64(15000.0)),
26+
Product(Flt64(15000.0)),
27+
Product(Flt64(10000.0))
3228
)
3329
private val materials = listOf(
3430
Material(
35-
0, Flt64(115.0), mapOf(
31+
Flt64(115.0), mapOf(
3632
products[0] to Flt64(30.0),
3733
products[1] to Flt64(10.0)
3834
)
3935
),
4036
Material(
41-
1, Flt64(97.0), mapOf(
37+
Flt64(97.0), mapOf(
4238
products[0] to Flt64(15.0),
4339
products[2] to Flt64(20.0)
4440
)
4541
),
4642
Material(
47-
2, Flt64(82.0), mapOf(
43+
Flt64(82.0), mapOf(
4844
products[1] to Flt64(25.0),
4945
products[2] to Flt64(15.0)
5046
)
5147
),
5248
Material(
53-
3, Flt64(76.0), mapOf(
49+
Flt64(76.0), mapOf(
5450
products[0] to Flt64(15.0),
5551
products[1] to Flt64(15.0),
5652
products[2] to Flt64(15.0)
@@ -77,11 +73,11 @@ data object Demo3 {
7773
suspend operator fun invoke(): Try {
7874
for (process in subProcesses) {
7975
when (val result = process()) {
76+
is Ok -> {}
77+
8078
is Failed -> {
8179
return Failed(result.error)
8280
}
83-
84-
else -> {}
8581
}
8682
}
8783
return ok

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

+9-14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package fuookami.ospf.kotlin.example.core_demo
22

3+
import fuookami.ospf.kotlin.utils.math.*
34
import fuookami.ospf.kotlin.utils.concept.*
4-
import fuookami.ospf.kotlin.utils.error.*
55
import fuookami.ospf.kotlin.utils.functional.*
66
import fuookami.ospf.kotlin.utils.multi_array.*
7-
import fuookami.ospf.kotlin.utils.math.*
87
import fuookami.ospf.kotlin.core.frontend.variable.*
98
import fuookami.ospf.kotlin.core.frontend.expression.monomial.*
109
import fuookami.ospf.kotlin.core.frontend.expression.polynomial.*
@@ -14,31 +13,27 @@ import fuookami.ospf.kotlin.core.frontend.model.mechanism.*
1413
import fuookami.ospf.kotlin.core.backend.plugins.scip.*
1514

1615
data object Demo4 {
17-
data class Material(
18-
override val index: Int,
19-
val available: Flt64
20-
) : Indexed
16+
data class Material(val available: Flt64) : AutoIndexed(Material::class)
2117

2218
data class Product(
23-
override val index: Int,
2419
val profit: Flt64,
2520
val maxYield: Flt64,
2621
val use: Map<Material, Flt64>
27-
) : Indexed
22+
) : AutoIndexed(Product::class)
2823

2924
private val materials = listOf(
30-
Material(0, Flt64(24.0)),
31-
Material(1, Flt64(8.0))
25+
Material(Flt64(24.0)),
26+
Material(Flt64(8.0))
3227
)
3328
private val products = listOf(
3429
Product(
35-
0, Flt64(5.0), Flt64(3.0), mapOf(
30+
Flt64(5.0), Flt64(3.0), mapOf(
3631
materials[0] to Flt64(6.0),
3732
materials[1] to Flt64(1.0),
3833
)
3934
),
4035
Product(
41-
1, Flt64(4.0), Flt64(2.0), mapOf(
36+
Flt64(4.0), Flt64(2.0), mapOf(
4237
materials[0] to Flt64(4.0),
4338
materials[1] to Flt64(2.0),
4439
)
@@ -65,11 +60,11 @@ data object Demo4 {
6560
suspend operator fun invoke(): Try {
6661
for (process in subProcesses) {
6762
when (val result = process()) {
63+
is Ok -> {}
64+
6865
is Failed -> {
6966
return Failed(result.error)
7067
}
71-
72-
else -> {}
7368
}
7469
}
7570
return ok
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
}

examples/ospf-kotlin-example/src/test/fuookami/ospf/kotlin/example/CoreDemoTest.kt

+5
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,9 @@ class CoreDemoTest {
2424
fun runDemo4() {
2525
assert(runBlocking { Demo4().ok })
2626
}
27+
28+
@Test
29+
fun runDemo5() {
30+
assert(runBlocking { Demo5().ok })
31+
}
2732
}

0 commit comments

Comments
 (0)