Skip to content

Commit 1f3c319

Browse files
committed
feature: add core demo3.
1 parent 570d8eb commit 1f3c319

31 files changed

+251
-88
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,15 @@ each OSPF implementation consists of the following components:
144144
<td>❗</td>
145145
</tr>
146146
<tr>
147-
<td>QMILP</td>
147+
<td>MIQCQP</td>
148148
<td>❌</td>
149149
<td>❌</td>
150150
<td>❗</td>
151151
<td>❌</td>
152152
<td>❌</td>
153153
</tr>
154154
<tr>
155-
<td>NLMILP</td>
155+
<td>MINLP</td>
156156
<td>❌</td>
157157
<td>❌</td>
158158
<td>❌</td>

README_ch.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,15 @@ ospf 采用内部<strong><em>领域特定语言</em></strong>(DSL) 的形式
143143
<td>❗</td>
144144
</tr>
145145
<tr>
146-
<td>QMILP</td>
146+
<td>MIQCQP</td>
147147
<td>❌</td>
148148
<td>❌</td>
149149
<td>❗</td>
150150
<td>❌</td>
151151
<td>❌</td>
152152
</tr>
153153
<tr>
154-
<td>NLMILP</td>
154+
<td>MINLP</td>
155155
<td>❌</td>
156156
<td>❌</td>
157157
<td>❌</td>

examples/ospf-kotlin-example/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>io.github.fuookami.ospf.kotlin</groupId>
77
<artifactId>ospf-kotlin-parent</artifactId>
8-
<version>1.0.4</version>
8+
<version>1.0.5</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

examples/ospf-kotlin-example/src/main/fuookami/ospf/kotlin/example/column_generation_demo/Demo.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package fuookami.ospf.kotlin.example.column_generation_demo
22

33
import fuookami.ospf.kotlin.utils.functional.*
44

5-
class Demo1 {
5+
data object Demo1 {
66
suspend operator fun invoke(): Try {
77
val demo = fuookami.ospf.kotlin.example.column_generation_demo.demo1.CSP()
88
return demo()

examples/ospf-kotlin-example/src/main/fuookami/ospf/kotlin/example/column_generation_demo/demo1/Main.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class CSP {
6363
})
6464
}
6565
}
66-
return Ok(success)
66+
return ok
6767
}
6868

6969
private fun reducedCost(cuttingPlan: CuttingPlan, shadowPrices: SPM) = Flt64.one -

examples/ospf-kotlin-example/src/main/fuookami/ospf/kotlin/example/column_generation_demo/demo1/RMP.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class RMP(
8484

8585
// solve lp
8686
suspend operator fun invoke(iteration: UInt64): Ret<SPM> {
87-
return when (val result = solver.solveLP("demo1-rmp-$iteration", metaModel, true)) {
87+
return when (val result = solver.solveLP("demo1-rmp-$iteration", metaModel)) {
8888
is Ok -> {
8989
Ok(extractShadowPriceMap(result.value.dualSolution))
9090
}

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

+23-25
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import fuookami.ospf.kotlin.core.frontend.inequality.*
1212
import fuookami.ospf.kotlin.core.frontend.model.mechanism.*
1313
import fuookami.ospf.kotlin.core.backend.plugins.scip.*
1414

15-
class Demo1 {
15+
data object Demo1 {
1616
data class Company(
1717
val capital: Flt64,
1818
val liability: Flt64,
@@ -30,16 +30,14 @@ class Demo1 {
3030

3131
private val metaModel: LinearMetaModel = LinearMetaModel("demo1")
3232

33-
companion object {
34-
val subProcesses = arrayListOf(
35-
Demo1::initVariable,
36-
Demo1::initSymbol,
37-
Demo1::initObject,
38-
Demo1::initConstraint,
39-
Demo1::solve,
40-
Demo1::analyzeSolution
41-
)
42-
}
33+
private val subProcesses = arrayListOf(
34+
Demo1::initVariable,
35+
Demo1::initSymbol,
36+
Demo1::initObject,
37+
Demo1::initConstraint,
38+
Demo1::solve,
39+
Demo1::analyzeSolution
40+
)
4341

4442
init {
4543
companies.add(Company(Flt64(3.48), Flt64(1.28), Flt64(5400.0)))
@@ -51,27 +49,27 @@ class Demo1 {
5149

5250
suspend operator fun invoke(): Try {
5351
for (process in subProcesses) {
54-
when (val result = process(this)) {
52+
when (val result = process()) {
5553
is Failed -> {
5654
return Failed(result.error)
5755
}
5856

5957
else -> {}
6058
}
6159
}
62-
return Ok(success)
60+
return ok
6361
}
6462

65-
suspend fun initVariable(): Try {
63+
private suspend fun initVariable(): Try {
6664
x = BinVariable1("x", Shape1(companies.size))
6765
for (c in companies) {
6866
x[c].name = "${x.name}_${c.index}"
6967
}
7068
metaModel.addVars(x)
71-
return Ok(success)
69+
return ok
7270
}
7371

74-
suspend fun initSymbol(): Try {
72+
private suspend fun initSymbol(): Try {
7573
capital = LinearExpressionSymbol(sum(companies) { it.capital * x[it] }, "capital")
7674
metaModel.addSymbol(capital)
7775

@@ -80,21 +78,21 @@ class Demo1 {
8078

8179
profit = LinearExpressionSymbol(sum(companies) { it.profit * x[it] }, "profit")
8280
metaModel.addSymbol(profit)
83-
return Ok(success)
81+
return ok
8482
}
8583

86-
suspend fun initObject(): Try {
84+
private suspend fun initObject(): Try {
8785
metaModel.maximize(profit)
88-
return Ok(success)
86+
return ok
8987
}
9088

91-
suspend fun initConstraint(): Try {
89+
private suspend fun initConstraint(): Try {
9290
metaModel.addConstraint(capital geq minCapital)
9391
metaModel.addConstraint(liability leq maxLiability)
94-
return Ok(success)
92+
return ok
9593
}
9694

97-
suspend fun solve(): Try {
95+
private suspend fun solve(): Try {
9896
val solver = SCIPLinearSolver()
9997
when (val ret = solver(metaModel)) {
10098
is Ok -> {
@@ -105,16 +103,16 @@ class Demo1 {
105103
return Failed(ret.error)
106104
}
107105
}
108-
return Ok(success)
106+
return ok
109107
}
110108

111-
suspend fun analyzeSolution(): Try {
109+
private suspend fun analyzeSolution(): Try {
112110
val ret = ArrayList<Company>()
113111
for (token in metaModel.tokens.tokens) {
114112
if (token.result!! eq Flt64.one) {
115113
ret.add(companies[token.variable.index])
116114
}
117115
}
118-
return Ok(success)
116+
return ok
119117
}
120118
}

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

+23-26
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ import fuookami.ospf.kotlin.core.frontend.expression.symbol.*
1111
import fuookami.ospf.kotlin.core.frontend.inequality.*
1212
import fuookami.ospf.kotlin.core.frontend.model.mechanism.*
1313
import fuookami.ospf.kotlin.core.backend.plugins.scip.*
14-
import fuookami.ospf.kotlin.core.backend.solver.config.*
1514

16-
class Demo2 {
15+
data object Demo2 {
1716
class Product : AutoIndexed(Product::class)
1817

1918
data class Company(
@@ -30,16 +29,14 @@ class Demo2 {
3029

3130
private val metaModel: LinearMetaModel = LinearMetaModel("demo2")
3231

33-
companion object {
34-
val subProcesses = arrayListOf(
35-
Demo2::initVariable,
36-
Demo2::initSymbol,
37-
Demo2::initObject,
38-
Demo2::initConstraint,
39-
Demo2::solve,
40-
Demo2::analyzeSolution
41-
)
42-
}
32+
private val subProcesses = arrayListOf(
33+
Demo2::initVariable,
34+
Demo2::initSymbol,
35+
Demo2::initObject,
36+
Demo2::initConstraint,
37+
Demo2::solve,
38+
Demo2::analyzeSolution
39+
)
4340

4441
init {
4542
products.add(Product())
@@ -94,29 +91,29 @@ class Demo2 {
9491

9592
suspend operator fun invoke(): Try {
9693
for (process in subProcesses) {
97-
when (val result = process(this)) {
94+
when (val result = process()) {
9895
is Failed -> {
9996
return Failed(result.error)
10097
}
10198

10299
else -> {}
103100
}
104101
}
105-
return Ok(success)
102+
return ok
106103
}
107104

108-
suspend fun initVariable(): Try {
105+
private suspend fun initVariable(): Try {
109106
x = BinVariable2("x", Shape2(companies.size, products.size))
110107
for (c in companies) {
111108
for (p in products) {
112109
x[c, p].name = "${x.name}_${c.index},${p.index}"
113110
}
114111
}
115112
metaModel.addVars(x)
116-
return Ok(success)
113+
return ok
117114
}
118115

119-
suspend fun initSymbol(): Try {
116+
private suspend fun initSymbol(): Try {
120117
cost = LinearExpressionSymbol(flatSum(companies) { c ->
121118
products.map { p ->
122119
c.cost[p]?.let { it * x[c, p] }
@@ -140,25 +137,25 @@ class Demo2 {
140137
)
141138
metaModel.addSymbols(assignmentProduct)
142139

143-
return Ok(success)
140+
return ok
144141
}
145142

146-
suspend fun initObject(): Try {
143+
private suspend fun initObject(): Try {
147144
metaModel.minimize(cost)
148-
return Ok(success)
145+
return ok
149146
}
150147

151-
suspend fun initConstraint(): Try {
148+
private suspend fun initConstraint(): Try {
152149
for (c in companies) {
153150
metaModel.addConstraint(assignmentCompany[c] leq 1)
154151
}
155152
for (p in products) {
156153
metaModel.addConstraint(assignmentProduct[p] eq 1)
157154
}
158-
return Ok(success)
155+
return ok
159156
}
160157

161-
suspend fun solve(): Try {
158+
private suspend fun solve(): Try {
162159
val solver = SCIPLinearSolver()
163160
when (val ret = solver(metaModel)) {
164161
is Ok -> {
@@ -169,10 +166,10 @@ class Demo2 {
169166
return Failed(ret.error)
170167
}
171168
}
172-
return Ok(success)
169+
return ok
173170
}
174171

175-
suspend fun analyzeSolution(): Try {
172+
private suspend fun analyzeSolution(): Try {
176173
val ret = ArrayList<Pair<Company, Product>>()
177174
for (token in metaModel.tokens.tokens) {
178175
if (token.result!! eq Flt64.one
@@ -181,6 +178,6 @@ class Demo2 {
181178
ret.add(Pair(companies[token.variable.vectorView[0]], products[token.variable.vectorView[1]]))
182179
}
183180
}
184-
return Ok(success)
181+
return ok
185182
}
186183
}

0 commit comments

Comments
 (0)