@ohtaman/washi 0.1.2 → 0.3.0
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.
- package/README.ja.md +195 -105
- package/README.md +195 -105
- package/README.zh-CN.md +197 -107
- package/dist/browser/highs-worker.js +2 -2
- package/dist/browser/{highs-loader-T47YOyos.js → loader-CVt4CloI.js} +2 -2
- package/dist/browser/solvers/glpk.js +1065 -0
- package/dist/browser/solvers/glpk.min.js +1 -0
- package/dist/browser/solvers/z3-built.js +8245 -0
- package/dist/browser/solvers/z3-built.wasm +4 -0
- package/dist/browser/solvers/z3.js +7550 -0
- package/dist/browser/solvers/z3.min.js +5 -0
- package/dist/browser/washi.js +370 -642
- package/dist/browser/washi.min.js +4 -4
- package/dist/lib/async-mutex-Duo4jwzn.js +195 -0
- package/dist/lib/constraint-BRuB3ooC.js +90 -0
- package/dist/lib/highs-worker.js +2 -2
- package/dist/lib/{highs-loader-Blb99785.js → loader-Dti0v9yC.js} +1 -1
- package/dist/lib/registry-DksqCSjI.js +32 -0
- package/dist/lib/solvers/glpk.js +161 -2
- package/dist/lib/solvers/z3-assets.js +1 -0
- package/dist/lib/solvers/z3-built.js +8245 -0
- package/dist/lib/solvers/z3-built.wasm +4 -0
- package/dist/lib/solvers/z3.js +476 -2
- package/dist/lib/washi.js +604 -248
- package/dist/types/index.d.ts +7 -10
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/modeler/constraint.d.ts +0 -3
- package/dist/types/modeler/constraint.d.ts.map +1 -1
- package/dist/types/modeler/expression.d.ts +16 -10
- package/dist/types/modeler/expression.d.ts.map +1 -1
- package/dist/types/modeler/index.d.ts +3 -5
- package/dist/types/modeler/index.d.ts.map +1 -1
- package/dist/types/modeler/model.d.ts +91 -12
- package/dist/types/modeler/model.d.ts.map +1 -1
- package/dist/types/modeler/solvers/glpk/index.d.ts +3 -0
- package/dist/types/modeler/solvers/glpk/index.d.ts.map +1 -0
- package/dist/types/modeler/solvers/glpk/loader.d.ts +16 -0
- package/dist/types/modeler/solvers/glpk/loader.d.ts.map +1 -0
- package/dist/types/modeler/solvers/glpk/solver.d.ts +26 -0
- package/dist/types/modeler/solvers/glpk/solver.d.ts.map +1 -0
- package/dist/types/modeler/solvers/glpk.d.ts +1 -21
- package/dist/types/modeler/solvers/glpk.d.ts.map +1 -1
- package/dist/types/{highs-assets.d.ts → modeler/solvers/highs/assets.d.ts} +1 -1
- package/dist/types/modeler/solvers/highs/assets.d.ts.map +1 -0
- package/dist/types/modeler/solvers/highs/browser-runtime.d.ts +2 -0
- package/dist/types/modeler/solvers/highs/browser-runtime.d.ts.map +1 -0
- package/dist/types/modeler/solvers/highs/index.d.ts +6 -0
- package/dist/types/modeler/solvers/highs/index.d.ts.map +1 -0
- package/dist/types/{highs-loader.d.ts → modeler/solvers/highs/loader.d.ts} +2 -2
- package/dist/types/modeler/solvers/highs/loader.d.ts.map +1 -0
- package/dist/types/{highs-node-runtime.d.ts → modeler/solvers/highs/node-runtime.d.ts} +1 -1
- package/dist/types/modeler/solvers/highs/node-runtime.d.ts.map +1 -0
- package/dist/types/modeler/solvers/highs/solver.d.ts +33 -0
- package/dist/types/modeler/solvers/highs/solver.d.ts.map +1 -0
- package/dist/types/modeler/solvers/highs/worker-client.d.ts +13 -0
- package/dist/types/modeler/solvers/highs/worker-client.d.ts.map +1 -0
- package/dist/types/{highs-worker-factory.d.ts → modeler/solvers/highs/worker-factory.d.ts} +1 -1
- package/dist/types/modeler/solvers/highs/worker-factory.d.ts.map +1 -0
- package/dist/types/modeler/solvers/highs/worker.d.ts +2 -0
- package/dist/types/modeler/solvers/highs/worker.d.ts.map +1 -0
- package/dist/types/modeler/solvers/highs.d.ts +1 -31
- package/dist/types/modeler/solvers/highs.d.ts.map +1 -1
- package/dist/types/modeler/solvers/index.d.ts +0 -2
- package/dist/types/modeler/solvers/index.d.ts.map +1 -1
- package/dist/types/modeler/solvers/mock.d.ts +1 -0
- package/dist/types/modeler/solvers/mock.d.ts.map +1 -1
- package/dist/types/modeler/solvers/registry.d.ts +1 -2
- package/dist/types/modeler/solvers/registry.d.ts.map +1 -1
- package/dist/types/modeler/solvers/types.d.ts +1 -1
- package/dist/types/modeler/solvers/z3/assets.d.ts +11 -0
- package/dist/types/modeler/solvers/z3/assets.d.ts.map +1 -0
- package/dist/types/modeler/solvers/z3/index.d.ts +3 -0
- package/dist/types/modeler/solvers/z3/index.d.ts.map +1 -0
- package/dist/types/modeler/solvers/z3/loader.d.ts +56 -0
- package/dist/types/modeler/solvers/z3/loader.d.ts.map +1 -0
- package/dist/types/modeler/solvers/z3/solver.d.ts +34 -0
- package/dist/types/modeler/solvers/z3/solver.d.ts.map +1 -0
- package/dist/types/modeler/solvers/z3.d.ts +1 -35
- package/dist/types/modeler/solvers/z3.d.ts.map +1 -1
- package/dist/types/modeler/types.d.ts +8 -3
- package/dist/types/modeler/types.d.ts.map +1 -1
- package/dist/types/modeler/variable.d.ts +1 -6
- package/dist/types/modeler/variable.d.ts.map +1 -1
- package/package.json +28 -6
- package/dist/browser/_washi-optional-engine-282Ug7Gw.js +0 -7
- package/dist/lib/glpk-5cUpjyb9.js +0 -135
- package/dist/lib/z3-C6GeIicJ.js +0 -509
- package/dist/types/glpk-loader.d.ts +0 -8
- package/dist/types/glpk-loader.d.ts.map +0 -1
- package/dist/types/highs-assets.d.ts.map +0 -1
- package/dist/types/highs-browser-runtime.d.ts +0 -2
- package/dist/types/highs-browser-runtime.d.ts.map +0 -1
- package/dist/types/highs-loader.d.ts.map +0 -1
- package/dist/types/highs-node-runtime.d.ts.map +0 -1
- package/dist/types/highs-worker-client.d.ts +0 -12
- package/dist/types/highs-worker-client.d.ts.map +0 -1
- package/dist/types/highs-worker-factory.d.ts.map +0 -1
- package/dist/types/highs-worker.d.ts +0 -2
- package/dist/types/highs-worker.d.ts.map +0 -1
- package/dist/types/modeler/mip-compat.d.ts +0 -43
- package/dist/types/modeler/mip-compat.d.ts.map +0 -1
- package/dist/types/modeler/pulp-compat.d.ts +0 -67
- package/dist/types/modeler/pulp-compat.d.ts.map +0 -1
- /package/dist/browser/{highs-browser-runtime-D13gHOT5.js → browser-runtime-D13gHOT5.js} +0 -0
package/dist/browser/washi.js
CHANGED
|
@@ -1,32 +1,37 @@
|
|
|
1
|
-
import { a as e, c as t, i as n, l as r, n as i, o as a, r as o, s, t as c } from "./
|
|
1
|
+
import { a as e, c as t, i as n, l as r, n as i, o as a, r as o, s, t as c } from "./loader-CVt4CloI.js";
|
|
2
|
+
//#region src/modeler/types.ts
|
|
3
|
+
function l(e) {
|
|
4
|
+
return e && e.toLowerCase() === "maximize" ? "Maximize" : "Minimize";
|
|
5
|
+
}
|
|
6
|
+
//#endregion
|
|
2
7
|
//#region src/modeler/boolean-expr.ts
|
|
3
|
-
var
|
|
8
|
+
var u = 0, d = class {
|
|
4
9
|
and(e) {
|
|
5
|
-
return new
|
|
10
|
+
return new p("and", [this, e]);
|
|
6
11
|
}
|
|
7
12
|
or(e) {
|
|
8
|
-
return new
|
|
13
|
+
return new p("or", [this, e]);
|
|
9
14
|
}
|
|
10
15
|
implies(e) {
|
|
11
|
-
return new
|
|
16
|
+
return new p("implies", [this, e]);
|
|
12
17
|
}
|
|
13
18
|
xor(e) {
|
|
14
|
-
return new
|
|
19
|
+
return new p("xor", [this, e]);
|
|
15
20
|
}
|
|
16
21
|
not() {
|
|
17
|
-
return new
|
|
22
|
+
return new p("not", [this]);
|
|
18
23
|
}
|
|
19
|
-
},
|
|
24
|
+
}, f = class extends d {
|
|
20
25
|
id;
|
|
21
26
|
name;
|
|
22
27
|
value = null;
|
|
23
28
|
constructor(e) {
|
|
24
|
-
super(), this.id = ++
|
|
29
|
+
super(), this.id = ++u, this.name = e || `p_${this.id}`;
|
|
25
30
|
}
|
|
26
31
|
toString() {
|
|
27
32
|
return this.name;
|
|
28
33
|
}
|
|
29
|
-
},
|
|
34
|
+
}, p = class extends d {
|
|
30
35
|
op;
|
|
31
36
|
children;
|
|
32
37
|
constructor(e, t) {
|
|
@@ -36,27 +41,27 @@ var l = 0, u = class {
|
|
|
36
41
|
return this.op === "not" ? `not(${this.children[0].toString()})` : `(${this.children.map((e) => e.toString()).join(` ${this.op} `)})`;
|
|
37
42
|
}
|
|
38
43
|
};
|
|
39
|
-
function
|
|
44
|
+
function m(...e) {
|
|
40
45
|
if (e.length === 0) throw Error("and() requires at least one expression");
|
|
41
|
-
return new
|
|
46
|
+
return new p("and", e);
|
|
42
47
|
}
|
|
43
|
-
function
|
|
48
|
+
function h(...e) {
|
|
44
49
|
if (e.length === 0) throw Error("or() requires at least one expression");
|
|
45
|
-
return new
|
|
50
|
+
return new p("or", e);
|
|
46
51
|
}
|
|
47
|
-
function
|
|
48
|
-
return new
|
|
52
|
+
function g(e) {
|
|
53
|
+
return new p("not", [e]);
|
|
49
54
|
}
|
|
50
|
-
function
|
|
51
|
-
return new
|
|
55
|
+
function _(e, t) {
|
|
56
|
+
return new p("implies", [e, t]);
|
|
52
57
|
}
|
|
53
|
-
function
|
|
58
|
+
function v(...e) {
|
|
54
59
|
if (e.length === 0) throw Error("xor() requires at least one expression");
|
|
55
|
-
return new
|
|
60
|
+
return new p("xor", e);
|
|
56
61
|
}
|
|
57
62
|
//#endregion
|
|
58
63
|
//#region src/modeler/constraint.ts
|
|
59
|
-
var
|
|
64
|
+
var ee = 0, y = class {
|
|
60
65
|
id;
|
|
61
66
|
lhs;
|
|
62
67
|
sense;
|
|
@@ -65,25 +70,22 @@ var v = 0, y = class {
|
|
|
65
70
|
slack = null;
|
|
66
71
|
dual = null;
|
|
67
72
|
constructor(e, t, n, r) {
|
|
68
|
-
this.id = ++
|
|
69
|
-
}
|
|
70
|
-
get pi() {
|
|
71
|
-
return this.dual;
|
|
73
|
+
this.id = ++ee, this.lhs = e, this.sense = t, this.rhs = n, this.name = r || `c_${this.id}`;
|
|
72
74
|
}
|
|
73
75
|
and(e) {
|
|
74
|
-
return new
|
|
76
|
+
return new p("and", [this, e]);
|
|
75
77
|
}
|
|
76
78
|
or(e) {
|
|
77
|
-
return new
|
|
79
|
+
return new p("or", [this, e]);
|
|
78
80
|
}
|
|
79
81
|
implies(e) {
|
|
80
|
-
return new
|
|
82
|
+
return new p("implies", [this, e]);
|
|
81
83
|
}
|
|
82
84
|
xor(e) {
|
|
83
|
-
return new
|
|
85
|
+
return new p("xor", [this, e]);
|
|
84
86
|
}
|
|
85
87
|
not() {
|
|
86
|
-
return new
|
|
88
|
+
return new p("not", [this]);
|
|
87
89
|
}
|
|
88
90
|
toString() {
|
|
89
91
|
let e = this.sense === "==" ? "=" : this.sense;
|
|
@@ -287,26 +289,112 @@ var x = class e {
|
|
|
287
289
|
}
|
|
288
290
|
};
|
|
289
291
|
function C(e, t) {
|
|
290
|
-
let n = /* @__PURE__ */ new Map(), r = 0
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
292
|
+
let n = /* @__PURE__ */ new Map(), r = 0;
|
|
293
|
+
function i(e) {
|
|
294
|
+
if (e != null) {
|
|
295
|
+
if (typeof e == "number") r += e;
|
|
296
|
+
else if (typeof e == "object" && "terms" in e) {
|
|
297
|
+
let t = e;
|
|
298
|
+
for (let [e, r] of t.terms) {
|
|
299
|
+
let t = n.get(e) || 0;
|
|
300
|
+
n.set(e, t + r);
|
|
301
|
+
}
|
|
302
|
+
r += t.constant;
|
|
303
|
+
} else {
|
|
304
|
+
let t = e, r = n.get(t) || 0;
|
|
305
|
+
n.set(t, r + 1);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
if (typeof e == "number") {
|
|
310
|
+
let n = e, r = t;
|
|
311
|
+
for (let e = 0; e < n; e++) i(r(e, e));
|
|
312
|
+
} else {
|
|
313
|
+
let n = 0;
|
|
314
|
+
for (let r of e) i(t ? t(r, n++) : r);
|
|
315
|
+
}
|
|
316
|
+
for (let [e, t] of n) Math.abs(t) < 1e-12 && n.delete(e);
|
|
317
|
+
return new S(n, r);
|
|
318
|
+
}
|
|
319
|
+
function w(e, t) {
|
|
320
|
+
let n = /* @__PURE__ */ new Map(), r = 0;
|
|
321
|
+
function i(e) {
|
|
322
|
+
return typeof e == "object" && !!e && typeof e.id == "number" && typeof e.name == "string" && typeof e.mul == "function" && typeof e.le == "function";
|
|
323
|
+
}
|
|
324
|
+
function a(e) {
|
|
325
|
+
return i(e) || e instanceof S || e instanceof x;
|
|
326
|
+
}
|
|
327
|
+
function o(e, t) {
|
|
328
|
+
if (typeof t == "number") r += e * t;
|
|
329
|
+
else if (t instanceof S) {
|
|
330
|
+
for (let [r, i] of t.terms) {
|
|
331
|
+
let t = n.get(r) || 0;
|
|
332
|
+
n.set(r, t + e * i);
|
|
333
|
+
}
|
|
334
|
+
r += e * t.constant;
|
|
335
|
+
} else if (i(t)) {
|
|
336
|
+
let r = t, i = n.get(r) || 0;
|
|
337
|
+
n.set(r, i + e);
|
|
338
|
+
} else throw Error("Cannot multiply number by non-variable/expression operand in dot().");
|
|
339
|
+
}
|
|
340
|
+
function s(e) {
|
|
341
|
+
return typeof e == "object" && !!e && !Array.isArray(e) && !a(e);
|
|
342
|
+
}
|
|
343
|
+
function c(e, t, n = "") {
|
|
344
|
+
if (e === void 0 || t === void 0 || e === null || t === null) throw Error(`Nullish or undefined operand encountered in dot() at '${n || "root"}'.`);
|
|
345
|
+
if (typeof e == "number") {
|
|
346
|
+
o(e, t);
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
if (typeof t == "number") {
|
|
350
|
+
o(t, e);
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
if (Array.isArray(e) || Array.isArray(t)) {
|
|
354
|
+
if (Array.isArray(e) && Array.isArray(t)) {
|
|
355
|
+
if (e.length !== t.length) throw Error(`Array length mismatch in dot() at '${n || "root"}': received lengths ${e.length} and ${t.length}.`);
|
|
356
|
+
for (let r = 0; r < e.length; r++) c(e[r], t[r], n ? `${n}[${r}]` : `[${r}]`);
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
let r = Array.isArray(e) ? e : t, i = Array.isArray(e) ? t : e;
|
|
360
|
+
if (s(i)) {
|
|
361
|
+
let a = Object.keys(i);
|
|
362
|
+
if (a.length > 0 && a.every((e) => /^\d+$/.test(e))) {
|
|
363
|
+
if (a.length !== r.length) throw Error(`Array length mismatch in dot() at '${n || "root"}': array has length ${r.length}, but object has ${a.length} indices.`);
|
|
364
|
+
let o = [];
|
|
365
|
+
for (let e = 0; e < r.length; e++) !(e in i) && !(String(e) in i) && o.push(e);
|
|
366
|
+
if (o.length > 0) throw Error(`Key mismatch in dot() at '${n || "root"}': missing indices [${o.join(", ")}] in numeric record.`);
|
|
367
|
+
for (let i = 0; i < r.length; i++) c(e[i], t[i], n ? `${n}[${i}]` : `[${i}]`);
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
throw Error(`Type mismatch in dot() at '${n || "root"}': one operand is an Array while the other is an Object with keys [${a.slice(0, 5).join(", ")}].`);
|
|
299
371
|
}
|
|
300
|
-
|
|
301
|
-
} else if (e) {
|
|
302
|
-
let t = e, r = n.get(t) || 0;
|
|
303
|
-
n.set(t, r + 1);
|
|
372
|
+
throw Error(`Type mismatch in dot() at '${n || "root"}': one operand is an Array while the other is not.`);
|
|
304
373
|
}
|
|
374
|
+
if (s(e) && s(t)) {
|
|
375
|
+
let r = Object.keys(e), i = Object.keys(t), a = r.filter((e) => !(e in t)), o = i.filter((t) => !(t in e));
|
|
376
|
+
if (a.length > 0 || o.length > 0) {
|
|
377
|
+
let e = [];
|
|
378
|
+
throw a.length > 0 && e.push(`missing in second operand: [${a.join(", ")}]`), o.length > 0 && e.push(`missing in first operand: [${o.join(", ")}]`), Error(`Key mismatch in dot() at '${n || "root"}': ${e.join("; ")}.`);
|
|
379
|
+
}
|
|
380
|
+
for (let i of r) c(e[i], t[i], n ? `${n}.${i}` : i);
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
throw Error(`Unsupported operand types in dot() at '${n || "root"}'.`);
|
|
305
384
|
}
|
|
385
|
+
c(e, t);
|
|
306
386
|
for (let [e, t] of n) Math.abs(t) < 1e-12 && n.delete(e);
|
|
307
387
|
return new S(n, r);
|
|
308
388
|
}
|
|
309
|
-
|
|
389
|
+
//#endregion
|
|
390
|
+
//#region src/modeler/variable.ts
|
|
391
|
+
var te = 0;
|
|
392
|
+
function T(e) {
|
|
393
|
+
if (!e) return "Continuous";
|
|
394
|
+
let t = e.toLowerCase();
|
|
395
|
+
return t === "binary" || t === "bin" ? "Binary" : t === "integer" || t === "int" ? "Integer" : "Continuous";
|
|
396
|
+
}
|
|
397
|
+
var E = class e {
|
|
310
398
|
id;
|
|
311
399
|
name;
|
|
312
400
|
type;
|
|
@@ -315,13 +403,7 @@ var w = C, T = C, E = 0, D = class e {
|
|
|
315
403
|
value = null;
|
|
316
404
|
reducedCost = null;
|
|
317
405
|
constructor(e = {}) {
|
|
318
|
-
this.id = ++
|
|
319
|
-
}
|
|
320
|
-
get x() {
|
|
321
|
-
return this.value;
|
|
322
|
-
}
|
|
323
|
-
get varValue() {
|
|
324
|
-
return this.value;
|
|
406
|
+
this.id = ++te, this.name = e.name || `x_${this.id}`, this.type = T(e.type), this.type === "Binary" ? (this.lb = 0, this.ub = 1) : (this.lb = e.lb === void 0 ? 0 : e.lb, this.ub = e.ub === void 0 ? Infinity : e.ub);
|
|
325
407
|
}
|
|
326
408
|
mul(t) {
|
|
327
409
|
if (typeof t == "number") return new S(/* @__PURE__ */ new Map([[this, t]]), 0);
|
|
@@ -367,7 +449,7 @@ var w = C, T = C, E = 0, D = class e {
|
|
|
367
449
|
};
|
|
368
450
|
//#endregion
|
|
369
451
|
//#region src/modeler/parser.ts
|
|
370
|
-
function
|
|
452
|
+
function D(e) {
|
|
371
453
|
let t = [], n = 0, r = e.length;
|
|
372
454
|
for (; n < r;) {
|
|
373
455
|
let i = e[n];
|
|
@@ -423,7 +505,7 @@ function ee(e) {
|
|
|
423
505
|
}
|
|
424
506
|
return t;
|
|
425
507
|
}
|
|
426
|
-
var
|
|
508
|
+
var O = class {
|
|
427
509
|
tokens;
|
|
428
510
|
pos = 0;
|
|
429
511
|
resolver;
|
|
@@ -505,10 +587,10 @@ var te = class {
|
|
|
505
587
|
return t;
|
|
506
588
|
}
|
|
507
589
|
};
|
|
508
|
-
function
|
|
509
|
-
return new
|
|
590
|
+
function k(e, t) {
|
|
591
|
+
return new O(D(e), t).parseExpression();
|
|
510
592
|
}
|
|
511
|
-
function
|
|
593
|
+
function A(e, t, n) {
|
|
512
594
|
let r = e.match(/(<=|>=|==|=<|=>|=)/);
|
|
513
595
|
if (!r) throw Error(`Constraint must contain a comparison operator (<=, >=, ==, =): "${e}"`);
|
|
514
596
|
let i = r[0], a = r.index, o = e.substring(0, a).trim(), s = e.substring(a + i.length).trim(), c;
|
|
@@ -516,15 +598,15 @@ function k(e, t, n) {
|
|
|
516
598
|
else if (i === ">=" || i === "=>") c = ">=";
|
|
517
599
|
else if (i === "==" || i === "=") c = "==";
|
|
518
600
|
else throw Error(`Unsupported operator "${i}"`);
|
|
519
|
-
let l =
|
|
601
|
+
let l = k(o, t), u = k(s, t);
|
|
520
602
|
return c === "<=" ? l.le(u, n) : c === ">=" ? l.ge(u, n) : l.eq(u, n);
|
|
521
603
|
}
|
|
522
|
-
function
|
|
604
|
+
function j(e, t) {
|
|
523
605
|
let n = /* @__PURE__ */ new Map(), r = "";
|
|
524
606
|
for (let i = 0; i < e.length; i++) if (r += e[i], i < t.length) {
|
|
525
607
|
let e = t[i];
|
|
526
608
|
if (typeof e == "number") r += `(${e})`;
|
|
527
|
-
else if (e instanceof
|
|
609
|
+
else if (e instanceof E) {
|
|
528
610
|
let t = `__v_${i}__`;
|
|
529
611
|
n.set(t, e), r += t;
|
|
530
612
|
} else if (e instanceof S) {
|
|
@@ -537,35 +619,35 @@ function A(e, t) {
|
|
|
537
619
|
resolver: (e) => n.get(e)
|
|
538
620
|
};
|
|
539
621
|
}
|
|
540
|
-
function
|
|
541
|
-
let { fullText: n, resolver: r } =
|
|
542
|
-
return
|
|
622
|
+
function M(e, ...t) {
|
|
623
|
+
let { fullText: n, resolver: r } = j(e, t);
|
|
624
|
+
return A(n, r);
|
|
543
625
|
}
|
|
544
|
-
function
|
|
545
|
-
let { fullText: n, resolver: r } =
|
|
546
|
-
return /(<=|>=|==|=<|=>|=)/.test(n) ?
|
|
626
|
+
function N(e, ...t) {
|
|
627
|
+
let { fullText: n, resolver: r } = j(e, t);
|
|
628
|
+
return /(<=|>=|==|=<|=>|=)/.test(n) ? A(n, r) : k(n, r);
|
|
547
629
|
}
|
|
548
630
|
//#endregion
|
|
549
631
|
//#region src/modeler/lp-writer.ts
|
|
550
|
-
function
|
|
632
|
+
function P(e, t, n) {
|
|
551
633
|
let r = e.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
552
634
|
return /^[0-9]/.test(r) && (r = `${t}_${r}`), (!r || r.length === 0) && (r = `${t}_${n}`), r;
|
|
553
635
|
}
|
|
554
|
-
function
|
|
636
|
+
function F(e) {
|
|
555
637
|
let { sense: t, objective: n, constraints: r, variables: i } = e, a = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Set();
|
|
556
638
|
for (let e of i) {
|
|
557
|
-
let t =
|
|
639
|
+
let t = P(e.name, "x", e.id), n = t, r = 1;
|
|
558
640
|
for (; c.has(n);) n = `${t}_${r++}`;
|
|
559
641
|
c.add(n), o.set(e, n), a.set(n, e);
|
|
560
642
|
}
|
|
561
643
|
let l = /* @__PURE__ */ new Set(), u = /* @__PURE__ */ new Map();
|
|
562
644
|
for (let e of r) {
|
|
563
|
-
let t =
|
|
645
|
+
let t = P(e.name, "c", e.id), n = t, r = 1;
|
|
564
646
|
for (; l.has(n);) n = `${t}_${r++}`;
|
|
565
647
|
l.add(n), u.set(e, n), s.set(n, e);
|
|
566
648
|
}
|
|
567
649
|
let d = [];
|
|
568
|
-
d.push(t === "
|
|
650
|
+
d.push(t && t.toLowerCase() === "maximize" ? "Maximize" : "Minimize");
|
|
569
651
|
let f = [], p = !0, m = n instanceof x ? n.linearTerms : n.terms;
|
|
570
652
|
if (m) for (let [e, t] of m) {
|
|
571
653
|
if (Math.abs(t) < 1e-12) continue;
|
|
@@ -623,8 +705,39 @@ function M(e) {
|
|
|
623
705
|
};
|
|
624
706
|
}
|
|
625
707
|
//#endregion
|
|
708
|
+
//#region src/modeler/solvers/registry.ts
|
|
709
|
+
var I = Symbol.for("washi.solverRegistry"), L = Symbol.for("washi.defaultSolverFactory"), R = globalThis;
|
|
710
|
+
R[I] || (R[I] = /* @__PURE__ */ new Map());
|
|
711
|
+
var z = R[I];
|
|
712
|
+
function B(e, t) {
|
|
713
|
+
z.set(e.toLowerCase(), t);
|
|
714
|
+
}
|
|
715
|
+
function V(e) {
|
|
716
|
+
if (typeof e == "string") {
|
|
717
|
+
let t = z.get(e.toLowerCase());
|
|
718
|
+
if (!t) throw Error(`Solver '${e}' is not registered.`);
|
|
719
|
+
R[L] = t;
|
|
720
|
+
} else R[L] = typeof e == "function" ? e : () => e;
|
|
721
|
+
}
|
|
722
|
+
function H(e, t) {
|
|
723
|
+
if (!e) {
|
|
724
|
+
let e = R[L] || z.get("highs");
|
|
725
|
+
if (!e) throw Error(`No default solver available. Available solvers: ${U().join(", ")}`);
|
|
726
|
+
return e(t);
|
|
727
|
+
}
|
|
728
|
+
if (typeof e == "string") {
|
|
729
|
+
let n = e.toLowerCase(), r = z.get(n);
|
|
730
|
+
if (!r) throw Error(n === "glpk" ? `Solver 'glpk' not found. Available solvers: ${U().join(", ")}. Please import '@ohtaman/washi/solvers/glpk' before selecting this solver.` : n === "z3" ? `Solver 'z3' not found. Available solvers: ${U().join(", ")}. Please import '@ohtaman/washi/solvers/z3' before selecting this solver.` : `Solver '${e}' not found. Available solvers: ${U().join(", ")}`);
|
|
731
|
+
return r(t);
|
|
732
|
+
}
|
|
733
|
+
return e;
|
|
734
|
+
}
|
|
735
|
+
function U() {
|
|
736
|
+
return Array.from(z.keys());
|
|
737
|
+
}
|
|
738
|
+
//#endregion
|
|
626
739
|
//#region \0washi-browser-worker-factory
|
|
627
|
-
function
|
|
740
|
+
function W() {
|
|
628
741
|
let e = r("highs-worker.js");
|
|
629
742
|
if (new URL(e).origin === window.location.origin) return new Worker(e, { type: "module" });
|
|
630
743
|
let t = URL.createObjectURL(new Blob(["import " + JSON.stringify(e) + ";"], { type: "text/javascript" }));
|
|
@@ -636,42 +749,42 @@ function ie() {
|
|
|
636
749
|
}
|
|
637
750
|
}
|
|
638
751
|
//#endregion
|
|
639
|
-
//#region src/highs
|
|
640
|
-
var
|
|
641
|
-
function
|
|
752
|
+
//#region src/modeler/solvers/highs/worker-client.ts
|
|
753
|
+
var G = null, K = 0, q = /* @__PURE__ */ new Map();
|
|
754
|
+
function J() {
|
|
642
755
|
if (typeof window > "u" || typeof Worker > "u") return null;
|
|
643
|
-
if (!
|
|
644
|
-
|
|
645
|
-
let e =
|
|
646
|
-
|
|
647
|
-
let { type: t, id: n, result: r, runTime: i, error: a } = e.data, o =
|
|
648
|
-
o && (
|
|
756
|
+
if (!G) try {
|
|
757
|
+
G = W();
|
|
758
|
+
let e = G;
|
|
759
|
+
G.onmessage = (e) => {
|
|
760
|
+
let { type: t, id: n, result: r, runTime: i, error: a } = e.data, o = q.get(n);
|
|
761
|
+
o && (q.delete(n), t === "SOLVE_SUCCESS" ? o.resolve({
|
|
649
762
|
result: r,
|
|
650
763
|
runTime: i
|
|
651
764
|
}) : o.reject(Error(a || "Worker solve failed")));
|
|
652
765
|
};
|
|
653
766
|
let t = (t) => {
|
|
654
|
-
if (
|
|
655
|
-
|
|
656
|
-
for (let e of
|
|
657
|
-
|
|
767
|
+
if (G === e) {
|
|
768
|
+
G = null, e.terminate();
|
|
769
|
+
for (let e of q.values()) e.reject(t);
|
|
770
|
+
q.clear();
|
|
658
771
|
}
|
|
659
772
|
};
|
|
660
|
-
|
|
661
|
-
} catch
|
|
662
|
-
|
|
773
|
+
G.onerror = (e) => t(/* @__PURE__ */ Error(`[WaSHI] HiGHS Worker failed to load or execute. Check Worker assets, CORS and CSP. ${e?.message || ""}`)), G.onmessageerror = () => t(/* @__PURE__ */ Error("[WaSHI] Could not deserialize the HiGHS Worker response."));
|
|
774
|
+
} catch {
|
|
775
|
+
G = null;
|
|
663
776
|
}
|
|
664
|
-
return
|
|
777
|
+
return G;
|
|
665
778
|
}
|
|
666
|
-
async function
|
|
667
|
-
let r = await a(n.wasmUrl), i = n.useWorker ?? typeof window < "u" ?
|
|
779
|
+
async function Y(e, n = {}) {
|
|
780
|
+
let r = await a(n.wasmUrl), i = n.useWorker ?? typeof window < "u" ? J() : null;
|
|
668
781
|
if (!i) return t(e, n);
|
|
669
782
|
let o = {
|
|
670
783
|
...n,
|
|
671
784
|
wasmUrl: r
|
|
672
|
-
}, s = `req_${++
|
|
785
|
+
}, s = `req_${++K}`;
|
|
673
786
|
return new Promise((t, n) => {
|
|
674
|
-
|
|
787
|
+
q.set(s, {
|
|
675
788
|
resolve: t,
|
|
676
789
|
reject: n
|
|
677
790
|
});
|
|
@@ -683,13 +796,13 @@ async function se(e, n = {}) {
|
|
|
683
796
|
options: o
|
|
684
797
|
});
|
|
685
798
|
} catch (e) {
|
|
686
|
-
|
|
799
|
+
q.delete(s), n(e);
|
|
687
800
|
}
|
|
688
801
|
});
|
|
689
802
|
}
|
|
690
803
|
//#endregion
|
|
691
|
-
//#region src/modeler/solvers/highs.ts
|
|
692
|
-
function
|
|
804
|
+
//#region src/modeler/solvers/highs/solver.ts
|
|
805
|
+
function X(e) {
|
|
693
806
|
if (typeof e != "string") return "Unknown";
|
|
694
807
|
let t = e.trim();
|
|
695
808
|
if (!t) return "Unknown";
|
|
@@ -718,7 +831,7 @@ function ce(e) {
|
|
|
718
831
|
let n = t.toLowerCase();
|
|
719
832
|
return n.includes("infeasible") && n.includes("unbounded") ? "Unknown" : n.includes("infeasible") ? "Infeasible" : n.includes("unbounded") ? "Unbounded" : n.includes("time limit") || n.includes("timelimit") ? "TimeLimit" : n.includes("iteration limit") || n.includes("iterationlimit") ? "IterationLimit" : n.includes("error") ? "Error" : n === "optimal" ? "Optimal" : "Unknown";
|
|
720
833
|
}
|
|
721
|
-
var
|
|
834
|
+
var Z = class e {
|
|
722
835
|
static capabilities = {
|
|
723
836
|
supportsMIP: !0,
|
|
724
837
|
supportsQP: !0
|
|
@@ -734,7 +847,18 @@ var F = class e {
|
|
|
734
847
|
let r = {
|
|
735
848
|
...this.options,
|
|
736
849
|
...n
|
|
737
|
-
}, { lpString: i, nameToVar: a, nameToConstraint: o } = t, { result: s, runTime: c } = await
|
|
850
|
+
}, { lpString: i, nameToVar: a, nameToConstraint: o } = t, { result: s, runTime: c } = await Y(i, r), l = X(s?.Status);
|
|
851
|
+
if (l === "Infeasible" || l === "Unbounded") return {
|
|
852
|
+
status: l,
|
|
853
|
+
objectiveValue: null,
|
|
854
|
+
values: {},
|
|
855
|
+
reducedCosts: {},
|
|
856
|
+
duals: {},
|
|
857
|
+
slacks: {},
|
|
858
|
+
runTime: c,
|
|
859
|
+
raw: s
|
|
860
|
+
};
|
|
861
|
+
let u = s.ObjectiveValue, d = typeof u == "number" ? u + e.objective.constant : null, f = {}, p = {}, m = {}, h = {};
|
|
738
862
|
if (s.Columns) for (let [e, t] of Object.entries(s.Columns)) {
|
|
739
863
|
let n = a.get(e);
|
|
740
864
|
if (n) {
|
|
@@ -763,129 +887,14 @@ var F = class e {
|
|
|
763
887
|
raw: s
|
|
764
888
|
};
|
|
765
889
|
}
|
|
766
|
-
},
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
try {
|
|
770
|
-
let e = await import("./_washi-optional-engine-282Ug7Gw.js");
|
|
771
|
-
return await (e.default || e)();
|
|
772
|
-
} catch (e) {
|
|
773
|
-
throw Error("[WaSHI] Could not load GLPK. Install the optional glpk.js dependency before selecting this solver.", { cause: e });
|
|
774
|
-
}
|
|
775
|
-
})(), I);
|
|
890
|
+
}, Q = Z;
|
|
891
|
+
function ne() {
|
|
892
|
+
B("highs", (e) => new Z(e)), V((e) => new Z(e));
|
|
776
893
|
}
|
|
894
|
+
ne();
|
|
777
895
|
//#endregion
|
|
778
|
-
//#region src/modeler/solvers/
|
|
779
|
-
var
|
|
780
|
-
static capabilities = {
|
|
781
|
-
supportsMIP: !0,
|
|
782
|
-
supportsQP: !1
|
|
783
|
-
};
|
|
784
|
-
name = "GLPK";
|
|
785
|
-
version = "5.0.0";
|
|
786
|
-
capabilities = e.capabilities;
|
|
787
|
-
options;
|
|
788
|
-
constructor(e = {}) {
|
|
789
|
-
this.options = e;
|
|
790
|
-
}
|
|
791
|
-
async solve(e, t, n = {}) {
|
|
792
|
-
let r = {
|
|
793
|
-
...this.options,
|
|
794
|
-
...n
|
|
795
|
-
}, i = await L(), { nameToVar: a, nameToConstraint: o } = t, s = /* @__PURE__ */ new Map();
|
|
796
|
-
for (let [e, t] of a.entries()) s.set(t, e);
|
|
797
|
-
let c = /* @__PURE__ */ new Map();
|
|
798
|
-
for (let [e, t] of o.entries()) c.set(t, e);
|
|
799
|
-
let l = [], u = "linearTerms" in e.objective ? e.objective.linearTerms : e.objective.terms;
|
|
800
|
-
if (u) for (let [e, t] of u) {
|
|
801
|
-
if (Math.abs(t) < 1e-12) continue;
|
|
802
|
-
let n = s.get(e) || e.name;
|
|
803
|
-
l.push({
|
|
804
|
-
name: n,
|
|
805
|
-
coef: Number(t)
|
|
806
|
-
});
|
|
807
|
-
}
|
|
808
|
-
let d = [];
|
|
809
|
-
for (let t of e.constraints) {
|
|
810
|
-
let e = c.get(t) || t.name || `c_${d.length + 1}`, n = [];
|
|
811
|
-
for (let [e, r] of t.lhs.terms) {
|
|
812
|
-
if (Math.abs(r) < 1e-12) continue;
|
|
813
|
-
let t = s.get(e) || e.name;
|
|
814
|
-
n.push({
|
|
815
|
-
name: t,
|
|
816
|
-
coef: Number(r)
|
|
817
|
-
});
|
|
818
|
-
}
|
|
819
|
-
let r;
|
|
820
|
-
r = t.sense === "==" ? {
|
|
821
|
-
type: i.GLP_FX,
|
|
822
|
-
lb: t.rhs,
|
|
823
|
-
ub: t.rhs
|
|
824
|
-
} : t.sense === "<=" ? {
|
|
825
|
-
type: i.GLP_UP,
|
|
826
|
-
lb: -Infinity,
|
|
827
|
-
ub: t.rhs
|
|
828
|
-
} : {
|
|
829
|
-
type: i.GLP_LO,
|
|
830
|
-
lb: t.rhs,
|
|
831
|
-
ub: Infinity
|
|
832
|
-
}, d.push({
|
|
833
|
-
name: e,
|
|
834
|
-
vars: n,
|
|
835
|
-
bnds: r
|
|
836
|
-
});
|
|
837
|
-
}
|
|
838
|
-
let f = [], p = [], m = [];
|
|
839
|
-
for (let t of e.variables) {
|
|
840
|
-
let e = s.get(t) || t.name;
|
|
841
|
-
t.type === "Binary" ? f.push(e) : t.type === "Integer" && p.push(e);
|
|
842
|
-
let n = t.lb, r = t.ub, a;
|
|
843
|
-
a = n === -Infinity && r === Infinity ? i.GLP_FR : n !== -Infinity && r === Infinity ? i.GLP_LO : n === -Infinity && r !== Infinity ? i.GLP_UP : n === r ? i.GLP_FX : i.GLP_DB, m.push({
|
|
844
|
-
name: e,
|
|
845
|
-
type: a,
|
|
846
|
-
lb: n === -Infinity ? 0 : n,
|
|
847
|
-
ub: r === Infinity ? 0 : r
|
|
848
|
-
});
|
|
849
|
-
}
|
|
850
|
-
let h = {
|
|
851
|
-
name: e.name || "GLPK_Model",
|
|
852
|
-
objective: {
|
|
853
|
-
direction: e.sense === "Maximize" ? i.GLP_MAX : i.GLP_MIN,
|
|
854
|
-
name: "obj",
|
|
855
|
-
vars: l
|
|
856
|
-
},
|
|
857
|
-
subjectTo: d
|
|
858
|
-
};
|
|
859
|
-
m.length > 0 && (h.bounds = m), f.length > 0 && (h.binaries = f), p.length > 0 && (h.generals = p);
|
|
860
|
-
let g = {
|
|
861
|
-
msglev: r.verbose ? i.GLP_MSG_ALL : i.GLP_MSG_OFF,
|
|
862
|
-
presol: r.presolve !== "off"
|
|
863
|
-
};
|
|
864
|
-
typeof r.timeLimit == "number" && (g.tmlim = r.timeLimit), typeof r.mipGap == "number" && (g.mipgap = r.mipGap);
|
|
865
|
-
let _ = performance.now(), v = await i.solve(h, g), y = (performance.now() - _) / 1e3, b = v?.result?.status, x;
|
|
866
|
-
x = b === i.GLP_OPT ? "Optimal" : b === i.GLP_FEAS ? "Feasible" : b === i.GLP_INFEAS || b === i.GLP_NOFEAS ? "Infeasible" : b === i.GLP_UNBND ? "Unbounded" : "Optimal";
|
|
867
|
-
let S = v?.result?.z, C = typeof S == "number" ? S + e.objective.constant : null, w = {}, T = v?.result?.vars || {};
|
|
868
|
-
for (let [e, t] of Object.entries(T)) {
|
|
869
|
-
let n = a.get(e), r = n ? n.name : e;
|
|
870
|
-
w[r] = typeof t == "number" ? Math.abs(t) < 1e-9 ? 0 : t : 0;
|
|
871
|
-
}
|
|
872
|
-
for (let t of e.variables) w[t.name] === void 0 && (w[t.name] = 0);
|
|
873
|
-
let E = {};
|
|
874
|
-
if (v?.result?.dual) for (let [e, t] of Object.entries(v.result.dual)) {
|
|
875
|
-
let n = o.get(e), r = n ? n.name : e;
|
|
876
|
-
E[r] = Number(t);
|
|
877
|
-
}
|
|
878
|
-
return {
|
|
879
|
-
status: x,
|
|
880
|
-
objectiveValue: C,
|
|
881
|
-
values: w,
|
|
882
|
-
duals: E,
|
|
883
|
-
runTime: v?.time || y,
|
|
884
|
-
logs: [`GLPK solved in ${(v?.time || y) * 1e3} ms.`],
|
|
885
|
-
raw: v
|
|
886
|
-
};
|
|
887
|
-
}
|
|
888
|
-
}, z = class {
|
|
896
|
+
//#region src/modeler/solvers/mock.ts
|
|
897
|
+
var $ = class {
|
|
889
898
|
name;
|
|
890
899
|
capabilities;
|
|
891
900
|
options;
|
|
@@ -911,268 +920,14 @@ var R = class e {
|
|
|
911
920
|
raw: { mock: !0 }
|
|
912
921
|
};
|
|
913
922
|
}
|
|
914
|
-
}, B = /* @__PURE__ */ new Map();
|
|
915
|
-
B.set("highs", (e) => new F(e)), B.set("glpk", (e) => new R(e)), B.set("mock", (e) => new z(e));
|
|
916
|
-
var V = (e) => new F(e);
|
|
917
|
-
function H(e, t) {
|
|
918
|
-
B.set(e.toLowerCase(), t);
|
|
919
|
-
}
|
|
920
|
-
function ue(e) {
|
|
921
|
-
if (typeof e == "string") {
|
|
922
|
-
let t = B.get(e.toLowerCase());
|
|
923
|
-
if (!t) throw Error(`Solver '${e}' is not registered.`);
|
|
924
|
-
V = t;
|
|
925
|
-
} else V = typeof e == "function" ? e : () => e;
|
|
926
|
-
}
|
|
927
|
-
function U(e, t) {
|
|
928
|
-
if (!e) return V(t);
|
|
929
|
-
if (typeof e == "string") {
|
|
930
|
-
let n = B.get(e.toLowerCase());
|
|
931
|
-
if (!n) throw Error(`Solver '${e}' not found. Available solvers: ${W().join(", ")}`);
|
|
932
|
-
return n(t);
|
|
933
|
-
}
|
|
934
|
-
return e;
|
|
935
|
-
}
|
|
936
|
-
function W() {
|
|
937
|
-
return Array.from(B.keys());
|
|
938
|
-
}
|
|
939
|
-
//#endregion
|
|
940
|
-
//#region src/modeler/solvers/z3.ts
|
|
941
|
-
var G = null;
|
|
942
|
-
async function K() {
|
|
943
|
-
if (!G) {
|
|
944
|
-
let e = (async () => {
|
|
945
|
-
try {
|
|
946
|
-
return await (await import("./_washi-optional-engine-282Ug7Gw.js")).init();
|
|
947
|
-
} catch (e) {
|
|
948
|
-
throw Error(`[WaSHI] Failed to load z3-solver. Ensure the optional dependency is installed (npm install z3-solver) and supported in this environment. Error: ${e instanceof Error ? e.message : String(e)}`, { cause: e });
|
|
949
|
-
}
|
|
950
|
-
})();
|
|
951
|
-
e.catch(() => {
|
|
952
|
-
G === e && (G = null);
|
|
953
|
-
}), G = e;
|
|
954
|
-
}
|
|
955
|
-
return G;
|
|
956
|
-
}
|
|
957
|
-
var q = class {
|
|
958
|
-
name = "z3";
|
|
959
|
-
capabilities = {
|
|
960
|
-
supportsMIP: !0,
|
|
961
|
-
supportsQP: !1,
|
|
962
|
-
supportsSMT: !0,
|
|
963
|
-
supportsUnsatCore: !0
|
|
964
|
-
};
|
|
965
|
-
createContextTranslator(e) {
|
|
966
|
-
let t = /* @__PURE__ */ new Map(), n = /* @__PURE__ */ new Map();
|
|
967
|
-
function r(n) {
|
|
968
|
-
if (!t.has(n.name)) {
|
|
969
|
-
let r = n.type === "Continuous" || n.type === "CONTINUOUS" ? e.Real.const(n.name) : e.Int.const(n.name);
|
|
970
|
-
t.set(n.name, r);
|
|
971
|
-
}
|
|
972
|
-
return t.get(n.name);
|
|
973
|
-
}
|
|
974
|
-
function i(t) {
|
|
975
|
-
if (!n.has(t.name)) {
|
|
976
|
-
let r = e.Bool.const(t.name);
|
|
977
|
-
n.set(t.name, r);
|
|
978
|
-
}
|
|
979
|
-
return n.get(t.name);
|
|
980
|
-
}
|
|
981
|
-
function a(t) {
|
|
982
|
-
return typeof e.isInt == "function" && e.isInt(t) ? e.ToReal(t) : t;
|
|
983
|
-
}
|
|
984
|
-
function o(t, n) {
|
|
985
|
-
let r = typeof e.isInt == "function" && e.isInt(t), i = typeof e.isInt == "function" && e.isInt(n);
|
|
986
|
-
return r && i ? t.add(n) : a(t).add(a(n));
|
|
987
|
-
}
|
|
988
|
-
function s(t, n) {
|
|
989
|
-
return typeof e.isInt == "function" && e.isInt(t) && Number.isInteger(n) ? t.mul(e.Int.val(n)) : a(t).mul(e.Real.val(n));
|
|
990
|
-
}
|
|
991
|
-
function c(t, n) {
|
|
992
|
-
let r = typeof e.isInt == "function" && e.isInt(t), i = typeof e.isInt == "function" && e.isInt(n);
|
|
993
|
-
return r && i ? t.mul(n) : a(t).mul(a(n));
|
|
994
|
-
}
|
|
995
|
-
function l(t, n) {
|
|
996
|
-
return n && Number.isInteger(t) ? e.Int.val(t) : e.Real.val(t);
|
|
997
|
-
}
|
|
998
|
-
function u(t, n, r) {
|
|
999
|
-
let i = typeof e.isInt == "function" && e.isInt(t) && Number.isInteger(r), o = i ? t : a(t), s = i ? e.Int.val(r) : e.Real.val(r);
|
|
1000
|
-
return n === "<=" ? o.le(s) : n === ">=" ? o.ge(s) : o.eq(s);
|
|
1001
|
-
}
|
|
1002
|
-
function p(t) {
|
|
1003
|
-
let n = null;
|
|
1004
|
-
if (t && t.terms) for (let [e, i] of t.terms.entries()) {
|
|
1005
|
-
let t = s(r(e), i);
|
|
1006
|
-
n = n === null ? t : o(n, t);
|
|
1007
|
-
}
|
|
1008
|
-
let i = t && typeof t.constant == "number" ? t.constant : 0;
|
|
1009
|
-
if (i !== 0 || n === null) {
|
|
1010
|
-
let t = l(i, n === null || typeof e.isInt == "function" && e.isInt(n));
|
|
1011
|
-
n = n === null ? t : o(n, t);
|
|
1012
|
-
}
|
|
1013
|
-
return n;
|
|
1014
|
-
}
|
|
1015
|
-
function m(t) {
|
|
1016
|
-
let n = null;
|
|
1017
|
-
if (t.linearTerms) for (let [e, i] of t.linearTerms.entries()) {
|
|
1018
|
-
let t = s(r(e), i);
|
|
1019
|
-
n = n === null ? t : o(n, t);
|
|
1020
|
-
}
|
|
1021
|
-
if (t.quadTerms) for (let e of t.quadTerms.values()) {
|
|
1022
|
-
let t = s(c(r(e.var1), r(e.var2)), e.coeff);
|
|
1023
|
-
n = n === null ? t : o(n, t);
|
|
1024
|
-
}
|
|
1025
|
-
let i = t && typeof t.constant == "number" ? t.constant : 0;
|
|
1026
|
-
if (i !== 0 || n === null) {
|
|
1027
|
-
let t = l(i, n === null || typeof e.isInt == "function" && e.isInt(n));
|
|
1028
|
-
n = n === null ? t : o(n, t);
|
|
1029
|
-
}
|
|
1030
|
-
return n;
|
|
1031
|
-
}
|
|
1032
|
-
function h(e) {
|
|
1033
|
-
let t = e.lhs instanceof Object && "quadTerms" in e.lhs ? m(e.lhs) : p(e.lhs), n = typeof e.rhs == "number" ? e.rhs : 0;
|
|
1034
|
-
return u(t, e.sense, n);
|
|
1035
|
-
}
|
|
1036
|
-
function g(t) {
|
|
1037
|
-
if (t instanceof d) return i(t);
|
|
1038
|
-
if (t instanceof y) return h(t);
|
|
1039
|
-
if (t instanceof f) switch (t.op) {
|
|
1040
|
-
case "and": return e.And(...t.children.map(g));
|
|
1041
|
-
case "or": return e.Or(...t.children.map(g));
|
|
1042
|
-
case "not": return e.Not(g(t.children[0]));
|
|
1043
|
-
case "implies": return e.Implies(g(t.children[0]), g(t.children[1]));
|
|
1044
|
-
case "xor": return e.Xor(g(t.children[0]), g(t.children[1]));
|
|
1045
|
-
default: throw Error(`Unsupported boolean operation: ${t.op}`);
|
|
1046
|
-
}
|
|
1047
|
-
throw Error(`Unknown logic element: ${String(t)}`);
|
|
1048
|
-
}
|
|
1049
|
-
function _(e) {
|
|
1050
|
-
if (!e) return 0;
|
|
1051
|
-
if (typeof e.asNumber == "function") return e.asNumber();
|
|
1052
|
-
let t = e.toString();
|
|
1053
|
-
if (t.startsWith("(/ ")) {
|
|
1054
|
-
let e = t.slice(3, -1).trim().split(/\s+/);
|
|
1055
|
-
if (e.length === 2) return Number(e[0]) / Number(e[1]);
|
|
1056
|
-
}
|
|
1057
|
-
return Number(t);
|
|
1058
|
-
}
|
|
1059
|
-
return {
|
|
1060
|
-
getVarAst: r,
|
|
1061
|
-
getBoolVarAst: i,
|
|
1062
|
-
compareAstWithNumber: u,
|
|
1063
|
-
translateLinExpr: p,
|
|
1064
|
-
translateQuadExpr: m,
|
|
1065
|
-
translateConstraint: h,
|
|
1066
|
-
translateBoolExpr: g,
|
|
1067
|
-
parseAstValue: _,
|
|
1068
|
-
varAstMap: t,
|
|
1069
|
-
boolVarAstMap: n
|
|
1070
|
-
};
|
|
1071
|
-
}
|
|
1072
|
-
async check(e, t = {}) {
|
|
1073
|
-
let { Context: n } = await K(), r = new n(`check_${Date.now()}_${Math.random().toString(36).substring(2, 7)}`), i = new r.Solver(), a = this.createContextTranslator(r), o = e.variables || e.vars || [], s = e.boolVars || [], c = e.constraints || [], l = e.logicalConstraints || [];
|
|
1074
|
-
for (let e of o) {
|
|
1075
|
-
let t = a.getVarAst(e);
|
|
1076
|
-
e.lb !== -Infinity && i.add(a.compareAstWithNumber(t, ">=", e.lb)), e.ub !== Infinity && i.add(a.compareAstWithNumber(t, "<=", e.ub)), (e.type === "Binary" || e.type === "BINARY") && (i.add(a.compareAstWithNumber(t, ">=", 0)), i.add(a.compareAstWithNumber(t, "<=", 1)));
|
|
1077
|
-
}
|
|
1078
|
-
for (let e of c) {
|
|
1079
|
-
let t = a.translateConstraint(e), n = r.Bool.const(e.name);
|
|
1080
|
-
i.addAndTrack(t, n);
|
|
1081
|
-
}
|
|
1082
|
-
for (let e = 0; e < l.length; e++) {
|
|
1083
|
-
let t = l[e], n = t && t.name || `lc_${e + 1}`, o = t && t.expr || t, s = a.translateBoolExpr(o), c = r.Bool.const(n);
|
|
1084
|
-
i.addAndTrack(s, c);
|
|
1085
|
-
}
|
|
1086
|
-
let u = (await i.check()).toString();
|
|
1087
|
-
if (u === "sat") {
|
|
1088
|
-
let e = i.model(), t = {};
|
|
1089
|
-
for (let n of o) {
|
|
1090
|
-
let r = a.getVarAst(n), i = e.eval(r);
|
|
1091
|
-
t[n.name] = a.parseAstValue(i);
|
|
1092
|
-
}
|
|
1093
|
-
for (let n of s) {
|
|
1094
|
-
let r = a.getBoolVarAst(n), i = e.eval(r);
|
|
1095
|
-
t[n.name] = i.toString() === "true";
|
|
1096
|
-
}
|
|
1097
|
-
return {
|
|
1098
|
-
status: "Sat",
|
|
1099
|
-
values: t
|
|
1100
|
-
};
|
|
1101
|
-
}
|
|
1102
|
-
if (u === "unsat") {
|
|
1103
|
-
let e = i.unsatCore(), t = [], n = typeof e.length == "function" ? e.length() : e.length;
|
|
1104
|
-
for (let r = 0; r < n; r++) t.push(e.get(r).toString());
|
|
1105
|
-
return {
|
|
1106
|
-
status: "Unsat",
|
|
1107
|
-
values: {},
|
|
1108
|
-
unsatCore: t
|
|
1109
|
-
};
|
|
1110
|
-
}
|
|
1111
|
-
return {
|
|
1112
|
-
status: "Unknown",
|
|
1113
|
-
values: {}
|
|
1114
|
-
};
|
|
1115
|
-
}
|
|
1116
|
-
async solve(e, t, n = {}) {
|
|
1117
|
-
if (!(e.objective !== null && e.objective !== void 0 && ("terms" in e.objective && e.objective.terms.size > 0 || "quadTerms" in e.objective && e.objective.quadTerms.length > 0 || e.objective.offset && e.objective.offset !== 0))) {
|
|
1118
|
-
let t = await this.check(e, n), r = {};
|
|
1119
|
-
for (let [e, n] of Object.entries(t.values)) r[e] = typeof n == "boolean" ? +!!n : n;
|
|
1120
|
-
return {
|
|
1121
|
-
status: t.status === "Sat" ? "Optimal" : t.status === "Unsat" ? "Infeasible" : "Error",
|
|
1122
|
-
objectiveValue: 0,
|
|
1123
|
-
values: r
|
|
1124
|
-
};
|
|
1125
|
-
}
|
|
1126
|
-
let { Context: r } = await K(), i = new r(`opt_${Date.now()}_${Math.random().toString(36).substring(2, 7)}`), a = new i.Optimize(), o = this.createContextTranslator(i), s = e.variables || e.vars || [], c = e.boolVars || [], l = e.constraints || [], u = e.logicalConstraints || [];
|
|
1127
|
-
for (let e of s) {
|
|
1128
|
-
let t = o.getVarAst(e);
|
|
1129
|
-
e.lb !== -Infinity && a.add(o.compareAstWithNumber(t, ">=", e.lb)), e.ub !== Infinity && a.add(o.compareAstWithNumber(t, "<=", e.ub)), (e.type === "Binary" || e.type === "BINARY") && (a.add(o.compareAstWithNumber(t, ">=", 0)), a.add(o.compareAstWithNumber(t, "<=", 1)));
|
|
1130
|
-
}
|
|
1131
|
-
for (let e of l) {
|
|
1132
|
-
let t = o.translateConstraint(e);
|
|
1133
|
-
a.add(t);
|
|
1134
|
-
}
|
|
1135
|
-
for (let e of u) {
|
|
1136
|
-
let t = e && e.expr || e, n = o.translateBoolExpr(t);
|
|
1137
|
-
a.add(n);
|
|
1138
|
-
}
|
|
1139
|
-
let d = e.objective instanceof Object && "quadTerms" in e.objective ? o.translateQuadExpr(e.objective) : o.translateLinExpr(e.objective);
|
|
1140
|
-
(e.sense || "MINIMIZE").toUpperCase() === "MAXIMIZE" ? a.maximize(d) : a.minimize(d);
|
|
1141
|
-
let f = (await a.check()).toString();
|
|
1142
|
-
if (f === "sat") {
|
|
1143
|
-
let e = a.model(), t = {};
|
|
1144
|
-
for (let n of s) {
|
|
1145
|
-
let r = o.getVarAst(n), i = e.eval(r);
|
|
1146
|
-
t[n.name] = o.parseAstValue(i);
|
|
1147
|
-
}
|
|
1148
|
-
for (let n of c) {
|
|
1149
|
-
let r = o.getBoolVarAst(n), i = e.eval(r);
|
|
1150
|
-
t[n.name] = +(i.toString() === "true");
|
|
1151
|
-
}
|
|
1152
|
-
return {
|
|
1153
|
-
status: "Optimal",
|
|
1154
|
-
objectiveValue: o.parseAstValue(e.eval(d)),
|
|
1155
|
-
values: t
|
|
1156
|
-
};
|
|
1157
|
-
}
|
|
1158
|
-
return f === "unsat" ? {
|
|
1159
|
-
status: "Infeasible",
|
|
1160
|
-
objectiveValue: null,
|
|
1161
|
-
values: {}
|
|
1162
|
-
} : {
|
|
1163
|
-
status: "Error",
|
|
1164
|
-
objectiveValue: null,
|
|
1165
|
-
values: {}
|
|
1166
|
-
};
|
|
1167
|
-
}
|
|
1168
923
|
};
|
|
1169
|
-
function
|
|
1170
|
-
|
|
924
|
+
function re() {
|
|
925
|
+
B("mock", (e) => new $(e));
|
|
1171
926
|
}
|
|
1172
|
-
|
|
927
|
+
re();
|
|
1173
928
|
//#endregion
|
|
1174
929
|
//#region src/modeler/model.ts
|
|
1175
|
-
var
|
|
930
|
+
var ie = class {
|
|
1176
931
|
name;
|
|
1177
932
|
sense;
|
|
1178
933
|
objective;
|
|
@@ -1189,8 +944,12 @@ var Y = class {
|
|
|
1189
944
|
runTime = 0;
|
|
1190
945
|
solution = null;
|
|
1191
946
|
solver = null;
|
|
1192
|
-
constructor(e = {}) {
|
|
1193
|
-
|
|
947
|
+
constructor(e = {}, t) {
|
|
948
|
+
let n;
|
|
949
|
+
n = typeof e == "string" ? {
|
|
950
|
+
...t,
|
|
951
|
+
name: e
|
|
952
|
+
} : e, this.name = n.name || "OptimizationModel", this.sense = l(n.sense), this.objective = new S(), n.solver && (this.solver = H(n.solver));
|
|
1194
953
|
}
|
|
1195
954
|
get vars() {
|
|
1196
955
|
return this.variables;
|
|
@@ -1204,6 +963,60 @@ var Y = class {
|
|
|
1204
963
|
get numConstrs() {
|
|
1205
964
|
return this.constraints.length;
|
|
1206
965
|
}
|
|
966
|
+
createDomainVars(e, t, n, r) {
|
|
967
|
+
if (typeof n == "number" || Array.isArray(n)) {
|
|
968
|
+
let i = typeof t == "number" ? Array.from({ length: t }, (e, t) => t) : t, a = typeof n == "number" ? Array.from({ length: n }, (e, t) => t) : n, o = r || {}, s = o.prefix || o.name || "x", c = {};
|
|
969
|
+
for (let t of i) {
|
|
970
|
+
c[t] = {};
|
|
971
|
+
for (let n of a) c[t][n] = this.addVar({
|
|
972
|
+
...o,
|
|
973
|
+
type: e,
|
|
974
|
+
name: `${s}_${t}_${n}`
|
|
975
|
+
});
|
|
976
|
+
}
|
|
977
|
+
return c;
|
|
978
|
+
}
|
|
979
|
+
if (typeof t == "string") {
|
|
980
|
+
let r = n || {};
|
|
981
|
+
return this.addVar({
|
|
982
|
+
...r,
|
|
983
|
+
type: e,
|
|
984
|
+
name: t
|
|
985
|
+
});
|
|
986
|
+
}
|
|
987
|
+
let i = n || {}, a = i.prefix || i.name;
|
|
988
|
+
if (typeof t == "number") {
|
|
989
|
+
let n = [], r = a || "x";
|
|
990
|
+
for (let a = 0; a < t; a++) n.push(this.addVar({
|
|
991
|
+
...i,
|
|
992
|
+
type: e,
|
|
993
|
+
name: `${r}_${a}`
|
|
994
|
+
}));
|
|
995
|
+
return n;
|
|
996
|
+
}
|
|
997
|
+
if (Array.isArray(t) || t && typeof t[Symbol.iterator] == "function") {
|
|
998
|
+
let n = {};
|
|
999
|
+
for (let r of t) {
|
|
1000
|
+
let t = a ? `${a}_${r}` : `${r}`;
|
|
1001
|
+
n[r] = this.addVar({
|
|
1002
|
+
...i,
|
|
1003
|
+
type: e,
|
|
1004
|
+
name: t
|
|
1005
|
+
});
|
|
1006
|
+
}
|
|
1007
|
+
return n;
|
|
1008
|
+
}
|
|
1009
|
+
throw Error(`Invalid arguments provided for ${e.toLowerCase()} variable creation`);
|
|
1010
|
+
}
|
|
1011
|
+
binary(e, t, n) {
|
|
1012
|
+
return this.createDomainVars("Binary", e, t, n);
|
|
1013
|
+
}
|
|
1014
|
+
integer(e, t, n) {
|
|
1015
|
+
return this.createDomainVars("Integer", e, t, n);
|
|
1016
|
+
}
|
|
1017
|
+
continuous(e, t, n) {
|
|
1018
|
+
return this.createDomainVars("Continuous", e, t, n);
|
|
1019
|
+
}
|
|
1207
1020
|
addVar(e, t, n, r) {
|
|
1208
1021
|
let i;
|
|
1209
1022
|
i = typeof e == "string" ? {
|
|
@@ -1212,7 +1025,7 @@ var Y = class {
|
|
|
1212
1025
|
lb: n,
|
|
1213
1026
|
ub: r
|
|
1214
1027
|
} : e || {};
|
|
1215
|
-
let a = new
|
|
1028
|
+
let a = new E(i);
|
|
1216
1029
|
if (this.varMapByName.has(a.name)) throw Error(`Variable with name '${a.name}' already exists in model`);
|
|
1217
1030
|
return this.variables.push(a), this.varMapByName.set(a.name, a), a;
|
|
1218
1031
|
}
|
|
@@ -1267,7 +1080,8 @@ var Y = class {
|
|
|
1267
1080
|
return this.constrMapByName.get(e);
|
|
1268
1081
|
}
|
|
1269
1082
|
setObjective(e, t) {
|
|
1270
|
-
|
|
1083
|
+
if (e instanceof y) throw Error("Cannot set objective to a constraint comparison (<=, >=, ==). Did you mean to use m.constrain(expr`... <= ...`)? For objectives, supply an unconstrained expression like expr`45 * ${x} + 80 * ${y}`.");
|
|
1084
|
+
t && (this.sense = l(t)), this.objective = typeof e == "string" ? k(e, (e) => this.getVarByName(e)) : typeof e == "number" ? new S(/* @__PURE__ */ new Map(), e) : e instanceof x || "terms" in e ? e.clone() : new S(/* @__PURE__ */ new Map([[e, 1]]), 0);
|
|
1271
1085
|
}
|
|
1272
1086
|
maximize(e) {
|
|
1273
1087
|
this.setObjective(e, "Maximize");
|
|
@@ -1275,12 +1089,32 @@ var Y = class {
|
|
|
1275
1089
|
minimize(e) {
|
|
1276
1090
|
this.setObjective(e, "Minimize");
|
|
1277
1091
|
}
|
|
1092
|
+
constrain(e, t) {
|
|
1093
|
+
if (Array.isArray(e)) {
|
|
1094
|
+
for (let t of e) this.constrain(t);
|
|
1095
|
+
return this;
|
|
1096
|
+
}
|
|
1097
|
+
if (e instanceof S) throw Error("Cannot add a linear expression without a comparison operator (<=, >=, ==) as a constraint. Use expr`... <= ...` or .le(), .ge(), .eq().");
|
|
1098
|
+
return this.addConstraint(e, t), this;
|
|
1099
|
+
}
|
|
1100
|
+
getValues(e) {
|
|
1101
|
+
if (e == null) return null;
|
|
1102
|
+
if (e instanceof E) return e.value;
|
|
1103
|
+
if (Array.isArray(e)) return e.map((e) => this.getValues(e));
|
|
1104
|
+
if (typeof e == "object") {
|
|
1105
|
+
let t = {};
|
|
1106
|
+
for (let [n, r] of Object.entries(e)) t[n] = this.getValues(r);
|
|
1107
|
+
return t;
|
|
1108
|
+
}
|
|
1109
|
+
return null;
|
|
1110
|
+
}
|
|
1278
1111
|
addBoolVar(e) {
|
|
1279
|
-
let t = new
|
|
1112
|
+
let t = new f(e);
|
|
1280
1113
|
return this.boolVars.push(t), t;
|
|
1281
1114
|
}
|
|
1282
1115
|
addConstraint(e, t) {
|
|
1283
|
-
if (e instanceof
|
|
1116
|
+
if (e instanceof S) throw Error("Cannot add a linear expression without a comparison operator (<=, >=, ==) as a constraint. Use expr`... <= ...` or .le(), .ge(), .eq().");
|
|
1117
|
+
if (e instanceof d) {
|
|
1284
1118
|
let n = t || `lc_${this.logicalConstraints.length + 1}`;
|
|
1285
1119
|
return this.logicalConstraints.push({
|
|
1286
1120
|
name: n,
|
|
@@ -1288,7 +1122,7 @@ var Y = class {
|
|
|
1288
1122
|
}), e;
|
|
1289
1123
|
}
|
|
1290
1124
|
let n;
|
|
1291
|
-
if (typeof e == "string" ? n =
|
|
1125
|
+
if (typeof e == "string" ? n = A(e, (e) => this.getVarByName(e), t) : (n = e, t && (n.name = t)), this.constrMapByName.has(n.name)) throw Error(`Constraint with name '${n.name}' already exists in model`);
|
|
1292
1126
|
return this.constraints.push(n), this.constrMapByName.set(n.name, n), n;
|
|
1293
1127
|
}
|
|
1294
1128
|
addConstr(e, t) {
|
|
@@ -1302,33 +1136,37 @@ var Y = class {
|
|
|
1302
1136
|
for (let t of e) this.add(t);
|
|
1303
1137
|
return this;
|
|
1304
1138
|
}
|
|
1305
|
-
return e instanceof
|
|
1139
|
+
return e instanceof E ? (this.varMapByName.has(e.name) || (this.variables.push(e), this.varMapByName.set(e.name, e)), this) : (this.addConstraint(e, t), this);
|
|
1306
1140
|
}
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1141
|
+
indicator(e, t, n = {}) {
|
|
1142
|
+
if (!("sense" in t) || !("lhs" in t)) throw Error("Invalid constraint expression passed to indicator(). Expression must include a comparison operator (<=, >=, ==).");
|
|
1143
|
+
let r = t, i = n.activeValue === void 0 ? 1 : n.activeValue, a = n.M === void 0 ? 1e5 : n.M, o = n.name || `ind_${e.name}_${r.name}`, s = [], { lhs: c, sense: l, rhs: u } = r;
|
|
1144
|
+
if (i === 1) {
|
|
1145
|
+
if (l === "<=" || l === "==") {
|
|
1146
|
+
let t = this.addConstraint(c.add(e.mul(a)).le(u + a), l === "==" ? `${o}_le` : o);
|
|
1147
|
+
s.push(t);
|
|
1313
1148
|
}
|
|
1314
|
-
if (
|
|
1315
|
-
let t = this.addConstraint(
|
|
1316
|
-
|
|
1149
|
+
if (l === ">=" || l === "==") {
|
|
1150
|
+
let t = this.addConstraint(c.sub(e.mul(a)).ge(u - a), l === "==" ? `${o}_ge` : o);
|
|
1151
|
+
s.push(t);
|
|
1317
1152
|
}
|
|
1318
1153
|
} else {
|
|
1319
|
-
if (
|
|
1320
|
-
let t = this.addConstraint(
|
|
1321
|
-
|
|
1154
|
+
if (l === "<=" || l === "==") {
|
|
1155
|
+
let t = this.addConstraint(c.sub(e.mul(a)).le(u), l === "==" ? `${o}_le` : o);
|
|
1156
|
+
s.push(t);
|
|
1322
1157
|
}
|
|
1323
|
-
if (
|
|
1324
|
-
let t = this.addConstraint(
|
|
1325
|
-
|
|
1158
|
+
if (l === ">=" || l === "==") {
|
|
1159
|
+
let t = this.addConstraint(c.add(e.mul(a)).ge(u), l === "==" ? `${o}_ge` : o);
|
|
1160
|
+
s.push(t);
|
|
1326
1161
|
}
|
|
1327
1162
|
}
|
|
1328
|
-
return
|
|
1163
|
+
return s;
|
|
1164
|
+
}
|
|
1165
|
+
addIndicator(e, t, n = {}) {
|
|
1166
|
+
return this.indicator(e, t, n);
|
|
1329
1167
|
}
|
|
1330
1168
|
toLpString() {
|
|
1331
|
-
let { lpString: e } =
|
|
1169
|
+
let { lpString: e } = F({
|
|
1332
1170
|
name: this.name,
|
|
1333
1171
|
sense: this.sense,
|
|
1334
1172
|
objective: this.objective,
|
|
@@ -1339,7 +1177,7 @@ var Y = class {
|
|
|
1339
1177
|
}
|
|
1340
1178
|
async optimize(e, t) {
|
|
1341
1179
|
let n, r;
|
|
1342
|
-
if (e && typeof e == "object" && "solve" in e && typeof e.solve == "function" ? (n = e, r = t || {}) : typeof e == "function" ? (n = new e(t), r = t || {}) : typeof e == "string" ? (n =
|
|
1180
|
+
if (e && typeof e == "object" && "solve" in e && typeof e.solve == "function" ? (n = e, r = t || {}) : typeof e == "function" ? (n = new e(t), r = t || {}) : typeof e == "string" ? (n = H(e, t), r = t || {}) : (r = { ...e }, r.solver ? (n = H(r.solver, r), delete r.solver) : n = this.solver || H()), this.objective instanceof x && this.objective.quadTerms.size > 0 && n.capabilities?.supportsQP === !1) throw Error(`Solver "${n.name}" does not support quadratic objectives (QP). Please use a QP-capable solver such as HiGHS or Clarabel.`);
|
|
1343
1181
|
let i = {
|
|
1344
1182
|
name: this.name,
|
|
1345
1183
|
sense: this.sense,
|
|
@@ -1348,13 +1186,23 @@ var Y = class {
|
|
|
1348
1186
|
variables: this.variables,
|
|
1349
1187
|
logicalConstraints: this.logicalConstraints,
|
|
1350
1188
|
boolVars: this.boolVars
|
|
1351
|
-
}, a =
|
|
1189
|
+
}, a = F(i), o = await n.solve(i, a, r);
|
|
1352
1190
|
this.runTime = o.runTime ?? 0, this.status = o.status, this.objectiveValue = o.objectiveValue ?? null;
|
|
1353
1191
|
let s = {}, c = {}, l = {}, u = {};
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1192
|
+
this.values.clear();
|
|
1193
|
+
for (let e of this.variables) e.value = null, e.reducedCost = null;
|
|
1194
|
+
for (let e of this.boolVars) e.value = null;
|
|
1195
|
+
for (let e of this.constraints) e.dual = null, e.slack = null;
|
|
1196
|
+
if (a?.nameToVar) for (let e of a.nameToVar.values()) e.value = null, e.reducedCost = null;
|
|
1197
|
+
if (a?.nameToConstraint) for (let e of a.nameToConstraint.values()) e.dual = null, e.slack = null;
|
|
1198
|
+
if (o.values && o.status !== "Infeasible" && o.status !== "Unbounded") {
|
|
1199
|
+
for (let [e, t] of Object.entries(o.values)) if (typeof t == "number" && !Number.isNaN(t)) {
|
|
1200
|
+
this.values.set(e, t);
|
|
1201
|
+
let n = this.getVarByName(e) || a.nameToVar.get(e);
|
|
1202
|
+
n && (n.value = t, s[n.name] = t);
|
|
1203
|
+
let r = this.boolVars.find((t) => t.name === e);
|
|
1204
|
+
r && (r.value = !!t);
|
|
1205
|
+
}
|
|
1358
1206
|
}
|
|
1359
1207
|
if (o.reducedCosts) for (let [e, t] of Object.entries(o.reducedCosts)) {
|
|
1360
1208
|
let n = this.getVarByName(e) || a.nameToVar.get(e);
|
|
@@ -1384,7 +1232,7 @@ var Y = class {
|
|
|
1384
1232
|
}
|
|
1385
1233
|
async check(e, t) {
|
|
1386
1234
|
let n, r;
|
|
1387
|
-
if (e && typeof e == "object" && "solve" in e && typeof e.solve == "function" ? (n = e, r = t || {}) : typeof e == "function" ? (n = new e(t), r = t || {}) : typeof e == "string" ? (n =
|
|
1235
|
+
if (e && typeof e == "object" && "solve" in e && typeof e.solve == "function" ? (n = e, r = t || {}) : typeof e == "function" ? (n = new e(t), r = t || {}) : typeof e == "string" ? (n = H(e, t), r = t || {}) : (r = { ...e }, r.solver ? (n = H(r.solver, r), delete r.solver) : n = this.solver || H()), this.logicalConstraints.length > 0 && n.capabilities?.supportsSMT !== !0) throw Error(`Solver "${n.name}" does not support SMT / logical constraints (or, implies, not, xor). Please use an SMT-capable solver such as Z3.`);
|
|
1388
1236
|
if (typeof n.check == "function") {
|
|
1389
1237
|
let e = await n.check({
|
|
1390
1238
|
name: this.name,
|
|
@@ -1393,13 +1241,15 @@ var Y = class {
|
|
|
1393
1241
|
constraints: this.constraints,
|
|
1394
1242
|
logicalConstraints: this.logicalConstraints
|
|
1395
1243
|
}, r);
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1244
|
+
this.status = e.status, this.runTime = e.runTime ?? 0, this.unsatCore = e.unsatCore || [], this.values.clear();
|
|
1245
|
+
for (let e of this.variables) e.value = null;
|
|
1246
|
+
for (let e of this.boolVars) e.value = null;
|
|
1247
|
+
if (e.values && e.status === "Sat") for (let [t, n] of Object.entries(e.values)) {
|
|
1248
|
+
this.values.set(t, n);
|
|
1249
|
+
let e = this.getVarByName(t);
|
|
1250
|
+
e && typeof n == "number" && (e.value = n);
|
|
1251
|
+
let r = this.boolVars.find((e) => e.name === t);
|
|
1252
|
+
r && (r.value = !!n);
|
|
1403
1253
|
}
|
|
1404
1254
|
return e;
|
|
1405
1255
|
}
|
|
@@ -1407,7 +1257,10 @@ var Y = class {
|
|
|
1407
1257
|
try {
|
|
1408
1258
|
this.objective = new S(), this.sense = "Minimize";
|
|
1409
1259
|
let e = await this.optimize(n, r), t = e === "Optimal" || e === "Feasible", i = e === "Infeasible", a = {};
|
|
1410
|
-
|
|
1260
|
+
if (t) {
|
|
1261
|
+
for (let e of this.variables) e.value !== null && (a[e.name] = e.value);
|
|
1262
|
+
for (let e of this.boolVars) e.value !== null && (a[e.name] = e.value);
|
|
1263
|
+
}
|
|
1411
1264
|
return {
|
|
1412
1265
|
status: t ? "Sat" : i ? "Unsat" : "Unknown",
|
|
1413
1266
|
values: a,
|
|
@@ -1417,131 +1270,6 @@ var Y = class {
|
|
|
1417
1270
|
this.objective = i, this.sense = a;
|
|
1418
1271
|
}
|
|
1419
1272
|
}
|
|
1420
|
-
}, X = "Minimize", de = "Maximize", fe = "Continuous", pe = "Integer", me = "Binary", he = {
|
|
1421
|
-
Optimal: "Optimal",
|
|
1422
|
-
Feasible: "Feasible",
|
|
1423
|
-
Infeasible: "Infeasible",
|
|
1424
|
-
Unbounded: "Unbounded",
|
|
1425
|
-
TimeLimit: "Not Solved (Time Limit)",
|
|
1426
|
-
IterationLimit: "Not Solved (Iteration Limit)",
|
|
1427
|
-
Error: "Error",
|
|
1428
|
-
NotSolved: "Not Solved",
|
|
1429
|
-
Sat: "Sat",
|
|
1430
|
-
Unsat: "Unsat",
|
|
1431
|
-
Unknown: "Unknown"
|
|
1432
|
-
}, ge = class e extends D {
|
|
1433
|
-
constructor(e, t, n, r = "Continuous") {
|
|
1434
|
-
super({
|
|
1435
|
-
name: e,
|
|
1436
|
-
lb: t === void 0 ? 0 : t,
|
|
1437
|
-
ub: n === void 0 ? Infinity : n,
|
|
1438
|
-
type: r
|
|
1439
|
-
});
|
|
1440
|
-
}
|
|
1441
|
-
static dicts(t, n, r, i, a = "Continuous") {
|
|
1442
|
-
let o = {};
|
|
1443
|
-
for (let s of n) {
|
|
1444
|
-
let n = `${t}_${s}`;
|
|
1445
|
-
o[s] = new e(n, r, i, a);
|
|
1446
|
-
}
|
|
1447
|
-
return o;
|
|
1448
|
-
}
|
|
1449
|
-
};
|
|
1450
|
-
function _e(e) {
|
|
1451
|
-
return e == null ? null : typeof e == "number" ? e : e.value;
|
|
1452
|
-
}
|
|
1453
|
-
var Z = class {
|
|
1454
|
-
model;
|
|
1455
|
-
constructor(e = "NoName", t = X) {
|
|
1456
|
-
this.model = new Y({
|
|
1457
|
-
name: e,
|
|
1458
|
-
sense: t
|
|
1459
|
-
});
|
|
1460
|
-
}
|
|
1461
|
-
get name() {
|
|
1462
|
-
return this.model.name;
|
|
1463
|
-
}
|
|
1464
|
-
get sense() {
|
|
1465
|
-
return this.model.sense;
|
|
1466
|
-
}
|
|
1467
|
-
set sense(e) {
|
|
1468
|
-
this.model.sense = e;
|
|
1469
|
-
}
|
|
1470
|
-
get objective() {
|
|
1471
|
-
return this.model.objective;
|
|
1472
|
-
}
|
|
1473
|
-
get variables() {
|
|
1474
|
-
return this.model.variables;
|
|
1475
|
-
}
|
|
1476
|
-
get constraints() {
|
|
1477
|
-
return this.model.constraints;
|
|
1478
|
-
}
|
|
1479
|
-
get status() {
|
|
1480
|
-
return this.model.status;
|
|
1481
|
-
}
|
|
1482
|
-
get objectiveValue() {
|
|
1483
|
-
return this.model.objectiveValue;
|
|
1484
|
-
}
|
|
1485
|
-
addVariable(e) {
|
|
1486
|
-
return this.model.getVarByName(e.name) || (this.model.variables.push(e), this.model.varMapByName.set(e.name, e)), e;
|
|
1487
|
-
}
|
|
1488
|
-
setObjective(e) {
|
|
1489
|
-
if (typeof e == "object" && e) {
|
|
1490
|
-
if ("terms" in e) for (let [t] of e.terms) this.addVariable(t);
|
|
1491
|
-
else e instanceof D && this.addVariable(e);
|
|
1492
|
-
}
|
|
1493
|
-
this.model.setObjective(e);
|
|
1494
|
-
}
|
|
1495
|
-
addConstraint(e, t) {
|
|
1496
|
-
if (typeof e == "object" && e && "lhs" in e) for (let [t] of e.lhs.terms) this.addVariable(t);
|
|
1497
|
-
return this.model.addConstraint(e, t);
|
|
1498
|
-
}
|
|
1499
|
-
add(e, t) {
|
|
1500
|
-
typeof e == "string" ? /(<=|>=|==|=<|=>|=)/.test(e) ? this.addConstraint(e, t) : this.setObjective(e) : e instanceof y ? this.addConstraint(e, t) : this.setObjective(e);
|
|
1501
|
-
}
|
|
1502
|
-
async solve(e, t) {
|
|
1503
|
-
return this.model.optimize(e, t);
|
|
1504
|
-
}
|
|
1505
|
-
toLpString() {
|
|
1506
|
-
return this.model.toLpString();
|
|
1507
|
-
}
|
|
1508
|
-
}, Q = "Minimize", $ = "Maximize", ve = "Binary", ye = "Integer", be = "Continuous";
|
|
1509
|
-
function xe(e) {
|
|
1510
|
-
return {
|
|
1511
|
-
expr: e,
|
|
1512
|
-
sense: $
|
|
1513
|
-
};
|
|
1514
|
-
}
|
|
1515
|
-
function Se(e) {
|
|
1516
|
-
return {
|
|
1517
|
-
expr: e,
|
|
1518
|
-
sense: Q
|
|
1519
|
-
};
|
|
1520
|
-
}
|
|
1521
|
-
var Ce = class extends Y {
|
|
1522
|
-
constructor(e = "MipModel", t = Q) {
|
|
1523
|
-
super({
|
|
1524
|
-
name: e,
|
|
1525
|
-
sense: t
|
|
1526
|
-
});
|
|
1527
|
-
}
|
|
1528
|
-
add_var(e = {}) {
|
|
1529
|
-
return this.addVar({
|
|
1530
|
-
name: e.name,
|
|
1531
|
-
type: e.var_type,
|
|
1532
|
-
lb: e.lb,
|
|
1533
|
-
ub: e.ub
|
|
1534
|
-
});
|
|
1535
|
-
}
|
|
1536
|
-
set objective_spec(e) {
|
|
1537
|
-
this.setObjective(e.expr, e.sense);
|
|
1538
|
-
}
|
|
1539
|
-
get objective_value() {
|
|
1540
|
-
return this.objectiveValue;
|
|
1541
|
-
}
|
|
1542
|
-
add_constr(e, t) {
|
|
1543
|
-
return this.addConstraint(e, t);
|
|
1544
|
-
}
|
|
1545
1273
|
};
|
|
1546
1274
|
//#endregion
|
|
1547
|
-
export {
|
|
1275
|
+
export { d as BoolExpr, p as BoolOp, f as BoolVar, y as Constraint, Q as HiGHS, Z as HighsSolver, S as LinExpr, $ as MockSolver, ie as Model, c as OFFICIAL_HIGHS_CDN_WASM_URL, x as QuadExpr, E as Var, m as and, M as c, w as dot, N as expr, F as generateLpString, i as getHighs, o as getHighsWasmUrl, H as getSolver, _ as implies, n as isValidWasmHeader, U as listAvailableSolvers, l as normalizeSense, g as not, h as or, A as parseConstraint, k as parseExpr, e as probeWasmUrl, B as registerSolver, a as resolveBrowserWasmUrl, V as setDefaultSolver, s as setHighsWasmUrl, t as solveLpWithHighs, C as sum, v as xor };
|