@ohtaman/washi 0.2.0 → 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 +131 -115
- package/README.md +131 -115
- package/README.zh-CN.md +133 -117
- package/dist/browser/solvers/glpk.js +10 -2
- package/dist/browser/solvers/glpk.min.js +1 -1
- package/dist/browser/solvers/z3.js +34 -12
- package/dist/browser/solvers/z3.min.js +2 -2
- package/dist/browser/washi.js +368 -306
- package/dist/browser/washi.min.js +4 -4
- package/dist/lib/{constraint-XWdn0ID9.js → constraint-BRuB3ooC.js} +0 -3
- package/dist/lib/solvers/glpk.js +10 -2
- package/dist/lib/solvers/z3.js +35 -10
- package/dist/lib/washi.js +368 -321
- package/dist/types/index.d.ts +5 -7
- 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 +2 -4
- package/dist/types/modeler/index.d.ts.map +1 -1
- package/dist/types/modeler/model.d.ts +90 -11
- package/dist/types/modeler/model.d.ts.map +1 -1
- package/dist/types/modeler/solvers/glpk/solver.d.ts.map +1 -1
- package/dist/types/modeler/solvers/highs/solver.d.ts +1 -1
- package/dist/types/modeler/solvers/highs/solver.d.ts.map +1 -1
- package/dist/types/modeler/solvers/types.d.ts +1 -1
- package/dist/types/modeler/solvers/z3/loader.d.ts +12 -0
- package/dist/types/modeler/solvers/z3/loader.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 +10 -2
- 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/washi.js
CHANGED
|
@@ -1,32 +1,37 @@
|
|
|
1
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 ee = 0,
|
|
64
|
+
var ee = 0, y = class {
|
|
60
65
|
id;
|
|
61
66
|
lhs;
|
|
62
67
|
sense;
|
|
@@ -67,23 +72,20 @@ var ee = 0, v = class {
|
|
|
67
72
|
constructor(e, t, n, r) {
|
|
68
73
|
this.id = ++ee, this.lhs = e, this.sense = t, this.rhs = n, this.name = r || `c_${this.id}`;
|
|
69
74
|
}
|
|
70
|
-
get pi() {
|
|
71
|
-
return this.dual;
|
|
72
|
-
}
|
|
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;
|
|
@@ -92,11 +94,11 @@ var ee = 0, v = class {
|
|
|
92
94
|
};
|
|
93
95
|
//#endregion
|
|
94
96
|
//#region src/modeler/quad-expr.ts
|
|
95
|
-
function
|
|
97
|
+
function b(e, t) {
|
|
96
98
|
let n = e.id, r = t.id;
|
|
97
99
|
return n <= r ? `${n}:${r}` : `${r}:${n}`;
|
|
98
100
|
}
|
|
99
|
-
var
|
|
101
|
+
var x = class e {
|
|
100
102
|
linearTerms;
|
|
101
103
|
quadTerms;
|
|
102
104
|
constant;
|
|
@@ -109,7 +111,7 @@ var b = class e {
|
|
|
109
111
|
return new e(this.linearTerms, this.quadTerms, this.constant);
|
|
110
112
|
}
|
|
111
113
|
getQuadCoeff(e, t) {
|
|
112
|
-
let n =
|
|
114
|
+
let n = b(e, t), r = this.quadTerms.get(n);
|
|
113
115
|
return r ? r.coeff : 0;
|
|
114
116
|
}
|
|
115
117
|
add(t) {
|
|
@@ -182,14 +184,14 @@ var b = class e {
|
|
|
182
184
|
return this.value;
|
|
183
185
|
}
|
|
184
186
|
static fromProduct(t, n, r = 1) {
|
|
185
|
-
let i = new e(), a =
|
|
187
|
+
let i = new e(), a = b(t, n);
|
|
186
188
|
return i.quadTerms.set(a, {
|
|
187
189
|
var1: t.id <= n.id ? t : n,
|
|
188
190
|
var2: t.id <= n.id ? n : t,
|
|
189
191
|
coeff: r
|
|
190
192
|
}), i;
|
|
191
193
|
}
|
|
192
|
-
},
|
|
194
|
+
}, S = class e {
|
|
193
195
|
terms;
|
|
194
196
|
constant;
|
|
195
197
|
constructor(e = /* @__PURE__ */ new Map(), t = 0) {
|
|
@@ -199,7 +201,7 @@ var b = class e {
|
|
|
199
201
|
return new e(this.terms, this.constant);
|
|
200
202
|
}
|
|
201
203
|
add(e) {
|
|
202
|
-
if (e instanceof
|
|
204
|
+
if (e instanceof x) return e.add(this);
|
|
203
205
|
let t = this.clone();
|
|
204
206
|
if (typeof e == "number") t.constant += e;
|
|
205
207
|
else if ("terms" in e) {
|
|
@@ -215,7 +217,7 @@ var b = class e {
|
|
|
215
217
|
return t;
|
|
216
218
|
}
|
|
217
219
|
sub(e) {
|
|
218
|
-
return e instanceof
|
|
220
|
+
return e instanceof x ? e.neg().add(this) : typeof e == "number" ? this.add(-e) : ("terms" in e, this.add(e.mul(-1)));
|
|
219
221
|
}
|
|
220
222
|
mul(t) {
|
|
221
223
|
if (typeof t == "number") {
|
|
@@ -226,9 +228,9 @@ var b = class e {
|
|
|
226
228
|
}
|
|
227
229
|
return n;
|
|
228
230
|
}
|
|
229
|
-
let n = new
|
|
231
|
+
let n = new x();
|
|
230
232
|
Math.abs(this.constant) >= 1e-12 && n.linearTerms.set(t, this.constant);
|
|
231
|
-
for (let [e, r] of this.terms) n = n.add(
|
|
233
|
+
for (let [e, r] of this.terms) n = n.add(x.fromProduct(e, t, r));
|
|
232
234
|
return n;
|
|
233
235
|
}
|
|
234
236
|
div(e) {
|
|
@@ -271,7 +273,7 @@ var b = class e {
|
|
|
271
273
|
let i = this.clone(), a = 0;
|
|
272
274
|
typeof n == "number" ? a = n : "terms" in n ? (i = i.sub(new e(n.terms, 0)), a = n.constant) : i = i.sub(n);
|
|
273
275
|
let o = a - i.constant;
|
|
274
|
-
return i.constant = 0, new
|
|
276
|
+
return i.constant = 0, new y(i, t, o, r);
|
|
275
277
|
}
|
|
276
278
|
toString() {
|
|
277
279
|
let e = [], t = !0;
|
|
@@ -286,27 +288,113 @@ var b = class e {
|
|
|
286
288
|
return e.join(" ") || "0";
|
|
287
289
|
}
|
|
288
290
|
};
|
|
289
|
-
function
|
|
290
|
-
let n = /* @__PURE__ */ new Map(), r = 0
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
291
|
+
function C(e, t) {
|
|
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);
|
|
299
306
|
}
|
|
300
|
-
r += t.constant;
|
|
301
|
-
} else if (e) {
|
|
302
|
-
let t = e, r = n.get(t) || 0;
|
|
303
|
-
n.set(t, r + 1);
|
|
304
307
|
}
|
|
305
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
|
+
}
|
|
306
316
|
for (let [e, t] of n) Math.abs(t) < 1e-12 && n.delete(e);
|
|
307
|
-
return new
|
|
317
|
+
return new S(n, r);
|
|
308
318
|
}
|
|
309
|
-
|
|
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(", ")}].`);
|
|
371
|
+
}
|
|
372
|
+
throw Error(`Type mismatch in dot() at '${n || "root"}': one operand is an Array while the other is not.`);
|
|
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"}'.`);
|
|
384
|
+
}
|
|
385
|
+
c(e, t);
|
|
386
|
+
for (let [e, t] of n) Math.abs(t) < 1e-12 && n.delete(e);
|
|
387
|
+
return new S(n, r);
|
|
388
|
+
}
|
|
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,48 +403,42 @@ var C = S, w = S, T = 0, E = 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
|
-
if (typeof t == "number") return new
|
|
328
|
-
if (t instanceof e) return
|
|
329
|
-
let n = new
|
|
409
|
+
if (typeof t == "number") return new S(/* @__PURE__ */ new Map([[this, t]]), 0);
|
|
410
|
+
if (t instanceof e) return x.fromProduct(this, t, 1);
|
|
411
|
+
let n = new x();
|
|
330
412
|
Math.abs(t.constant) >= 1e-12 && n.linearTerms.set(this, t.constant);
|
|
331
413
|
for (let [e, r] of t.terms) {
|
|
332
|
-
let t =
|
|
414
|
+
let t = x.fromProduct(this, e, r);
|
|
333
415
|
n = n.add(t);
|
|
334
416
|
}
|
|
335
417
|
return n;
|
|
336
418
|
}
|
|
337
419
|
add(e) {
|
|
338
|
-
return e instanceof
|
|
420
|
+
return e instanceof x ? e.add(this) : new S(/* @__PURE__ */ new Map([[this, 1]]), 0).add(e);
|
|
339
421
|
}
|
|
340
422
|
sub(e) {
|
|
341
|
-
return e instanceof
|
|
423
|
+
return e instanceof x ? e.neg().add(this) : new S(/* @__PURE__ */ new Map([[this, 1]]), 0).sub(e);
|
|
342
424
|
}
|
|
343
425
|
neg() {
|
|
344
426
|
return this.mul(-1);
|
|
345
427
|
}
|
|
346
428
|
le(e, t) {
|
|
347
|
-
return new
|
|
429
|
+
return new S(/* @__PURE__ */ new Map([[this, 1]]), 0).le(e, t);
|
|
348
430
|
}
|
|
349
431
|
lessThanOrEqual(e, t) {
|
|
350
432
|
return this.le(e, t);
|
|
351
433
|
}
|
|
352
434
|
ge(e, t) {
|
|
353
|
-
return new
|
|
435
|
+
return new S(/* @__PURE__ */ new Map([[this, 1]]), 0).ge(e, t);
|
|
354
436
|
}
|
|
355
437
|
greaterThanOrEqual(e, t) {
|
|
356
438
|
return this.ge(e, t);
|
|
357
439
|
}
|
|
358
440
|
eq(e, t) {
|
|
359
|
-
return new
|
|
441
|
+
return new S(/* @__PURE__ */ new Map([[this, 1]]), 0).eq(e, t);
|
|
360
442
|
}
|
|
361
443
|
equalTo(e, t) {
|
|
362
444
|
return this.eq(e, t);
|
|
@@ -367,7 +449,7 @@ var C = S, w = S, T = 0, E = 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 te(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;
|
|
@@ -485,12 +567,12 @@ var ne = class {
|
|
|
485
567
|
let e = this.consume();
|
|
486
568
|
return this.resolveVariable(e.value).mul(t);
|
|
487
569
|
}
|
|
488
|
-
return new
|
|
570
|
+
return new S(/* @__PURE__ */ new Map(), t);
|
|
489
571
|
}
|
|
490
572
|
if (e.type === "IDENT") {
|
|
491
573
|
this.consume();
|
|
492
574
|
let t = this.resolveVariable(e.value);
|
|
493
|
-
return new
|
|
575
|
+
return new S(/* @__PURE__ */ new Map([[t, 1]]), 0);
|
|
494
576
|
}
|
|
495
577
|
if (e.type === "LPAREN") {
|
|
496
578
|
this.consume("LPAREN");
|
|
@@ -505,10 +587,10 @@ var ne = 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,10 +598,10 @@ function O(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];
|
|
@@ -527,7 +609,7 @@ function k(e, t) {
|
|
|
527
609
|
else if (e instanceof E) {
|
|
528
610
|
let t = `__v_${i}__`;
|
|
529
611
|
n.set(t, e), r += t;
|
|
530
|
-
} else if (e instanceof
|
|
612
|
+
} else if (e instanceof S) {
|
|
531
613
|
`${i}`, r += `(${e.toString()})`;
|
|
532
614
|
for (let [t] of e.terms) n.set(t.name, t);
|
|
533
615
|
}
|
|
@@ -537,42 +619,42 @@ function k(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 === "
|
|
569
|
-
let f = [], p = !0, m = n instanceof
|
|
650
|
+
d.push(t && t.toLowerCase() === "maximize" ? "Maximize" : "Minimize");
|
|
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;
|
|
572
654
|
let n = o.get(e), r = t >= 0 ? p ? "" : "+ " : "- ", i = Math.abs(t), a = i === 1 ? "" : `${i} `;
|
|
573
655
|
f.push(`${r}${a}${n}`), p = !1;
|
|
574
656
|
}
|
|
575
|
-
if (n instanceof
|
|
657
|
+
if (n instanceof x && n.quadTerms.size > 0) {
|
|
576
658
|
let e = [], t = !0;
|
|
577
659
|
for (let r of n.quadTerms.values()) {
|
|
578
660
|
if (Math.abs(r.coeff) < 1e-12) continue;
|
|
@@ -624,38 +706,38 @@ function j(e) {
|
|
|
624
706
|
}
|
|
625
707
|
//#endregion
|
|
626
708
|
//#region src/modeler/solvers/registry.ts
|
|
627
|
-
var
|
|
628
|
-
|
|
629
|
-
var
|
|
630
|
-
function
|
|
631
|
-
|
|
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);
|
|
632
714
|
}
|
|
633
|
-
function
|
|
715
|
+
function V(e) {
|
|
634
716
|
if (typeof e == "string") {
|
|
635
|
-
let t =
|
|
717
|
+
let t = z.get(e.toLowerCase());
|
|
636
718
|
if (!t) throw Error(`Solver '${e}' is not registered.`);
|
|
637
|
-
|
|
638
|
-
} else
|
|
719
|
+
R[L] = t;
|
|
720
|
+
} else R[L] = typeof e == "function" ? e : () => e;
|
|
639
721
|
}
|
|
640
|
-
function
|
|
722
|
+
function H(e, t) {
|
|
641
723
|
if (!e) {
|
|
642
|
-
let e =
|
|
643
|
-
if (!e) throw Error(`No default solver available. Available solvers: ${
|
|
724
|
+
let e = R[L] || z.get("highs");
|
|
725
|
+
if (!e) throw Error(`No default solver available. Available solvers: ${U().join(", ")}`);
|
|
644
726
|
return e(t);
|
|
645
727
|
}
|
|
646
728
|
if (typeof e == "string") {
|
|
647
|
-
let n = e.toLowerCase(), r =
|
|
648
|
-
if (!r) throw Error(n === "glpk" ? `Solver 'glpk' not found. Available solvers: ${
|
|
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(", ")}`);
|
|
649
731
|
return r(t);
|
|
650
732
|
}
|
|
651
733
|
return e;
|
|
652
734
|
}
|
|
653
|
-
function
|
|
654
|
-
return Array.from(
|
|
735
|
+
function U() {
|
|
736
|
+
return Array.from(z.keys());
|
|
655
737
|
}
|
|
656
738
|
//#endregion
|
|
657
739
|
//#region \0washi-browser-worker-factory
|
|
658
|
-
function
|
|
740
|
+
function W() {
|
|
659
741
|
let e = r("highs-worker.js");
|
|
660
742
|
if (new URL(e).origin === window.location.origin) return new Worker(e, { type: "module" });
|
|
661
743
|
let t = URL.createObjectURL(new Blob(["import " + JSON.stringify(e) + ";"], { type: "text/javascript" }));
|
|
@@ -668,41 +750,41 @@ function ae() {
|
|
|
668
750
|
}
|
|
669
751
|
//#endregion
|
|
670
752
|
//#region src/modeler/solvers/highs/worker-client.ts
|
|
671
|
-
var
|
|
672
|
-
function
|
|
753
|
+
var G = null, K = 0, q = /* @__PURE__ */ new Map();
|
|
754
|
+
function J() {
|
|
673
755
|
if (typeof window > "u" || typeof Worker > "u") return null;
|
|
674
|
-
if (!
|
|
675
|
-
|
|
676
|
-
let e =
|
|
677
|
-
|
|
678
|
-
let { type: t, id: n, result: r, runTime: i, error: a } = e.data, o =
|
|
679
|
-
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({
|
|
680
762
|
result: r,
|
|
681
763
|
runTime: i
|
|
682
764
|
}) : o.reject(Error(a || "Worker solve failed")));
|
|
683
765
|
};
|
|
684
766
|
let t = (t) => {
|
|
685
|
-
if (
|
|
686
|
-
|
|
687
|
-
for (let e of
|
|
688
|
-
|
|
767
|
+
if (G === e) {
|
|
768
|
+
G = null, e.terminate();
|
|
769
|
+
for (let e of q.values()) e.reject(t);
|
|
770
|
+
q.clear();
|
|
689
771
|
}
|
|
690
772
|
};
|
|
691
|
-
|
|
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."));
|
|
692
774
|
} catch {
|
|
693
|
-
|
|
775
|
+
G = null;
|
|
694
776
|
}
|
|
695
|
-
return
|
|
777
|
+
return G;
|
|
696
778
|
}
|
|
697
|
-
async function
|
|
698
|
-
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;
|
|
699
781
|
if (!i) return t(e, n);
|
|
700
782
|
let o = {
|
|
701
783
|
...n,
|
|
702
784
|
wasmUrl: r
|
|
703
|
-
}, s = `req_${++
|
|
785
|
+
}, s = `req_${++K}`;
|
|
704
786
|
return new Promise((t, n) => {
|
|
705
|
-
|
|
787
|
+
q.set(s, {
|
|
706
788
|
resolve: t,
|
|
707
789
|
reject: n
|
|
708
790
|
});
|
|
@@ -714,13 +796,13 @@ async function W(e, n = {}) {
|
|
|
714
796
|
options: o
|
|
715
797
|
});
|
|
716
798
|
} catch (e) {
|
|
717
|
-
|
|
799
|
+
q.delete(s), n(e);
|
|
718
800
|
}
|
|
719
801
|
});
|
|
720
802
|
}
|
|
721
803
|
//#endregion
|
|
722
804
|
//#region src/modeler/solvers/highs/solver.ts
|
|
723
|
-
function
|
|
805
|
+
function X(e) {
|
|
724
806
|
if (typeof e != "string") return "Unknown";
|
|
725
807
|
let t = e.trim();
|
|
726
808
|
if (!t) return "Unknown";
|
|
@@ -749,7 +831,7 @@ function G(e) {
|
|
|
749
831
|
let n = t.toLowerCase();
|
|
750
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";
|
|
751
833
|
}
|
|
752
|
-
var
|
|
834
|
+
var Z = class e {
|
|
753
835
|
static capabilities = {
|
|
754
836
|
supportsMIP: !0,
|
|
755
837
|
supportsQP: !0
|
|
@@ -765,7 +847,18 @@ var K = class e {
|
|
|
765
847
|
let r = {
|
|
766
848
|
...this.options,
|
|
767
849
|
...n
|
|
768
|
-
}, { 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 = {};
|
|
769
862
|
if (s.Columns) for (let [e, t] of Object.entries(s.Columns)) {
|
|
770
863
|
let n = a.get(e);
|
|
771
864
|
if (n) {
|
|
@@ -794,14 +887,14 @@ var K = class e {
|
|
|
794
887
|
raw: s
|
|
795
888
|
};
|
|
796
889
|
}
|
|
797
|
-
},
|
|
798
|
-
function
|
|
799
|
-
|
|
890
|
+
}, Q = Z;
|
|
891
|
+
function ne() {
|
|
892
|
+
B("highs", (e) => new Z(e)), V((e) => new Z(e));
|
|
800
893
|
}
|
|
801
|
-
|
|
894
|
+
ne();
|
|
802
895
|
//#endregion
|
|
803
896
|
//#region src/modeler/solvers/mock.ts
|
|
804
|
-
var
|
|
897
|
+
var $ = class {
|
|
805
898
|
name;
|
|
806
899
|
capabilities;
|
|
807
900
|
options;
|
|
@@ -828,13 +921,13 @@ var J = class {
|
|
|
828
921
|
};
|
|
829
922
|
}
|
|
830
923
|
};
|
|
831
|
-
function
|
|
832
|
-
|
|
924
|
+
function re() {
|
|
925
|
+
B("mock", (e) => new $(e));
|
|
833
926
|
}
|
|
834
|
-
|
|
927
|
+
re();
|
|
835
928
|
//#endregion
|
|
836
929
|
//#region src/modeler/model.ts
|
|
837
|
-
var
|
|
930
|
+
var ie = class {
|
|
838
931
|
name;
|
|
839
932
|
sense;
|
|
840
933
|
objective;
|
|
@@ -856,7 +949,7 @@ var Y = class {
|
|
|
856
949
|
n = typeof e == "string" ? {
|
|
857
950
|
...t,
|
|
858
951
|
name: e
|
|
859
|
-
} : e, this.name = n.name || "OptimizationModel", this.sense = n.sense
|
|
952
|
+
} : e, this.name = n.name || "OptimizationModel", this.sense = l(n.sense), this.objective = new S(), n.solver && (this.solver = H(n.solver));
|
|
860
953
|
}
|
|
861
954
|
get vars() {
|
|
862
955
|
return this.variables;
|
|
@@ -870,6 +963,60 @@ var Y = class {
|
|
|
870
963
|
get numConstrs() {
|
|
871
964
|
return this.constraints.length;
|
|
872
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
|
+
}
|
|
873
1020
|
addVar(e, t, n, r) {
|
|
874
1021
|
let i;
|
|
875
1022
|
i = typeof e == "string" ? {
|
|
@@ -933,7 +1080,8 @@ var Y = class {
|
|
|
933
1080
|
return this.constrMapByName.get(e);
|
|
934
1081
|
}
|
|
935
1082
|
setObjective(e, t) {
|
|
936
|
-
|
|
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);
|
|
937
1085
|
}
|
|
938
1086
|
maximize(e) {
|
|
939
1087
|
this.setObjective(e, "Maximize");
|
|
@@ -941,12 +1089,32 @@ var Y = class {
|
|
|
941
1089
|
minimize(e) {
|
|
942
1090
|
this.setObjective(e, "Minimize");
|
|
943
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
|
+
}
|
|
944
1111
|
addBoolVar(e) {
|
|
945
|
-
let t = new
|
|
1112
|
+
let t = new f(e);
|
|
946
1113
|
return this.boolVars.push(t), t;
|
|
947
1114
|
}
|
|
948
1115
|
addConstraint(e, t) {
|
|
949
|
-
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) {
|
|
950
1118
|
let n = t || `lc_${this.logicalConstraints.length + 1}`;
|
|
951
1119
|
return this.logicalConstraints.push({
|
|
952
1120
|
name: n,
|
|
@@ -954,7 +1122,7 @@ var Y = class {
|
|
|
954
1122
|
}), e;
|
|
955
1123
|
}
|
|
956
1124
|
let n;
|
|
957
|
-
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`);
|
|
958
1126
|
return this.constraints.push(n), this.constrMapByName.set(n.name, n), n;
|
|
959
1127
|
}
|
|
960
1128
|
addConstr(e, t) {
|
|
@@ -970,31 +1138,35 @@ var Y = class {
|
|
|
970
1138
|
}
|
|
971
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);
|
|
972
1140
|
}
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
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);
|
|
979
1148
|
}
|
|
980
|
-
if (
|
|
981
|
-
let t = this.addConstraint(
|
|
982
|
-
|
|
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);
|
|
983
1152
|
}
|
|
984
1153
|
} else {
|
|
985
|
-
if (
|
|
986
|
-
let t = this.addConstraint(
|
|
987
|
-
|
|
1154
|
+
if (l === "<=" || l === "==") {
|
|
1155
|
+
let t = this.addConstraint(c.sub(e.mul(a)).le(u), l === "==" ? `${o}_le` : o);
|
|
1156
|
+
s.push(t);
|
|
988
1157
|
}
|
|
989
|
-
if (
|
|
990
|
-
let t = this.addConstraint(
|
|
991
|
-
|
|
1158
|
+
if (l === ">=" || l === "==") {
|
|
1159
|
+
let t = this.addConstraint(c.add(e.mul(a)).ge(u), l === "==" ? `${o}_ge` : o);
|
|
1160
|
+
s.push(t);
|
|
992
1161
|
}
|
|
993
1162
|
}
|
|
994
|
-
return
|
|
1163
|
+
return s;
|
|
1164
|
+
}
|
|
1165
|
+
addIndicator(e, t, n = {}) {
|
|
1166
|
+
return this.indicator(e, t, n);
|
|
995
1167
|
}
|
|
996
1168
|
toLpString() {
|
|
997
|
-
let { lpString: e } =
|
|
1169
|
+
let { lpString: e } = F({
|
|
998
1170
|
name: this.name,
|
|
999
1171
|
sense: this.sense,
|
|
1000
1172
|
objective: this.objective,
|
|
@@ -1005,7 +1177,7 @@ var Y = class {
|
|
|
1005
1177
|
}
|
|
1006
1178
|
async optimize(e, t) {
|
|
1007
1179
|
let n, r;
|
|
1008
|
-
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.`);
|
|
1009
1181
|
let i = {
|
|
1010
1182
|
name: this.name,
|
|
1011
1183
|
sense: this.sense,
|
|
@@ -1014,13 +1186,23 @@ var Y = class {
|
|
|
1014
1186
|
variables: this.variables,
|
|
1015
1187
|
logicalConstraints: this.logicalConstraints,
|
|
1016
1188
|
boolVars: this.boolVars
|
|
1017
|
-
}, a =
|
|
1189
|
+
}, a = F(i), o = await n.solve(i, a, r);
|
|
1018
1190
|
this.runTime = o.runTime ?? 0, this.status = o.status, this.objectiveValue = o.objectiveValue ?? null;
|
|
1019
1191
|
let s = {}, c = {}, l = {}, u = {};
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
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
|
+
}
|
|
1024
1206
|
}
|
|
1025
1207
|
if (o.reducedCosts) for (let [e, t] of Object.entries(o.reducedCosts)) {
|
|
1026
1208
|
let n = this.getVarByName(e) || a.nameToVar.get(e);
|
|
@@ -1050,7 +1232,7 @@ var Y = class {
|
|
|
1050
1232
|
}
|
|
1051
1233
|
async check(e, t) {
|
|
1052
1234
|
let n, r;
|
|
1053
|
-
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.`);
|
|
1054
1236
|
if (typeof n.check == "function") {
|
|
1055
1237
|
let e = await n.check({
|
|
1056
1238
|
name: this.name,
|
|
@@ -1059,21 +1241,26 @@ var Y = class {
|
|
|
1059
1241
|
constraints: this.constraints,
|
|
1060
1242
|
logicalConstraints: this.logicalConstraints
|
|
1061
1243
|
}, r);
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
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);
|
|
1069
1253
|
}
|
|
1070
1254
|
return e;
|
|
1071
1255
|
}
|
|
1072
1256
|
let i = this.objective, a = this.sense;
|
|
1073
1257
|
try {
|
|
1074
|
-
this.objective = new
|
|
1258
|
+
this.objective = new S(), this.sense = "Minimize";
|
|
1075
1259
|
let e = await this.optimize(n, r), t = e === "Optimal" || e === "Feasible", i = e === "Infeasible", a = {};
|
|
1076
|
-
|
|
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
|
+
}
|
|
1077
1264
|
return {
|
|
1078
1265
|
status: t ? "Sat" : i ? "Unsat" : "Unknown",
|
|
1079
1266
|
values: a,
|
|
@@ -1083,131 +1270,6 @@ var Y = class {
|
|
|
1083
1270
|
this.objective = i, this.sense = a;
|
|
1084
1271
|
}
|
|
1085
1272
|
}
|
|
1086
|
-
}, X = "Minimize", ce = "Maximize", le = "Continuous", ue = "Integer", de = "Binary", Z = {
|
|
1087
|
-
Optimal: "Optimal",
|
|
1088
|
-
Feasible: "Feasible",
|
|
1089
|
-
Infeasible: "Infeasible",
|
|
1090
|
-
Unbounded: "Unbounded",
|
|
1091
|
-
TimeLimit: "Not Solved (Time Limit)",
|
|
1092
|
-
IterationLimit: "Not Solved (Iteration Limit)",
|
|
1093
|
-
Error: "Error",
|
|
1094
|
-
NotSolved: "Not Solved",
|
|
1095
|
-
Sat: "Sat",
|
|
1096
|
-
Unsat: "Unsat",
|
|
1097
|
-
Unknown: "Unknown"
|
|
1098
|
-
}, fe = class e extends E {
|
|
1099
|
-
constructor(e, t, n, r = "Continuous") {
|
|
1100
|
-
super({
|
|
1101
|
-
name: e,
|
|
1102
|
-
lb: t === void 0 ? 0 : t,
|
|
1103
|
-
ub: n === void 0 ? Infinity : n,
|
|
1104
|
-
type: r
|
|
1105
|
-
});
|
|
1106
|
-
}
|
|
1107
|
-
static dicts(t, n, r, i, a = "Continuous") {
|
|
1108
|
-
let o = {};
|
|
1109
|
-
for (let s of n) {
|
|
1110
|
-
let n = `${t}_${s}`;
|
|
1111
|
-
o[s] = new e(n, r, i, a);
|
|
1112
|
-
}
|
|
1113
|
-
return o;
|
|
1114
|
-
}
|
|
1115
|
-
};
|
|
1116
|
-
function pe(e) {
|
|
1117
|
-
return e == null ? null : typeof e == "number" ? e : e.value;
|
|
1118
|
-
}
|
|
1119
|
-
var me = class {
|
|
1120
|
-
model;
|
|
1121
|
-
constructor(e = "NoName", t = X) {
|
|
1122
|
-
this.model = new Y({
|
|
1123
|
-
name: e,
|
|
1124
|
-
sense: t
|
|
1125
|
-
});
|
|
1126
|
-
}
|
|
1127
|
-
get name() {
|
|
1128
|
-
return this.model.name;
|
|
1129
|
-
}
|
|
1130
|
-
get sense() {
|
|
1131
|
-
return this.model.sense;
|
|
1132
|
-
}
|
|
1133
|
-
set sense(e) {
|
|
1134
|
-
this.model.sense = e;
|
|
1135
|
-
}
|
|
1136
|
-
get objective() {
|
|
1137
|
-
return this.model.objective;
|
|
1138
|
-
}
|
|
1139
|
-
get variables() {
|
|
1140
|
-
return this.model.variables;
|
|
1141
|
-
}
|
|
1142
|
-
get constraints() {
|
|
1143
|
-
return this.model.constraints;
|
|
1144
|
-
}
|
|
1145
|
-
get status() {
|
|
1146
|
-
return this.model.status;
|
|
1147
|
-
}
|
|
1148
|
-
get objectiveValue() {
|
|
1149
|
-
return this.model.objectiveValue;
|
|
1150
|
-
}
|
|
1151
|
-
addVariable(e) {
|
|
1152
|
-
return this.model.getVarByName(e.name) || (this.model.variables.push(e), this.model.varMapByName.set(e.name, e)), e;
|
|
1153
|
-
}
|
|
1154
|
-
setObjective(e) {
|
|
1155
|
-
if (typeof e == "object" && e) {
|
|
1156
|
-
if ("terms" in e) for (let [t] of e.terms) this.addVariable(t);
|
|
1157
|
-
else e instanceof E && this.addVariable(e);
|
|
1158
|
-
}
|
|
1159
|
-
this.model.setObjective(e);
|
|
1160
|
-
}
|
|
1161
|
-
addConstraint(e, t) {
|
|
1162
|
-
if (typeof e == "object" && e && "lhs" in e) for (let [t] of e.lhs.terms) this.addVariable(t);
|
|
1163
|
-
return this.model.addConstraint(e, t);
|
|
1164
|
-
}
|
|
1165
|
-
add(e, t) {
|
|
1166
|
-
typeof e == "string" ? /(<=|>=|==|=<|=>|=)/.test(e) ? this.addConstraint(e, t) : this.setObjective(e) : e instanceof v ? this.addConstraint(e, t) : this.setObjective(e);
|
|
1167
|
-
}
|
|
1168
|
-
async solve(e, t) {
|
|
1169
|
-
return this.model.optimize(e, t);
|
|
1170
|
-
}
|
|
1171
|
-
toLpString() {
|
|
1172
|
-
return this.model.toLpString();
|
|
1173
|
-
}
|
|
1174
|
-
}, Q = "Minimize", $ = "Maximize", he = "Binary", ge = "Integer", _e = "Continuous";
|
|
1175
|
-
function ve(e) {
|
|
1176
|
-
return {
|
|
1177
|
-
expr: e,
|
|
1178
|
-
sense: $
|
|
1179
|
-
};
|
|
1180
|
-
}
|
|
1181
|
-
function ye(e) {
|
|
1182
|
-
return {
|
|
1183
|
-
expr: e,
|
|
1184
|
-
sense: Q
|
|
1185
|
-
};
|
|
1186
|
-
}
|
|
1187
|
-
var be = class extends Y {
|
|
1188
|
-
constructor(e = "MipModel", t = Q) {
|
|
1189
|
-
super({
|
|
1190
|
-
name: e,
|
|
1191
|
-
sense: t
|
|
1192
|
-
});
|
|
1193
|
-
}
|
|
1194
|
-
add_var(e = {}) {
|
|
1195
|
-
return this.addVar({
|
|
1196
|
-
name: e.name,
|
|
1197
|
-
type: e.var_type,
|
|
1198
|
-
lb: e.lb,
|
|
1199
|
-
ub: e.ub
|
|
1200
|
-
});
|
|
1201
|
-
}
|
|
1202
|
-
set objective_spec(e) {
|
|
1203
|
-
this.setObjective(e.expr, e.sense);
|
|
1204
|
-
}
|
|
1205
|
-
get objective_value() {
|
|
1206
|
-
return this.objectiveValue;
|
|
1207
|
-
}
|
|
1208
|
-
add_constr(e, t) {
|
|
1209
|
-
return this.addConstraint(e, t);
|
|
1210
|
-
}
|
|
1211
1273
|
};
|
|
1212
1274
|
//#endregion
|
|
1213
|
-
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 };
|