@ohtaman/washi 0.8.0 → 0.8.2
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.md +32 -34
- package/dist/browser/{__vite-browser-external-rTVKos2U.js → solvers/highs-__vite-browser-external-Dn-83hnF.js} +1 -1
- package/dist/browser/{browser-runtime-AG5QFtKf.js → solvers/highs-browser-runtime-0Bgq5ksG.js} +1 -1
- package/dist/browser/{loader-i3FnqMes.js → solvers/highs-loader-BLY3wzDH.js} +25 -18
- package/dist/browser/{highs-worker.js → solvers/highs-worker.js} +1 -1
- package/dist/browser/solvers/highs.js +1414 -0
- package/dist/browser/washi.js +1090 -1473
- package/dist/browser/washi.min.js +4 -4
- package/dist/lib/solvers/glpk.js +34 -32
- package/dist/lib/{highs-node-runtime.js → solvers/highs-node-runtime.js} +1 -1
- package/dist/lib/solvers/highs.js +410 -0
- package/dist/lib/solvers/lp-writer-CLScMFbS.js +115 -0
- package/dist/lib/solvers/pumpkin-node-runtime.js +1 -1
- package/dist/lib/solvers/pumpkin-worker.js +1 -1
- package/dist/lib/solvers/pumpkin.js +247 -1146
- package/dist/lib/solvers/registry-CrYRRk9q.js +907 -0
- package/dist/lib/solvers/types-BGqPkxwu.js +12 -0
- package/dist/lib/solvers/z3.js +125 -123
- package/dist/lib/washi.js +2426 -1340
- package/dist/types/index.d.ts +1 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/modeler/index.d.ts +0 -1
- package/dist/types/modeler/index.d.ts.map +1 -1
- package/dist/types/modeler/model.d.ts +6 -7
- package/dist/types/modeler/model.d.ts.map +1 -1
- package/dist/types/modeler/solvers/index.d.ts +2 -1
- package/dist/types/modeler/solvers/index.d.ts.map +1 -1
- package/dist/types/modeler/solvers/registry.d.ts.map +1 -1
- package/docs/examples/diet-sensitivity.md +33 -14
- package/docs/examples/ecommerce-coupons.md +17 -9
- package/docs/examples/index.md +34 -30
- package/docs/examples/knapsack.md +11 -5
- package/docs/examples/portfolio-qp.md +21 -8
- package/docs/examples/production.md +10 -5
- package/docs/examples/project-scheduling.md +25 -12
- package/docs/examples/shift-scheduling.md +27 -19
- package/docs/examples/smt-security.md +27 -12
- package/docs/examples/transportation.md +23 -13
- package/docs/examples/traveling-salesperson.md +34 -22
- package/docs/examples/unsat-core.md +27 -15
- package/docs/examples/vehicle-routing.md +74 -28
- package/docs/getting-started/ai-agents.md +129 -90
- package/docs/getting-started/coding-agents.md +60 -41
- package/docs/getting-started/cross-origin.md +27 -14
- package/docs/getting-started/index.md +23 -16
- package/docs/getting-started/runtimes.md +70 -40
- package/docs/guide/constraint-programming.md +74 -44
- package/docs/guide/constraints.md +71 -39
- package/docs/guide/decomposition.md +72 -42
- package/docs/guide/expressions.md +79 -48
- package/docs/guide/formulation-tips.md +46 -27
- package/docs/guide/objectives.md +55 -31
- package/docs/guide/unsat-core.md +56 -53
- package/docs/guide/variables.md +61 -28
- package/docs/solvers/custom-solver.md +155 -119
- package/docs/solvers/glpk.md +58 -17
- package/docs/solvers/highs.md +115 -43
- package/docs/solvers/index.md +33 -26
- package/docs/solvers/pumpkin.md +71 -63
- package/docs/solvers/z3.md +63 -25
- package/llms-full.txt +1623 -953
- package/llms.txt +125 -93
- package/package.json +29 -22
- package/skills/washi/SKILL.md +43 -22
- package/README.ja.md +0 -248
- package/README.zh-CN.md +0 -248
- package/dist/browser/solvers/pumpkin.min.js +0 -1
- package/dist/lib/registry-dx7x4e5c.js +0 -1694
- /package/dist/browser/{highs.wasm → solvers/highs.wasm} +0 -0
- /package/dist/browser/{highs.wasm.LICENSE → solvers/highs.wasm.LICENSE} +0 -0
- /package/dist/lib/{__vite-browser-external-oLchLuMx.js → solvers/__vite-browser-external-oLchLuMx.js} +0 -0
- /package/dist/lib/{highs-assets.js → solvers/highs-assets.js} +0 -0
- /package/dist/lib/{highs-browser-runtime.js → solvers/highs-browser-runtime.js} +0 -0
- /package/dist/lib/{highs-runtime.js → solvers/highs-runtime.js} +0 -0
- /package/dist/lib/{highs-worker-factory.js → solvers/highs-worker-factory.js} +0 -0
- /package/dist/lib/{highs-worker.js → solvers/highs-worker.js} +0 -0
- /package/dist/lib/{highs.wasm → solvers/highs.wasm} +0 -0
- /package/dist/lib/{highs.wasm.LICENSE → solvers/highs.wasm.LICENSE} +0 -0
- /package/dist/lib/{loader-CDpPXN_F.js → solvers/loader-CDpPXN_F.js} +0 -0
- /package/dist/lib/solvers/{pumpkin-loader-Ow74Fa1B.js → loader-Ow74Fa1B.js} +0 -0
- /package/dist/lib/{rolldown-runtime-DtPi1Y-2.js → solvers/rolldown-runtime-DtPi1Y-2.js} +0 -0
|
@@ -1,914 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { c as e, l as t, o as n, r, s as i, t as a } from "./registry-CrYRRk9q.js";
|
|
2
|
+
import { t as o } from "./types-BGqPkxwu.js";
|
|
3
|
+
import { a as s, c, i as l, l as u, n as d, o as f, r as p, s as m, t as h } from "./loader-Ow74Fa1B.js";
|
|
4
|
+
import { createPumpkinWorker as g } from "./pumpkin-worker-factory.js";
|
|
3
5
|
export * from "./pumpkin-assets.js";
|
|
4
6
|
export * from "./pumpkin-worker-factory.js";
|
|
5
|
-
//#region src/modeler/quad-expr.ts
|
|
6
|
-
function u(e, t) {
|
|
7
|
-
let n = e.id, r = t.id;
|
|
8
|
-
return n <= r ? `${n}:${r}` : `${r}:${n}`;
|
|
9
|
-
}
|
|
10
|
-
var d = class e {
|
|
11
|
-
kind = "quadratic";
|
|
12
|
-
linearTerms;
|
|
13
|
-
quadTerms;
|
|
14
|
-
constant;
|
|
15
|
-
nonlinearTerms;
|
|
16
|
-
constructor(e = /* @__PURE__ */ new Map(), t = /* @__PURE__ */ new Map(), n = 0, r = []) {
|
|
17
|
-
this.linearTerms = new Map(e), this.quadTerms = /* @__PURE__ */ new Map();
|
|
18
|
-
for (let [e, n] of t) this.quadTerms.set(e, { ...n });
|
|
19
|
-
this.constant = n, this.nonlinearTerms = r.filter((e) => e.coeff !== 0).map((e) => ({
|
|
20
|
-
atom: h(e.atom),
|
|
21
|
-
coeff: e.coeff
|
|
22
|
-
}));
|
|
23
|
-
}
|
|
24
|
-
hasNonlinearTerms() {
|
|
25
|
-
return this.nonlinearTerms.length > 0;
|
|
26
|
-
}
|
|
27
|
-
clone() {
|
|
28
|
-
return new e(this.linearTerms, this.quadTerms, this.constant, this.nonlinearTerms);
|
|
29
|
-
}
|
|
30
|
-
getQuadCoeff(e, t) {
|
|
31
|
-
let n = u(e, t), r = this.quadTerms.get(n);
|
|
32
|
-
return r ? r.coeff : 0;
|
|
33
|
-
}
|
|
34
|
-
add(t) {
|
|
35
|
-
if (T(t)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
36
|
-
let n = this.clone();
|
|
37
|
-
if (typeof t == "number") return n.constant += t, n;
|
|
38
|
-
if (t instanceof e) {
|
|
39
|
-
for (let [e, r] of t.quadTerms) {
|
|
40
|
-
let t = n.quadTerms.get(e), i = (t ? t.coeff : 0) + r.coeff;
|
|
41
|
-
i === 0 ? n.quadTerms.delete(e) : n.quadTerms.set(e, {
|
|
42
|
-
var1: r.var1,
|
|
43
|
-
var2: r.var2,
|
|
44
|
-
coeff: i
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
for (let [e, r] of t.linearTerms) {
|
|
48
|
-
let t = (n.linearTerms.get(e) ?? 0) + r;
|
|
49
|
-
t === 0 ? n.linearTerms.delete(e) : n.linearTerms.set(e, t);
|
|
50
|
-
}
|
|
51
|
-
if (n.constant += t.constant, Array.isArray(t.nonlinearTerms)) for (let e of t.nonlinearTerms) e.coeff !== 0 && n.nonlinearTerms.push({
|
|
52
|
-
atom: h(e.atom),
|
|
53
|
-
coeff: e.coeff
|
|
54
|
-
});
|
|
55
|
-
return n;
|
|
56
|
-
}
|
|
57
|
-
if ("terms" in t && t.terms instanceof Map) {
|
|
58
|
-
for (let [e, r] of t.terms) {
|
|
59
|
-
let t = (n.linearTerms.get(e) ?? 0) + r;
|
|
60
|
-
t === 0 ? n.linearTerms.delete(e) : n.linearTerms.set(e, t);
|
|
61
|
-
}
|
|
62
|
-
if (n.constant += t.constant, Array.isArray(t.nonlinearTerms)) for (let e of t.nonlinearTerms) e.coeff !== 0 && n.nonlinearTerms.push({
|
|
63
|
-
atom: h(e.atom),
|
|
64
|
-
coeff: e.coeff
|
|
65
|
-
});
|
|
66
|
-
return n;
|
|
67
|
-
}
|
|
68
|
-
let r = t, i = (n.linearTerms.get(r) ?? 0) + 1;
|
|
69
|
-
return i === 0 ? n.linearTerms.delete(r) : n.linearTerms.set(r, i), n;
|
|
70
|
-
}
|
|
71
|
-
sub(e) {
|
|
72
|
-
if (T(e)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
73
|
-
return typeof e == "number" ? this.add(-e) : this.add(e.mul(-1));
|
|
74
|
-
}
|
|
75
|
-
mul(t) {
|
|
76
|
-
if (T(t)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
77
|
-
if (typeof t == "number") {
|
|
78
|
-
let n = this.linearTerms.size > 0 || this.quadTerms.size > 0 || this.nonlinearTerms.length > 0, r = this.constant === 0 && n ? 0 : this.constant * t;
|
|
79
|
-
if (this.constant !== 0 && t !== 0 && r === 0) throw Error("Constant multiplication underflows to zero.");
|
|
80
|
-
let i = new e(/* @__PURE__ */ new Map(), /* @__PURE__ */ new Map(), r);
|
|
81
|
-
for (let [e, n] of this.linearTerms) {
|
|
82
|
-
let r = n * t;
|
|
83
|
-
if (n !== 0 && t !== 0 && r === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
84
|
-
r !== 0 && i.linearTerms.set(e, r);
|
|
85
|
-
}
|
|
86
|
-
for (let [e, n] of this.quadTerms) {
|
|
87
|
-
let r = n.coeff * t;
|
|
88
|
-
if (n.coeff !== 0 && t !== 0 && r === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
89
|
-
r !== 0 && i.quadTerms.set(e, {
|
|
90
|
-
var1: n.var1,
|
|
91
|
-
var2: n.var2,
|
|
92
|
-
coeff: r
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
if (t !== 0) for (let e of this.nonlinearTerms) {
|
|
96
|
-
let n = e.coeff * t;
|
|
97
|
-
if (e.coeff !== 0 && n === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
98
|
-
n !== 0 && i.nonlinearTerms.push({
|
|
99
|
-
atom: h(e.atom),
|
|
100
|
-
coeff: n
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
return i;
|
|
104
|
-
}
|
|
105
|
-
let n = "id" in t && "name" in t && !("terms" in t) && !("quadTerms" in t), r = "quadTerms" in t ? t.quadTerms.size : 0, i = !n && "nonlinearTerms" in t && Array.isArray(t.nonlinearTerms) ? t.nonlinearTerms.length : 0, a = n ? /* @__PURE__ */ new Map([[t, 1]]) : "quadTerms" in t ? t.linearTerms : t.terms, o = n ? 0 : t.constant;
|
|
106
|
-
if (this.nonlinearTerms.length > 0) {
|
|
107
|
-
if (!n && r === 0 && a.size === 0 && i === 0) return this.mul(o);
|
|
108
|
-
throw Error("Cannot multiply nonlinear expression (max, min, abs, or at) by a variable expression: only scalar multiplication is supported.");
|
|
109
|
-
}
|
|
110
|
-
if (i > 0) {
|
|
111
|
-
if (this.quadTerms.size === 0 && this.linearTerms.size === 0) {
|
|
112
|
-
let n = t.mul(this.constant);
|
|
113
|
-
return "quadTerms" in n ? n : new e(n.terms, /* @__PURE__ */ new Map(), n.constant, n.nonlinearTerms);
|
|
114
|
-
}
|
|
115
|
-
throw Error("Cannot multiply nonlinear expression (max, min, abs, or at) by a variable expression: only scalar multiplication is supported.");
|
|
116
|
-
}
|
|
117
|
-
if (this.quadTerms.size > 0 && (r > 0 || a.size > 0) || r > 0 && this.linearTerms.size > 0) throw Error("Multiplication creates a non-linear term of degree > 2 (not allowed in MILP/LP/QP)");
|
|
118
|
-
if (r === 0 && a.size === 0) return this.mul(o);
|
|
119
|
-
if (this.quadTerms.size === 0 && this.linearTerms.size === 0 && !n) {
|
|
120
|
-
let n = t.mul(this.constant);
|
|
121
|
-
return "quadTerms" in n ? n : new e(n.terms, /* @__PURE__ */ new Map(), n.constant, n.nonlinearTerms);
|
|
122
|
-
}
|
|
123
|
-
return e.multiplyLinearTerms(this.constant, this.linearTerms, o, a);
|
|
124
|
-
}
|
|
125
|
-
div(t) {
|
|
126
|
-
let n = e.extractScalarDivisor(t, "QuadExpr.div()");
|
|
127
|
-
return this.mul(1 / n);
|
|
128
|
-
}
|
|
129
|
-
neg() {
|
|
130
|
-
return this.mul(-1);
|
|
131
|
-
}
|
|
132
|
-
get value() {
|
|
133
|
-
let e = this.constant;
|
|
134
|
-
for (let [t, n] of this.linearTerms) {
|
|
135
|
-
if (t.value === null) return null;
|
|
136
|
-
e += n * t.value;
|
|
137
|
-
}
|
|
138
|
-
for (let t of this.quadTerms.values()) {
|
|
139
|
-
if (t.var1.value === null || t.var2.value === null) return null;
|
|
140
|
-
e += t.coeff * t.var1.value * t.var2.value;
|
|
141
|
-
}
|
|
142
|
-
for (let t of this.nonlinearTerms) {
|
|
143
|
-
let n = _(t.atom);
|
|
144
|
-
if (n === null) return null;
|
|
145
|
-
e += t.coeff * n;
|
|
146
|
-
}
|
|
147
|
-
return e;
|
|
148
|
-
}
|
|
149
|
-
le(e, t) {
|
|
150
|
-
return this.toConstraint("<=", e, t);
|
|
151
|
-
}
|
|
152
|
-
ge(e, t) {
|
|
153
|
-
return this.toConstraint(">=", e, t);
|
|
154
|
-
}
|
|
155
|
-
eq(e, t) {
|
|
156
|
-
return this.toConstraint("==", e, t);
|
|
157
|
-
}
|
|
158
|
-
lt(e, t) {
|
|
159
|
-
return this.toDiscreteComparison("<", e, t);
|
|
160
|
-
}
|
|
161
|
-
gt(e, t) {
|
|
162
|
-
return this.toDiscreteComparison(">", e, t);
|
|
163
|
-
}
|
|
164
|
-
ne(e, t) {
|
|
165
|
-
return this.toDiscreteComparison("!=", e, t);
|
|
166
|
-
}
|
|
167
|
-
toDiscreteComparison(e, t, n) {
|
|
168
|
-
if (this.quadTerms.size > 0) throw Error(`Quadratic expressions are not supported in discrete comparisons (${e}): quadratic terms are only allowed in objectives.`);
|
|
169
|
-
let r = new b(this.linearTerms, this.constant, this.nonlinearTerms);
|
|
170
|
-
return e === "<" ? r.lt(t, n) : e === ">" ? r.gt(t, n) : r.ne(t, n);
|
|
171
|
-
}
|
|
172
|
-
toConstraint(e, t, n) {
|
|
173
|
-
if (this.quadTerms.size > 0) throw Error(`Quadratic expressions are not supported in linear constraints (${e}): quadratic terms are only allowed in objectives.`);
|
|
174
|
-
let r = new b(this.linearTerms, this.constant, this.nonlinearTerms);
|
|
175
|
-
return e === "<=" ? r.le(t, n) : e === ">=" ? r.ge(t, n) : r.eq(t, n);
|
|
176
|
-
}
|
|
177
|
-
and(e) {
|
|
178
|
-
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'and': expected a BoolVar, Constraint, or BoolOp.");
|
|
179
|
-
}
|
|
180
|
-
or(e) {
|
|
181
|
-
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'or': expected a BoolVar, Constraint, or BoolOp.");
|
|
182
|
-
}
|
|
183
|
-
implies(e) {
|
|
184
|
-
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'implies': expected a BoolVar, Constraint, or BoolOp.");
|
|
185
|
-
}
|
|
186
|
-
xor(e) {
|
|
187
|
-
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'xor': expected a BoolVar, Constraint, or BoolOp.");
|
|
188
|
-
}
|
|
189
|
-
not() {
|
|
190
|
-
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'not': expected a BoolVar, Constraint, or BoolOp.");
|
|
191
|
-
}
|
|
192
|
-
toString() {
|
|
193
|
-
let e = [], t = !0;
|
|
194
|
-
for (let n of this.quadTerms.values()) {
|
|
195
|
-
let r = n.coeff, i = r >= 0 ? t ? "" : "+ " : "- ", a = Math.abs(r), o = a === 1 ? "" : `${a} `;
|
|
196
|
-
n.var1 === n.var2 ? e.push(`${i}${o}${n.var1.name}^2`) : e.push(`${i}${o}${n.var1.name} * ${n.var2.name}`), t = !1;
|
|
197
|
-
}
|
|
198
|
-
for (let [n, r] of this.linearTerms) {
|
|
199
|
-
let i = r >= 0 ? t ? "" : "+ " : "- ", a = Math.abs(r), o = a === 1 ? "" : `${a} `;
|
|
200
|
-
e.push(`${i}${o}${n.name}`), t = !1;
|
|
201
|
-
}
|
|
202
|
-
if (this.nonlinearTerms.length > 0) {
|
|
203
|
-
let n = new b(/* @__PURE__ */ new Map(), 0, this.nonlinearTerms).toString();
|
|
204
|
-
n !== "0" && (e.push(t ? n : n.startsWith("-") ? `- ${n.slice(2)}` : `+ ${n}`), t = !1);
|
|
205
|
-
}
|
|
206
|
-
if (this.constant !== 0 || e.length === 0) {
|
|
207
|
-
let n = this.constant >= 0 ? t ? "" : "+ " : "- ";
|
|
208
|
-
e.push(`${n}${Math.abs(this.constant)}`);
|
|
209
|
-
}
|
|
210
|
-
return e.join(" ") || "0";
|
|
211
|
-
}
|
|
212
|
-
static fromProduct(t, n, r = 1) {
|
|
213
|
-
let i = new e();
|
|
214
|
-
if (r === 0) return i;
|
|
215
|
-
let a = u(t, n);
|
|
216
|
-
return i.quadTerms.set(a, {
|
|
217
|
-
var1: t.id <= n.id ? t : n,
|
|
218
|
-
var2: t.id <= n.id ? n : t,
|
|
219
|
-
coeff: r
|
|
220
|
-
}), i;
|
|
221
|
-
}
|
|
222
|
-
static multiplyLinearTerms(t, n, r, i) {
|
|
223
|
-
let a = n.size > 0 || i.size > 0, o = (t === 0 || r === 0) && a ? 0 : t * r;
|
|
224
|
-
if (t !== 0 && r !== 0 && o === 0) throw Error("Constant multiplication underflows to zero.");
|
|
225
|
-
let s = new e(/* @__PURE__ */ new Map(), /* @__PURE__ */ new Map(), o === 0 ? 0 : o);
|
|
226
|
-
if (r !== 0) for (let [e, t] of n) {
|
|
227
|
-
let n = t * r;
|
|
228
|
-
if (t !== 0 && n === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
229
|
-
n !== 0 && s.linearTerms.set(e, n);
|
|
230
|
-
}
|
|
231
|
-
if (t !== 0) for (let [e, n] of i) {
|
|
232
|
-
let r = s.linearTerms.get(e) ?? 0, i = n * t;
|
|
233
|
-
if (n !== 0 && i === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
234
|
-
let a = r + i;
|
|
235
|
-
a === 0 ? s.linearTerms.delete(e) : s.linearTerms.set(e, a);
|
|
236
|
-
}
|
|
237
|
-
for (let [t, r] of n) for (let [n, a] of i) {
|
|
238
|
-
let i = r * a;
|
|
239
|
-
if (r !== 0 && a !== 0 && i === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
240
|
-
s = s.add(e.fromProduct(t, n, i));
|
|
241
|
-
}
|
|
242
|
-
return s;
|
|
243
|
-
}
|
|
244
|
-
static extractScalarDivisor(e, t) {
|
|
245
|
-
if (T(e)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
246
|
-
if (typeof e == "number") {
|
|
247
|
-
if (e === 0) throw Error(`Division by zero in ${t}`);
|
|
248
|
-
return e;
|
|
249
|
-
}
|
|
250
|
-
let n = "quadTerms" in e ? e.linearTerms.size : e.terms.size, r = "quadTerms" in e ? e.quadTerms.size : 0, i = "nonlinearTerms" in e && Array.isArray(e.nonlinearTerms) ? e.nonlinearTerms.length : 0;
|
|
251
|
-
if (n > 0 || r > 0 || i > 0) throw Error("Cannot divide by variable in expression");
|
|
252
|
-
if (e.constant === 0) throw Error(`Division by zero in ${t}`);
|
|
253
|
-
return e.constant;
|
|
254
|
-
}
|
|
255
|
-
}, f = 0;
|
|
256
|
-
function p(e) {
|
|
257
|
-
if (e === void 0) return "Continuous";
|
|
258
|
-
if (typeof e == "string") {
|
|
259
|
-
let t = e.toLowerCase();
|
|
260
|
-
if (t === "binary" || t === "bin") return "Binary";
|
|
261
|
-
if (t === "integer" || t === "int") return "Integer";
|
|
262
|
-
if (t === "continuous" || t === "cont") return "Continuous";
|
|
263
|
-
}
|
|
264
|
-
throw Error(`Invalid variable type "${String(e)}": expected "Continuous", "Integer", or "Binary".`);
|
|
265
|
-
}
|
|
266
|
-
var m = class {
|
|
267
|
-
id;
|
|
268
|
-
_name;
|
|
269
|
-
prefix;
|
|
270
|
-
hasExplicitName;
|
|
271
|
-
ownerModel = null;
|
|
272
|
-
_type;
|
|
273
|
-
_lb;
|
|
274
|
-
_ub;
|
|
275
|
-
_initialValue;
|
|
276
|
-
value = null;
|
|
277
|
-
reducedCost = null;
|
|
278
|
-
constructor(e = {}) {
|
|
279
|
-
this.id = ++f;
|
|
280
|
-
let t = typeof e.name == "string" ? e.name.trim() : "", n = typeof e.prefix == "string" ? e.prefix.trim() : "";
|
|
281
|
-
this.prefix = n.length > 0 ? n : "x", this.hasExplicitName = t.length > 0, this._name = this.hasExplicitName ? t : `${this.prefix}_${this.id}`, this._type = p(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), this._initialValue = e.initialValue === void 0 ? void 0 : Object.is(e.initialValue, -0) ? 0 : e.initialValue, this._initialValue !== void 0 && ee(this);
|
|
282
|
-
}
|
|
283
|
-
get name() {
|
|
284
|
-
return this._name;
|
|
285
|
-
}
|
|
286
|
-
get type() {
|
|
287
|
-
return this._type;
|
|
288
|
-
}
|
|
289
|
-
set type(e) {
|
|
290
|
-
let t = p(e);
|
|
291
|
-
this._type !== t && (this._type = t, S(this.ownerModel));
|
|
292
|
-
}
|
|
293
|
-
get lb() {
|
|
294
|
-
return this._lb;
|
|
295
|
-
}
|
|
296
|
-
set lb(e) {
|
|
297
|
-
Object.is(this._lb, e) || (this._lb = e, S(this.ownerModel));
|
|
298
|
-
}
|
|
299
|
-
get ub() {
|
|
300
|
-
return this._ub;
|
|
301
|
-
}
|
|
302
|
-
set ub(e) {
|
|
303
|
-
Object.is(this._ub, e) || (this._ub = e, S(this.ownerModel));
|
|
304
|
-
}
|
|
305
|
-
get initialValue() {
|
|
306
|
-
return this._initialValue;
|
|
307
|
-
}
|
|
308
|
-
set initialValue(e) {
|
|
309
|
-
let t = e !== void 0 && Object.is(e, -0) ? 0 : e;
|
|
310
|
-
Object.is(this._initialValue, t) || (this._initialValue = t, S(this.ownerModel));
|
|
311
|
-
}
|
|
312
|
-
_assignModelName(e) {
|
|
313
|
-
this._name = e;
|
|
314
|
-
}
|
|
315
|
-
mul(e) {
|
|
316
|
-
return typeof e == "number" ? new b(/* @__PURE__ */ new Map([[this, 1]]), 0).mul(e) : new d(/* @__PURE__ */ new Map([[this, 1]]), /* @__PURE__ */ new Map(), 0).mul(e);
|
|
317
|
-
}
|
|
318
|
-
add(e) {
|
|
319
|
-
return e instanceof d ? e.add(this) : new b(/* @__PURE__ */ new Map([[this, 1]]), 0).add(e);
|
|
320
|
-
}
|
|
321
|
-
sub(e) {
|
|
322
|
-
return e instanceof d ? e.neg().add(this) : new b(/* @__PURE__ */ new Map([[this, 1]]), 0).sub(e);
|
|
323
|
-
}
|
|
324
|
-
neg() {
|
|
325
|
-
return this.mul(-1);
|
|
326
|
-
}
|
|
327
|
-
le(e, t) {
|
|
328
|
-
return new b(/* @__PURE__ */ new Map([[this, 1]]), 0).le(e, t);
|
|
329
|
-
}
|
|
330
|
-
ge(e, t) {
|
|
331
|
-
return new b(/* @__PURE__ */ new Map([[this, 1]]), 0).ge(e, t);
|
|
332
|
-
}
|
|
333
|
-
eq(e, t) {
|
|
334
|
-
return new b(/* @__PURE__ */ new Map([[this, 1]]), 0).eq(e, t);
|
|
335
|
-
}
|
|
336
|
-
lt(e, t) {
|
|
337
|
-
return new b(/* @__PURE__ */ new Map([[this, 1]]), 0).lt(e, t);
|
|
338
|
-
}
|
|
339
|
-
gt(e, t) {
|
|
340
|
-
return new b(/* @__PURE__ */ new Map([[this, 1]]), 0).gt(e, t);
|
|
341
|
-
}
|
|
342
|
-
ne(e, t) {
|
|
343
|
-
return new b(/* @__PURE__ */ new Map([[this, 1]]), 0).ne(e, t);
|
|
344
|
-
}
|
|
345
|
-
toString() {
|
|
346
|
-
return this.name;
|
|
347
|
-
}
|
|
348
|
-
};
|
|
349
|
-
//#endregion
|
|
350
|
-
//#region src/modeler/expression.ts
|
|
351
|
-
function h(e) {
|
|
352
|
-
switch (e.kind) {
|
|
353
|
-
case "abs": return {
|
|
354
|
-
kind: "abs",
|
|
355
|
-
arg: e.arg.clone()
|
|
356
|
-
};
|
|
357
|
-
case "max": return {
|
|
358
|
-
kind: "max",
|
|
359
|
-
args: e.args.map((e) => e.clone())
|
|
360
|
-
};
|
|
361
|
-
case "min": return {
|
|
362
|
-
kind: "min",
|
|
363
|
-
args: e.args.map((e) => e.clone())
|
|
364
|
-
};
|
|
365
|
-
case "at": return {
|
|
366
|
-
kind: "at",
|
|
367
|
-
array: e.array.map((e) => e.clone()),
|
|
368
|
-
index: e.index.clone(),
|
|
369
|
-
discreteVars: new Set(e.discreteVars)
|
|
370
|
-
};
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
function g(e) {
|
|
374
|
-
switch (e.kind) {
|
|
375
|
-
case "abs": return `abs(${e.arg.toString()})`;
|
|
376
|
-
case "max": return `max(${e.args.map((e) => e.toString()).join(", ")})`;
|
|
377
|
-
case "min": return `min(${e.args.map((e) => e.toString()).join(", ")})`;
|
|
378
|
-
case "at": return `at([${e.array.map((e) => e.toString()).join(", ")}], ${e.index.toString()})`;
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
function _(e) {
|
|
382
|
-
switch (e.kind) {
|
|
383
|
-
case "abs": {
|
|
384
|
-
let t = e.arg.value;
|
|
385
|
-
return t === null ? null : Math.abs(t);
|
|
386
|
-
}
|
|
387
|
-
case "max": {
|
|
388
|
-
let t = -Infinity;
|
|
389
|
-
for (let n of e.args) {
|
|
390
|
-
let e = n.value;
|
|
391
|
-
if (e === null) return null;
|
|
392
|
-
e > t && (t = e);
|
|
393
|
-
}
|
|
394
|
-
return t;
|
|
395
|
-
}
|
|
396
|
-
case "min": {
|
|
397
|
-
let t = Infinity;
|
|
398
|
-
for (let n of e.args) {
|
|
399
|
-
let e = n.value;
|
|
400
|
-
if (e === null) return null;
|
|
401
|
-
e < t && (t = e);
|
|
402
|
-
}
|
|
403
|
-
return t;
|
|
404
|
-
}
|
|
405
|
-
case "at": {
|
|
406
|
-
let t = e.index.value;
|
|
407
|
-
if (t === null || !Number.isFinite(t)) return null;
|
|
408
|
-
let n = Math.round(t);
|
|
409
|
-
return Math.abs(t - n) > 1e-6 || n < 0 || n >= e.array.length ? null : e.array[n].value;
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
function v(e) {
|
|
414
|
-
return e instanceof m || typeof e == "object" && !!e && !T(e) && typeof e.id == "number" && typeof e.name == "string" && typeof e.lb == "number" && typeof e.ub == "number" && typeof e.type == "string" && typeof e.mul == "function";
|
|
415
|
-
}
|
|
416
|
-
function y(e, t, n = /* @__PURE__ */ new Set()) {
|
|
417
|
-
if (typeof e == "number") {
|
|
418
|
-
if (!Number.isSafeInteger(e)) throw Error(`${t} requires Integer or Binary variables and integer constants/coefficients, but received constant ${e}.`);
|
|
419
|
-
return n;
|
|
420
|
-
}
|
|
421
|
-
if (T(e)) throw Error(`${t} cannot accept a Constraint comparison.`);
|
|
422
|
-
if (e instanceof d || "quadTerms" in e && e.quadTerms instanceof Map && "linearTerms" in e && e.linearTerms instanceof Map && typeof e.constant == "number") {
|
|
423
|
-
let r = e;
|
|
424
|
-
if (r.quadTerms.size > 0) throw Error(`${t} does not support quadratic expressions.`);
|
|
425
|
-
return y(new b(r.linearTerms, r.constant, r.nonlinearTerms ?? []), t, n);
|
|
426
|
-
}
|
|
427
|
-
if (v(e)) {
|
|
428
|
-
if (e.type !== "Integer" && e.type !== "Binary") throw Error(`${t} requires Integer or Binary variables, but involves Continuous variable "${e.name}".`);
|
|
429
|
-
return n.add(e), n;
|
|
430
|
-
}
|
|
431
|
-
if ("terms" in e && e.terms instanceof Map) {
|
|
432
|
-
let r = e, i = r.constant ?? 0;
|
|
433
|
-
if (!Number.isSafeInteger(i)) throw Error(`${t} requires Integer or Binary variables and integer constants/coefficients, but received constant ${i}.`);
|
|
434
|
-
for (let [e, i] of r.terms) if (i !== 0) {
|
|
435
|
-
if (e.type !== "Integer" && e.type !== "Binary") throw Error(`${t} requires Integer or Binary variables, but involves Continuous variable "${e.name}".`);
|
|
436
|
-
if (!Number.isSafeInteger(i)) throw Error(`${t} requires integer coefficients, but variable "${e.name}" has coefficient ${i}.`);
|
|
437
|
-
n.add(e);
|
|
438
|
-
}
|
|
439
|
-
for (let e of r.nonlinearTerms ?? []) {
|
|
440
|
-
if (e.coeff === 0) continue;
|
|
441
|
-
if (!Number.isSafeInteger(e.coeff)) throw Error(`${t} requires integer coefficients on nonlinear expressions, received ${e.coeff}.`);
|
|
442
|
-
let r = e.atom;
|
|
443
|
-
if (r.kind === "abs") y(r.arg, t, n);
|
|
444
|
-
else if (r.kind === "max" || r.kind === "min") for (let e of r.args) y(e, t, n);
|
|
445
|
-
else if (r.kind === "at") {
|
|
446
|
-
y(r.index, t, n);
|
|
447
|
-
for (let e of r.array) y(e, t, n);
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
return n;
|
|
451
|
-
}
|
|
452
|
-
throw Error(`${t} expected an integer-valued scalar expression.`);
|
|
453
|
-
}
|
|
454
|
-
var b = class e {
|
|
455
|
-
kind = "linear";
|
|
456
|
-
terms;
|
|
457
|
-
constant;
|
|
458
|
-
nonlinearTerms;
|
|
459
|
-
constructor(e = /* @__PURE__ */ new Map(), t = 0, n = []) {
|
|
460
|
-
this.terms = new Map(e), this.constant = t, this.nonlinearTerms = n.filter((e) => e.coeff !== 0).map((e) => ({
|
|
461
|
-
atom: h(e.atom),
|
|
462
|
-
coeff: e.coeff
|
|
463
|
-
}));
|
|
464
|
-
}
|
|
465
|
-
hasNonlinearTerms() {
|
|
466
|
-
return this.nonlinearTerms.length > 0;
|
|
467
|
-
}
|
|
468
|
-
setTerm(e, t) {
|
|
469
|
-
return t === 0 ? this.terms.delete(e) : this.terms.set(e, t), this;
|
|
470
|
-
}
|
|
471
|
-
addTerm(e, t) {
|
|
472
|
-
let n = (this.terms.get(e) ?? 0) + t;
|
|
473
|
-
return n === 0 ? this.terms.delete(e) : this.terms.set(e, n), this;
|
|
474
|
-
}
|
|
475
|
-
clone() {
|
|
476
|
-
return new e(this.terms, this.constant, this.nonlinearTerms);
|
|
477
|
-
}
|
|
478
|
-
add(e) {
|
|
479
|
-
if (T(e)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
480
|
-
if (e instanceof d) return e.add(this);
|
|
481
|
-
let t = this.clone();
|
|
482
|
-
if (typeof e == "number") t.constant += e;
|
|
483
|
-
else if ("terms" in e) {
|
|
484
|
-
for (let [n, r] of e.terms) {
|
|
485
|
-
let e = (t.terms.get(n) ?? 0) + r;
|
|
486
|
-
e === 0 ? t.terms.delete(n) : t.terms.set(n, e);
|
|
487
|
-
}
|
|
488
|
-
if (t.constant += e.constant, Array.isArray(e.nonlinearTerms)) for (let n of e.nonlinearTerms) n.coeff !== 0 && t.nonlinearTerms.push({
|
|
489
|
-
atom: h(n.atom),
|
|
490
|
-
coeff: n.coeff
|
|
491
|
-
});
|
|
492
|
-
} else {
|
|
493
|
-
let n = (t.terms.get(e) ?? 0) + 1;
|
|
494
|
-
n === 0 ? t.terms.delete(e) : t.terms.set(e, n);
|
|
495
|
-
}
|
|
496
|
-
return t;
|
|
497
|
-
}
|
|
498
|
-
sub(e) {
|
|
499
|
-
if (T(e)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
500
|
-
return e instanceof d ? e.neg().add(this) : typeof e == "number" ? this.add(-e) : this.add(e.mul(-1));
|
|
501
|
-
}
|
|
502
|
-
mul(t) {
|
|
503
|
-
if (T(t)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
504
|
-
if (typeof t == "number") {
|
|
505
|
-
let n = this.terms.size > 0 || this.nonlinearTerms.length > 0, r = this.constant === 0 && n ? 0 : this.constant * t;
|
|
506
|
-
if (this.constant !== 0 && t !== 0 && r === 0) throw Error("Constant multiplication underflows to zero.");
|
|
507
|
-
let i = new e(/* @__PURE__ */ new Map(), r);
|
|
508
|
-
for (let [e, n] of this.terms) {
|
|
509
|
-
let r = n * t;
|
|
510
|
-
if (n !== 0 && t !== 0 && r === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
511
|
-
r !== 0 && i.terms.set(e, r);
|
|
512
|
-
}
|
|
513
|
-
if (t !== 0) for (let e of this.nonlinearTerms) {
|
|
514
|
-
let n = e.coeff * t;
|
|
515
|
-
if (e.coeff !== 0 && n === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
516
|
-
n !== 0 && i.nonlinearTerms.push({
|
|
517
|
-
atom: h(e.atom),
|
|
518
|
-
coeff: n
|
|
519
|
-
});
|
|
520
|
-
}
|
|
521
|
-
return i;
|
|
522
|
-
}
|
|
523
|
-
if (t instanceof m || typeof t == "object" && t && "id" in t && !("terms" in t) && !("quadTerms" in t)) {
|
|
524
|
-
if (this.hasNonlinearTerms()) throw Error("Cannot multiply nonlinear expression (max, min, abs, or at) by a variable expression: only scalar multiplication is supported.");
|
|
525
|
-
return d.multiplyLinearTerms(this.constant, this.terms, 0, /* @__PURE__ */ new Map([[t, 1]]));
|
|
526
|
-
}
|
|
527
|
-
if (t instanceof d) {
|
|
528
|
-
let e = t.linearTerms.size > 0 || t.quadTerms.size > 0 || t.hasNonlinearTerms();
|
|
529
|
-
if (this.hasNonlinearTerms()) {
|
|
530
|
-
if (!e) return this.mul(t.constant);
|
|
531
|
-
throw Error("Cannot multiply nonlinear expression (max, min, abs, or at) by a variable expression: only scalar multiplication is supported.");
|
|
532
|
-
}
|
|
533
|
-
if (t.hasNonlinearTerms()) {
|
|
534
|
-
if (this.terms.size === 0) return t.mul(this.constant);
|
|
535
|
-
throw Error("Cannot multiply nonlinear expression (max, min, abs, or at) by a variable expression: only scalar multiplication is supported.");
|
|
536
|
-
}
|
|
537
|
-
return this.terms.size === 0 ? t.mul(this.constant) : new d(this.terms, /* @__PURE__ */ new Map(), this.constant).mul(t);
|
|
538
|
-
}
|
|
539
|
-
let n = Array.isArray(t.nonlinearTerms) && t.nonlinearTerms.length > 0;
|
|
540
|
-
if (this.hasNonlinearTerms()) {
|
|
541
|
-
if (t.terms.size === 0 && !n) return this.mul(t.constant);
|
|
542
|
-
throw Error("Cannot multiply nonlinear expression (max, min, abs, or at) by a variable expression: only scalar multiplication is supported.");
|
|
543
|
-
}
|
|
544
|
-
if (n) {
|
|
545
|
-
if (this.terms.size === 0) return t.mul(this.constant);
|
|
546
|
-
throw Error("Cannot multiply nonlinear expression (max, min, abs, or at) by a variable expression: only scalar multiplication is supported.");
|
|
547
|
-
}
|
|
548
|
-
return this.terms.size === 0 ? t.mul(this.constant) : t.terms.size === 0 ? this.mul(t.constant) : d.multiplyLinearTerms(this.constant, this.terms, t.constant, t.terms);
|
|
549
|
-
}
|
|
550
|
-
div(e) {
|
|
551
|
-
let t = d.extractScalarDivisor(e, "LinExpr.div()");
|
|
552
|
-
return this.mul(1 / t);
|
|
553
|
-
}
|
|
554
|
-
neg() {
|
|
555
|
-
return this.mul(-1);
|
|
556
|
-
}
|
|
557
|
-
get value() {
|
|
558
|
-
let e = this.constant;
|
|
559
|
-
for (let [t, n] of this.terms) {
|
|
560
|
-
if (t.value === null) return null;
|
|
561
|
-
e += n * t.value;
|
|
562
|
-
}
|
|
563
|
-
for (let t of this.nonlinearTerms) {
|
|
564
|
-
let n = _(t.atom);
|
|
565
|
-
if (n === null) return null;
|
|
566
|
-
e += t.coeff * n;
|
|
567
|
-
}
|
|
568
|
-
return e;
|
|
569
|
-
}
|
|
570
|
-
le(e, t) {
|
|
571
|
-
return this.toConstraint("<=", e, t);
|
|
572
|
-
}
|
|
573
|
-
ge(e, t) {
|
|
574
|
-
return this.toConstraint(">=", e, t);
|
|
575
|
-
}
|
|
576
|
-
eq(e, t) {
|
|
577
|
-
return this.toConstraint("==", e, t);
|
|
578
|
-
}
|
|
579
|
-
lt(e, t) {
|
|
580
|
-
return this.toDiscreteComparison("<", e, t);
|
|
581
|
-
}
|
|
582
|
-
gt(e, t) {
|
|
583
|
-
return this.toDiscreteComparison(">", e, t);
|
|
584
|
-
}
|
|
585
|
-
ne(e, t) {
|
|
586
|
-
return this.toDiscreteComparison("!=", e, t);
|
|
587
|
-
}
|
|
588
|
-
toDiscreteComparison(e, t, n) {
|
|
589
|
-
if (t instanceof w || typeof t == "object" && t && "sense" in t && "lhs" in t) throw Error(`Cannot compare against a Constraint in (${e}): expected a finite integer, discrete Var, or discrete LinExpr.`);
|
|
590
|
-
let r = `discrete ${e}`, i = /* @__PURE__ */ new Set();
|
|
591
|
-
y(this, r, i), y(t, r, i);
|
|
592
|
-
let a = this.toConstraint(e === "<" ? "<=" : e === ">" ? ">=" : "!=", t, n);
|
|
593
|
-
if (e === "<") {
|
|
594
|
-
let e = a.rhs - 1;
|
|
595
|
-
if (!Number.isSafeInteger(e)) throw Error(`Discrete comparison (<) adjusted right-hand side (${e}) exceeds safe integer range.`);
|
|
596
|
-
a.rhs = e;
|
|
597
|
-
} else if (e === ">") {
|
|
598
|
-
let e = a.rhs + 1;
|
|
599
|
-
if (!Number.isSafeInteger(e)) throw Error(`Discrete comparison (>) adjusted right-hand side (${e}) exceeds safe integer range.`);
|
|
600
|
-
a.rhs = e;
|
|
601
|
-
} else if (!Number.isSafeInteger(a.rhs)) throw Error(`Discrete comparison (!=) adjusted right-hand side (${a.rhs}) exceeds safe integer range.`);
|
|
602
|
-
return a._setDiscreteVars(i), a;
|
|
603
|
-
}
|
|
604
|
-
toConstraint(t, n, r) {
|
|
605
|
-
let i = this.clone(), a = 0;
|
|
606
|
-
if (typeof n == "number") a = n;
|
|
607
|
-
else if (n instanceof w || typeof n == "object" && n && "sense" in n && "lhs" in n) throw Error(`Cannot compare against a Constraint in (${t}): expected a number, Var, or LinExpr.`);
|
|
608
|
-
else if (n instanceof d || typeof n == "object" && n && "quadTerms" in n) {
|
|
609
|
-
let r = n;
|
|
610
|
-
if (r.quadTerms.size > 0) throw Error(`Quadratic expressions are not supported in linear constraints (${t}): quadratic terms are only allowed in objectives.`);
|
|
611
|
-
i = i.sub(new e(r.linearTerms, 0, r.nonlinearTerms ?? [])), a = r.constant;
|
|
612
|
-
} else "terms" in n ? (i = i.sub(new e(n.terms, 0, n.nonlinearTerms ?? [])), a = n.constant) : i = i.sub(n);
|
|
613
|
-
let o = a - i.constant;
|
|
614
|
-
return i.constant = 0, new w(i, t, o, r);
|
|
615
|
-
}
|
|
616
|
-
and(e) {
|
|
617
|
-
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==, !=, <, >) in logical 'and': expected a BoolVar, Constraint, or BoolOp.");
|
|
618
|
-
}
|
|
619
|
-
or(e) {
|
|
620
|
-
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==, !=, <, >) in logical 'or': expected a BoolVar, Constraint, or BoolOp.");
|
|
621
|
-
}
|
|
622
|
-
implies(e) {
|
|
623
|
-
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==, !=, <, >) in logical 'implies': expected a BoolVar, Constraint, or BoolOp.");
|
|
624
|
-
}
|
|
625
|
-
xor(e) {
|
|
626
|
-
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==, !=, <, >) in logical 'xor': expected a BoolVar, Constraint, or BoolOp.");
|
|
627
|
-
}
|
|
628
|
-
not() {
|
|
629
|
-
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==, !=, <, >) in logical 'not': expected a BoolVar, Constraint, or BoolOp.");
|
|
630
|
-
}
|
|
631
|
-
toString() {
|
|
632
|
-
let e = [], t = !0;
|
|
633
|
-
for (let [n, r] of this.terms) {
|
|
634
|
-
let i = r >= 0 ? t ? "" : "+ " : "- ", a = Math.abs(r), o = a === 1 ? "" : `${a} `;
|
|
635
|
-
e.push(`${i}${o}${n.name}`), t = !1;
|
|
636
|
-
}
|
|
637
|
-
for (let n of this.nonlinearTerms) {
|
|
638
|
-
let r = n.coeff, i = r >= 0 ? t ? "" : "+ " : "- ", a = Math.abs(r), o = a === 1 ? "" : `${a} `;
|
|
639
|
-
e.push(`${i}${o}${g(n.atom)}`), t = !1;
|
|
640
|
-
}
|
|
641
|
-
if (this.constant !== 0 || e.length === 0) {
|
|
642
|
-
let n = this.constant >= 0 ? t ? "" : "+ " : "- ";
|
|
643
|
-
e.push(`${n}${Math.abs(this.constant)}`);
|
|
644
|
-
}
|
|
645
|
-
return e.join(" ") || "0";
|
|
646
|
-
}
|
|
647
|
-
}, x = /* @__PURE__ */ new WeakMap();
|
|
648
|
-
function S(e) {
|
|
649
|
-
e && x.get(e)?.();
|
|
650
|
-
}
|
|
651
|
-
var C = 1e-6;
|
|
652
|
-
function ee(e) {
|
|
653
|
-
if (typeof e.lb != "number" || Number.isNaN(e.lb)) throw Error(`Variable "${e.name}" has invalid lower bound: ${e.lb} (NaN or non-numeric bound is not allowed).`);
|
|
654
|
-
if (e.lb === Infinity) throw Error(`Variable "${e.name}" has invalid lower bound: +Infinity is not allowed as a lower bound.`);
|
|
655
|
-
if (typeof e.ub != "number" || Number.isNaN(e.ub)) throw Error(`Variable "${e.name}" has invalid upper bound: ${e.ub} (NaN or non-numeric bound is not allowed).`);
|
|
656
|
-
if (e.ub === -Infinity) throw Error(`Variable "${e.name}" has invalid upper bound: -Infinity is not allowed as an upper bound.`);
|
|
657
|
-
if (e.lb > e.ub) throw Error(`Variable "${e.name}" has invalid bounds: lower bound (${e.lb}) is strictly greater than upper bound (${e.ub}).`);
|
|
658
|
-
if (e.initialValue !== void 0) {
|
|
659
|
-
if (typeof e.initialValue != "number" || !Number.isFinite(e.initialValue)) throw Error(`Variable "${e.name}" has invalid initialValue: ${String(e.initialValue)} (initialValue must be a finite number).`);
|
|
660
|
-
let t = e.type === "Binary" ? Math.max(0, e.lb) : e.lb, n = e.type === "Binary" ? Math.min(1, e.ub) : e.ub;
|
|
661
|
-
if (e.initialValue < t - 1e-6 || e.initialValue > n + 1e-6) throw Error(`Variable "${e.name}" has initialValue (${e.initialValue}) outside its bounds [${t}, ${n}] (tolerance ${C}).`);
|
|
662
|
-
if (e.type === "Integer" || e.type === "Binary") {
|
|
663
|
-
let r = Math.round(e.initialValue);
|
|
664
|
-
if (Math.abs(e.initialValue - r) > 1e-6 || r < t || r > n) throw Error(`Variable "${e.name}" of type "${e.type}" has non-integral or out-of-domain initialValue: ${e.initialValue} (must be within ${C} of an integer in [${t}, ${n}]).`);
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
}
|
|
668
|
-
//#endregion
|
|
669
|
-
//#region src/modeler/constraint.ts
|
|
670
|
-
var te = 0, w = class e {
|
|
671
|
-
kind = "constraint";
|
|
672
|
-
id;
|
|
673
|
-
_lhs;
|
|
674
|
-
_sense;
|
|
675
|
-
_rhs;
|
|
676
|
-
_name;
|
|
677
|
-
hasExplicitName;
|
|
678
|
-
ownerModel = null;
|
|
679
|
-
_indicatorOrigin;
|
|
680
|
-
_indicatorSnapshot;
|
|
681
|
-
_discreteVars;
|
|
682
|
-
slack = null;
|
|
683
|
-
dual = null;
|
|
684
|
-
addTerm(e, t) {
|
|
685
|
-
return t === 0 ? this : (this._indicatorOrigin = void 0, this._indicatorSnapshot = void 0, this._lhs.addTerm(e, t), S(this.ownerModel), this);
|
|
686
|
-
}
|
|
687
|
-
setTerm(e, t) {
|
|
688
|
-
return this._indicatorOrigin = void 0, this._indicatorSnapshot = void 0, this._lhs.setTerm(e, t), S(this.ownerModel), this;
|
|
689
|
-
}
|
|
690
|
-
constructor(e, t, n, r) {
|
|
691
|
-
this.id = ++te, this._lhs = e, this._sense = t, this._rhs = n;
|
|
692
|
-
let i = typeof r == "string" ? r.trim() : void 0;
|
|
693
|
-
i && i.length > 0 ? (this._name = i, this.hasExplicitName = !0) : (this._name = `c_${this.id}`, this.hasExplicitName = !1);
|
|
694
|
-
}
|
|
695
|
-
get lhs() {
|
|
696
|
-
return this._lhs;
|
|
697
|
-
}
|
|
698
|
-
set lhs(e) {
|
|
699
|
-
this._lhs !== e && (this._indicatorOrigin = void 0, this._indicatorSnapshot = void 0, this._lhs = e, S(this.ownerModel));
|
|
700
|
-
}
|
|
701
|
-
get sense() {
|
|
702
|
-
return this._sense;
|
|
703
|
-
}
|
|
704
|
-
set sense(e) {
|
|
705
|
-
if (e === "!=" && !Number.isSafeInteger(this._rhs)) throw Error(`Discrete "!=" in constraint "${this._name}" requires a safe integer RHS, received ${this._rhs}.`);
|
|
706
|
-
this._sense !== e && (this._indicatorOrigin = void 0, this._indicatorSnapshot = void 0, this._sense = e, S(this.ownerModel));
|
|
707
|
-
}
|
|
708
|
-
get rhs() {
|
|
709
|
-
return this._rhs;
|
|
710
|
-
}
|
|
711
|
-
set rhs(e) {
|
|
712
|
-
if (this._sense === "!=" && !Number.isSafeInteger(e)) throw Error(`Discrete "!=" in constraint "${this._name}" requires a safe integer RHS, received ${e}.`);
|
|
713
|
-
Object.is(this._rhs, e) || (this._indicatorOrigin = void 0, this._indicatorSnapshot = void 0, this._rhs = e, S(this.ownerModel));
|
|
714
|
-
}
|
|
715
|
-
get name() {
|
|
716
|
-
return this._name;
|
|
717
|
-
}
|
|
718
|
-
set name(e) {
|
|
719
|
-
this._name !== e && (this._name = e, S(this.ownerModel)), this.hasExplicitName = !0;
|
|
720
|
-
}
|
|
721
|
-
get _indicatorMeta() {
|
|
722
|
-
let e = this._indicatorOrigin, t = this._indicatorSnapshot;
|
|
723
|
-
if (!e || !t) return;
|
|
724
|
-
if (this._lhs !== t.lhsRef || this._sense !== t.sense || !Object.is(this._rhs, t.rhs) || !Object.is(this._lhs.constant, t.constant) || this._lhs.nonlinearTerms.length > 0 || this._lhs.terms.size !== t.terms.size) {
|
|
725
|
-
this._indicatorOrigin = void 0, this._indicatorSnapshot = void 0;
|
|
726
|
-
return;
|
|
727
|
-
}
|
|
728
|
-
for (let [e, n] of t.terms) if (!this._lhs.terms.has(e) || !Object.is(this._lhs.terms.get(e), n)) {
|
|
729
|
-
this._indicatorOrigin = void 0, this._indicatorSnapshot = void 0;
|
|
730
|
-
return;
|
|
731
|
-
}
|
|
732
|
-
let n = e.targetRhs - (e.targetLhs.constant ?? 0), r = 0;
|
|
733
|
-
for (let [t, n] of e.targetLhs.terms) {
|
|
734
|
-
if (n === 0) continue;
|
|
735
|
-
let i = e.targetSense === "<=" ? n > 0 ? t.ub : t.lb : n > 0 ? t.lb : t.ub;
|
|
736
|
-
if (!Number.isFinite(i) || (r += n * i, !Number.isFinite(r))) return;
|
|
737
|
-
}
|
|
738
|
-
if (!((e.targetSense === "<=" ? r - n : n - r) > e.derivedM + 1e-9)) return e;
|
|
739
|
-
}
|
|
740
|
-
_setIndicatorOrigin(e) {
|
|
741
|
-
if (!e) {
|
|
742
|
-
this._indicatorOrigin = void 0, this._indicatorSnapshot = void 0;
|
|
743
|
-
return;
|
|
744
|
-
}
|
|
745
|
-
this._indicatorOrigin = e, this._indicatorSnapshot = {
|
|
746
|
-
lhsRef: this._lhs,
|
|
747
|
-
terms: new Map(this._lhs.terms),
|
|
748
|
-
constant: this._lhs.constant,
|
|
749
|
-
sense: this._sense,
|
|
750
|
-
rhs: this._rhs
|
|
751
|
-
};
|
|
752
|
-
}
|
|
753
|
-
get _requiredDiscreteVars() {
|
|
754
|
-
return this._discreteVars;
|
|
755
|
-
}
|
|
756
|
-
_setDiscreteVars(e) {
|
|
757
|
-
this._discreteVars = e;
|
|
758
|
-
}
|
|
759
|
-
_assignModelName(e, t) {
|
|
760
|
-
this._name = e, this.hasExplicitName = t;
|
|
761
|
-
}
|
|
762
|
-
and(e) {
|
|
763
|
-
return new O("and", [this, e]);
|
|
764
|
-
}
|
|
765
|
-
or(e) {
|
|
766
|
-
return new O("or", [this, e]);
|
|
767
|
-
}
|
|
768
|
-
implies(e) {
|
|
769
|
-
return new O("implies", [this, e]);
|
|
770
|
-
}
|
|
771
|
-
xor(e) {
|
|
772
|
-
return new O("xor", [this, e]);
|
|
773
|
-
}
|
|
774
|
-
not() {
|
|
775
|
-
return new O("not", [this]);
|
|
776
|
-
}
|
|
777
|
-
add(e) {
|
|
778
|
-
throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
779
|
-
}
|
|
780
|
-
sub(e) {
|
|
781
|
-
throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
782
|
-
}
|
|
783
|
-
mul(e) {
|
|
784
|
-
throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
785
|
-
}
|
|
786
|
-
div(e) {
|
|
787
|
-
throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
788
|
-
}
|
|
789
|
-
neg() {
|
|
790
|
-
throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
791
|
-
}
|
|
792
|
-
le(e, t) {
|
|
793
|
-
throw Error("Cannot compare a Constraint in (<=): expected a number, Var, or LinExpr.");
|
|
794
|
-
}
|
|
795
|
-
ge(e, t) {
|
|
796
|
-
throw Error("Cannot compare a Constraint in (>=): expected a number, Var, or LinExpr.");
|
|
797
|
-
}
|
|
798
|
-
eq(e, t) {
|
|
799
|
-
throw Error("Cannot compare a Constraint in (==): expected a number, Var, or LinExpr.");
|
|
800
|
-
}
|
|
801
|
-
lt(e, t) {
|
|
802
|
-
throw Error("Cannot compare a Constraint in (<): expected a number, Var, or LinExpr.");
|
|
803
|
-
}
|
|
804
|
-
gt(e, t) {
|
|
805
|
-
throw Error("Cannot compare a Constraint in (>): expected a number, Var, or LinExpr.");
|
|
806
|
-
}
|
|
807
|
-
ne(e, t) {
|
|
808
|
-
throw Error("Cannot compare a Constraint in (!=): expected a number, Var, or LinExpr.");
|
|
809
|
-
}
|
|
810
|
-
get value() {
|
|
811
|
-
throw Error("Cannot read numeric value of a Constraint: expected a Var, LinExpr, or QuadExpr.");
|
|
812
|
-
}
|
|
813
|
-
get constant() {
|
|
814
|
-
throw Error("Cannot read numeric constant of a Constraint: expected a LinExpr or QuadExpr.");
|
|
815
|
-
}
|
|
816
|
-
clone() {
|
|
817
|
-
let t = new e(this._lhs.clone(), this._sense, this._rhs, this.hasExplicitName ? this._name : void 0);
|
|
818
|
-
return this._discreteVars && t._setDiscreteVars(new Set(this._discreteVars)), t;
|
|
819
|
-
}
|
|
820
|
-
toString() {
|
|
821
|
-
let e = this.sense === "==" ? "=" : this.sense;
|
|
822
|
-
return `${this.name}: ${this.lhs.toString()} ${e} ${this.rhs}`;
|
|
823
|
-
}
|
|
824
|
-
};
|
|
825
|
-
function T(e) {
|
|
826
|
-
return e instanceof w || typeof e == "object" && !!e && "sense" in e && "lhs" in e && "rhs" in e;
|
|
827
|
-
}
|
|
828
|
-
//#endregion
|
|
829
|
-
//#region src/modeler/boolean-expr.ts
|
|
830
|
-
var ne = 0;
|
|
831
|
-
function E(e, t) {
|
|
832
|
-
if (e instanceof D || e instanceof w) return e;
|
|
833
|
-
throw typeof e == "object" && e && ("terms" in e || "quadTerms" in e) ? Error(`Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical operator "${t}". Use a comparison constraint such as expr\`\${x} <= 10\` or a BoolVar/BoolExpr.`) : typeof e == "object" && e && "id" in e && "name" in e && "lb" in e && "ub" in e && !("sense" in e) ? Error(`Invalid operand passed to logical operator "${t}": expected a BoolVar, BoolExpr, or Constraint, but received numeric variable "${e.name}". Use m.bool() for propositional variables, add a comparison such as expr\`\${x} == 1\`, or use m.indicator(binVar, constraint) for 0-1 Big-M indicator constraints.`) : Error(`Invalid operand passed to logical operator "${t}": expected a BoolVar, BoolExpr, or Constraint.`);
|
|
834
|
-
}
|
|
835
|
-
var D = class {
|
|
836
|
-
and(e) {
|
|
837
|
-
return new O("and", [this, E(e, "and")]);
|
|
838
|
-
}
|
|
839
|
-
or(e) {
|
|
840
|
-
return new O("or", [this, E(e, "or")]);
|
|
841
|
-
}
|
|
842
|
-
implies(e) {
|
|
843
|
-
return new O("implies", [this, E(e, "implies")]);
|
|
844
|
-
}
|
|
845
|
-
xor(e) {
|
|
846
|
-
return new O("xor", [this, E(e, "xor")]);
|
|
847
|
-
}
|
|
848
|
-
not() {
|
|
849
|
-
return new O("not", [this]);
|
|
850
|
-
}
|
|
851
|
-
}, re = class extends D {
|
|
852
|
-
id;
|
|
853
|
-
type = "Boolean";
|
|
854
|
-
name;
|
|
855
|
-
value = null;
|
|
856
|
-
constructor(e) {
|
|
857
|
-
super(), this.id = ++ne, this.name = e || `p_${this.id}`;
|
|
858
|
-
}
|
|
859
|
-
toString() {
|
|
860
|
-
return this.name;
|
|
861
|
-
}
|
|
862
|
-
}, O = class extends D {
|
|
863
|
-
op;
|
|
864
|
-
children;
|
|
865
|
-
constructor(e, t) {
|
|
866
|
-
super(), this.op = e, this.children = t.map((t) => E(t, e));
|
|
867
|
-
}
|
|
868
|
-
toString() {
|
|
869
|
-
return this.op === "not" ? `not(${this.children[0].toString()})` : `(${this.children.map((e) => e.toString()).join(` ${this.op} `)})`;
|
|
870
|
-
}
|
|
871
|
-
}, ie = class extends D {
|
|
872
|
-
spec;
|
|
873
|
-
discreteVars;
|
|
874
|
-
constructor(e, t) {
|
|
875
|
-
super(), this.spec = e, this.discreteVars = t;
|
|
876
|
-
}
|
|
877
|
-
toString() {
|
|
878
|
-
switch (this.spec.kind) {
|
|
879
|
-
case "distinct": return `distinct(${this.spec.args.map((e) => e.toString()).join(", ")})`;
|
|
880
|
-
case "disjoint": return `disjoint([${this.spec.tasks.map((e) => `{ start: ${e.start.toString()}, duration: ${e.duration} }`).join(", ")}])`;
|
|
881
|
-
case "cumulative": return `cumulative([${this.spec.tasks.map((e) => `{ start: ${e.start.toString()}, duration: ${e.duration}, demand: ${e.demand} }`).join(", ")}], ${this.spec.capacity})`;
|
|
882
|
-
case "allowed": return `allowed([${this.spec.vars.map((e) => e.toString()).join(", ")}], [${this.spec.tuples.map((e) => `[${e.join(", ")}]`).join(", ")}])`;
|
|
883
|
-
}
|
|
884
|
-
}
|
|
885
|
-
};
|
|
886
|
-
//#endregion
|
|
887
|
-
//#region src/modeler/types.ts
|
|
888
|
-
function ae(e) {
|
|
889
|
-
if (e === void 0) return "Minimize";
|
|
890
|
-
if (typeof e == "string") {
|
|
891
|
-
let t = e.toLowerCase();
|
|
892
|
-
if (t === "maximize") return "Maximize";
|
|
893
|
-
if (t === "minimize") return "Minimize";
|
|
894
|
-
}
|
|
895
|
-
throw Error(`Invalid optimization sense "${String(e)}": expected "Maximize", "Minimize", "maximize", or "minimize".`);
|
|
896
|
-
}
|
|
897
|
-
//#endregion
|
|
898
|
-
//#region src/modeler/solvers/registry.ts
|
|
899
|
-
var k = Symbol.for("washi.solverRegistry"), A = globalThis;
|
|
900
|
-
A[k] || (A[k] = /* @__PURE__ */ new Map());
|
|
901
|
-
var oe = A[k];
|
|
902
|
-
function se(e, t) {
|
|
903
|
-
oe.set(e.toLowerCase(), t);
|
|
904
|
-
}
|
|
905
|
-
//#endregion
|
|
906
7
|
//#region src/modeler/solvers/pumpkin/worker-client.ts
|
|
907
|
-
function
|
|
8
|
+
function _(e) {
|
|
908
9
|
if (e === "") throw Error("Invalid pumpkin.wasm URL: expected non-empty string or null (received empty string \"\").");
|
|
909
10
|
if (e != null && typeof e != "string") throw Error(`Invalid pumpkin.wasm URL: expected string or null, got ${typeof e}.`);
|
|
910
11
|
}
|
|
911
|
-
function
|
|
12
|
+
function v(e) {
|
|
912
13
|
return {
|
|
913
14
|
status: "Unknown",
|
|
914
15
|
values: [],
|
|
@@ -916,7 +17,7 @@ function M(e) {
|
|
|
916
17
|
runTime: Math.max((performance.now() - e) / 1e3, 1e-6)
|
|
917
18
|
};
|
|
918
19
|
}
|
|
919
|
-
function
|
|
20
|
+
function y(e, t) {
|
|
920
21
|
let n = Math.max((performance.now() - e) / 1e3, 1e-6);
|
|
921
22
|
return t ? {
|
|
922
23
|
status: "Feasible",
|
|
@@ -934,29 +35,29 @@ function N(e, t) {
|
|
|
934
35
|
runTime: n
|
|
935
36
|
};
|
|
936
37
|
}
|
|
937
|
-
function
|
|
938
|
-
let t = e.workerFactory ??
|
|
939
|
-
|
|
940
|
-
let
|
|
941
|
-
let t =
|
|
38
|
+
function b(e = {}) {
|
|
39
|
+
let t = e.workerFactory ?? g, n = null, r = 1, i = null, a = [], o = /* @__PURE__ */ new Set(), s = !1;
|
|
40
|
+
_(e.wasmUrl);
|
|
41
|
+
let c = (e) => (_(e), typeof e == "string" ? e : "__bundled__"), l = (e) => {
|
|
42
|
+
let t = c(e);
|
|
942
43
|
for (o.delete(t), o.add(t); o.size > 4;) {
|
|
943
44
|
let e = o.keys().next().value;
|
|
944
45
|
if (e === void 0) break;
|
|
945
46
|
o.delete(e);
|
|
946
47
|
}
|
|
947
|
-
},
|
|
48
|
+
}, u = () => {
|
|
948
49
|
o.clear(), n &&= (n.onmessage = null, n.onerror = null, n.onmessageerror = null, n.terminate(), null);
|
|
949
|
-
},
|
|
50
|
+
}, d = (e) => {
|
|
950
51
|
i === e && (i = null), e.cleanupAbort?.();
|
|
52
|
+
}, f = (e, t) => {
|
|
53
|
+
d(e), e.resolve(t), C();
|
|
54
|
+
}, p = (e, t) => {
|
|
55
|
+
d(e), e.resolve(t), C();
|
|
951
56
|
}, m = (e, t) => {
|
|
952
|
-
|
|
953
|
-
},
|
|
954
|
-
|
|
955
|
-
},
|
|
956
|
-
p(e), e.reject(t), b();
|
|
957
|
-
}, _ = (e) => {
|
|
958
|
-
e.type === "check" ? m(e, M(e.startTimeMs)) : h(e, N(e.startTimeMs, e.lastIncumbent));
|
|
959
|
-
}, v = (e, t, n) => {
|
|
57
|
+
d(e), e.reject(t), C();
|
|
58
|
+
}, b = (e) => {
|
|
59
|
+
e.type === "check" ? f(e, v(e.startTimeMs)) : p(e, y(e.startTimeMs, e.lastIncumbent));
|
|
60
|
+
}, x = (e, t, n) => {
|
|
960
61
|
if (typeof e != "string") throw Error("Pumpkin worker response is missing canonical responseJson string.");
|
|
961
62
|
let r = JSON.parse(e);
|
|
962
63
|
if (!r) throw Error("Pumpkin worker returned an empty success payload.");
|
|
@@ -965,7 +66,7 @@ function P(e = {}) {
|
|
|
965
66
|
...r,
|
|
966
67
|
runTime: typeof t == "number" ? t : Math.max((performance.now() - n) / 1e3, 1e-6)
|
|
967
68
|
};
|
|
968
|
-
},
|
|
69
|
+
}, S = () => {
|
|
969
70
|
if (n) return n;
|
|
970
71
|
let e = t();
|
|
971
72
|
return e.onmessage = (t) => {
|
|
@@ -986,92 +87,92 @@ function P(e = {}) {
|
|
|
986
87
|
});
|
|
987
88
|
}
|
|
988
89
|
if (n !== e || i !== a) return;
|
|
989
|
-
a.signal?.aborted && (
|
|
90
|
+
a.signal?.aborted && (u(), b(a));
|
|
990
91
|
} catch (t) {
|
|
991
|
-
n === e && i === a && (
|
|
92
|
+
n === e && i === a && (u(), m(a, t));
|
|
992
93
|
}
|
|
993
94
|
return;
|
|
994
95
|
}
|
|
995
96
|
if (r.type === "CHECK_SUCCESS") {
|
|
996
97
|
if (a.type !== "check") {
|
|
997
|
-
|
|
98
|
+
u(), m(a, /* @__PURE__ */ Error("Received \"CHECK_SUCCESS\" for a solve task."));
|
|
998
99
|
return;
|
|
999
100
|
}
|
|
1000
101
|
try {
|
|
1001
|
-
let e =
|
|
1002
|
-
|
|
102
|
+
let e = x(r.responseJson, r.runTime, a.startTimeMs);
|
|
103
|
+
l(a.wasmUrl), f(a, e);
|
|
1003
104
|
} catch (t) {
|
|
1004
|
-
n === e && i === a && (
|
|
105
|
+
n === e && i === a && (u(), m(a, t));
|
|
1005
106
|
}
|
|
1006
107
|
return;
|
|
1007
108
|
}
|
|
1008
109
|
if (r.type === "SOLVE_SUCCESS") {
|
|
1009
110
|
if (a.type !== "solve") {
|
|
1010
|
-
|
|
111
|
+
u(), m(a, /* @__PURE__ */ Error("Received \"SOLVE_SUCCESS\" for a check task."));
|
|
1011
112
|
return;
|
|
1012
113
|
}
|
|
1013
114
|
try {
|
|
1014
|
-
let e =
|
|
1015
|
-
|
|
115
|
+
let e = x(r.responseJson, r.runTime, a.startTimeMs);
|
|
116
|
+
l(a.wasmUrl), p(a, e);
|
|
1016
117
|
} catch (t) {
|
|
1017
|
-
n === e && i === a && (
|
|
118
|
+
n === e && i === a && (u(), m(a, t));
|
|
1018
119
|
}
|
|
1019
120
|
return;
|
|
1020
121
|
}
|
|
1021
122
|
if (r.type === "SOLVER_ERROR") {
|
|
1022
123
|
let e = typeof r.error == "string" ? r.error : "Pumpkin worker execution failed.";
|
|
1023
|
-
|
|
124
|
+
m(a, Error(e));
|
|
1024
125
|
}
|
|
1025
126
|
}
|
|
1026
127
|
}, e.onerror = (t) => {
|
|
1027
128
|
if (n !== e) return;
|
|
1028
129
|
let r = i;
|
|
1029
|
-
if (
|
|
130
|
+
if (u(), r) {
|
|
1030
131
|
let e = t?.message || "Pumpkin Web Worker encountered an unexpected error.";
|
|
1031
|
-
|
|
132
|
+
m(r, Error(e));
|
|
1032
133
|
}
|
|
1033
134
|
}, e.onmessageerror = () => {
|
|
1034
135
|
if (n !== e) return;
|
|
1035
136
|
let t = i;
|
|
1036
|
-
|
|
137
|
+
u(), t && m(t, /* @__PURE__ */ Error("Failed to deserialize message from Pumpkin Web Worker."));
|
|
1037
138
|
}, n = e, e;
|
|
1038
|
-
},
|
|
139
|
+
}, C = () => {
|
|
1039
140
|
if (!(i || s)) for (; a.length > 0;) {
|
|
1040
141
|
let e = a.shift();
|
|
1041
142
|
if (e.signal?.aborted) {
|
|
1042
|
-
e.type === "check" ? e.resolve(
|
|
143
|
+
e.type === "check" ? e.resolve(v(e.startTimeMs)) : e.resolve(y(e.startTimeMs, e.lastIncumbent));
|
|
1043
144
|
continue;
|
|
1044
145
|
}
|
|
1045
146
|
if (i = e, e.startTimeMs = performance.now(), e.signal) {
|
|
1046
147
|
let t = () => {
|
|
1047
|
-
i === e && (
|
|
148
|
+
i === e && (u(), b(e));
|
|
1048
149
|
};
|
|
1049
150
|
e.signal.addEventListener("abort", t, { once: !0 }), e.cleanupAbort = () => e.signal?.removeEventListener("abort", t);
|
|
1050
151
|
}
|
|
1051
152
|
try {
|
|
1052
|
-
|
|
153
|
+
S().postMessage({
|
|
1053
154
|
id: e.id,
|
|
1054
155
|
type: e.type,
|
|
1055
156
|
requestJson: e.requestJson,
|
|
1056
157
|
wasmUrl: e.wasmUrl
|
|
1057
158
|
});
|
|
1058
159
|
} catch (t) {
|
|
1059
|
-
i === e && (
|
|
160
|
+
i === e && (u(), m(e, t));
|
|
1060
161
|
}
|
|
1061
162
|
return;
|
|
1062
163
|
}
|
|
1063
|
-
},
|
|
1064
|
-
let n = t === void 0 ? e.wasmUrl === void 0 ?
|
|
1065
|
-
return
|
|
1066
|
-
},
|
|
1067
|
-
a.push(e(t, n)),
|
|
164
|
+
}, w = (t) => {
|
|
165
|
+
let n = t === void 0 ? e.wasmUrl === void 0 ? h() : e.wasmUrl : t;
|
|
166
|
+
return _(n), n ?? null;
|
|
167
|
+
}, T = (e) => new Promise((t, n) => {
|
|
168
|
+
a.push(e(t, n)), C();
|
|
1068
169
|
});
|
|
1069
170
|
return {
|
|
1070
171
|
check(e, t) {
|
|
1071
172
|
if (s) return Promise.reject(/* @__PURE__ */ Error("[WaSHI] Pumpkin worker client has been disposed."));
|
|
1072
|
-
if (t?.signal?.aborted) return Promise.resolve(
|
|
1073
|
-
let n =
|
|
1074
|
-
return
|
|
173
|
+
if (t?.signal?.aborted) return Promise.resolve(v(performance.now()));
|
|
174
|
+
let n = w(e.wasmUrl);
|
|
175
|
+
return T((i, a) => ({
|
|
1075
176
|
id: r++,
|
|
1076
177
|
type: "check",
|
|
1077
178
|
requestJson: e.requestJson,
|
|
@@ -1085,9 +186,9 @@ function P(e = {}) {
|
|
|
1085
186
|
},
|
|
1086
187
|
solve(e, t) {
|
|
1087
188
|
if (s) return Promise.reject(/* @__PURE__ */ Error("[WaSHI] Pumpkin worker client has been disposed."));
|
|
1088
|
-
if (t?.signal?.aborted) return Promise.resolve(
|
|
1089
|
-
let n =
|
|
1090
|
-
return
|
|
189
|
+
if (t?.signal?.aborted) return Promise.resolve(y(performance.now(), null));
|
|
190
|
+
let n = w(e.wasmUrl);
|
|
191
|
+
return T((i, a) => ({
|
|
1091
192
|
id: r++,
|
|
1092
193
|
type: "solve",
|
|
1093
194
|
requestJson: e.requestJson,
|
|
@@ -1101,10 +202,10 @@ function P(e = {}) {
|
|
|
1101
202
|
}));
|
|
1102
203
|
},
|
|
1103
204
|
isWarmed(e) {
|
|
1104
|
-
return !s && n !== null && o.has(
|
|
205
|
+
return !s && n !== null && o.has(c(w(e)));
|
|
1105
206
|
},
|
|
1106
207
|
dispose() {
|
|
1107
|
-
for (s = !0,
|
|
208
|
+
for (s = !0, u(); a.length > 0;) {
|
|
1108
209
|
let e = a.shift();
|
|
1109
210
|
e.cleanupAbort?.(), e.reject(/* @__PURE__ */ Error("[WaSHI] Pumpkin worker client disposed."));
|
|
1110
211
|
}
|
|
@@ -1115,24 +216,24 @@ function P(e = {}) {
|
|
|
1115
216
|
}
|
|
1116
217
|
};
|
|
1117
218
|
}
|
|
1118
|
-
var
|
|
1119
|
-
function
|
|
1120
|
-
return
|
|
1121
|
-
|
|
1122
|
-
})),
|
|
219
|
+
var x = null;
|
|
220
|
+
function S() {
|
|
221
|
+
return x || (x = b(), s(() => {
|
|
222
|
+
x &&= (x.dispose(), null);
|
|
223
|
+
})), x;
|
|
1123
224
|
}
|
|
1124
|
-
function
|
|
1125
|
-
|
|
225
|
+
function C() {
|
|
226
|
+
x &&= (x.dispose(), null);
|
|
1126
227
|
}
|
|
1127
|
-
function
|
|
1128
|
-
return
|
|
228
|
+
function w(e = {}) {
|
|
229
|
+
return _(e.wasmUrl), e.useWorker ?? u() ? x !== null && x.isWarmed(e.wasmUrl) : d(e.wasmUrl);
|
|
1129
230
|
}
|
|
1130
|
-
async function
|
|
231
|
+
async function T(e = {}) {
|
|
1131
232
|
let { signal: t } = e;
|
|
1132
|
-
if (
|
|
1133
|
-
if (
|
|
1134
|
-
let
|
|
1135
|
-
return
|
|
233
|
+
if (_(e.wasmUrl), t?.aborted) return { cached: !1 };
|
|
234
|
+
if (w(e)) return { cached: !0 };
|
|
235
|
+
let n = !!(e.useWorker ?? u()), r = e.wasmUrl === void 0 ? h() : e.wasmUrl;
|
|
236
|
+
return n ? (await S().check({
|
|
1136
237
|
requestJson: JSON.stringify({
|
|
1137
238
|
vars: [],
|
|
1138
239
|
boolVars: [],
|
|
@@ -1141,25 +242,25 @@ async function R(e = {}) {
|
|
|
1141
242
|
auxPropagators: [],
|
|
1142
243
|
timeLimitMs: null
|
|
1143
244
|
}),
|
|
1144
|
-
wasmUrl:
|
|
1145
|
-
}, { signal: t }), { cached: !1 }) : t ? (await new Promise((e,
|
|
1146
|
-
let i = !1,
|
|
245
|
+
wasmUrl: r
|
|
246
|
+
}, { signal: t }), { cached: !1 }) : t ? (await new Promise((e, n) => {
|
|
247
|
+
let i = !1, a = () => {
|
|
1147
248
|
i || (i = !0, e());
|
|
1148
249
|
};
|
|
1149
|
-
t.addEventListener("abort",
|
|
1150
|
-
t.removeEventListener("abort",
|
|
250
|
+
t.addEventListener("abort", a, { once: !0 }), l(r).then(() => {
|
|
251
|
+
t.removeEventListener("abort", a), i || (i = !0, e());
|
|
1151
252
|
}, (e) => {
|
|
1152
|
-
t.removeEventListener("abort",
|
|
253
|
+
t.removeEventListener("abort", a), i || (i = !0, n(e));
|
|
1153
254
|
});
|
|
1154
|
-
}), { cached: !1 }) : (await
|
|
255
|
+
}), { cached: !1 }) : (await l(r), { cached: !1 });
|
|
1155
256
|
}
|
|
1156
257
|
//#endregion
|
|
1157
258
|
//#region src/modeler/solvers/pumpkin/solver.ts
|
|
1158
|
-
var
|
|
1159
|
-
function
|
|
259
|
+
var E = 1073741823, D = 2147483646, O = 1e-6;
|
|
260
|
+
function k(e) {
|
|
1160
261
|
return Object.is(e, -0) || e === 0 ? 0 : e;
|
|
1161
262
|
}
|
|
1162
|
-
function
|
|
263
|
+
function A(e, t, n) {
|
|
1163
264
|
Object.defineProperty(e, t, {
|
|
1164
265
|
value: n,
|
|
1165
266
|
writable: !0,
|
|
@@ -1167,15 +268,15 @@ function U(e, t, n) {
|
|
|
1167
268
|
configurable: !0
|
|
1168
269
|
});
|
|
1169
270
|
}
|
|
1170
|
-
function
|
|
271
|
+
function j(e, t) {
|
|
1171
272
|
if (typeof e != "number" || Number.isNaN(e)) throw Error(`PumpkinSolver encountered non-finite numeric value in ${t}.`);
|
|
1172
273
|
if (!Number.isFinite(e)) throw Error(`PumpkinSolver only supports bounded finite integers (${t}: got ${e}).`);
|
|
1173
274
|
if (!Number.isInteger(e)) throw Error(`PumpkinSolver only supports integer values (${t}: got ${e}).`);
|
|
1174
|
-
let n =
|
|
1175
|
-
if (n < -1073741823 || n >
|
|
275
|
+
let n = k(e);
|
|
276
|
+
if (n < -1073741823 || n > E) throw Error(`Value out of Pumpkin safe 32-bit integer range [-${E}, ${E}] (${t}: got ${n}).`);
|
|
1176
277
|
return n;
|
|
1177
278
|
}
|
|
1178
|
-
function
|
|
279
|
+
function M(e) {
|
|
1179
280
|
if (e === void 0 || e === Infinity) return null;
|
|
1180
281
|
if (typeof e != "number" || Number.isNaN(e) || e < 0 || !Number.isFinite(e)) throw Error(`Invalid timeLimit (${String(e)}): expected a finite non-negative number of seconds.`);
|
|
1181
282
|
if (e === 0) return 0;
|
|
@@ -1183,18 +284,18 @@ function G(e) {
|
|
|
1183
284
|
if (t > 2 ** 53 - 1) throw Error(`timeLimit (${e}s) exceeds safe millisecond range.`);
|
|
1184
285
|
return Math.max(1, t);
|
|
1185
286
|
}
|
|
1186
|
-
function
|
|
1187
|
-
return e instanceof
|
|
287
|
+
function N(e) {
|
|
288
|
+
return e instanceof r || typeof e == "object" && !!e && "quadTerms" in e;
|
|
1188
289
|
}
|
|
1189
|
-
function
|
|
290
|
+
function P(e, t, n) {
|
|
1190
291
|
if (e.initialValue === void 0) return;
|
|
1191
292
|
if (typeof e.initialValue != "number" || !Number.isFinite(e.initialValue)) throw Error(`Variable "${e.name}" has invalid initialValue: ${String(e.initialValue)} (initialValue must be a finite number).`);
|
|
1192
|
-
if (e.initialValue < t -
|
|
1193
|
-
let r =
|
|
1194
|
-
if (Math.abs(e.initialValue - r) >
|
|
1195
|
-
return
|
|
293
|
+
if (e.initialValue < t - O || e.initialValue > n + O) throw Error(`Variable "${e.name}" has initialValue (${e.initialValue}) outside its bounds [${t}, ${n}] (tolerance ${O}).`);
|
|
294
|
+
let r = k(Math.round(e.initialValue));
|
|
295
|
+
if (Math.abs(e.initialValue - r) > O || r < t || r > n) throw Error(`Variable "${e.name}" of type "${e.type}" has non-integral or out-of-domain initialValue: ${e.initialValue} (must be within ${O} of an integer in [${t}, ${n}]).`);
|
|
296
|
+
return j(r, `initialValue of variable "${e.name}"`);
|
|
1196
297
|
}
|
|
1197
|
-
function
|
|
298
|
+
function F(e) {
|
|
1198
299
|
let t = [], n = /* @__PURE__ */ new Map(), r = !1;
|
|
1199
300
|
for (let i = 0; i < e.length; i++) {
|
|
1200
301
|
let a = e[i], o = String(a.type).toLowerCase();
|
|
@@ -1203,7 +304,7 @@ function le(e) {
|
|
|
1203
304
|
if (typeof a.lb != "number" || Number.isNaN(a.lb) || typeof a.ub != "number" || Number.isNaN(a.ub)) throw Error(`PumpkinSolver encountered non-finite numeric value in bounds of variable "${a.name}".`);
|
|
1204
305
|
if (a.lb === Infinity || a.ub === -Infinity || a.lb > a.ub) throw Error(`Variable "${a.name}" has invalid bounds lb (${a.lb}) > ub (${a.ub}).`);
|
|
1205
306
|
if (Number.isFinite(a.lb) && !Number.isInteger(a.lb) || Number.isFinite(a.ub) && !Number.isInteger(a.ub)) throw Error(`PumpkinSolver only supports integer values (bounds of binary variable "${a.name}").`);
|
|
1206
|
-
let e = a.lb === -Infinity ? 0 : Math.max(0,
|
|
307
|
+
let e = a.lb === -Infinity ? 0 : Math.max(0, k(a.lb)), n = a.ub === Infinity ? 1 : Math.min(1, k(a.ub)), i = P(a, e, n);
|
|
1207
308
|
e > n ? (r = !0, t.push({
|
|
1208
309
|
name: a.name,
|
|
1209
310
|
lb: 0,
|
|
@@ -1220,9 +321,9 @@ function le(e) {
|
|
|
1220
321
|
});
|
|
1221
322
|
continue;
|
|
1222
323
|
}
|
|
1223
|
-
let s =
|
|
324
|
+
let s = j(a.lb, `lower bound of variable "${a.name}"`), c = j(a.ub, `upper bound of variable "${a.name}"`);
|
|
1224
325
|
if (s > c) throw Error(`Variable "${a.name}" has invalid bounds lb (${s}) > ub (${c}).`);
|
|
1225
|
-
let l =
|
|
326
|
+
let l = P(a, s, c);
|
|
1226
327
|
t.push(l === void 0 ? {
|
|
1227
328
|
name: a.name,
|
|
1228
329
|
lb: s,
|
|
@@ -1240,10 +341,10 @@ function le(e) {
|
|
|
1240
341
|
hasEmptyDomain: r
|
|
1241
342
|
};
|
|
1242
343
|
}
|
|
1243
|
-
function
|
|
344
|
+
function I(e, t, n) {
|
|
1244
345
|
let r = 0;
|
|
1245
346
|
function i(e, n, i) {
|
|
1246
|
-
let a =
|
|
347
|
+
let a = j(e, `internal ${i} lb`), o = j(n, `internal ${i} ub`), s = t.length;
|
|
1247
348
|
return t.push({
|
|
1248
349
|
name: `__pumpkin_${i}_${++r}`,
|
|
1249
350
|
lb: a,
|
|
@@ -1258,23 +359,23 @@ function ue(e, t, n) {
|
|
|
1258
359
|
if (n === void 0) throw Error(`Variable "${t.name}" in ${i} is not registered in the model.`);
|
|
1259
360
|
if (typeof r != "number" || !Number.isFinite(r)) throw Error(`Invalid non-finite coefficient for "${t.name}" in ${i}.`);
|
|
1260
361
|
if (!Number.isInteger(r)) throw Error(`PumpkinSolver only supports integer coefficients (variable "${t.name}" in ${i} has ${r}).`);
|
|
1261
|
-
let o = (a.get(n) ?? 0) +
|
|
362
|
+
let o = (a.get(n) ?? 0) + k(r);
|
|
1262
363
|
if (!Number.isSafeInteger(o)) throw Error(`Coefficient sum overflow for "${t.name}" in ${i}.`);
|
|
1263
364
|
a.set(n, o);
|
|
1264
365
|
}
|
|
1265
366
|
for (let e of r) {
|
|
1266
|
-
let t = (a.get(e.varIndex) ?? 0) +
|
|
367
|
+
let t = (a.get(e.varIndex) ?? 0) + k(e.coeff);
|
|
1267
368
|
if (!Number.isSafeInteger(t)) throw Error(`Coefficient sum overflow in ${i}.`);
|
|
1268
369
|
a.set(e.varIndex, t);
|
|
1269
370
|
}
|
|
1270
371
|
let o = [], s = 0;
|
|
1271
372
|
for (let [e, n] of a.entries()) {
|
|
1272
|
-
let r =
|
|
373
|
+
let r = k(n);
|
|
1273
374
|
if (r === 0) continue;
|
|
1274
375
|
let a = t[e];
|
|
1275
|
-
|
|
376
|
+
j(r, `coefficient of "${a.name}" in ${i}`);
|
|
1276
377
|
let c = r * a.lb, l = r * a.ub;
|
|
1277
|
-
if (
|
|
378
|
+
if (j(c, `scaled lower bound of "${a.name}" in ${i}`), j(l, `scaled upper bound of "${a.name}" in ${i}`), s += Math.max(Math.abs(c), Math.abs(l)), s > D) throw Error(`Linear expression magnitude in ${i} exceeds Pumpkin safe 32-bit propagator overflow limit.`);
|
|
1278
379
|
o.push({
|
|
1279
380
|
varIndex: e,
|
|
1280
381
|
coeff: r
|
|
@@ -1292,8 +393,8 @@ function ue(e, t, n) {
|
|
|
1292
393
|
i += Math.min(r, o), a += Math.max(r, o);
|
|
1293
394
|
}
|
|
1294
395
|
return {
|
|
1295
|
-
lb:
|
|
1296
|
-
ub:
|
|
396
|
+
lb: j(k(i), `lower bound of ${r}`),
|
|
397
|
+
ub: j(k(a), `upper bound of ${r}`)
|
|
1297
398
|
};
|
|
1298
399
|
}
|
|
1299
400
|
function s(e, r, a) {
|
|
@@ -1340,16 +441,16 @@ function ue(e, t, n) {
|
|
|
1340
441
|
function c(e, t, n, r = !1) {
|
|
1341
442
|
if (typeof e.constant != "number" || !Number.isFinite(e.constant)) throw Error(`Invalid non-finite constant in ${n}.`);
|
|
1342
443
|
let i;
|
|
1343
|
-
if (r) i =
|
|
444
|
+
if (r) i = k(e.constant);
|
|
1344
445
|
else {
|
|
1345
446
|
if (!Number.isInteger(e.constant)) throw Error(`PumpkinSolver only supports integer constants (${n}: got ${e.constant}).`);
|
|
1346
|
-
i =
|
|
447
|
+
i = j(k(e.constant), `constant of ${n}`);
|
|
1347
448
|
}
|
|
1348
449
|
let o = [];
|
|
1349
450
|
if (Array.isArray(e.nonlinearTerms)) for (let r of e.nonlinearTerms) {
|
|
1350
451
|
if (r.coeff === 0) continue;
|
|
1351
452
|
if (typeof r.coeff != "number" || !Number.isFinite(r.coeff) || !Number.isInteger(r.coeff)) throw Error(`PumpkinSolver only supports safe integer coefficients in ${n} (got ${r.coeff}).`);
|
|
1352
|
-
let e =
|
|
453
|
+
let e = j(k(r.coeff), `nonlinear coefficient in ${n}`), i = s(r.atom, t, n);
|
|
1353
454
|
o.push({
|
|
1354
455
|
varIndex: i,
|
|
1355
456
|
coeff: e
|
|
@@ -1370,11 +471,11 @@ function ue(e, t, n) {
|
|
|
1370
471
|
let e = [...s, {
|
|
1371
472
|
varIndex: f,
|
|
1372
473
|
coeff: -1
|
|
1373
|
-
}], i =
|
|
474
|
+
}], i = j(k(-l), `linearEq RHS in ${a}`), o = e.reduce((e, n) => {
|
|
1374
475
|
let r = t[n.varIndex];
|
|
1375
476
|
return e + Math.max(Math.abs(n.coeff * r.lb), Math.abs(n.coeff * r.ub));
|
|
1376
477
|
}, 0);
|
|
1377
|
-
if (Math.abs(i) + o >
|
|
478
|
+
if (Math.abs(i) + o > D) throw Error(`Linear expression magnitude in ${a} exceeds Pumpkin safe 32-bit propagator overflow limit.`);
|
|
1378
479
|
n.push({
|
|
1379
480
|
kind: "linearEq",
|
|
1380
481
|
terms: e,
|
|
@@ -1387,8 +488,8 @@ function ue(e, t, n) {
|
|
|
1387
488
|
function u(e, t, n, r) {
|
|
1388
489
|
if (typeof t != "number" || !Number.isFinite(t)) throw Error(`Invalid non-finite RHS in ${r}.`);
|
|
1389
490
|
if (!Number.isInteger(t)) throw Error(`PumpkinSolver only supports integer RHS (${r}: got ${t}).`);
|
|
1390
|
-
let { terms: i, constant: a, sumMaxAbs: o } = c(e, n, r), s =
|
|
1391
|
-
if (Math.abs(s) + o >
|
|
491
|
+
let { terms: i, constant: a, sumMaxAbs: o } = c(e, n, r), s = j(k(t - a), `RHS of ${r}`);
|
|
492
|
+
if (Math.abs(s) + o > D) throw Error(`Linear expression magnitude in ${r} exceeds Pumpkin safe 32-bit propagator overflow limit.`);
|
|
1392
493
|
return {
|
|
1393
494
|
terms: i,
|
|
1394
495
|
rhs: s
|
|
@@ -1400,91 +501,91 @@ function ue(e, t, n) {
|
|
|
1400
501
|
serializeConstraintSide: u
|
|
1401
502
|
};
|
|
1402
503
|
}
|
|
1403
|
-
function
|
|
1404
|
-
if (
|
|
1405
|
-
let
|
|
1406
|
-
if (
|
|
504
|
+
function L(r, a, o, s, c) {
|
|
505
|
+
if (s.has(r) || r instanceof i || typeof r == "object" && r && "name" in r && "id" in r && !("op" in r) && !("lhs" in r) && !("spec" in r)) {
|
|
506
|
+
let e = r, t = s.get(e);
|
|
507
|
+
if (t === void 0) throw Error(`Boolean variable "${e.name}" in ${c} is not registered in the model.`);
|
|
1407
508
|
return {
|
|
1408
509
|
kind: "boolVar",
|
|
1409
|
-
index:
|
|
510
|
+
index: t
|
|
1410
511
|
};
|
|
1411
512
|
}
|
|
1412
|
-
if (
|
|
1413
|
-
let
|
|
1414
|
-
if (
|
|
1415
|
-
let { terms:
|
|
513
|
+
if (r instanceof t || "lhs" in r && "sense" in r && "rhs" in r) {
|
|
514
|
+
let e = r;
|
|
515
|
+
if (N(e.lhs)) throw Error(`PumpkinSolver does not support quadratic constraints inside ${c}.`);
|
|
516
|
+
let { terms: t, rhs: n } = a.serializeConstraintSide(e.lhs, e.rhs, o, c);
|
|
1416
517
|
return {
|
|
1417
518
|
kind: "linear",
|
|
1418
|
-
terms:
|
|
1419
|
-
sense:
|
|
1420
|
-
rhs:
|
|
519
|
+
terms: t,
|
|
520
|
+
sense: e.sense,
|
|
521
|
+
rhs: n
|
|
1421
522
|
};
|
|
1422
523
|
}
|
|
1423
|
-
if (
|
|
1424
|
-
let
|
|
1425
|
-
switch (
|
|
524
|
+
if (r instanceof e || typeof r == "object" && r && "spec" in r && typeof r.spec == "object") {
|
|
525
|
+
let e = r.spec;
|
|
526
|
+
switch (e.kind) {
|
|
1426
527
|
case "distinct": return {
|
|
1427
528
|
kind: "allDifferent",
|
|
1428
|
-
vars:
|
|
529
|
+
vars: e.args.map((e, t) => a.materializeExprToVarIndex(e, o, `${c} distinct()[${t}]`))
|
|
1429
530
|
};
|
|
1430
531
|
case "disjoint": return {
|
|
1431
532
|
kind: "disjunctive",
|
|
1432
|
-
tasks:
|
|
1433
|
-
startVar:
|
|
1434
|
-
duration:
|
|
533
|
+
tasks: e.tasks.map((e, t) => ({
|
|
534
|
+
startVar: a.materializeExprToVarIndex(e.start, o, `${c} disjoint()[${t}]`),
|
|
535
|
+
duration: j(e.duration, `duration of ${c} disjoint()[${t}]`)
|
|
1435
536
|
}))
|
|
1436
537
|
};
|
|
1437
538
|
case "cumulative": return {
|
|
1438
539
|
kind: "cumulative",
|
|
1439
|
-
tasks:
|
|
1440
|
-
startVar:
|
|
1441
|
-
duration:
|
|
1442
|
-
demand:
|
|
540
|
+
tasks: e.tasks.map((e, t) => ({
|
|
541
|
+
startVar: a.materializeExprToVarIndex(e.start, o, `${c} cumulative()[${t}]`),
|
|
542
|
+
duration: j(e.duration, `duration of ${c} cumulative()[${t}]`),
|
|
543
|
+
demand: j(e.demand, `demand of ${c} cumulative()[${t}]`)
|
|
1443
544
|
})),
|
|
1444
|
-
capacity:
|
|
545
|
+
capacity: j(e.capacity, `capacity of ${c} cumulative()`)
|
|
1445
546
|
};
|
|
1446
547
|
case "allowed": return {
|
|
1447
548
|
kind: "table",
|
|
1448
|
-
vars:
|
|
1449
|
-
tuples:
|
|
549
|
+
vars: e.vars.map((e, t) => a.materializeExprToVarIndex(e, o, `${c} allowed() vars[${t}]`)),
|
|
550
|
+
tuples: e.tuples.map((e, t) => e.map((e, n) => j(e, `tuple[${t}][${n}] in ${c}`)))
|
|
1450
551
|
};
|
|
1451
552
|
}
|
|
1452
553
|
}
|
|
1453
|
-
if (
|
|
1454
|
-
let
|
|
1455
|
-
switch (
|
|
554
|
+
if (r instanceof n || "op" in r && "children" in r) {
|
|
555
|
+
let e = r;
|
|
556
|
+
switch (e.op) {
|
|
1456
557
|
case "and": return {
|
|
1457
558
|
kind: "and",
|
|
1458
|
-
children:
|
|
559
|
+
children: e.children.map((e) => L(e, a, o, s, c))
|
|
1459
560
|
};
|
|
1460
561
|
case "or": return {
|
|
1461
562
|
kind: "or",
|
|
1462
|
-
children:
|
|
563
|
+
children: e.children.map((e) => L(e, a, o, s, c))
|
|
1463
564
|
};
|
|
1464
565
|
case "not":
|
|
1465
|
-
if (
|
|
566
|
+
if (e.children.length !== 1) throw Error(`Invalid 'not' operator arity in ${c}.`);
|
|
1466
567
|
return {
|
|
1467
568
|
kind: "not",
|
|
1468
|
-
child:
|
|
569
|
+
child: L(e.children[0], a, o, s, c)
|
|
1469
570
|
};
|
|
1470
571
|
case "implies":
|
|
1471
|
-
if (
|
|
572
|
+
if (e.children.length !== 2) throw Error(`Invalid 'implies' operator arity in ${c}.`);
|
|
1472
573
|
return {
|
|
1473
574
|
kind: "implies",
|
|
1474
|
-
left:
|
|
1475
|
-
right:
|
|
575
|
+
left: L(e.children[0], a, o, s, c),
|
|
576
|
+
right: L(e.children[1], a, o, s, c)
|
|
1476
577
|
};
|
|
1477
578
|
case "xor": return {
|
|
1478
579
|
kind: "xor",
|
|
1479
|
-
children:
|
|
580
|
+
children: e.children.map((e) => L(e, a, o, s, c))
|
|
1480
581
|
};
|
|
1481
|
-
default: throw Error(`Unsupported logical operator "${String(
|
|
582
|
+
default: throw Error(`Unsupported logical operator "${String(e.op)}" in ${c}.`);
|
|
1482
583
|
}
|
|
1483
584
|
}
|
|
1484
|
-
throw Error(`Unsupported logical constraint node in ${
|
|
585
|
+
throw Error(`Unsupported logical constraint node in ${c}.`);
|
|
1485
586
|
}
|
|
1486
|
-
function
|
|
1487
|
-
let { vars: t, varIndexMap: n, hasEmptyDomain: r } =
|
|
587
|
+
function R(e) {
|
|
588
|
+
let { vars: t, varIndexMap: n, hasEmptyDomain: r } = F(e.variables), i = [], a = I(n, t, i), o = e.boolVars ?? [], s = /* @__PURE__ */ new Map(), c = [];
|
|
1488
589
|
for (let e = 0; e < o.length; e++) s.set(o[e], e), c.push(o[e].name);
|
|
1489
590
|
let l = [];
|
|
1490
591
|
for (let t = 0; t < e.constraints.length; t++) {
|
|
@@ -1508,7 +609,7 @@ function Y(e) {
|
|
|
1508
609
|
});
|
|
1509
610
|
continue;
|
|
1510
611
|
}
|
|
1511
|
-
if (
|
|
612
|
+
if (N(r.lhs)) throw Error(`PumpkinSolver does not support quadratic constraints (${i}).`);
|
|
1512
613
|
let { terms: s, rhs: c } = a.serializeConstraintSide(r.lhs, r.rhs, o, i);
|
|
1513
614
|
l.push({
|
|
1514
615
|
name: r.name,
|
|
@@ -1522,7 +623,7 @@ function Y(e) {
|
|
|
1522
623
|
let t = d[e], n = `logical constraint "${t.name}"`, r = {
|
|
1523
624
|
kind: "logic",
|
|
1524
625
|
index: e
|
|
1525
|
-
}, i =
|
|
626
|
+
}, i = L(t.expr, a, r, s, n);
|
|
1526
627
|
u.push({
|
|
1527
628
|
name: t.name,
|
|
1528
629
|
expr: i
|
|
@@ -1539,33 +640,33 @@ function Y(e) {
|
|
|
1539
640
|
hasEmptyDomain: r
|
|
1540
641
|
};
|
|
1541
642
|
}
|
|
1542
|
-
function
|
|
643
|
+
function z(e, t, n) {
|
|
1543
644
|
let r = {};
|
|
1544
|
-
if (e) for (let [t, n] of e)
|
|
645
|
+
if (e) for (let [t, n] of e) A(r, t, k(n));
|
|
1545
646
|
let i;
|
|
1546
647
|
if (t && t.length > 0) {
|
|
1547
648
|
i = {};
|
|
1548
|
-
for (let [e, a] of t)
|
|
649
|
+
for (let [e, a] of t) A(i, e, a), n && A(r, e, +!!a);
|
|
1549
650
|
}
|
|
1550
651
|
return {
|
|
1551
652
|
values: r,
|
|
1552
653
|
...i === void 0 ? {} : { boolValues: i }
|
|
1553
654
|
};
|
|
1554
655
|
}
|
|
1555
|
-
function
|
|
656
|
+
function B(e, t) {
|
|
1556
657
|
let n = {};
|
|
1557
|
-
if (e) for (let [t, r] of e)
|
|
1558
|
-
if (t) for (let [e, r] of t)
|
|
658
|
+
if (e) for (let [t, r] of e) A(n, t, k(r));
|
|
659
|
+
if (t) for (let [e, r] of t) A(n, e, r);
|
|
1559
660
|
return n;
|
|
1560
661
|
}
|
|
1561
|
-
var
|
|
662
|
+
var V = /* @__PURE__ */ new Set([
|
|
1562
663
|
"presolve",
|
|
1563
664
|
"relativeGap",
|
|
1564
665
|
"absoluteGap",
|
|
1565
666
|
"threads",
|
|
1566
667
|
"verbose",
|
|
1567
668
|
"outputFlag"
|
|
1568
|
-
]),
|
|
669
|
+
]), H = /* @__PURE__ */ new Set([
|
|
1569
670
|
"timeLimit",
|
|
1570
671
|
"wasmUrl",
|
|
1571
672
|
"useWorker",
|
|
@@ -1573,22 +674,22 @@ var pe = /* @__PURE__ */ new Set([
|
|
|
1573
674
|
"onProgress",
|
|
1574
675
|
"relax"
|
|
1575
676
|
]);
|
|
1576
|
-
function
|
|
677
|
+
function U(e = {}) {
|
|
1577
678
|
if (e.relax !== void 0) {
|
|
1578
679
|
if (typeof e.relax != "boolean") throw Error(`Invalid Pumpkin option "relax": expected boolean, got ${typeof e.relax}.`);
|
|
1579
680
|
if (e.relax === !0) throw Error("PumpkinSolver does not support LP relaxation (relax: true). Use a simplex/IPM solver such as HiGHS or GLPK.");
|
|
1580
681
|
}
|
|
1581
|
-
for (let t of
|
|
1582
|
-
for (let [t, n] of Object.entries(e)) if (n !== void 0 && !
|
|
682
|
+
for (let t of V) if (e[t] !== void 0) throw Error(`PumpkinSolver does not support option "${t}".`);
|
|
683
|
+
for (let [t, n] of Object.entries(e)) if (n !== void 0 && !H.has(t)) throw Error(`PumpkinSolver does not support option "${t}".`);
|
|
1583
684
|
if (e.useWorker !== void 0 && typeof e.useWorker != "boolean") throw Error(`Invalid Pumpkin option "useWorker": expected boolean, got ${typeof e.useWorker}.`);
|
|
1584
685
|
if (e.onProgress !== void 0 && typeof e.onProgress != "function") throw Error(`Invalid Pumpkin option "onProgress": expected function, got ${typeof e.onProgress}.`);
|
|
1585
686
|
}
|
|
1586
|
-
function
|
|
687
|
+
function W(e) {
|
|
1587
688
|
if (e.wasmUrl !== void 0 && e.wasmUrl !== null && typeof e.wasmUrl != "string") throw Error(`Invalid pumpkin.wasm URL: expected string or null, got ${typeof e.wasmUrl}.`);
|
|
1588
689
|
if (e.wasmUrl === "") throw Error("Invalid pumpkin.wasm URL: expected non-empty string or null (received empty string \"\").");
|
|
1589
|
-
return e.wasmUrl === void 0 ?
|
|
690
|
+
return e.wasmUrl === void 0 ? h() : e.wasmUrl;
|
|
1590
691
|
}
|
|
1591
|
-
var
|
|
692
|
+
var G = class e {
|
|
1592
693
|
static capabilities = {
|
|
1593
694
|
supportsMIP: !0,
|
|
1594
695
|
supportsQP: !1,
|
|
@@ -1605,54 +706,54 @@ var Q = class e {
|
|
|
1605
706
|
this.options = e;
|
|
1606
707
|
}
|
|
1607
708
|
async check(e, t = {}) {
|
|
1608
|
-
let
|
|
709
|
+
let n = {
|
|
1609
710
|
...this.options,
|
|
1610
711
|
...t
|
|
1611
712
|
};
|
|
1612
|
-
|
|
1613
|
-
let
|
|
1614
|
-
if (
|
|
713
|
+
U(n);
|
|
714
|
+
let r = W(n), i = M(n.timeLimit), a = R(e);
|
|
715
|
+
if (n.signal?.aborted) return {
|
|
1615
716
|
status: "Unknown",
|
|
1616
717
|
values: {},
|
|
1617
718
|
runTime: 0
|
|
1618
719
|
};
|
|
1619
|
-
if (
|
|
720
|
+
if (a.hasEmptyDomain) return {
|
|
1620
721
|
status: "Unsat",
|
|
1621
722
|
values: {},
|
|
1622
723
|
unsatCore: [],
|
|
1623
724
|
conflicts: [],
|
|
1624
725
|
runTime: 1e-6
|
|
1625
726
|
};
|
|
1626
|
-
let
|
|
1627
|
-
vars:
|
|
1628
|
-
boolVars:
|
|
1629
|
-
constraints:
|
|
1630
|
-
logicalConstraints:
|
|
1631
|
-
auxPropagators:
|
|
1632
|
-
timeLimitMs:
|
|
1633
|
-
}),
|
|
1634
|
-
if (
|
|
1635
|
-
requestJson:
|
|
1636
|
-
wasmUrl:
|
|
1637
|
-
}, { signal:
|
|
727
|
+
let o = JSON.stringify({
|
|
728
|
+
vars: a.vars,
|
|
729
|
+
boolVars: a.boolVars,
|
|
730
|
+
constraints: a.constraints,
|
|
731
|
+
logicalConstraints: a.logicalConstraints,
|
|
732
|
+
auxPropagators: a.auxPropagators,
|
|
733
|
+
timeLimitMs: i
|
|
734
|
+
}), s = n.useWorker ?? u(), c;
|
|
735
|
+
if (s) c = await S().check({
|
|
736
|
+
requestJson: o,
|
|
737
|
+
wasmUrl: r
|
|
738
|
+
}, { signal: n.signal });
|
|
1638
739
|
else {
|
|
1639
|
-
let e = await
|
|
1640
|
-
if (
|
|
740
|
+
let e = await l(r);
|
|
741
|
+
if (n.signal?.aborted) return {
|
|
1641
742
|
status: "Unknown",
|
|
1642
743
|
values: {},
|
|
1643
744
|
runTime: 0
|
|
1644
745
|
};
|
|
1645
|
-
let t = performance.now(),
|
|
1646
|
-
if (
|
|
746
|
+
let t = performance.now(), i = e.check_with_callback(o, () => !!n.signal?.aborted), a = Math.max((performance.now() - t) / 1e3, 1e-6);
|
|
747
|
+
if (c = JSON.parse(i), c.runTime = a, c.error) throw Error(c.error);
|
|
1647
748
|
}
|
|
1648
|
-
let d =
|
|
1649
|
-
if (
|
|
749
|
+
let d = c.runTime ?? 0;
|
|
750
|
+
if (c.status === "Sat") return {
|
|
1650
751
|
status: "Sat",
|
|
1651
|
-
values:
|
|
752
|
+
values: B(c.values, c.boolValues),
|
|
1652
753
|
runTime: d
|
|
1653
754
|
};
|
|
1654
|
-
if (
|
|
1655
|
-
let e =
|
|
755
|
+
if (c.status === "Unsat") {
|
|
756
|
+
let e = c.unsatCore ? [...c.unsatCore] : [];
|
|
1656
757
|
return {
|
|
1657
758
|
status: "Unsat",
|
|
1658
759
|
values: {},
|
|
@@ -1668,18 +769,18 @@ var Q = class e {
|
|
|
1668
769
|
};
|
|
1669
770
|
}
|
|
1670
771
|
async solve(e, t = {}) {
|
|
1671
|
-
let
|
|
772
|
+
let n = {
|
|
1672
773
|
...this.options,
|
|
1673
774
|
...t
|
|
1674
775
|
};
|
|
1675
|
-
|
|
1676
|
-
let
|
|
1677
|
-
if (
|
|
1678
|
-
let
|
|
1679
|
-
if (typeof
|
|
1680
|
-
let
|
|
1681
|
-
if (
|
|
1682
|
-
if (
|
|
776
|
+
U(n);
|
|
777
|
+
let r = W(n), i = M(n.timeLimit), a = o(e.sense), s = R(e);
|
|
778
|
+
if (N(e.objective)) throw Error("PumpkinSolver does not support quadratic objective expressions (QuadExpr). Use a QP solver such as HiGHS.");
|
|
779
|
+
let c = e.objective;
|
|
780
|
+
if (typeof c.constant != "number" || !Number.isFinite(c.constant)) throw Error("Invalid non-finite objective constant.");
|
|
781
|
+
let d = k(c.constant), { terms: f, sumMaxAbs: p } = s.lowerer.lowerLinExpr(c, { kind: "objective" }, "objective expression", !0);
|
|
782
|
+
if (p > E) throw Error(`Objective expression magnitude (${p}) exceeds Pumpkin safe 32-bit integer range [-${E}, ${E}].`);
|
|
783
|
+
if (n.signal?.aborted) return {
|
|
1683
784
|
status: "Unknown",
|
|
1684
785
|
terminationStatus: "Interrupted",
|
|
1685
786
|
hasPrimalSolution: !1,
|
|
@@ -1687,7 +788,7 @@ var Q = class e {
|
|
|
1687
788
|
values: {},
|
|
1688
789
|
runTime: 0
|
|
1689
790
|
};
|
|
1690
|
-
if (
|
|
791
|
+
if (s.hasEmptyDomain) return {
|
|
1691
792
|
status: "Infeasible",
|
|
1692
793
|
terminationStatus: "Infeasible",
|
|
1693
794
|
hasPrimalSolution: !1,
|
|
@@ -1695,29 +796,29 @@ var Q = class e {
|
|
|
1695
796
|
values: {},
|
|
1696
797
|
runTime: 1e-6
|
|
1697
798
|
};
|
|
1698
|
-
let
|
|
1699
|
-
vars:
|
|
1700
|
-
boolVars:
|
|
1701
|
-
constraints:
|
|
1702
|
-
logicalConstraints:
|
|
1703
|
-
auxPropagators:
|
|
1704
|
-
sense:
|
|
1705
|
-
objectiveTerms:
|
|
1706
|
-
objectiveConstant:
|
|
1707
|
-
timeLimitMs:
|
|
1708
|
-
}),
|
|
1709
|
-
|
|
1710
|
-
} : void 0,
|
|
1711
|
-
if (
|
|
1712
|
-
requestJson:
|
|
1713
|
-
wasmUrl:
|
|
799
|
+
let m = JSON.stringify({
|
|
800
|
+
vars: s.vars,
|
|
801
|
+
boolVars: s.boolVars,
|
|
802
|
+
constraints: s.constraints,
|
|
803
|
+
logicalConstraints: s.logicalConstraints,
|
|
804
|
+
auxPropagators: s.auxPropagators,
|
|
805
|
+
sense: a,
|
|
806
|
+
objectiveTerms: f,
|
|
807
|
+
objectiveConstant: d,
|
|
808
|
+
timeLimitMs: i
|
|
809
|
+
}), h = n.useWorker ?? u(), g = n.onProgress, _ = 0, v = g ? (e) => {
|
|
810
|
+
_++, g(e);
|
|
811
|
+
} : void 0, y;
|
|
812
|
+
if (h) y = await S().solve({
|
|
813
|
+
requestJson: m,
|
|
814
|
+
wasmUrl: r
|
|
1714
815
|
}, {
|
|
1715
|
-
signal:
|
|
1716
|
-
onProgress:
|
|
816
|
+
signal: n.signal,
|
|
817
|
+
onProgress: v
|
|
1717
818
|
});
|
|
1718
819
|
else {
|
|
1719
|
-
let e = await
|
|
1720
|
-
if (
|
|
820
|
+
let e = await l(r);
|
|
821
|
+
if (n.signal?.aborted) return {
|
|
1721
822
|
status: "Unknown",
|
|
1722
823
|
terminationStatus: "Interrupted",
|
|
1723
824
|
hasPrimalSolution: !1,
|
|
@@ -1725,43 +826,43 @@ var Q = class e {
|
|
|
1725
826
|
values: {},
|
|
1726
827
|
runTime: 0
|
|
1727
828
|
};
|
|
1728
|
-
let t = performance.now(),
|
|
829
|
+
let t = performance.now(), i = null, a = n.signal, o = v ? (e) => {
|
|
1729
830
|
try {
|
|
1730
831
|
let n = JSON.parse(e), r = Math.max((performance.now() - t) / 1e3, 1e-6);
|
|
1731
|
-
return
|
|
832
|
+
return v({
|
|
1732
833
|
elapsedTime: r,
|
|
1733
834
|
elapsedSeconds: r,
|
|
1734
835
|
objectiveValue: n.objectiveValue
|
|
1735
|
-
}), !!
|
|
836
|
+
}), !!a?.aborted;
|
|
1736
837
|
} catch (e) {
|
|
1737
|
-
return
|
|
838
|
+
return i = e, !0;
|
|
1738
839
|
}
|
|
1739
|
-
} : null,
|
|
1740
|
-
if (
|
|
840
|
+
} : null, s = a ? () => !!a.aborted : null, c = e.solve_with_callback(m, o, s);
|
|
841
|
+
if (i !== null) throw i;
|
|
1741
842
|
let u = Math.max((performance.now() - t) / 1e3, 1e-6);
|
|
1742
|
-
if (
|
|
843
|
+
if (y = JSON.parse(c), y.runTime = u, y.error) throw Error(y.error);
|
|
1743
844
|
}
|
|
1744
|
-
let
|
|
1745
|
-
|
|
1746
|
-
elapsedTime:
|
|
1747
|
-
elapsedSeconds:
|
|
1748
|
-
...
|
|
845
|
+
let b = y.status === "Optimal" || y.status === "Feasible", x = typeof y.objectiveValue == "number" ? k(y.objectiveValue) : null, C = y.runTime ?? 0;
|
|
846
|
+
b && _ === 0 && v && v({
|
|
847
|
+
elapsedTime: C,
|
|
848
|
+
elapsedSeconds: C,
|
|
849
|
+
...x === null ? {} : { objectiveValue: x }
|
|
1749
850
|
});
|
|
1750
|
-
let
|
|
851
|
+
let w = b ? z(y.values, y.boolValues, !0) : { values: {} };
|
|
1751
852
|
return {
|
|
1752
|
-
status:
|
|
1753
|
-
terminationStatus:
|
|
1754
|
-
hasPrimalSolution:
|
|
1755
|
-
objectiveValue:
|
|
1756
|
-
values:
|
|
1757
|
-
...
|
|
1758
|
-
runTime:
|
|
853
|
+
status: y.status,
|
|
854
|
+
terminationStatus: y.terminationStatus,
|
|
855
|
+
hasPrimalSolution: b,
|
|
856
|
+
objectiveValue: x,
|
|
857
|
+
values: w.values,
|
|
858
|
+
...w.boolValues === void 0 ? {} : { boolValues: w.boolValues },
|
|
859
|
+
runTime: C
|
|
1759
860
|
};
|
|
1760
861
|
}
|
|
1761
862
|
};
|
|
1762
|
-
function
|
|
1763
|
-
|
|
863
|
+
function K(e) {
|
|
864
|
+
a("pumpkin", (t) => new G(t ?? e));
|
|
1764
865
|
}
|
|
1765
|
-
|
|
866
|
+
K();
|
|
1766
867
|
//#endregion
|
|
1767
|
-
export {
|
|
868
|
+
export { G as PumpkinSolver, b as createPumpkinWorkerClient, C as disposeSharedPumpkinWorkerClient, h as getPumpkinWasmUrl, S as getSharedPumpkinWorkerClient, d as isPumpkinRuntimeCached, w as isPumpkinWarmed, p as isValidWasmHeader, l as loadPumpkinRuntime, K as registerPumpkinSolver, s as registerPumpkinWorkerResetHook, f as resetPumpkinLoader, m as resolvePumpkinWasmUrl, c as setPumpkinWasmUrl, u as shouldUseWorker, T as warmupPumpkin };
|