@ohtaman/washi 0.6.0 → 0.7.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 +103 -421
- package/README.md +103 -421
- package/README.zh-CN.md +103 -421
- package/THIRD_PARTY_LICENSES.md +5 -3
- package/dist/browser/solvers/glpk.js +2 -2
- package/dist/browser/solvers/glpk.min.js +1 -1
- package/dist/browser/washi.js +777 -261
- package/dist/browser/washi.min.js +3 -3
- package/dist/lib/{registry-C20sxTwm.js → registry-D7TZ3Wh_.js} +88 -18
- package/dist/lib/solvers/glpk.js +3 -3
- package/dist/lib/solvers/z3.js +1 -1
- package/dist/lib/washi.js +864 -421
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/modeler/expression.d.ts +47 -26
- package/dist/types/modeler/expression.d.ts.map +1 -1
- package/dist/types/modeler/index.d.ts +2 -2
- package/dist/types/modeler/index.d.ts.map +1 -1
- package/dist/types/modeler/model.d.ts +12 -6
- package/dist/types/modeler/model.d.ts.map +1 -1
- package/dist/types/modeler/parser.d.ts +25 -5
- package/dist/types/modeler/parser.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.map +1 -1
- package/dist/types/modeler/solvers/highs/worker-client.d.ts.map +1 -1
- package/dist/types/modeler/solvers/types.d.ts +2 -0
- package/dist/types/modeler/solvers/types.d.ts.map +1 -1
- package/docs/examples/diet-sensitivity.md +15 -14
- package/docs/examples/ecommerce-coupons.md +15 -22
- package/docs/examples/index.md +14 -12
- package/docs/examples/knapsack.md +8 -7
- package/docs/examples/portfolio-qp.md +11 -4
- package/docs/examples/production.md +4 -4
- package/docs/examples/shift-scheduling.md +20 -7
- package/docs/examples/smt-security.md +16 -4
- package/docs/examples/transportation.md +46 -19
- package/docs/examples/traveling-salesperson.md +28 -44
- package/docs/examples/unsat-core.md +15 -5
- package/docs/examples/vehicle-routing.md +10 -8
- package/docs/getting-started/ai-agents.md +25 -14
- package/docs/getting-started/cross-origin.md +56 -28
- package/docs/getting-started/index.md +40 -69
- package/docs/getting-started/runtimes.md +66 -61
- package/docs/guide/constraints.md +46 -40
- package/docs/guide/decomposition.md +127 -259
- package/docs/guide/expressions.md +90 -39
- package/docs/guide/formulation-tips.md +212 -0
- package/docs/guide/objectives.md +7 -4
- package/docs/guide/unsat-core.md +18 -6
- package/docs/guide/variables.md +33 -38
- package/docs/ja/getting-started/ai-agents.md +24 -14
- package/docs/solvers/highs.md +27 -6
- package/docs/solvers/index.md +1 -1
- package/docs/solvers/z3.md +3 -0
- package/docs/zh/getting-started/ai-agents.md +24 -14
- package/llms-full.txt +929 -687
- package/llms.txt +21 -19
- package/package.json +3 -3
- package/skills/washi/SKILL.md +14 -10
package/dist/lib/washi.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import { A as e, C as t, D as n, E as r, M as i, N as a, O as o, S as s, T as c, _ as l, a as u, b as d, c as f, d as p, f as m, g as h, h as g, i as _, j as v, k as y, l as b, m as x, n as S, o as C, p as
|
|
2
|
-
import { a as
|
|
3
|
-
import { createHighsWorker as
|
|
1
|
+
import { A as e, C as t, D as n, E as r, M as i, N as a, O as o, S as s, T as c, _ as l, a as u, b as d, c as f, d as p, f as m, g as h, h as g, i as _, j as v, k as y, l as b, m as x, n as S, o as C, p as ee, r as w, s as T, t as E, u as D, v as O, w as k, x as A, y as te } from "./registry-D7TZ3Wh_.js";
|
|
2
|
+
import { a as j, c as M, d as ne, f as re, h as ie, i as ae, l as oe, m as se, n as ce, o as N, p as le, r as P, s as F, t as ue, u as de } from "./loader-BZUQNvh4.js";
|
|
3
|
+
import { createHighsWorker as fe } from "./highs-worker-factory.js";
|
|
4
4
|
//#region \0rolldown/runtime.js
|
|
5
|
-
var
|
|
5
|
+
var pe = Object.defineProperty, me = Object.getOwnPropertyDescriptor, he = Object.getOwnPropertyNames, ge = Object.prototype.hasOwnProperty, _e = (e, t) => {
|
|
6
6
|
let n = {};
|
|
7
|
-
for (var r in e)
|
|
7
|
+
for (var r in e) pe(n, r, {
|
|
8
8
|
get: e[r],
|
|
9
9
|
enumerable: !0
|
|
10
10
|
});
|
|
11
|
-
return t ||
|
|
12
|
-
},
|
|
13
|
-
if (t && typeof t == "object" || typeof t == "function") for (var i =
|
|
11
|
+
return t || pe(n, Symbol.toStringTag, { value: "Module" }), n;
|
|
12
|
+
}, ve = (e, t, n, r) => {
|
|
13
|
+
if (t && typeof t == "object" || typeof t == "function") for (var i = he(t), a = 0, o = i.length, s; a < o; a++) s = i[a], !ge.call(e, s) && s !== n && pe(e, s, {
|
|
14
14
|
get: ((e) => t[e]).bind(null, s),
|
|
15
|
-
enumerable: !(r =
|
|
15
|
+
enumerable: !(r = me(t, s)) || r.enumerable
|
|
16
16
|
});
|
|
17
17
|
return e;
|
|
18
|
-
},
|
|
18
|
+
}, I = (e, t, n) => (ve(e, t, "default"), n && ve(n, t, "default"));
|
|
19
19
|
//#endregion
|
|
20
20
|
//#region src/modeler/types.ts
|
|
21
|
-
function
|
|
21
|
+
function ye(e) {
|
|
22
22
|
if (e === void 0) return "Minimize";
|
|
23
23
|
if (typeof e == "string") {
|
|
24
24
|
let t = e.toLowerCase();
|
|
@@ -29,8 +29,11 @@ function _e(e) {
|
|
|
29
29
|
}
|
|
30
30
|
//#endregion
|
|
31
31
|
//#region src/modeler/parser.ts
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
function L(e) {
|
|
33
|
+
return typeof e == "object" && !!e && e.kind === "container";
|
|
34
|
+
}
|
|
35
|
+
var be = /^(?:[0-9]+(?:\.[0-9]+)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?$/;
|
|
36
|
+
function xe(e, t) {
|
|
34
37
|
let n = [], r = 0, i = e.length;
|
|
35
38
|
for (; r < i;) {
|
|
36
39
|
let a = e[r];
|
|
@@ -69,6 +72,10 @@ function ye(e, t) {
|
|
|
69
72
|
type: "RPAREN",
|
|
70
73
|
value: ")"
|
|
71
74
|
}), r++;
|
|
75
|
+
else if (a === ",") n.push({
|
|
76
|
+
type: "COMMA",
|
|
77
|
+
value: ","
|
|
78
|
+
}), r++;
|
|
72
79
|
else if (a === "<" || a === ">" || a === "=") {
|
|
73
80
|
let t = a;
|
|
74
81
|
r + 1 < i && (e[r + 1] === "=" || e[r + 1] === "<" || e[r + 1] === ">") ? (t += e[r + 1], r += 2) : r++, n.push({
|
|
@@ -86,7 +93,7 @@ function ye(e, t) {
|
|
|
86
93
|
for (; r < i && (e[r] === "." || e[r] === "e" || e[r] === "E" || /[0-9]/.test(e[r]));) a += e[r++];
|
|
87
94
|
}
|
|
88
95
|
} else for (; r < i && (e[r] === "." || /[0-9]/.test(e[r]));) a += e[r++];
|
|
89
|
-
if (!
|
|
96
|
+
if (!be.test(a)) throw Error(`Invalid numeric literal '${a}' at position ${t} in: "${e}"`);
|
|
90
97
|
let o = Number(a);
|
|
91
98
|
if (!Number.isFinite(o)) throw Error(`Numeric literal '${a}' overflows finite floating-point range in: "${e}"`);
|
|
92
99
|
if (o === 0 && !/^[0.]+(?:[eE][+-]?\d+)?$/.test(a)) throw Error(`Numeric literal '${a}' underflows to zero at position ${t} in: "${e}"`);
|
|
@@ -113,12 +120,205 @@ function ye(e, t) {
|
|
|
113
120
|
}
|
|
114
121
|
return n;
|
|
115
122
|
}
|
|
116
|
-
|
|
123
|
+
function Se(e) {
|
|
124
|
+
return e instanceof k && e.quadTerms.size === 0 ? new A(e.linearTerms, e.constant) : e;
|
|
125
|
+
}
|
|
126
|
+
function Ce(e) {
|
|
127
|
+
return typeof e == "number" || e instanceof b || e instanceof A || e instanceof k || H(e);
|
|
128
|
+
}
|
|
129
|
+
function we(e) {
|
|
130
|
+
return typeof e == "number" ? new A(/* @__PURE__ */ new Map(), e) : e instanceof b || "id" in e && !("terms" in e) && !("quadTerms" in e) ? new A(/* @__PURE__ */ new Map([[e, 1]]), 0) : e.clone();
|
|
131
|
+
}
|
|
132
|
+
function Te(e, t, n) {
|
|
133
|
+
if (typeof e == "number" && typeof t == "number") {
|
|
134
|
+
if (n === "+") {
|
|
135
|
+
let n = e + t;
|
|
136
|
+
if (!Number.isFinite(n)) throw Error(`Invalid non-finite constant "${n}" in expression.`);
|
|
137
|
+
return n;
|
|
138
|
+
}
|
|
139
|
+
if (n === "-") {
|
|
140
|
+
let n = e - t;
|
|
141
|
+
if (!Number.isFinite(n)) throw Error(`Invalid non-finite constant "${n}" in expression.`);
|
|
142
|
+
return n;
|
|
143
|
+
}
|
|
144
|
+
if (n === "*") {
|
|
145
|
+
let n = e * t;
|
|
146
|
+
if (e !== 0 && t !== 0 && n === 0) throw Error("Constant multiplication underflows to zero.");
|
|
147
|
+
if (!Number.isFinite(n)) throw Error(`Invalid non-finite constant "${n}" in expression.`);
|
|
148
|
+
return n;
|
|
149
|
+
}
|
|
150
|
+
if (t === 0) throw Error("Division by zero");
|
|
151
|
+
let r = 1 / t * e;
|
|
152
|
+
if (e !== 0 && r === 0) throw Error("Constant multiplication underflows to zero.");
|
|
153
|
+
if (!Number.isFinite(r)) throw Error(`Invalid non-finite constant "${r}" in expression.`);
|
|
154
|
+
return r;
|
|
155
|
+
}
|
|
156
|
+
let r = we(e), i = we(t), a;
|
|
157
|
+
a = n === "+" ? r.add(i) : n === "-" ? r.sub(i) : n === "*" ? r.mul(i) : r.div(i);
|
|
158
|
+
let o = Se(a);
|
|
159
|
+
return U(o), o;
|
|
160
|
+
}
|
|
161
|
+
function R(e, t) {
|
|
162
|
+
if (Array.isArray(e)) {
|
|
163
|
+
let n = Array(e.length);
|
|
164
|
+
for (let r = 0; r < e.length; r++) {
|
|
165
|
+
let i = e[r];
|
|
166
|
+
n[r] = Ce(i) ? t(i) : R(i, t);
|
|
167
|
+
}
|
|
168
|
+
return n;
|
|
169
|
+
}
|
|
170
|
+
let n = Object.create(null);
|
|
171
|
+
for (let r of Object.keys(e)) {
|
|
172
|
+
let i = e[r];
|
|
173
|
+
n[r] = Ce(i) ? t(i) : R(i, t);
|
|
174
|
+
}
|
|
175
|
+
return n;
|
|
176
|
+
}
|
|
177
|
+
function Ee(e, t, n, r = "") {
|
|
178
|
+
let i = Ce(e), a = Ce(t);
|
|
179
|
+
if (i && a) return Te(e, t, n);
|
|
180
|
+
if (i || a) throw Error(`Dimension mismatch in template expression at '${r || "root"}': cannot combine scalar element with sub-container.`);
|
|
181
|
+
if (Array.isArray(e) || Array.isArray(t)) {
|
|
182
|
+
if (Array.isArray(e) && Array.isArray(t)) {
|
|
183
|
+
if (e.length !== t.length) throw Error(`Array length mismatch in template expression at '${r || "root"}': received lengths ${e.length} and ${t.length}.`);
|
|
184
|
+
let i = Array(e.length);
|
|
185
|
+
for (let a = 0; a < e.length; a++) i[a] = Ee(e[a], t[a], n, r ? `${r}[${a}]` : `[${a}]`);
|
|
186
|
+
return i;
|
|
187
|
+
}
|
|
188
|
+
let i = Array.isArray(e) ? e : t, a = Array.isArray(e) ? t : e, o = Object.keys(a);
|
|
189
|
+
if (o.length === i.length && o.every((e) => /^(?:0|[1-9]\d*)$/.test(e))) {
|
|
190
|
+
let o = [];
|
|
191
|
+
for (let e = 0; e < i.length; e++) Object.prototype.hasOwnProperty.call(a, e) || o.push(e);
|
|
192
|
+
if (o.length > 0) throw Error(`Key mismatch in template expression at '${r || "root"}': missing indices [${o.join(", ")}] in numeric record.`);
|
|
193
|
+
let s = Array(i.length);
|
|
194
|
+
for (let a = 0; a < i.length; a++) {
|
|
195
|
+
let i = e[a], o = t[a];
|
|
196
|
+
s[a] = Ee(i, o, n, r ? `${r}[${a}]` : `[${a}]`);
|
|
197
|
+
}
|
|
198
|
+
return s;
|
|
199
|
+
}
|
|
200
|
+
throw o.length > 0 && o.every((e) => /^(?:0|[1-9]\d*)$/.test(e)) ? Error(`Array length mismatch in template expression at '${r || "root"}': array has length ${i.length}, but object has ${o.length} indices.`) : Error(`Type mismatch in template expression at '${r || "root"}': one operand is an Array while the other is an Object with keys [${o.slice(0, 5).join(", ")}].`);
|
|
201
|
+
}
|
|
202
|
+
let o = e, s = t, c = Object.keys(o), l = Object.keys(s), u = c.filter((e) => !Object.prototype.hasOwnProperty.call(s, e)), d = l.filter((e) => !Object.prototype.hasOwnProperty.call(o, e));
|
|
203
|
+
if (u.length > 0 || d.length > 0) {
|
|
204
|
+
let e = [];
|
|
205
|
+
throw u.length > 0 && e.push(`missing in second operand: [${u.join(", ")}]`), d.length > 0 && e.push(`missing in first operand: [${d.join(", ")}]`), Error(`Key mismatch in template expression at '${r || "root"}': ${e.join("; ")}.`);
|
|
206
|
+
}
|
|
207
|
+
let f = Object.create(null);
|
|
208
|
+
for (let e of c) f[e] = Ee(o[e], s[e], n, r ? `${r}.${e}` : e);
|
|
209
|
+
return f;
|
|
210
|
+
}
|
|
211
|
+
function z(e, t, n) {
|
|
212
|
+
let r = L(e), i = L(t);
|
|
213
|
+
if (!r && !i) return Te(e, t, n);
|
|
214
|
+
if (r && !i) return {
|
|
215
|
+
kind: "container",
|
|
216
|
+
data: R(e.data, (e) => Te(e, t, n)),
|
|
217
|
+
fromNonArrayIterable: e.fromNonArrayIterable
|
|
218
|
+
};
|
|
219
|
+
if (!r && i) return {
|
|
220
|
+
kind: "container",
|
|
221
|
+
data: R(t.data, (t) => Te(e, t, n)),
|
|
222
|
+
fromNonArrayIterable: t.fromNonArrayIterable
|
|
223
|
+
};
|
|
224
|
+
let a = e, o = t;
|
|
225
|
+
return {
|
|
226
|
+
kind: "container",
|
|
227
|
+
data: Ee(a.data, o.data, n),
|
|
228
|
+
fromNonArrayIterable: a.fromNonArrayIterable || o.fromNonArrayIterable
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
function De(e) {
|
|
232
|
+
return typeof e == "number" ? -e : L(e) ? {
|
|
233
|
+
kind: "container",
|
|
234
|
+
data: R(e.data, (e) => typeof e == "number" ? -e : we(e).neg()),
|
|
235
|
+
fromNonArrayIterable: e.fromNonArrayIterable
|
|
236
|
+
} : e.neg();
|
|
237
|
+
}
|
|
238
|
+
function Oe(e, t) {
|
|
239
|
+
let n = e instanceof k, r = n ? e.quadTerms.size : 0, i = n ? e.linearTerms.size : e.terms.size;
|
|
240
|
+
if (r === 0 && i === 0) {
|
|
241
|
+
let n = e.constant ** +t;
|
|
242
|
+
if (e.constant !== 0 && t > 0 && n === 0) throw Error("Constant exponentiation underflows to zero.");
|
|
243
|
+
return new A(/* @__PURE__ */ new Map(), n);
|
|
244
|
+
}
|
|
245
|
+
if (t === 0) return new A(/* @__PURE__ */ new Map(), 1);
|
|
246
|
+
if (t === 1) return e;
|
|
247
|
+
if (t === 2) return e.mul(e);
|
|
248
|
+
throw Error(`Exponent ${t} creates a non-linear term of degree > 2 (not allowed in MILP/LP/QP)`);
|
|
249
|
+
}
|
|
250
|
+
function B(e) {
|
|
251
|
+
if (typeof e != "object" || !e || Array.isArray(e) || H(e) || W(e)) return !1;
|
|
252
|
+
let t = Object.getPrototypeOf(e);
|
|
253
|
+
return t === Object.prototype || t === null;
|
|
254
|
+
}
|
|
255
|
+
function ke(e) {
|
|
256
|
+
if (typeof e == "number") {
|
|
257
|
+
if (!Number.isFinite(e)) throw Error(`Invalid numeric value "${e}" in template expression: interpolated numbers must be finite.`);
|
|
258
|
+
return e;
|
|
259
|
+
}
|
|
260
|
+
if (W(e)) throw Error("Cannot interpolate a constraint comparison (<=, >=, ==) inside a template expression: expected a finite number, Var, LinExpr, or QuadExpr.");
|
|
261
|
+
if (H(e)) return U(e), e instanceof b || "id" in e && !("terms" in e) && !("quadTerms" in e) ? e : e.clone();
|
|
262
|
+
throw Error("Unsupported interpolation value in template expression: expected a finite number, Var, LinExpr, or QuadExpr.");
|
|
263
|
+
}
|
|
264
|
+
function V(e) {
|
|
265
|
+
return typeof e == "object" && !!e && !(e instanceof String) && !(e instanceof Map) && !H(e) && !W(e) && Symbol.iterator in e && typeof e[Symbol.iterator] == "function";
|
|
266
|
+
}
|
|
267
|
+
function Ae(e, t = 0) {
|
|
268
|
+
if (Array.isArray(e)) {
|
|
269
|
+
let n = !1, r = Array(e.length);
|
|
270
|
+
for (let i = 0; i < e.length; i++) {
|
|
271
|
+
if (!(i in e)) throw Error(`Unsupported interpolation value in template expression: sparse array hole at index ${i}.`);
|
|
272
|
+
let a = e[i];
|
|
273
|
+
if (Array.isArray(a) || B(a) || V(a)) {
|
|
274
|
+
if (t >= 1) throw Error("Nested containers deeper than 2D are not supported in template expressions.");
|
|
275
|
+
let e = Ae(a, t + 1);
|
|
276
|
+
e.fromNonArrayIterable && (n = !0), r[i] = e.data;
|
|
277
|
+
} else r[i] = ke(a);
|
|
278
|
+
}
|
|
279
|
+
return {
|
|
280
|
+
kind: "container",
|
|
281
|
+
data: r,
|
|
282
|
+
fromNonArrayIterable: n
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
if (V(e)) return {
|
|
286
|
+
kind: "container",
|
|
287
|
+
data: Array.from(e).map((e) => {
|
|
288
|
+
if (Array.isArray(e) || B(e) || V(e)) {
|
|
289
|
+
if (t >= 1) throw Error("Nested containers deeper than 2D are not supported in template expressions.");
|
|
290
|
+
return Ae(e, t + 1).data;
|
|
291
|
+
}
|
|
292
|
+
return ke(e);
|
|
293
|
+
}),
|
|
294
|
+
fromNonArrayIterable: !0
|
|
295
|
+
};
|
|
296
|
+
if (B(e)) {
|
|
297
|
+
if (Object.getOwnPropertySymbols(e).length > 0) throw Error("Symbol keys are not supported in template expression at 'root'.");
|
|
298
|
+
let n = !1, r = Object.create(null);
|
|
299
|
+
for (let i of Object.keys(e)) {
|
|
300
|
+
let a = e[i];
|
|
301
|
+
if (Array.isArray(a) || B(a) || V(a)) {
|
|
302
|
+
if (t >= 1) throw Error("Nested containers deeper than 2D are not supported in template expressions.");
|
|
303
|
+
let e = Ae(a, t + 1);
|
|
304
|
+
e.fromNonArrayIterable && (n = !0), r[i] = e.data;
|
|
305
|
+
} else r[i] = ke(a);
|
|
306
|
+
}
|
|
307
|
+
return {
|
|
308
|
+
kind: "container",
|
|
309
|
+
data: r,
|
|
310
|
+
fromNonArrayIterable: n
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
throw Error("Unsupported interpolation value in template expression: expected a finite number, Var, LinExpr, or QuadExpr.");
|
|
314
|
+
}
|
|
315
|
+
var je = class {
|
|
117
316
|
tokens;
|
|
118
317
|
pos = 0;
|
|
119
318
|
resolver;
|
|
120
|
-
|
|
121
|
-
|
|
319
|
+
templateCtx;
|
|
320
|
+
constructor(e, t, n) {
|
|
321
|
+
this.tokens = e, this.resolver = t, this.templateCtx = n;
|
|
122
322
|
}
|
|
123
323
|
peek() {
|
|
124
324
|
return this.tokens[this.pos];
|
|
@@ -130,54 +330,240 @@ var be = class {
|
|
|
130
330
|
return t;
|
|
131
331
|
}
|
|
132
332
|
parseExpression() {
|
|
133
|
-
let e = this.
|
|
333
|
+
let e = this.parseExpressionNode();
|
|
334
|
+
if (L(e)) throw Error("Container expression (Array or Record) cannot be used directly as a scalar expression or constraint; wrap it in sum(...) or use dot(a, b).");
|
|
335
|
+
return typeof e == "number" ? new A(/* @__PURE__ */ new Map(), e) : Se(e);
|
|
336
|
+
}
|
|
337
|
+
parseExpressionNode() {
|
|
338
|
+
let e = this.parseTermNode();
|
|
134
339
|
for (; this.pos < this.tokens.length;) {
|
|
135
340
|
let t = this.peek();
|
|
136
341
|
if (!t) break;
|
|
137
|
-
if (t.type === "PLUS") this.consume(), e = e
|
|
138
|
-
else if (t.type === "MINUS") this.consume(), e = e
|
|
342
|
+
if (t.type === "PLUS") this.consume(), e = z(e, this.parseTermNode(), "+");
|
|
343
|
+
else if (t.type === "MINUS") this.consume(), e = z(e, this.parseTermNode(), "-");
|
|
139
344
|
else break;
|
|
140
345
|
}
|
|
141
|
-
return e
|
|
346
|
+
return L(e) || typeof e == "number" ? e : Se(e);
|
|
142
347
|
}
|
|
143
|
-
|
|
144
|
-
let e = this.
|
|
348
|
+
parseTermNode() {
|
|
349
|
+
let e = this.parseFactorNode();
|
|
145
350
|
for (; this.pos < this.tokens.length;) {
|
|
146
351
|
let t = this.peek();
|
|
147
352
|
if (!t) break;
|
|
148
|
-
if (t.type === "STAR") this.consume(), e = e
|
|
149
|
-
else if (t.type === "SLASH") this.consume(), e = e
|
|
353
|
+
if (t.type === "STAR") this.consume(), e = z(e, this.parseFactorNode(), "*");
|
|
354
|
+
else if (t.type === "SLASH") this.consume(), e = z(e, this.parseFactorNode(), "/");
|
|
150
355
|
else break;
|
|
151
356
|
}
|
|
152
357
|
return e;
|
|
153
358
|
}
|
|
154
|
-
|
|
359
|
+
parseFactorNode() {
|
|
155
360
|
let e = this.peek();
|
|
156
361
|
if (!e) throw Error("Unexpected end of expression");
|
|
157
|
-
if (e.type === "PLUS") return this.consume(), this.
|
|
158
|
-
if (e.type === "MINUS") return this.consume(), this.
|
|
362
|
+
if (e.type === "PLUS") return this.consume(), this.parseFactorNode();
|
|
363
|
+
if (e.type === "MINUS") return this.consume(), De(this.parseFactorNode());
|
|
159
364
|
let t;
|
|
160
365
|
if (e.type === "NUMBER") {
|
|
161
366
|
this.consume();
|
|
162
367
|
let n = e.numericValue ?? Number(e.value);
|
|
163
368
|
if (this.peek()?.type === "IDENT" || this.peek()?.type === "LPAREN") {
|
|
164
|
-
let e;
|
|
165
|
-
|
|
166
|
-
let t = this.consume(), n = this.resolve(t.value);
|
|
167
|
-
e = n instanceof b || "id" in n && !("terms" in n) && !("quadTerms" in n) ? new j(/* @__PURE__ */ new Map([[n, 1]]), 0) : n.clone();
|
|
168
|
-
} else this.consume("LPAREN"), e = this.parseExpression(), this.consume("RPAREN");
|
|
169
|
-
return this.applyPower(e).mul(n);
|
|
369
|
+
let e = this.parsePrimaryNode();
|
|
370
|
+
return z(this.applyPowerNode(e), n, "*");
|
|
170
371
|
}
|
|
171
|
-
t =
|
|
172
|
-
} else if (e.type === "IDENT")
|
|
173
|
-
this.consume();
|
|
174
|
-
let n = this.resolve(e.value);
|
|
175
|
-
t = n instanceof b || "id" in n && !("terms" in n) && !("quadTerms" in n) ? new j(/* @__PURE__ */ new Map([[n, 1]]), 0) : n.clone();
|
|
176
|
-
} else if (e.type === "LPAREN") this.consume("LPAREN"), t = this.parseExpression(), this.consume("RPAREN");
|
|
372
|
+
t = n;
|
|
373
|
+
} else if (e.type === "IDENT" || e.type === "LPAREN") t = this.parsePrimaryNode();
|
|
177
374
|
else throw Error(`Unexpected token '${e.value}' in expression`);
|
|
178
|
-
return this.
|
|
375
|
+
return this.applyPowerNode(t);
|
|
376
|
+
}
|
|
377
|
+
parsePrimaryNode() {
|
|
378
|
+
if (this.peek()?.type === "IDENT") {
|
|
379
|
+
let e = this.consume();
|
|
380
|
+
return (e.value === "sum" || e.value === "dot") && this.peek()?.type === "LPAREN" ? this.parseFunctionCall(e.value) : this.resolveNode(e.value);
|
|
381
|
+
}
|
|
382
|
+
this.consume("LPAREN");
|
|
383
|
+
let e = this.parseExpressionNode();
|
|
384
|
+
return this.consume("RPAREN"), e;
|
|
385
|
+
}
|
|
386
|
+
parseFunctionCall(e) {
|
|
387
|
+
this.consume("LPAREN");
|
|
388
|
+
let t = 1;
|
|
389
|
+
for (let n = this.pos; n < this.tokens.length && t > 0; n++) {
|
|
390
|
+
let r = this.tokens[n];
|
|
391
|
+
if (r.type === "LPAREN") t++;
|
|
392
|
+
else if (r.type === "RPAREN") t--;
|
|
393
|
+
else if (r.type === "COMP") throw Error(e === "sum" ? "Cannot sum a constraint comparison (<=, >=, ==) in sum(): expected a number, Var, LinExpr, or QuadExpr." : "Cannot use a constraint comparison (<=, >=, ==) in dot(): expected 1D or 2D Array or Record containers.");
|
|
394
|
+
}
|
|
395
|
+
if (this.peek()?.type === "RPAREN") throw this.consume("RPAREN"), Error(e === "sum" ? "sum() in template expression requires at least 1 argument." : "dot() in template expression requires exactly 2 arguments: dot(a, b).");
|
|
396
|
+
if (e === "sum") {
|
|
397
|
+
if (this.tokens[this.pos]?.type === "IDENT" && this.templateCtx?.rawContainerMap.has(this.tokens[this.pos].value) && this.tokens[this.pos + 1]?.type === "RPAREN") {
|
|
398
|
+
let e = this.consume();
|
|
399
|
+
this.consume("RPAREN");
|
|
400
|
+
let t = this.templateCtx.rawContainerMap.get(e.value);
|
|
401
|
+
return this.accumulateSumItems((e) => {
|
|
402
|
+
e(t, 0);
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
let e = this.pos, t = 0;
|
|
406
|
+
for (; this.tokens[e]?.type === "LPAREN";) t++, e++;
|
|
407
|
+
if (this.tokens[e]?.type === "IDENT" && this.templateCtx?.rawContainerMap.has(this.tokens[e].value)) {
|
|
408
|
+
let n = this.tokens[e].value, r = e + 1, i = 0;
|
|
409
|
+
for (; i < t && this.tokens[r]?.type === "RPAREN";) i++, r++;
|
|
410
|
+
if (i === t && this.tokens[r]?.type === "COMMA") {
|
|
411
|
+
this.pos = r + 1;
|
|
412
|
+
let e = this.peek();
|
|
413
|
+
if (!e || e.type !== "IDENT" || !this.templateCtx.functionMap.has(e.value)) throw Error("Second argument to sum() must be a mapping function: sum(itemsOrCount, mapFn).");
|
|
414
|
+
if (this.consume(), this.peek()?.type === "COMMA") throw Error("sum() in template expression accepts at most 2 arguments: sum(container) or sum(itemsOrCount, mapFn).");
|
|
415
|
+
return this.consume("RPAREN"), this.evalSumWithMapper(this.templateCtx.rawContainerMap.get(n), this.templateCtx.functionMap.get(e.value));
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
let n = this.parseExpressionNode();
|
|
419
|
+
if (this.peek()?.type === "COMMA") {
|
|
420
|
+
this.consume("COMMA");
|
|
421
|
+
let e = this.peek();
|
|
422
|
+
if (!e || e.type !== "IDENT" || !this.templateCtx?.functionMap.has(e.value)) throw Error("Second argument to sum() must be a mapping function: sum(itemsOrCount, mapFn).");
|
|
423
|
+
if (this.consume(), this.peek()?.type === "COMMA") throw Error("sum() in template expression accepts at most 2 arguments: sum(container) or sum(itemsOrCount, mapFn).");
|
|
424
|
+
this.consume("RPAREN");
|
|
425
|
+
let t = this.templateCtx.functionMap.get(e.value);
|
|
426
|
+
if (typeof n == "number") return this.evalSumWithMapper(n, t);
|
|
427
|
+
if (!L(n) && n instanceof A && n.terms.size === 0) return this.evalSumWithMapper(n.constant, t);
|
|
428
|
+
if (L(n)) return this.evalSumWithMapper(n.data, t);
|
|
429
|
+
throw Error("Invalid first argument in sum(itemsOrCount, mapFn): expected an Iterable, Record, or non-negative integer count.");
|
|
430
|
+
}
|
|
431
|
+
return this.consume("RPAREN"), this.evalSumSingle(n);
|
|
432
|
+
}
|
|
433
|
+
let n = this.parseExpressionNode();
|
|
434
|
+
if (this.peek()?.type !== "COMMA") throw Error("dot() in template expression requires exactly 2 arguments: dot(a, b).");
|
|
435
|
+
this.consume("COMMA");
|
|
436
|
+
let r = this.parseExpressionNode();
|
|
437
|
+
if (this.peek()?.type === "COMMA") throw Error("dot() in template expression requires exactly 2 arguments: dot(a, b).");
|
|
438
|
+
return this.consume("RPAREN"), this.evalDot(n, r);
|
|
439
|
+
}
|
|
440
|
+
accumulateSumItems(e) {
|
|
441
|
+
let t = /* @__PURE__ */ new Map(), n = /* @__PURE__ */ new Map(), r = 0, i = (e, a = 0) => {
|
|
442
|
+
if (e != null) {
|
|
443
|
+
if (typeof e == "number") {
|
|
444
|
+
if (!Number.isFinite(e)) throw Error(`Invalid non-finite number "${e}" in sum().`);
|
|
445
|
+
let t = r + e;
|
|
446
|
+
if (!Number.isFinite(t)) throw Error("Constant accumulation overflows finite floating-point range in sum().");
|
|
447
|
+
r = t;
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
if (W(e)) throw Error("Cannot sum a constraint comparison (<=, >=, ==) in sum(): expected a number, Var, LinExpr, or QuadExpr.");
|
|
451
|
+
if (e instanceof k || typeof e == "object" && e && "quadTerms" in e && e.quadTerms instanceof Map && "linearTerms" in e && e.linearTerms instanceof Map) {
|
|
452
|
+
let i = e;
|
|
453
|
+
U(i);
|
|
454
|
+
let a = r + i.constant;
|
|
455
|
+
if (!Number.isFinite(a)) throw Error("Constant accumulation overflows finite floating-point range in sum().");
|
|
456
|
+
r = a;
|
|
457
|
+
for (let [e, n] of i.linearTerms) {
|
|
458
|
+
let r = (t.get(e) ?? 0) + n;
|
|
459
|
+
if (!Number.isFinite(r)) throw Error("Coefficient accumulation overflows finite floating-point range in sum().");
|
|
460
|
+
t.set(e, r);
|
|
461
|
+
}
|
|
462
|
+
for (let [e, t] of i.quadTerms) {
|
|
463
|
+
let r = n.get(e);
|
|
464
|
+
if (r) {
|
|
465
|
+
let i = r.coeff + t.coeff;
|
|
466
|
+
if (!Number.isFinite(i)) throw Error("Quadratic coefficient accumulation overflows finite floating-point range in sum().");
|
|
467
|
+
n.set(e, {
|
|
468
|
+
var1: t.var1,
|
|
469
|
+
var2: t.var2,
|
|
470
|
+
coeff: i
|
|
471
|
+
});
|
|
472
|
+
} else n.set(e, {
|
|
473
|
+
var1: t.var1,
|
|
474
|
+
var2: t.var2,
|
|
475
|
+
coeff: t.coeff
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
if (e instanceof A || typeof e == "object" && e && "terms" in e && e.terms instanceof Map && typeof e.constant == "number") {
|
|
481
|
+
let n = e;
|
|
482
|
+
U(n);
|
|
483
|
+
let i = r + n.constant;
|
|
484
|
+
if (!Number.isFinite(i)) throw Error("Constant accumulation overflows finite floating-point range in sum().");
|
|
485
|
+
r = i;
|
|
486
|
+
for (let [e, r] of n.terms) {
|
|
487
|
+
let n = (t.get(e) ?? 0) + r;
|
|
488
|
+
if (!Number.isFinite(n)) throw Error("Coefficient accumulation overflows finite floating-point range in sum().");
|
|
489
|
+
t.set(e, n);
|
|
490
|
+
}
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
if (H(e)) {
|
|
494
|
+
let n = e, r = (t.get(n) ?? 0) + 1;
|
|
495
|
+
if (!Number.isFinite(r)) throw Error("Coefficient accumulation overflows finite floating-point range in sum().");
|
|
496
|
+
t.set(n, r);
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
if (Array.isArray(e)) {
|
|
500
|
+
if (a >= 2) throw Error("Nested containers deeper than 2D are not supported in sum().");
|
|
501
|
+
for (let t = 0; t < e.length; t++) {
|
|
502
|
+
if (!(t in e)) throw Error(`Unsupported item in sum(): sparse array hole at index ${t}.`);
|
|
503
|
+
i(e[t], a + 1);
|
|
504
|
+
}
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
if (V(e)) {
|
|
508
|
+
if (a >= 2) throw Error("Nested containers deeper than 2D are not supported in sum().");
|
|
509
|
+
for (let t of e) i(t, a + 1);
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
if (B(e)) {
|
|
513
|
+
if (a >= 2) throw Error("Nested containers deeper than 2D are not supported in sum().");
|
|
514
|
+
if (Object.getOwnPropertySymbols(e).length > 0) throw Error("Symbol keys are not supported in sum().");
|
|
515
|
+
for (let t of Object.keys(e)) i(e[t], a + 1);
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
throw Error("Unsupported item in sum(): expected a number, Var, LinExpr, or QuadExpr.");
|
|
519
|
+
}
|
|
520
|
+
};
|
|
521
|
+
e(i);
|
|
522
|
+
for (let [e, n] of t) n === 0 && t.delete(e);
|
|
523
|
+
for (let [e, t] of n) t.coeff === 0 && n.delete(e);
|
|
524
|
+
return n.size > 0 ? new k(t, n, r) : new A(t, r);
|
|
525
|
+
}
|
|
526
|
+
evalSumSingle(e) {
|
|
527
|
+
if (!L(e)) throw Error("sum() requires a container (Array, Iterable, or Record) or (itemsOrCount, mapFn).");
|
|
528
|
+
return this.accumulateSumItems((t) => {
|
|
529
|
+
t(e.data, 0);
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
evalSumWithMapper(e, t) {
|
|
533
|
+
return this.accumulateSumItems((n) => {
|
|
534
|
+
if (typeof e == "number") {
|
|
535
|
+
if (!Number.isInteger(e) || e < 0) throw Error("sum(count, mapFn) requires a non-negative integer count.");
|
|
536
|
+
for (let r = 0; r < e; r++) n(t(r, r));
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
if (Array.isArray(e)) {
|
|
540
|
+
for (let r = 0; r < e.length; r++) {
|
|
541
|
+
if (!(r in e)) throw Error(`Unsupported item in sum(): sparse array hole at index ${r}.`);
|
|
542
|
+
n(t(e[r], r));
|
|
543
|
+
}
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
if (typeof e == "object" && e && !(e instanceof String) && Symbol.iterator in e && typeof e[Symbol.iterator] == "function") {
|
|
547
|
+
let r = 0;
|
|
548
|
+
for (let i of e) n(t(i, r++));
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
if (B(e)) {
|
|
552
|
+
if (Object.getOwnPropertySymbols(e).length > 0) throw Error("Symbol keys are not supported in sum().");
|
|
553
|
+
let r = 0;
|
|
554
|
+
for (let i of Object.keys(e)) n(t(e[i], r++));
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
throw Error("Invalid first argument in sum(itemsOrCount, mapFn): expected an Iterable, Record, or non-negative integer count.");
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
evalDot(e, t) {
|
|
561
|
+
if (!L(e) || !L(t)) throw Error("dot() in template expression requires 1D or 2D Array or Record containers as arguments.");
|
|
562
|
+
if (e.fromNonArrayIterable || t.fromNonArrayIterable) throw Error("Unsupported operand types in dot() at 'root': dot() requires Array or Record containers (use sum() for general Iterables).");
|
|
563
|
+
let n = s(e.data, t.data);
|
|
564
|
+
return U(n), n;
|
|
179
565
|
}
|
|
180
|
-
|
|
566
|
+
applyPowerNode(e) {
|
|
181
567
|
if (this.peek()?.type !== "CARET") return e;
|
|
182
568
|
let t = [];
|
|
183
569
|
for (; this.peek()?.type === "CARET";) {
|
|
@@ -194,55 +580,87 @@ var be = class {
|
|
|
194
580
|
let n = t[t.length - 1];
|
|
195
581
|
for (let e = t.length - 2; e >= 0; e--) n = t[e] ** +n;
|
|
196
582
|
if (!Number.isFinite(n) || n < 0) throw Error("Exponent creates an invalid or out-of-range power");
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
if (
|
|
201
|
-
return
|
|
583
|
+
if (typeof e == "number") {
|
|
584
|
+
let t = e ** +n;
|
|
585
|
+
if (e !== 0 && n > 0 && t === 0) throw Error("Constant exponentiation underflows to zero.");
|
|
586
|
+
if (!Number.isFinite(t)) throw Error(`Invalid non-finite constant "${t}" in expression.`);
|
|
587
|
+
return t;
|
|
202
588
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
589
|
+
return L(e) ? {
|
|
590
|
+
kind: "container",
|
|
591
|
+
data: R(e.data, (e) => {
|
|
592
|
+
if (typeof e == "number") {
|
|
593
|
+
let t = e ** +n;
|
|
594
|
+
if (e !== 0 && n > 0 && t === 0) throw Error("Constant exponentiation underflows to zero.");
|
|
595
|
+
if (!Number.isFinite(t)) throw Error("Constant exponentiation overflows finite floating-point range.");
|
|
596
|
+
return t;
|
|
597
|
+
}
|
|
598
|
+
let t = Se(Oe(we(e), n));
|
|
599
|
+
return U(t), t;
|
|
600
|
+
}),
|
|
601
|
+
fromNonArrayIterable: e.fromNonArrayIterable
|
|
602
|
+
} : Oe(e, n);
|
|
207
603
|
}
|
|
208
604
|
getRemainingToken() {
|
|
209
605
|
return this.peek();
|
|
210
606
|
}
|
|
211
|
-
|
|
607
|
+
resolveNode(e) {
|
|
608
|
+
if (this.templateCtx?.functionMap.has(e)) throw Error("Function interpolation is only supported as the second argument of sum(itemsOrCount, mapFn) in template expressions.");
|
|
609
|
+
if (this.templateCtx?.rawContainerMap.has(e)) return Ae(this.templateCtx.rawContainerMap.get(e));
|
|
212
610
|
let t = this.resolver(e);
|
|
213
611
|
if (!t) throw Error(`Unknown variable '${e}' in expression`);
|
|
214
|
-
return t;
|
|
612
|
+
return t instanceof b || "id" in t && !("terms" in t) && !("quadTerms" in t) ? new A(/* @__PURE__ */ new Map([[t, 1]]), 0) : t.clone();
|
|
215
613
|
}
|
|
216
614
|
};
|
|
217
|
-
function
|
|
218
|
-
let
|
|
219
|
-
if (
|
|
220
|
-
return
|
|
615
|
+
function Me(e, t, n, r) {
|
|
616
|
+
let i = new je(xe(e, n), t, r), a = i.parseExpression(), o = i.getRemainingToken();
|
|
617
|
+
if (o) throw Error(`Unexpected token '${o.value}' after expression in "${e}"`);
|
|
618
|
+
return U(a), a;
|
|
221
619
|
}
|
|
222
|
-
function
|
|
223
|
-
return
|
|
620
|
+
function Ne(e, t) {
|
|
621
|
+
return Me(e, t);
|
|
224
622
|
}
|
|
225
|
-
function
|
|
226
|
-
let
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
623
|
+
function Pe(e) {
|
|
624
|
+
let t = 0;
|
|
625
|
+
for (let n = 0; n < e.length; n++) {
|
|
626
|
+
let r = e[n];
|
|
627
|
+
if (r === "(") t++;
|
|
628
|
+
else if (r === ")") t > 0 && t--;
|
|
629
|
+
else if (t === 0) {
|
|
630
|
+
let t = e.slice(n, n + 2);
|
|
631
|
+
if (t === "<=" || t === ">=" || t === "==" || t === "=<" || t === "=>") return {
|
|
632
|
+
rawOp: t,
|
|
633
|
+
index: n
|
|
634
|
+
};
|
|
635
|
+
if (r === "=") return {
|
|
636
|
+
rawOp: "=",
|
|
637
|
+
index: n
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
return null;
|
|
642
|
+
}
|
|
643
|
+
function Fe(e, t, n, r, i) {
|
|
644
|
+
let a = Pe(e);
|
|
645
|
+
if (!a) throw Error(`Constraint must contain a comparison operator (<=, >=, ==, =): "${e}"`);
|
|
646
|
+
let o = a.rawOp, s = a.index, c = e.substring(0, s).trim(), l = e.substring(s + o.length).trim(), u;
|
|
647
|
+
if (o === "<=" || o === "=<") u = "<=";
|
|
648
|
+
else if (o === ">=" || o === "=>") u = ">=";
|
|
649
|
+
else if (o === "==" || o === "=") u = "==";
|
|
650
|
+
else throw Error(`Unsupported operator "${o}"`);
|
|
651
|
+
let d = Me(c, t, r, i), f = Me(l, t, r, i);
|
|
652
|
+
if (d instanceof k && d.quadTerms.size > 0 || f instanceof k && f.quadTerms.size > 0) throw Error("Quadratic constraints are not supported (only linear constraints are allowed in MILP/LP/QP)");
|
|
653
|
+
let p = d instanceof k ? new A(d.linearTerms, d.constant) : d, m = f instanceof k ? new A(f.linearTerms, f.constant) : f, h;
|
|
654
|
+
if (h = u === "<=" ? p.le(m, n) : u === ">=" ? p.ge(m, n) : p.eq(m, n), U(h.lhs), !Number.isFinite(h.rhs)) throw Error(`Invalid non-finite right-hand side "${h.rhs}" in constraint.`);
|
|
655
|
+
return h;
|
|
238
656
|
}
|
|
239
|
-
function
|
|
240
|
-
return
|
|
657
|
+
function Ie(e, t, n) {
|
|
658
|
+
return Fe(e, t, n);
|
|
241
659
|
}
|
|
242
|
-
function
|
|
243
|
-
return e instanceof b || e instanceof
|
|
660
|
+
function H(e) {
|
|
661
|
+
return e instanceof b || e instanceof A || e instanceof k ? !0 : typeof e != "object" || !e || W(e) ? !1 : "terms" in e && e.terms instanceof Map && typeof e.constant == "number" || "quadTerms" in e && e.quadTerms instanceof Map && "linearTerms" in e && e.linearTerms instanceof Map && typeof e.constant == "number" || typeof e.id == "number" && typeof e.name == "string" && typeof e.lb == "number" && typeof e.ub == "number" && typeof e.type == "string" && typeof e.mul == "function";
|
|
244
662
|
}
|
|
245
|
-
function
|
|
663
|
+
function U(e) {
|
|
246
664
|
if ("constant" in e && !Number.isFinite(e.constant)) throw Error(`Invalid non-finite constant "${e.constant}" in expression.`);
|
|
247
665
|
if ("terms" in e && e.terms instanceof Map) {
|
|
248
666
|
for (let [, t] of e.terms) if (!Number.isFinite(t)) throw Error(`Invalid non-finite coefficient "${t}" in expression.`);
|
|
@@ -254,40 +672,50 @@ function Ee(e) {
|
|
|
254
672
|
for (let [, t] of e.quadTerms) if (!Number.isFinite(t.coeff)) throw Error(`Invalid non-finite quadratic coefficient "${t.coeff}" in expression.`);
|
|
255
673
|
}
|
|
256
674
|
}
|
|
257
|
-
function
|
|
258
|
-
return e instanceof c
|
|
675
|
+
function W(e) {
|
|
676
|
+
return e instanceof c ? !0 : typeof e != "object" || !e ? !1 : e.kind === "constraint" || "sense" in e && (e.sense === "<=" || e.sense === ">=" || e.sense === "==") && "lhs" in e && "rhs" in e && typeof e.rhs == "number" && typeof e.lhs == "object" && e.lhs !== null && "terms" in e.lhs && e.lhs.terms instanceof Map;
|
|
259
677
|
}
|
|
260
|
-
function
|
|
261
|
-
let n = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Map(), i = e.join(""),
|
|
262
|
-
for (;
|
|
263
|
-
let
|
|
264
|
-
for (let
|
|
265
|
-
let e = t[
|
|
678
|
+
function Le(e, t) {
|
|
679
|
+
let n = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new Map(), o = e.join(""), s = "__washi_";
|
|
680
|
+
for (; o.includes(s);) s += "_";
|
|
681
|
+
let c = "";
|
|
682
|
+
for (let o = 0; o < e.length; o++) if (c += e[o], o < t.length) {
|
|
683
|
+
let e = t[o];
|
|
266
684
|
if (typeof e == "number") {
|
|
267
685
|
if (!Number.isFinite(e)) throw Error(`Invalid numeric value "${e}" in template expression: interpolated numbers must be finite.`);
|
|
268
|
-
let t = `${
|
|
269
|
-
r.set(t, e),
|
|
270
|
-
} else if (
|
|
271
|
-
else if (
|
|
272
|
-
|
|
273
|
-
let t = `${
|
|
274
|
-
n.set(t, e),
|
|
686
|
+
let t = `${s}num_${o}__`;
|
|
687
|
+
r.set(t, e), c += `(${t})`;
|
|
688
|
+
} else if (W(e)) throw Error("Cannot interpolate a constraint comparison (<=, >=, ==) inside a template expression: expected a finite number, Var, LinExpr, or QuadExpr.");
|
|
689
|
+
else if (H(e)) {
|
|
690
|
+
U(e);
|
|
691
|
+
let t = `${s}ph_${o}__`;
|
|
692
|
+
n.set(t, e), c += ` ${t} `;
|
|
693
|
+
} else if (typeof e == "function") {
|
|
694
|
+
let t = `${s}fn_${o}__`;
|
|
695
|
+
a.set(t, e), c += ` ${t} `;
|
|
696
|
+
} else if (Array.isArray(e) || V(e) || e instanceof Map || B(e)) {
|
|
697
|
+
let t = `${s}ph_${o}__`;
|
|
698
|
+
i.set(t, e), c += ` ${t} `;
|
|
275
699
|
} else throw Error("Unsupported interpolation value in template expression: expected a finite number, Var, LinExpr, or QuadExpr.");
|
|
276
700
|
}
|
|
277
701
|
return {
|
|
278
|
-
fullText:
|
|
702
|
+
fullText: c,
|
|
279
703
|
resolver: (e) => n.get(e),
|
|
280
|
-
numberMap: r
|
|
704
|
+
numberMap: r,
|
|
705
|
+
templateCtx: {
|
|
706
|
+
rawContainerMap: i,
|
|
707
|
+
functionMap: a
|
|
708
|
+
}
|
|
281
709
|
};
|
|
282
710
|
}
|
|
283
|
-
function
|
|
284
|
-
let { fullText: n, resolver: r, numberMap: i } =
|
|
285
|
-
return
|
|
711
|
+
function Re(e, ...t) {
|
|
712
|
+
let { fullText: n, resolver: r, numberMap: i, templateCtx: a } = Le(e, t);
|
|
713
|
+
return Pe(n) ? Fe(n, r, void 0, i, a) : Me(n, r, i, a);
|
|
286
714
|
}
|
|
287
715
|
//#endregion
|
|
288
716
|
//#region src/modeler/solvers/highs/worker-client.ts
|
|
289
|
-
var
|
|
290
|
-
function
|
|
717
|
+
var G = null, ze = 0, K = /* @__PURE__ */ new Map();
|
|
718
|
+
function Be(e, t) {
|
|
291
719
|
return e ? {
|
|
292
720
|
Status: "Interrupted",
|
|
293
721
|
StatusCode: 17,
|
|
@@ -311,8 +739,8 @@ function je(e, t) {
|
|
|
311
739
|
Rows: []
|
|
312
740
|
};
|
|
313
741
|
}
|
|
314
|
-
function
|
|
315
|
-
let t =
|
|
742
|
+
function Ve(e) {
|
|
743
|
+
let t = K.values().next().value;
|
|
316
744
|
if (t && !t.dispatched) {
|
|
317
745
|
t.dispatched = !0;
|
|
318
746
|
try {
|
|
@@ -324,30 +752,30 @@ function Me(e) {
|
|
|
324
752
|
...t.reportProgress ? { reportProgress: !0 } : {}
|
|
325
753
|
});
|
|
326
754
|
} catch (n) {
|
|
327
|
-
|
|
755
|
+
K.delete(t.id), t.cleanupAbort?.(), t.reject(n), Ve(e);
|
|
328
756
|
}
|
|
329
757
|
}
|
|
330
758
|
}
|
|
331
|
-
function
|
|
332
|
-
let e =
|
|
333
|
-
e && (
|
|
334
|
-
for (let e of
|
|
335
|
-
if (
|
|
336
|
-
let e =
|
|
337
|
-
if (e)
|
|
759
|
+
function He() {
|
|
760
|
+
let e = G;
|
|
761
|
+
e && (G = null, e.terminate());
|
|
762
|
+
for (let e of K.values()) e.dispatched = !1;
|
|
763
|
+
if (K.size > 0) {
|
|
764
|
+
let e = Ue();
|
|
765
|
+
if (e) Ve(e);
|
|
338
766
|
else {
|
|
339
767
|
let e = /* @__PURE__ */ Error("[WaSHI] Failed to recreate HiGHS Worker after interruption.");
|
|
340
|
-
for (let t of Array.from(
|
|
768
|
+
for (let t of Array.from(K.values())) K.delete(t.id), t.cleanupAbort?.(), t.reject(e);
|
|
341
769
|
}
|
|
342
770
|
}
|
|
343
771
|
}
|
|
344
|
-
function
|
|
772
|
+
function Ue() {
|
|
345
773
|
if (typeof window > "u" || typeof Worker > "u") return null;
|
|
346
|
-
if (!
|
|
347
|
-
|
|
348
|
-
let e =
|
|
349
|
-
|
|
350
|
-
let { type: n, id: r, result: i, runTime: a, error: o, progress: s, incumbent: c } = t.data, l =
|
|
774
|
+
if (!G) try {
|
|
775
|
+
G = fe();
|
|
776
|
+
let e = G;
|
|
777
|
+
G.onmessage = (t) => {
|
|
778
|
+
let { type: n, id: r, result: i, runTime: a, error: o, progress: s, incumbent: c } = t.data, l = K.get(r);
|
|
351
779
|
if (l) {
|
|
352
780
|
if (n === "SOLVE_PROGRESS") {
|
|
353
781
|
if (s) {
|
|
@@ -355,10 +783,10 @@ function Pe() {
|
|
|
355
783
|
try {
|
|
356
784
|
l.onProgress?.(s);
|
|
357
785
|
} catch (e) {
|
|
358
|
-
let t =
|
|
786
|
+
let t = K.get(r);
|
|
359
787
|
if (t) {
|
|
360
|
-
let n =
|
|
361
|
-
|
|
788
|
+
let n = K.keys().next().value;
|
|
789
|
+
K.delete(r), t.cleanupAbort?.(), n === r && He(), t.reject(e);
|
|
362
790
|
}
|
|
363
791
|
}
|
|
364
792
|
}
|
|
@@ -368,72 +796,72 @@ function Pe() {
|
|
|
368
796
|
c && (l.lastIncumbent = c);
|
|
369
797
|
return;
|
|
370
798
|
}
|
|
371
|
-
|
|
799
|
+
K.delete(r), l.cleanupAbort?.(), n === "SOLVE_SUCCESS" ? l.resolve({
|
|
372
800
|
result: i,
|
|
373
801
|
runTime: a
|
|
374
|
-
}) : l.reject(Error(o || "Worker solve failed")),
|
|
802
|
+
}) : l.reject(Error(o || "Worker solve failed")), G === e && Ve(e);
|
|
375
803
|
}
|
|
376
804
|
};
|
|
377
805
|
let t = (t) => {
|
|
378
|
-
if (
|
|
379
|
-
|
|
380
|
-
for (let e of
|
|
381
|
-
|
|
806
|
+
if (G === e) {
|
|
807
|
+
G = null, e.terminate();
|
|
808
|
+
for (let e of K.values()) e.cleanupAbort?.(), e.reject(t);
|
|
809
|
+
K.clear();
|
|
382
810
|
}
|
|
383
811
|
};
|
|
384
|
-
|
|
812
|
+
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."));
|
|
385
813
|
} catch {
|
|
386
|
-
|
|
814
|
+
G = null;
|
|
387
815
|
}
|
|
388
|
-
return
|
|
816
|
+
return G;
|
|
389
817
|
}
|
|
390
|
-
async function
|
|
818
|
+
async function We(e, t = {}) {
|
|
391
819
|
if (t.signal?.aborted) return {
|
|
392
|
-
result:
|
|
820
|
+
result: Be(),
|
|
393
821
|
runTime: 0
|
|
394
822
|
};
|
|
395
|
-
let n = await
|
|
823
|
+
let n = await N(t.wasmUrl);
|
|
396
824
|
if (t.signal?.aborted) return {
|
|
397
|
-
result:
|
|
825
|
+
result: Be(),
|
|
398
826
|
runTime: 0
|
|
399
827
|
};
|
|
400
|
-
let r = t.useWorker ?? typeof window < "u" ?
|
|
401
|
-
if (!
|
|
402
|
-
let { signal:
|
|
403
|
-
for (let [e, t] of Object.entries(
|
|
404
|
-
|
|
405
|
-
let
|
|
828
|
+
let r = typeof window < "u" ? typeof window.origin == "string" ? window.origin : window.location?.origin : void 0, i = t.useWorker ?? (typeof window < "u" && r !== "null" && !r?.startsWith("file:")) ? Ue() : null;
|
|
829
|
+
if (!i) return M(e, t);
|
|
830
|
+
let { signal: a, onProgress: o, ...s } = t, c = {};
|
|
831
|
+
for (let [e, t] of Object.entries(s)) typeof t != "function" && (c[e] = t);
|
|
832
|
+
c.wasmUrl = n;
|
|
833
|
+
let l = !!(o || a), u = `req_${++ze}`, d = performance.now();
|
|
406
834
|
return new Promise((t, n) => {
|
|
407
|
-
let
|
|
408
|
-
id:
|
|
835
|
+
let r = {
|
|
836
|
+
id: u,
|
|
409
837
|
lpString: e,
|
|
410
|
-
workerOptions:
|
|
411
|
-
reportProgress:
|
|
412
|
-
startTime:
|
|
413
|
-
onProgress:
|
|
838
|
+
workerOptions: c,
|
|
839
|
+
reportProgress: l,
|
|
840
|
+
startTime: d,
|
|
841
|
+
onProgress: o,
|
|
414
842
|
resolve: t,
|
|
415
843
|
reject: n
|
|
416
844
|
};
|
|
417
|
-
if (
|
|
845
|
+
if (a) {
|
|
418
846
|
let e = () => {
|
|
419
|
-
let e =
|
|
847
|
+
let e = K.get(u);
|
|
420
848
|
if (!e) return;
|
|
421
|
-
let t =
|
|
422
|
-
|
|
849
|
+
let t = K.keys().next().value;
|
|
850
|
+
K.delete(u), e.cleanupAbort?.(), t === u && He();
|
|
423
851
|
let n = (performance.now() - e.startTime) / 1e3;
|
|
424
852
|
e.resolve({
|
|
425
|
-
result:
|
|
853
|
+
result: Be(e.lastIncumbent, e.lastProgress),
|
|
426
854
|
runTime: n
|
|
427
855
|
});
|
|
428
856
|
};
|
|
429
|
-
|
|
857
|
+
a.addEventListener("abort", e, { once: !0 }), r.cleanupAbort = () => a.removeEventListener("abort", e);
|
|
430
858
|
}
|
|
431
|
-
|
|
859
|
+
K.set(u, r), Ve(i);
|
|
432
860
|
});
|
|
433
861
|
}
|
|
434
862
|
//#endregion
|
|
435
863
|
//#region src/modeler/solvers/highs/solver.ts
|
|
436
|
-
function
|
|
864
|
+
function q(e, t) {
|
|
437
865
|
if (typeof t == "number") switch (t) {
|
|
438
866
|
case 0: return "NotSolved";
|
|
439
867
|
case 1:
|
|
@@ -492,8 +920,8 @@ function H(e, t) {
|
|
|
492
920
|
let r = n.toLowerCase();
|
|
493
921
|
return r.includes("infeasible") && r.includes("unbounded") ? "InfeasibleOrUnbounded" : r.includes("infeasible") ? "Infeasible" : r.includes("unbounded") ? "Unbounded" : r.includes("time limit") || r.includes("timelimit") ? "TimeLimit" : r.includes("iteration limit") || r.includes("iterationlimit") ? "IterationLimit" : r.includes("solution limit") || r.includes("solutionlimit") ? "SolutionLimit" : r.includes("memory limit") || r.includes("memorylimit") ? "MemoryLimit" : r.includes("bound on objective") ? "ObjectiveBound" : r.includes("target for objective") ? "ObjectiveTarget" : r.includes("interrupt") ? "Interrupted" : r.includes("error") ? "Error" : r === "optimal" ? "Optimal" : "Unknown";
|
|
494
922
|
}
|
|
495
|
-
function
|
|
496
|
-
switch (
|
|
923
|
+
function Ge(e, t, n) {
|
|
924
|
+
switch (q(e, t)) {
|
|
497
925
|
case "Optimal": return "Optimal";
|
|
498
926
|
case "Infeasible": return "Infeasible";
|
|
499
927
|
case "Unbounded": return "Unbounded";
|
|
@@ -510,7 +938,7 @@ function U(e, t, n) {
|
|
|
510
938
|
default: return "Unknown";
|
|
511
939
|
}
|
|
512
940
|
}
|
|
513
|
-
var
|
|
941
|
+
var J = class e {
|
|
514
942
|
static capabilities = {
|
|
515
943
|
supportsMIP: !0,
|
|
516
944
|
supportsQP: !0,
|
|
@@ -524,7 +952,7 @@ var W = class e {
|
|
|
524
952
|
this.options = e;
|
|
525
953
|
}
|
|
526
954
|
async solve(e, t = {}, n = {}) {
|
|
527
|
-
let r =
|
|
955
|
+
let r = T(t), i = r ? n : t, a = {
|
|
528
956
|
...this.options,
|
|
529
957
|
...i
|
|
530
958
|
}, o = e.objective.constant || 0;
|
|
@@ -532,13 +960,13 @@ var W = class e {
|
|
|
532
960
|
let { lpString: s, nameToVar: c, nameToConstraint: l } = r ? t : C(e, { relax: a.relax }), u = Object.create(null), p = !1, m = /* @__PURE__ */ new Set();
|
|
533
961
|
for (let [e, t] of c.entries()) {
|
|
534
962
|
m.add(t);
|
|
535
|
-
let n =
|
|
963
|
+
let n = ee(t);
|
|
536
964
|
n !== void 0 && (u[e] = n, p = !0);
|
|
537
965
|
}
|
|
538
966
|
for (let t = 0; t < e.variables.length; t++) {
|
|
539
967
|
let n = e.variables[t];
|
|
540
968
|
if (!m.has(n)) {
|
|
541
|
-
let e =
|
|
969
|
+
let e = ee(n);
|
|
542
970
|
if (e !== void 0) {
|
|
543
971
|
let r = f(n.name, "x", t);
|
|
544
972
|
Object.prototype.hasOwnProperty.call(u, r) || (u[r] = e, p = !0);
|
|
@@ -551,33 +979,33 @@ var W = class e {
|
|
|
551
979
|
...o === 0 ? {} : { objectiveOffset: o },
|
|
552
980
|
initialValues: p ? u : void 0
|
|
553
981
|
});
|
|
554
|
-
let { result: g, runTime: _ } = await
|
|
982
|
+
let { result: g, runTime: _ } = await We(s, h), v = g?.ObjectiveOffsetApplied ? 0 : o, y = q(g?.Status, g?.StatusCode), b = a.relax ? !1 : typeof g?.IsMip == "boolean" ? g.IsMip : e.variables.some((e) => e.type === "Integer" || e.type === "Binary"), x;
|
|
555
983
|
if (typeof g?.HasPrimalSolution == "boolean") x = g.HasPrimalSolution;
|
|
556
984
|
else {
|
|
557
985
|
let e = g?.Columns ? Object.values(g.Columns) : [], t = e.length > 0 && e.some((e) => typeof e?.Primal == "number" && Number.isFinite(e.Primal)), n = typeof g?.ObjectiveValue == "number" && Number.isFinite(g.ObjectiveValue);
|
|
558
986
|
x = y === "Infeasible" || y === "Unbounded" || y === "InfeasibleOrUnbounded" || y === "Error" || y === "NotSolved" ? !1 : y === "Optimal" || y === "Feasible" ? n || t : n && (e.length === 0 || t);
|
|
559
987
|
}
|
|
560
|
-
let S =
|
|
561
|
-
x && typeof g?.ObjectiveValue == "number" && Number.isFinite(g.ObjectiveValue) && (
|
|
562
|
-
let D, O
|
|
563
|
-
if (typeof g?.MipDualBound == "number" && Number.isFinite(g.MipDualBound) ?
|
|
564
|
-
if (
|
|
565
|
-
else if (Math.abs(
|
|
566
|
-
let e =
|
|
567
|
-
|
|
568
|
-
} else
|
|
569
|
-
} else typeof g?.MipGap == "number" && Number.isFinite(g.MipGap) ?
|
|
988
|
+
let S = Ge(g?.Status, g?.StatusCode, x), w = null;
|
|
989
|
+
x && typeof g?.ObjectiveValue == "number" && Number.isFinite(g.ObjectiveValue) && (w = g.ObjectiveValue + v);
|
|
990
|
+
let E, D, O;
|
|
991
|
+
if (typeof g?.MipDualBound == "number" && Number.isFinite(g.MipDualBound) ? E = g.MipDualBound + v : !b && y === "Optimal" && w !== null && (E = w), w !== null && E !== void 0 && Number.isFinite(E) && (O = Math.abs(w - E)), v !== 0 && b && w !== null && E !== void 0 && O !== void 0) {
|
|
992
|
+
if (O === 0) D = 0;
|
|
993
|
+
else if (Math.abs(w) > 0) {
|
|
994
|
+
let e = O / Math.abs(w);
|
|
995
|
+
D = Number.isFinite(e) ? e : void 0;
|
|
996
|
+
} else D = void 0;
|
|
997
|
+
} else typeof g?.MipGap == "number" && Number.isFinite(g.MipGap) ? D = g.MipGap : b && y === "Optimal" && x && (O === void 0 || O === 0) && (D = 0);
|
|
570
998
|
if (!x) return {
|
|
571
999
|
status: S,
|
|
572
1000
|
terminationStatus: y,
|
|
573
1001
|
hasPrimalSolution: !1,
|
|
574
1002
|
objectiveValue: null,
|
|
575
|
-
...
|
|
576
|
-
...
|
|
577
|
-
relGap:
|
|
578
|
-
mipGap:
|
|
1003
|
+
...E === void 0 ? {} : { objectiveBound: E },
|
|
1004
|
+
...D === void 0 ? {} : {
|
|
1005
|
+
relGap: D,
|
|
1006
|
+
mipGap: D
|
|
579
1007
|
},
|
|
580
|
-
...
|
|
1008
|
+
...O === void 0 ? {} : { absGap: O },
|
|
581
1009
|
values: {},
|
|
582
1010
|
reducedCosts: {},
|
|
583
1011
|
duals: {},
|
|
@@ -585,31 +1013,31 @@ var W = class e {
|
|
|
585
1013
|
runTime: _,
|
|
586
1014
|
raw: g
|
|
587
1015
|
};
|
|
588
|
-
let
|
|
1016
|
+
let k = (e, t, n) => {
|
|
589
1017
|
Object.defineProperty(e, t, {
|
|
590
1018
|
value: n,
|
|
591
1019
|
enumerable: !0,
|
|
592
1020
|
writable: !0,
|
|
593
1021
|
configurable: !0
|
|
594
1022
|
});
|
|
595
|
-
},
|
|
1023
|
+
}, A = {}, te = {}, j = {}, M = {};
|
|
596
1024
|
if (g.Columns) for (let [e, t] of Object.entries(g.Columns)) {
|
|
597
1025
|
let n = c.get(e);
|
|
598
1026
|
if (n) {
|
|
599
1027
|
if (typeof t.Primal == "number" && Number.isFinite(t.Primal)) {
|
|
600
1028
|
let e = Math.abs(t.Primal) < 1e-9 ? 0 : t.Primal;
|
|
601
|
-
|
|
1029
|
+
!a.relax && (n.type === "Integer" || n.type === "Binary") && Math.abs(e - Math.round(e)) <= 1e-5 && (e = Math.round(e) + 0), k(A, n.name, e);
|
|
602
1030
|
}
|
|
603
|
-
typeof t.Dual == "number" && Number.isFinite(t.Dual) &&
|
|
1031
|
+
typeof t.Dual == "number" && Number.isFinite(t.Dual) && k(j, n.name, Math.abs(t.Dual) < 1e-9 ? 0 : t.Dual);
|
|
604
1032
|
}
|
|
605
1033
|
}
|
|
606
1034
|
if (g.Rows) {
|
|
607
1035
|
let e = Array.isArray(g.Rows) ? g.Rows : Object.values(g.Rows);
|
|
608
1036
|
for (let t of e) {
|
|
609
1037
|
let e = t.Name, n = e ? l.get(e) : void 0;
|
|
610
|
-
if (n && (typeof t.Dual == "number" && Number.isFinite(t.Dual) &&
|
|
1038
|
+
if (n && (typeof t.Dual == "number" && Number.isFinite(t.Dual) && k(te, n.name, Math.abs(t.Dual) < 1e-9 ? 0 : t.Dual), typeof t.Primal == "number" && Number.isFinite(t.Primal))) {
|
|
611
1039
|
let e = n.rhs - t.Primal;
|
|
612
|
-
|
|
1040
|
+
k(M, n.name, Math.abs(e) < 1e-9 ? 0 : e);
|
|
613
1041
|
}
|
|
614
1042
|
}
|
|
615
1043
|
}
|
|
@@ -617,84 +1045,84 @@ var W = class e {
|
|
|
617
1045
|
status: S,
|
|
618
1046
|
terminationStatus: y,
|
|
619
1047
|
hasPrimalSolution: !0,
|
|
620
|
-
objectiveValue:
|
|
621
|
-
...
|
|
622
|
-
...
|
|
623
|
-
relGap:
|
|
624
|
-
mipGap:
|
|
1048
|
+
objectiveValue: w,
|
|
1049
|
+
...E === void 0 ? {} : { objectiveBound: E },
|
|
1050
|
+
...D === void 0 ? {} : {
|
|
1051
|
+
relGap: D,
|
|
1052
|
+
mipGap: D
|
|
625
1053
|
},
|
|
626
|
-
...
|
|
627
|
-
values:
|
|
628
|
-
reducedCosts:
|
|
629
|
-
duals:
|
|
630
|
-
slacks:
|
|
1054
|
+
...O === void 0 ? {} : { absGap: O },
|
|
1055
|
+
values: A,
|
|
1056
|
+
reducedCosts: j,
|
|
1057
|
+
duals: te,
|
|
1058
|
+
slacks: M,
|
|
631
1059
|
runTime: _,
|
|
632
1060
|
raw: g
|
|
633
1061
|
};
|
|
634
1062
|
}
|
|
635
|
-
},
|
|
636
|
-
function
|
|
637
|
-
|
|
1063
|
+
}, Ke = J;
|
|
1064
|
+
function qe() {
|
|
1065
|
+
w("highs", (e) => new J(e)), _((e) => new J(e));
|
|
638
1066
|
}
|
|
639
|
-
|
|
1067
|
+
qe();
|
|
640
1068
|
//#endregion
|
|
641
1069
|
//#region src/modeler/solvers/highs/index.ts
|
|
642
|
-
var
|
|
643
|
-
HIGHS_CONSTANTS: () =>
|
|
644
|
-
HIGHS_STATUS_NAMES: () =>
|
|
645
|
-
HiGHS: () =>
|
|
646
|
-
Highs: () =>
|
|
647
|
-
HighsError: () =>
|
|
648
|
-
HighsModel: () =>
|
|
649
|
-
HighsSolver: () =>
|
|
650
|
-
HighsUnsupportedOptionError: () =>
|
|
651
|
-
OFFICIAL_HIGHS_CDN_WASM_URL: () =>
|
|
652
|
-
bindHighsFunctions: () =>
|
|
653
|
-
getHighs: () =>
|
|
654
|
-
getHighsWasmUrl: () =>
|
|
655
|
-
isValidWasmHeader: () =>
|
|
656
|
-
mapHighsStatus: () =>
|
|
657
|
-
mapHighsTerminationStatus: () =>
|
|
658
|
-
probeWasmUrl: () =>
|
|
659
|
-
registerHighsSolver: () =>
|
|
660
|
-
resolveBrowserWasmUrl: () =>
|
|
661
|
-
setHighsWasmUrl: () =>
|
|
662
|
-
solveLp: () =>
|
|
663
|
-
solveLpWithHighs: () =>
|
|
1070
|
+
var Je = /* @__PURE__ */ _e({
|
|
1071
|
+
HIGHS_CONSTANTS: () => oe,
|
|
1072
|
+
HIGHS_STATUS_NAMES: () => de,
|
|
1073
|
+
HiGHS: () => Ke,
|
|
1074
|
+
Highs: () => ne,
|
|
1075
|
+
HighsError: () => re,
|
|
1076
|
+
HighsModel: () => le,
|
|
1077
|
+
HighsSolver: () => J,
|
|
1078
|
+
HighsUnsupportedOptionError: () => se,
|
|
1079
|
+
OFFICIAL_HIGHS_CDN_WASM_URL: () => ue,
|
|
1080
|
+
bindHighsFunctions: () => ie,
|
|
1081
|
+
getHighs: () => ce,
|
|
1082
|
+
getHighsWasmUrl: () => P,
|
|
1083
|
+
isValidWasmHeader: () => ae,
|
|
1084
|
+
mapHighsStatus: () => Ge,
|
|
1085
|
+
mapHighsTerminationStatus: () => q,
|
|
1086
|
+
probeWasmUrl: () => j,
|
|
1087
|
+
registerHighsSolver: () => qe,
|
|
1088
|
+
resolveBrowserWasmUrl: () => N,
|
|
1089
|
+
setHighsWasmUrl: () => F,
|
|
1090
|
+
solveLp: () => We,
|
|
1091
|
+
solveLpWithHighs: () => M
|
|
664
1092
|
});
|
|
665
|
-
import * as
|
|
666
|
-
|
|
667
|
-
import * as
|
|
668
|
-
|
|
1093
|
+
import * as Ye from "./highs-worker-factory.js";
|
|
1094
|
+
I(Je, Ye);
|
|
1095
|
+
import * as Xe from "./highs-assets.js";
|
|
1096
|
+
I(Je, Xe);
|
|
669
1097
|
//#endregion
|
|
670
1098
|
//#region src/modeler/solvers/highs.ts
|
|
671
|
-
var
|
|
672
|
-
HIGHS_CONSTANTS: () =>
|
|
673
|
-
HIGHS_STATUS_NAMES: () =>
|
|
674
|
-
HiGHS: () =>
|
|
675
|
-
Highs: () =>
|
|
676
|
-
HighsError: () =>
|
|
677
|
-
HighsModel: () =>
|
|
678
|
-
HighsSolver: () =>
|
|
679
|
-
HighsUnsupportedOptionError: () =>
|
|
680
|
-
OFFICIAL_HIGHS_CDN_WASM_URL: () =>
|
|
681
|
-
bindHighsFunctions: () =>
|
|
682
|
-
getHighs: () =>
|
|
683
|
-
getHighsWasmUrl: () =>
|
|
684
|
-
isValidWasmHeader: () =>
|
|
685
|
-
mapHighsStatus: () =>
|
|
686
|
-
mapHighsTerminationStatus: () =>
|
|
687
|
-
probeWasmUrl: () =>
|
|
688
|
-
registerHighsSolver: () =>
|
|
689
|
-
resolveBrowserWasmUrl: () =>
|
|
690
|
-
setHighsWasmUrl: () =>
|
|
691
|
-
solveLp: () =>
|
|
692
|
-
solveLpWithHighs: () =>
|
|
1099
|
+
var Ze = /* @__PURE__ */ _e({
|
|
1100
|
+
HIGHS_CONSTANTS: () => oe,
|
|
1101
|
+
HIGHS_STATUS_NAMES: () => de,
|
|
1102
|
+
HiGHS: () => Ke,
|
|
1103
|
+
Highs: () => ne,
|
|
1104
|
+
HighsError: () => re,
|
|
1105
|
+
HighsModel: () => le,
|
|
1106
|
+
HighsSolver: () => J,
|
|
1107
|
+
HighsUnsupportedOptionError: () => se,
|
|
1108
|
+
OFFICIAL_HIGHS_CDN_WASM_URL: () => ue,
|
|
1109
|
+
bindHighsFunctions: () => ie,
|
|
1110
|
+
getHighs: () => ce,
|
|
1111
|
+
getHighsWasmUrl: () => P,
|
|
1112
|
+
isValidWasmHeader: () => ae,
|
|
1113
|
+
mapHighsStatus: () => Ge,
|
|
1114
|
+
mapHighsTerminationStatus: () => q,
|
|
1115
|
+
probeWasmUrl: () => j,
|
|
1116
|
+
registerHighsSolver: () => qe,
|
|
1117
|
+
resolveBrowserWasmUrl: () => N,
|
|
1118
|
+
setHighsWasmUrl: () => F,
|
|
1119
|
+
solveLp: () => We,
|
|
1120
|
+
solveLpWithHighs: () => M
|
|
693
1121
|
});
|
|
694
|
-
|
|
1122
|
+
I(Ze, Je);
|
|
695
1123
|
//#endregion
|
|
696
1124
|
//#region src/modeler/solvers/mock.ts
|
|
697
|
-
var
|
|
1125
|
+
var Qe = class {
|
|
698
1126
|
name;
|
|
699
1127
|
capabilities;
|
|
700
1128
|
options;
|
|
@@ -706,7 +1134,7 @@ var ze = class {
|
|
|
706
1134
|
}, this.options = e;
|
|
707
1135
|
}
|
|
708
1136
|
async solve(e, t = {}, n = {}) {
|
|
709
|
-
let r =
|
|
1137
|
+
let r = T(t) ? n : t, i = {
|
|
710
1138
|
...this.options,
|
|
711
1139
|
...r
|
|
712
1140
|
}, a = !!(i.values && Object.keys(i.values).length > 0 || i.boolValues && Object.keys(i.boolValues).length > 0);
|
|
@@ -794,51 +1222,51 @@ var ze = class {
|
|
|
794
1222
|
};
|
|
795
1223
|
}
|
|
796
1224
|
};
|
|
797
|
-
function
|
|
798
|
-
|
|
1225
|
+
function $e() {
|
|
1226
|
+
w("mock", (e) => new Qe(e));
|
|
799
1227
|
}
|
|
800
|
-
|
|
1228
|
+
$e();
|
|
801
1229
|
//#endregion
|
|
802
1230
|
//#region src/modeler/solvers/index.ts
|
|
803
|
-
var
|
|
804
|
-
HIGHS_CONSTANTS: () =>
|
|
805
|
-
HIGHS_STATUS_NAMES: () =>
|
|
806
|
-
HiGHS: () =>
|
|
807
|
-
Highs: () =>
|
|
808
|
-
HighsError: () =>
|
|
809
|
-
HighsModel: () =>
|
|
810
|
-
HighsSolver: () =>
|
|
811
|
-
HighsUnsupportedOptionError: () =>
|
|
812
|
-
MockSolver: () =>
|
|
813
|
-
OFFICIAL_HIGHS_CDN_WASM_URL: () =>
|
|
814
|
-
bindHighsFunctions: () =>
|
|
815
|
-
getHighs: () =>
|
|
816
|
-
getHighsWasmUrl: () =>
|
|
817
|
-
getSolver: () =>
|
|
818
|
-
isValidWasmHeader: () =>
|
|
1231
|
+
var et = /* @__PURE__ */ _e({
|
|
1232
|
+
HIGHS_CONSTANTS: () => oe,
|
|
1233
|
+
HIGHS_STATUS_NAMES: () => de,
|
|
1234
|
+
HiGHS: () => Ke,
|
|
1235
|
+
Highs: () => ne,
|
|
1236
|
+
HighsError: () => re,
|
|
1237
|
+
HighsModel: () => le,
|
|
1238
|
+
HighsSolver: () => J,
|
|
1239
|
+
HighsUnsupportedOptionError: () => se,
|
|
1240
|
+
MockSolver: () => Qe,
|
|
1241
|
+
OFFICIAL_HIGHS_CDN_WASM_URL: () => ue,
|
|
1242
|
+
bindHighsFunctions: () => ie,
|
|
1243
|
+
getHighs: () => ce,
|
|
1244
|
+
getHighsWasmUrl: () => P,
|
|
1245
|
+
getSolver: () => E,
|
|
1246
|
+
isValidWasmHeader: () => ae,
|
|
819
1247
|
listAvailableSolvers: () => S,
|
|
820
|
-
mapHighsStatus: () =>
|
|
821
|
-
mapHighsTerminationStatus: () =>
|
|
822
|
-
probeWasmUrl: () =>
|
|
823
|
-
registerHighsSolver: () =>
|
|
824
|
-
registerMockSolver: () =>
|
|
825
|
-
registerSolver: () =>
|
|
826
|
-
resolveBrowserWasmUrl: () =>
|
|
1248
|
+
mapHighsStatus: () => Ge,
|
|
1249
|
+
mapHighsTerminationStatus: () => q,
|
|
1250
|
+
probeWasmUrl: () => j,
|
|
1251
|
+
registerHighsSolver: () => qe,
|
|
1252
|
+
registerMockSolver: () => $e,
|
|
1253
|
+
registerSolver: () => w,
|
|
1254
|
+
resolveBrowserWasmUrl: () => N,
|
|
827
1255
|
setDefaultSolver: () => _,
|
|
828
|
-
setHighsWasmUrl: () =>
|
|
829
|
-
solveLp: () =>
|
|
830
|
-
solveLpWithHighs: () =>
|
|
1256
|
+
setHighsWasmUrl: () => F,
|
|
1257
|
+
solveLp: () => We,
|
|
1258
|
+
solveLpWithHighs: () => M
|
|
831
1259
|
});
|
|
832
|
-
|
|
1260
|
+
I(et, Ze);
|
|
833
1261
|
//#endregion
|
|
834
1262
|
//#region src/modeler/solution-validator.ts
|
|
835
|
-
function
|
|
1263
|
+
function tt(e, t) {
|
|
836
1264
|
if (e === void 0) return 1e-6;
|
|
837
1265
|
if (typeof e != "number" || !Number.isFinite(e) || e < 0) throw Error(`Invalid ${t}: expected a finite non-negative number, received ${String(e)}.`);
|
|
838
1266
|
return e;
|
|
839
1267
|
}
|
|
840
|
-
function
|
|
841
|
-
let r =
|
|
1268
|
+
function nt(e, t = {}) {
|
|
1269
|
+
let r = tt(t.feasibilityTolerance, "feasibilityTolerance"), i = tt(t.integralityTolerance, "integralityTolerance"), a = t.evaluateLogicalConstraints === void 0 || !!t.evaluateLogicalConstraints, s = t.values !== void 0 && t.values !== null;
|
|
842
1270
|
if (t.values !== void 0 && (typeof t.values != "object" || t.values === null || Array.isArray(t.values))) throw Error("Invalid values option: expected a plain object mapping variable names to values.");
|
|
843
1271
|
let l = t.values, u = [], d = Object.create(null), f = [], p = [], m = 0, h = (e) => {
|
|
844
1272
|
typeof e == "number" && !Number.isNaN(e) && e > m && (m = e);
|
|
@@ -996,7 +1424,7 @@ function Ue(e, t = {}) {
|
|
|
996
1424
|
ok: !0,
|
|
997
1425
|
value: n
|
|
998
1426
|
};
|
|
999
|
-
},
|
|
1427
|
+
}, ee = (e, t = r) => {
|
|
1000
1428
|
if (typeof e.rhs != "number" || !Number.isFinite(e.rhs)) return {
|
|
1001
1429
|
status: "unchecked",
|
|
1002
1430
|
reason: `non-finite right-hand side (${String(e.rhs)})`
|
|
@@ -1026,7 +1454,7 @@ function Ue(e, t = {}) {
|
|
|
1026
1454
|
};
|
|
1027
1455
|
};
|
|
1028
1456
|
for (let t of e.constraints) {
|
|
1029
|
-
let e =
|
|
1457
|
+
let e = ee(t);
|
|
1030
1458
|
e.status === "unchecked" ? (f.push(t.name), g({
|
|
1031
1459
|
type: "unchecked_constraint",
|
|
1032
1460
|
target: "constraint",
|
|
@@ -1049,7 +1477,7 @@ function Ue(e, t = {}) {
|
|
|
1049
1477
|
message: e.message
|
|
1050
1478
|
})) : (x++, h(e.violation));
|
|
1051
1479
|
}
|
|
1052
|
-
let
|
|
1480
|
+
let w = (e) => {
|
|
1053
1481
|
if (e instanceof o) return v.has(e) ? {
|
|
1054
1482
|
ok: !0,
|
|
1055
1483
|
value: v.get(e),
|
|
@@ -1059,7 +1487,7 @@ function Ue(e, t = {}) {
|
|
|
1059
1487
|
reason: `missing or invalid value for Boolean variable "${e.name}"`
|
|
1060
1488
|
};
|
|
1061
1489
|
if (e instanceof c) {
|
|
1062
|
-
let t =
|
|
1490
|
+
let t = ee(e, 0);
|
|
1063
1491
|
if (t.status === "unchecked") return {
|
|
1064
1492
|
ok: !1,
|
|
1065
1493
|
reason: t.reason
|
|
@@ -1079,7 +1507,7 @@ function Ue(e, t = {}) {
|
|
|
1079
1507
|
}
|
|
1080
1508
|
if (e instanceof n) switch (e.op) {
|
|
1081
1509
|
case "not": {
|
|
1082
|
-
let t =
|
|
1510
|
+
let t = w(e.children[0]);
|
|
1083
1511
|
return t.ok ? {
|
|
1084
1512
|
ok: !0,
|
|
1085
1513
|
value: !t.value,
|
|
@@ -1089,7 +1517,7 @@ function Ue(e, t = {}) {
|
|
|
1089
1517
|
case "and": {
|
|
1090
1518
|
let t = 0;
|
|
1091
1519
|
for (let n of e.children) {
|
|
1092
|
-
let e =
|
|
1520
|
+
let e = w(n);
|
|
1093
1521
|
if (!e.ok) return e;
|
|
1094
1522
|
if (!e.value) return {
|
|
1095
1523
|
ok: !0,
|
|
@@ -1107,7 +1535,7 @@ function Ue(e, t = {}) {
|
|
|
1107
1535
|
case "or": {
|
|
1108
1536
|
let t = !1, n = Infinity;
|
|
1109
1537
|
for (let r of e.children) {
|
|
1110
|
-
let e =
|
|
1538
|
+
let e = w(r);
|
|
1111
1539
|
if (!e.ok) return e;
|
|
1112
1540
|
e.value && (t = !0, e.violation < n && (n = e.violation));
|
|
1113
1541
|
}
|
|
@@ -1118,14 +1546,14 @@ function Ue(e, t = {}) {
|
|
|
1118
1546
|
};
|
|
1119
1547
|
}
|
|
1120
1548
|
case "implies": {
|
|
1121
|
-
let t =
|
|
1549
|
+
let t = w(e.children[0]);
|
|
1122
1550
|
if (!t.ok) return t;
|
|
1123
1551
|
if (!t.value) return {
|
|
1124
1552
|
ok: !0,
|
|
1125
1553
|
value: !0,
|
|
1126
1554
|
violation: 0
|
|
1127
1555
|
};
|
|
1128
|
-
let n =
|
|
1556
|
+
let n = w(e.children[1]);
|
|
1129
1557
|
return n.ok ? {
|
|
1130
1558
|
ok: !0,
|
|
1131
1559
|
value: n.value,
|
|
@@ -1135,7 +1563,7 @@ function Ue(e, t = {}) {
|
|
|
1135
1563
|
case "xor": {
|
|
1136
1564
|
let t = !1, n = 0;
|
|
1137
1565
|
for (let r of e.children) {
|
|
1138
|
-
let e =
|
|
1566
|
+
let e = w(r);
|
|
1139
1567
|
if (!e.ok) return e;
|
|
1140
1568
|
e.value && (t = !t, e.violation > n && (n = e.violation));
|
|
1141
1569
|
}
|
|
@@ -1167,7 +1595,7 @@ function Ue(e, t = {}) {
|
|
|
1167
1595
|
});
|
|
1168
1596
|
continue;
|
|
1169
1597
|
}
|
|
1170
|
-
let i =
|
|
1598
|
+
let i = w(n.expr);
|
|
1171
1599
|
if (!i.ok) {
|
|
1172
1600
|
f.push(r), g({
|
|
1173
1601
|
type: "unchecked_constraint",
|
|
@@ -1189,10 +1617,10 @@ function Ue(e, t = {}) {
|
|
|
1189
1617
|
message: `Logical constraint "${r}" evaluated to false.`
|
|
1190
1618
|
});
|
|
1191
1619
|
}
|
|
1192
|
-
let
|
|
1620
|
+
let T = y === e.variables.length && b === e.boolVars.length && x === e.constraints.length && S === e.logicalConstraints.length && f.length === 0 && p.length === 0;
|
|
1193
1621
|
return {
|
|
1194
|
-
valid:
|
|
1195
|
-
complete:
|
|
1622
|
+
valid: T && u.length === 0,
|
|
1623
|
+
complete: T,
|
|
1196
1624
|
violations: u,
|
|
1197
1625
|
violationsByName: d,
|
|
1198
1626
|
uncheckedConstraints: f,
|
|
@@ -1212,10 +1640,10 @@ function Ue(e, t = {}) {
|
|
|
1212
1640
|
}
|
|
1213
1641
|
//#endregion
|
|
1214
1642
|
//#region src/modeler/big-m.ts
|
|
1215
|
-
function
|
|
1643
|
+
function rt(e, t) {
|
|
1216
1644
|
if (typeof e != "number" || !Number.isFinite(e) || e <= 0) throw Error(`Invalid Big-M value for indicator constraint "${t}": expected a finite positive number, received ${e}.`);
|
|
1217
1645
|
}
|
|
1218
|
-
function
|
|
1646
|
+
function it(e, t, n) {
|
|
1219
1647
|
let r = e.constant === void 0 ? 0 : e.constant;
|
|
1220
1648
|
if (!Number.isFinite(t) || !Number.isFinite(r)) throw Error(`Cannot derive safe Big-M for indicator constraint "${n}": constraint contains non-finite constant (${r}) or right-hand side (${t}).`);
|
|
1221
1649
|
let i = t - r;
|
|
@@ -1226,8 +1654,8 @@ function Ge(e, t, n) {
|
|
|
1226
1654
|
}
|
|
1227
1655
|
return i;
|
|
1228
1656
|
}
|
|
1229
|
-
function
|
|
1230
|
-
let r =
|
|
1657
|
+
function at(e, t, n) {
|
|
1658
|
+
let r = it(e, t, n), i = 0;
|
|
1231
1659
|
for (let [t, r] of e.terms) {
|
|
1232
1660
|
if (r === 0) continue;
|
|
1233
1661
|
let e;
|
|
@@ -1247,8 +1675,8 @@ function q(e, t, n) {
|
|
|
1247
1675
|
if (!Number.isFinite(o) || !Number.isFinite(r + o)) throw Error(`Cannot derive safe Big-M for indicator constraint "${n}": derived Big-M (${o}) or shifted right-hand side overflows finite floating-point range. Please rescale coefficients/bounds or specify a finite { M } explicitly.`);
|
|
1248
1676
|
return o;
|
|
1249
1677
|
}
|
|
1250
|
-
function
|
|
1251
|
-
let r =
|
|
1678
|
+
function ot(e, t, n) {
|
|
1679
|
+
let r = it(e, t, n), i = 0;
|
|
1252
1680
|
for (let [t, r] of e.terms) {
|
|
1253
1681
|
if (r === 0) continue;
|
|
1254
1682
|
let e;
|
|
@@ -1271,7 +1699,7 @@ function J(e, t, n) {
|
|
|
1271
1699
|
function Y(e, t, n, r, i, a, o) {
|
|
1272
1700
|
if (!(e instanceof b && (e.type === "Binary" || e.type === "Integer" && e.lb === 0 && e.ub === 1))) throw Error(`Indicator variable "${e?.name ?? String(e)}" must be a Binary variable (or Integer variable with bounds [0, 1]), received "${e?.type ?? typeof e}" with bounds [${e?.lb}, ${e?.ub}].`);
|
|
1273
1701
|
if (!Number.isFinite(e.lb) || !Number.isFinite(e.ub) || e.lb < 0 || e.ub > 1 || e.lb > e.ub) throw Error(`Binary indicator variable "${e.name}" has invalid bounds [${e.lb}, ${e.ub}]. Binary variables must stay within [0, 1] with lb <= ub.`);
|
|
1274
|
-
let s =
|
|
1702
|
+
let s = it(t, r, o);
|
|
1275
1703
|
if (i !== 0 && i !== 1) throw Error(`Invalid activeValue: ${i}. Indicator activation value must be 0 or 1.`);
|
|
1276
1704
|
if (n !== "<=" && n !== ">=") throw Error(`Invalid indicator sense for "${o}": expected "<=" or ">=", received "${String(n)}".`);
|
|
1277
1705
|
if (!Number.isFinite(a) || a < 0) throw Error(`Invalid Big-M value for indicator constraint "${o}": expected a finite non-negative number, received ${a}.`);
|
|
@@ -1282,16 +1710,16 @@ function Y(e, t, n, r, i, a, o) {
|
|
|
1282
1710
|
let d = /* @__PURE__ */ new Map();
|
|
1283
1711
|
for (let [e, n] of t.terms) n !== 0 && d.set(e, n);
|
|
1284
1712
|
return l !== 0 && d.set(e, l), {
|
|
1285
|
-
lhs: new
|
|
1713
|
+
lhs: new A(d, 0),
|
|
1286
1714
|
rhs: u
|
|
1287
1715
|
};
|
|
1288
1716
|
}
|
|
1289
1717
|
//#endregion
|
|
1290
1718
|
//#region src/modeler/linearizer.ts
|
|
1291
1719
|
function X(e, t = 0) {
|
|
1292
|
-
return new
|
|
1720
|
+
return new A(new Map(e), t);
|
|
1293
1721
|
}
|
|
1294
|
-
function
|
|
1722
|
+
function st(e, t, r, i) {
|
|
1295
1723
|
let a = [...e], s = [...r], l = /* @__PURE__ */ new Map(), u = /* @__PURE__ */ new Map();
|
|
1296
1724
|
for (let e of r) u.set(e, e.name);
|
|
1297
1725
|
let d = new Set(e.map((e) => e.name)), f = 0;
|
|
@@ -1314,7 +1742,7 @@ function Ke(e, t, r, i) {
|
|
|
1314
1742
|
l.set(e, n), a.push(n);
|
|
1315
1743
|
}
|
|
1316
1744
|
function m(e, t) {
|
|
1317
|
-
if (e instanceof
|
|
1745
|
+
if (e instanceof ct) return e.wrappedVar;
|
|
1318
1746
|
if (e instanceof o) {
|
|
1319
1747
|
let n = l.get(e);
|
|
1320
1748
|
if (!n) throw Error(`Unregistered BoolVar "${e.name}" in logical constraint "${t}".`);
|
|
@@ -1536,7 +1964,7 @@ function Ke(e, t, r, i) {
|
|
|
1536
1964
|
}
|
|
1537
1965
|
if (i instanceof n) {
|
|
1538
1966
|
if (i.op === "and") {
|
|
1539
|
-
for (let e = 0; e < i.children.length; e++) _(new n("implies", [d instanceof b ? new
|
|
1967
|
+
for (let e = 0; e < i.children.length; e++) _(new n("implies", [d instanceof b ? new ct(d) : r, i.children[e]]), `${t}_and_${e}`);
|
|
1540
1968
|
return;
|
|
1541
1969
|
}
|
|
1542
1970
|
if (i.op === "not") {
|
|
@@ -1555,10 +1983,10 @@ function Ke(e, t, r, i) {
|
|
|
1555
1983
|
function v(e, t, n, r, i) {
|
|
1556
1984
|
let { lhs: a, sense: o, rhs: l } = t;
|
|
1557
1985
|
if (o === "<=" || o === ">=") {
|
|
1558
|
-
let t = Y(e, a, o, l, n, o === "<=" ?
|
|
1986
|
+
let t = Y(e, a, o, l, n, o === "<=" ? at(a, l, r) : ot(a, l, r), r), d = new c(t.lhs, o, t.rhs, r);
|
|
1559
1987
|
s.push(d), u.set(d, i);
|
|
1560
1988
|
} else {
|
|
1561
|
-
let t =
|
|
1989
|
+
let t = at(a, l, `${r}_le`), o = ot(a, l, `${r}_ge`), d = Y(e, a, "<=", l, n, t, `${r}_le`), f = Y(e, a, ">=", l, n, o, `${r}_ge`), p = new c(d.lhs, "<=", d.rhs, `${r}_le`), m = new c(f.lhs, ">=", f.rhs, `${r}_ge`);
|
|
1562
1990
|
s.push(p, m), u.set(p, i), u.set(m, i);
|
|
1563
1991
|
}
|
|
1564
1992
|
}
|
|
@@ -1569,7 +1997,7 @@ function Ke(e, t, r, i) {
|
|
|
1569
1997
|
constraintToOriginalName: u
|
|
1570
1998
|
};
|
|
1571
1999
|
}
|
|
1572
|
-
var
|
|
2000
|
+
var ct = class extends o {
|
|
1573
2001
|
wrappedVar;
|
|
1574
2002
|
constructor(e) {
|
|
1575
2003
|
super(e.name), this.wrappedVar = e;
|
|
@@ -1577,7 +2005,7 @@ var qe = class extends o {
|
|
|
1577
2005
|
};
|
|
1578
2006
|
//#endregion
|
|
1579
2007
|
//#region src/decomposition/column-generation.ts
|
|
1580
|
-
async function
|
|
2008
|
+
async function lt(e) {
|
|
1581
2009
|
let t = e.master;
|
|
1582
2010
|
if (!t) throw Error("A master Model must be provided for solveColumnGeneration.");
|
|
1583
2011
|
let { pricing: n, maxIterations: r = 100, reducedCostTolerance: i = 1e-6, finalizeAsMIP: a = !0, solver: o, solverOptions: s, onIteration: c } = e, l = t.sense.toLowerCase() === "maximize", u = n;
|
|
@@ -1648,7 +2076,7 @@ async function Je(e) {
|
|
|
1648
2076
|
}
|
|
1649
2077
|
//#endregion
|
|
1650
2078
|
//#region src/modeler/objective.ts
|
|
1651
|
-
function
|
|
2079
|
+
function ut(e, t) {
|
|
1652
2080
|
let n = e;
|
|
1653
2081
|
if (n instanceof c || typeof n == "object" && n && "sense" in n && "lhs" in n && "rhs" in n) throw Error(`Cannot use a Constraint as an objective in ${t}(). Use m.constraint(expr\`... <= ...\`) for constraints, or supply an unconstrained arithmetic expression to ${t}(), e.g. m.objective(${t}(expr\`30 * \${x}\`)).`);
|
|
1654
2082
|
if (typeof n == "string" && /(<=|>=|==|=<|=>|=)/.test(n)) throw Error(`Cannot use a comparison constraint as an objective in ${t}(). Use m.constraint(expr\`... <= ...\`) for constraints, or supply an unconstrained arithmetic expression to ${t}(), e.g. m.objective(${t}(expr\`30 * \${x}\`)).`);
|
|
@@ -1656,27 +2084,27 @@ function Ye(e, t) {
|
|
|
1656
2084
|
if (!Number.isFinite(n)) throw Error(`Invalid non-finite objective constant "${n}" passed to ${t}().`);
|
|
1657
2085
|
return n;
|
|
1658
2086
|
}
|
|
1659
|
-
if (typeof n == "string" || n instanceof
|
|
2087
|
+
if (typeof n == "string" || n instanceof A || n instanceof k || n instanceof b || typeof n == "object" && n && ("terms" in n && n.terms instanceof Map && typeof n.constant == "number" || "quadTerms" in n && n.quadTerms instanceof Map && "linearTerms" in n && n.linearTerms instanceof Map && typeof n.constant == "number" || "id" in n && "name" in n && "lb" in n && "ub" in n)) return n;
|
|
1660
2088
|
throw Error(`Invalid expression passed to ${t}(): expected a finite number, Var, LinExpr, QuadExpr, or arithmetic expression string.`);
|
|
1661
2089
|
}
|
|
1662
|
-
function
|
|
2090
|
+
function dt(e) {
|
|
1663
2091
|
return {
|
|
1664
|
-
expr:
|
|
2092
|
+
expr: ut(e, "maximize"),
|
|
1665
2093
|
sense: "Maximize"
|
|
1666
2094
|
};
|
|
1667
2095
|
}
|
|
1668
|
-
function
|
|
2096
|
+
function ft(e) {
|
|
1669
2097
|
return {
|
|
1670
|
-
expr:
|
|
2098
|
+
expr: ut(e, "minimize"),
|
|
1671
2099
|
sense: "Minimize"
|
|
1672
2100
|
};
|
|
1673
2101
|
}
|
|
1674
2102
|
//#endregion
|
|
1675
2103
|
//#region src/decomposition/benders.ts
|
|
1676
|
-
function
|
|
2104
|
+
function pt(e) {
|
|
1677
2105
|
return !!(e && typeof e == "object" && "constraints" in e && "variables" in e && typeof e.solve == "function");
|
|
1678
2106
|
}
|
|
1679
|
-
async function
|
|
2107
|
+
async function mt(e, t, n, r, i, a, o, s, l = 1) {
|
|
1680
2108
|
let u = [];
|
|
1681
2109
|
for (let r = 0; r < t.length; r++) {
|
|
1682
2110
|
let a = t[r], o = a.lhs, s = a.sense, d = a.rhs - (o.constant || 0), f = [], p = [], m = 0;
|
|
@@ -1685,7 +2113,7 @@ async function $e(e, t, n, r, i, a, o, s, l = 1) {
|
|
|
1685
2113
|
let n = i[e.name] ?? 0;
|
|
1686
2114
|
m += t * n;
|
|
1687
2115
|
} else f.push([e, t]);
|
|
1688
|
-
let h = d - m, g = new
|
|
2116
|
+
let h = d - m, g = new A();
|
|
1689
2117
|
for (let [e, t] of f) g.addTerm(e, t);
|
|
1690
2118
|
let _ = `__benders_link_${l}_${r}`, v = new c(g, s, h, _);
|
|
1691
2119
|
e.constraint(v), u.push({
|
|
@@ -1721,7 +2149,7 @@ async function $e(e, t, n, r, i, a, o, s, l = 1) {
|
|
|
1721
2149
|
}
|
|
1722
2150
|
}
|
|
1723
2151
|
}
|
|
1724
|
-
let v = new
|
|
2152
|
+
let v = new A();
|
|
1725
2153
|
v.addTerm(r, 1);
|
|
1726
2154
|
for (let [e, t] of g.entries()) Math.abs(t) > 1e-12 && v.addTerm(e, t);
|
|
1727
2155
|
let y = new c(v, a ? ">=" : "<=", h, `__benders_opt_cut_${l}`), b = (n.objectiveValue ?? 0) - p + f;
|
|
@@ -1732,7 +2160,7 @@ async function $e(e, t, n, r, i, a, o, s, l = 1) {
|
|
|
1732
2160
|
subproblemObjective: b
|
|
1733
2161
|
};
|
|
1734
2162
|
}
|
|
1735
|
-
async function
|
|
2163
|
+
async function ht(e) {
|
|
1736
2164
|
let t = e.master;
|
|
1737
2165
|
if (!t) throw Error("A master Model must be provided for solveBenders.");
|
|
1738
2166
|
let { subproblem: n, maxIterations: r = 100, gapTolerance: i = 1e-4, solver: a, solverOptions: o, onIteration: s } = e, c;
|
|
@@ -1756,10 +2184,10 @@ async function et(e) {
|
|
|
1756
2184
|
master: t
|
|
1757
2185
|
}), x = [];
|
|
1758
2186
|
if (b) {
|
|
1759
|
-
if (
|
|
2187
|
+
if (pt(b)) {
|
|
1760
2188
|
let n = b, r = n.linkingConstraints;
|
|
1761
|
-
c || (c = t.continuous("__benders_theta", { lb: 0 }), v ? t.objective(
|
|
1762
|
-
let i = await
|
|
2189
|
+
c || (c = t.continuous("__benders_theta", { lb: 0 }), v ? t.objective(ft(t.objectiveExpr.add(c))) : t.objective(dt(t.objectiveExpr.sub(c))));
|
|
2190
|
+
let i = await mt(n, r, t, c, y, v, a, o, e);
|
|
1763
2191
|
i && x.push(i);
|
|
1764
2192
|
} else Array.isArray(b) ? x.push(...b) : x.push(b);
|
|
1765
2193
|
}
|
|
@@ -1815,13 +2243,13 @@ function Z(e) {
|
|
|
1815
2243
|
function Q(e) {
|
|
1816
2244
|
return typeof e == "object" && !!e && !Array.isArray(e) && !(e instanceof String) && typeof e[Symbol.iterator] != "function";
|
|
1817
2245
|
}
|
|
1818
|
-
function
|
|
2246
|
+
function gt(e) {
|
|
1819
2247
|
return e instanceof b || typeof e == "object" && !!e && !(e instanceof r) && !(e instanceof c) && "id" in e && "name" in e && "lb" in e && "ub" in e && !("terms" in e) && !("quadTerms" in e) && !("sense" in e);
|
|
1820
2248
|
}
|
|
1821
2249
|
function $(e, t) {
|
|
1822
2250
|
if (!Number.isInteger(e) || e < 0) throw Error(`Invalid count (${e}) for ${t} variable creation: count must be a non-negative integer.`);
|
|
1823
2251
|
}
|
|
1824
|
-
var
|
|
2252
|
+
var _t = class {
|
|
1825
2253
|
name;
|
|
1826
2254
|
_sense;
|
|
1827
2255
|
_objectiveExpr;
|
|
@@ -1864,13 +2292,13 @@ var nt = class {
|
|
|
1864
2292
|
n = typeof e == "string" ? {
|
|
1865
2293
|
...t,
|
|
1866
2294
|
name: e
|
|
1867
|
-
} : e, this.name = n.name || "OptimizationModel", this._sense =
|
|
2295
|
+
} : e, this.name = n.name || "OptimizationModel", this._sense = ye(n.sense), this._objectiveExpr = new A(), n.solver && (this.solver = E(n.solver));
|
|
1868
2296
|
}
|
|
1869
2297
|
get sense() {
|
|
1870
2298
|
return this._sense;
|
|
1871
2299
|
}
|
|
1872
2300
|
set sense(e) {
|
|
1873
|
-
let t =
|
|
2301
|
+
let t = ye(e);
|
|
1874
2302
|
this._sense !== t && (this._sense = t, this.recordModelMutation());
|
|
1875
2303
|
}
|
|
1876
2304
|
get objectiveExpr() {
|
|
@@ -2162,30 +2590,31 @@ var nt = class {
|
|
|
2162
2590
|
}
|
|
2163
2591
|
applyObjective(e, t) {
|
|
2164
2592
|
if (e instanceof c || typeof e == "object" && e && "sense" in e && "lhs" in e && "rhs" in e || typeof e == "string" && /(<=|>=|==|=<|=>|=)/.test(e)) throw Error(typeof e == "string" ? "Cannot use a comparison constraint as an objective. Did you mean to use m.constraint(expr`... <= ...`)? To set an objective, wrap an unconstrained expression with maximize(...) or minimize(...), e.g. m.objective(maximize(expr`30 * ${x}`))." : "Cannot use a Constraint as an objective. Did you mean to use m.constraint(expr`... <= ...`)? To set an objective, wrap an unconstrained expression with maximize(...) or minimize(...), e.g. m.objective(maximize(expr`30 * ${x}`)).");
|
|
2165
|
-
let n = t === void 0 ? this.sense :
|
|
2166
|
-
if (typeof e == "string") r =
|
|
2167
|
-
else if (typeof e == "number") r = new
|
|
2593
|
+
let n = t === void 0 ? this.sense : ye(t), r;
|
|
2594
|
+
if (typeof e == "string") r = Ne(e, (e) => this.getVarByName(e));
|
|
2595
|
+
else if (typeof e == "number") r = new A(/* @__PURE__ */ new Map(), e);
|
|
2596
|
+
else if (e instanceof k) r = e.clone();
|
|
2168
2597
|
else if (e instanceof A) r = e.clone();
|
|
2169
|
-
else if (e instanceof j) r = e.clone();
|
|
2170
2598
|
else if (typeof e == "object" && e && "quadTerms" in e && e.quadTerms instanceof Map && "linearTerms" in e && e.linearTerms instanceof Map && typeof e.constant == "number") {
|
|
2171
2599
|
let t = e;
|
|
2172
|
-
r = new
|
|
2600
|
+
r = new k(t.linearTerms, t.quadTerms, t.constant);
|
|
2173
2601
|
} else if (typeof e == "object" && e && "terms" in e && e.terms instanceof Map && typeof e.constant == "number") {
|
|
2174
2602
|
let t = e;
|
|
2175
|
-
r = new
|
|
2176
|
-
} else if (e instanceof b || typeof e == "object" && e && "id" in e && "name" in e && "lb" in e && "ub" in e) r = new
|
|
2603
|
+
r = new A(t.terms, t.constant);
|
|
2604
|
+
} else if (e instanceof b || typeof e == "object" && e && "id" in e && "name" in e && "lb" in e && "ub" in e) r = new A(/* @__PURE__ */ new Map([[e, 1]]), 0);
|
|
2177
2605
|
else throw Error("Invalid objective expression: expected a finite number, Var, LinExpr, QuadExpr, or arithmetic expression string.");
|
|
2178
|
-
|
|
2606
|
+
te(r, this.name, (e) => this.varMapByName.get(e.name) === e), this._sense = n, this._objectiveExpr = r, this.recordModelMutation();
|
|
2179
2607
|
}
|
|
2180
2608
|
constraint(e, t) {
|
|
2181
2609
|
if (Array.isArray(e)) return this.runTransactionalBatch(() => {
|
|
2182
|
-
|
|
2183
|
-
|
|
2610
|
+
let t = [];
|
|
2611
|
+
for (let n of e) t.push(this.constraint(n));
|
|
2612
|
+
return t;
|
|
2184
2613
|
});
|
|
2185
|
-
if (e instanceof
|
|
2186
|
-
if (e instanceof
|
|
2187
|
-
if (
|
|
2188
|
-
return this.addConstraint(e, t)
|
|
2614
|
+
if (e instanceof k || typeof e == "object" && e && "quadTerms" in e) throw Error("Constraint must contain a comparison operator (<=, >=, ==): cannot add a quadratic expression as a constraint.");
|
|
2615
|
+
if (e instanceof A || typeof e == "object" && e && "terms" in e) throw Error("Constraint must contain a comparison operator (<=, >=, ==): cannot add a linear expression without a comparison operator. Use expr`... <= ...` or .le(), .ge(), .eq().");
|
|
2616
|
+
if (gt(e)) throw Error(`Cannot add numeric variable "${e.name}" directly as a constraint without a comparison operator (<=, >=, ==). Numeric variables require a comparison such as expr\`\${x} <= 10\` or x.le(10), whereas propositional BoolVar variables must be created with m.bool().`);
|
|
2617
|
+
return this.addConstraint(e, t);
|
|
2189
2618
|
}
|
|
2190
2619
|
getValues(e) {
|
|
2191
2620
|
if (e == null) return null;
|
|
@@ -2199,9 +2628,9 @@ var nt = class {
|
|
|
2199
2628
|
return null;
|
|
2200
2629
|
}
|
|
2201
2630
|
addConstraint(e, t) {
|
|
2202
|
-
if (e instanceof
|
|
2203
|
-
if (e instanceof
|
|
2204
|
-
if (
|
|
2631
|
+
if (e instanceof k || typeof e == "object" && e && "quadTerms" in e) throw Error("Constraint must contain a comparison operator (<=, >=, ==): cannot add a quadratic expression as a constraint.");
|
|
2632
|
+
if (e instanceof A || typeof e == "object" && e && "terms" in e) throw Error("Constraint must contain a comparison operator (<=, >=, ==): cannot add a linear expression without a comparison operator. Use expr`... <= ...` or .le(), .ge(), .eq().");
|
|
2633
|
+
if (gt(e)) throw Error(`Cannot add numeric variable "${e.name}" directly as a constraint without a comparison operator (<=, >=, ==). Numeric variables require a comparison such as expr\`\${x} <= 10\` or x.le(10), whereas propositional BoolVar variables must be created with m.bool().`);
|
|
2205
2634
|
if (e instanceof r) {
|
|
2206
2635
|
let n = typeof t == "string" && t.trim().length > 0 ? t.trim() : void 0;
|
|
2207
2636
|
if (n !== void 0) {
|
|
@@ -2214,14 +2643,14 @@ var nt = class {
|
|
|
2214
2643
|
} catch (e) {
|
|
2215
2644
|
throw n === void 0 && (this.logicalConstrCounter = r), e;
|
|
2216
2645
|
}
|
|
2217
|
-
|
|
2646
|
+
D(e, this, this.batchDepth > 0 ? this.batchUndoActions : void 0);
|
|
2218
2647
|
let a = {
|
|
2219
2648
|
name: i,
|
|
2220
2649
|
expr: e
|
|
2221
2650
|
};
|
|
2222
2651
|
return this.logicalConstraints.push(a), this.logicalConstrMapByName.set(i, a), this.recordModelMutation(), e;
|
|
2223
2652
|
}
|
|
2224
|
-
let n = typeof e == "string" ?
|
|
2653
|
+
let n = typeof e == "string" ? Ie(e, (e) => this.getVarByName(e), t) : e;
|
|
2225
2654
|
if (typeof n != "object" || !n || !("sense" in n) || !("lhs" in n) || !("rhs" in n)) throw Error("Invalid constraint passed to m.constraint(...): expected a linear Constraint (e.g. expr`${x} <= 10` or x.le(10)), a propositional BoolVar/BoolExpr (created with m.bool()), or an array of constraints.");
|
|
2226
2655
|
n.ownerModel && n.ownerModel !== this && (h(n, this.name, (e) => this.varMapByName.get(e.name) === e, n.name), p(n, this));
|
|
2227
2656
|
let i = typeof t == "string" && t.trim().length > 0 ? t.trim() : n.hasExplicitName ? n.name.trim() : void 0;
|
|
@@ -2244,7 +2673,7 @@ var nt = class {
|
|
|
2244
2673
|
if (e instanceof r) throw Error("Invalid binary variable passed to indicator(): received a propositional BoolVar/BoolExpr, but m.indicator() requires a 0-1 numeric binary Var created with m.binary(). For general boolean logic with m.bool(), use m.constraint(implies(flag, expr`${x} <= 10`)) with the Z3 solver.");
|
|
2245
2674
|
if (!(e instanceof b)) throw Error("Invalid binary variable passed to indicator(). Expected a 0-1 binary Var instance created with m.binary() (or use implies(flag, ...) with m.bool() and the Z3 solver for general boolean logic).");
|
|
2246
2675
|
if (!(e.type === "Binary" || e.type === "Integer" && e.lb === 0 && e.ub === 1)) throw Error(`Invalid variable "${e.name}" passed to indicator(): variable has type "${e.type}" with bounds [${e.lb}, ${e.ub}]. Indicator activation variable must be a Binary variable created with m.binary() (or Integer variable with bounds [0, 1]).`);
|
|
2247
|
-
if (t instanceof
|
|
2676
|
+
if (t instanceof k || typeof t == "object" && t && "quadTerms" in t) throw Error("Quadratic expressions or constraints are not supported in indicator constraints (only linear Constraints with <=, >=, or == such as expr`${x} <= 10` are allowed).");
|
|
2248
2677
|
if (typeof t != "object" || !t || !("sense" in t) || !("lhs" in t) || !("rhs" in t)) throw Error("Invalid constraint expression passed to indicator(). Expression must include a comparison operator (<=, >=, ==), e.g. m.indicator(b, expr`${x} <= 10`). For general boolean logic (such as BoolVar or implies/and/or/not), use m.constraint(implies(flag, ...)) with the Z3 solver.");
|
|
2249
2678
|
let i = t, a = n.activeValue === void 0 ? 1 : n.activeValue;
|
|
2250
2679
|
if (a !== 0 && a !== 1) throw Error(`Invalid activeValue: ${a}. Indicator activeValue must be 0 or 1.`);
|
|
@@ -2260,15 +2689,15 @@ var nt = class {
|
|
|
2260
2689
|
try {
|
|
2261
2690
|
if ((u === "<=" || u === "==") && (this.hasConstraintName(p) || this.hasBoolVarName(p))) throw Error(this.hasBoolVarName(p) ? `Constraint name '${p}' conflicts with an existing Boolean variable in model` : `Constraint with name '${p}' already exists in model`);
|
|
2262
2691
|
if ((u === ">=" || u === "==") && (this.hasConstraintName(m) || this.hasBoolVarName(m))) throw Error(this.hasBoolVarName(m) ? `Constraint name '${m}' conflicts with an existing Boolean variable in model` : `Constraint with name '${m}' already exists in model`);
|
|
2263
|
-
n.M !== void 0 &&
|
|
2692
|
+
n.M !== void 0 && rt(n.M, f);
|
|
2264
2693
|
let t = (e) => this.varMapByName.get(e.name) === e;
|
|
2265
2694
|
for (let [e] of l.terms) if (!t(e)) throw Error(`Variable "${e.name}" in constraint "${f}" does not belong to model "${this.name}". All variables must belong to the model by object identity.`);
|
|
2266
2695
|
if (u === "<=" || u === "==") {
|
|
2267
|
-
let t = Y(e, l, "<=", d, a, n.M === void 0 ?
|
|
2696
|
+
let t = Y(e, l, "<=", d, a, n.M === void 0 ? at(l, d, p) : n.M, p);
|
|
2268
2697
|
g.push([new c(t.lhs, "<=", t.rhs, p), p]);
|
|
2269
2698
|
}
|
|
2270
2699
|
if (u === ">=" || u === "==") {
|
|
2271
|
-
let t = Y(e, l, ">=", d, a, n.M === void 0 ?
|
|
2700
|
+
let t = Y(e, l, ">=", d, a, n.M === void 0 ? ot(l, d, m) : n.M, m);
|
|
2272
2701
|
g.push([new c(t.lhs, ">=", t.rhs, m), m]);
|
|
2273
2702
|
}
|
|
2274
2703
|
for (let [e, n] of g) h(e, this.name, t, n);
|
|
@@ -2289,7 +2718,7 @@ var nt = class {
|
|
|
2289
2718
|
return e;
|
|
2290
2719
|
}
|
|
2291
2720
|
validateIntegrity() {
|
|
2292
|
-
|
|
2721
|
+
O(this);
|
|
2293
2722
|
}
|
|
2294
2723
|
ensureExecutionValid(e, t, n) {
|
|
2295
2724
|
if (this.modelRevision !== e) throw this.activeOperationId === t && this.clearSolutionFields(), Error(`Model "${this.name}" was modified while ${n}() was in progress; stale solver result was discarded.`);
|
|
@@ -2309,6 +2738,9 @@ var nt = class {
|
|
|
2309
2738
|
let t = e.status, n = e.terminationStatus ?? t, r = e.values || {}, i = e.boolValues || {};
|
|
2310
2739
|
return t !== "Infeasible" && t !== "Unsat" && t !== "Unbounded" && t !== "Error" && t !== "NotSolved" && n !== "Infeasible" && n !== "Unsat" && n !== "Unbounded" && n !== "InfeasibleOrUnbounded" && n !== "Error" && n !== "NotSolved" && (t === "Optimal" || t === "Feasible" || t === "Sat" || n === "Optimal" || n === "Feasible" || n === "Sat" || typeof e.objectiveValue == "number" && Number.isFinite(e.objectiveValue) || Object.values(r).some((e) => typeof e == "number" && Number.isFinite(e) || typeof e == "boolean") || Object.values(i).some((e) => typeof e == "boolean"));
|
|
2311
2740
|
}
|
|
2741
|
+
normalizePrimalValue(e, t, n, r) {
|
|
2742
|
+
return (r.relax ?? n.options?.relax) !== !0 && (e.type === "Integer" || e.type === "Binary") && Math.abs(t - Math.round(t)) <= 1e-5 ? Math.round(t) + 0 : t;
|
|
2743
|
+
}
|
|
2312
2744
|
resolveSolverAndOptions(e, t) {
|
|
2313
2745
|
if (e && typeof e == "object" && "solve" in e && typeof e.solve == "function") {
|
|
2314
2746
|
let { solver: n, ...r } = t || {};
|
|
@@ -2327,16 +2759,16 @@ var nt = class {
|
|
|
2327
2759
|
if (typeof e == "string") {
|
|
2328
2760
|
let { solver: n, ...r } = t || {};
|
|
2329
2761
|
return {
|
|
2330
|
-
activeSolver:
|
|
2762
|
+
activeSolver: E(e, r),
|
|
2331
2763
|
activeOptions: r
|
|
2332
2764
|
};
|
|
2333
2765
|
}
|
|
2334
2766
|
let { solver: n, ...r } = e || {};
|
|
2335
2767
|
return n ? {
|
|
2336
|
-
activeSolver:
|
|
2768
|
+
activeSolver: E(n, r),
|
|
2337
2769
|
activeOptions: r
|
|
2338
2770
|
} : {
|
|
2339
|
-
activeSolver: this.solver ||
|
|
2771
|
+
activeSolver: this.solver || E(),
|
|
2340
2772
|
activeOptions: r
|
|
2341
2773
|
};
|
|
2342
2774
|
}
|
|
@@ -2354,9 +2786,9 @@ var nt = class {
|
|
|
2354
2786
|
let n = ++this.activeOperationId, r = this.modelRevision;
|
|
2355
2787
|
this.clearSolutionFields(), this.validateIntegrity();
|
|
2356
2788
|
let { activeSolver: i, activeOptions: a } = this.resolveSolverAndOptions(e, t);
|
|
2357
|
-
if (this._objectiveExpr instanceof
|
|
2789
|
+
if (this._objectiveExpr instanceof k && this._objectiveExpr.quadTerms.size > 0 && i.capabilities?.supportsQP === !1) throw Error(`Solver "${i.name}" does not support quadratic objectives (QP). Please use a QP-capable solver such as HiGHS or Clarabel.`);
|
|
2358
2790
|
let o, s = null;
|
|
2359
|
-
if (this.logicalConstraints.length > 0 && i.capabilities?.supportsSMT !== !0 ? (s =
|
|
2791
|
+
if (this.logicalConstraints.length > 0 && i.capabilities?.supportsSMT !== !0 ? (s = st(this.variables, this.boolVars, this.constraints, this.logicalConstraints), o = {
|
|
2360
2792
|
name: this.name,
|
|
2361
2793
|
sense: this.sense,
|
|
2362
2794
|
objective: this._objectiveExpr,
|
|
@@ -2395,7 +2827,11 @@ var nt = class {
|
|
|
2395
2827
|
if (this.values.clear(), u && l.values) {
|
|
2396
2828
|
for (let [e, t] of Object.entries(l.values)) if (typeof t == "number" && Number.isFinite(t)) {
|
|
2397
2829
|
let n = this.getVarByName(e), r = this.getBoolVarByName(e);
|
|
2398
|
-
|
|
2830
|
+
if (n) {
|
|
2831
|
+
let e = this.normalizePrimalValue(n, t, i, a);
|
|
2832
|
+
n.value = e, f(p, n.name, e), this.values.set(n.name, e);
|
|
2833
|
+
}
|
|
2834
|
+
r && (r.value = !!t, this.values.set(r.name, t)), !n && !r && this.values.set(e, t);
|
|
2399
2835
|
} else if (typeof t == "boolean") {
|
|
2400
2836
|
let n = t, r = this.getBoolVarByName(e);
|
|
2401
2837
|
r ? (r.value = n, f(p, r.name, +!!n), this.values.set(r.name, n)) : this.values.set(e, n);
|
|
@@ -2481,8 +2917,12 @@ var nt = class {
|
|
|
2481
2917
|
if (e.values && e.status === "Sat") {
|
|
2482
2918
|
for (let [n, r] of Object.entries(e.values)) if (typeof r == "number") {
|
|
2483
2919
|
if (!Number.isFinite(r)) continue;
|
|
2484
|
-
let e = this.getVarByName(n),
|
|
2485
|
-
|
|
2920
|
+
let e = this.getVarByName(n), o = this.getBoolVarByName(n);
|
|
2921
|
+
if (e) {
|
|
2922
|
+
let n = this.normalizePrimalValue(e, r, i, a);
|
|
2923
|
+
e.value = n, this.values.set(e.name, n), s(t, e.name, n);
|
|
2924
|
+
}
|
|
2925
|
+
o && (o.value = !!r, this.values.set(o.name, o.value), s(t, o.name, o.value)), !e && !o && (this.values.set(n, r), s(t, n, r));
|
|
2486
2926
|
} else if (typeof r == "boolean") {
|
|
2487
2927
|
let e = this.getBoolVarByName(n);
|
|
2488
2928
|
e ? (e.value = r, this.values.set(e.name, r), s(t, e.name, r)) : (this.values.set(n, r), s(t, n, r));
|
|
@@ -2496,7 +2936,7 @@ var nt = class {
|
|
|
2496
2936
|
runTime: this.runTime
|
|
2497
2937
|
};
|
|
2498
2938
|
}
|
|
2499
|
-
let u = this.logicalConstraints.length > 0 || this.boolVars.length > 0 ?
|
|
2939
|
+
let u = this.logicalConstraints.length > 0 || this.boolVars.length > 0 ? st(this.variables, this.boolVars, this.constraints, this.logicalConstraints) : {
|
|
2500
2940
|
variables: [...this.variables],
|
|
2501
2941
|
constraints: [...this.constraints],
|
|
2502
2942
|
boolVarToBinary: /* @__PURE__ */ new Map(),
|
|
@@ -2512,7 +2952,7 @@ var nt = class {
|
|
|
2512
2952
|
ub: Infinity
|
|
2513
2953
|
});
|
|
2514
2954
|
f.push(r), p.push([r, 1]), m.set(r, t);
|
|
2515
|
-
let a = n.clone().add(new
|
|
2955
|
+
let a = n.clone().add(new A(/* @__PURE__ */ new Map([[r, -1]]), 0));
|
|
2516
2956
|
d.push(new c(a, "<=", i, `${e.name}_slack`));
|
|
2517
2957
|
} else if (r === ">=") {
|
|
2518
2958
|
let r = new b({
|
|
@@ -2522,7 +2962,7 @@ var nt = class {
|
|
|
2522
2962
|
ub: Infinity
|
|
2523
2963
|
});
|
|
2524
2964
|
f.push(r), p.push([r, 1]), m.set(r, t);
|
|
2525
|
-
let a = n.clone().add(new
|
|
2965
|
+
let a = n.clone().add(new A(/* @__PURE__ */ new Map([[r, 1]]), 0));
|
|
2526
2966
|
d.push(new c(a, ">=", i, `${e.name}_slack`));
|
|
2527
2967
|
} else {
|
|
2528
2968
|
let r = new b({
|
|
@@ -2537,14 +2977,14 @@ var nt = class {
|
|
|
2537
2977
|
ub: Infinity
|
|
2538
2978
|
});
|
|
2539
2979
|
f.push(r, a), p.push([r, 1], [a, 1]), m.set(r, t), m.set(a, t);
|
|
2540
|
-
let o = n.clone().add(new
|
|
2980
|
+
let o = n.clone().add(new A(/* @__PURE__ */ new Map([[r, -1], [a, 1]]), 0));
|
|
2541
2981
|
d.push(new c(o, "==", i, `${e.name}_slack`));
|
|
2542
2982
|
}
|
|
2543
2983
|
}
|
|
2544
2984
|
let g = {
|
|
2545
2985
|
name: `${this.name}_elastic_check`,
|
|
2546
2986
|
sense: "Minimize",
|
|
2547
|
-
objective: new
|
|
2987
|
+
objective: new A(new Map(p), 0),
|
|
2548
2988
|
constraints: d,
|
|
2549
2989
|
variables: [...u.variables, ...f]
|
|
2550
2990
|
}, _;
|
|
@@ -2554,40 +2994,43 @@ var nt = class {
|
|
|
2554
2994
|
throw this.activeOperationId === n && this.clearSolutionFields(), e;
|
|
2555
2995
|
}
|
|
2556
2996
|
this.ensureExecutionValid(r, n, "check"), this.runTime = _.runTime ?? 0;
|
|
2557
|
-
let v = _.status, y = _.terminationStatus ?? v, x = this.hasPrimalFromResult(_), S = typeof _.objectiveValue == "number" && Number.isFinite(_.objectiveValue) ? _.objectiveValue : Infinity, C = (v === "Optimal" || v === "Feasible" || x) && S <= 1e-5,
|
|
2558
|
-
this.status =
|
|
2559
|
-
let
|
|
2997
|
+
let v = _.status, y = _.terminationStatus ?? v, x = this.hasPrimalFromResult(_), S = typeof _.objectiveValue == "number" && Number.isFinite(_.objectiveValue) ? _.objectiveValue : Infinity, C = (v === "Optimal" || v === "Feasible" || x) && S <= 1e-5, ee = typeof _.objectiveBound == "number" && Number.isFinite(_.objectiveBound) && _.objectiveBound > 1e-5, w = !C && (v === "Infeasible" || v === "Optimal" && S > 1e-5 || ee), T = C ? "Sat" : w ? "Unsat" : "Unknown";
|
|
2998
|
+
this.status = T, this.terminationStatus = a.signal?.aborted || y === "Interrupted" ? "Interrupted" : y === "Optimal" || y === "Feasible" || y === "Infeasible" ? T : y, this.hasPrimalSolution = C, this.objectiveValue = null, this.objectiveBound = void 0, this.relGap = void 0, this.mipGap = void 0, this.absGap = void 0, this.solution = null, this.values.clear();
|
|
2999
|
+
let E = {};
|
|
2560
3000
|
if (C && _.values) {
|
|
2561
3001
|
for (let e of this.variables) {
|
|
2562
3002
|
let t = _.values[e.name];
|
|
2563
|
-
typeof t == "number" && Number.isFinite(t)
|
|
3003
|
+
if (typeof t == "number" && Number.isFinite(t)) {
|
|
3004
|
+
let n = this.normalizePrimalValue(e, t, i, a);
|
|
3005
|
+
e.value = n, this.values.set(e.name, n), s(E, e.name, n);
|
|
3006
|
+
}
|
|
2564
3007
|
}
|
|
2565
3008
|
for (let e of this.boolVars) {
|
|
2566
3009
|
let t = u.boolVarToBinary.get(e), n = t ? _.values[t.name] : _.values[e.name];
|
|
2567
3010
|
if (typeof n == "number" && Number.isFinite(n)) {
|
|
2568
3011
|
let t = Math.round(n) === 1;
|
|
2569
|
-
e.value = t, this.values.set(e.name, t), s(
|
|
3012
|
+
e.value = t, this.values.set(e.name, t), s(E, e.name, t);
|
|
2570
3013
|
}
|
|
2571
3014
|
}
|
|
2572
3015
|
}
|
|
2573
|
-
let
|
|
2574
|
-
if (
|
|
3016
|
+
let D = /* @__PURE__ */ new Set();
|
|
3017
|
+
if (w && _.values) for (let [e, t] of m.entries()) {
|
|
2575
3018
|
let n = _.values[e.name];
|
|
2576
|
-
typeof n == "number" && n > 1e-5 &&
|
|
3019
|
+
typeof n == "number" && n > 1e-5 && D.add(t);
|
|
2577
3020
|
}
|
|
2578
|
-
return this.conflicts =
|
|
2579
|
-
status:
|
|
2580
|
-
values:
|
|
3021
|
+
return this.conflicts = w ? Object.freeze(Array.from(D)) : void 0, this.unsatCore = void 0, this.hasSolutionState = !0, {
|
|
3022
|
+
status: T,
|
|
3023
|
+
values: E,
|
|
2581
3024
|
conflicts: this.conflicts,
|
|
2582
3025
|
unsatCore: this.unsatCore,
|
|
2583
3026
|
runTime: this.runTime
|
|
2584
3027
|
};
|
|
2585
3028
|
}
|
|
2586
3029
|
validateSolution(e = {}) {
|
|
2587
|
-
return
|
|
3030
|
+
return nt(this, e);
|
|
2588
3031
|
}
|
|
2589
3032
|
link(e) {
|
|
2590
|
-
if (e instanceof
|
|
3033
|
+
if (e instanceof k || typeof e == "object" && e && "quadTerms" in e) throw Error("Linking constraint cannot be a quadratic expression.");
|
|
2591
3034
|
if (!(e instanceof c) && (typeof e != "object" || !e || !("sense" in e) || !("lhs" in e) || !("rhs" in e))) throw Error("Linking constraint must contain a comparison operator (<=, >=, ==). Use expr`... <= ...` or .le(), .ge(), .eq().");
|
|
2592
3035
|
this._linkingConstraints ||= [];
|
|
2593
3036
|
let t = e;
|
|
@@ -2597,79 +3040,79 @@ var nt = class {
|
|
|
2597
3040
|
return this._linkingConstraints ?? [];
|
|
2598
3041
|
}
|
|
2599
3042
|
async solveBenders(e) {
|
|
2600
|
-
let t = await
|
|
3043
|
+
let t = await ht({
|
|
2601
3044
|
master: this,
|
|
2602
3045
|
...e
|
|
2603
3046
|
});
|
|
2604
3047
|
return this.decomposition = t, t;
|
|
2605
3048
|
}
|
|
2606
3049
|
async solveColumnGeneration(e) {
|
|
2607
|
-
let t = await
|
|
3050
|
+
let t = await lt({
|
|
2608
3051
|
master: this,
|
|
2609
3052
|
...e
|
|
2610
3053
|
});
|
|
2611
3054
|
return this.decomposition = t, t;
|
|
2612
3055
|
}
|
|
2613
3056
|
};
|
|
2614
|
-
|
|
3057
|
+
I(/* @__PURE__ */ _e({
|
|
2615
3058
|
BoolExpr: () => r,
|
|
2616
3059
|
BoolOp: () => n,
|
|
2617
3060
|
BoolVar: () => o,
|
|
2618
3061
|
Constraint: () => c,
|
|
2619
|
-
HIGHS_CONSTANTS: () =>
|
|
2620
|
-
HIGHS_STATUS_NAMES: () =>
|
|
2621
|
-
HiGHS: () =>
|
|
2622
|
-
Highs: () =>
|
|
2623
|
-
HighsError: () =>
|
|
2624
|
-
HighsModel: () =>
|
|
2625
|
-
HighsSolver: () =>
|
|
2626
|
-
HighsUnsupportedOptionError: () =>
|
|
2627
|
-
LinExpr: () =>
|
|
2628
|
-
MockSolver: () =>
|
|
2629
|
-
Model: () =>
|
|
2630
|
-
OFFICIAL_HIGHS_CDN_WASM_URL: () =>
|
|
2631
|
-
QuadExpr: () =>
|
|
3062
|
+
HIGHS_CONSTANTS: () => oe,
|
|
3063
|
+
HIGHS_STATUS_NAMES: () => de,
|
|
3064
|
+
HiGHS: () => Ke,
|
|
3065
|
+
Highs: () => ne,
|
|
3066
|
+
HighsError: () => re,
|
|
3067
|
+
HighsModel: () => le,
|
|
3068
|
+
HighsSolver: () => J,
|
|
3069
|
+
HighsUnsupportedOptionError: () => se,
|
|
3070
|
+
LinExpr: () => A,
|
|
3071
|
+
MockSolver: () => Qe,
|
|
3072
|
+
Model: () => _t,
|
|
3073
|
+
OFFICIAL_HIGHS_CDN_WASM_URL: () => ue,
|
|
3074
|
+
QuadExpr: () => k,
|
|
2632
3075
|
Var: () => b,
|
|
2633
3076
|
and: () => y,
|
|
2634
|
-
bindHighsFunctions: () =>
|
|
3077
|
+
bindHighsFunctions: () => ie,
|
|
2635
3078
|
buildIndicatorLinearization: () => Y,
|
|
2636
3079
|
buildLpSymbolMaps: () => u,
|
|
2637
|
-
deriveBigMForGe: () =>
|
|
2638
|
-
deriveBigMForLe: () =>
|
|
3080
|
+
deriveBigMForGe: () => ot,
|
|
3081
|
+
deriveBigMForLe: () => at,
|
|
2639
3082
|
dot: () => s,
|
|
2640
|
-
expr: () =>
|
|
3083
|
+
expr: () => Re,
|
|
2641
3084
|
generateLpString: () => C,
|
|
2642
|
-
getHighs: () =>
|
|
2643
|
-
getHighsWasmUrl: () =>
|
|
2644
|
-
getSolver: () =>
|
|
3085
|
+
getHighs: () => ce,
|
|
3086
|
+
getHighsWasmUrl: () => P,
|
|
3087
|
+
getSolver: () => E,
|
|
2645
3088
|
implies: () => e,
|
|
2646
|
-
isLpExportResult: () =>
|
|
2647
|
-
isValidWasmHeader: () =>
|
|
2648
|
-
linearizeModelForMip: () =>
|
|
3089
|
+
isLpExportResult: () => T,
|
|
3090
|
+
isValidWasmHeader: () => ae,
|
|
3091
|
+
linearizeModelForMip: () => st,
|
|
2649
3092
|
listAvailableSolvers: () => S,
|
|
2650
|
-
mapHighsStatus: () =>
|
|
2651
|
-
mapHighsTerminationStatus: () =>
|
|
2652
|
-
maximize: () =>
|
|
2653
|
-
minimize: () =>
|
|
3093
|
+
mapHighsStatus: () => Ge,
|
|
3094
|
+
mapHighsTerminationStatus: () => q,
|
|
3095
|
+
maximize: () => dt,
|
|
3096
|
+
minimize: () => ft,
|
|
2654
3097
|
not: () => v,
|
|
2655
3098
|
or: () => i,
|
|
2656
|
-
parseConstraint: () =>
|
|
2657
|
-
parseExpr: () =>
|
|
2658
|
-
probeWasmUrl: () =>
|
|
2659
|
-
registerHighsSolver: () =>
|
|
2660
|
-
registerMockSolver: () =>
|
|
2661
|
-
registerSolver: () =>
|
|
2662
|
-
resolveBrowserWasmUrl: () =>
|
|
3099
|
+
parseConstraint: () => Ie,
|
|
3100
|
+
parseExpr: () => Ne,
|
|
3101
|
+
probeWasmUrl: () => j,
|
|
3102
|
+
registerHighsSolver: () => qe,
|
|
3103
|
+
registerMockSolver: () => $e,
|
|
3104
|
+
registerSolver: () => w,
|
|
3105
|
+
resolveBrowserWasmUrl: () => N,
|
|
2663
3106
|
sanitizeIdentifier: () => f,
|
|
2664
3107
|
setDefaultSolver: () => _,
|
|
2665
|
-
setHighsWasmUrl: () =>
|
|
2666
|
-
solveLp: () =>
|
|
2667
|
-
solveLpWithHighs: () =>
|
|
3108
|
+
setHighsWasmUrl: () => F,
|
|
3109
|
+
solveLp: () => We,
|
|
3110
|
+
solveLpWithHighs: () => M,
|
|
2668
3111
|
sum: () => t,
|
|
2669
|
-
validateModelIntegrity: () =>
|
|
2670
|
-
validateModelSolution: () =>
|
|
2671
|
-
validateUserBigM: () =>
|
|
3112
|
+
validateModelIntegrity: () => O,
|
|
3113
|
+
validateModelSolution: () => nt,
|
|
3114
|
+
validateUserBigM: () => rt,
|
|
2672
3115
|
xor: () => a
|
|
2673
|
-
}),
|
|
3116
|
+
}), et);
|
|
2674
3117
|
//#endregion
|
|
2675
|
-
export { r as BoolExpr, n as BoolOp, o as BoolVar, c as Constraint,
|
|
3118
|
+
export { r as BoolExpr, n as BoolOp, o as BoolVar, c as Constraint, Ke as HiGHS, ne as Highs, J as HighsSolver, A as LinExpr, Qe as MockSolver, _t as Model, k as QuadExpr, b as Var, y as and, Y as buildIndicatorLinearization, u as buildLpSymbolMaps, ot as deriveBigMForGe, at as deriveBigMForLe, s as dot, Re as expr, C as generateLpString, ce as getHighs, P as getHighsWasmUrl, E as getSolver, e as implies, T as isLpExportResult, st as linearizeModelForMip, S as listAvailableSolvers, dt as maximize, ft as minimize, ye as normalizeSense, v as not, i as or, Ie as parseConstraint, Ne as parseExpr, w as registerSolver, f as sanitizeIdentifier, _ as setDefaultSolver, F as setHighsWasmUrl, ht as solveBenders, lt as solveColumnGeneration, M as solveLpWithHighs, t as sum, O as validateModelIntegrity, nt as validateModelSolution, rt as validateUserBigM, a as xor };
|