@ohtaman/washi 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.ja.md +381 -0
- package/README.md +380 -2
- package/README.zh-CN.md +381 -0
- package/THIRD_PARTY_LICENSES.md +83 -0
- package/dist/lib/assets/highs-worker-DeDWU6KI.js +3 -0
- package/dist/lib/demo/highs.wasm +0 -0
- package/dist/lib/glpk-BC32ZaPG.js +126 -0
- package/dist/lib/highs.wasm +0 -0
- package/dist/lib/highs.wasm.LICENSE +22 -0
- package/dist/lib/solvers/glpk.js +2 -0
- package/dist/lib/solvers/z3.js +2 -0
- package/dist/lib/washi-logo.png +0 -0
- package/dist/lib/washi-logo.svg +70 -0
- package/dist/lib/washi.js +908 -0
- package/dist/lib/z3-C8g-PtmP.js +582 -0
- package/dist/types/glpk-loader.d.ts +8 -0
- package/dist/types/glpk-loader.d.ts.map +1 -0
- package/dist/types/highs-loader.d.ts +55 -0
- package/dist/types/highs-loader.d.ts.map +1 -0
- package/dist/types/highs-worker-client.d.ts +12 -0
- package/dist/types/highs-worker-client.d.ts.map +1 -0
- package/dist/types/highs-worker.d.ts +2 -0
- package/dist/types/highs-worker.d.ts.map +1 -0
- package/dist/types/index.d.ts +14 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/modeler/boolean-expr.d.ts +38 -0
- package/dist/types/modeler/boolean-expr.d.ts.map +1 -0
- package/dist/types/modeler/constraint.d.ts +28 -0
- package/dist/types/modeler/constraint.d.ts.map +1 -0
- package/dist/types/modeler/expression.d.ts +79 -0
- package/dist/types/modeler/expression.d.ts.map +1 -0
- package/dist/types/modeler/index.d.ts +14 -0
- package/dist/types/modeler/index.d.ts.map +1 -0
- package/dist/types/modeler/lp-writer.d.ts +30 -0
- package/dist/types/modeler/lp-writer.d.ts.map +1 -0
- package/dist/types/modeler/mip-compat.d.ts +43 -0
- package/dist/types/modeler/mip-compat.d.ts.map +1 -0
- package/dist/types/modeler/model.d.ts +156 -0
- package/dist/types/modeler/model.d.ts.map +1 -0
- package/dist/types/modeler/parser.d.ts +27 -0
- package/dist/types/modeler/parser.d.ts.map +1 -0
- package/dist/types/modeler/pulp-compat.d.ts +67 -0
- package/dist/types/modeler/pulp-compat.d.ts.map +1 -0
- package/dist/types/modeler/quad-expr.d.ts +55 -0
- package/dist/types/modeler/quad-expr.d.ts.map +1 -0
- package/dist/types/modeler/solvers/glpk.d.ts +22 -0
- package/dist/types/modeler/solvers/glpk.d.ts.map +1 -0
- package/dist/types/modeler/solvers/highs.d.ts +26 -0
- package/dist/types/modeler/solvers/highs.d.ts.map +1 -0
- package/dist/types/modeler/solvers/index.d.ts +7 -0
- package/dist/types/modeler/solvers/index.d.ts.map +1 -0
- package/dist/types/modeler/solvers/mock.d.ts +25 -0
- package/dist/types/modeler/solvers/mock.d.ts.map +1 -0
- package/dist/types/modeler/solvers/registry.d.ts +20 -0
- package/dist/types/modeler/solvers/registry.d.ts.map +1 -0
- package/dist/types/modeler/solvers/types.d.ts +86 -0
- package/dist/types/modeler/solvers/types.d.ts.map +1 -0
- package/dist/types/modeler/solvers/z3.d.ts +37 -0
- package/dist/types/modeler/solvers/z3.d.ts.map +1 -0
- package/dist/types/modeler/types.d.ts +27 -0
- package/dist/types/modeler/types.d.ts.map +1 -0
- package/dist/types/modeler/variable.d.ts +52 -0
- package/dist/types/modeler/variable.d.ts.map +1 -0
- package/package.json +98 -4
|
@@ -0,0 +1,908 @@
|
|
|
1
|
+
import { C as e, E as t, S as n, T as r, _ as i, a, b as o, c as s, d as c, f as l, g as u, h as d, i as f, l as p, m, n as h, o as g, p as _, r as v, s as ee, t as y, u as b, v as x, w as S, x as C, y as w } from "./z3-C8g-PtmP.js";
|
|
2
|
+
import { n as T, t as E } from "./glpk-BC32ZaPG.js";
|
|
3
|
+
//#region src/modeler/quad-expr.ts
|
|
4
|
+
function D(e, t) {
|
|
5
|
+
let n = e.id, r = t.id;
|
|
6
|
+
return n <= r ? `${n}:${r}` : `${r}:${n}`;
|
|
7
|
+
}
|
|
8
|
+
var O = class e {
|
|
9
|
+
linearTerms;
|
|
10
|
+
quadTerms;
|
|
11
|
+
constant;
|
|
12
|
+
constructor(e = /* @__PURE__ */ new Map(), t = /* @__PURE__ */ new Map(), n = 0) {
|
|
13
|
+
this.linearTerms = new Map(e), this.quadTerms = /* @__PURE__ */ new Map();
|
|
14
|
+
for (let [e, n] of t) this.quadTerms.set(e, { ...n });
|
|
15
|
+
this.constant = n;
|
|
16
|
+
}
|
|
17
|
+
clone() {
|
|
18
|
+
return new e(this.linearTerms, this.quadTerms, this.constant);
|
|
19
|
+
}
|
|
20
|
+
getQuadCoeff(e, t) {
|
|
21
|
+
let n = D(e, t), r = this.quadTerms.get(n);
|
|
22
|
+
return r ? r.coeff : 0;
|
|
23
|
+
}
|
|
24
|
+
add(t) {
|
|
25
|
+
let n = this.clone();
|
|
26
|
+
if (typeof t == "number") return n.constant += t, n;
|
|
27
|
+
if (t instanceof e) {
|
|
28
|
+
for (let [e, r] of t.quadTerms) {
|
|
29
|
+
let t = n.quadTerms.get(e), i = (t ? t.coeff : 0) + r.coeff;
|
|
30
|
+
Math.abs(i) < 1e-12 ? n.quadTerms.delete(e) : n.quadTerms.set(e, {
|
|
31
|
+
var1: r.var1,
|
|
32
|
+
var2: r.var2,
|
|
33
|
+
coeff: i
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
for (let [e, r] of t.linearTerms) {
|
|
37
|
+
let t = (n.linearTerms.get(e) || 0) + r;
|
|
38
|
+
Math.abs(t) < 1e-12 ? n.linearTerms.delete(e) : n.linearTerms.set(e, t);
|
|
39
|
+
}
|
|
40
|
+
return n.constant += t.constant, n;
|
|
41
|
+
}
|
|
42
|
+
if ("terms" in t && t.terms instanceof Map) {
|
|
43
|
+
for (let [e, r] of t.terms) {
|
|
44
|
+
let t = (n.linearTerms.get(e) || 0) + r;
|
|
45
|
+
Math.abs(t) < 1e-12 ? n.linearTerms.delete(e) : n.linearTerms.set(e, t);
|
|
46
|
+
}
|
|
47
|
+
return n.constant += t.constant, n;
|
|
48
|
+
}
|
|
49
|
+
let r = t, i = (n.linearTerms.get(r) || 0) + 1;
|
|
50
|
+
return Math.abs(i) < 1e-12 ? n.linearTerms.delete(r) : n.linearTerms.set(r, i), n;
|
|
51
|
+
}
|
|
52
|
+
sub(t) {
|
|
53
|
+
return typeof t == "number" ? this.add(-t) : t instanceof e ? this.add(t.neg()) : ("terms" in t && t.terms instanceof Map, this.add(t.mul(-1)));
|
|
54
|
+
}
|
|
55
|
+
mul(t) {
|
|
56
|
+
let n = new e(/* @__PURE__ */ new Map(), /* @__PURE__ */ new Map(), this.constant * t);
|
|
57
|
+
for (let [e, r] of this.linearTerms) {
|
|
58
|
+
let i = r * t;
|
|
59
|
+
Math.abs(i) >= 1e-12 && n.linearTerms.set(e, i);
|
|
60
|
+
}
|
|
61
|
+
for (let [e, r] of this.quadTerms) {
|
|
62
|
+
let i = r.coeff * t;
|
|
63
|
+
Math.abs(i) >= 1e-12 && n.quadTerms.set(e, {
|
|
64
|
+
var1: r.var1,
|
|
65
|
+
var2: r.var2,
|
|
66
|
+
coeff: i
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
return n;
|
|
70
|
+
}
|
|
71
|
+
div(e) {
|
|
72
|
+
if (Math.abs(e) < 1e-12) throw Error("Division by zero in QuadExpr.div()");
|
|
73
|
+
return this.mul(1 / e);
|
|
74
|
+
}
|
|
75
|
+
neg() {
|
|
76
|
+
return this.mul(-1);
|
|
77
|
+
}
|
|
78
|
+
get value() {
|
|
79
|
+
let e = this.constant;
|
|
80
|
+
for (let [t, n] of this.linearTerms) {
|
|
81
|
+
if (t.value === null) return null;
|
|
82
|
+
e += n * t.value;
|
|
83
|
+
}
|
|
84
|
+
for (let t of this.quadTerms.values()) {
|
|
85
|
+
if (t.var1.value === null || t.var2.value === null) return null;
|
|
86
|
+
e += t.coeff * t.var1.value * t.var2.value;
|
|
87
|
+
}
|
|
88
|
+
return e;
|
|
89
|
+
}
|
|
90
|
+
getValue() {
|
|
91
|
+
return this.value;
|
|
92
|
+
}
|
|
93
|
+
static fromProduct(t, n, r = 1) {
|
|
94
|
+
let i = new e(), a = D(t, n);
|
|
95
|
+
return i.quadTerms.set(a, {
|
|
96
|
+
var1: t.id <= n.id ? t : n,
|
|
97
|
+
var2: t.id <= n.id ? n : t,
|
|
98
|
+
coeff: r
|
|
99
|
+
}), i;
|
|
100
|
+
}
|
|
101
|
+
}, k = class e {
|
|
102
|
+
terms;
|
|
103
|
+
constant;
|
|
104
|
+
constructor(e = /* @__PURE__ */ new Map(), t = 0) {
|
|
105
|
+
this.terms = new Map(e), this.constant = t;
|
|
106
|
+
}
|
|
107
|
+
clone() {
|
|
108
|
+
return new e(this.terms, this.constant);
|
|
109
|
+
}
|
|
110
|
+
add(e) {
|
|
111
|
+
if (e instanceof O) return e.add(this);
|
|
112
|
+
let t = this.clone();
|
|
113
|
+
if (typeof e == "number") t.constant += e;
|
|
114
|
+
else if ("terms" in e) {
|
|
115
|
+
for (let [n, r] of e.terms) {
|
|
116
|
+
let e = (t.terms.get(n) || 0) + r;
|
|
117
|
+
Math.abs(e) < 1e-12 ? t.terms.delete(n) : t.terms.set(n, e);
|
|
118
|
+
}
|
|
119
|
+
t.constant += e.constant;
|
|
120
|
+
} else {
|
|
121
|
+
let n = (t.terms.get(e) || 0) + 1;
|
|
122
|
+
Math.abs(n) < 1e-12 ? t.terms.delete(e) : t.terms.set(e, n);
|
|
123
|
+
}
|
|
124
|
+
return t;
|
|
125
|
+
}
|
|
126
|
+
sub(e) {
|
|
127
|
+
return e instanceof O ? e.neg().add(this) : typeof e == "number" ? this.add(-e) : ("terms" in e, this.add(e.mul(-1)));
|
|
128
|
+
}
|
|
129
|
+
mul(t) {
|
|
130
|
+
if (typeof t == "number") {
|
|
131
|
+
let n = new e(/* @__PURE__ */ new Map(), this.constant * t);
|
|
132
|
+
for (let [e, r] of this.terms) {
|
|
133
|
+
let i = r * t;
|
|
134
|
+
Math.abs(i) >= 1e-12 && n.terms.set(e, i);
|
|
135
|
+
}
|
|
136
|
+
return n;
|
|
137
|
+
}
|
|
138
|
+
let n = new O();
|
|
139
|
+
Math.abs(this.constant) >= 1e-12 && n.linearTerms.set(t, this.constant);
|
|
140
|
+
for (let [e, r] of this.terms) n = n.add(O.fromProduct(e, t, r));
|
|
141
|
+
return n;
|
|
142
|
+
}
|
|
143
|
+
div(e) {
|
|
144
|
+
if (Math.abs(e) < 1e-12) throw Error("Division by zero in LinExpr.div()");
|
|
145
|
+
return this.mul(1 / e);
|
|
146
|
+
}
|
|
147
|
+
neg() {
|
|
148
|
+
return this.mul(-1);
|
|
149
|
+
}
|
|
150
|
+
get value() {
|
|
151
|
+
let e = this.constant;
|
|
152
|
+
for (let [t, n] of this.terms) {
|
|
153
|
+
if (t.value === null) return null;
|
|
154
|
+
e += n * t.value;
|
|
155
|
+
}
|
|
156
|
+
return e;
|
|
157
|
+
}
|
|
158
|
+
getValue() {
|
|
159
|
+
return this.value;
|
|
160
|
+
}
|
|
161
|
+
le(e, t) {
|
|
162
|
+
return this.toConstraint("<=", e, t);
|
|
163
|
+
}
|
|
164
|
+
lessThanOrEqual(e, t) {
|
|
165
|
+
return this.le(e, t);
|
|
166
|
+
}
|
|
167
|
+
ge(e, t) {
|
|
168
|
+
return this.toConstraint(">=", e, t);
|
|
169
|
+
}
|
|
170
|
+
greaterThanOrEqual(e, t) {
|
|
171
|
+
return this.ge(e, t);
|
|
172
|
+
}
|
|
173
|
+
eq(e, t) {
|
|
174
|
+
return this.toConstraint("==", e, t);
|
|
175
|
+
}
|
|
176
|
+
equalTo(e, t) {
|
|
177
|
+
return this.eq(e, t);
|
|
178
|
+
}
|
|
179
|
+
toConstraint(t, n, r) {
|
|
180
|
+
let i = this.clone(), a = 0;
|
|
181
|
+
typeof n == "number" ? a = n : "terms" in n ? (i = i.sub(new e(n.terms, 0)), a = n.constant) : i = i.sub(n);
|
|
182
|
+
let o = a - i.constant;
|
|
183
|
+
return i.constant = 0, new x(i, t, o, r);
|
|
184
|
+
}
|
|
185
|
+
toString() {
|
|
186
|
+
let e = [], t = !0;
|
|
187
|
+
for (let [n, r] of this.terms) {
|
|
188
|
+
let i = r >= 0 ? t ? "" : "+ " : "- ", a = Math.abs(r), o = a === 1 ? "" : `${a} `;
|
|
189
|
+
e.push(`${i}${o}${n.name}`), t = !1;
|
|
190
|
+
}
|
|
191
|
+
if (this.constant !== 0 || e.length === 0) {
|
|
192
|
+
let n = this.constant >= 0 ? t ? "" : "+ " : "- ";
|
|
193
|
+
e.push(`${n}${Math.abs(this.constant)}`);
|
|
194
|
+
}
|
|
195
|
+
return e.join(" ") || "0";
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
function A(e, t) {
|
|
199
|
+
let n = /* @__PURE__ */ new Map(), r = 0, i = 0;
|
|
200
|
+
for (let a of e) {
|
|
201
|
+
let e = t ? t(a, i++) : a;
|
|
202
|
+
if (typeof e == "number") r += e;
|
|
203
|
+
else if (e && typeof e == "object" && "terms" in e) {
|
|
204
|
+
let t = e;
|
|
205
|
+
for (let [e, r] of t.terms) {
|
|
206
|
+
let t = n.get(e) || 0;
|
|
207
|
+
n.set(e, t + r);
|
|
208
|
+
}
|
|
209
|
+
r += t.constant;
|
|
210
|
+
} else if (e) {
|
|
211
|
+
let t = e, r = n.get(t) || 0;
|
|
212
|
+
n.set(t, r + 1);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
for (let [e, t] of n) Math.abs(t) < 1e-12 && n.delete(e);
|
|
216
|
+
return new k(n, r);
|
|
217
|
+
}
|
|
218
|
+
var j = A, M = A, N = 0, P = class e {
|
|
219
|
+
id;
|
|
220
|
+
name;
|
|
221
|
+
type;
|
|
222
|
+
lb;
|
|
223
|
+
ub;
|
|
224
|
+
value = null;
|
|
225
|
+
reducedCost = null;
|
|
226
|
+
constructor(e = {}) {
|
|
227
|
+
this.id = ++N, this.name = e.name || `x_${this.id}`, this.type = e.type || "Continuous", this.type === "Binary" ? (this.lb = 0, this.ub = 1) : (this.lb = e.lb === void 0 ? 0 : e.lb, this.ub = e.ub === void 0 ? Infinity : e.ub);
|
|
228
|
+
}
|
|
229
|
+
get x() {
|
|
230
|
+
return this.value;
|
|
231
|
+
}
|
|
232
|
+
get varValue() {
|
|
233
|
+
return this.value;
|
|
234
|
+
}
|
|
235
|
+
mul(t) {
|
|
236
|
+
if (typeof t == "number") return new k(/* @__PURE__ */ new Map([[this, t]]), 0);
|
|
237
|
+
if (t instanceof e) return O.fromProduct(this, t, 1);
|
|
238
|
+
let n = new O();
|
|
239
|
+
Math.abs(t.constant) >= 1e-12 && n.linearTerms.set(this, t.constant);
|
|
240
|
+
for (let [e, r] of t.terms) {
|
|
241
|
+
let t = O.fromProduct(this, e, r);
|
|
242
|
+
n = n.add(t);
|
|
243
|
+
}
|
|
244
|
+
return n;
|
|
245
|
+
}
|
|
246
|
+
add(e) {
|
|
247
|
+
return e instanceof O ? e.add(this) : new k(/* @__PURE__ */ new Map([[this, 1]]), 0).add(e);
|
|
248
|
+
}
|
|
249
|
+
sub(e) {
|
|
250
|
+
return e instanceof O ? e.neg().add(this) : new k(/* @__PURE__ */ new Map([[this, 1]]), 0).sub(e);
|
|
251
|
+
}
|
|
252
|
+
neg() {
|
|
253
|
+
return this.mul(-1);
|
|
254
|
+
}
|
|
255
|
+
le(e, t) {
|
|
256
|
+
return new k(/* @__PURE__ */ new Map([[this, 1]]), 0).le(e, t);
|
|
257
|
+
}
|
|
258
|
+
lessThanOrEqual(e, t) {
|
|
259
|
+
return this.le(e, t);
|
|
260
|
+
}
|
|
261
|
+
ge(e, t) {
|
|
262
|
+
return new k(/* @__PURE__ */ new Map([[this, 1]]), 0).ge(e, t);
|
|
263
|
+
}
|
|
264
|
+
greaterThanOrEqual(e, t) {
|
|
265
|
+
return this.ge(e, t);
|
|
266
|
+
}
|
|
267
|
+
eq(e, t) {
|
|
268
|
+
return new k(/* @__PURE__ */ new Map([[this, 1]]), 0).eq(e, t);
|
|
269
|
+
}
|
|
270
|
+
equalTo(e, t) {
|
|
271
|
+
return this.eq(e, t);
|
|
272
|
+
}
|
|
273
|
+
toString() {
|
|
274
|
+
return this.name;
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
//#endregion
|
|
278
|
+
//#region src/modeler/parser.ts
|
|
279
|
+
function F(e) {
|
|
280
|
+
let t = [], n = 0, r = e.length;
|
|
281
|
+
for (; n < r;) {
|
|
282
|
+
let i = e[n];
|
|
283
|
+
if (/\s/.test(i)) {
|
|
284
|
+
n++;
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
if (i === "+") t.push({
|
|
288
|
+
type: "PLUS",
|
|
289
|
+
value: "+"
|
|
290
|
+
}), n++;
|
|
291
|
+
else if (i === "-") t.push({
|
|
292
|
+
type: "MINUS",
|
|
293
|
+
value: "-"
|
|
294
|
+
}), n++;
|
|
295
|
+
else if (i === "*") t.push({
|
|
296
|
+
type: "STAR",
|
|
297
|
+
value: "*"
|
|
298
|
+
}), n++;
|
|
299
|
+
else if (i === "/") t.push({
|
|
300
|
+
type: "SLASH",
|
|
301
|
+
value: "/"
|
|
302
|
+
}), n++;
|
|
303
|
+
else if (i === "(") t.push({
|
|
304
|
+
type: "LPAREN",
|
|
305
|
+
value: "("
|
|
306
|
+
}), n++;
|
|
307
|
+
else if (i === ")") t.push({
|
|
308
|
+
type: "RPAREN",
|
|
309
|
+
value: ")"
|
|
310
|
+
}), n++;
|
|
311
|
+
else if (i === "<" || i === ">" || i === "=") {
|
|
312
|
+
let a = i;
|
|
313
|
+
n + 1 < r && (e[n + 1] === "=" || e[n + 1] === "<" || e[n + 1] === ">") ? (a += e[n + 1], n += 2) : n++, t.push({
|
|
314
|
+
type: "COMP",
|
|
315
|
+
value: a
|
|
316
|
+
});
|
|
317
|
+
} else if (/[0-9.]/.test(i)) {
|
|
318
|
+
let i = "";
|
|
319
|
+
for (; n < r && /[0-9.]/.test(e[n]);) i += e[n], n++;
|
|
320
|
+
t.push({
|
|
321
|
+
type: "NUMBER",
|
|
322
|
+
value: i
|
|
323
|
+
});
|
|
324
|
+
} else if (/[a-zA-Z_]/.test(i)) {
|
|
325
|
+
let i = "";
|
|
326
|
+
for (; n < r && /[a-zA-Z0-9_]/.test(e[n]);) i += e[n], n++;
|
|
327
|
+
t.push({
|
|
328
|
+
type: "IDENT",
|
|
329
|
+
value: i
|
|
330
|
+
});
|
|
331
|
+
} else throw Error(`Unexpected character '${i}' at position ${n} in: "${e}"`);
|
|
332
|
+
}
|
|
333
|
+
return t;
|
|
334
|
+
}
|
|
335
|
+
var te = class {
|
|
336
|
+
tokens;
|
|
337
|
+
pos = 0;
|
|
338
|
+
resolver;
|
|
339
|
+
constructor(e, t) {
|
|
340
|
+
this.tokens = e, this.resolver = t;
|
|
341
|
+
}
|
|
342
|
+
peek() {
|
|
343
|
+
return this.tokens[this.pos];
|
|
344
|
+
}
|
|
345
|
+
consume(e) {
|
|
346
|
+
let t = this.tokens[this.pos++];
|
|
347
|
+
if (!t) throw Error("Unexpected end of input");
|
|
348
|
+
if (e && t.value !== e && t.type !== e) throw Error(`Expected '${e}', but found '${t.value}'`);
|
|
349
|
+
return t;
|
|
350
|
+
}
|
|
351
|
+
parseExpression() {
|
|
352
|
+
let e = this.parseTerm();
|
|
353
|
+
for (; this.pos < this.tokens.length;) {
|
|
354
|
+
let t = this.peek();
|
|
355
|
+
if (!t) break;
|
|
356
|
+
if (t.type === "PLUS") this.consume(), e = e.add(this.parseTerm());
|
|
357
|
+
else if (t.type === "MINUS") this.consume(), e = e.sub(this.parseTerm());
|
|
358
|
+
else break;
|
|
359
|
+
}
|
|
360
|
+
return e;
|
|
361
|
+
}
|
|
362
|
+
parseTerm() {
|
|
363
|
+
let e = this.parseFactor();
|
|
364
|
+
for (; this.pos < this.tokens.length;) {
|
|
365
|
+
let t = this.peek();
|
|
366
|
+
if (!t) break;
|
|
367
|
+
if (t.type === "STAR") {
|
|
368
|
+
this.consume();
|
|
369
|
+
let t = this.parseFactor();
|
|
370
|
+
e = this.multiplyExpressions(e, t);
|
|
371
|
+
} else if (t.type === "SLASH") {
|
|
372
|
+
this.consume();
|
|
373
|
+
let t = this.parseFactor();
|
|
374
|
+
if (t.terms.size > 0) throw Error("Cannot divide by variable in linear expression");
|
|
375
|
+
if (Math.abs(t.constant) < 1e-12) throw Error("Division by zero in expression");
|
|
376
|
+
e = e.div(t.constant);
|
|
377
|
+
} else break;
|
|
378
|
+
}
|
|
379
|
+
return e;
|
|
380
|
+
}
|
|
381
|
+
multiplyExpressions(e, t) {
|
|
382
|
+
if (e.terms.size > 0 && t.terms.size > 0) throw Error("Multiplication of two variables creates a non-linear term (not allowed in MILP/LP)");
|
|
383
|
+
return e.terms.size === 0 ? t.mul(e.constant) : e.mul(t.constant);
|
|
384
|
+
}
|
|
385
|
+
parseFactor() {
|
|
386
|
+
let e = this.peek();
|
|
387
|
+
if (!e) throw Error("Unexpected end of expression");
|
|
388
|
+
if (e.type === "PLUS") return this.consume(), this.parseFactor();
|
|
389
|
+
if (e.type === "MINUS") return this.consume(), this.parseFactor().neg();
|
|
390
|
+
if (e.type === "NUMBER") {
|
|
391
|
+
this.consume();
|
|
392
|
+
let t = parseFloat(e.value);
|
|
393
|
+
if (this.peek()?.type === "IDENT") {
|
|
394
|
+
let e = this.consume();
|
|
395
|
+
return this.resolveVariable(e.value).mul(t);
|
|
396
|
+
}
|
|
397
|
+
return new k(/* @__PURE__ */ new Map(), t);
|
|
398
|
+
}
|
|
399
|
+
if (e.type === "IDENT") {
|
|
400
|
+
this.consume();
|
|
401
|
+
let t = this.resolveVariable(e.value);
|
|
402
|
+
return new k(/* @__PURE__ */ new Map([[t, 1]]), 0);
|
|
403
|
+
}
|
|
404
|
+
if (e.type === "LPAREN") {
|
|
405
|
+
this.consume("LPAREN");
|
|
406
|
+
let e = this.parseExpression();
|
|
407
|
+
return this.consume("RPAREN"), e;
|
|
408
|
+
}
|
|
409
|
+
throw Error(`Unexpected token '${e.value}' in expression`);
|
|
410
|
+
}
|
|
411
|
+
resolveVariable(e) {
|
|
412
|
+
let t = this.resolver(e);
|
|
413
|
+
if (!t) throw Error(`Unknown variable '${e}' in expression`);
|
|
414
|
+
return t;
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
function I(e, t) {
|
|
418
|
+
return new te(F(e), t).parseExpression();
|
|
419
|
+
}
|
|
420
|
+
function L(e, t, n) {
|
|
421
|
+
let r = e.match(/(<=|>=|==|=<|=>|=)/);
|
|
422
|
+
if (!r) throw Error(`Constraint must contain a comparison operator (<=, >=, ==, =): "${e}"`);
|
|
423
|
+
let i = r[0], a = r.index, o = e.substring(0, a).trim(), s = e.substring(a + i.length).trim(), c;
|
|
424
|
+
if (i === "<=" || i === "=<") c = "<=";
|
|
425
|
+
else if (i === ">=" || i === "=>") c = ">=";
|
|
426
|
+
else if (i === "==" || i === "=") c = "==";
|
|
427
|
+
else throw Error(`Unsupported operator "${i}"`);
|
|
428
|
+
let l = I(o, t), u = I(s, t);
|
|
429
|
+
return c === "<=" ? l.le(u, n) : c === ">=" ? l.ge(u, n) : l.eq(u, n);
|
|
430
|
+
}
|
|
431
|
+
function R(e, t) {
|
|
432
|
+
let n = /* @__PURE__ */ new Map(), r = "";
|
|
433
|
+
for (let i = 0; i < e.length; i++) if (r += e[i], i < t.length) {
|
|
434
|
+
let e = t[i];
|
|
435
|
+
if (typeof e == "number") r += `(${e})`;
|
|
436
|
+
else if (e instanceof P) {
|
|
437
|
+
let t = `__v_${i}__`;
|
|
438
|
+
n.set(t, e), r += t;
|
|
439
|
+
} else if (e instanceof k) {
|
|
440
|
+
`${i}`, r += `(${e.toString()})`;
|
|
441
|
+
for (let [t] of e.terms) n.set(t.name, t);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
return {
|
|
445
|
+
fullText: r,
|
|
446
|
+
resolver: (e) => n.get(e)
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
function z(e, ...t) {
|
|
450
|
+
let { fullText: n, resolver: r } = R(e, t);
|
|
451
|
+
return L(n, r);
|
|
452
|
+
}
|
|
453
|
+
function B(e, ...t) {
|
|
454
|
+
let { fullText: n, resolver: r } = R(e, t);
|
|
455
|
+
return /(<=|>=|==|=<|=>|=)/.test(n) ? L(n, r) : I(n, r);
|
|
456
|
+
}
|
|
457
|
+
//#endregion
|
|
458
|
+
//#region src/modeler/lp-writer.ts
|
|
459
|
+
function V(e, t, n) {
|
|
460
|
+
let r = e.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
461
|
+
return /^[0-9]/.test(r) && (r = `${t}_${r}`), (!r || r.length === 0) && (r = `${t}_${n}`), r;
|
|
462
|
+
}
|
|
463
|
+
function H(e) {
|
|
464
|
+
let { sense: t, objective: n, constraints: r, variables: i } = e, a = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Set();
|
|
465
|
+
for (let e of i) {
|
|
466
|
+
let t = V(e.name, "x", e.id), n = t, r = 1;
|
|
467
|
+
for (; c.has(n);) n = `${t}_${r++}`;
|
|
468
|
+
c.add(n), o.set(e, n), a.set(n, e);
|
|
469
|
+
}
|
|
470
|
+
let l = /* @__PURE__ */ new Set(), u = /* @__PURE__ */ new Map();
|
|
471
|
+
for (let e of r) {
|
|
472
|
+
let t = V(e.name, "c", e.id), n = t, r = 1;
|
|
473
|
+
for (; l.has(n);) n = `${t}_${r++}`;
|
|
474
|
+
l.add(n), u.set(e, n), s.set(n, e);
|
|
475
|
+
}
|
|
476
|
+
let d = [];
|
|
477
|
+
d.push(t === "Maximize" ? "Maximize" : "Minimize");
|
|
478
|
+
let f = [], p = !0, m = n instanceof O ? n.linearTerms : n.terms;
|
|
479
|
+
if (m) for (let [e, t] of m) {
|
|
480
|
+
if (Math.abs(t) < 1e-12) continue;
|
|
481
|
+
let n = o.get(e), r = t >= 0 ? p ? "" : "+ " : "- ", i = Math.abs(t), a = i === 1 ? "" : `${i} `;
|
|
482
|
+
f.push(`${r}${a}${n}`), p = !1;
|
|
483
|
+
}
|
|
484
|
+
if (n instanceof O && n.quadTerms.size > 0) {
|
|
485
|
+
let e = [], t = !0;
|
|
486
|
+
for (let r of n.quadTerms.values()) {
|
|
487
|
+
if (Math.abs(r.coeff) < 1e-12) continue;
|
|
488
|
+
let n = 2 * r.coeff, i = n >= 0 ? t ? "" : "+ " : "- ", a = Math.abs(n), s = a === 1 ? "" : `${a} `, c = o.get(r.var1), l = o.get(r.var2);
|
|
489
|
+
r.var1 === r.var2 ? e.push(`${i}${s}${c} ^ 2`) : e.push(`${i}${s}${c} * ${l}`), t = !1;
|
|
490
|
+
}
|
|
491
|
+
if (e.length > 0) {
|
|
492
|
+
let t = `[ ${e.join(" ")} ] / 2`;
|
|
493
|
+
f.length > 0 ? f.push("+", t) : f.push(t);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
let h = f.length > 0 ? f.join(" ") : "0";
|
|
497
|
+
d.push(` obj: ${h}`), d.push("Subject To");
|
|
498
|
+
for (let e of r) {
|
|
499
|
+
let t = u.get(e), n = [], r = !0;
|
|
500
|
+
for (let [t, i] of e.lhs.terms) {
|
|
501
|
+
if (Math.abs(i) < 1e-12) continue;
|
|
502
|
+
let e = o.get(t), a = i >= 0 ? r ? "" : "+ " : "- ", s = Math.abs(i), c = s === 1 ? "" : `${s} `;
|
|
503
|
+
n.push(`${a}${c}${e}`), r = !1;
|
|
504
|
+
}
|
|
505
|
+
let i = n.length > 0 ? n.join(" ") : "0", a = e.sense === "==" ? "=" : e.sense;
|
|
506
|
+
d.push(` ${t}: ${i} ${a} ${e.rhs}`);
|
|
507
|
+
}
|
|
508
|
+
d.push("Bounds");
|
|
509
|
+
let g = [], _ = [];
|
|
510
|
+
for (let e of i) {
|
|
511
|
+
let t = o.get(e);
|
|
512
|
+
if (e.type === "Binary") {
|
|
513
|
+
_.push(t);
|
|
514
|
+
continue;
|
|
515
|
+
}
|
|
516
|
+
e.type === "Integer" && g.push(t);
|
|
517
|
+
let n = e.lb === -Infinity, r = e.ub === Infinity;
|
|
518
|
+
n && r ? d.push(` ${t} free`) : n && !r ? d.push(` -infinity <= ${t} <= ${e.ub}`) : !n && r ? e.lb !== 0 && d.push(` ${t} >= ${e.lb}`) : d.push(` ${e.lb} <= ${t} <= ${e.ub}`);
|
|
519
|
+
}
|
|
520
|
+
if (g.length > 0) {
|
|
521
|
+
d.push("Generals");
|
|
522
|
+
for (let e = 0; e < g.length; e += 10) d.push(" " + g.slice(e, e + 10).join(" "));
|
|
523
|
+
}
|
|
524
|
+
if (_.length > 0) {
|
|
525
|
+
d.push("Binary");
|
|
526
|
+
for (let e = 0; e < _.length; e += 10) d.push(" " + _.slice(e, e + 10).join(" "));
|
|
527
|
+
}
|
|
528
|
+
return d.push("End"), {
|
|
529
|
+
lpString: d.join("\n") + "\n",
|
|
530
|
+
nameToVar: a,
|
|
531
|
+
nameToConstraint: s
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
//#endregion
|
|
535
|
+
//#region src/modeler/model.ts
|
|
536
|
+
var U = class {
|
|
537
|
+
name;
|
|
538
|
+
sense;
|
|
539
|
+
objective;
|
|
540
|
+
variables = [];
|
|
541
|
+
constraints = [];
|
|
542
|
+
boolVars = [];
|
|
543
|
+
logicalConstraints = [];
|
|
544
|
+
unsatCore = [];
|
|
545
|
+
values = /* @__PURE__ */ new Map();
|
|
546
|
+
varMapByName = /* @__PURE__ */ new Map();
|
|
547
|
+
constrMapByName = /* @__PURE__ */ new Map();
|
|
548
|
+
status = "NotSolved";
|
|
549
|
+
objectiveValue = null;
|
|
550
|
+
runTime = 0;
|
|
551
|
+
solution = null;
|
|
552
|
+
solver = null;
|
|
553
|
+
constructor(e = {}) {
|
|
554
|
+
this.name = e.name || "OptimizationModel", this.sense = e.sense || "Minimize", this.objective = new k(), e.solver && (this.solver = v(e.solver));
|
|
555
|
+
}
|
|
556
|
+
get vars() {
|
|
557
|
+
return this.variables;
|
|
558
|
+
}
|
|
559
|
+
get constrs() {
|
|
560
|
+
return this.constraints;
|
|
561
|
+
}
|
|
562
|
+
get numVars() {
|
|
563
|
+
return this.variables.length;
|
|
564
|
+
}
|
|
565
|
+
get numConstrs() {
|
|
566
|
+
return this.constraints.length;
|
|
567
|
+
}
|
|
568
|
+
addVar(e, t, n, r) {
|
|
569
|
+
let i;
|
|
570
|
+
i = typeof e == "string" ? {
|
|
571
|
+
name: e,
|
|
572
|
+
type: t,
|
|
573
|
+
lb: n,
|
|
574
|
+
ub: r
|
|
575
|
+
} : e || {};
|
|
576
|
+
let a = new P(i);
|
|
577
|
+
if (this.varMapByName.has(a.name)) throw Error(`Variable with name '${a.name}' already exists in model`);
|
|
578
|
+
return this.variables.push(a), this.varMapByName.set(a.name, a), a;
|
|
579
|
+
}
|
|
580
|
+
addVars(e, t, n) {
|
|
581
|
+
if (typeof t == "number" || Array.isArray(t)) {
|
|
582
|
+
let r = typeof e == "number" ? Array.from({ length: e }, (e, t) => t) : e, i = typeof t == "number" ? Array.from({ length: t }, (e, t) => t) : t, a = n || {}, o = a.prefix || a.name || "x", s = {};
|
|
583
|
+
for (let e of r) {
|
|
584
|
+
s[e] = {};
|
|
585
|
+
for (let t of i) s[e][t] = this.addVar({
|
|
586
|
+
...a,
|
|
587
|
+
name: `${o}_${e}_${t}`
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
return s;
|
|
591
|
+
}
|
|
592
|
+
let r = t || {}, i = r.prefix || r.name;
|
|
593
|
+
if (typeof e == "number") {
|
|
594
|
+
let t = [], n = i || "x";
|
|
595
|
+
for (let i = 0; i < e; i++) t.push(this.addVar({
|
|
596
|
+
...r,
|
|
597
|
+
name: `${n}_${i}`
|
|
598
|
+
}));
|
|
599
|
+
return t;
|
|
600
|
+
}
|
|
601
|
+
{
|
|
602
|
+
let t = {};
|
|
603
|
+
for (let n of e) {
|
|
604
|
+
let e = i ? `${i}_${n}` : `${n}`;
|
|
605
|
+
t[n] = this.addVar({
|
|
606
|
+
...r,
|
|
607
|
+
name: e
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
return t;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
addVars2D(e, t, n) {
|
|
614
|
+
let r = {}, i = n?.name || "x";
|
|
615
|
+
for (let a of e) {
|
|
616
|
+
r[a] = {};
|
|
617
|
+
for (let e of t) r[a][e] = this.addVar({
|
|
618
|
+
...n,
|
|
619
|
+
name: `${i}_${a}_${e}`
|
|
620
|
+
});
|
|
621
|
+
}
|
|
622
|
+
return r;
|
|
623
|
+
}
|
|
624
|
+
getVarByName(e) {
|
|
625
|
+
return this.varMapByName.get(e);
|
|
626
|
+
}
|
|
627
|
+
getConstraintByName(e) {
|
|
628
|
+
return this.constrMapByName.get(e);
|
|
629
|
+
}
|
|
630
|
+
setObjective(e, t) {
|
|
631
|
+
t && (this.sense = t), this.objective = typeof e == "string" ? I(e, (e) => this.getVarByName(e)) : typeof e == "number" ? new k(/* @__PURE__ */ new Map(), e) : e instanceof O || "terms" in e ? e.clone() : new k(/* @__PURE__ */ new Map([[e, 1]]), 0);
|
|
632
|
+
}
|
|
633
|
+
maximize(e) {
|
|
634
|
+
this.setObjective(e, "Maximize");
|
|
635
|
+
}
|
|
636
|
+
minimize(e) {
|
|
637
|
+
this.setObjective(e, "Minimize");
|
|
638
|
+
}
|
|
639
|
+
addBoolVar(e) {
|
|
640
|
+
let t = new C(e);
|
|
641
|
+
return this.boolVars.push(t), t;
|
|
642
|
+
}
|
|
643
|
+
addConstraint(e, t) {
|
|
644
|
+
if (e instanceof w) {
|
|
645
|
+
let n = t || `lc_${this.logicalConstraints.length + 1}`;
|
|
646
|
+
return this.logicalConstraints.push({
|
|
647
|
+
name: n,
|
|
648
|
+
expr: e
|
|
649
|
+
}), e;
|
|
650
|
+
}
|
|
651
|
+
let n;
|
|
652
|
+
if (typeof e == "string" ? n = L(e, (e) => this.getVarByName(e), t) : (n = e, t && (n.name = t)), this.constrMapByName.has(n.name)) throw Error(`Constraint with name '${n.name}' already exists in model`);
|
|
653
|
+
return this.constraints.push(n), this.constrMapByName.set(n.name, n), n;
|
|
654
|
+
}
|
|
655
|
+
addConstr(e, t) {
|
|
656
|
+
return this.addConstraint(e, t);
|
|
657
|
+
}
|
|
658
|
+
addConstrs(e) {
|
|
659
|
+
return e.map((e) => this.addConstraint(e));
|
|
660
|
+
}
|
|
661
|
+
add(e, t) {
|
|
662
|
+
if (Array.isArray(e)) {
|
|
663
|
+
for (let t of e) this.add(t);
|
|
664
|
+
return this;
|
|
665
|
+
}
|
|
666
|
+
return e instanceof P ? (this.varMapByName.has(e.name) || (this.variables.push(e), this.varMapByName.set(e.name, e)), this) : (this.addConstraint(e, t), this);
|
|
667
|
+
}
|
|
668
|
+
addIndicator(e, t, n = {}) {
|
|
669
|
+
let r = n.activeValue === void 0 ? 1 : n.activeValue, i = n.M === void 0 ? 1e5 : n.M, a = n.name || `ind_${e.name}_${t.name}`, o = [], { lhs: s, sense: c, rhs: l } = t;
|
|
670
|
+
if (r === 1) {
|
|
671
|
+
if (c === "<=" || c === "==") {
|
|
672
|
+
let t = this.addConstraint(s.add(e.mul(i)).le(l + i), c === "==" ? `${a}_le` : a);
|
|
673
|
+
o.push(t);
|
|
674
|
+
}
|
|
675
|
+
if (c === ">=" || c === "==") {
|
|
676
|
+
let t = this.addConstraint(s.sub(e.mul(i)).ge(l - i), c === "==" ? `${a}_ge` : a);
|
|
677
|
+
o.push(t);
|
|
678
|
+
}
|
|
679
|
+
} else {
|
|
680
|
+
if (c === "<=" || c === "==") {
|
|
681
|
+
let t = this.addConstraint(s.sub(e.mul(i)).le(l), c === "==" ? `${a}_le` : a);
|
|
682
|
+
o.push(t);
|
|
683
|
+
}
|
|
684
|
+
if (c === ">=" || c === "==") {
|
|
685
|
+
let t = this.addConstraint(s.add(e.mul(i)).ge(l), c === "==" ? `${a}_ge` : a);
|
|
686
|
+
o.push(t);
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
return o;
|
|
690
|
+
}
|
|
691
|
+
toLpString() {
|
|
692
|
+
let { lpString: e } = H({
|
|
693
|
+
name: this.name,
|
|
694
|
+
sense: this.sense,
|
|
695
|
+
objective: this.objective,
|
|
696
|
+
constraints: this.constraints,
|
|
697
|
+
variables: this.variables
|
|
698
|
+
});
|
|
699
|
+
return e;
|
|
700
|
+
}
|
|
701
|
+
async optimize(e, t) {
|
|
702
|
+
let n, r;
|
|
703
|
+
if (e && typeof e == "object" && "solve" in e && typeof e.solve == "function" ? (n = e, r = t || {}) : typeof e == "function" ? (n = new e(t), r = t || {}) : typeof e == "string" ? (n = v(e, t), r = t || {}) : (r = { ...e }, r.solver ? (n = v(r.solver, r), delete r.solver) : n = this.solver || v()), this.objective instanceof O && this.objective.quadTerms.size > 0 && n.capabilities?.supportsQP === !1) throw Error(`Solver "${n.name}" does not support quadratic objectives (QP). Please use a QP-capable solver such as HiGHS or Clarabel.`);
|
|
704
|
+
let i = {
|
|
705
|
+
name: this.name,
|
|
706
|
+
sense: this.sense,
|
|
707
|
+
objective: this.objective,
|
|
708
|
+
constraints: this.constraints,
|
|
709
|
+
variables: this.variables,
|
|
710
|
+
logicalConstraints: this.logicalConstraints,
|
|
711
|
+
boolVars: this.boolVars
|
|
712
|
+
}, a = H(i), o = await n.solve(i, a, r);
|
|
713
|
+
this.runTime = o.runTime ?? 0, this.status = o.status, this.objectiveValue = o.objectiveValue ?? null;
|
|
714
|
+
let s = {}, c = {}, l = {}, u = {};
|
|
715
|
+
if (this.values.clear(), o.values) for (let [e, t] of Object.entries(o.values)) {
|
|
716
|
+
this.values.set(e, t);
|
|
717
|
+
let n = this.getVarByName(e) || a.nameToVar.get(e);
|
|
718
|
+
n && (n.value = t, s[n.name] = t);
|
|
719
|
+
}
|
|
720
|
+
if (o.reducedCosts) for (let [e, t] of Object.entries(o.reducedCosts)) {
|
|
721
|
+
let n = this.getVarByName(e) || a.nameToVar.get(e);
|
|
722
|
+
n && (n.reducedCost = t, l[n.name] = t);
|
|
723
|
+
}
|
|
724
|
+
if (o.duals) for (let [e, t] of Object.entries(o.duals)) {
|
|
725
|
+
let n = this.getConstraintByName(e) || a.nameToConstraint.get(e);
|
|
726
|
+
n && (n.dual = t, c[n.name] = t);
|
|
727
|
+
}
|
|
728
|
+
if (o.slacks) for (let [e, t] of Object.entries(o.slacks)) {
|
|
729
|
+
let n = this.getConstraintByName(e) || a.nameToConstraint.get(e);
|
|
730
|
+
n && (n.slack = t, u[n.name] = t);
|
|
731
|
+
}
|
|
732
|
+
return this.solution = {
|
|
733
|
+
status: this.status,
|
|
734
|
+
objectiveValue: this.objectiveValue,
|
|
735
|
+
values: s,
|
|
736
|
+
duals: c,
|
|
737
|
+
reducedCosts: l,
|
|
738
|
+
slacks: u,
|
|
739
|
+
runTime: this.runTime,
|
|
740
|
+
raw: o.raw
|
|
741
|
+
}, this.status;
|
|
742
|
+
}
|
|
743
|
+
async solve(e, t) {
|
|
744
|
+
return this.optimize(e, t);
|
|
745
|
+
}
|
|
746
|
+
async check(e, t) {
|
|
747
|
+
let n, r;
|
|
748
|
+
if (e && typeof e == "object" && "solve" in e && typeof e.solve == "function" ? (n = e, r = t || {}) : typeof e == "function" ? (n = new e(t), r = t || {}) : typeof e == "string" ? (n = v(e, t), r = t || {}) : (r = { ...e }, r.solver ? (n = v(r.solver, r), delete r.solver) : n = this.solver || v()), this.logicalConstraints.length > 0 && n.capabilities?.supportsSMT !== !0) throw Error(`Solver "${n.name}" does not support SMT / logical constraints (or, implies, not, xor). Please use an SMT-capable solver such as Z3.`);
|
|
749
|
+
if (typeof n.check == "function") {
|
|
750
|
+
let e = await n.check({
|
|
751
|
+
name: this.name,
|
|
752
|
+
variables: this.variables,
|
|
753
|
+
boolVars: this.boolVars,
|
|
754
|
+
constraints: this.constraints,
|
|
755
|
+
logicalConstraints: this.logicalConstraints
|
|
756
|
+
}, r);
|
|
757
|
+
if (this.status = e.status, this.runTime = e.runTime ?? 0, this.unsatCore = e.unsatCore || [], e.values) {
|
|
758
|
+
this.values.clear();
|
|
759
|
+
for (let [t, n] of Object.entries(e.values)) {
|
|
760
|
+
this.values.set(t, n);
|
|
761
|
+
let e = this.getVarByName(t);
|
|
762
|
+
e && typeof n == "number" && (e.value = n);
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
return e;
|
|
766
|
+
}
|
|
767
|
+
let i = this.objective, a = this.sense;
|
|
768
|
+
try {
|
|
769
|
+
this.objective = new k(), this.sense = "Minimize";
|
|
770
|
+
let e = await this.optimize(n, r), t = e === "Optimal" || e === "Feasible", i = e === "Infeasible", a = {};
|
|
771
|
+
for (let e of this.variables) a[e.name] = e.value ?? 0;
|
|
772
|
+
return {
|
|
773
|
+
status: t ? "Sat" : i ? "Unsat" : "Unknown",
|
|
774
|
+
values: a,
|
|
775
|
+
runTime: this.runTime
|
|
776
|
+
};
|
|
777
|
+
} finally {
|
|
778
|
+
this.objective = i, this.sense = a;
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
}, W = "Minimize", G = "Maximize", K = "Continuous", q = "Integer", J = "Binary", Y = {
|
|
782
|
+
Optimal: "Optimal",
|
|
783
|
+
Feasible: "Feasible",
|
|
784
|
+
Infeasible: "Infeasible",
|
|
785
|
+
Unbounded: "Unbounded",
|
|
786
|
+
TimeLimit: "Not Solved (Time Limit)",
|
|
787
|
+
IterationLimit: "Not Solved (Iteration Limit)",
|
|
788
|
+
Error: "Error",
|
|
789
|
+
NotSolved: "Not Solved",
|
|
790
|
+
Sat: "Sat",
|
|
791
|
+
Unsat: "Unsat",
|
|
792
|
+
Unknown: "Unknown"
|
|
793
|
+
}, X = class e extends P {
|
|
794
|
+
constructor(e, t, n, r = "Continuous") {
|
|
795
|
+
super({
|
|
796
|
+
name: e,
|
|
797
|
+
lb: t === void 0 ? 0 : t,
|
|
798
|
+
ub: n === void 0 ? Infinity : n,
|
|
799
|
+
type: r
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
static dicts(t, n, r, i, a = "Continuous") {
|
|
803
|
+
let o = {};
|
|
804
|
+
for (let s of n) {
|
|
805
|
+
let n = `${t}_${s}`;
|
|
806
|
+
o[s] = new e(n, r, i, a);
|
|
807
|
+
}
|
|
808
|
+
return o;
|
|
809
|
+
}
|
|
810
|
+
};
|
|
811
|
+
function Z(e) {
|
|
812
|
+
return e == null ? null : typeof e == "number" ? e : e.value;
|
|
813
|
+
}
|
|
814
|
+
var ne = class {
|
|
815
|
+
model;
|
|
816
|
+
constructor(e = "NoName", t = W) {
|
|
817
|
+
this.model = new U({
|
|
818
|
+
name: e,
|
|
819
|
+
sense: t
|
|
820
|
+
});
|
|
821
|
+
}
|
|
822
|
+
get name() {
|
|
823
|
+
return this.model.name;
|
|
824
|
+
}
|
|
825
|
+
get sense() {
|
|
826
|
+
return this.model.sense;
|
|
827
|
+
}
|
|
828
|
+
set sense(e) {
|
|
829
|
+
this.model.sense = e;
|
|
830
|
+
}
|
|
831
|
+
get objective() {
|
|
832
|
+
return this.model.objective;
|
|
833
|
+
}
|
|
834
|
+
get variables() {
|
|
835
|
+
return this.model.variables;
|
|
836
|
+
}
|
|
837
|
+
get constraints() {
|
|
838
|
+
return this.model.constraints;
|
|
839
|
+
}
|
|
840
|
+
get status() {
|
|
841
|
+
return this.model.status;
|
|
842
|
+
}
|
|
843
|
+
get objectiveValue() {
|
|
844
|
+
return this.model.objectiveValue;
|
|
845
|
+
}
|
|
846
|
+
addVariable(e) {
|
|
847
|
+
return this.model.getVarByName(e.name) || (this.model.variables.push(e), this.model.varMapByName.set(e.name, e)), e;
|
|
848
|
+
}
|
|
849
|
+
setObjective(e) {
|
|
850
|
+
if (typeof e == "object" && e) {
|
|
851
|
+
if ("terms" in e) for (let [t] of e.terms) this.addVariable(t);
|
|
852
|
+
else e instanceof P && this.addVariable(e);
|
|
853
|
+
}
|
|
854
|
+
this.model.setObjective(e);
|
|
855
|
+
}
|
|
856
|
+
addConstraint(e, t) {
|
|
857
|
+
if (typeof e == "object" && e && "lhs" in e) for (let [t] of e.lhs.terms) this.addVariable(t);
|
|
858
|
+
return this.model.addConstraint(e, t);
|
|
859
|
+
}
|
|
860
|
+
add(e, t) {
|
|
861
|
+
typeof e == "string" ? /(<=|>=|==|=<|=>|=)/.test(e) ? this.addConstraint(e, t) : this.setObjective(e) : e instanceof x ? this.addConstraint(e, t) : this.setObjective(e);
|
|
862
|
+
}
|
|
863
|
+
async solve(e, t) {
|
|
864
|
+
return this.model.optimize(e, t);
|
|
865
|
+
}
|
|
866
|
+
toLpString() {
|
|
867
|
+
return this.model.toLpString();
|
|
868
|
+
}
|
|
869
|
+
}, Q = "Minimize", $ = "Maximize", re = "Binary", ie = "Integer", ae = "Continuous";
|
|
870
|
+
function oe(e) {
|
|
871
|
+
return {
|
|
872
|
+
expr: e,
|
|
873
|
+
sense: $
|
|
874
|
+
};
|
|
875
|
+
}
|
|
876
|
+
function se(e) {
|
|
877
|
+
return {
|
|
878
|
+
expr: e,
|
|
879
|
+
sense: Q
|
|
880
|
+
};
|
|
881
|
+
}
|
|
882
|
+
var ce = class extends U {
|
|
883
|
+
constructor(e = "MipModel", t = Q) {
|
|
884
|
+
super({
|
|
885
|
+
name: e,
|
|
886
|
+
sense: t
|
|
887
|
+
});
|
|
888
|
+
}
|
|
889
|
+
add_var(e = {}) {
|
|
890
|
+
return this.addVar({
|
|
891
|
+
name: e.name,
|
|
892
|
+
type: e.var_type,
|
|
893
|
+
lb: e.lb,
|
|
894
|
+
ub: e.ub
|
|
895
|
+
});
|
|
896
|
+
}
|
|
897
|
+
set objective_spec(e) {
|
|
898
|
+
this.setObjective(e.expr, e.sense);
|
|
899
|
+
}
|
|
900
|
+
get objective_value() {
|
|
901
|
+
return this.objectiveValue;
|
|
902
|
+
}
|
|
903
|
+
add_constr(e, t) {
|
|
904
|
+
return this.addConstraint(e, t);
|
|
905
|
+
}
|
|
906
|
+
};
|
|
907
|
+
//#endregion
|
|
908
|
+
export { re as BINARY, w as BoolExpr, o as BoolOp, C as BoolVar, ae as CONTINUOUS, x as Constraint, E as GLPKSolver, s as HiGHS, p as HighsSolver, ie as INTEGER, k as LinExpr, J as LpBinary, K as LpContinuous, q as LpInteger, G as LpMaximize, W as LpMinimize, ne as LpProblem, Y as LpStatus, X as LpVariable, $ as MAXIMIZE, Q as MINIMIZE, ce as MipModel, ee as MockSolver, U as Model, b as OFFICIAL_HIGHS_CDN_WASM_URL, O as QuadExpr, P as Var, y as Z3Solver, n as and, z as c, B as expr, H as generateLpString, T as getGlpk, c as getHighs, l as getHighsWasmUrl, v as getSolver, e as implies, _ as isValidWasmHeader, f as listAvailableSolvers, M as lpSum, oe as maximize, se as minimize, S as not, r as or, L as parseConstraint, I as parseExpr, m as probeWasmUrl, a as registerSolver, h as registerZ3Solver, d as resolveBrowserWasmUrl, g as setDefaultSolver, u as setHighsWasmUrl, i as solveLpWithHighs, A as sum, Z as value, t as xor, j as xsum };
|