Skip to content

Commit e5ee373

Browse files
committed
feature: add core demo4.
1 parent 1f3c319 commit e5ee373

File tree

6 files changed

+247
-119
lines changed

6 files changed

+247
-119
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ ospf is designed and implemented using an internal <strong><em>Domain Specific L
8686

8787
### Shared Components
8888

89-
- <strong>[examples](examples)</strong>: examples demonstrating how to use OSPF for modeling and solving.
89+
- <strong>[examples](examples)</strong>: examples demonstrating how to use ospf for modeling and solving.
9090

9191
- <strong>[framework](framework)</strong>: a set of common components developed for specific problem domains, including visualization tools for results.
9292

9393
- <strong>[remote](remote)</strong>: a remote solver scheduler and server used to execute solvers on a server and retrieve results through a network interface.
9494

9595
### Components In Host Language Implementation
9696

97-
each OSPF implementation consists of the following components:
97+
each ospf implementation consists of the following components:
9898

9999
- <strong>utils</strong>: utilities containing classes and functions required for implementing ospf DSL.
100100
- <strong>core</strong>: core components containing essential functionalities such as modeling, solver interfaces, result processing, etc.

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

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

22-
private val companies: ArrayList<Company> = ArrayList()
22+
private val companies: List<Company> = listOf(
23+
Company(Flt64(3.48), Flt64(1.28), Flt64(5400.0)),
24+
Company(Flt64(5.62), Flt64(2.53), Flt64(2300.0)),
25+
Company(Flt64(7.33), Flt64(1.02), Flt64(4600.0)),
26+
Company(Flt64(6.27), Flt64(3.55), Flt64(3300.0)),
27+
Company(Flt64(2.14), Flt64(0.53), Flt64(980.0))
28+
)
2329
private val minCapital: Flt64 = Flt64(10.0)
2430
private val maxLiability: Flt64 = Flt64(5.0)
2531

26-
lateinit var x: BinVariable1
27-
lateinit var capital: LinearExpressionSymbol
28-
lateinit var liability: LinearExpressionSymbol
29-
lateinit var profit: LinearExpressionSymbol
32+
private lateinit var x: BinVariable1
33+
private lateinit var capital: LinearExpressionSymbol
34+
private lateinit var liability: LinearExpressionSymbol
35+
private lateinit var profit: LinearExpressionSymbol
3036

3137
private val metaModel: LinearMetaModel = LinearMetaModel("demo1")
3238

33-
private val subProcesses = arrayListOf(
39+
private val subProcesses = listOf(
3440
Demo1::initVariable,
3541
Demo1::initSymbol,
3642
Demo1::initObject,
@@ -39,14 +45,6 @@ data object Demo1 {
3945
Demo1::analyzeSolution
4046
)
4147

42-
init {
43-
companies.add(Company(Flt64(3.48), Flt64(1.28), Flt64(5400.0)))
44-
companies.add(Company(Flt64(5.62), Flt64(2.53), Flt64(2300.0)))
45-
companies.add(Company(Flt64(7.33), Flt64(1.02), Flt64(4600.0)))
46-
companies.add(Company(Flt64(6.27), Flt64(3.55), Flt64(3300.0)))
47-
companies.add(Company(Flt64(2.14), Flt64(0.53), Flt64(980.0)))
48-
}
49-
5048
suspend operator fun invoke(): Try {
5149
for (process in subProcesses) {
5250
when (val result = process()) {

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

+40-58
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,50 @@ data object Demo2 {
1919
val cost: Map<Product, Flt64>
2020
) : AutoIndexed(Company::class)
2121

22-
val products: ArrayList<Product> = ArrayList()
23-
val companies: ArrayList<Company> = ArrayList()
22+
private val products = listOf(Product(), Product(), Product(), Product())
23+
private val companies = listOf(
24+
Company(
25+
mapOf(
26+
products[0] to Flt64(920.0),
27+
products[1] to Flt64(480.0),
28+
products[2] to Flt64(650.0),
29+
products[3] to Flt64(340.0)
30+
)
31+
),
32+
Company(
33+
mapOf(
34+
products[0] to Flt64(870.0),
35+
products[1] to Flt64(510.0),
36+
products[2] to Flt64(700.0),
37+
products[3] to Flt64(350.0)
38+
)
39+
),
40+
Company(
41+
mapOf(
42+
products[0] to Flt64(880.0),
43+
products[1] to Flt64(500.0),
44+
products[2] to Flt64(720.0),
45+
products[3] to Flt64(400.0)
46+
)
47+
),
48+
Company(
49+
mapOf(
50+
products[0] to Flt64(930.0),
51+
products[1] to Flt64(490.0),
52+
products[2] to Flt64(680.0),
53+
products[3] to Flt64(410.0)
54+
)
55+
)
56+
)
2457

25-
lateinit var x: BinVariable2
26-
lateinit var cost: LinearExpressionSymbol
27-
lateinit var assignmentCompany: LinearExpressionSymbols1
28-
lateinit var assignmentProduct: LinearExpressionSymbols1
58+
private lateinit var x: BinVariable2
59+
private lateinit var cost: LinearExpressionSymbol
60+
private lateinit var assignmentCompany: LinearExpressionSymbols1
61+
private lateinit var assignmentProduct: LinearExpressionSymbols1
2962

3063
private val metaModel: LinearMetaModel = LinearMetaModel("demo2")
3164

32-
private val subProcesses = arrayListOf(
65+
private val subProcesses = listOf(
3366
Demo2::initVariable,
3467
Demo2::initSymbol,
3568
Demo2::initObject,
@@ -38,57 +71,6 @@ data object Demo2 {
3871
Demo2::analyzeSolution
3972
)
4073

41-
init {
42-
products.add(Product())
43-
products.add(Product())
44-
products.add(Product())
45-
products.add(Product())
46-
47-
companies.add(
48-
Company(
49-
mapOf(
50-
products[0] to Flt64(920.0),
51-
products[1] to Flt64(480.0),
52-
products[2] to Flt64(650.0),
53-
products[3] to Flt64(340.0)
54-
)
55-
)
56-
)
57-
58-
companies.add(
59-
Company(
60-
mapOf(
61-
products[0] to Flt64(870.0),
62-
products[1] to Flt64(510.0),
63-
products[2] to Flt64(700.0),
64-
products[3] to Flt64(350.0)
65-
)
66-
)
67-
)
68-
69-
companies.add(
70-
Company(
71-
mapOf(
72-
products[0] to Flt64(880.0),
73-
products[1] to Flt64(500.0),
74-
products[2] to Flt64(720.0),
75-
products[3] to Flt64(400.0)
76-
)
77-
)
78-
)
79-
80-
companies.add(
81-
Company(
82-
mapOf(
83-
products[0] to Flt64(930.0),
84-
products[1] to Flt64(490.0),
85-
products[2] to Flt64(680.0),
86-
products[3] to Flt64(410.0)
87-
)
88-
)
89-
)
90-
}
91-
9274
suspend operator fun invoke(): Try {
9375
for (process in subProcesses) {
9476
when (val result = process()) {

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

+33-45
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,38 @@ data object Demo3 {
2525
val yieldValue: Map<Product, Flt64>
2626
) : Indexed
2727

28-
private val products: ArrayList<Product> = ArrayList()
29-
private val materials: ArrayList<Material> = ArrayList()
28+
private val products = listOf(
29+
Product(0, Flt64(15000.0)),
30+
Product(1, Flt64(15000.0)),
31+
Product(2, Flt64(10000.0))
32+
)
33+
private val materials = listOf(
34+
Material(
35+
0, Flt64(115.0), mapOf(
36+
products[0] to Flt64(30.0),
37+
products[1] to Flt64(10.0)
38+
)
39+
),
40+
Material(
41+
1, Flt64(97.0), mapOf(
42+
products[0] to Flt64(15.0),
43+
products[2] to Flt64(20.0)
44+
)
45+
),
46+
Material(
47+
2, Flt64(82.0), mapOf(
48+
products[1] to Flt64(25.0),
49+
products[2] to Flt64(15.0)
50+
)
51+
),
52+
Material(
53+
3, Flt64(76.0), mapOf(
54+
products[0] to Flt64(15.0),
55+
products[1] to Flt64(15.0),
56+
products[2] to Flt64(15.0)
57+
)
58+
)
59+
)
3060

3161
private lateinit var x: UIntVariable1
3262

@@ -35,7 +65,7 @@ data object Demo3 {
3565

3666
private val metaModel: LinearMetaModel = LinearMetaModel("demo3")
3767

38-
private val subProcesses = arrayListOf(
68+
private val subProcesses = listOf(
3969
Demo3::initVariable,
4070
Demo3::initSymbol,
4171
Demo3::initObject,
@@ -44,46 +74,6 @@ data object Demo3 {
4474
Demo3::analyzeSolution
4575
)
4676

47-
init {
48-
products.add(Product(0, Flt64(15000.0)))
49-
products.add(Product(1, Flt64(15000.0)))
50-
products.add(Product(2, Flt64(10000.0)))
51-
52-
materials.add(
53-
Material(
54-
0, Flt64(115.0), mapOf(
55-
products[0] to Flt64(30.0),
56-
products[1] to Flt64(10.0)
57-
)
58-
)
59-
)
60-
materials.add(
61-
Material(
62-
1, Flt64(97.0), mapOf(
63-
products[0] to Flt64(15.0),
64-
products[2] to Flt64(20.0)
65-
)
66-
)
67-
)
68-
materials.add(
69-
Material(
70-
2, Flt64(82.0), mapOf(
71-
products[1] to Flt64(25.0),
72-
products[2] to Flt64(15.0)
73-
)
74-
)
75-
)
76-
materials.add(
77-
Material(
78-
3, Flt64(76.0), mapOf(
79-
products[0] to Flt64(15.0),
80-
products[1] to Flt64(15.0),
81-
products[2] to Flt64(15.0)
82-
)
83-
)
84-
)
85-
}
86-
8777
suspend operator fun invoke(): Try {
8878
for (process in subProcesses) {
8979
when (val result = process()) {
@@ -137,8 +127,6 @@ data object Demo3 {
137127
}
138128

139129
private suspend fun solve(): Try {
140-
metaModel.export("1.opm")
141-
142130
val solver = SCIPLinearSolver()
143131
when (val ret = solver(metaModel)) {
144132
is Ok -> {

0 commit comments

Comments
 (0)