@ohtaman/washi 0.8.0 → 0.8.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/README.ja.md +22 -24
- package/README.md +20 -22
- package/README.zh-CN.md +25 -27
- package/dist/browser/{__vite-browser-external-rTVKos2U.js → solvers/highs-__vite-browser-external-Dn-83hnF.js} +1 -1
- package/dist/browser/{browser-runtime-AG5QFtKf.js → solvers/highs-browser-runtime-0Bgq5ksG.js} +1 -1
- package/dist/browser/{loader-i3FnqMes.js → solvers/highs-loader-BLY3wzDH.js} +25 -18
- package/dist/browser/{highs-worker.js → solvers/highs-worker.js} +1 -1
- package/dist/browser/solvers/highs.js +1414 -0
- package/dist/browser/washi.js +1090 -1473
- package/dist/browser/washi.min.js +4 -4
- package/dist/lib/solvers/glpk.js +34 -32
- package/dist/lib/{highs-node-runtime.js → solvers/highs-node-runtime.js} +1 -1
- package/dist/lib/solvers/highs.js +410 -0
- package/dist/lib/solvers/lp-writer-CLScMFbS.js +115 -0
- package/dist/lib/solvers/pumpkin-node-runtime.js +1 -1
- package/dist/lib/solvers/pumpkin-worker.js +1 -1
- package/dist/lib/solvers/pumpkin.js +247 -1146
- package/dist/lib/solvers/registry-CrYRRk9q.js +907 -0
- package/dist/lib/solvers/types-BGqPkxwu.js +12 -0
- package/dist/lib/solvers/z3.js +125 -123
- package/dist/lib/washi.js +2426 -1340
- package/dist/types/index.d.ts +1 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/modeler/index.d.ts +0 -1
- package/dist/types/modeler/index.d.ts.map +1 -1
- package/dist/types/modeler/model.d.ts +6 -7
- package/dist/types/modeler/model.d.ts.map +1 -1
- package/dist/types/modeler/solvers/index.d.ts +2 -1
- package/dist/types/modeler/solvers/index.d.ts.map +1 -1
- package/dist/types/modeler/solvers/registry.d.ts.map +1 -1
- package/docs/examples/diet-sensitivity.md +33 -14
- package/docs/examples/ecommerce-coupons.md +17 -9
- package/docs/examples/index.md +34 -30
- package/docs/examples/knapsack.md +11 -5
- package/docs/examples/portfolio-qp.md +21 -8
- package/docs/examples/production.md +10 -5
- package/docs/examples/project-scheduling.md +25 -12
- package/docs/examples/shift-scheduling.md +27 -19
- package/docs/examples/smt-security.md +27 -12
- package/docs/examples/transportation.md +23 -13
- package/docs/examples/traveling-salesperson.md +34 -22
- package/docs/examples/unsat-core.md +27 -15
- package/docs/examples/vehicle-routing.md +74 -28
- package/docs/getting-started/ai-agents.md +129 -90
- package/docs/getting-started/coding-agents.md +60 -41
- package/docs/getting-started/cross-origin.md +27 -14
- package/docs/getting-started/index.md +23 -16
- package/docs/getting-started/runtimes.md +70 -40
- package/docs/guide/constraint-programming.md +74 -44
- package/docs/guide/constraints.md +71 -39
- package/docs/guide/decomposition.md +72 -42
- package/docs/guide/expressions.md +79 -48
- package/docs/guide/formulation-tips.md +46 -27
- package/docs/guide/objectives.md +55 -31
- package/docs/guide/unsat-core.md +56 -53
- package/docs/guide/variables.md +61 -28
- package/docs/solvers/custom-solver.md +155 -119
- package/docs/solvers/glpk.md +58 -17
- package/docs/solvers/highs.md +115 -43
- package/docs/solvers/index.md +33 -26
- package/docs/solvers/pumpkin.md +71 -63
- package/docs/solvers/z3.md +63 -25
- package/llms-full.txt +1623 -953
- package/llms.txt +125 -93
- package/package.json +29 -20
- package/skills/washi/SKILL.md +43 -22
- package/dist/browser/solvers/pumpkin.min.js +0 -1
- package/dist/lib/registry-dx7x4e5c.js +0 -1694
- /package/dist/browser/{highs.wasm → solvers/highs.wasm} +0 -0
- /package/dist/browser/{highs.wasm.LICENSE → solvers/highs.wasm.LICENSE} +0 -0
- /package/dist/lib/{__vite-browser-external-oLchLuMx.js → solvers/__vite-browser-external-oLchLuMx.js} +0 -0
- /package/dist/lib/{highs-assets.js → solvers/highs-assets.js} +0 -0
- /package/dist/lib/{highs-browser-runtime.js → solvers/highs-browser-runtime.js} +0 -0
- /package/dist/lib/{highs-runtime.js → solvers/highs-runtime.js} +0 -0
- /package/dist/lib/{highs-worker-factory.js → solvers/highs-worker-factory.js} +0 -0
- /package/dist/lib/{highs-worker.js → solvers/highs-worker.js} +0 -0
- /package/dist/lib/{highs.wasm → solvers/highs.wasm} +0 -0
- /package/dist/lib/{highs.wasm.LICENSE → solvers/highs.wasm.LICENSE} +0 -0
- /package/dist/lib/{loader-CDpPXN_F.js → solvers/loader-CDpPXN_F.js} +0 -0
- /package/dist/lib/solvers/{pumpkin-loader-Ow74Fa1B.js → loader-Ow74Fa1B.js} +0 -0
- /package/dist/lib/{rolldown-runtime-DtPi1Y-2.js → solvers/rolldown-runtime-DtPi1Y-2.js} +0 -0
|
@@ -1,1694 +0,0 @@
|
|
|
1
|
-
//#region src/modeler/types.ts
|
|
2
|
-
function e(e) {
|
|
3
|
-
if (e === void 0) return "Minimize";
|
|
4
|
-
if (typeof e == "string") {
|
|
5
|
-
let t = e.toLowerCase();
|
|
6
|
-
if (t === "maximize") return "Maximize";
|
|
7
|
-
if (t === "minimize") return "Minimize";
|
|
8
|
-
}
|
|
9
|
-
throw Error(`Invalid optimization sense "${String(e)}": expected "Maximize", "Minimize", "maximize", or "minimize".`);
|
|
10
|
-
}
|
|
11
|
-
//#endregion
|
|
12
|
-
//#region src/modeler/boolean-expr.ts
|
|
13
|
-
var t = 0;
|
|
14
|
-
function n(e, t) {
|
|
15
|
-
if (e instanceof r || e instanceof F) return e;
|
|
16
|
-
throw typeof e == "object" && e && ("terms" in e || "quadTerms" in e) ? Error(`Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical operator "${t}". Use a comparison constraint such as expr\`\${x} <= 10\` or a BoolVar/BoolExpr.`) : typeof e == "object" && e && "id" in e && "name" in e && "lb" in e && "ub" in e && !("sense" in e) ? Error(`Invalid operand passed to logical operator "${t}": expected a BoolVar, BoolExpr, or Constraint, but received numeric variable "${e.name}". Use m.bool() for propositional variables, add a comparison such as expr\`\${x} == 1\`, or use m.indicator(binVar, constraint) for 0-1 Big-M indicator constraints.`) : Error(`Invalid operand passed to logical operator "${t}": expected a BoolVar, BoolExpr, or Constraint.`);
|
|
17
|
-
}
|
|
18
|
-
var r = class {
|
|
19
|
-
and(e) {
|
|
20
|
-
return new a("and", [this, n(e, "and")]);
|
|
21
|
-
}
|
|
22
|
-
or(e) {
|
|
23
|
-
return new a("or", [this, n(e, "or")]);
|
|
24
|
-
}
|
|
25
|
-
implies(e) {
|
|
26
|
-
return new a("implies", [this, n(e, "implies")]);
|
|
27
|
-
}
|
|
28
|
-
xor(e) {
|
|
29
|
-
return new a("xor", [this, n(e, "xor")]);
|
|
30
|
-
}
|
|
31
|
-
not() {
|
|
32
|
-
return new a("not", [this]);
|
|
33
|
-
}
|
|
34
|
-
}, i = class extends r {
|
|
35
|
-
id;
|
|
36
|
-
type = "Boolean";
|
|
37
|
-
name;
|
|
38
|
-
value = null;
|
|
39
|
-
constructor(e) {
|
|
40
|
-
super(), this.id = ++t, this.name = e || `p_${this.id}`;
|
|
41
|
-
}
|
|
42
|
-
toString() {
|
|
43
|
-
return this.name;
|
|
44
|
-
}
|
|
45
|
-
}, a = class extends r {
|
|
46
|
-
op;
|
|
47
|
-
children;
|
|
48
|
-
constructor(e, t) {
|
|
49
|
-
super(), this.op = e, this.children = t.map((t) => n(t, e));
|
|
50
|
-
}
|
|
51
|
-
toString() {
|
|
52
|
-
return this.op === "not" ? `not(${this.children[0].toString()})` : `(${this.children.map((e) => e.toString()).join(` ${this.op} `)})`;
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
function o(...e) {
|
|
56
|
-
if (e.length === 0) throw Error("and() requires at least one expression");
|
|
57
|
-
return new a("and", e);
|
|
58
|
-
}
|
|
59
|
-
function s(...e) {
|
|
60
|
-
if (e.length === 0) throw Error("or() requires at least one expression");
|
|
61
|
-
return new a("or", e);
|
|
62
|
-
}
|
|
63
|
-
function c(e) {
|
|
64
|
-
return new a("not", [e]);
|
|
65
|
-
}
|
|
66
|
-
function l(e, t) {
|
|
67
|
-
return new a("implies", [e, t]);
|
|
68
|
-
}
|
|
69
|
-
function u(...e) {
|
|
70
|
-
if (e.length === 0) throw Error("xor() requires at least one expression");
|
|
71
|
-
return new a("xor", e);
|
|
72
|
-
}
|
|
73
|
-
var d = class extends r {
|
|
74
|
-
spec;
|
|
75
|
-
discreteVars;
|
|
76
|
-
constructor(e, t) {
|
|
77
|
-
super(), this.spec = e, this.discreteVars = t;
|
|
78
|
-
}
|
|
79
|
-
toString() {
|
|
80
|
-
switch (this.spec.kind) {
|
|
81
|
-
case "distinct": return `distinct(${this.spec.args.map((e) => e.toString()).join(", ")})`;
|
|
82
|
-
case "disjoint": return `disjoint([${this.spec.tasks.map((e) => `{ start: ${e.start.toString()}, duration: ${e.duration} }`).join(", ")}])`;
|
|
83
|
-
case "cumulative": return `cumulative([${this.spec.tasks.map((e) => `{ start: ${e.start.toString()}, duration: ${e.duration}, demand: ${e.demand} }`).join(", ")}], ${this.spec.capacity})`;
|
|
84
|
-
case "allowed": return `allowed([${this.spec.vars.map((e) => e.toString()).join(", ")}], [${this.spec.tuples.map((e) => `[${e.join(", ")}]`).join(", ")}])`;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
function f(e) {
|
|
89
|
-
if (typeof e != "object" || !e || Array.isArray(e) || e instanceof F || e instanceof r || e instanceof H) return !1;
|
|
90
|
-
let t = Object.getPrototypeOf(e);
|
|
91
|
-
return t === Object.prototype || t === null;
|
|
92
|
-
}
|
|
93
|
-
function p(e) {
|
|
94
|
-
return typeof e == "number" || e instanceof F || e instanceof H || typeof e == "object" && !!e && ("terms" in e && e.terms instanceof Map || "quadTerms" in e && e.quadTerms instanceof Map || "id" in e && "name" in e && "lb" in e && "ub" in e && !("sense" in e));
|
|
95
|
-
}
|
|
96
|
-
function m(...e) {
|
|
97
|
-
if (e.length === 0) throw Error("distinct() requires at least one expression or container.");
|
|
98
|
-
let t = [], n = /* @__PURE__ */ new Set(), r = (e, i) => {
|
|
99
|
-
if (e == null) throw Error("Unsupported nullish item in distinct(): expected an integer-valued expression.");
|
|
100
|
-
if (p(e)) {
|
|
101
|
-
let r = V(e, "distinct()");
|
|
102
|
-
B(r, "distinct()", n), t.push(r);
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
if (Array.isArray(e)) {
|
|
106
|
-
if (i >= 2) throw Error("Nested containers deeper than 2D are not supported in distinct().");
|
|
107
|
-
for (let t = 0; t < e.length; t++) {
|
|
108
|
-
if (!(t in e)) throw Error(`Unsupported item in distinct(): sparse array hole at index ${t}.`);
|
|
109
|
-
r(e[t], i + 1);
|
|
110
|
-
}
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
if (typeof e == "object" && !(e instanceof String) && !(e instanceof Map) && Symbol.iterator in e && typeof e[Symbol.iterator] == "function") {
|
|
114
|
-
if (i >= 2) throw Error("Nested containers deeper than 2D are not supported in distinct().");
|
|
115
|
-
for (let t of e) r(t, i + 1);
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
if (f(e)) {
|
|
119
|
-
if (i >= 2) throw Error("Nested containers deeper than 2D are not supported in distinct().");
|
|
120
|
-
if (Object.getOwnPropertySymbols(e).length > 0) throw Error("Symbol keys are not supported in distinct().");
|
|
121
|
-
for (let t of Object.keys(e)) r(e[t], i + 1);
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
throw Error("Unsupported item in distinct(): expected an integer-valued expression or container.");
|
|
125
|
-
};
|
|
126
|
-
for (let t of e) r(t, 0);
|
|
127
|
-
return new d({
|
|
128
|
-
kind: "distinct",
|
|
129
|
-
args: t
|
|
130
|
-
}, n);
|
|
131
|
-
}
|
|
132
|
-
function h(e, t) {
|
|
133
|
-
if (e == null) throw Error(`${t}(tasks) requires an Array, Iterable, or Record of task objects.`);
|
|
134
|
-
if (Array.isArray(e)) {
|
|
135
|
-
let n = [];
|
|
136
|
-
for (let r = 0; r < e.length; r++) {
|
|
137
|
-
if (!(r in e)) throw Error(`${t}(tasks) encountered sparse array hole at index ${r}.`);
|
|
138
|
-
n.push(e[r]);
|
|
139
|
-
}
|
|
140
|
-
return n;
|
|
141
|
-
}
|
|
142
|
-
if (typeof e == "object" && !(e instanceof String) && !(e instanceof Map) && Symbol.iterator in e && typeof e[Symbol.iterator] == "function") return Array.from(e);
|
|
143
|
-
if (f(e)) {
|
|
144
|
-
if ("start" in e && "duration" in e) throw Error(`${t}(tasks) expects an Array, Iterable, or Record of task objects, not a single task object.`);
|
|
145
|
-
if (Object.getOwnPropertySymbols(e).length > 0) throw Error(`Symbol keys are not supported in ${t}().`);
|
|
146
|
-
return Object.keys(e).map((t) => e[t]);
|
|
147
|
-
}
|
|
148
|
-
throw Error(`${t}(tasks) requires an Array, Iterable, or Record of task objects.`);
|
|
149
|
-
}
|
|
150
|
-
function g(e) {
|
|
151
|
-
let t = h(e, "disjoint"), n = /* @__PURE__ */ new Set(), r = [];
|
|
152
|
-
for (let e = 0; e < t.length; e++) {
|
|
153
|
-
let i = t[e];
|
|
154
|
-
if (typeof i != "object" || !i || !("start" in i) || !("duration" in i)) throw Error(`disjoint() task at index ${e} must be an object with { start, duration }.`);
|
|
155
|
-
let a = i, o = V(a.start, `disjoint() task[${e}].start`);
|
|
156
|
-
if (B(o, `disjoint() task[${e}].start`, n), typeof a.duration != "number" || !Number.isSafeInteger(a.duration) || a.duration < 0) throw Error(`disjoint() task[${e}].duration must be a finite non-negative integer, received ${String(a.duration)}.`);
|
|
157
|
-
r.push({
|
|
158
|
-
start: o,
|
|
159
|
-
duration: Object.is(a.duration, -0) ? 0 : a.duration
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
return new d({
|
|
163
|
-
kind: "disjoint",
|
|
164
|
-
tasks: r
|
|
165
|
-
}, n);
|
|
166
|
-
}
|
|
167
|
-
function _(e, t) {
|
|
168
|
-
if (typeof t != "number" || !Number.isSafeInteger(t) || t < 0) throw Error(`cumulative() capacity must be a finite non-negative integer, received ${String(t)}.`);
|
|
169
|
-
let n = h(e, "cumulative"), r = /* @__PURE__ */ new Set(), i = [];
|
|
170
|
-
for (let e = 0; e < n.length; e++) {
|
|
171
|
-
let t = n[e];
|
|
172
|
-
if (typeof t != "object" || !t || !("start" in t) || !("duration" in t) || !("demand" in t)) throw Error(`cumulative() task at index ${e} must be an object with { start, duration, demand }.`);
|
|
173
|
-
let a = t, o = V(a.start, `cumulative() task[${e}].start`);
|
|
174
|
-
if (B(o, `cumulative() task[${e}].start`, r), typeof a.duration != "number" || !Number.isSafeInteger(a.duration) || a.duration < 0) throw Error(`cumulative() task[${e}].duration must be a finite non-negative integer, received ${String(a.duration)}.`);
|
|
175
|
-
if (typeof a.demand != "number" || !Number.isSafeInteger(a.demand) || a.demand < 0) throw Error(`cumulative() task[${e}].demand must be a finite non-negative integer, received ${String(a.demand)}.`);
|
|
176
|
-
i.push({
|
|
177
|
-
start: o,
|
|
178
|
-
duration: Object.is(a.duration, -0) ? 0 : a.duration,
|
|
179
|
-
demand: Object.is(a.demand, -0) ? 0 : a.demand
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
return new d({
|
|
183
|
-
kind: "cumulative",
|
|
184
|
-
tasks: i,
|
|
185
|
-
capacity: Object.is(t, -0) ? 0 : t
|
|
186
|
-
}, r);
|
|
187
|
-
}
|
|
188
|
-
function v(e, t) {
|
|
189
|
-
if (!Array.isArray(e) || e.length === 0) throw Error("allowed(vars, tuples) requires a non-empty Array of expressions as its first argument.");
|
|
190
|
-
let n = /* @__PURE__ */ new Set(), r = Array(e.length);
|
|
191
|
-
for (let t = 0; t < e.length; t++) {
|
|
192
|
-
if (!(t in e)) throw Error(`allowed() vars encountered sparse array hole at index ${t}.`);
|
|
193
|
-
let i = V(e[t], `allowed() vars[${t}]`);
|
|
194
|
-
B(i, `allowed() vars[${t}]`, n), r[t] = i;
|
|
195
|
-
}
|
|
196
|
-
if (typeof t != "object" || !t || t instanceof String || !(Symbol.iterator in t) || typeof t[Symbol.iterator] != "function") throw Error("allowed(vars, tuples) requires an Iterable of integer tuples as its second argument.");
|
|
197
|
-
let i = [], a = 0;
|
|
198
|
-
for (let n of t) {
|
|
199
|
-
if (!Array.isArray(n) || n.length !== e.length) throw Error(`allowed() tuple at index ${a} must be an Array of length ${e.length}.`);
|
|
200
|
-
let t = Array(e.length);
|
|
201
|
-
for (let r = 0; r < e.length; r++) {
|
|
202
|
-
if (!(r in n)) throw Error(`allowed() tuple[${a}] encountered sparse array hole at index ${r}.`);
|
|
203
|
-
let e = n[r];
|
|
204
|
-
if (typeof e != "number" || !Number.isSafeInteger(e)) throw Error(`allowed() tuple[${a}][${r}] must be a safe integer, received ${String(e)}.`);
|
|
205
|
-
t[r] = Object.is(e, -0) ? 0 : e;
|
|
206
|
-
}
|
|
207
|
-
i.push(t), a++;
|
|
208
|
-
}
|
|
209
|
-
return new d({
|
|
210
|
-
kind: "allowed",
|
|
211
|
-
vars: r,
|
|
212
|
-
tuples: i
|
|
213
|
-
}, n);
|
|
214
|
-
}
|
|
215
|
-
//#endregion
|
|
216
|
-
//#region src/modeler/quad-expr.ts
|
|
217
|
-
function y(e, t) {
|
|
218
|
-
let n = e.id, r = t.id;
|
|
219
|
-
return n <= r ? `${n}:${r}` : `${r}:${n}`;
|
|
220
|
-
}
|
|
221
|
-
var b = class e {
|
|
222
|
-
kind = "quadratic";
|
|
223
|
-
linearTerms;
|
|
224
|
-
quadTerms;
|
|
225
|
-
constant;
|
|
226
|
-
nonlinearTerms;
|
|
227
|
-
constructor(e = /* @__PURE__ */ new Map(), t = /* @__PURE__ */ new Map(), n = 0, r = []) {
|
|
228
|
-
this.linearTerms = new Map(e), this.quadTerms = /* @__PURE__ */ new Map();
|
|
229
|
-
for (let [e, n] of t) this.quadTerms.set(e, { ...n });
|
|
230
|
-
this.constant = n, this.nonlinearTerms = r.filter((e) => e.coeff !== 0).map((e) => ({
|
|
231
|
-
atom: L(e.atom),
|
|
232
|
-
coeff: e.coeff
|
|
233
|
-
}));
|
|
234
|
-
}
|
|
235
|
-
hasNonlinearTerms() {
|
|
236
|
-
return this.nonlinearTerms.length > 0;
|
|
237
|
-
}
|
|
238
|
-
clone() {
|
|
239
|
-
return new e(this.linearTerms, this.quadTerms, this.constant, this.nonlinearTerms);
|
|
240
|
-
}
|
|
241
|
-
getQuadCoeff(e, t) {
|
|
242
|
-
let n = y(e, t), r = this.quadTerms.get(n);
|
|
243
|
-
return r ? r.coeff : 0;
|
|
244
|
-
}
|
|
245
|
-
add(t) {
|
|
246
|
-
if (I(t)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
247
|
-
let n = this.clone();
|
|
248
|
-
if (typeof t == "number") return n.constant += t, n;
|
|
249
|
-
if (t instanceof e) {
|
|
250
|
-
for (let [e, r] of t.quadTerms) {
|
|
251
|
-
let t = n.quadTerms.get(e), i = (t ? t.coeff : 0) + r.coeff;
|
|
252
|
-
i === 0 ? n.quadTerms.delete(e) : n.quadTerms.set(e, {
|
|
253
|
-
var1: r.var1,
|
|
254
|
-
var2: r.var2,
|
|
255
|
-
coeff: i
|
|
256
|
-
});
|
|
257
|
-
}
|
|
258
|
-
for (let [e, r] of t.linearTerms) {
|
|
259
|
-
let t = (n.linearTerms.get(e) ?? 0) + r;
|
|
260
|
-
t === 0 ? n.linearTerms.delete(e) : n.linearTerms.set(e, t);
|
|
261
|
-
}
|
|
262
|
-
if (n.constant += t.constant, Array.isArray(t.nonlinearTerms)) for (let e of t.nonlinearTerms) e.coeff !== 0 && n.nonlinearTerms.push({
|
|
263
|
-
atom: L(e.atom),
|
|
264
|
-
coeff: e.coeff
|
|
265
|
-
});
|
|
266
|
-
return n;
|
|
267
|
-
}
|
|
268
|
-
if ("terms" in t && t.terms instanceof Map) {
|
|
269
|
-
for (let [e, r] of t.terms) {
|
|
270
|
-
let t = (n.linearTerms.get(e) ?? 0) + r;
|
|
271
|
-
t === 0 ? n.linearTerms.delete(e) : n.linearTerms.set(e, t);
|
|
272
|
-
}
|
|
273
|
-
if (n.constant += t.constant, Array.isArray(t.nonlinearTerms)) for (let e of t.nonlinearTerms) e.coeff !== 0 && n.nonlinearTerms.push({
|
|
274
|
-
atom: L(e.atom),
|
|
275
|
-
coeff: e.coeff
|
|
276
|
-
});
|
|
277
|
-
return n;
|
|
278
|
-
}
|
|
279
|
-
let r = t, i = (n.linearTerms.get(r) ?? 0) + 1;
|
|
280
|
-
return i === 0 ? n.linearTerms.delete(r) : n.linearTerms.set(r, i), n;
|
|
281
|
-
}
|
|
282
|
-
sub(e) {
|
|
283
|
-
if (I(e)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
284
|
-
return typeof e == "number" ? this.add(-e) : this.add(e.mul(-1));
|
|
285
|
-
}
|
|
286
|
-
mul(t) {
|
|
287
|
-
if (I(t)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
288
|
-
if (typeof t == "number") {
|
|
289
|
-
let n = this.linearTerms.size > 0 || this.quadTerms.size > 0 || this.nonlinearTerms.length > 0, r = this.constant === 0 && n ? 0 : this.constant * t;
|
|
290
|
-
if (this.constant !== 0 && t !== 0 && r === 0) throw Error("Constant multiplication underflows to zero.");
|
|
291
|
-
let i = new e(/* @__PURE__ */ new Map(), /* @__PURE__ */ new Map(), r);
|
|
292
|
-
for (let [e, n] of this.linearTerms) {
|
|
293
|
-
let r = n * t;
|
|
294
|
-
if (n !== 0 && t !== 0 && r === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
295
|
-
r !== 0 && i.linearTerms.set(e, r);
|
|
296
|
-
}
|
|
297
|
-
for (let [e, n] of this.quadTerms) {
|
|
298
|
-
let r = n.coeff * t;
|
|
299
|
-
if (n.coeff !== 0 && t !== 0 && r === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
300
|
-
r !== 0 && i.quadTerms.set(e, {
|
|
301
|
-
var1: n.var1,
|
|
302
|
-
var2: n.var2,
|
|
303
|
-
coeff: r
|
|
304
|
-
});
|
|
305
|
-
}
|
|
306
|
-
if (t !== 0) for (let e of this.nonlinearTerms) {
|
|
307
|
-
let n = e.coeff * t;
|
|
308
|
-
if (e.coeff !== 0 && n === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
309
|
-
n !== 0 && i.nonlinearTerms.push({
|
|
310
|
-
atom: L(e.atom),
|
|
311
|
-
coeff: n
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
return i;
|
|
315
|
-
}
|
|
316
|
-
let n = "id" in t && "name" in t && !("terms" in t) && !("quadTerms" in t), r = "quadTerms" in t ? t.quadTerms.size : 0, i = !n && "nonlinearTerms" in t && Array.isArray(t.nonlinearTerms) ? t.nonlinearTerms.length : 0, a = n ? /* @__PURE__ */ new Map([[t, 1]]) : "quadTerms" in t ? t.linearTerms : t.terms, o = n ? 0 : t.constant;
|
|
317
|
-
if (this.nonlinearTerms.length > 0) {
|
|
318
|
-
if (!n && r === 0 && a.size === 0 && i === 0) return this.mul(o);
|
|
319
|
-
throw Error("Cannot multiply nonlinear expression (max, min, abs, or at) by a variable expression: only scalar multiplication is supported.");
|
|
320
|
-
}
|
|
321
|
-
if (i > 0) {
|
|
322
|
-
if (this.quadTerms.size === 0 && this.linearTerms.size === 0) {
|
|
323
|
-
let n = t.mul(this.constant);
|
|
324
|
-
return "quadTerms" in n ? n : new e(n.terms, /* @__PURE__ */ new Map(), n.constant, n.nonlinearTerms);
|
|
325
|
-
}
|
|
326
|
-
throw Error("Cannot multiply nonlinear expression (max, min, abs, or at) by a variable expression: only scalar multiplication is supported.");
|
|
327
|
-
}
|
|
328
|
-
if (this.quadTerms.size > 0 && (r > 0 || a.size > 0) || r > 0 && this.linearTerms.size > 0) throw Error("Multiplication creates a non-linear term of degree > 2 (not allowed in MILP/LP/QP)");
|
|
329
|
-
if (r === 0 && a.size === 0) return this.mul(o);
|
|
330
|
-
if (this.quadTerms.size === 0 && this.linearTerms.size === 0 && !n) {
|
|
331
|
-
let n = t.mul(this.constant);
|
|
332
|
-
return "quadTerms" in n ? n : new e(n.terms, /* @__PURE__ */ new Map(), n.constant, n.nonlinearTerms);
|
|
333
|
-
}
|
|
334
|
-
return e.multiplyLinearTerms(this.constant, this.linearTerms, o, a);
|
|
335
|
-
}
|
|
336
|
-
div(t) {
|
|
337
|
-
let n = e.extractScalarDivisor(t, "QuadExpr.div()");
|
|
338
|
-
return this.mul(1 / n);
|
|
339
|
-
}
|
|
340
|
-
neg() {
|
|
341
|
-
return this.mul(-1);
|
|
342
|
-
}
|
|
343
|
-
get value() {
|
|
344
|
-
let e = this.constant;
|
|
345
|
-
for (let [t, n] of this.linearTerms) {
|
|
346
|
-
if (t.value === null) return null;
|
|
347
|
-
e += n * t.value;
|
|
348
|
-
}
|
|
349
|
-
for (let t of this.quadTerms.values()) {
|
|
350
|
-
if (t.var1.value === null || t.var2.value === null) return null;
|
|
351
|
-
e += t.coeff * t.var1.value * t.var2.value;
|
|
352
|
-
}
|
|
353
|
-
for (let t of this.nonlinearTerms) {
|
|
354
|
-
let n = R(t.atom);
|
|
355
|
-
if (n === null) return null;
|
|
356
|
-
e += t.coeff * n;
|
|
357
|
-
}
|
|
358
|
-
return e;
|
|
359
|
-
}
|
|
360
|
-
le(e, t) {
|
|
361
|
-
return this.toConstraint("<=", e, t);
|
|
362
|
-
}
|
|
363
|
-
ge(e, t) {
|
|
364
|
-
return this.toConstraint(">=", e, t);
|
|
365
|
-
}
|
|
366
|
-
eq(e, t) {
|
|
367
|
-
return this.toConstraint("==", e, t);
|
|
368
|
-
}
|
|
369
|
-
lt(e, t) {
|
|
370
|
-
return this.toDiscreteComparison("<", e, t);
|
|
371
|
-
}
|
|
372
|
-
gt(e, t) {
|
|
373
|
-
return this.toDiscreteComparison(">", e, t);
|
|
374
|
-
}
|
|
375
|
-
ne(e, t) {
|
|
376
|
-
return this.toDiscreteComparison("!=", e, t);
|
|
377
|
-
}
|
|
378
|
-
toDiscreteComparison(e, t, n) {
|
|
379
|
-
if (this.quadTerms.size > 0) throw Error(`Quadratic expressions are not supported in discrete comparisons (${e}): quadratic terms are only allowed in objectives.`);
|
|
380
|
-
let r = new H(this.linearTerms, this.constant, this.nonlinearTerms);
|
|
381
|
-
return e === "<" ? r.lt(t, n) : e === ">" ? r.gt(t, n) : r.ne(t, n);
|
|
382
|
-
}
|
|
383
|
-
toConstraint(e, t, n) {
|
|
384
|
-
if (this.quadTerms.size > 0) throw Error(`Quadratic expressions are not supported in linear constraints (${e}): quadratic terms are only allowed in objectives.`);
|
|
385
|
-
let r = new H(this.linearTerms, this.constant, this.nonlinearTerms);
|
|
386
|
-
return e === "<=" ? r.le(t, n) : e === ">=" ? r.ge(t, n) : r.eq(t, n);
|
|
387
|
-
}
|
|
388
|
-
and(e) {
|
|
389
|
-
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'and': expected a BoolVar, Constraint, or BoolOp.");
|
|
390
|
-
}
|
|
391
|
-
or(e) {
|
|
392
|
-
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'or': expected a BoolVar, Constraint, or BoolOp.");
|
|
393
|
-
}
|
|
394
|
-
implies(e) {
|
|
395
|
-
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'implies': expected a BoolVar, Constraint, or BoolOp.");
|
|
396
|
-
}
|
|
397
|
-
xor(e) {
|
|
398
|
-
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'xor': expected a BoolVar, Constraint, or BoolOp.");
|
|
399
|
-
}
|
|
400
|
-
not() {
|
|
401
|
-
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'not': expected a BoolVar, Constraint, or BoolOp.");
|
|
402
|
-
}
|
|
403
|
-
toString() {
|
|
404
|
-
let e = [], t = !0;
|
|
405
|
-
for (let n of this.quadTerms.values()) {
|
|
406
|
-
let r = n.coeff, i = r >= 0 ? t ? "" : "+ " : "- ", a = Math.abs(r), o = a === 1 ? "" : `${a} `;
|
|
407
|
-
n.var1 === n.var2 ? e.push(`${i}${o}${n.var1.name}^2`) : e.push(`${i}${o}${n.var1.name} * ${n.var2.name}`), t = !1;
|
|
408
|
-
}
|
|
409
|
-
for (let [n, r] of this.linearTerms) {
|
|
410
|
-
let i = r >= 0 ? t ? "" : "+ " : "- ", a = Math.abs(r), o = a === 1 ? "" : `${a} `;
|
|
411
|
-
e.push(`${i}${o}${n.name}`), t = !1;
|
|
412
|
-
}
|
|
413
|
-
if (this.nonlinearTerms.length > 0) {
|
|
414
|
-
let n = new H(/* @__PURE__ */ new Map(), 0, this.nonlinearTerms).toString();
|
|
415
|
-
n !== "0" && (e.push(t ? n : n.startsWith("-") ? `- ${n.slice(2)}` : `+ ${n}`), t = !1);
|
|
416
|
-
}
|
|
417
|
-
if (this.constant !== 0 || e.length === 0) {
|
|
418
|
-
let n = this.constant >= 0 ? t ? "" : "+ " : "- ";
|
|
419
|
-
e.push(`${n}${Math.abs(this.constant)}`);
|
|
420
|
-
}
|
|
421
|
-
return e.join(" ") || "0";
|
|
422
|
-
}
|
|
423
|
-
static fromProduct(t, n, r = 1) {
|
|
424
|
-
let i = new e();
|
|
425
|
-
if (r === 0) return i;
|
|
426
|
-
let a = y(t, n);
|
|
427
|
-
return i.quadTerms.set(a, {
|
|
428
|
-
var1: t.id <= n.id ? t : n,
|
|
429
|
-
var2: t.id <= n.id ? n : t,
|
|
430
|
-
coeff: r
|
|
431
|
-
}), i;
|
|
432
|
-
}
|
|
433
|
-
static multiplyLinearTerms(t, n, r, i) {
|
|
434
|
-
let a = n.size > 0 || i.size > 0, o = (t === 0 || r === 0) && a ? 0 : t * r;
|
|
435
|
-
if (t !== 0 && r !== 0 && o === 0) throw Error("Constant multiplication underflows to zero.");
|
|
436
|
-
let s = new e(/* @__PURE__ */ new Map(), /* @__PURE__ */ new Map(), o === 0 ? 0 : o);
|
|
437
|
-
if (r !== 0) for (let [e, t] of n) {
|
|
438
|
-
let n = t * r;
|
|
439
|
-
if (t !== 0 && n === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
440
|
-
n !== 0 && s.linearTerms.set(e, n);
|
|
441
|
-
}
|
|
442
|
-
if (t !== 0) for (let [e, n] of i) {
|
|
443
|
-
let r = s.linearTerms.get(e) ?? 0, i = n * t;
|
|
444
|
-
if (n !== 0 && i === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
445
|
-
let a = r + i;
|
|
446
|
-
a === 0 ? s.linearTerms.delete(e) : s.linearTerms.set(e, a);
|
|
447
|
-
}
|
|
448
|
-
for (let [t, r] of n) for (let [n, a] of i) {
|
|
449
|
-
let i = r * a;
|
|
450
|
-
if (r !== 0 && a !== 0 && i === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
451
|
-
s = s.add(e.fromProduct(t, n, i));
|
|
452
|
-
}
|
|
453
|
-
return s;
|
|
454
|
-
}
|
|
455
|
-
static extractScalarDivisor(e, t) {
|
|
456
|
-
if (I(e)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
457
|
-
if (typeof e == "number") {
|
|
458
|
-
if (e === 0) throw Error(`Division by zero in ${t}`);
|
|
459
|
-
return e;
|
|
460
|
-
}
|
|
461
|
-
let n = "quadTerms" in e ? e.linearTerms.size : e.terms.size, r = "quadTerms" in e ? e.quadTerms.size : 0, i = "nonlinearTerms" in e && Array.isArray(e.nonlinearTerms) ? e.nonlinearTerms.length : 0;
|
|
462
|
-
if (n > 0 || r > 0 || i > 0) throw Error("Cannot divide by variable in expression");
|
|
463
|
-
if (e.constant === 0) throw Error(`Division by zero in ${t}`);
|
|
464
|
-
return e.constant;
|
|
465
|
-
}
|
|
466
|
-
}, x = /* @__PURE__ */ new WeakMap(), S = /* @__PURE__ */ new WeakMap(), C = /* @__PURE__ */ new WeakMap(), w = /* @__PURE__ */ new WeakMap();
|
|
467
|
-
function ee(e, t) {
|
|
468
|
-
w.set(e, t);
|
|
469
|
-
}
|
|
470
|
-
function T(e) {
|
|
471
|
-
e && w.get(e)?.();
|
|
472
|
-
}
|
|
473
|
-
function E(e, t, n) {
|
|
474
|
-
if (e instanceof a) for (let r of e.children) E(r, t, n);
|
|
475
|
-
else if (e instanceof F) {
|
|
476
|
-
let r = e.ownerModel, i = S.get(e);
|
|
477
|
-
(r !== t || i !== t) && (n && n.push(() => {
|
|
478
|
-
e.ownerModel = r, i ? S.set(e, i) : S.delete(e);
|
|
479
|
-
}), e.ownerModel = t, S.set(e, t));
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
function te(e, t) {
|
|
483
|
-
let n = x.get(e);
|
|
484
|
-
if (n && n !== t) throw Error(`Variable "${e.name}" already belongs to model "${n.name}" and cannot be added to model "${t.name}".`);
|
|
485
|
-
x.set(e, t);
|
|
486
|
-
}
|
|
487
|
-
function ne(e, t) {
|
|
488
|
-
let n = C.get(e);
|
|
489
|
-
if (n) throw Error(n === t ? `Constraint "${e.name}" is already registered in model "${t.name}".` : `Constraint "${e.name}" already belongs to model "${n.name}" and cannot be added to model "${t.name}".`);
|
|
490
|
-
let r = S.get(e);
|
|
491
|
-
if (r && r !== t) throw Error(`Constraint "${e.name}" already belongs to model "${r.name}" and cannot be added to model "${t.name}".`);
|
|
492
|
-
return C.set(e, t), S.set(e, t), r;
|
|
493
|
-
}
|
|
494
|
-
function re(e, t) {
|
|
495
|
-
x.get(e) === t && x.delete(e);
|
|
496
|
-
}
|
|
497
|
-
function ie(e, t, n) {
|
|
498
|
-
C.get(e) === t && C.delete(e), S.get(e) === t && (n ? S.set(e, n) : S.delete(e));
|
|
499
|
-
}
|
|
500
|
-
var D = 1e-6;
|
|
501
|
-
function O(e) {
|
|
502
|
-
if (typeof e.lb != "number" || Number.isNaN(e.lb)) throw Error(`Variable "${e.name}" has invalid lower bound: ${e.lb} (NaN or non-numeric bound is not allowed).`);
|
|
503
|
-
if (e.lb === Infinity) throw Error(`Variable "${e.name}" has invalid lower bound: +Infinity is not allowed as a lower bound.`);
|
|
504
|
-
if (typeof e.ub != "number" || Number.isNaN(e.ub)) throw Error(`Variable "${e.name}" has invalid upper bound: ${e.ub} (NaN or non-numeric bound is not allowed).`);
|
|
505
|
-
if (e.ub === -Infinity) throw Error(`Variable "${e.name}" has invalid upper bound: -Infinity is not allowed as an upper bound.`);
|
|
506
|
-
if (e.lb > e.ub) throw Error(`Variable "${e.name}" has invalid bounds: lower bound (${e.lb}) is strictly greater than upper bound (${e.ub}).`);
|
|
507
|
-
if (e.initialValue !== void 0) {
|
|
508
|
-
if (typeof e.initialValue != "number" || !Number.isFinite(e.initialValue)) throw Error(`Variable "${e.name}" has invalid initialValue: ${String(e.initialValue)} (initialValue must be a finite number).`);
|
|
509
|
-
let t = e.type === "Binary" ? Math.max(0, e.lb) : e.lb, n = e.type === "Binary" ? Math.min(1, e.ub) : e.ub;
|
|
510
|
-
if (e.initialValue < t - 1e-6 || e.initialValue > n + 1e-6) throw Error(`Variable "${e.name}" has initialValue (${e.initialValue}) outside its bounds [${t}, ${n}] (tolerance ${D}).`);
|
|
511
|
-
if (e.type === "Integer" || e.type === "Binary") {
|
|
512
|
-
let r = Math.round(e.initialValue);
|
|
513
|
-
if (Math.abs(e.initialValue - r) > 1e-6 || r < t || r > n) throw Error(`Variable "${e.name}" of type "${e.type}" has non-integral or out-of-domain initialValue: ${e.initialValue} (must be within ${D} of an integer in [${t}, ${n}]).`);
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
function ae(e) {
|
|
518
|
-
if (e.initialValue === void 0) return;
|
|
519
|
-
if (O(e), e.type === "Integer" || e.type === "Binary") {
|
|
520
|
-
let t = Math.round(e.initialValue);
|
|
521
|
-
return Object.is(t, -0) ? 0 : t;
|
|
522
|
-
}
|
|
523
|
-
let t = Math.min(e.ub, Math.max(e.lb, e.initialValue));
|
|
524
|
-
return Object.is(t, -0) ? 0 : t;
|
|
525
|
-
}
|
|
526
|
-
function k(e, t) {
|
|
527
|
-
if (e) {
|
|
528
|
-
for (let n of e) if (n.type !== "Integer" && n.type !== "Binary") throw Error(`${t} requires Integer or Binary variables, but variable "${n.name}" has type "${n.type}".`);
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
function A(e, t, n, r) {
|
|
532
|
-
if (e && e.length !== 0) for (let i of e) {
|
|
533
|
-
if (!Number.isFinite(i.coeff)) throw Error(`Nonlinear expression in ${t} has invalid coefficient: ${i.coeff}. Coefficients must be finite numbers.`);
|
|
534
|
-
let e = i.atom;
|
|
535
|
-
if (e.kind === "abs") j(e.arg, t, n, r);
|
|
536
|
-
else if (e.kind === "max" || e.kind === "min") for (let i of e.args) j(i, t, n, r);
|
|
537
|
-
else if (e.kind === "at") {
|
|
538
|
-
k(e.discreteVars, `at() index in ${t}`), j(e.index, t, n, r);
|
|
539
|
-
for (let i of e.array) j(i, t, n, r);
|
|
540
|
-
}
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
function j(e, t, n, r, i) {
|
|
544
|
-
if (e.constant !== void 0 && !Number.isFinite(e.constant)) {
|
|
545
|
-
let n = i?.constantPrefix ?? t, r = i?.constantKind ?? "constant";
|
|
546
|
-
throw Error(`${n} has invalid ${r}: ${e.constant}. Constant must be a finite number.`);
|
|
547
|
-
}
|
|
548
|
-
let a = i?.varLocationPhrase ?? `in ${t}`;
|
|
549
|
-
for (let [i, o] of e.terms) {
|
|
550
|
-
if (!r(i)) throw Error(`Variable "${i.name}" ${a} does not belong to model "${n}". All variables must belong to the model by object identity.`);
|
|
551
|
-
if (!Number.isFinite(o)) throw Error(`Variable "${i.name}" in ${t} has invalid coefficient: ${o}. Coefficients must be finite numbers.`);
|
|
552
|
-
}
|
|
553
|
-
A(e.nonlinearTerms, t, n, r);
|
|
554
|
-
}
|
|
555
|
-
function M(e, t, n) {
|
|
556
|
-
if (e instanceof b) {
|
|
557
|
-
if (!Number.isFinite(e.constant)) throw Error(`Objective function has invalid constant: ${e.constant}. Constant must be a finite number.`);
|
|
558
|
-
for (let [r, i] of e.linearTerms) {
|
|
559
|
-
if (!n(r)) throw Error(`Variable "${r.name}" in objective function does not belong to model "${t}". All variables must belong to the model by object identity.`);
|
|
560
|
-
if (!Number.isFinite(i)) throw Error(`Variable "${r.name}" in objective function has invalid coefficient: ${i}. Coefficients must be finite numbers.`);
|
|
561
|
-
}
|
|
562
|
-
for (let [, r] of e.quadTerms) {
|
|
563
|
-
if (!n(r.var1)) throw Error(`Variable "${r.var1.name}" in quadratic objective function does not belong to model "${t}". All variables must belong to the model by object identity.`);
|
|
564
|
-
if (!n(r.var2)) throw Error(`Variable "${r.var2.name}" in quadratic objective function does not belong to model "${t}". All variables must belong to the model by object identity.`);
|
|
565
|
-
if (!Number.isFinite(r.coeff) || !Number.isFinite(r.coeff * 2)) throw Error(`Quadratic term (${r.var1.name} * ${r.var2.name}) in objective function has invalid coefficient: ${r.coeff}.`);
|
|
566
|
-
}
|
|
567
|
-
A(e.nonlinearTerms, "objective function", t, n);
|
|
568
|
-
} else "terms" in e && j(e, "objective function", t, n, { constantPrefix: "Objective function" });
|
|
569
|
-
}
|
|
570
|
-
function N(e, t, n, r = e.name) {
|
|
571
|
-
if (!Number.isFinite(e.rhs)) throw Error(`Constraint "${r}" has invalid right-hand side (RHS): ${e.rhs}. RHS must be a finite number.`);
|
|
572
|
-
if (k(e._requiredDiscreteVars, `Discrete constraint "${r}"`), j(e.lhs, `constraint "${r}"`, t, n), e.sense === "!=") {
|
|
573
|
-
if (!Number.isSafeInteger(e.rhs)) throw Error(`Discrete "!=" in constraint "${r}" requires a safe integer RHS, received ${e.rhs}.`);
|
|
574
|
-
B(e.lhs, `Discrete "!=" in constraint "${r}"`);
|
|
575
|
-
}
|
|
576
|
-
if (e._indicatorMeta) {
|
|
577
|
-
let i = e._indicatorMeta.binVar;
|
|
578
|
-
if (!n(i)) throw Error(`Indicator binary variable "${i.name}" in constraint "${r}" does not belong to model "${t}".`);
|
|
579
|
-
if (!(i.type === "Binary" || i.type === "Integer" && i.lb === 0 && i.ub === 1)) throw Error(`Indicator variable "${i.name}" in constraint "${r}" has invalid type "${i.type}" or bounds [${i.lb}, ${i.ub}]: expected Binary or Integer [0, 1].`);
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
function P(e, t, n, r, o, s) {
|
|
583
|
-
if (e instanceof i) {
|
|
584
|
-
if (!o(e)) throw Error(`Boolean variable "${e.name}" in logical constraint "${t}" does not belong to model "${n}".`);
|
|
585
|
-
} else if (e instanceof a) for (let i of e.children) P(i, t, n, r, o, s);
|
|
586
|
-
else if (e instanceof d) {
|
|
587
|
-
k(e.discreteVars, `Global constraint "${t}"`);
|
|
588
|
-
let i = `logical constraint "${t}"`;
|
|
589
|
-
switch (e.spec.kind) {
|
|
590
|
-
case "distinct":
|
|
591
|
-
for (let t of e.spec.args) j(t, i, n, r);
|
|
592
|
-
break;
|
|
593
|
-
case "disjoint":
|
|
594
|
-
for (let t of e.spec.tasks) j(t.start, i, n, r);
|
|
595
|
-
break;
|
|
596
|
-
case "cumulative":
|
|
597
|
-
for (let t of e.spec.tasks) j(t.start, i, n, r);
|
|
598
|
-
break;
|
|
599
|
-
case "allowed": for (let t of e.spec.vars) j(t, i, n, r);
|
|
600
|
-
}
|
|
601
|
-
} else if (e instanceof F) {
|
|
602
|
-
if (s) {
|
|
603
|
-
let r = S.get(e) ?? e.ownerModel ?? void 0;
|
|
604
|
-
if (r && r !== s) throw Error(`Constraint "${e.name}" embedded in logical constraint "${t}" already belongs to model "${r.name ?? "another model"}" and cannot be added to model "${n}".`);
|
|
605
|
-
}
|
|
606
|
-
if (!Number.isFinite(e.rhs)) throw Error(`Constraint "${e.name}" embedded in logical constraint "${t}" has invalid RHS: ${e.rhs}.`);
|
|
607
|
-
if (k(e._requiredDiscreteVars, `Discrete constraint "${e.name}" embedded in logical constraint "${t}"`), j(e.lhs, `constraint "${e.name}" embedded in logical constraint "${t}"`, n, r, {
|
|
608
|
-
constantPrefix: `Constraint "${e.name}" embedded in logical constraint "${t}"`,
|
|
609
|
-
constantKind: "LHS constant",
|
|
610
|
-
varLocationPhrase: `embedded in logical constraint "${t}"`
|
|
611
|
-
}), e.sense === "!=") {
|
|
612
|
-
if (!Number.isSafeInteger(e.rhs)) throw Error(`Discrete "!=" in constraint "${e.name}" embedded in logical constraint "${t}" requires a safe integer RHS, received ${e.rhs}.`);
|
|
613
|
-
B(e.lhs, `Discrete "!=" in constraint "${e.name}" embedded in logical constraint "${t}"`);
|
|
614
|
-
}
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
function oe(e) {
|
|
618
|
-
let { model: t } = e, n = /* @__PURE__ */ new Set(), r = /* @__PURE__ */ new Set();
|
|
619
|
-
for (let i of e.variables) {
|
|
620
|
-
O(i);
|
|
621
|
-
let a = x.get(i);
|
|
622
|
-
if (a && a !== t || n.has(i.name) || e.getVarByName(i.name) !== i) throw Error(`Variable "${i.name}" is duplicated or not canonically registered in model "${e.name}".`);
|
|
623
|
-
n.add(i.name), r.add(i);
|
|
624
|
-
}
|
|
625
|
-
let i = /* @__PURE__ */ new Set(), a = /* @__PURE__ */ new Set();
|
|
626
|
-
for (let t of e.boolVars) {
|
|
627
|
-
if (i.has(t.name) || e.getBoolVarByName(t.name) !== t) throw Error(`Boolean variable "${t.name}" is duplicated or not canonically registered in model "${e.name}".`);
|
|
628
|
-
i.add(t.name), a.add(t);
|
|
629
|
-
}
|
|
630
|
-
let o = /* @__PURE__ */ new Set();
|
|
631
|
-
for (let t of e.constraints) {
|
|
632
|
-
if (i.has(t.name)) throw Error(`Constraint name "${t.name}" conflicts with Boolean variable "${t.name}" in model "${e.name}".`);
|
|
633
|
-
if (o.has(t.name)) throw Error(`Duplicate constraint name "${t.name}" in model "${e.name}".`);
|
|
634
|
-
o.add(t.name);
|
|
635
|
-
}
|
|
636
|
-
for (let t of e.logicalConstraints) {
|
|
637
|
-
if (i.has(t.name)) throw Error(`Logical constraint name "${t.name}" conflicts with Boolean variable "${t.name}" in model "${e.name}".`);
|
|
638
|
-
if (o.has(t.name)) throw Error(`Conflicting constraint name "${t.name}" across arithmetic and logical constraints in model "${e.name}".`);
|
|
639
|
-
o.add(t.name);
|
|
640
|
-
}
|
|
641
|
-
for (let n of e.constraints) {
|
|
642
|
-
let r = S.get(n);
|
|
643
|
-
if (r && r !== t || e.getConstraintByName(n.name) !== n) throw Error(`Constraint "${n.name}" is duplicated or not canonically registered in model "${e.name}".`);
|
|
644
|
-
}
|
|
645
|
-
for (let t of e.logicalConstraints) if (e.getLogicalConstraintByName(t.name) !== t) throw Error(`Logical constraint "${t.name}" is duplicated or not canonically registered in model "${e.name}".`);
|
|
646
|
-
e.objectiveExpr && M(e.objectiveExpr, e.name, (e) => r.has(e));
|
|
647
|
-
for (let t of e.constraints) N(t, e.name, (e) => r.has(e));
|
|
648
|
-
for (let n of e.logicalConstraints) P(n.expr, n.name, e.name, (e) => r.has(e), (e) => a.has(e), t);
|
|
649
|
-
}
|
|
650
|
-
//#endregion
|
|
651
|
-
//#region src/modeler/constraint.ts
|
|
652
|
-
var se = 0, F = class e {
|
|
653
|
-
kind = "constraint";
|
|
654
|
-
id;
|
|
655
|
-
_lhs;
|
|
656
|
-
_sense;
|
|
657
|
-
_rhs;
|
|
658
|
-
_name;
|
|
659
|
-
hasExplicitName;
|
|
660
|
-
ownerModel = null;
|
|
661
|
-
_indicatorOrigin;
|
|
662
|
-
_indicatorSnapshot;
|
|
663
|
-
_discreteVars;
|
|
664
|
-
slack = null;
|
|
665
|
-
dual = null;
|
|
666
|
-
addTerm(e, t) {
|
|
667
|
-
return t === 0 ? this : (this._indicatorOrigin = void 0, this._indicatorSnapshot = void 0, this._lhs.addTerm(e, t), T(this.ownerModel), this);
|
|
668
|
-
}
|
|
669
|
-
setTerm(e, t) {
|
|
670
|
-
return this._indicatorOrigin = void 0, this._indicatorSnapshot = void 0, this._lhs.setTerm(e, t), T(this.ownerModel), this;
|
|
671
|
-
}
|
|
672
|
-
constructor(e, t, n, r) {
|
|
673
|
-
this.id = ++se, this._lhs = e, this._sense = t, this._rhs = n;
|
|
674
|
-
let i = typeof r == "string" ? r.trim() : void 0;
|
|
675
|
-
i && i.length > 0 ? (this._name = i, this.hasExplicitName = !0) : (this._name = `c_${this.id}`, this.hasExplicitName = !1);
|
|
676
|
-
}
|
|
677
|
-
get lhs() {
|
|
678
|
-
return this._lhs;
|
|
679
|
-
}
|
|
680
|
-
set lhs(e) {
|
|
681
|
-
this._lhs !== e && (this._indicatorOrigin = void 0, this._indicatorSnapshot = void 0, this._lhs = e, T(this.ownerModel));
|
|
682
|
-
}
|
|
683
|
-
get sense() {
|
|
684
|
-
return this._sense;
|
|
685
|
-
}
|
|
686
|
-
set sense(e) {
|
|
687
|
-
if (e === "!=" && !Number.isSafeInteger(this._rhs)) throw Error(`Discrete "!=" in constraint "${this._name}" requires a safe integer RHS, received ${this._rhs}.`);
|
|
688
|
-
this._sense !== e && (this._indicatorOrigin = void 0, this._indicatorSnapshot = void 0, this._sense = e, T(this.ownerModel));
|
|
689
|
-
}
|
|
690
|
-
get rhs() {
|
|
691
|
-
return this._rhs;
|
|
692
|
-
}
|
|
693
|
-
set rhs(e) {
|
|
694
|
-
if (this._sense === "!=" && !Number.isSafeInteger(e)) throw Error(`Discrete "!=" in constraint "${this._name}" requires a safe integer RHS, received ${e}.`);
|
|
695
|
-
Object.is(this._rhs, e) || (this._indicatorOrigin = void 0, this._indicatorSnapshot = void 0, this._rhs = e, T(this.ownerModel));
|
|
696
|
-
}
|
|
697
|
-
get name() {
|
|
698
|
-
return this._name;
|
|
699
|
-
}
|
|
700
|
-
set name(e) {
|
|
701
|
-
this._name !== e && (this._name = e, T(this.ownerModel)), this.hasExplicitName = !0;
|
|
702
|
-
}
|
|
703
|
-
get _indicatorMeta() {
|
|
704
|
-
let e = this._indicatorOrigin, t = this._indicatorSnapshot;
|
|
705
|
-
if (!e || !t) return;
|
|
706
|
-
if (this._lhs !== t.lhsRef || this._sense !== t.sense || !Object.is(this._rhs, t.rhs) || !Object.is(this._lhs.constant, t.constant) || this._lhs.nonlinearTerms.length > 0 || this._lhs.terms.size !== t.terms.size) {
|
|
707
|
-
this._indicatorOrigin = void 0, this._indicatorSnapshot = void 0;
|
|
708
|
-
return;
|
|
709
|
-
}
|
|
710
|
-
for (let [e, n] of t.terms) if (!this._lhs.terms.has(e) || !Object.is(this._lhs.terms.get(e), n)) {
|
|
711
|
-
this._indicatorOrigin = void 0, this._indicatorSnapshot = void 0;
|
|
712
|
-
return;
|
|
713
|
-
}
|
|
714
|
-
let n = e.targetRhs - (e.targetLhs.constant ?? 0), r = 0;
|
|
715
|
-
for (let [t, n] of e.targetLhs.terms) {
|
|
716
|
-
if (n === 0) continue;
|
|
717
|
-
let i = e.targetSense === "<=" ? n > 0 ? t.ub : t.lb : n > 0 ? t.lb : t.ub;
|
|
718
|
-
if (!Number.isFinite(i) || (r += n * i, !Number.isFinite(r))) return;
|
|
719
|
-
}
|
|
720
|
-
if (!((e.targetSense === "<=" ? r - n : n - r) > e.derivedM + 1e-9)) return e;
|
|
721
|
-
}
|
|
722
|
-
_setIndicatorOrigin(e) {
|
|
723
|
-
if (!e) {
|
|
724
|
-
this._indicatorOrigin = void 0, this._indicatorSnapshot = void 0;
|
|
725
|
-
return;
|
|
726
|
-
}
|
|
727
|
-
this._indicatorOrigin = e, this._indicatorSnapshot = {
|
|
728
|
-
lhsRef: this._lhs,
|
|
729
|
-
terms: new Map(this._lhs.terms),
|
|
730
|
-
constant: this._lhs.constant,
|
|
731
|
-
sense: this._sense,
|
|
732
|
-
rhs: this._rhs
|
|
733
|
-
};
|
|
734
|
-
}
|
|
735
|
-
get _requiredDiscreteVars() {
|
|
736
|
-
return this._discreteVars;
|
|
737
|
-
}
|
|
738
|
-
_setDiscreteVars(e) {
|
|
739
|
-
this._discreteVars = e;
|
|
740
|
-
}
|
|
741
|
-
_assignModelName(e, t) {
|
|
742
|
-
this._name = e, this.hasExplicitName = t;
|
|
743
|
-
}
|
|
744
|
-
and(e) {
|
|
745
|
-
return new a("and", [this, e]);
|
|
746
|
-
}
|
|
747
|
-
or(e) {
|
|
748
|
-
return new a("or", [this, e]);
|
|
749
|
-
}
|
|
750
|
-
implies(e) {
|
|
751
|
-
return new a("implies", [this, e]);
|
|
752
|
-
}
|
|
753
|
-
xor(e) {
|
|
754
|
-
return new a("xor", [this, e]);
|
|
755
|
-
}
|
|
756
|
-
not() {
|
|
757
|
-
return new a("not", [this]);
|
|
758
|
-
}
|
|
759
|
-
add(e) {
|
|
760
|
-
throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
761
|
-
}
|
|
762
|
-
sub(e) {
|
|
763
|
-
throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
764
|
-
}
|
|
765
|
-
mul(e) {
|
|
766
|
-
throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
767
|
-
}
|
|
768
|
-
div(e) {
|
|
769
|
-
throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
770
|
-
}
|
|
771
|
-
neg() {
|
|
772
|
-
throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
773
|
-
}
|
|
774
|
-
le(e, t) {
|
|
775
|
-
throw Error("Cannot compare a Constraint in (<=): expected a number, Var, or LinExpr.");
|
|
776
|
-
}
|
|
777
|
-
ge(e, t) {
|
|
778
|
-
throw Error("Cannot compare a Constraint in (>=): expected a number, Var, or LinExpr.");
|
|
779
|
-
}
|
|
780
|
-
eq(e, t) {
|
|
781
|
-
throw Error("Cannot compare a Constraint in (==): expected a number, Var, or LinExpr.");
|
|
782
|
-
}
|
|
783
|
-
lt(e, t) {
|
|
784
|
-
throw Error("Cannot compare a Constraint in (<): expected a number, Var, or LinExpr.");
|
|
785
|
-
}
|
|
786
|
-
gt(e, t) {
|
|
787
|
-
throw Error("Cannot compare a Constraint in (>): expected a number, Var, or LinExpr.");
|
|
788
|
-
}
|
|
789
|
-
ne(e, t) {
|
|
790
|
-
throw Error("Cannot compare a Constraint in (!=): expected a number, Var, or LinExpr.");
|
|
791
|
-
}
|
|
792
|
-
get value() {
|
|
793
|
-
throw Error("Cannot read numeric value of a Constraint: expected a Var, LinExpr, or QuadExpr.");
|
|
794
|
-
}
|
|
795
|
-
get constant() {
|
|
796
|
-
throw Error("Cannot read numeric constant of a Constraint: expected a LinExpr or QuadExpr.");
|
|
797
|
-
}
|
|
798
|
-
clone() {
|
|
799
|
-
let t = new e(this._lhs.clone(), this._sense, this._rhs, this.hasExplicitName ? this._name : void 0);
|
|
800
|
-
return this._discreteVars && t._setDiscreteVars(new Set(this._discreteVars)), t;
|
|
801
|
-
}
|
|
802
|
-
toString() {
|
|
803
|
-
let e = this.sense === "==" ? "=" : this.sense;
|
|
804
|
-
return `${this.name}: ${this.lhs.toString()} ${e} ${this.rhs}`;
|
|
805
|
-
}
|
|
806
|
-
};
|
|
807
|
-
function I(e) {
|
|
808
|
-
return e instanceof F || typeof e == "object" && !!e && "sense" in e && "lhs" in e && "rhs" in e;
|
|
809
|
-
}
|
|
810
|
-
//#endregion
|
|
811
|
-
//#region src/modeler/expression.ts
|
|
812
|
-
function L(e) {
|
|
813
|
-
switch (e.kind) {
|
|
814
|
-
case "abs": return {
|
|
815
|
-
kind: "abs",
|
|
816
|
-
arg: e.arg.clone()
|
|
817
|
-
};
|
|
818
|
-
case "max": return {
|
|
819
|
-
kind: "max",
|
|
820
|
-
args: e.args.map((e) => e.clone())
|
|
821
|
-
};
|
|
822
|
-
case "min": return {
|
|
823
|
-
kind: "min",
|
|
824
|
-
args: e.args.map((e) => e.clone())
|
|
825
|
-
};
|
|
826
|
-
case "at": return {
|
|
827
|
-
kind: "at",
|
|
828
|
-
array: e.array.map((e) => e.clone()),
|
|
829
|
-
index: e.index.clone(),
|
|
830
|
-
discreteVars: new Set(e.discreteVars)
|
|
831
|
-
};
|
|
832
|
-
}
|
|
833
|
-
}
|
|
834
|
-
function ce(e) {
|
|
835
|
-
switch (e.kind) {
|
|
836
|
-
case "abs": return `abs(${e.arg.toString()})`;
|
|
837
|
-
case "max": return `max(${e.args.map((e) => e.toString()).join(", ")})`;
|
|
838
|
-
case "min": return `min(${e.args.map((e) => e.toString()).join(", ")})`;
|
|
839
|
-
case "at": return `at([${e.array.map((e) => e.toString()).join(", ")}], ${e.index.toString()})`;
|
|
840
|
-
}
|
|
841
|
-
}
|
|
842
|
-
function R(e) {
|
|
843
|
-
switch (e.kind) {
|
|
844
|
-
case "abs": {
|
|
845
|
-
let t = e.arg.value;
|
|
846
|
-
return t === null ? null : Math.abs(t);
|
|
847
|
-
}
|
|
848
|
-
case "max": {
|
|
849
|
-
let t = -Infinity;
|
|
850
|
-
for (let n of e.args) {
|
|
851
|
-
let e = n.value;
|
|
852
|
-
if (e === null) return null;
|
|
853
|
-
e > t && (t = e);
|
|
854
|
-
}
|
|
855
|
-
return t;
|
|
856
|
-
}
|
|
857
|
-
case "min": {
|
|
858
|
-
let t = Infinity;
|
|
859
|
-
for (let n of e.args) {
|
|
860
|
-
let e = n.value;
|
|
861
|
-
if (e === null) return null;
|
|
862
|
-
e < t && (t = e);
|
|
863
|
-
}
|
|
864
|
-
return t;
|
|
865
|
-
}
|
|
866
|
-
case "at": {
|
|
867
|
-
let t = e.index.value;
|
|
868
|
-
if (t === null || !Number.isFinite(t)) return null;
|
|
869
|
-
let n = Math.round(t);
|
|
870
|
-
return Math.abs(t - n) > 1e-6 || n < 0 || n >= e.array.length ? null : e.array[n].value;
|
|
871
|
-
}
|
|
872
|
-
}
|
|
873
|
-
}
|
|
874
|
-
function z(e) {
|
|
875
|
-
return e instanceof K || typeof e == "object" && !!e && !I(e) && typeof e.id == "number" && typeof e.name == "string" && typeof e.lb == "number" && typeof e.ub == "number" && typeof e.type == "string" && typeof e.mul == "function";
|
|
876
|
-
}
|
|
877
|
-
function B(e, t, n = /* @__PURE__ */ new Set()) {
|
|
878
|
-
if (typeof e == "number") {
|
|
879
|
-
if (!Number.isSafeInteger(e)) throw Error(`${t} requires Integer or Binary variables and integer constants/coefficients, but received constant ${e}.`);
|
|
880
|
-
return n;
|
|
881
|
-
}
|
|
882
|
-
if (I(e)) throw Error(`${t} cannot accept a Constraint comparison.`);
|
|
883
|
-
if (e instanceof b || "quadTerms" in e && e.quadTerms instanceof Map && "linearTerms" in e && e.linearTerms instanceof Map && typeof e.constant == "number") {
|
|
884
|
-
let r = e;
|
|
885
|
-
if (r.quadTerms.size > 0) throw Error(`${t} does not support quadratic expressions.`);
|
|
886
|
-
return B(new H(r.linearTerms, r.constant, r.nonlinearTerms ?? []), t, n);
|
|
887
|
-
}
|
|
888
|
-
if (z(e)) {
|
|
889
|
-
if (e.type !== "Integer" && e.type !== "Binary") throw Error(`${t} requires Integer or Binary variables, but involves Continuous variable "${e.name}".`);
|
|
890
|
-
return n.add(e), n;
|
|
891
|
-
}
|
|
892
|
-
if ("terms" in e && e.terms instanceof Map) {
|
|
893
|
-
let r = e, i = r.constant ?? 0;
|
|
894
|
-
if (!Number.isSafeInteger(i)) throw Error(`${t} requires Integer or Binary variables and integer constants/coefficients, but received constant ${i}.`);
|
|
895
|
-
for (let [e, i] of r.terms) if (i !== 0) {
|
|
896
|
-
if (e.type !== "Integer" && e.type !== "Binary") throw Error(`${t} requires Integer or Binary variables, but involves Continuous variable "${e.name}".`);
|
|
897
|
-
if (!Number.isSafeInteger(i)) throw Error(`${t} requires integer coefficients, but variable "${e.name}" has coefficient ${i}.`);
|
|
898
|
-
n.add(e);
|
|
899
|
-
}
|
|
900
|
-
for (let e of r.nonlinearTerms ?? []) {
|
|
901
|
-
if (e.coeff === 0) continue;
|
|
902
|
-
if (!Number.isSafeInteger(e.coeff)) throw Error(`${t} requires integer coefficients on nonlinear expressions, received ${e.coeff}.`);
|
|
903
|
-
let r = e.atom;
|
|
904
|
-
if (r.kind === "abs") B(r.arg, t, n);
|
|
905
|
-
else if (r.kind === "max" || r.kind === "min") for (let e of r.args) B(e, t, n);
|
|
906
|
-
else if (r.kind === "at") {
|
|
907
|
-
B(r.index, t, n);
|
|
908
|
-
for (let e of r.array) B(e, t, n);
|
|
909
|
-
}
|
|
910
|
-
}
|
|
911
|
-
return n;
|
|
912
|
-
}
|
|
913
|
-
throw Error(`${t} expected an integer-valued scalar expression.`);
|
|
914
|
-
}
|
|
915
|
-
function V(e, t) {
|
|
916
|
-
if (typeof e == "number") {
|
|
917
|
-
if (!Number.isFinite(e)) throw Error(`Invalid non-finite number "${e}" in ${t}.`);
|
|
918
|
-
return new H(/* @__PURE__ */ new Map(), Object.is(e, -0) ? 0 : e);
|
|
919
|
-
}
|
|
920
|
-
if (I(e)) throw t.startsWith("sum") ? Error(`Cannot sum a constraint comparison (<=, >=, ==, !=, <, >): expected arithmetic expressions in ${t}.`) : Error(`Cannot use a constraint comparison (<=, >=, ==, !=, <, >) in ${t}: expected a finite number, Var, or LinExpr.`);
|
|
921
|
-
if (e instanceof b || typeof e == "object" && e && "quadTerms" in e && e.quadTerms instanceof Map && "linearTerms" in e && e.linearTerms instanceof Map) {
|
|
922
|
-
let n = e;
|
|
923
|
-
if (n.quadTerms.size > 0) throw Error(`Quadratic expressions are not supported in ${t}.`);
|
|
924
|
-
if (!Number.isFinite(n.constant)) throw Error(`Invalid non-finite constant "${n.constant}" in ${t}.`);
|
|
925
|
-
for (let [, e] of n.linearTerms) if (!Number.isFinite(e)) throw Error(`Invalid non-finite coefficient "${e}" in ${t}.`);
|
|
926
|
-
return new H(n.linearTerms, n.constant, n.nonlinearTerms ?? []);
|
|
927
|
-
}
|
|
928
|
-
if (e instanceof H || typeof e == "object" && e && "terms" in e && e.terms instanceof Map && typeof e.constant == "number") {
|
|
929
|
-
let n = e;
|
|
930
|
-
if (!Number.isFinite(n.constant)) throw Error(`Invalid non-finite constant "${n.constant}" in ${t}.`);
|
|
931
|
-
for (let [, e] of n.terms) if (!Number.isFinite(e)) throw Error(`Invalid non-finite coefficient "${e}" in ${t}.`);
|
|
932
|
-
return n.clone();
|
|
933
|
-
}
|
|
934
|
-
if (z(e)) return new H(/* @__PURE__ */ new Map([[e, 1]]), 0);
|
|
935
|
-
throw Error(`Unsupported item in ${t}: expected a finite number, Var, or LinExpr.`);
|
|
936
|
-
}
|
|
937
|
-
var H = class e {
|
|
938
|
-
kind = "linear";
|
|
939
|
-
terms;
|
|
940
|
-
constant;
|
|
941
|
-
nonlinearTerms;
|
|
942
|
-
constructor(e = /* @__PURE__ */ new Map(), t = 0, n = []) {
|
|
943
|
-
this.terms = new Map(e), this.constant = t, this.nonlinearTerms = n.filter((e) => e.coeff !== 0).map((e) => ({
|
|
944
|
-
atom: L(e.atom),
|
|
945
|
-
coeff: e.coeff
|
|
946
|
-
}));
|
|
947
|
-
}
|
|
948
|
-
hasNonlinearTerms() {
|
|
949
|
-
return this.nonlinearTerms.length > 0;
|
|
950
|
-
}
|
|
951
|
-
setTerm(e, t) {
|
|
952
|
-
return t === 0 ? this.terms.delete(e) : this.terms.set(e, t), this;
|
|
953
|
-
}
|
|
954
|
-
addTerm(e, t) {
|
|
955
|
-
let n = (this.terms.get(e) ?? 0) + t;
|
|
956
|
-
return n === 0 ? this.terms.delete(e) : this.terms.set(e, n), this;
|
|
957
|
-
}
|
|
958
|
-
clone() {
|
|
959
|
-
return new e(this.terms, this.constant, this.nonlinearTerms);
|
|
960
|
-
}
|
|
961
|
-
add(e) {
|
|
962
|
-
if (I(e)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
963
|
-
if (e instanceof b) return e.add(this);
|
|
964
|
-
let t = this.clone();
|
|
965
|
-
if (typeof e == "number") t.constant += e;
|
|
966
|
-
else if ("terms" in e) {
|
|
967
|
-
for (let [n, r] of e.terms) {
|
|
968
|
-
let e = (t.terms.get(n) ?? 0) + r;
|
|
969
|
-
e === 0 ? t.terms.delete(n) : t.terms.set(n, e);
|
|
970
|
-
}
|
|
971
|
-
if (t.constant += e.constant, Array.isArray(e.nonlinearTerms)) for (let n of e.nonlinearTerms) n.coeff !== 0 && t.nonlinearTerms.push({
|
|
972
|
-
atom: L(n.atom),
|
|
973
|
-
coeff: n.coeff
|
|
974
|
-
});
|
|
975
|
-
} else {
|
|
976
|
-
let n = (t.terms.get(e) ?? 0) + 1;
|
|
977
|
-
n === 0 ? t.terms.delete(e) : t.terms.set(e, n);
|
|
978
|
-
}
|
|
979
|
-
return t;
|
|
980
|
-
}
|
|
981
|
-
sub(e) {
|
|
982
|
-
if (I(e)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
983
|
-
return e instanceof b ? e.neg().add(this) : typeof e == "number" ? this.add(-e) : this.add(e.mul(-1));
|
|
984
|
-
}
|
|
985
|
-
mul(t) {
|
|
986
|
-
if (I(t)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
987
|
-
if (typeof t == "number") {
|
|
988
|
-
let n = this.terms.size > 0 || this.nonlinearTerms.length > 0, r = this.constant === 0 && n ? 0 : this.constant * t;
|
|
989
|
-
if (this.constant !== 0 && t !== 0 && r === 0) throw Error("Constant multiplication underflows to zero.");
|
|
990
|
-
let i = new e(/* @__PURE__ */ new Map(), r);
|
|
991
|
-
for (let [e, n] of this.terms) {
|
|
992
|
-
let r = n * t;
|
|
993
|
-
if (n !== 0 && t !== 0 && r === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
994
|
-
r !== 0 && i.terms.set(e, r);
|
|
995
|
-
}
|
|
996
|
-
if (t !== 0) for (let e of this.nonlinearTerms) {
|
|
997
|
-
let n = e.coeff * t;
|
|
998
|
-
if (e.coeff !== 0 && n === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
999
|
-
n !== 0 && i.nonlinearTerms.push({
|
|
1000
|
-
atom: L(e.atom),
|
|
1001
|
-
coeff: n
|
|
1002
|
-
});
|
|
1003
|
-
}
|
|
1004
|
-
return i;
|
|
1005
|
-
}
|
|
1006
|
-
if (t instanceof K || typeof t == "object" && t && "id" in t && !("terms" in t) && !("quadTerms" in t)) {
|
|
1007
|
-
if (this.hasNonlinearTerms()) throw Error("Cannot multiply nonlinear expression (max, min, abs, or at) by a variable expression: only scalar multiplication is supported.");
|
|
1008
|
-
return b.multiplyLinearTerms(this.constant, this.terms, 0, /* @__PURE__ */ new Map([[t, 1]]));
|
|
1009
|
-
}
|
|
1010
|
-
if (t instanceof b) {
|
|
1011
|
-
let e = t.linearTerms.size > 0 || t.quadTerms.size > 0 || t.hasNonlinearTerms();
|
|
1012
|
-
if (this.hasNonlinearTerms()) {
|
|
1013
|
-
if (!e) return this.mul(t.constant);
|
|
1014
|
-
throw Error("Cannot multiply nonlinear expression (max, min, abs, or at) by a variable expression: only scalar multiplication is supported.");
|
|
1015
|
-
}
|
|
1016
|
-
if (t.hasNonlinearTerms()) {
|
|
1017
|
-
if (this.terms.size === 0) return t.mul(this.constant);
|
|
1018
|
-
throw Error("Cannot multiply nonlinear expression (max, min, abs, or at) by a variable expression: only scalar multiplication is supported.");
|
|
1019
|
-
}
|
|
1020
|
-
return this.terms.size === 0 ? t.mul(this.constant) : new b(this.terms, /* @__PURE__ */ new Map(), this.constant).mul(t);
|
|
1021
|
-
}
|
|
1022
|
-
let n = Array.isArray(t.nonlinearTerms) && t.nonlinearTerms.length > 0;
|
|
1023
|
-
if (this.hasNonlinearTerms()) {
|
|
1024
|
-
if (t.terms.size === 0 && !n) return this.mul(t.constant);
|
|
1025
|
-
throw Error("Cannot multiply nonlinear expression (max, min, abs, or at) by a variable expression: only scalar multiplication is supported.");
|
|
1026
|
-
}
|
|
1027
|
-
if (n) {
|
|
1028
|
-
if (this.terms.size === 0) return t.mul(this.constant);
|
|
1029
|
-
throw Error("Cannot multiply nonlinear expression (max, min, abs, or at) by a variable expression: only scalar multiplication is supported.");
|
|
1030
|
-
}
|
|
1031
|
-
return this.terms.size === 0 ? t.mul(this.constant) : t.terms.size === 0 ? this.mul(t.constant) : b.multiplyLinearTerms(this.constant, this.terms, t.constant, t.terms);
|
|
1032
|
-
}
|
|
1033
|
-
div(e) {
|
|
1034
|
-
let t = b.extractScalarDivisor(e, "LinExpr.div()");
|
|
1035
|
-
return this.mul(1 / t);
|
|
1036
|
-
}
|
|
1037
|
-
neg() {
|
|
1038
|
-
return this.mul(-1);
|
|
1039
|
-
}
|
|
1040
|
-
get value() {
|
|
1041
|
-
let e = this.constant;
|
|
1042
|
-
for (let [t, n] of this.terms) {
|
|
1043
|
-
if (t.value === null) return null;
|
|
1044
|
-
e += n * t.value;
|
|
1045
|
-
}
|
|
1046
|
-
for (let t of this.nonlinearTerms) {
|
|
1047
|
-
let n = R(t.atom);
|
|
1048
|
-
if (n === null) return null;
|
|
1049
|
-
e += t.coeff * n;
|
|
1050
|
-
}
|
|
1051
|
-
return e;
|
|
1052
|
-
}
|
|
1053
|
-
le(e, t) {
|
|
1054
|
-
return this.toConstraint("<=", e, t);
|
|
1055
|
-
}
|
|
1056
|
-
ge(e, t) {
|
|
1057
|
-
return this.toConstraint(">=", e, t);
|
|
1058
|
-
}
|
|
1059
|
-
eq(e, t) {
|
|
1060
|
-
return this.toConstraint("==", e, t);
|
|
1061
|
-
}
|
|
1062
|
-
lt(e, t) {
|
|
1063
|
-
return this.toDiscreteComparison("<", e, t);
|
|
1064
|
-
}
|
|
1065
|
-
gt(e, t) {
|
|
1066
|
-
return this.toDiscreteComparison(">", e, t);
|
|
1067
|
-
}
|
|
1068
|
-
ne(e, t) {
|
|
1069
|
-
return this.toDiscreteComparison("!=", e, t);
|
|
1070
|
-
}
|
|
1071
|
-
toDiscreteComparison(e, t, n) {
|
|
1072
|
-
if (t instanceof F || typeof t == "object" && t && "sense" in t && "lhs" in t) throw Error(`Cannot compare against a Constraint in (${e}): expected a finite integer, discrete Var, or discrete LinExpr.`);
|
|
1073
|
-
let r = `discrete ${e}`, i = /* @__PURE__ */ new Set();
|
|
1074
|
-
B(this, r, i), B(t, r, i);
|
|
1075
|
-
let a = this.toConstraint(e === "<" ? "<=" : e === ">" ? ">=" : "!=", t, n);
|
|
1076
|
-
if (e === "<") {
|
|
1077
|
-
let e = a.rhs - 1;
|
|
1078
|
-
if (!Number.isSafeInteger(e)) throw Error(`Discrete comparison (<) adjusted right-hand side (${e}) exceeds safe integer range.`);
|
|
1079
|
-
a.rhs = e;
|
|
1080
|
-
} else if (e === ">") {
|
|
1081
|
-
let e = a.rhs + 1;
|
|
1082
|
-
if (!Number.isSafeInteger(e)) throw Error(`Discrete comparison (>) adjusted right-hand side (${e}) exceeds safe integer range.`);
|
|
1083
|
-
a.rhs = e;
|
|
1084
|
-
} else if (!Number.isSafeInteger(a.rhs)) throw Error(`Discrete comparison (!=) adjusted right-hand side (${a.rhs}) exceeds safe integer range.`);
|
|
1085
|
-
return a._setDiscreteVars(i), a;
|
|
1086
|
-
}
|
|
1087
|
-
toConstraint(t, n, r) {
|
|
1088
|
-
let i = this.clone(), a = 0;
|
|
1089
|
-
if (typeof n == "number") a = n;
|
|
1090
|
-
else if (n instanceof F || typeof n == "object" && n && "sense" in n && "lhs" in n) throw Error(`Cannot compare against a Constraint in (${t}): expected a number, Var, or LinExpr.`);
|
|
1091
|
-
else if (n instanceof b || typeof n == "object" && n && "quadTerms" in n) {
|
|
1092
|
-
let r = n;
|
|
1093
|
-
if (r.quadTerms.size > 0) throw Error(`Quadratic expressions are not supported in linear constraints (${t}): quadratic terms are only allowed in objectives.`);
|
|
1094
|
-
i = i.sub(new e(r.linearTerms, 0, r.nonlinearTerms ?? [])), a = r.constant;
|
|
1095
|
-
} else "terms" in n ? (i = i.sub(new e(n.terms, 0, n.nonlinearTerms ?? [])), a = n.constant) : i = i.sub(n);
|
|
1096
|
-
let o = a - i.constant;
|
|
1097
|
-
return i.constant = 0, new F(i, t, o, r);
|
|
1098
|
-
}
|
|
1099
|
-
and(e) {
|
|
1100
|
-
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==, !=, <, >) in logical 'and': expected a BoolVar, Constraint, or BoolOp.");
|
|
1101
|
-
}
|
|
1102
|
-
or(e) {
|
|
1103
|
-
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==, !=, <, >) in logical 'or': expected a BoolVar, Constraint, or BoolOp.");
|
|
1104
|
-
}
|
|
1105
|
-
implies(e) {
|
|
1106
|
-
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==, !=, <, >) in logical 'implies': expected a BoolVar, Constraint, or BoolOp.");
|
|
1107
|
-
}
|
|
1108
|
-
xor(e) {
|
|
1109
|
-
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==, !=, <, >) in logical 'xor': expected a BoolVar, Constraint, or BoolOp.");
|
|
1110
|
-
}
|
|
1111
|
-
not() {
|
|
1112
|
-
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==, !=, <, >) in logical 'not': expected a BoolVar, Constraint, or BoolOp.");
|
|
1113
|
-
}
|
|
1114
|
-
toString() {
|
|
1115
|
-
let e = [], t = !0;
|
|
1116
|
-
for (let [n, r] of this.terms) {
|
|
1117
|
-
let i = r >= 0 ? t ? "" : "+ " : "- ", a = Math.abs(r), o = a === 1 ? "" : `${a} `;
|
|
1118
|
-
e.push(`${i}${o}${n.name}`), t = !1;
|
|
1119
|
-
}
|
|
1120
|
-
for (let n of this.nonlinearTerms) {
|
|
1121
|
-
let r = n.coeff, i = r >= 0 ? t ? "" : "+ " : "- ", a = Math.abs(r), o = a === 1 ? "" : `${a} `;
|
|
1122
|
-
e.push(`${i}${o}${ce(n.atom)}`), t = !1;
|
|
1123
|
-
}
|
|
1124
|
-
if (this.constant !== 0 || e.length === 0) {
|
|
1125
|
-
let n = this.constant >= 0 ? t ? "" : "+ " : "- ";
|
|
1126
|
-
e.push(`${n}${Math.abs(this.constant)}`);
|
|
1127
|
-
}
|
|
1128
|
-
return e.join(" ") || "0";
|
|
1129
|
-
}
|
|
1130
|
-
};
|
|
1131
|
-
function le(e, t) {
|
|
1132
|
-
let n = /* @__PURE__ */ new Map(), r = [], i = 0;
|
|
1133
|
-
function a(e) {
|
|
1134
|
-
return e instanceof F || e.kind === "constraint" || "sense" in e && (e.sense === "<=" || 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;
|
|
1135
|
-
}
|
|
1136
|
-
function o(e) {
|
|
1137
|
-
return e instanceof b || "quadTerms" in e && e.quadTerms instanceof Map && "linearTerms" in e && e.linearTerms instanceof Map && typeof e.constant == "number";
|
|
1138
|
-
}
|
|
1139
|
-
function s(e) {
|
|
1140
|
-
return e instanceof H || "terms" in e && e.terms instanceof Map && typeof e.constant == "number";
|
|
1141
|
-
}
|
|
1142
|
-
function c(e) {
|
|
1143
|
-
return e instanceof K || !a(e) && typeof e.id == "number" && typeof e.name == "string" && typeof e.lb == "number" && typeof e.ub == "number" && typeof e.type == "string" && typeof e.mul == "function";
|
|
1144
|
-
}
|
|
1145
|
-
function l(e) {
|
|
1146
|
-
return a(e) || o(e) || s(e) || c(e);
|
|
1147
|
-
}
|
|
1148
|
-
function u(e) {
|
|
1149
|
-
if (typeof e != "object" || !e || Array.isArray(e) || l(e)) return !1;
|
|
1150
|
-
let t = Object.getPrototypeOf(e);
|
|
1151
|
-
return t === Object.prototype || t === null;
|
|
1152
|
-
}
|
|
1153
|
-
function d(e) {
|
|
1154
|
-
if (e != null) {
|
|
1155
|
-
if (typeof e == "number") {
|
|
1156
|
-
if (!Number.isFinite(e)) throw Error(`Invalid non-finite number "${e}" in sum().`);
|
|
1157
|
-
let t = i + e;
|
|
1158
|
-
if (!Number.isFinite(t)) throw Error("Constant accumulation overflows finite floating-point range in sum().");
|
|
1159
|
-
i = t;
|
|
1160
|
-
} else if (typeof e == "object" && a(e)) throw Error("Cannot sum a constraint comparison (<=, >=, ==) in sum(): expected a number, Var, or LinExpr.");
|
|
1161
|
-
else if (typeof e == "object" && o(e)) {
|
|
1162
|
-
let t = e;
|
|
1163
|
-
if (t.quadTerms.size > 0) throw Error("Quadratic expressions are not supported in sum(): sum() produces a linear expression (LinExpr).");
|
|
1164
|
-
if (!Number.isFinite(t.constant)) throw Error(`Invalid non-finite constant "${t.constant}" in sum().`);
|
|
1165
|
-
for (let [e, r] of t.linearTerms) {
|
|
1166
|
-
if (!Number.isFinite(r)) throw Error(`Invalid non-finite coefficient "${r}" in sum().`);
|
|
1167
|
-
let t = (n.get(e) ?? 0) + r;
|
|
1168
|
-
if (!Number.isFinite(t)) throw Error("Coefficient accumulation overflows finite floating-point range in sum().");
|
|
1169
|
-
n.set(e, t);
|
|
1170
|
-
}
|
|
1171
|
-
let a = i + t.constant;
|
|
1172
|
-
if (!Number.isFinite(a)) throw Error("Constant accumulation overflows finite floating-point range in sum().");
|
|
1173
|
-
if (i = a, Array.isArray(t.nonlinearTerms)) for (let e of t.nonlinearTerms) {
|
|
1174
|
-
if (!Number.isFinite(e.coeff)) throw Error(`Invalid non-finite coefficient "${e.coeff}" in sum().`);
|
|
1175
|
-
e.coeff !== 0 && r.push({
|
|
1176
|
-
atom: L(e.atom),
|
|
1177
|
-
coeff: e.coeff
|
|
1178
|
-
});
|
|
1179
|
-
}
|
|
1180
|
-
} else if (typeof e == "object" && s(e)) {
|
|
1181
|
-
let t = e;
|
|
1182
|
-
if (!Number.isFinite(t.constant)) throw Error(`Invalid non-finite constant "${t.constant}" in sum().`);
|
|
1183
|
-
for (let [e, r] of t.terms) {
|
|
1184
|
-
if (!Number.isFinite(r)) throw Error(`Invalid non-finite coefficient "${r}" in sum().`);
|
|
1185
|
-
let t = (n.get(e) ?? 0) + r;
|
|
1186
|
-
if (!Number.isFinite(t)) throw Error("Coefficient accumulation overflows finite floating-point range in sum().");
|
|
1187
|
-
n.set(e, t);
|
|
1188
|
-
}
|
|
1189
|
-
let a = i + t.constant;
|
|
1190
|
-
if (!Number.isFinite(a)) throw Error("Constant accumulation overflows finite floating-point range in sum().");
|
|
1191
|
-
if (i = a, Array.isArray(t.nonlinearTerms)) for (let e of t.nonlinearTerms) {
|
|
1192
|
-
if (!Number.isFinite(e.coeff)) throw Error(`Invalid non-finite coefficient "${e.coeff}" in sum().`);
|
|
1193
|
-
e.coeff !== 0 && r.push({
|
|
1194
|
-
atom: L(e.atom),
|
|
1195
|
-
coeff: e.coeff
|
|
1196
|
-
});
|
|
1197
|
-
}
|
|
1198
|
-
} else if (typeof e == "object" && c(e)) {
|
|
1199
|
-
let t = e, r = (n.get(t) ?? 0) + 1;
|
|
1200
|
-
if (!Number.isFinite(r)) throw Error("Coefficient accumulation overflows finite floating-point range in sum().");
|
|
1201
|
-
n.set(t, r);
|
|
1202
|
-
} else throw Error("Unsupported item in sum(): expected a number, Var, or LinExpr.");
|
|
1203
|
-
}
|
|
1204
|
-
}
|
|
1205
|
-
function f(e, t = 0) {
|
|
1206
|
-
if (e != null) {
|
|
1207
|
-
if (typeof e == "number") {
|
|
1208
|
-
d(e);
|
|
1209
|
-
return;
|
|
1210
|
-
}
|
|
1211
|
-
if (typeof e == "object") {
|
|
1212
|
-
if (l(e)) {
|
|
1213
|
-
d(e);
|
|
1214
|
-
return;
|
|
1215
|
-
}
|
|
1216
|
-
if (!(e instanceof String) && !(e instanceof Map) && Symbol.iterator in e && typeof e[Symbol.iterator] == "function") {
|
|
1217
|
-
if (t >= 2) throw Error("Nested containers deeper than 2D are not supported in sum().");
|
|
1218
|
-
for (let n of e) f(n, t + 1);
|
|
1219
|
-
return;
|
|
1220
|
-
}
|
|
1221
|
-
if (u(e)) {
|
|
1222
|
-
if (t >= 2) throw Error("Nested containers deeper than 2D are not supported in sum().");
|
|
1223
|
-
if (Object.getOwnPropertySymbols(e).length > 0) throw Error("Symbol keys are not supported in sum().");
|
|
1224
|
-
for (let n of Object.keys(e)) f(e[n], t + 1);
|
|
1225
|
-
return;
|
|
1226
|
-
}
|
|
1227
|
-
}
|
|
1228
|
-
d(e);
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
1231
|
-
if (typeof e == "number") {
|
|
1232
|
-
if (typeof t != "function") throw Error("sum(count, mapFn) requires a mapping function as the second argument.");
|
|
1233
|
-
if (!Number.isInteger(e) || e < 0) throw Error("sum(count, mapFn) requires a non-negative integer count.");
|
|
1234
|
-
let n = e;
|
|
1235
|
-
for (let e = 0; e < n; e++) f(t(e, e), 0);
|
|
1236
|
-
} else if (typeof e == "object" && e && !(e instanceof String) && (!(e instanceof Map) || typeof t == "function") && Symbol.iterator in e && typeof e[Symbol.iterator] == "function") {
|
|
1237
|
-
let n = 0;
|
|
1238
|
-
for (let r of e) t ? f(t(r, n++), 0) : f(r, 1);
|
|
1239
|
-
} else if (u(e)) {
|
|
1240
|
-
if (Object.getOwnPropertySymbols(e).length > 0) throw Error("Symbol keys are not supported in sum().");
|
|
1241
|
-
let n = 0;
|
|
1242
|
-
for (let r of Object.keys(e)) {
|
|
1243
|
-
let i = e[r];
|
|
1244
|
-
t ? f(t(i, n++), 0) : f(i, 1);
|
|
1245
|
-
}
|
|
1246
|
-
} else throw Error("Unsupported item in sum(): expected an Iterable, Record, or (count, mapFn).");
|
|
1247
|
-
for (let [e, t] of n) t === 0 && n.delete(e);
|
|
1248
|
-
return new H(n, i, r);
|
|
1249
|
-
}
|
|
1250
|
-
function ue(e, t) {
|
|
1251
|
-
let n = /* @__PURE__ */ new Map(), r = [], i = 0;
|
|
1252
|
-
function a(e) {
|
|
1253
|
-
return typeof e == "object" && !!e && !(e instanceof F) && typeof e.id == "number" && typeof e.name == "string" && typeof e.lb == "number" && typeof e.ub == "number" && typeof e.type == "string" && typeof e.mul == "function" && typeof e.le == "function";
|
|
1254
|
-
}
|
|
1255
|
-
function o(e) {
|
|
1256
|
-
return a(e) || e instanceof H || e instanceof b;
|
|
1257
|
-
}
|
|
1258
|
-
function s(e, t, o) {
|
|
1259
|
-
if (!Number.isFinite(e)) throw Error(`Invalid non-finite coefficient "${e}" in dot() at '${o || "root"}'.`);
|
|
1260
|
-
if (t instanceof b) throw Error(`Quadratic expressions are not supported in dot() at '${o || "root"}': dot() produces a linear expression (LinExpr).`);
|
|
1261
|
-
if (t instanceof H) {
|
|
1262
|
-
if (!Number.isFinite(t.constant)) throw Error(`Invalid non-finite constant "${t.constant}" in dot() at '${o || "root"}'.`);
|
|
1263
|
-
let a = e === 0 || t.constant === 0 ? 0 : e * t.constant;
|
|
1264
|
-
if (e !== 0 && t.constant !== 0 && a === 0) throw Error("Constant multiplication underflows to zero.");
|
|
1265
|
-
if (!Number.isFinite(a)) throw Error(`Coefficient multiplication overflows finite floating-point range in dot() at '${o || "root"}'.`);
|
|
1266
|
-
let s = i + a;
|
|
1267
|
-
if (!Number.isFinite(s)) throw Error(`Constant accumulation overflows finite floating-point range in dot() at '${o || "root"}'.`);
|
|
1268
|
-
i = s;
|
|
1269
|
-
for (let [r, i] of t.terms) {
|
|
1270
|
-
if (!Number.isFinite(i)) throw Error(`Invalid non-finite coefficient "${i}" in dot() at '${o || "root"}'.`);
|
|
1271
|
-
let t = e * i;
|
|
1272
|
-
if (e !== 0 && i !== 0 && t === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
1273
|
-
if (!Number.isFinite(t)) throw Error(`Coefficient multiplication overflows finite floating-point range in dot() at '${o || "root"}'.`);
|
|
1274
|
-
let a = (n.get(r) ?? 0) + t;
|
|
1275
|
-
if (!Number.isFinite(a)) throw Error(`Coefficient accumulation overflows finite floating-point range in dot() at '${o || "root"}'.`);
|
|
1276
|
-
n.set(r, a);
|
|
1277
|
-
}
|
|
1278
|
-
if (Array.isArray(t.nonlinearTerms) && e !== 0) for (let n of t.nonlinearTerms) {
|
|
1279
|
-
if (!Number.isFinite(n.coeff)) throw Error(`Invalid non-finite coefficient "${n.coeff}" in dot() at '${o || "root"}'.`);
|
|
1280
|
-
let t = e * n.coeff;
|
|
1281
|
-
if (n.coeff !== 0 && t === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
1282
|
-
if (!Number.isFinite(t)) throw Error(`Coefficient multiplication overflows finite floating-point range in dot() at '${o || "root"}'.`);
|
|
1283
|
-
t !== 0 && r.push({
|
|
1284
|
-
atom: L(n.atom),
|
|
1285
|
-
coeff: t
|
|
1286
|
-
});
|
|
1287
|
-
}
|
|
1288
|
-
} else if (a(t)) {
|
|
1289
|
-
let r = t, i = (n.get(r) ?? 0) + e;
|
|
1290
|
-
if (!Number.isFinite(i)) throw Error(`Coefficient accumulation overflows finite floating-point range in dot() at '${o || "root"}'.`);
|
|
1291
|
-
n.set(r, i);
|
|
1292
|
-
} else throw Error(`Cannot multiply number by non-variable/expression operand in dot() at '${o || "root"}'.`);
|
|
1293
|
-
}
|
|
1294
|
-
function c(e) {
|
|
1295
|
-
return typeof e == "object" && !!e && !Array.isArray(e) && !o(e) && !(e instanceof F) && e.kind !== "constraint";
|
|
1296
|
-
}
|
|
1297
|
-
function l(e, t, n = "") {
|
|
1298
|
-
if (e === void 0 || t === void 0 || e === null || t === null) throw Error(`Nullish or undefined operand encountered in dot() at '${n || "root"}'.`);
|
|
1299
|
-
if (typeof e == "number" && typeof t == "number") throw Error(`Cannot multiply two numbers in dot() at '${n || "root"}': expected one numeric coefficient and one Var or LinExpr.`);
|
|
1300
|
-
if (typeof e == "number") {
|
|
1301
|
-
s(e, t, n);
|
|
1302
|
-
return;
|
|
1303
|
-
}
|
|
1304
|
-
if (typeof t == "number") {
|
|
1305
|
-
s(t, e, n);
|
|
1306
|
-
return;
|
|
1307
|
-
}
|
|
1308
|
-
if (Array.isArray(e) || Array.isArray(t)) {
|
|
1309
|
-
if (Array.isArray(e) && Array.isArray(t)) {
|
|
1310
|
-
if (e.length !== t.length) throw Error(`Array length mismatch in dot() at '${n || "root"}': received lengths ${e.length} and ${t.length}.`);
|
|
1311
|
-
for (let r = 0; r < e.length; r++) l(e[r], t[r], n ? `${n}[${r}]` : `[${r}]`);
|
|
1312
|
-
return;
|
|
1313
|
-
}
|
|
1314
|
-
let r = Array.isArray(e) ? e : t, i = Array.isArray(e) ? t : e;
|
|
1315
|
-
if (c(i)) {
|
|
1316
|
-
if (Object.getOwnPropertySymbols(i).length > 0) throw Error(`Symbol keys are not supported in dot() at '${n || "root"}'.`);
|
|
1317
|
-
let a = Object.keys(i);
|
|
1318
|
-
if (a.length === r.length && a.every((e) => /^(?:0|[1-9]\d*)$/.test(e))) {
|
|
1319
|
-
let a = [];
|
|
1320
|
-
for (let e = 0; e < r.length; e++) Object.prototype.hasOwnProperty.call(i, e) || a.push(e);
|
|
1321
|
-
if (a.length > 0) throw Error(`Key mismatch in dot() at '${n || "root"}': missing indices [${a.join(", ")}] in numeric record.`);
|
|
1322
|
-
for (let i = 0; i < r.length; i++) l(e[i], t[i], n ? `${n}[${i}]` : `[${i}]`);
|
|
1323
|
-
return;
|
|
1324
|
-
}
|
|
1325
|
-
throw a.length > 0 && a.every((e) => /^(?:0|[1-9]\d*)$/.test(e)) ? Error(`Array length mismatch in dot() at '${n || "root"}': array has length ${r.length}, but object has ${a.length} indices.`) : Error(`Type mismatch in dot() at '${n || "root"}': one operand is an Array while the other is an Object with keys [${a.slice(0, 5).join(", ")}].`);
|
|
1326
|
-
}
|
|
1327
|
-
throw Error(`Type mismatch in dot() at '${n || "root"}': one operand is an Array while the other is not.`);
|
|
1328
|
-
}
|
|
1329
|
-
if (c(e) && c(t)) {
|
|
1330
|
-
if (Object.getOwnPropertySymbols(e).length > 0 || Object.getOwnPropertySymbols(t).length > 0) throw Error(`Symbol keys are not supported in dot() at '${n || "root"}'.`);
|
|
1331
|
-
let r = Object.keys(e), i = Object.keys(t), a = r.filter((e) => !Object.prototype.hasOwnProperty.call(t, e)), o = i.filter((t) => !Object.prototype.hasOwnProperty.call(e, t));
|
|
1332
|
-
if (a.length > 0 || o.length > 0) {
|
|
1333
|
-
let e = [];
|
|
1334
|
-
throw a.length > 0 && e.push(`missing in second operand: [${a.join(", ")}]`), o.length > 0 && e.push(`missing in first operand: [${o.join(", ")}]`), Error(`Key mismatch in dot() at '${n || "root"}': ${e.join("; ")}.`);
|
|
1335
|
-
}
|
|
1336
|
-
for (let i of r) l(e[i], t[i], n ? `${n}.${i}` : i);
|
|
1337
|
-
return;
|
|
1338
|
-
}
|
|
1339
|
-
throw Error(`Unsupported operand types in dot() at '${n || "root"}'.`);
|
|
1340
|
-
}
|
|
1341
|
-
l(e, t);
|
|
1342
|
-
for (let [e, t] of n) t === 0 && n.delete(e);
|
|
1343
|
-
return new H(n, i, r);
|
|
1344
|
-
}
|
|
1345
|
-
function de(e) {
|
|
1346
|
-
let t = V(e, "abs()");
|
|
1347
|
-
return t.terms.size === 0 && t.nonlinearTerms.length === 0 ? new H(/* @__PURE__ */ new Map(), Math.abs(t.constant) + 0) : new H(/* @__PURE__ */ new Map(), 0, [{
|
|
1348
|
-
atom: {
|
|
1349
|
-
kind: "abs",
|
|
1350
|
-
arg: t
|
|
1351
|
-
},
|
|
1352
|
-
coeff: 1
|
|
1353
|
-
}]);
|
|
1354
|
-
}
|
|
1355
|
-
function U(e, t) {
|
|
1356
|
-
if (t.length === 0) throw Error(`${e}() requires at least one expression.`);
|
|
1357
|
-
let n = [], r = (e) => typeof e == "number" || I(e) || z(e) || e instanceof H || e instanceof b || typeof e == "object" && !!e && ("terms" in e && e.terms instanceof Map || "quadTerms" in e && e.quadTerms instanceof Map), i = (e) => {
|
|
1358
|
-
if (typeof e != "object" || !e || Array.isArray(e) || r(e)) return !1;
|
|
1359
|
-
let t = Object.getPrototypeOf(e);
|
|
1360
|
-
return t === Object.prototype || t === null;
|
|
1361
|
-
}, a = (t) => {
|
|
1362
|
-
n.push(V(t, `${e}()`));
|
|
1363
|
-
}, o = (t, n) => {
|
|
1364
|
-
if (t == null) throw Error(`Unsupported nullish item in ${e}(): expected a finite number, Var, or LinExpr.`);
|
|
1365
|
-
if (typeof t == "number" || r(t)) {
|
|
1366
|
-
a(t);
|
|
1367
|
-
return;
|
|
1368
|
-
}
|
|
1369
|
-
if (Array.isArray(t)) {
|
|
1370
|
-
if (n >= 2) throw Error(`Nested containers deeper than 2D are not supported in ${e}().`);
|
|
1371
|
-
for (let r = 0; r < t.length; r++) {
|
|
1372
|
-
if (!(r in t)) throw Error(`Unsupported item in ${e}(): sparse array hole at index ${r}.`);
|
|
1373
|
-
o(t[r], n + 1);
|
|
1374
|
-
}
|
|
1375
|
-
return;
|
|
1376
|
-
}
|
|
1377
|
-
if (typeof t == "object" && !(t instanceof String) && !(t instanceof Map) && Symbol.iterator in t && typeof t[Symbol.iterator] == "function") {
|
|
1378
|
-
if (n >= 2) throw Error(`Nested containers deeper than 2D are not supported in ${e}().`);
|
|
1379
|
-
for (let e of t) o(e, n + 1);
|
|
1380
|
-
return;
|
|
1381
|
-
}
|
|
1382
|
-
if (i(t)) {
|
|
1383
|
-
if (n >= 2) throw Error(`Nested containers deeper than 2D are not supported in ${e}().`);
|
|
1384
|
-
if (Object.getOwnPropertySymbols(t).length > 0) throw Error(`Symbol keys are not supported in ${e}().`);
|
|
1385
|
-
for (let e of Object.keys(t)) o(t[e], n + 1);
|
|
1386
|
-
return;
|
|
1387
|
-
}
|
|
1388
|
-
throw Error(`Unsupported item in ${e}(): expected a finite number, Var, or LinExpr.`);
|
|
1389
|
-
};
|
|
1390
|
-
if (t.length === 2 && typeof t[1] == "function") {
|
|
1391
|
-
let n = t[0], r = t[1];
|
|
1392
|
-
if (typeof n == "number") {
|
|
1393
|
-
if (!Number.isInteger(n) || n < 0) throw Error(`${e}(count, mapFn) requires a non-negative integer count.`);
|
|
1394
|
-
for (let e = 0; e < n; e++) o(r(e, e), 0);
|
|
1395
|
-
} else if (Array.isArray(n)) for (let t = 0; t < n.length; t++) {
|
|
1396
|
-
if (!(t in n)) throw Error(`Unsupported item in ${e}(): sparse array hole at index ${t}.`);
|
|
1397
|
-
o(r(n[t], t), 0);
|
|
1398
|
-
}
|
|
1399
|
-
else if (typeof n == "object" && n && !(n instanceof String) && Symbol.iterator in n && typeof n[Symbol.iterator] == "function") {
|
|
1400
|
-
let e = 0;
|
|
1401
|
-
for (let t of n) o(r(t, e++), 0);
|
|
1402
|
-
} else if (i(n)) {
|
|
1403
|
-
if (Object.getOwnPropertySymbols(n).length > 0) throw Error(`Symbol keys are not supported in ${e}().`);
|
|
1404
|
-
let t = 0;
|
|
1405
|
-
for (let e of Object.keys(n)) o(r(n[e], t++), 0);
|
|
1406
|
-
} else throw Error(`Invalid first argument in ${e}(itemsOrCount, mapFn): expected an Iterable, Record, or non-negative integer count.`);
|
|
1407
|
-
} else for (let n of t) {
|
|
1408
|
-
if (typeof n == "function") throw Error(`Function interpolation in ${e}() is only supported as ${e}(itemsOrCount, mapFn).`);
|
|
1409
|
-
o(n, 0);
|
|
1410
|
-
}
|
|
1411
|
-
if (n.length === 0) throw Error(`${e}() requires at least one expression.`);
|
|
1412
|
-
return n;
|
|
1413
|
-
}
|
|
1414
|
-
function W(e, t) {
|
|
1415
|
-
if (t.length === 1) return t[0].clone();
|
|
1416
|
-
if (t.every((e) => e.terms.size === 0 && e.nonlinearTerms.length === 0)) {
|
|
1417
|
-
let n = t.map((e) => e.constant), r = e === "max" ? Math.max(...n) : Math.min(...n);
|
|
1418
|
-
return new H(/* @__PURE__ */ new Map(), Object.is(r, -0) ? 0 : r);
|
|
1419
|
-
}
|
|
1420
|
-
return new H(/* @__PURE__ */ new Map(), 0, [{
|
|
1421
|
-
atom: {
|
|
1422
|
-
kind: e,
|
|
1423
|
-
args: t
|
|
1424
|
-
},
|
|
1425
|
-
coeff: 1
|
|
1426
|
-
}]);
|
|
1427
|
-
}
|
|
1428
|
-
function fe(...e) {
|
|
1429
|
-
return W("max", U("max", e));
|
|
1430
|
-
}
|
|
1431
|
-
function pe(...e) {
|
|
1432
|
-
return W("min", U("min", e));
|
|
1433
|
-
}
|
|
1434
|
-
function me(e, t) {
|
|
1435
|
-
if (!Array.isArray(e)) throw Error("at(array, index) requires an Array as its first argument.");
|
|
1436
|
-
if (e.length === 0) throw Error("at(array, index) requires an array with at least 1 element.");
|
|
1437
|
-
let n = Array(e.length);
|
|
1438
|
-
for (let t = 0; t < e.length; t++) {
|
|
1439
|
-
if (!(t in e)) throw Error(`at(array, index) encountered sparse array hole at index ${t}.`);
|
|
1440
|
-
n[t] = V(e[t], `at() array[${t}]`);
|
|
1441
|
-
}
|
|
1442
|
-
let r = V(t, "at() index"), i = B(r, "at() index");
|
|
1443
|
-
return new H(/* @__PURE__ */ new Map(), 0, [{
|
|
1444
|
-
atom: {
|
|
1445
|
-
kind: "at",
|
|
1446
|
-
array: n,
|
|
1447
|
-
index: r,
|
|
1448
|
-
discreteVars: i
|
|
1449
|
-
},
|
|
1450
|
-
coeff: 1
|
|
1451
|
-
}]);
|
|
1452
|
-
}
|
|
1453
|
-
//#endregion
|
|
1454
|
-
//#region src/modeler/variable.ts
|
|
1455
|
-
var he = 0;
|
|
1456
|
-
function G(e) {
|
|
1457
|
-
if (e === void 0) return "Continuous";
|
|
1458
|
-
if (typeof e == "string") {
|
|
1459
|
-
let t = e.toLowerCase();
|
|
1460
|
-
if (t === "binary" || t === "bin") return "Binary";
|
|
1461
|
-
if (t === "integer" || t === "int") return "Integer";
|
|
1462
|
-
if (t === "continuous" || t === "cont") return "Continuous";
|
|
1463
|
-
}
|
|
1464
|
-
throw Error(`Invalid variable type "${String(e)}": expected "Continuous", "Integer", or "Binary".`);
|
|
1465
|
-
}
|
|
1466
|
-
var K = class {
|
|
1467
|
-
id;
|
|
1468
|
-
_name;
|
|
1469
|
-
prefix;
|
|
1470
|
-
hasExplicitName;
|
|
1471
|
-
ownerModel = null;
|
|
1472
|
-
_type;
|
|
1473
|
-
_lb;
|
|
1474
|
-
_ub;
|
|
1475
|
-
_initialValue;
|
|
1476
|
-
value = null;
|
|
1477
|
-
reducedCost = null;
|
|
1478
|
-
constructor(e = {}) {
|
|
1479
|
-
this.id = ++he;
|
|
1480
|
-
let t = typeof e.name == "string" ? e.name.trim() : "", n = typeof e.prefix == "string" ? e.prefix.trim() : "";
|
|
1481
|
-
this.prefix = n.length > 0 ? n : "x", this.hasExplicitName = t.length > 0, this._name = this.hasExplicitName ? t : `${this.prefix}_${this.id}`, this._type = G(e.type), this._type === "Binary" ? (this._lb = 0, this._ub = 1) : (this._lb = e.lb === void 0 ? 0 : e.lb, this._ub = e.ub === void 0 ? Infinity : e.ub), this._initialValue = e.initialValue === void 0 ? void 0 : Object.is(e.initialValue, -0) ? 0 : e.initialValue, this._initialValue !== void 0 && O(this);
|
|
1482
|
-
}
|
|
1483
|
-
get name() {
|
|
1484
|
-
return this._name;
|
|
1485
|
-
}
|
|
1486
|
-
get type() {
|
|
1487
|
-
return this._type;
|
|
1488
|
-
}
|
|
1489
|
-
set type(e) {
|
|
1490
|
-
let t = G(e);
|
|
1491
|
-
this._type !== t && (this._type = t, T(this.ownerModel));
|
|
1492
|
-
}
|
|
1493
|
-
get lb() {
|
|
1494
|
-
return this._lb;
|
|
1495
|
-
}
|
|
1496
|
-
set lb(e) {
|
|
1497
|
-
Object.is(this._lb, e) || (this._lb = e, T(this.ownerModel));
|
|
1498
|
-
}
|
|
1499
|
-
get ub() {
|
|
1500
|
-
return this._ub;
|
|
1501
|
-
}
|
|
1502
|
-
set ub(e) {
|
|
1503
|
-
Object.is(this._ub, e) || (this._ub = e, T(this.ownerModel));
|
|
1504
|
-
}
|
|
1505
|
-
get initialValue() {
|
|
1506
|
-
return this._initialValue;
|
|
1507
|
-
}
|
|
1508
|
-
set initialValue(e) {
|
|
1509
|
-
let t = e !== void 0 && Object.is(e, -0) ? 0 : e;
|
|
1510
|
-
Object.is(this._initialValue, t) || (this._initialValue = t, T(this.ownerModel));
|
|
1511
|
-
}
|
|
1512
|
-
_assignModelName(e) {
|
|
1513
|
-
this._name = e;
|
|
1514
|
-
}
|
|
1515
|
-
mul(e) {
|
|
1516
|
-
return typeof e == "number" ? new H(/* @__PURE__ */ new Map([[this, 1]]), 0).mul(e) : new b(/* @__PURE__ */ new Map([[this, 1]]), /* @__PURE__ */ new Map(), 0).mul(e);
|
|
1517
|
-
}
|
|
1518
|
-
add(e) {
|
|
1519
|
-
return e instanceof b ? e.add(this) : new H(/* @__PURE__ */ new Map([[this, 1]]), 0).add(e);
|
|
1520
|
-
}
|
|
1521
|
-
sub(e) {
|
|
1522
|
-
return e instanceof b ? e.neg().add(this) : new H(/* @__PURE__ */ new Map([[this, 1]]), 0).sub(e);
|
|
1523
|
-
}
|
|
1524
|
-
neg() {
|
|
1525
|
-
return this.mul(-1);
|
|
1526
|
-
}
|
|
1527
|
-
le(e, t) {
|
|
1528
|
-
return new H(/* @__PURE__ */ new Map([[this, 1]]), 0).le(e, t);
|
|
1529
|
-
}
|
|
1530
|
-
ge(e, t) {
|
|
1531
|
-
return new H(/* @__PURE__ */ new Map([[this, 1]]), 0).ge(e, t);
|
|
1532
|
-
}
|
|
1533
|
-
eq(e, t) {
|
|
1534
|
-
return new H(/* @__PURE__ */ new Map([[this, 1]]), 0).eq(e, t);
|
|
1535
|
-
}
|
|
1536
|
-
lt(e, t) {
|
|
1537
|
-
return new H(/* @__PURE__ */ new Map([[this, 1]]), 0).lt(e, t);
|
|
1538
|
-
}
|
|
1539
|
-
gt(e, t) {
|
|
1540
|
-
return new H(/* @__PURE__ */ new Map([[this, 1]]), 0).gt(e, t);
|
|
1541
|
-
}
|
|
1542
|
-
ne(e, t) {
|
|
1543
|
-
return new H(/* @__PURE__ */ new Map([[this, 1]]), 0).ne(e, t);
|
|
1544
|
-
}
|
|
1545
|
-
toString() {
|
|
1546
|
-
return this.name;
|
|
1547
|
-
}
|
|
1548
|
-
};
|
|
1549
|
-
//#endregion
|
|
1550
|
-
//#region src/modeler/lp-writer.ts
|
|
1551
|
-
function ge(e) {
|
|
1552
|
-
return typeof e == "object" && !!e && typeof e.lpString == "string" && e.nameToVar instanceof Map && e.nameToConstraint instanceof Map;
|
|
1553
|
-
}
|
|
1554
|
-
function q(e, t = "x", n = 0) {
|
|
1555
|
-
let r = e.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
1556
|
-
return /^[0-9]/.test(r) && (r = `${t}_${r}`), (!r || r.length === 0) && (r = `${t}_${n}`), r;
|
|
1557
|
-
}
|
|
1558
|
-
function J(e) {
|
|
1559
|
-
let t = e.variables || [], n = e.constraints || [], r = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Set();
|
|
1560
|
-
for (let e = 0; e < t.length; e++) {
|
|
1561
|
-
let n = t[e];
|
|
1562
|
-
s.set(n, e);
|
|
1563
|
-
let a = q(n.name, "x", e), o = a, l = 1;
|
|
1564
|
-
for (; c.has(o);) o = `${a}_${l++}`;
|
|
1565
|
-
c.add(o), i.set(n, o), r.set(o, n);
|
|
1566
|
-
}
|
|
1567
|
-
let l = /* @__PURE__ */ new Set();
|
|
1568
|
-
for (let e = 0; e < n.length; e++) {
|
|
1569
|
-
let t = n[e], r = q(t.name, "c", e + 1), i = r, s = 1;
|
|
1570
|
-
for (; l.has(i);) i = `${r}_${s++}`;
|
|
1571
|
-
l.add(i), o.set(t, i), a.set(i, t);
|
|
1572
|
-
}
|
|
1573
|
-
return {
|
|
1574
|
-
nameToVar: r,
|
|
1575
|
-
varToName: i,
|
|
1576
|
-
nameToConstraint: a,
|
|
1577
|
-
constraintToName: o,
|
|
1578
|
-
constrToName: o,
|
|
1579
|
-
varOrder: s
|
|
1580
|
-
};
|
|
1581
|
-
}
|
|
1582
|
-
function _e(e, t) {
|
|
1583
|
-
let { sense: n, objective: r, constraints: i, variables: a } = e;
|
|
1584
|
-
if (typeof r.hasNonlinearTerms == "function" && r.hasNonlinearTerms()) throw Error("generateLpString() received an unlowered nonlinear objective (max/min/abs/at). Lower via Model.toLpString() or linearizeModelForMip() first.");
|
|
1585
|
-
let o = t?.relax === !0, { nameToVar: s, varToName: c, nameToConstraint: l, constrToName: u, varOrder: d } = J(e), f = [];
|
|
1586
|
-
f.push(n && n.toLowerCase() === "maximize" ? "Maximize" : "Minimize");
|
|
1587
|
-
let p = [], m = !0, h = r instanceof b ? r.linearTerms : r.terms;
|
|
1588
|
-
if (h) for (let [e, t] of h) {
|
|
1589
|
-
if (t === 0) continue;
|
|
1590
|
-
let n = c.get(e), r = t >= 0 ? m ? "" : "+ " : "- ", i = Math.abs(t), a = i === 1 ? "" : `${i} `;
|
|
1591
|
-
p.push(`${r}${a}${n}`), m = !1;
|
|
1592
|
-
}
|
|
1593
|
-
if (r instanceof b && r.quadTerms.size > 0) {
|
|
1594
|
-
let e = [], t = !0, n = Array.from(r.quadTerms.values()).filter((e) => e.coeff !== 0).map((e) => {
|
|
1595
|
-
let t = d.get(e.var1) ?? e.var1.id, n = d.get(e.var2) ?? e.var2.id, [r, i, a, o] = t <= n ? [
|
|
1596
|
-
e.var1,
|
|
1597
|
-
e.var2,
|
|
1598
|
-
t,
|
|
1599
|
-
n
|
|
1600
|
-
] : [
|
|
1601
|
-
e.var2,
|
|
1602
|
-
e.var1,
|
|
1603
|
-
n,
|
|
1604
|
-
t
|
|
1605
|
-
];
|
|
1606
|
-
return {
|
|
1607
|
-
firstVar: r,
|
|
1608
|
-
secondVar: i,
|
|
1609
|
-
firstIdx: a,
|
|
1610
|
-
secondIdx: o,
|
|
1611
|
-
coeff: e.coeff
|
|
1612
|
-
};
|
|
1613
|
-
}).sort((e, t) => e.firstIdx - t.firstIdx || e.secondIdx - t.secondIdx);
|
|
1614
|
-
for (let r of n) {
|
|
1615
|
-
let n = 2 * r.coeff, i = n >= 0 ? t ? "" : "+ " : "- ", a = Math.abs(n), o = a === 1 ? "" : `${a} `, s = c.get(r.firstVar), l = c.get(r.secondVar);
|
|
1616
|
-
r.firstVar === r.secondVar ? e.push(`${i}${o}${s} ^ 2`) : e.push(`${i}${o}${s} * ${l}`), t = !1;
|
|
1617
|
-
}
|
|
1618
|
-
if (e.length > 0) {
|
|
1619
|
-
let t = `[ ${e.join(" ")} ] / 2`;
|
|
1620
|
-
p.length > 0 ? p.push("+", t) : p.push(t);
|
|
1621
|
-
}
|
|
1622
|
-
}
|
|
1623
|
-
let g = p.length > 0 ? p.join(" ") : "0";
|
|
1624
|
-
f.push(` obj: ${g}`), f.push("Subject To");
|
|
1625
|
-
for (let e of i) {
|
|
1626
|
-
if (e.sense === "!=" || e.lhs.hasNonlinearTerms()) throw Error(`generateLpString() received unlowered constraint "${e.name}" (${e.sense === "!=" ? "discrete !=" : "nonlinear max/min/abs/at"}). Lower via Model.toLpString() or linearizeModelForMip() first.`);
|
|
1627
|
-
let t = u.get(e), n = [], r = !0;
|
|
1628
|
-
for (let [t, i] of e.lhs.terms) {
|
|
1629
|
-
if (i === 0) continue;
|
|
1630
|
-
let e = c.get(t), a = i >= 0 ? r ? "" : "+ " : "- ", o = Math.abs(i), s = o === 1 ? "" : `${o} `;
|
|
1631
|
-
n.push(`${a}${s}${e}`), r = !1;
|
|
1632
|
-
}
|
|
1633
|
-
let i = n.length > 0 ? n.join(" ") : "0", a = e.sense === "==" ? "=" : e.sense;
|
|
1634
|
-
f.push(` ${t}: ${i} ${a} ${e.rhs}`);
|
|
1635
|
-
}
|
|
1636
|
-
f.push("Bounds");
|
|
1637
|
-
let _ = [], v = [];
|
|
1638
|
-
for (let e of a) {
|
|
1639
|
-
let t = c.get(e);
|
|
1640
|
-
if (!o && e.type === "Binary") {
|
|
1641
|
-
v.push(t);
|
|
1642
|
-
continue;
|
|
1643
|
-
}
|
|
1644
|
-
!o && e.type === "Integer" && _.push(t);
|
|
1645
|
-
let n = e.lb === -Infinity, r = e.ub === Infinity;
|
|
1646
|
-
n && r ? f.push(` ${t} free`) : n && !r ? f.push(` -infinity <= ${t} <= ${e.ub}`) : !n && r ? (e.lb !== 0 || e.initialValue !== void 0) && f.push(` ${t} >= ${e.lb}`) : f.push(` ${e.lb} <= ${t} <= ${e.ub}`);
|
|
1647
|
-
}
|
|
1648
|
-
if (_.length > 0) {
|
|
1649
|
-
f.push("Generals");
|
|
1650
|
-
for (let e = 0; e < _.length; e += 10) f.push(" " + _.slice(e, e + 10).join(" "));
|
|
1651
|
-
}
|
|
1652
|
-
if (v.length > 0) {
|
|
1653
|
-
f.push("Binary");
|
|
1654
|
-
for (let e = 0; e < v.length; e += 10) f.push(" " + v.slice(e, e + 10).join(" "));
|
|
1655
|
-
}
|
|
1656
|
-
return f.push("End"), {
|
|
1657
|
-
lpString: f.join("\n") + "\n",
|
|
1658
|
-
nameToVar: s,
|
|
1659
|
-
nameToConstraint: l
|
|
1660
|
-
};
|
|
1661
|
-
}
|
|
1662
|
-
//#endregion
|
|
1663
|
-
//#region src/modeler/solvers/registry.ts
|
|
1664
|
-
var Y = Symbol.for("washi.solverRegistry"), X = Symbol.for("washi.defaultSolverFactory"), Z = globalThis;
|
|
1665
|
-
Z[Y] || (Z[Y] = /* @__PURE__ */ new Map());
|
|
1666
|
-
var Q = Z[Y];
|
|
1667
|
-
function ve(e, t) {
|
|
1668
|
-
Q.set(e.toLowerCase(), t);
|
|
1669
|
-
}
|
|
1670
|
-
function ye(e) {
|
|
1671
|
-
if (typeof e == "string") {
|
|
1672
|
-
let t = Q.get(e.toLowerCase());
|
|
1673
|
-
if (!t) throw Error(`Solver '${e}' is not registered.`);
|
|
1674
|
-
Z[X] = t;
|
|
1675
|
-
} else Z[X] = typeof e == "function" ? e : () => e;
|
|
1676
|
-
}
|
|
1677
|
-
function be(e, t) {
|
|
1678
|
-
if (!e) {
|
|
1679
|
-
let e = Z[X] || Q.get("highs");
|
|
1680
|
-
if (!e) throw Error(`No default solver available. Available solvers: ${$().join(", ")}`);
|
|
1681
|
-
return e(t);
|
|
1682
|
-
}
|
|
1683
|
-
if (typeof e == "string") {
|
|
1684
|
-
let n = e.toLowerCase(), r = Q.get(n);
|
|
1685
|
-
if (!r) throw Error(n === "glpk" ? `Solver 'glpk' not found. Available solvers: ${$().join(", ")}. Please import '@ohtaman/washi/solvers/glpk' before selecting this solver.` : n === "z3" ? `Solver 'z3' not found. Available solvers: ${$().join(", ")}. Please import '@ohtaman/washi/solvers/z3' before selecting this solver.` : n === "pumpkin" ? `Solver 'pumpkin' not found. Available solvers: ${$().join(", ")}. Please import '@ohtaman/washi/solvers/pumpkin' before selecting this solver.` : `Solver '${e}' not found. Available solvers: ${$().join(", ")}`);
|
|
1686
|
-
return r(t);
|
|
1687
|
-
}
|
|
1688
|
-
return e;
|
|
1689
|
-
}
|
|
1690
|
-
function $() {
|
|
1691
|
-
return Array.from(Q.keys());
|
|
1692
|
-
}
|
|
1693
|
-
//#endregion
|
|
1694
|
-
export { O as A, m as B, ee as C, P as D, N as E, d as F, e as G, c as H, v as I, o as L, r as M, a as N, oe as O, i as P, _ as R, ae as S, re as T, s as U, l as V, u as W, le as _, J as a, ne as b, q as c, de as d, me as f, pe as g, fe as h, ye as i, b as j, M as k, K as l, ue as m, $ as n, _e as o, L as p, ve as r, ge as s, be as t, H as u, F as v, ie as w, te as x, E as y, g as z };
|