moirspy 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. moirspy-0.0.1/Cargo.lock +441 -0
  2. moirspy-0.0.1/Cargo.toml +19 -0
  3. moirspy-0.0.1/PKG-INFO +7 -0
  4. moirspy-0.0.1/crates/moi-bridge/Cargo.toml +13 -0
  5. moirspy-0.0.1/crates/moi-bridge/src/lib.rs +4 -0
  6. moirspy-0.0.1/crates/moi-bridge/src/optimizer.rs +206 -0
  7. moirspy-0.0.1/crates/moi-core/Cargo.toml +12 -0
  8. moirspy-0.0.1/crates/moi-core/src/attributes.rs +111 -0
  9. moirspy-0.0.1/crates/moi-core/src/constraint.rs +21 -0
  10. moirspy-0.0.1/crates/moi-core/src/errors.rs +22 -0
  11. moirspy-0.0.1/crates/moi-core/src/functions/affine.rs +97 -0
  12. moirspy-0.0.1/crates/moi-core/src/functions/function.rs +26 -0
  13. moirspy-0.0.1/crates/moi-core/src/functions/mod.rs +5 -0
  14. moirspy-0.0.1/crates/moi-core/src/indices.rs +36 -0
  15. moirspy-0.0.1/crates/moi-core/src/lib.rs +15 -0
  16. moirspy-0.0.1/crates/moi-core/src/sets.rs +21 -0
  17. moirspy-0.0.1/crates/moi-core/src/variable.rs +7 -0
  18. moirspy-0.0.1/crates/moi-solver-api/Cargo.toml +10 -0
  19. moirspy-0.0.1/crates/moi-solver-api/src/lib.rs +7 -0
  20. moirspy-0.0.1/crates/moi-solver-api/src/optimizer.rs +71 -0
  21. moirspy-0.0.1/crates/moi-solver-api/src/status.rs +1 -0
  22. moirspy-0.0.1/crates/moi-solver-api/src/utils.rs +10 -0
  23. moirspy-0.0.1/pyproject.toml +18 -0
  24. moirspy-0.0.1/python/moirspy/.gitignore +72 -0
  25. moirspy-0.0.1/python/moirspy/Cargo.toml +18 -0
  26. moirspy-0.0.1/python/moirspy/moirspy.pyi +52 -0
  27. moirspy-0.0.1/python/moirspy/src/constr.rs +23 -0
  28. moirspy-0.0.1/python/moirspy/src/env.rs +3 -0
  29. moirspy-0.0.1/python/moirspy/src/expr.rs +169 -0
  30. moirspy-0.0.1/python/moirspy/src/function.rs +24 -0
  31. moirspy-0.0.1/python/moirspy/src/lib.rs +33 -0
  32. moirspy-0.0.1/python/moirspy/src/model.rs +259 -0
  33. moirspy-0.0.1/python/moirspy/src/moi.rs +38 -0
  34. moirspy-0.0.1/python/moirspy/src/utils.rs +82 -0
  35. moirspy-0.0.1/python/moirspy/src/var.rs +278 -0
  36. moirspy-0.0.1/python/moirspy/tests/test_model.py +58 -0
  37. moirspy-0.0.1/python/moirspy/tests/test_moi.py +12 -0
  38. moirspy-0.0.1/python/moirspy/tests/test_var.py +31 -0
@@ -0,0 +1,441 @@
1
+ # This file is automatically @generated by Cargo.
2
+ # It is not intended for manual editing.
3
+ version = 4
4
+
5
+ [[package]]
6
+ name = "autocfg"
7
+ version = "1.5.0"
8
+ source = "registry+https://github.com/rust-lang/crates.io-index"
9
+ checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
10
+
11
+ [[package]]
12
+ name = "bincode"
13
+ version = "2.0.1"
14
+ source = "registry+https://github.com/rust-lang/crates.io-index"
15
+ checksum = "36eaf5d7b090263e8150820482d5d93cd964a81e4019913c972f4edcc6edb740"
16
+ dependencies = [
17
+ "bincode_derive",
18
+ "serde",
19
+ "unty",
20
+ ]
21
+
22
+ [[package]]
23
+ name = "bincode_derive"
24
+ version = "2.0.1"
25
+ source = "registry+https://github.com/rust-lang/crates.io-index"
26
+ checksum = "bf95709a440f45e986983918d0e8a1f30a9b1df04918fc828670606804ac3c09"
27
+ dependencies = [
28
+ "virtue",
29
+ ]
30
+
31
+ [[package]]
32
+ name = "cfg-if"
33
+ version = "1.0.4"
34
+ source = "registry+https://github.com/rust-lang/crates.io-index"
35
+ checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
36
+
37
+ [[package]]
38
+ name = "heck"
39
+ version = "0.5.0"
40
+ source = "registry+https://github.com/rust-lang/crates.io-index"
41
+ checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
42
+
43
+ [[package]]
44
+ name = "indoc"
45
+ version = "2.0.7"
46
+ source = "registry+https://github.com/rust-lang/crates.io-index"
47
+ checksum = "79cf5c93f93228cf8efb3ba362535fb11199ac548a09ce117c9b1adc3030d706"
48
+ dependencies = [
49
+ "rustversion",
50
+ ]
51
+
52
+ [[package]]
53
+ name = "libc"
54
+ version = "0.2.183"
55
+ source = "registry+https://github.com/rust-lang/crates.io-index"
56
+ checksum = "b5b646652bf6661599e1da8901b3b9522896f01e736bad5f723fe7a3a27f899d"
57
+
58
+ [[package]]
59
+ name = "libloading"
60
+ version = "0.9.0"
61
+ source = "registry+https://github.com/rust-lang/crates.io-index"
62
+ checksum = "754ca22de805bb5744484a5b151a9e1a8e837d5dc232c2d7d8c2e3492edc8b60"
63
+ dependencies = [
64
+ "cfg-if",
65
+ "windows-link",
66
+ ]
67
+
68
+ [[package]]
69
+ name = "matrixmultiply"
70
+ version = "0.3.10"
71
+ source = "registry+https://github.com/rust-lang/crates.io-index"
72
+ checksum = "a06de3016e9fae57a36fd14dba131fccf49f74b40b7fbdb472f96e361ec71a08"
73
+ dependencies = [
74
+ "autocfg",
75
+ "rawpointer",
76
+ ]
77
+
78
+ [[package]]
79
+ name = "memoffset"
80
+ version = "0.9.1"
81
+ source = "registry+https://github.com/rust-lang/crates.io-index"
82
+ checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a"
83
+ dependencies = [
84
+ "autocfg",
85
+ ]
86
+
87
+ [[package]]
88
+ name = "moi-bridge"
89
+ version = "0.0.1"
90
+ dependencies = [
91
+ "bincode",
92
+ "moi-core",
93
+ "moi-solver-api",
94
+ "serde",
95
+ ]
96
+
97
+ [[package]]
98
+ name = "moi-cache"
99
+ version = "0.0.1"
100
+
101
+ [[package]]
102
+ name = "moi-core"
103
+ version = "0.0.1"
104
+ dependencies = [
105
+ "bincode",
106
+ "serde",
107
+ "thiserror",
108
+ ]
109
+
110
+ [[package]]
111
+ name = "moi-expr"
112
+ version = "0.0.1"
113
+
114
+ [[package]]
115
+ name = "moi-model-dummy"
116
+ version = "0.0.1"
117
+ dependencies = [
118
+ "moi-core",
119
+ "moi-solver-api",
120
+ ]
121
+
122
+ [[package]]
123
+ name = "moi-nlp"
124
+ version = "0.0.1"
125
+
126
+ [[package]]
127
+ name = "moi-solver-api"
128
+ version = "0.0.1"
129
+ dependencies = [
130
+ "moi-core",
131
+ ]
132
+
133
+ [[package]]
134
+ name = "moi-solver-gurobi"
135
+ version = "0.0.1"
136
+ dependencies = [
137
+ "libloading",
138
+ "moi-bridge",
139
+ "moi-core",
140
+ "moi-solver-api",
141
+ ]
142
+
143
+ [[package]]
144
+ name = "moi-utils"
145
+ version = "0.0.1"
146
+
147
+ [[package]]
148
+ name = "moirspy"
149
+ version = "0.0.1"
150
+ dependencies = [
151
+ "bincode",
152
+ "moi-bridge",
153
+ "moi-core",
154
+ "moi-solver-api",
155
+ "numpy",
156
+ "pyo3",
157
+ ]
158
+
159
+ [[package]]
160
+ name = "moirspy-gurobi"
161
+ version = "0.0.1"
162
+ dependencies = [
163
+ "bincode",
164
+ "moi-bridge",
165
+ "moi-solver-api",
166
+ "moi-solver-gurobi",
167
+ "pyo3",
168
+ ]
169
+
170
+ [[package]]
171
+ name = "ndarray"
172
+ version = "0.17.2"
173
+ source = "registry+https://github.com/rust-lang/crates.io-index"
174
+ checksum = "520080814a7a6b4a6e9070823bb24b4531daac8c4627e08ba5de8c5ef2f2752d"
175
+ dependencies = [
176
+ "matrixmultiply",
177
+ "num-complex",
178
+ "num-integer",
179
+ "num-traits",
180
+ "portable-atomic",
181
+ "portable-atomic-util",
182
+ "rawpointer",
183
+ ]
184
+
185
+ [[package]]
186
+ name = "num-complex"
187
+ version = "0.4.6"
188
+ source = "registry+https://github.com/rust-lang/crates.io-index"
189
+ checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495"
190
+ dependencies = [
191
+ "num-traits",
192
+ ]
193
+
194
+ [[package]]
195
+ name = "num-integer"
196
+ version = "0.1.46"
197
+ source = "registry+https://github.com/rust-lang/crates.io-index"
198
+ checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"
199
+ dependencies = [
200
+ "num-traits",
201
+ ]
202
+
203
+ [[package]]
204
+ name = "num-traits"
205
+ version = "0.2.19"
206
+ source = "registry+https://github.com/rust-lang/crates.io-index"
207
+ checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
208
+ dependencies = [
209
+ "autocfg",
210
+ ]
211
+
212
+ [[package]]
213
+ name = "numpy"
214
+ version = "0.27.1"
215
+ source = "registry+https://github.com/rust-lang/crates.io-index"
216
+ checksum = "7aac2e6a6e4468ffa092ad43c39b81c79196c2bb773b8db4085f695efe3bba17"
217
+ dependencies = [
218
+ "libc",
219
+ "ndarray",
220
+ "num-complex",
221
+ "num-integer",
222
+ "num-traits",
223
+ "pyo3",
224
+ "pyo3-build-config",
225
+ "rustc-hash",
226
+ ]
227
+
228
+ [[package]]
229
+ name = "once_cell"
230
+ version = "1.21.3"
231
+ source = "registry+https://github.com/rust-lang/crates.io-index"
232
+ checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
233
+
234
+ [[package]]
235
+ name = "portable-atomic"
236
+ version = "1.13.1"
237
+ source = "registry+https://github.com/rust-lang/crates.io-index"
238
+ checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49"
239
+
240
+ [[package]]
241
+ name = "portable-atomic-util"
242
+ version = "0.2.5"
243
+ source = "registry+https://github.com/rust-lang/crates.io-index"
244
+ checksum = "7a9db96d7fa8782dd8c15ce32ffe8680bbd1e978a43bf51a34d39483540495f5"
245
+ dependencies = [
246
+ "portable-atomic",
247
+ ]
248
+
249
+ [[package]]
250
+ name = "proc-macro2"
251
+ version = "1.0.106"
252
+ source = "registry+https://github.com/rust-lang/crates.io-index"
253
+ checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
254
+ dependencies = [
255
+ "unicode-ident",
256
+ ]
257
+
258
+ [[package]]
259
+ name = "pyo3"
260
+ version = "0.27.2"
261
+ source = "registry+https://github.com/rust-lang/crates.io-index"
262
+ checksum = "ab53c047fcd1a1d2a8820fe84f05d6be69e9526be40cb03b73f86b6b03e6d87d"
263
+ dependencies = [
264
+ "indoc",
265
+ "libc",
266
+ "memoffset",
267
+ "once_cell",
268
+ "portable-atomic",
269
+ "pyo3-build-config",
270
+ "pyo3-ffi",
271
+ "pyo3-macros",
272
+ "unindent",
273
+ ]
274
+
275
+ [[package]]
276
+ name = "pyo3-build-config"
277
+ version = "0.27.2"
278
+ source = "registry+https://github.com/rust-lang/crates.io-index"
279
+ checksum = "b455933107de8642b4487ed26d912c2d899dec6114884214a0b3bb3be9261ea6"
280
+ dependencies = [
281
+ "target-lexicon",
282
+ ]
283
+
284
+ [[package]]
285
+ name = "pyo3-ffi"
286
+ version = "0.27.2"
287
+ source = "registry+https://github.com/rust-lang/crates.io-index"
288
+ checksum = "1c85c9cbfaddf651b1221594209aed57e9e5cff63c4d11d1feead529b872a089"
289
+ dependencies = [
290
+ "libc",
291
+ "pyo3-build-config",
292
+ ]
293
+
294
+ [[package]]
295
+ name = "pyo3-macros"
296
+ version = "0.27.2"
297
+ source = "registry+https://github.com/rust-lang/crates.io-index"
298
+ checksum = "0a5b10c9bf9888125d917fb4d2ca2d25c8df94c7ab5a52e13313a07e050a3b02"
299
+ dependencies = [
300
+ "proc-macro2",
301
+ "pyo3-macros-backend",
302
+ "quote",
303
+ "syn",
304
+ ]
305
+
306
+ [[package]]
307
+ name = "pyo3-macros-backend"
308
+ version = "0.27.2"
309
+ source = "registry+https://github.com/rust-lang/crates.io-index"
310
+ checksum = "03b51720d314836e53327f5871d4c0cfb4fb37cc2c4a11cc71907a86342c40f9"
311
+ dependencies = [
312
+ "heck",
313
+ "proc-macro2",
314
+ "pyo3-build-config",
315
+ "quote",
316
+ "syn",
317
+ ]
318
+
319
+ [[package]]
320
+ name = "quote"
321
+ version = "1.0.45"
322
+ source = "registry+https://github.com/rust-lang/crates.io-index"
323
+ checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
324
+ dependencies = [
325
+ "proc-macro2",
326
+ ]
327
+
328
+ [[package]]
329
+ name = "rawpointer"
330
+ version = "0.2.1"
331
+ source = "registry+https://github.com/rust-lang/crates.io-index"
332
+ checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3"
333
+
334
+ [[package]]
335
+ name = "rustc-hash"
336
+ version = "2.1.1"
337
+ source = "registry+https://github.com/rust-lang/crates.io-index"
338
+ checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d"
339
+
340
+ [[package]]
341
+ name = "rustversion"
342
+ version = "1.0.22"
343
+ source = "registry+https://github.com/rust-lang/crates.io-index"
344
+ checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
345
+
346
+ [[package]]
347
+ name = "serde"
348
+ version = "1.0.228"
349
+ source = "registry+https://github.com/rust-lang/crates.io-index"
350
+ checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
351
+ dependencies = [
352
+ "serde_core",
353
+ "serde_derive",
354
+ ]
355
+
356
+ [[package]]
357
+ name = "serde_core"
358
+ version = "1.0.228"
359
+ source = "registry+https://github.com/rust-lang/crates.io-index"
360
+ checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
361
+ dependencies = [
362
+ "serde_derive",
363
+ ]
364
+
365
+ [[package]]
366
+ name = "serde_derive"
367
+ version = "1.0.228"
368
+ source = "registry+https://github.com/rust-lang/crates.io-index"
369
+ checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
370
+ dependencies = [
371
+ "proc-macro2",
372
+ "quote",
373
+ "syn",
374
+ ]
375
+
376
+ [[package]]
377
+ name = "syn"
378
+ version = "2.0.117"
379
+ source = "registry+https://github.com/rust-lang/crates.io-index"
380
+ checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99"
381
+ dependencies = [
382
+ "proc-macro2",
383
+ "quote",
384
+ "unicode-ident",
385
+ ]
386
+
387
+ [[package]]
388
+ name = "target-lexicon"
389
+ version = "0.13.5"
390
+ source = "registry+https://github.com/rust-lang/crates.io-index"
391
+ checksum = "adb6935a6f5c20170eeceb1a3835a49e12e19d792f6dd344ccc76a985ca5a6ca"
392
+
393
+ [[package]]
394
+ name = "thiserror"
395
+ version = "2.0.18"
396
+ source = "registry+https://github.com/rust-lang/crates.io-index"
397
+ checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4"
398
+ dependencies = [
399
+ "thiserror-impl",
400
+ ]
401
+
402
+ [[package]]
403
+ name = "thiserror-impl"
404
+ version = "2.0.18"
405
+ source = "registry+https://github.com/rust-lang/crates.io-index"
406
+ checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5"
407
+ dependencies = [
408
+ "proc-macro2",
409
+ "quote",
410
+ "syn",
411
+ ]
412
+
413
+ [[package]]
414
+ name = "unicode-ident"
415
+ version = "1.0.24"
416
+ source = "registry+https://github.com/rust-lang/crates.io-index"
417
+ checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
418
+
419
+ [[package]]
420
+ name = "unindent"
421
+ version = "0.2.4"
422
+ source = "registry+https://github.com/rust-lang/crates.io-index"
423
+ checksum = "7264e107f553ccae879d21fbea1d6724ac785e8c3bfc762137959b5802826ef3"
424
+
425
+ [[package]]
426
+ name = "unty"
427
+ version = "0.0.4"
428
+ source = "registry+https://github.com/rust-lang/crates.io-index"
429
+ checksum = "6d49784317cd0d1ee7ec5c716dd598ec5b4483ea832a2dced265471cc0f690ae"
430
+
431
+ [[package]]
432
+ name = "virtue"
433
+ version = "0.0.18"
434
+ source = "registry+https://github.com/rust-lang/crates.io-index"
435
+ checksum = "051eb1abcf10076295e815102942cc58f9d5e3b4560e46e53c21e8ff6f3af7b1"
436
+
437
+ [[package]]
438
+ name = "windows-link"
439
+ version = "0.2.1"
440
+ source = "registry+https://github.com/rust-lang/crates.io-index"
441
+ checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
@@ -0,0 +1,19 @@
1
+ [workspace]
2
+ members = ["crates/*", "python/*"]
3
+ resolver = "2"
4
+
5
+ [workspace.package]
6
+ version = "0.0.1"
7
+ edition = "2024"
8
+ authors = ["Yuzheng Liu <15956386@stu.xjtu.edu.cn>"]
9
+ license = "MIT"
10
+
11
+ [workspace.dependencies]
12
+ # 在这里统一声明依赖版本,子 crate 就不需要写版本号了
13
+ pyo3 = { version = "0.27.2", features = ["extension-module", "abi3-py38"] }
14
+ libloading = "0.8"
15
+ # 内部依赖也在这里声明
16
+ moi-core = { path = "crates/moi-core" }
17
+ moi-solver-api = { path = "crates/moi-solver-api" }
18
+ moi-bridge = { path = "crates/moi-bridge" }
19
+ moi-solver-gurobi = { path = "crates/moi-solver-gurobi" }
moirspy-0.0.1/PKG-INFO ADDED
@@ -0,0 +1,7 @@
1
+ Metadata-Version: 2.4
2
+ Name: moirspy
3
+ Version: 0.0.1
4
+ Classifier: Programming Language :: Rust
5
+ Classifier: Programming Language :: Python :: Implementation :: CPython
6
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
7
+ Requires-Python: >=3.8
@@ -0,0 +1,13 @@
1
+ [package]
2
+ name = "moi-bridge"
3
+ # 继承工作区的通用配置
4
+ version.workspace = true
5
+ edition.workspace = true
6
+ authors.workspace = true
7
+ license.workspace = true
8
+
9
+ [dependencies]
10
+ moi-core.workspace = true
11
+ moi-solver-api.workspace = true
12
+ serde ={ version = "1.0.228", features = ["derive"] }
13
+ bincode = "2.0.1"
@@ -0,0 +1,4 @@
1
+ //! Bridge framework (MVP)
2
+
3
+ pub mod optimizer;
4
+ pub use optimizer::BridgeOptimizer;
@@ -0,0 +1,206 @@
1
+ use bincode::{Decode, Encode};
2
+ use moi_core::attributes::{AttrValue, ModelAttr};
3
+ use moi_core::*;
4
+ use moi_solver_api::*;
5
+ use serde::{Deserialize, Serialize};
6
+ use std::collections::HashMap;
7
+ /// Refactored BridgeOptimizer to hold model state.
8
+ #[derive(Clone, Debug, Serialize, Deserialize, Encode, Decode)]
9
+ pub struct BridgeOptimizer {
10
+ pub vars: Vec<VarInfo>,
11
+ pub constrs: HashMap<ConstrId, ConstrInfo>,
12
+ pub obj: Option<ScalarFunctionType>,
13
+ pub sense: Option<ModelSense>,
14
+ pub objval: Option<f64>,
15
+ pub needs_update: bool,
16
+ }
17
+
18
+ impl BridgeOptimizer {
19
+ pub fn new() -> Self {
20
+ Self {
21
+ vars: Vec::new(),
22
+ constrs: HashMap::new(),
23
+ obj: None,
24
+ sense: None,
25
+ objval: None,
26
+ needs_update: false,
27
+ }
28
+ }
29
+
30
+ pub fn is_empty(&self) -> bool {
31
+ self.vars.is_empty() && self.constrs.is_empty()
32
+ }
33
+
34
+ pub fn empty(&mut self) {
35
+ self.vars.clear();
36
+ self.constrs.clear();
37
+ self.obj = None;
38
+ self.sense = None;
39
+ self.objval = None;
40
+ self.needs_update = false;
41
+ }
42
+
43
+ pub fn get_var_name_by_id(&self, id: VarId) -> Option<String> {
44
+ self.vars.get(id.0).map(|var| var.name.clone())
45
+ }
46
+
47
+ pub fn get_value_by_var_id(&self, id: VarId) -> Option<f64> {
48
+ self.vars.get(id.0).and_then(|var| var.value)
49
+ }
50
+ }
51
+
52
+ impl BridgeOptimizer {
53
+ /// Explicit API: add an affine scalar-bound constraint.
54
+ /// This converts the bound to a standard constraint and adds it.
55
+ pub fn add_affine_bound(&mut self, f: ScalarAffineFn, s: ScalarSetType) -> ConstrId {
56
+ let fty = ScalarFunctionType::Affine(f);
57
+ self.add_constraint(fty, s, None)
58
+ }
59
+ }
60
+
61
+ impl ModelLike for BridgeOptimizer {
62
+ fn add_variable(
63
+ &mut self,
64
+ name: Option<&str>,
65
+ vtype: Option<char>,
66
+ lb: Option<f64>,
67
+ ub: Option<f64>,
68
+ ) -> VarId {
69
+ let var_id = self.vars.len();
70
+ self.vars.push(VarInfo {
71
+ col_index: var_id,
72
+ lb: lb.unwrap_or(0.0),
73
+ ub: ub.unwrap_or(f64::INFINITY),
74
+ vtype: vtype.unwrap_or('C'),
75
+ name: name.unwrap_or("").to_string(),
76
+ value: None,
77
+ });
78
+ self.needs_update = true;
79
+ VarId(var_id)
80
+ }
81
+
82
+ fn add_variables(
83
+ &mut self,
84
+ n: usize,
85
+ name: Option<NameType>,
86
+ vtype: Option<Vec<char>>,
87
+ lb: Option<BoundType>,
88
+ ub: Option<BoundType>,
89
+ ) -> Vec<VarId> {
90
+ let start_id = self.vars.len();
91
+ let lb_vec = match lb {
92
+ Some(BoundType::Single(val)) => vec![val; n],
93
+ Some(BoundType::Vector(vec)) => vec,
94
+ None => vec![0.0; n],
95
+ };
96
+ let ub_vec = match ub {
97
+ Some(BoundType::Single(val)) => vec![val; n],
98
+ Some(BoundType::Vector(vec)) => vec,
99
+ None => vec![f64::INFINITY; n],
100
+ };
101
+
102
+ let get_vtype = |i: usize| -> char {
103
+ match &vtype {
104
+ Some(v) => v[i],
105
+ None => 'C',
106
+ }
107
+ };
108
+
109
+ for i in 0..n {
110
+ let var_id = start_id + i;
111
+ let current_name = match &name {
112
+ Some(NameType::Single(s)) => {
113
+ if s.is_empty() {
114
+ format!("var_x_{}", var_id)
115
+ } else {
116
+ format!("{}_{}", s, i)
117
+ }
118
+ }
119
+ Some(NameType::Vector(vec)) => vec[i].clone(),
120
+ None => "".to_string(),
121
+ };
122
+ self.vars.push(VarInfo {
123
+ col_index: var_id,
124
+ lb: lb_vec[i],
125
+ ub: ub_vec[i],
126
+ vtype: get_vtype(i),
127
+ name: current_name,
128
+ value: None,
129
+ });
130
+ }
131
+ self.needs_update = true;
132
+ (start_id..start_id + n).map(VarId).collect()
133
+ }
134
+
135
+ fn add_constraint(
136
+ &mut self,
137
+ f: ScalarFunctionType,
138
+ s: ScalarSetType,
139
+ name: Option<String>,
140
+ ) -> ConstrId {
141
+ let constr_id = self.constrs.len();
142
+ self.constrs.insert(
143
+ ConstrId(constr_id),
144
+ ConstrInfo {
145
+ row_index: constr_id,
146
+ name: name.unwrap_or_default(),
147
+ f,
148
+ s,
149
+ },
150
+ );
151
+ self.needs_update = true;
152
+ ConstrId(constr_id)
153
+ }
154
+
155
+ fn add_constraints(
156
+ &mut self,
157
+ fs: Vec<ScalarFunctionType>,
158
+ ss: Vec<ScalarSetType>,
159
+ names: Option<Vec<String>>,
160
+ ) -> Vec<ConstrId> {
161
+ let names = names.unwrap_or_else(|| vec!["".to_string(); fs.len()]);
162
+ fs.into_iter()
163
+ .zip(ss.into_iter())
164
+ .zip(names.into_iter())
165
+ .map(|((f, s), n)| self.add_constraint(f, s, Some(n)))
166
+ .collect()
167
+ }
168
+
169
+ fn get_model_attr(&self, attr: ModelAttr) -> Option<AttrValue> {
170
+ match attr {
171
+ ModelAttr::ObjectiveSense => self.sense.map(AttrValue::ModelSense),
172
+ ModelAttr::ObjectiveFunction => self.obj.clone().map(AttrValue::ScalarFn),
173
+ _ => None,
174
+ }
175
+ }
176
+
177
+ fn set_model_attr(&mut self, attr: ModelAttr, value: AttrValue) -> Result<(), MoiError> {
178
+ match attr {
179
+ ModelAttr::ObjectiveSense => {
180
+ if let AttrValue::ModelSense(sense) = value {
181
+ self.sense = Some(sense);
182
+ self.needs_update = true;
183
+ Ok(())
184
+ } else {
185
+ Err(MoiError::Msg(
186
+ "Invalid attribute value type for ObjectiveSense".to_string(),
187
+ ))
188
+ }
189
+ }
190
+ ModelAttr::ObjectiveFunction => {
191
+ if let AttrValue::ScalarFn(f) = value {
192
+ self.obj = Some(f);
193
+ self.needs_update = true;
194
+ Ok(())
195
+ } else {
196
+ Err(MoiError::Msg(
197
+ "Invalid attribute value type for ObjectiveFunction".to_string(),
198
+ ))
199
+ }
200
+ }
201
+ _ => Err(MoiError::Msg(
202
+ "Setting this model attribute is not supported".to_string(),
203
+ )),
204
+ }
205
+ }
206
+ }
@@ -0,0 +1,12 @@
1
+ [package]
2
+ name = "moi-core"
3
+ # 继承工作区的通用配置
4
+ version.workspace = true
5
+ edition.workspace = true
6
+ authors.workspace = true
7
+ license.workspace = true
8
+
9
+ [dependencies]
10
+ thiserror = "2.0.17"
11
+ serde ={ version = "1.0.228", features = ["derive"] }
12
+ bincode = "2.0.1"