@ohtaman/washi 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/README.ja.md +111 -118
  2. package/README.md +109 -116
  3. package/README.zh-CN.md +112 -119
  4. package/dist/browser/highs-worker.js +21 -3
  5. package/dist/browser/loader-CtaDHef_.js +273 -0
  6. package/dist/browser/solvers/glpk.js +433 -286
  7. package/dist/browser/solvers/glpk.min.js +1 -1
  8. package/dist/browser/solvers/z3.js +1002 -981
  9. package/dist/browser/solvers/z3.min.js +2 -2
  10. package/dist/browser/washi.js +2178 -622
  11. package/dist/browser/washi.min.js +4 -4
  12. package/dist/lib/highs-worker.js +21 -3
  13. package/dist/lib/loader-DmNC_UNk.js +279 -0
  14. package/dist/lib/registry-BEfvpZW3.js +951 -0
  15. package/dist/lib/solvers/glpk.js +201 -90
  16. package/dist/lib/solvers/z3.js +399 -259
  17. package/dist/lib/washi.js +1674 -932
  18. package/dist/types/index.d.ts +4 -4
  19. package/dist/types/index.d.ts.map +1 -1
  20. package/dist/types/modeler/big-m.d.ts +75 -0
  21. package/dist/types/modeler/big-m.d.ts.map +1 -0
  22. package/dist/types/modeler/boolean-expr.d.ts +103 -14
  23. package/dist/types/modeler/boolean-expr.d.ts.map +1 -1
  24. package/dist/types/modeler/constraint.d.ts +36 -9
  25. package/dist/types/modeler/constraint.d.ts.map +1 -1
  26. package/dist/types/modeler/expression.d.ts +117 -27
  27. package/dist/types/modeler/expression.d.ts.map +1 -1
  28. package/dist/types/modeler/index.d.ts +10 -6
  29. package/dist/types/modeler/index.d.ts.map +1 -1
  30. package/dist/types/modeler/lp-writer.d.ts +27 -2
  31. package/dist/types/modeler/lp-writer.d.ts.map +1 -1
  32. package/dist/types/modeler/model.d.ts +491 -140
  33. package/dist/types/modeler/model.d.ts.map +1 -1
  34. package/dist/types/modeler/objective.d.ts +59 -0
  35. package/dist/types/modeler/objective.d.ts.map +1 -0
  36. package/dist/types/modeler/parser.d.ts +39 -14
  37. package/dist/types/modeler/parser.d.ts.map +1 -1
  38. package/dist/types/modeler/quad-expr.d.ts +28 -7
  39. package/dist/types/modeler/quad-expr.d.ts.map +1 -1
  40. package/dist/types/modeler/solution-validator.d.ts +93 -0
  41. package/dist/types/modeler/solution-validator.d.ts.map +1 -0
  42. package/dist/types/modeler/solvers/glpk/solver.d.ts +26 -2
  43. package/dist/types/modeler/solvers/glpk/solver.d.ts.map +1 -1
  44. package/dist/types/modeler/solvers/highs/loader.d.ts +36 -11
  45. package/dist/types/modeler/solvers/highs/loader.d.ts.map +1 -1
  46. package/dist/types/modeler/solvers/highs/solver.d.ts +11 -4
  47. package/dist/types/modeler/solvers/highs/solver.d.ts.map +1 -1
  48. package/dist/types/modeler/solvers/highs/worker-client.d.ts.map +1 -1
  49. package/dist/types/modeler/solvers/mock.d.ts +14 -5
  50. package/dist/types/modeler/solvers/mock.d.ts.map +1 -1
  51. package/dist/types/modeler/solvers/types.d.ts +71 -11
  52. package/dist/types/modeler/solvers/types.d.ts.map +1 -1
  53. package/dist/types/modeler/solvers/z3/loader.d.ts +2 -3
  54. package/dist/types/modeler/solvers/z3/loader.d.ts.map +1 -1
  55. package/dist/types/modeler/solvers/z3/solver.d.ts +15 -5
  56. package/dist/types/modeler/solvers/z3/solver.d.ts.map +1 -1
  57. package/dist/types/modeler/types.d.ts +60 -2
  58. package/dist/types/modeler/types.d.ts.map +1 -1
  59. package/dist/types/modeler/validation.d.ts +56 -0
  60. package/dist/types/modeler/validation.d.ts.map +1 -0
  61. package/dist/types/modeler/variable.d.ts +53 -13
  62. package/dist/types/modeler/variable.d.ts.map +1 -1
  63. package/docs/examples/diet-sensitivity.md +90 -0
  64. package/docs/examples/ecommerce-coupons.md +106 -0
  65. package/docs/examples/index.md +53 -0
  66. package/docs/examples/knapsack.md +60 -0
  67. package/docs/examples/portfolio-qp.md +83 -0
  68. package/docs/examples/production.md +51 -0
  69. package/docs/examples/shift-scheduling.md +175 -0
  70. package/docs/examples/smt-security.md +74 -0
  71. package/docs/examples/transportation.md +90 -0
  72. package/docs/examples/unsat-core.md +64 -0
  73. package/docs/getting-started/ai-agents.md +267 -0
  74. package/docs/getting-started/cross-origin.md +87 -0
  75. package/docs/getting-started/index.md +116 -0
  76. package/docs/getting-started/runtimes.md +227 -0
  77. package/docs/guide/constraints.md +177 -0
  78. package/docs/guide/expressions.md +204 -0
  79. package/docs/guide/objectives.md +170 -0
  80. package/docs/guide/unsat-core.md +147 -0
  81. package/docs/guide/variables.md +188 -0
  82. package/docs/ja/getting-started/ai-agents.md +266 -0
  83. package/docs/solvers/custom-solver.md +525 -0
  84. package/docs/solvers/glpk.md +66 -0
  85. package/docs/solvers/highs.md +93 -0
  86. package/docs/solvers/index.md +42 -0
  87. package/docs/solvers/z3.md +78 -0
  88. package/docs/zh/getting-started/ai-agents.md +266 -0
  89. package/llms-full.txt +3249 -0
  90. package/llms.txt +197 -0
  91. package/package.json +19 -6
  92. package/skills/washi/SKILL.md +197 -0
  93. package/dist/browser/loader-CVt4CloI.js +0 -58
  94. package/dist/lib/async-mutex-Duo4jwzn.js +0 -195
  95. package/dist/lib/constraint-BRuB3ooC.js +0 -90
  96. package/dist/lib/loader-Dti0v9yC.js +0 -50
  97. package/dist/lib/registry-DksqCSjI.js +0 -32
@@ -1,97 +1,176 @@
1
- import { a as e, c as t, i as n, l as r, n as i, o as a, r as o, s, t as c } from "./loader-CVt4CloI.js";
1
+ import { a as e, i as t, n, o as r, r as i, t as a } from "./loader-CtaDHef_.js";
2
2
  //#region src/modeler/types.ts
3
- function l(e) {
4
- return e && e.toLowerCase() === "maximize" ? "Maximize" : "Minimize";
3
+ function o(e) {
4
+ if (e === void 0) return "Minimize";
5
+ if (typeof e == "string") {
6
+ let t = e.toLowerCase();
7
+ if (t === "maximize") return "Maximize";
8
+ if (t === "minimize") return "Minimize";
9
+ }
10
+ throw Error(`Invalid optimization sense "${String(e)}": expected "Maximize", "Minimize", "maximize", or "minimize".`);
5
11
  }
6
12
  //#endregion
7
13
  //#region src/modeler/boolean-expr.ts
8
- var u = 0, d = class {
14
+ var s = 0;
15
+ function c(e, t) {
16
+ if (e instanceof l || e instanceof v) return e;
17
+ 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.`) : Error(`Invalid operand passed to logical operator "${t}": expected a BoolVar, BoolExpr, or Constraint.`);
18
+ }
19
+ var l = class {
9
20
  and(e) {
10
- return new p("and", [this, e]);
21
+ return new d("and", [this, c(e, "and")]);
11
22
  }
12
23
  or(e) {
13
- return new p("or", [this, e]);
24
+ return new d("or", [this, c(e, "or")]);
14
25
  }
15
26
  implies(e) {
16
- return new p("implies", [this, e]);
27
+ return new d("implies", [this, c(e, "implies")]);
17
28
  }
18
29
  xor(e) {
19
- return new p("xor", [this, e]);
30
+ return new d("xor", [this, c(e, "xor")]);
20
31
  }
21
32
  not() {
22
- return new p("not", [this]);
33
+ return new d("not", [this]);
23
34
  }
24
- }, f = class extends d {
35
+ }, u = class extends l {
25
36
  id;
26
37
  name;
27
38
  value = null;
28
39
  constructor(e) {
29
- super(), this.id = ++u, this.name = e || `p_${this.id}`;
40
+ super(), this.id = ++s, this.name = e || `p_${this.id}`;
30
41
  }
31
42
  toString() {
32
43
  return this.name;
33
44
  }
34
- }, p = class extends d {
45
+ }, d = class extends l {
35
46
  op;
36
47
  children;
37
48
  constructor(e, t) {
38
- super(), this.op = e, this.children = t;
49
+ super(), this.op = e, this.children = t.map((t) => c(t, e));
39
50
  }
40
51
  toString() {
41
52
  return this.op === "not" ? `not(${this.children[0].toString()})` : `(${this.children.map((e) => e.toString()).join(` ${this.op} `)})`;
42
53
  }
43
54
  };
44
- function m(...e) {
55
+ function f(...e) {
45
56
  if (e.length === 0) throw Error("and() requires at least one expression");
46
- return new p("and", e);
57
+ return new d("and", e);
47
58
  }
48
- function h(...e) {
59
+ function p(...e) {
49
60
  if (e.length === 0) throw Error("or() requires at least one expression");
50
- return new p("or", e);
61
+ return new d("or", e);
51
62
  }
52
- function g(e) {
53
- return new p("not", [e]);
63
+ function m(e) {
64
+ return new d("not", [e]);
54
65
  }
55
- function _(e, t) {
56
- return new p("implies", [e, t]);
66
+ function h(e, t) {
67
+ return new d("implies", [e, t]);
57
68
  }
58
- function v(...e) {
69
+ function g(...e) {
59
70
  if (e.length === 0) throw Error("xor() requires at least one expression");
60
- return new p("xor", e);
71
+ return new d("xor", e);
61
72
  }
62
73
  //#endregion
63
74
  //#region src/modeler/constraint.ts
64
- var ee = 0, y = class {
75
+ var _ = 0, v = class e {
76
+ kind = "constraint";
65
77
  id;
66
- lhs;
67
- sense;
68
- rhs;
69
- name;
78
+ _lhs;
79
+ _sense;
80
+ _rhs;
81
+ _name;
82
+ hasExplicitName;
83
+ ownerModel = null;
70
84
  slack = null;
71
85
  dual = null;
72
86
  constructor(e, t, n, r) {
73
- this.id = ++ee, this.lhs = e, this.sense = t, this.rhs = n, this.name = r || `c_${this.id}`;
87
+ this.id = ++_, this._lhs = e, this._sense = t, this._rhs = n;
88
+ let i = typeof r == "string" ? r.trim() : void 0;
89
+ i && i.length > 0 ? (this._name = i, this.hasExplicitName = !0) : (this._name = `c_${this.id}`, this.hasExplicitName = !1);
90
+ }
91
+ get lhs() {
92
+ return this._lhs;
93
+ }
94
+ set lhs(e) {
95
+ this._lhs !== e && (this._lhs = e, this.ownerModel?.notifyMutation?.());
96
+ }
97
+ get sense() {
98
+ return this._sense;
99
+ }
100
+ set sense(e) {
101
+ this._sense !== e && (this._sense = e, this.ownerModel?.notifyMutation?.());
102
+ }
103
+ get rhs() {
104
+ return this._rhs;
105
+ }
106
+ set rhs(e) {
107
+ Object.is(this._rhs, e) || (this._rhs = e, this.ownerModel?.notifyMutation?.());
108
+ }
109
+ get name() {
110
+ return this._name;
111
+ }
112
+ set name(e) {
113
+ this._name !== e && (this._name = e, this.ownerModel?.notifyMutation?.()), this.hasExplicitName = !0;
114
+ }
115
+ _assignModelName(e, t) {
116
+ this._name = e, this.hasExplicitName = t;
74
117
  }
75
118
  and(e) {
76
- return new p("and", [this, e]);
119
+ return new d("and", [this, e]);
77
120
  }
78
121
  or(e) {
79
- return new p("or", [this, e]);
122
+ return new d("or", [this, e]);
80
123
  }
81
124
  implies(e) {
82
- return new p("implies", [this, e]);
125
+ return new d("implies", [this, e]);
83
126
  }
84
127
  xor(e) {
85
- return new p("xor", [this, e]);
128
+ return new d("xor", [this, e]);
86
129
  }
87
130
  not() {
88
- return new p("not", [this]);
131
+ return new d("not", [this]);
132
+ }
133
+ add(e) {
134
+ throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
135
+ }
136
+ sub(e) {
137
+ throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
138
+ }
139
+ mul(e) {
140
+ throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
141
+ }
142
+ div(e) {
143
+ throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
144
+ }
145
+ neg() {
146
+ throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
147
+ }
148
+ le(e, t) {
149
+ throw Error("Cannot compare a Constraint in (<=): expected a number, Var, or LinExpr.");
150
+ }
151
+ ge(e, t) {
152
+ throw Error("Cannot compare a Constraint in (>=): expected a number, Var, or LinExpr.");
153
+ }
154
+ eq(e, t) {
155
+ throw Error("Cannot compare a Constraint in (==): expected a number, Var, or LinExpr.");
156
+ }
157
+ get value() {
158
+ throw Error("Cannot read numeric value of a Constraint: expected a Var, LinExpr, or QuadExpr.");
159
+ }
160
+ get constant() {
161
+ throw Error("Cannot read numeric constant of a Constraint: expected a LinExpr or QuadExpr.");
162
+ }
163
+ clone() {
164
+ return new e(this._lhs.clone(), this._sense, this._rhs, this.hasExplicitName ? this._name : void 0);
89
165
  }
90
166
  toString() {
91
167
  let e = this.sense === "==" ? "=" : this.sense;
92
168
  return `${this.name}: ${this.lhs.toString()} ${e} ${this.rhs}`;
93
169
  }
94
170
  };
171
+ function y(e) {
172
+ return e instanceof v || typeof e == "object" && !!e && "sense" in e && "lhs" in e && "rhs" in e;
173
+ }
95
174
  //#endregion
96
175
  //#region src/modeler/quad-expr.ts
97
176
  function b(e, t) {
@@ -99,6 +178,7 @@ function b(e, t) {
99
178
  return n <= r ? `${n}:${r}` : `${r}:${n}`;
100
179
  }
101
180
  var x = class e {
181
+ kind = "quadratic";
102
182
  linearTerms;
103
183
  quadTerms;
104
184
  constant;
@@ -115,55 +195,72 @@ var x = class e {
115
195
  return r ? r.coeff : 0;
116
196
  }
117
197
  add(t) {
198
+ if (y(t)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
118
199
  let n = this.clone();
119
200
  if (typeof t == "number") return n.constant += t, n;
120
201
  if (t instanceof e) {
121
202
  for (let [e, r] of t.quadTerms) {
122
203
  let t = n.quadTerms.get(e), i = (t ? t.coeff : 0) + r.coeff;
123
- Math.abs(i) < 1e-12 ? n.quadTerms.delete(e) : n.quadTerms.set(e, {
204
+ i === 0 ? n.quadTerms.delete(e) : n.quadTerms.set(e, {
124
205
  var1: r.var1,
125
206
  var2: r.var2,
126
207
  coeff: i
127
208
  });
128
209
  }
129
210
  for (let [e, r] of t.linearTerms) {
130
- let t = (n.linearTerms.get(e) || 0) + r;
131
- Math.abs(t) < 1e-12 ? n.linearTerms.delete(e) : n.linearTerms.set(e, t);
211
+ let t = (n.linearTerms.get(e) ?? 0) + r;
212
+ t === 0 ? n.linearTerms.delete(e) : n.linearTerms.set(e, t);
132
213
  }
133
214
  return n.constant += t.constant, n;
134
215
  }
135
216
  if ("terms" in t && t.terms instanceof Map) {
136
217
  for (let [e, r] of t.terms) {
137
- let t = (n.linearTerms.get(e) || 0) + r;
138
- Math.abs(t) < 1e-12 ? n.linearTerms.delete(e) : n.linearTerms.set(e, t);
218
+ let t = (n.linearTerms.get(e) ?? 0) + r;
219
+ t === 0 ? n.linearTerms.delete(e) : n.linearTerms.set(e, t);
139
220
  }
140
221
  return n.constant += t.constant, n;
141
222
  }
142
- let r = t, i = (n.linearTerms.get(r) || 0) + 1;
143
- return Math.abs(i) < 1e-12 ? n.linearTerms.delete(r) : n.linearTerms.set(r, i), n;
223
+ let r = t, i = (n.linearTerms.get(r) ?? 0) + 1;
224
+ return i === 0 ? n.linearTerms.delete(r) : n.linearTerms.set(r, i), n;
144
225
  }
145
- sub(t) {
146
- return typeof t == "number" ? this.add(-t) : t instanceof e ? this.add(t.neg()) : ("terms" in t && t.terms instanceof Map, this.add(t.mul(-1)));
226
+ sub(e) {
227
+ if (y(e)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
228
+ return typeof e == "number" ? this.add(-e) : this.add(e.mul(-1));
147
229
  }
148
230
  mul(t) {
149
- let n = new e(/* @__PURE__ */ new Map(), /* @__PURE__ */ new Map(), this.constant * t);
150
- for (let [e, r] of this.linearTerms) {
151
- let i = r * t;
152
- Math.abs(i) >= 1e-12 && n.linearTerms.set(e, i);
153
- }
154
- for (let [e, r] of this.quadTerms) {
155
- let i = r.coeff * t;
156
- Math.abs(i) >= 1e-12 && n.quadTerms.set(e, {
157
- var1: r.var1,
158
- var2: r.var2,
159
- coeff: i
160
- });
231
+ if (y(t)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
232
+ if (typeof t == "number") {
233
+ let n = this.linearTerms.size > 0 || this.quadTerms.size > 0, r = this.constant === 0 && n ? 0 : this.constant * t;
234
+ if (this.constant !== 0 && t !== 0 && r === 0) throw Error("Constant multiplication underflows to zero.");
235
+ let i = new e(/* @__PURE__ */ new Map(), /* @__PURE__ */ new Map(), r);
236
+ for (let [e, n] of this.linearTerms) {
237
+ let r = n * t;
238
+ if (n !== 0 && t !== 0 && r === 0) throw Error("Coefficient multiplication underflows to zero.");
239
+ r !== 0 && i.linearTerms.set(e, r);
240
+ }
241
+ for (let [e, n] of this.quadTerms) {
242
+ let r = n.coeff * t;
243
+ if (n.coeff !== 0 && t !== 0 && r === 0) throw Error("Coefficient multiplication underflows to zero.");
244
+ r !== 0 && i.quadTerms.set(e, {
245
+ var1: n.var1,
246
+ var2: n.var2,
247
+ coeff: r
248
+ });
249
+ }
250
+ return i;
161
251
  }
162
- return n;
252
+ let n = "id" in t && "name" in t && !("terms" in t) && !("quadTerms" in t), r = "quadTerms" in t ? t.quadTerms.size : 0, i = n ? /* @__PURE__ */ new Map([[t, 1]]) : "quadTerms" in t ? t.linearTerms : t.terms, a = n ? 0 : t.constant;
253
+ if (this.quadTerms.size > 0 && (r > 0 || i.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)");
254
+ if (r === 0 && i.size === 0) return this.mul(a);
255
+ if (this.quadTerms.size === 0 && this.linearTerms.size === 0 && !n) {
256
+ let n = t.mul(this.constant);
257
+ return "quadTerms" in n ? n : new e(n.terms, /* @__PURE__ */ new Map(), n.constant);
258
+ }
259
+ return e.multiplyLinearTerms(this.constant, this.linearTerms, a, i);
163
260
  }
164
- div(e) {
165
- if (Math.abs(e) < 1e-12) throw Error("Division by zero in QuadExpr.div()");
166
- return this.mul(1 / e);
261
+ div(t) {
262
+ let n = e.extractScalarDivisor(t, "QuadExpr.div()");
263
+ return this.mul(1 / n);
167
264
  }
168
265
  neg() {
169
266
  return this.mul(-1);
@@ -180,18 +277,96 @@ var x = class e {
180
277
  }
181
278
  return e;
182
279
  }
183
- getValue() {
184
- return this.value;
280
+ le(e, t) {
281
+ return this.toConstraint("<=", e, t);
282
+ }
283
+ ge(e, t) {
284
+ return this.toConstraint(">=", e, t);
285
+ }
286
+ eq(e, t) {
287
+ return this.toConstraint("==", e, t);
288
+ }
289
+ toConstraint(e, t, n) {
290
+ if (this.quadTerms.size > 0) throw Error(`Quadratic expressions are not supported in linear constraints (${e}): quadratic terms are only allowed in objectives.`);
291
+ let r = new S(this.linearTerms, this.constant);
292
+ return e === "<=" ? r.le(t, n) : e === ">=" ? r.ge(t, n) : r.eq(t, n);
293
+ }
294
+ and(e) {
295
+ throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'and': expected a BoolVar, Constraint, or BoolOp.");
296
+ }
297
+ or(e) {
298
+ throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'or': expected a BoolVar, Constraint, or BoolOp.");
299
+ }
300
+ implies(e) {
301
+ throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'implies': expected a BoolVar, Constraint, or BoolOp.");
302
+ }
303
+ xor(e) {
304
+ throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'xor': expected a BoolVar, Constraint, or BoolOp.");
305
+ }
306
+ not() {
307
+ throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'not': expected a BoolVar, Constraint, or BoolOp.");
308
+ }
309
+ toString() {
310
+ let e = [], t = !0;
311
+ for (let n of this.quadTerms.values()) {
312
+ let r = n.coeff, i = r >= 0 ? t ? "" : "+ " : "- ", a = Math.abs(r), o = a === 1 ? "" : `${a} `;
313
+ n.var1 === n.var2 ? e.push(`${i}${o}${n.var1.name}^2`) : e.push(`${i}${o}${n.var1.name} * ${n.var2.name}`), t = !1;
314
+ }
315
+ for (let [n, r] of this.linearTerms) {
316
+ let i = r >= 0 ? t ? "" : "+ " : "- ", a = Math.abs(r), o = a === 1 ? "" : `${a} `;
317
+ e.push(`${i}${o}${n.name}`), t = !1;
318
+ }
319
+ if (this.constant !== 0 || e.length === 0) {
320
+ let n = this.constant >= 0 ? t ? "" : "+ " : "- ";
321
+ e.push(`${n}${Math.abs(this.constant)}`);
322
+ }
323
+ return e.join(" ") || "0";
185
324
  }
186
325
  static fromProduct(t, n, r = 1) {
187
- let i = new e(), a = b(t, n);
326
+ let i = new e();
327
+ if (r === 0) return i;
328
+ let a = b(t, n);
188
329
  return i.quadTerms.set(a, {
189
330
  var1: t.id <= n.id ? t : n,
190
331
  var2: t.id <= n.id ? n : t,
191
332
  coeff: r
192
333
  }), i;
193
334
  }
335
+ static multiplyLinearTerms(t, n, r, i) {
336
+ let a = n.size > 0 || i.size > 0, o = (t === 0 || r === 0) && a ? 0 : t * r;
337
+ if (t !== 0 && r !== 0 && o === 0) throw Error("Constant multiplication underflows to zero.");
338
+ let s = new e(/* @__PURE__ */ new Map(), /* @__PURE__ */ new Map(), o === 0 ? 0 : o);
339
+ if (r !== 0) for (let [e, t] of n) {
340
+ let n = t * r;
341
+ if (t !== 0 && n === 0) throw Error("Coefficient multiplication underflows to zero.");
342
+ n !== 0 && s.linearTerms.set(e, n);
343
+ }
344
+ if (t !== 0) for (let [e, n] of i) {
345
+ let r = s.linearTerms.get(e) ?? 0, i = n * t;
346
+ if (n !== 0 && i === 0) throw Error("Coefficient multiplication underflows to zero.");
347
+ let a = r + i;
348
+ a === 0 ? s.linearTerms.delete(e) : s.linearTerms.set(e, a);
349
+ }
350
+ for (let [t, r] of n) for (let [n, a] of i) {
351
+ let i = r * a;
352
+ if (r !== 0 && a !== 0 && i === 0) throw Error("Coefficient multiplication underflows to zero.");
353
+ s = s.add(e.fromProduct(t, n, i));
354
+ }
355
+ return s;
356
+ }
357
+ static extractScalarDivisor(e, t) {
358
+ if (y(e)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
359
+ if (typeof e == "number") {
360
+ if (e === 0) throw Error(`Division by zero in ${t}`);
361
+ return e;
362
+ }
363
+ let n = "quadTerms" in e ? e.linearTerms.size : e.terms.size, r = "quadTerms" in e ? e.quadTerms.size : 0;
364
+ if (n > 0 || r > 0) throw Error("Cannot divide by variable in expression");
365
+ if (e.constant === 0) throw Error(`Division by zero in ${t}`);
366
+ return e.constant;
367
+ }
194
368
  }, S = class e {
369
+ kind = "linear";
195
370
  terms;
196
371
  constant;
197
372
  constructor(e = /* @__PURE__ */ new Map(), t = 0) {
@@ -201,41 +376,44 @@ var x = class e {
201
376
  return new e(this.terms, this.constant);
202
377
  }
203
378
  add(e) {
379
+ if (y(e)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
204
380
  if (e instanceof x) return e.add(this);
205
381
  let t = this.clone();
206
382
  if (typeof e == "number") t.constant += e;
207
383
  else if ("terms" in e) {
208
384
  for (let [n, r] of e.terms) {
209
- let e = (t.terms.get(n) || 0) + r;
210
- Math.abs(e) < 1e-12 ? t.terms.delete(n) : t.terms.set(n, e);
385
+ let e = (t.terms.get(n) ?? 0) + r;
386
+ e === 0 ? t.terms.delete(n) : t.terms.set(n, e);
211
387
  }
212
388
  t.constant += e.constant;
213
389
  } else {
214
- let n = (t.terms.get(e) || 0) + 1;
215
- Math.abs(n) < 1e-12 ? t.terms.delete(e) : t.terms.set(e, n);
390
+ let n = (t.terms.get(e) ?? 0) + 1;
391
+ n === 0 ? t.terms.delete(e) : t.terms.set(e, n);
216
392
  }
217
393
  return t;
218
394
  }
219
395
  sub(e) {
220
- return e instanceof x ? e.neg().add(this) : typeof e == "number" ? this.add(-e) : ("terms" in e, this.add(e.mul(-1)));
396
+ if (y(e)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
397
+ return e instanceof x ? e.neg().add(this) : typeof e == "number" ? this.add(-e) : this.add(e.mul(-1));
221
398
  }
222
399
  mul(t) {
400
+ if (y(t)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
223
401
  if (typeof t == "number") {
224
- let n = new e(/* @__PURE__ */ new Map(), this.constant * t);
225
- for (let [e, r] of this.terms) {
226
- let i = r * t;
227
- Math.abs(i) >= 1e-12 && n.terms.set(e, i);
402
+ let n = this.constant === 0 && this.terms.size > 0 ? 0 : this.constant * t;
403
+ if (this.constant !== 0 && t !== 0 && n === 0) throw Error("Constant multiplication underflows to zero.");
404
+ let r = new e(/* @__PURE__ */ new Map(), n);
405
+ for (let [e, n] of this.terms) {
406
+ let i = n * t;
407
+ if (n !== 0 && t !== 0 && i === 0) throw Error("Coefficient multiplication underflows to zero.");
408
+ i !== 0 && r.terms.set(e, i);
228
409
  }
229
- return n;
410
+ return r;
230
411
  }
231
- let n = new x();
232
- Math.abs(this.constant) >= 1e-12 && n.linearTerms.set(t, this.constant);
233
- for (let [e, r] of this.terms) n = n.add(x.fromProduct(e, t, r));
234
- return n;
412
+ return t instanceof N || typeof t == "object" && t && "id" in t && !("terms" in t) && !("quadTerms" in t) ? x.multiplyLinearTerms(this.constant, this.terms, 0, /* @__PURE__ */ new Map([[t, 1]])) : t instanceof x ? this.terms.size === 0 ? t.mul(this.constant) : new x(this.terms, /* @__PURE__ */ new Map(), this.constant).mul(t) : this.terms.size === 0 ? t.mul(this.constant) : t.terms.size === 0 ? this.mul(t.constant) : x.multiplyLinearTerms(this.constant, this.terms, t.constant, t.terms);
235
413
  }
236
414
  div(e) {
237
- if (Math.abs(e) < 1e-12) throw Error("Division by zero in LinExpr.div()");
238
- return this.mul(1 / e);
415
+ let t = x.extractScalarDivisor(e, "LinExpr.div()");
416
+ return this.mul(1 / t);
239
417
  }
240
418
  neg() {
241
419
  return this.mul(-1);
@@ -248,32 +426,41 @@ var x = class e {
248
426
  }
249
427
  return e;
250
428
  }
251
- getValue() {
252
- return this.value;
253
- }
254
429
  le(e, t) {
255
430
  return this.toConstraint("<=", e, t);
256
431
  }
257
- lessThanOrEqual(e, t) {
258
- return this.le(e, t);
259
- }
260
432
  ge(e, t) {
261
433
  return this.toConstraint(">=", e, t);
262
434
  }
263
- greaterThanOrEqual(e, t) {
264
- return this.ge(e, t);
265
- }
266
435
  eq(e, t) {
267
436
  return this.toConstraint("==", e, t);
268
437
  }
269
- equalTo(e, t) {
270
- return this.eq(e, t);
271
- }
272
438
  toConstraint(t, n, r) {
273
439
  let i = this.clone(), a = 0;
274
- typeof n == "number" ? a = n : "terms" in n ? (i = i.sub(new e(n.terms, 0)), a = n.constant) : i = i.sub(n);
440
+ if (typeof n == "number") a = n;
441
+ else if (n instanceof v || 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.`);
442
+ else if (n instanceof x || typeof n == "object" && n && "quadTerms" in n) {
443
+ let r = n;
444
+ if (r.quadTerms.size > 0) throw Error(`Quadratic expressions are not supported in linear constraints (${t}): quadratic terms are only allowed in objectives.`);
445
+ i = i.sub(new e(r.linearTerms, 0)), a = r.constant;
446
+ } else "terms" in n ? (i = i.sub(new e(n.terms, 0)), a = n.constant) : i = i.sub(n);
275
447
  let o = a - i.constant;
276
- return i.constant = 0, new y(i, t, o, r);
448
+ return i.constant = 0, new v(i, t, o, r);
449
+ }
450
+ and(e) {
451
+ throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'and': expected a BoolVar, Constraint, or BoolOp.");
452
+ }
453
+ or(e) {
454
+ throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'or': expected a BoolVar, Constraint, or BoolOp.");
455
+ }
456
+ implies(e) {
457
+ throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'implies': expected a BoolVar, Constraint, or BoolOp.");
458
+ }
459
+ xor(e) {
460
+ throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'xor': expected a BoolVar, Constraint, or BoolOp.");
461
+ }
462
+ not() {
463
+ throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'not': expected a BoolVar, Constraint, or BoolOp.");
277
464
  }
278
465
  toString() {
279
466
  let e = [], t = !0;
@@ -293,17 +480,26 @@ function C(e, t) {
293
480
  function i(e) {
294
481
  if (e != null) {
295
482
  if (typeof e == "number") r += e;
296
- else if (typeof e == "object" && "terms" in e) {
483
+ else if (e instanceof v || typeof e == "object" && "sense" in e && "lhs" in e && "rhs" in e) throw Error("Cannot sum a constraint comparison (<=, >=, ==) in sum(): expected a number, Var, or LinExpr.");
484
+ else if (e instanceof x || typeof e == "object" && "quadTerms" in e && e.quadTerms instanceof Map) {
485
+ let t = e;
486
+ if (t.quadTerms.size > 0) throw Error("Quadratic expressions are not supported in sum(): sum() produces a linear expression (LinExpr).");
487
+ for (let [e, r] of t.linearTerms) {
488
+ let t = n.get(e) ?? 0;
489
+ n.set(e, t + r);
490
+ }
491
+ r += t.constant;
492
+ } else if (typeof e == "object" && "terms" in e) {
297
493
  let t = e;
298
494
  for (let [e, r] of t.terms) {
299
- let t = n.get(e) || 0;
495
+ let t = n.get(e) ?? 0;
300
496
  n.set(e, t + r);
301
497
  }
302
498
  r += t.constant;
303
- } else {
304
- let t = e, r = n.get(t) || 0;
499
+ } else if (typeof e == "object" && "id" in e && "name" in e && "lb" in e && "ub" in e) {
500
+ let t = e, r = n.get(t) ?? 0;
305
501
  n.set(t, r + 1);
306
- }
502
+ } else throw Error("Unsupported item in sum(): expected a number, Var, or LinExpr.");
307
503
  }
308
504
  }
309
505
  if (typeof e == "number") {
@@ -313,41 +509,55 @@ function C(e, t) {
313
509
  let n = 0;
314
510
  for (let r of e) i(t ? t(r, n++) : r);
315
511
  }
316
- for (let [e, t] of n) Math.abs(t) < 1e-12 && n.delete(e);
512
+ for (let [e, t] of n) t === 0 && n.delete(e);
317
513
  return new S(n, r);
318
514
  }
319
515
  function w(e, t) {
320
516
  let n = /* @__PURE__ */ new Map(), r = 0;
321
517
  function i(e) {
322
- return typeof e == "object" && !!e && typeof e.id == "number" && typeof e.name == "string" && typeof e.mul == "function" && typeof e.le == "function";
518
+ return typeof e == "object" && !!e && !(e instanceof v) && 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";
323
519
  }
324
520
  function a(e) {
325
521
  return i(e) || e instanceof S || e instanceof x;
326
522
  }
327
- function o(e, t) {
328
- if (typeof t == "number") r += e * t;
329
- else if (t instanceof S) {
523
+ function o(e, t, a) {
524
+ if (!Number.isFinite(e)) throw Error(`Invalid non-finite coefficient "${e}" in dot() at '${a || "root"}'.`);
525
+ if (t instanceof x) throw Error(`Quadratic expressions are not supported in dot() at '${a || "root"}': dot() produces a linear expression (LinExpr).`);
526
+ if (t instanceof S) {
527
+ if (!Number.isFinite(t.constant)) throw Error(`Invalid non-finite constant "${t.constant}" in dot() at '${a || "root"}'.`);
528
+ let i = e === 0 || t.constant === 0 ? 0 : e * t.constant;
529
+ if (e !== 0 && t.constant !== 0 && i === 0) throw Error("Constant multiplication underflows to zero.");
530
+ if (!Number.isFinite(i)) throw Error(`Coefficient multiplication overflows finite floating-point range in dot() at '${a || "root"}'.`);
531
+ let o = r + i;
532
+ if (!Number.isFinite(o)) throw Error(`Constant accumulation overflows finite floating-point range in dot() at '${a || "root"}'.`);
533
+ r = o;
330
534
  for (let [r, i] of t.terms) {
331
- let t = n.get(r) || 0;
332
- n.set(r, t + e * i);
535
+ if (!Number.isFinite(i)) throw Error(`Invalid non-finite coefficient "${i}" in dot() at '${a || "root"}'.`);
536
+ let t = e * i;
537
+ if (e !== 0 && i !== 0 && t === 0) throw Error("Coefficient multiplication underflows to zero.");
538
+ if (!Number.isFinite(t)) throw Error(`Coefficient multiplication overflows finite floating-point range in dot() at '${a || "root"}'.`);
539
+ let o = (n.get(r) ?? 0) + t;
540
+ if (!Number.isFinite(o)) throw Error(`Coefficient accumulation overflows finite floating-point range in dot() at '${a || "root"}'.`);
541
+ n.set(r, o);
333
542
  }
334
- r += e * t.constant;
335
543
  } else if (i(t)) {
336
- let r = t, i = n.get(r) || 0;
337
- n.set(r, i + e);
338
- } else throw Error("Cannot multiply number by non-variable/expression operand in dot().");
544
+ let r = t, i = (n.get(r) ?? 0) + e;
545
+ if (!Number.isFinite(i)) throw Error(`Coefficient accumulation overflows finite floating-point range in dot() at '${a || "root"}'.`);
546
+ n.set(r, i);
547
+ } else throw Error(`Cannot multiply number by non-variable/expression operand in dot() at '${a || "root"}'.`);
339
548
  }
340
549
  function s(e) {
341
- return typeof e == "object" && !!e && !Array.isArray(e) && !a(e);
550
+ return typeof e == "object" && !!e && !Array.isArray(e) && !a(e) && !(e instanceof v) && e.kind !== "constraint";
342
551
  }
343
552
  function c(e, t, n = "") {
344
553
  if (e === void 0 || t === void 0 || e === null || t === null) throw Error(`Nullish or undefined operand encountered in dot() at '${n || "root"}'.`);
554
+ 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.`);
345
555
  if (typeof e == "number") {
346
- o(e, t);
556
+ o(e, t, n);
347
557
  return;
348
558
  }
349
559
  if (typeof t == "number") {
350
- o(t, e);
560
+ o(t, e, n);
351
561
  return;
352
562
  }
353
563
  if (Array.isArray(e) || Array.isArray(t)) {
@@ -358,21 +568,22 @@ function w(e, t) {
358
568
  }
359
569
  let r = Array.isArray(e) ? e : t, i = Array.isArray(e) ? t : e;
360
570
  if (s(i)) {
571
+ if (Object.getOwnPropertySymbols(i).length > 0) throw Error(`Symbol keys are not supported in dot() at '${n || "root"}'.`);
361
572
  let a = Object.keys(i);
362
- if (a.length > 0 && a.every((e) => /^\d+$/.test(e))) {
363
- if (a.length !== r.length) throw Error(`Array length mismatch in dot() at '${n || "root"}': array has length ${r.length}, but object has ${a.length} indices.`);
364
- let o = [];
365
- for (let e = 0; e < r.length; e++) !(e in i) && !(String(e) in i) && o.push(e);
366
- if (o.length > 0) throw Error(`Key mismatch in dot() at '${n || "root"}': missing indices [${o.join(", ")}] in numeric record.`);
573
+ if (a.length === r.length && a.every((e) => /^(?:0|[1-9]\d*)$/.test(e))) {
574
+ let a = [];
575
+ for (let e = 0; e < r.length; e++) Object.prototype.hasOwnProperty.call(i, e) || a.push(e);
576
+ if (a.length > 0) throw Error(`Key mismatch in dot() at '${n || "root"}': missing indices [${a.join(", ")}] in numeric record.`);
367
577
  for (let i = 0; i < r.length; i++) c(e[i], t[i], n ? `${n}[${i}]` : `[${i}]`);
368
578
  return;
369
579
  }
370
- throw Error(`Type mismatch in dot() at '${n || "root"}': one operand is an Array while the other is an Object with keys [${a.slice(0, 5).join(", ")}].`);
580
+ 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(", ")}].`);
371
581
  }
372
582
  throw Error(`Type mismatch in dot() at '${n || "root"}': one operand is an Array while the other is not.`);
373
583
  }
374
584
  if (s(e) && s(t)) {
375
- let r = Object.keys(e), i = Object.keys(t), a = r.filter((e) => !(e in t)), o = i.filter((t) => !(t in e));
585
+ if (Object.getOwnPropertySymbols(e).length > 0 || Object.getOwnPropertySymbols(t).length > 0) throw Error(`Symbol keys are not supported in dot() at '${n || "root"}'.`);
586
+ 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));
376
587
  if (a.length > 0 || o.length > 0) {
377
588
  let e = [];
378
589
  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("; ")}.`);
@@ -383,38 +594,207 @@ function w(e, t) {
383
594
  throw Error(`Unsupported operand types in dot() at '${n || "root"}'.`);
384
595
  }
385
596
  c(e, t);
386
- for (let [e, t] of n) Math.abs(t) < 1e-12 && n.delete(e);
597
+ for (let [e, t] of n) t === 0 && n.delete(e);
387
598
  return new S(n, r);
388
599
  }
389
600
  //#endregion
601
+ //#region src/modeler/validation.ts
602
+ var T = /* @__PURE__ */ new WeakMap(), E = /* @__PURE__ */ new WeakMap(), D = /* @__PURE__ */ new WeakMap();
603
+ function O(e, t, n) {
604
+ if (e instanceof d) for (let r of e.children) O(r, t, n);
605
+ else if (e instanceof v) {
606
+ let r = e.ownerModel, i = E.get(e);
607
+ (r !== t || i !== t) && (n && n.push(() => {
608
+ e.ownerModel = r, i ? E.set(e, i) : E.delete(e);
609
+ }), e.ownerModel = t, E.set(e, t));
610
+ }
611
+ }
612
+ function k(e, t) {
613
+ let n = T.get(e);
614
+ if (n && n !== t) throw Error(`Variable "${e.name}" already belongs to model "${n.name}" and cannot be added to model "${t.name}".`);
615
+ T.set(e, t);
616
+ }
617
+ function ee(e, t) {
618
+ let n = D.get(e);
619
+ 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}".`);
620
+ let r = E.get(e);
621
+ if (r && r !== t) throw Error(`Constraint "${e.name}" already belongs to model "${r.name}" and cannot be added to model "${t.name}".`);
622
+ return D.set(e, t), E.set(e, t), r;
623
+ }
624
+ function te(e, t) {
625
+ T.get(e) === t && T.delete(e);
626
+ }
627
+ function ne(e, t, n) {
628
+ D.get(e) === t && D.delete(e), E.get(e) === t && (n ? E.set(e, n) : E.delete(e));
629
+ }
630
+ var re = 1e-6;
631
+ function A(e) {
632
+ 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).`);
633
+ if (e.lb === Infinity) throw Error(`Variable "${e.name}" has invalid lower bound: +Infinity is not allowed as a lower bound.`);
634
+ 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).`);
635
+ if (e.ub === -Infinity) throw Error(`Variable "${e.name}" has invalid upper bound: -Infinity is not allowed as an upper bound.`);
636
+ 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}).`);
637
+ if (e.initialValue !== void 0) {
638
+ 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).`);
639
+ let t = e.type === "Binary" ? Math.max(0, e.lb) : e.lb, n = e.type === "Binary" ? Math.min(1, e.ub) : e.ub;
640
+ 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 ${re}).`);
641
+ if (e.type === "Integer" || e.type === "Binary") {
642
+ let r = Math.round(e.initialValue);
643
+ 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 ${re} of an integer in [${t}, ${n}]).`);
644
+ }
645
+ }
646
+ }
647
+ function ie(e) {
648
+ if (e.initialValue === void 0) return;
649
+ if (A(e), e.type === "Integer" || e.type === "Binary") {
650
+ let t = Math.round(e.initialValue);
651
+ return Object.is(t, -0) ? 0 : t;
652
+ }
653
+ let t = Math.min(e.ub, Math.max(e.lb, e.initialValue));
654
+ return Object.is(t, -0) ? 0 : t;
655
+ }
656
+ function ae(e, t, n) {
657
+ if (e instanceof x) {
658
+ if (!Number.isFinite(e.constant)) throw Error(`Objective function has invalid constant: ${e.constant}. Constant must be a finite number.`);
659
+ for (let [r, i] of e.linearTerms) {
660
+ 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.`);
661
+ if (!Number.isFinite(i)) throw Error(`Variable "${r.name}" in objective function has invalid coefficient: ${i}. Coefficients must be finite numbers.`);
662
+ }
663
+ for (let [, r] of e.quadTerms) {
664
+ 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.`);
665
+ 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.`);
666
+ 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}.`);
667
+ }
668
+ } else if ("terms" in e) {
669
+ if (!Number.isFinite(e.constant)) throw Error(`Objective function has invalid constant: ${e.constant}. Constant must be a finite number.`);
670
+ for (let [r, i] of e.terms) {
671
+ 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.`);
672
+ if (!Number.isFinite(i)) throw Error(`Variable "${r.name}" in objective function has invalid coefficient: ${i}. Coefficients must be finite numbers.`);
673
+ }
674
+ }
675
+ }
676
+ function j(e, t, n, r = e.name) {
677
+ if (!Number.isFinite(e.rhs)) throw Error(`Constraint "${r}" has invalid right-hand side (RHS): ${e.rhs}. RHS must be a finite number.`);
678
+ if (e.lhs.constant !== void 0 && !Number.isFinite(e.lhs.constant)) throw Error(`Constraint "${r}" has invalid LHS constant: ${e.lhs.constant}. Constant must be a finite number.`);
679
+ for (let [i, a] of e.lhs.terms) {
680
+ if (!n(i)) throw Error(`Variable "${i.name}" in constraint "${r}" does not belong to model "${t}". All variables must belong to the model by object identity.`);
681
+ if (!Number.isFinite(a)) throw Error(`Variable "${i.name}" in constraint "${r}" has invalid coefficient: ${a}. Coefficients must be finite numbers.`);
682
+ }
683
+ }
684
+ function M(e, t, n, r, i, a) {
685
+ if (e instanceof u) {
686
+ if (!i(e)) throw Error(`Boolean variable "${e.name}" in logical constraint "${t}" does not belong to model "${n}".`);
687
+ } else if (e instanceof d) for (let o of e.children) M(o, t, n, r, i, a);
688
+ else if (e instanceof v) {
689
+ if (a) {
690
+ let r = E.get(e) ?? e.ownerModel ?? void 0;
691
+ if (r && r !== a) 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}".`);
692
+ }
693
+ if (!Number.isFinite(e.rhs)) throw Error(`Constraint "${e.name}" embedded in logical constraint "${t}" has invalid RHS: ${e.rhs}.`);
694
+ if (e.lhs.constant !== void 0 && !Number.isFinite(e.lhs.constant)) throw Error(`Constraint "${e.name}" embedded in logical constraint "${t}" has invalid LHS constant: ${e.lhs.constant}.`);
695
+ for (let [i, a] of e.lhs.terms) {
696
+ if (!r(i)) throw Error(`Variable "${i.name}" embedded in logical constraint "${t}" does not belong to model "${n}".`);
697
+ if (!Number.isFinite(a)) throw Error(`Variable "${i.name}" embedded in logical constraint "${t}" has invalid coefficient: ${a}.`);
698
+ }
699
+ }
700
+ }
701
+ function oe(e) {
702
+ let t = /* @__PURE__ */ new Set(), n = /* @__PURE__ */ new Set();
703
+ for (let r of e.variables) {
704
+ A(r);
705
+ let i = T.get(r);
706
+ if (i && i !== e || t.has(r.name) || e.getVarByName(r.name) !== r) throw Error(`Variable "${r.name}" is duplicated or not canonically registered in model "${e.name}".`);
707
+ t.add(r.name), n.add(r);
708
+ }
709
+ let r = /* @__PURE__ */ new Set(), i = /* @__PURE__ */ new Set();
710
+ for (let t of e.boolVars) {
711
+ if (r.has(t.name) || e.getBoolVarByName(t.name) !== t) throw Error(`Boolean variable "${t.name}" is duplicated or not canonically registered in model "${e.name}".`);
712
+ r.add(t.name), i.add(t);
713
+ }
714
+ let a = /* @__PURE__ */ new Set();
715
+ for (let t of e.constraints) {
716
+ if (r.has(t.name)) throw Error(`Constraint name "${t.name}" conflicts with Boolean variable "${t.name}" in model "${e.name}".`);
717
+ if (a.has(t.name)) throw Error(`Duplicate constraint name "${t.name}" in model "${e.name}".`);
718
+ a.add(t.name);
719
+ }
720
+ for (let t of e.logicalConstraints) {
721
+ if (r.has(t.name)) throw Error(`Logical constraint name "${t.name}" conflicts with Boolean variable "${t.name}" in model "${e.name}".`);
722
+ if (a.has(t.name)) throw Error(`Conflicting constraint name "${t.name}" across arithmetic and logical constraints in model "${e.name}".`);
723
+ a.add(t.name);
724
+ }
725
+ for (let t of e.constraints) {
726
+ let n = E.get(t);
727
+ if (n && n !== e || e.getConstraintByName(t.name) !== t) throw Error(`Constraint "${t.name}" is duplicated or not canonically registered in model "${e.name}".`);
728
+ }
729
+ 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}".`);
730
+ e.objectiveExpr && ae(e.objectiveExpr, e.name, (e) => n.has(e));
731
+ for (let t of e.constraints) j(t, e.name, (e) => n.has(e));
732
+ for (let t of e.logicalConstraints) M(t.expr, t.name, e.name, (e) => n.has(e), (e) => i.has(e), e);
733
+ }
734
+ //#endregion
390
735
  //#region src/modeler/variable.ts
391
- var te = 0;
392
- function T(e) {
393
- if (!e) return "Continuous";
394
- let t = e.toLowerCase();
395
- return t === "binary" || t === "bin" ? "Binary" : t === "integer" || t === "int" ? "Integer" : "Continuous";
736
+ var se = 0;
737
+ function ce(e) {
738
+ if (e === void 0) return "Continuous";
739
+ if (typeof e == "string") {
740
+ let t = e.toLowerCase();
741
+ if (t === "binary" || t === "bin") return "Binary";
742
+ if (t === "integer" || t === "int") return "Integer";
743
+ if (t === "continuous" || t === "cont") return "Continuous";
744
+ }
745
+ throw Error(`Invalid variable type "${String(e)}": expected "Continuous", "Integer", or "Binary".`);
396
746
  }
397
- var E = class e {
747
+ var N = class {
398
748
  id;
399
- name;
400
- type;
401
- lb;
402
- ub;
749
+ _name;
750
+ prefix;
751
+ hasExplicitName;
752
+ ownerModel = null;
753
+ _type;
754
+ _lb;
755
+ _ub;
756
+ _initialValue;
403
757
  value = null;
404
758
  reducedCost = null;
405
759
  constructor(e = {}) {
406
- this.id = ++te, this.name = e.name || `x_${this.id}`, this.type = T(e.type), this.type === "Binary" ? (this.lb = 0, this.ub = 1) : (this.lb = e.lb === void 0 ? 0 : e.lb, this.ub = e.ub === void 0 ? Infinity : e.ub);
760
+ this.id = ++se;
761
+ let t = typeof e.name == "string" ? e.name.trim() : "", n = typeof e.prefix == "string" ? e.prefix.trim() : "";
762
+ this.prefix = n.length > 0 ? n : "x", this.hasExplicitName = t.length > 0, this._name = this.hasExplicitName ? t : `${this.prefix}_${this.id}`, this._type = ce(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 && A(this);
407
763
  }
408
- mul(t) {
409
- if (typeof t == "number") return new S(/* @__PURE__ */ new Map([[this, t]]), 0);
410
- if (t instanceof e) return x.fromProduct(this, t, 1);
411
- let n = new x();
412
- Math.abs(t.constant) >= 1e-12 && n.linearTerms.set(this, t.constant);
413
- for (let [e, r] of t.terms) {
414
- let t = x.fromProduct(this, e, r);
415
- n = n.add(t);
416
- }
417
- return n;
764
+ get name() {
765
+ return this._name;
766
+ }
767
+ get type() {
768
+ return this._type;
769
+ }
770
+ set type(e) {
771
+ let t = ce(e);
772
+ this._type !== t && (this._type = t, this.ownerModel?.notifyMutation?.());
773
+ }
774
+ get lb() {
775
+ return this._lb;
776
+ }
777
+ set lb(e) {
778
+ Object.is(this._lb, e) || (this._lb = e, this.ownerModel?.notifyMutation?.());
779
+ }
780
+ get ub() {
781
+ return this._ub;
782
+ }
783
+ set ub(e) {
784
+ Object.is(this._ub, e) || (this._ub = e, this.ownerModel?.notifyMutation?.());
785
+ }
786
+ get initialValue() {
787
+ return this._initialValue;
788
+ }
789
+ set initialValue(e) {
790
+ let t = e !== void 0 && Object.is(e, -0) ? 0 : e;
791
+ Object.is(this._initialValue, t) || (this._initialValue = t, this.ownerModel?.notifyMutation?.());
792
+ }
793
+ _assignModelName(e) {
794
+ this._name = e;
795
+ }
796
+ mul(e) {
797
+ return typeof e == "number" ? new S(/* @__PURE__ */ new Map([[this, 1]]), 0).mul(e) : new x(/* @__PURE__ */ new Map([[this, 1]]), /* @__PURE__ */ new Map(), 0).mul(e);
418
798
  }
419
799
  add(e) {
420
800
  return e instanceof x ? e.add(this) : new S(/* @__PURE__ */ new Map([[this, 1]]), 0).add(e);
@@ -428,84 +808,100 @@ var E = class e {
428
808
  le(e, t) {
429
809
  return new S(/* @__PURE__ */ new Map([[this, 1]]), 0).le(e, t);
430
810
  }
431
- lessThanOrEqual(e, t) {
432
- return this.le(e, t);
433
- }
434
811
  ge(e, t) {
435
812
  return new S(/* @__PURE__ */ new Map([[this, 1]]), 0).ge(e, t);
436
813
  }
437
- greaterThanOrEqual(e, t) {
438
- return this.ge(e, t);
439
- }
440
814
  eq(e, t) {
441
815
  return new S(/* @__PURE__ */ new Map([[this, 1]]), 0).eq(e, t);
442
816
  }
443
- equalTo(e, t) {
444
- return this.eq(e, t);
445
- }
446
817
  toString() {
447
818
  return this.name;
448
819
  }
449
- };
450
- //#endregion
451
- //#region src/modeler/parser.ts
452
- function D(e) {
453
- let t = [], n = 0, r = e.length;
454
- for (; n < r;) {
455
- let i = e[n];
456
- if (/\s/.test(i)) {
457
- n++;
820
+ }, le = /^(?:[0-9]+(?:\.[0-9]+)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?$/;
821
+ function ue(e, t) {
822
+ let n = [], r = 0, i = e.length;
823
+ for (; r < i;) {
824
+ let a = e[r];
825
+ if (/\s/.test(a)) {
826
+ r++;
458
827
  continue;
459
828
  }
460
- if (i === "+") t.push({
829
+ if (a === "+") n.push({
461
830
  type: "PLUS",
462
831
  value: "+"
463
- }), n++;
464
- else if (i === "-") t.push({
832
+ }), r++;
833
+ else if (a === "-") n.push({
465
834
  type: "MINUS",
466
835
  value: "-"
467
- }), n++;
468
- else if (i === "*") t.push({
836
+ }), r++;
837
+ else if (a === "*") r + 1 < i && e[r + 1] === "*" ? (n.push({
838
+ type: "CARET",
839
+ value: "**"
840
+ }), r += 2) : (n.push({
469
841
  type: "STAR",
470
842
  value: "*"
471
- }), n++;
472
- else if (i === "/") t.push({
843
+ }), r++);
844
+ else if (a === "^") n.push({
845
+ type: "CARET",
846
+ value: "^"
847
+ }), r++;
848
+ else if (a === "/") n.push({
473
849
  type: "SLASH",
474
850
  value: "/"
475
- }), n++;
476
- else if (i === "(") t.push({
851
+ }), r++;
852
+ else if (a === "(") n.push({
477
853
  type: "LPAREN",
478
854
  value: "("
479
- }), n++;
480
- else if (i === ")") t.push({
855
+ }), r++;
856
+ else if (a === ")") n.push({
481
857
  type: "RPAREN",
482
858
  value: ")"
483
- }), n++;
484
- else if (i === "<" || i === ">" || i === "=") {
485
- let a = i;
486
- n + 1 < r && (e[n + 1] === "=" || e[n + 1] === "<" || e[n + 1] === ">") ? (a += e[n + 1], n += 2) : n++, t.push({
859
+ }), r++;
860
+ else if (a === "<" || a === ">" || a === "=") {
861
+ let t = a;
862
+ r + 1 < i && (e[r + 1] === "=" || e[r + 1] === "<" || e[r + 1] === ">") ? (t += e[r + 1], r += 2) : r++, n.push({
487
863
  type: "COMP",
488
- value: a
864
+ value: t
489
865
  });
490
- } else if (/[0-9.]/.test(i)) {
491
- let i = "";
492
- for (; n < r && /[0-9.]/.test(e[n]);) i += e[n], n++;
493
- t.push({
866
+ } else if (/[0-9.]/.test(a)) {
867
+ let t = r, a = "";
868
+ for (; r < i && /[0-9]/.test(e[r]);) a += e[r++];
869
+ if (r < i && e[r] === ".") for (a += e[r++]; r < i && /[0-9]/.test(e[r]);) a += e[r++];
870
+ if (r < i && (e[r] === "e" || e[r] === "E")) {
871
+ let t = r + 1 < i ? e[r + 1] : "";
872
+ if (!/[a-zA-Z_]/.test(t)) {
873
+ for (a += e[r++], r < i && (e[r] === "+" || e[r] === "-") && (a += e[r++]); r < i && /[0-9]/.test(e[r]);) a += e[r++];
874
+ for (; r < i && (e[r] === "." || e[r] === "e" || e[r] === "E" || /[0-9]/.test(e[r]));) a += e[r++];
875
+ }
876
+ } else for (; r < i && (e[r] === "." || /[0-9]/.test(e[r]));) a += e[r++];
877
+ if (!le.test(a)) throw Error(`Invalid numeric literal '${a}' at position ${t} in: "${e}"`);
878
+ let o = Number(a);
879
+ if (!Number.isFinite(o)) throw Error(`Numeric literal '${a}' overflows finite floating-point range in: "${e}"`);
880
+ if (o === 0 && !/^[0.]+(?:[eE][+-]?\d+)?$/.test(a)) throw Error(`Numeric literal '${a}' underflows to zero at position ${t} in: "${e}"`);
881
+ n.push({
494
882
  type: "NUMBER",
495
- value: i
883
+ value: a,
884
+ numericValue: o
496
885
  });
497
- } else if (/[a-zA-Z_]/.test(i)) {
498
- let i = "";
499
- for (; n < r && /[a-zA-Z0-9_]/.test(e[n]);) i += e[n], n++;
500
- t.push({
886
+ } else if (/[a-zA-Z_]/.test(a)) {
887
+ let a = "";
888
+ for (; r < i && /[a-zA-Z0-9_]/.test(e[r]);) a += e[r], r++;
889
+ if (t && t.has(a)) {
890
+ let e = t.get(a);
891
+ n.push({
892
+ type: "NUMBER",
893
+ value: String(e),
894
+ numericValue: e
895
+ });
896
+ } else n.push({
501
897
  type: "IDENT",
502
- value: i
898
+ value: a
503
899
  });
504
- } else throw Error(`Unexpected character '${i}' at position ${n} in: "${e}"`);
900
+ } else throw Error(`Unexpected character '${a}' at position ${r} in: "${e}"`);
505
901
  }
506
- return t;
902
+ return n;
507
903
  }
508
- var O = class {
904
+ var de = class {
509
905
  tokens;
510
906
  pos = 0;
511
907
  resolver;
@@ -530,214 +926,296 @@ var O = class {
530
926
  else if (t.type === "MINUS") this.consume(), e = e.sub(this.parseTerm());
531
927
  else break;
532
928
  }
533
- return e;
929
+ return e instanceof x && e.quadTerms.size === 0 ? new S(e.linearTerms, e.constant) : e;
534
930
  }
535
931
  parseTerm() {
536
932
  let e = this.parseFactor();
537
933
  for (; this.pos < this.tokens.length;) {
538
934
  let t = this.peek();
539
935
  if (!t) break;
540
- if (t.type === "STAR") {
541
- this.consume();
542
- let t = this.parseFactor();
543
- e = this.multiplyExpressions(e, t);
544
- } else if (t.type === "SLASH") {
545
- this.consume();
546
- let t = this.parseFactor();
547
- if (t.terms.size > 0) throw Error("Cannot divide by variable in linear expression");
548
- if (Math.abs(t.constant) < 1e-12) throw Error("Division by zero in expression");
549
- e = e.div(t.constant);
550
- } else break;
936
+ if (t.type === "STAR") this.consume(), e = e.mul(this.parseFactor());
937
+ else if (t.type === "SLASH") this.consume(), e = e.div(this.parseFactor());
938
+ else break;
551
939
  }
552
940
  return e;
553
941
  }
554
- multiplyExpressions(e, t) {
555
- if (e.terms.size > 0 && t.terms.size > 0) throw Error("Multiplication of two variables creates a non-linear term (not allowed in MILP/LP)");
556
- return e.terms.size === 0 ? t.mul(e.constant) : e.mul(t.constant);
557
- }
558
942
  parseFactor() {
559
943
  let e = this.peek();
560
944
  if (!e) throw Error("Unexpected end of expression");
561
945
  if (e.type === "PLUS") return this.consume(), this.parseFactor();
562
946
  if (e.type === "MINUS") return this.consume(), this.parseFactor().neg();
947
+ let t;
563
948
  if (e.type === "NUMBER") {
564
949
  this.consume();
565
- let t = parseFloat(e.value);
566
- if (this.peek()?.type === "IDENT") {
567
- let e = this.consume();
568
- return this.resolveVariable(e.value).mul(t);
950
+ let n = e.numericValue ?? Number(e.value);
951
+ if (this.peek()?.type === "IDENT" || this.peek()?.type === "LPAREN") {
952
+ let e;
953
+ if (this.peek()?.type === "IDENT") {
954
+ let t = this.consume(), n = this.resolve(t.value);
955
+ e = n instanceof N || "id" in n && !("terms" in n) && !("quadTerms" in n) ? new S(/* @__PURE__ */ new Map([[n, 1]]), 0) : n.clone();
956
+ } else this.consume("LPAREN"), e = this.parseExpression(), this.consume("RPAREN");
957
+ return this.applyPower(e).mul(n);
569
958
  }
570
- return new S(/* @__PURE__ */ new Map(), t);
571
- }
572
- if (e.type === "IDENT") {
959
+ t = new S(/* @__PURE__ */ new Map(), n);
960
+ } else if (e.type === "IDENT") {
961
+ this.consume();
962
+ let n = this.resolve(e.value);
963
+ t = n instanceof N || "id" in n && !("terms" in n) && !("quadTerms" in n) ? new S(/* @__PURE__ */ new Map([[n, 1]]), 0) : n.clone();
964
+ } else if (e.type === "LPAREN") this.consume("LPAREN"), t = this.parseExpression(), this.consume("RPAREN");
965
+ else throw Error(`Unexpected token '${e.value}' in expression`);
966
+ return this.applyPower(t);
967
+ }
968
+ applyPower(e) {
969
+ if (this.peek()?.type !== "CARET") return e;
970
+ let t = [];
971
+ for (; this.peek()?.type === "CARET";) {
573
972
  this.consume();
574
- let t = this.resolveVariable(e.value);
575
- return new S(/* @__PURE__ */ new Map([[t, 1]]), 0);
973
+ let e = this.tokens[this.pos]?.type === "LPAREN" && this.tokens[this.pos + 1]?.type === "NUMBER" && this.tokens[this.pos + 2]?.type === "RPAREN";
974
+ e && this.consume("LPAREN");
975
+ let n = this.peek();
976
+ if (!n || n.type !== "NUMBER") throw Error("Exponent must be a non-negative integer number");
977
+ this.consume(), e && this.consume("RPAREN");
978
+ let r = n.numericValue ?? Number(n.value);
979
+ if (!Number.isInteger(r) || r < 0) throw Error("Exponent must be a non-negative integer");
980
+ t.push(r);
576
981
  }
577
- if (e.type === "LPAREN") {
578
- this.consume("LPAREN");
579
- let e = this.parseExpression();
580
- return this.consume("RPAREN"), e;
982
+ let n = t[t.length - 1];
983
+ for (let e = t.length - 2; e >= 0; e--) n = t[e] ** +n;
984
+ if (!Number.isFinite(n) || n < 0) throw Error("Exponent creates an invalid or out-of-range power");
985
+ let r = e instanceof x, i = r ? e.quadTerms.size : 0, a = r ? e.linearTerms.size : e.terms.size;
986
+ if (i === 0 && a === 0) {
987
+ let t = e.constant ** +n;
988
+ if (e.constant !== 0 && n > 0 && t === 0) throw Error("Constant exponentiation underflows to zero.");
989
+ return new S(/* @__PURE__ */ new Map(), t);
581
990
  }
582
- throw Error(`Unexpected token '${e.value}' in expression`);
991
+ if (n === 0) return new S(/* @__PURE__ */ new Map(), 1);
992
+ if (n === 1) return e;
993
+ if (n === 2) return e.mul(e);
994
+ throw Error(`Exponent ${n} creates a non-linear term of degree > 2 (not allowed in MILP/LP/QP)`);
995
+ }
996
+ getRemainingToken() {
997
+ return this.peek();
583
998
  }
584
- resolveVariable(e) {
999
+ resolve(e) {
585
1000
  let t = this.resolver(e);
586
1001
  if (!t) throw Error(`Unknown variable '${e}' in expression`);
587
1002
  return t;
588
1003
  }
589
1004
  };
590
- function k(e, t) {
591
- return new O(D(e), t).parseExpression();
592
- }
593
- function A(e, t, n) {
594
- let r = e.match(/(<=|>=|==|=<|=>|=)/);
595
- if (!r) throw Error(`Constraint must contain a comparison operator (<=, >=, ==, =): "${e}"`);
596
- let i = r[0], a = r.index, o = e.substring(0, a).trim(), s = e.substring(a + i.length).trim(), c;
597
- if (i === "<=" || i === "=<") c = "<=";
598
- else if (i === ">=" || i === "=>") c = ">=";
599
- else if (i === "==" || i === "=") c = "==";
600
- else throw Error(`Unsupported operator "${i}"`);
601
- let l = k(o, t), u = k(s, t);
602
- return c === "<=" ? l.le(u, n) : c === ">=" ? l.ge(u, n) : l.eq(u, n);
603
- }
604
- function j(e, t) {
605
- let n = /* @__PURE__ */ new Map(), r = "";
606
- for (let i = 0; i < e.length; i++) if (r += e[i], i < t.length) {
1005
+ function P(e, t, n) {
1006
+ let r = new de(ue(e, n), t), i = r.parseExpression(), a = r.getRemainingToken();
1007
+ if (a) throw Error(`Unexpected token '${a.value}' after expression in "${e}"`);
1008
+ return F(i), i;
1009
+ }
1010
+ function fe(e, t) {
1011
+ return P(e, t);
1012
+ }
1013
+ function pe(e, t, n, r) {
1014
+ let i = e.match(/(<=|>=|==|=<|=>|=)/);
1015
+ if (!i) throw Error(`Constraint must contain a comparison operator (<=, >=, ==, =): "${e}"`);
1016
+ let a = i[0], o = i.index, s = e.substring(0, o).trim(), c = e.substring(o + a.length).trim(), l;
1017
+ if (a === "<=" || a === "=<") l = "<=";
1018
+ else if (a === ">=" || a === "=>") l = ">=";
1019
+ else if (a === "==" || a === "=") l = "==";
1020
+ else throw Error(`Unsupported operator "${a}"`);
1021
+ let u = P(s, t, r), d = P(c, t, r);
1022
+ if (u instanceof x && u.quadTerms.size > 0 || d instanceof x && d.quadTerms.size > 0) throw Error("Quadratic constraints are not supported (only linear constraints are allowed in MILP/LP/QP)");
1023
+ let f = u instanceof x ? new S(u.linearTerms, u.constant) : u, p = d instanceof x ? new S(d.linearTerms, d.constant) : d, m;
1024
+ if (m = l === "<=" ? f.le(p, n) : l === ">=" ? f.ge(p, n) : f.eq(p, n), F(m.lhs), !Number.isFinite(m.rhs)) throw Error(`Invalid non-finite right-hand side "${m.rhs}" in constraint.`);
1025
+ return m;
1026
+ }
1027
+ function me(e, t, n) {
1028
+ return pe(e, t, n);
1029
+ }
1030
+ function he(e) {
1031
+ return e instanceof N || e instanceof S || e instanceof x ? !0 : typeof e != "object" || !e ? !1 : "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 && "type" in e && typeof e.mul == "function";
1032
+ }
1033
+ function F(e) {
1034
+ if ("constant" in e && !Number.isFinite(e.constant)) throw Error(`Invalid non-finite constant "${e.constant}" in expression.`);
1035
+ if ("terms" in e && e.terms instanceof Map) {
1036
+ for (let [, t] of e.terms) if (!Number.isFinite(t)) throw Error(`Invalid non-finite coefficient "${t}" in expression.`);
1037
+ }
1038
+ if ("linearTerms" in e && e.linearTerms instanceof Map) {
1039
+ for (let [, t] of e.linearTerms) if (!Number.isFinite(t)) throw Error(`Invalid non-finite coefficient "${t}" in expression.`);
1040
+ }
1041
+ if ("quadTerms" in e && e.quadTerms instanceof Map) {
1042
+ for (let [, t] of e.quadTerms) if (!Number.isFinite(t.coeff)) throw Error(`Invalid non-finite quadratic coefficient "${t.coeff}" in expression.`);
1043
+ }
1044
+ }
1045
+ function ge(e) {
1046
+ return e instanceof v || typeof e == "object" && !!e && "sense" in e && "lhs" in e && "rhs" in e;
1047
+ }
1048
+ function _e(e, t) {
1049
+ let n = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Map(), i = e.join(""), a = "__washi_";
1050
+ for (; i.includes(a);) a += "_";
1051
+ let o = "";
1052
+ for (let i = 0; i < e.length; i++) if (o += e[i], i < t.length) {
607
1053
  let e = t[i];
608
- if (typeof e == "number") r += `(${e})`;
609
- else if (e instanceof E) {
610
- let t = `__v_${i}__`;
611
- n.set(t, e), r += t;
612
- } else if (e instanceof S) {
613
- `${i}`, r += `(${e.toString()})`;
614
- for (let [t] of e.terms) n.set(t.name, t);
615
- }
1054
+ if (typeof e == "number") {
1055
+ if (!Number.isFinite(e)) throw Error(`Invalid numeric value "${e}" in template expression: interpolated numbers must be finite.`);
1056
+ let t = `${a}num_${i}__`;
1057
+ r.set(t, e), o += `(${t})`;
1058
+ } else if (ge(e)) throw Error("Cannot interpolate a constraint comparison (<=, >=, ==) inside a template expression: expected a finite number, Var, LinExpr, or QuadExpr.");
1059
+ else if (he(e)) {
1060
+ F(e);
1061
+ let t = `${a}ph_${i}__`;
1062
+ n.set(t, e), o += ` ${t} `;
1063
+ } else throw Error("Unsupported interpolation value in template expression: expected a finite number, Var, LinExpr, or QuadExpr.");
616
1064
  }
617
1065
  return {
618
- fullText: r,
619
- resolver: (e) => n.get(e)
1066
+ fullText: o,
1067
+ resolver: (e) => n.get(e),
1068
+ numberMap: r
620
1069
  };
621
1070
  }
622
- function M(e, ...t) {
623
- let { fullText: n, resolver: r } = j(e, t);
624
- return A(n, r);
625
- }
626
- function N(e, ...t) {
627
- let { fullText: n, resolver: r } = j(e, t);
628
- return /(<=|>=|==|=<|=>|=)/.test(n) ? A(n, r) : k(n, r);
1071
+ function ve(e, ...t) {
1072
+ let { fullText: n, resolver: r, numberMap: i } = _e(e, t);
1073
+ return /(<=|>=|==|=<|=>|=)/.test(n) ? pe(n, r, void 0, i) : P(n, r, i);
629
1074
  }
630
1075
  //#endregion
631
1076
  //#region src/modeler/lp-writer.ts
632
- function P(e, t, n) {
1077
+ function I(e) {
1078
+ return typeof e == "object" && !!e && typeof e.lpString == "string" && e.nameToVar instanceof Map && e.nameToConstraint instanceof Map;
1079
+ }
1080
+ function L(e, t = "x", n = 0) {
633
1081
  let r = e.replace(/[^a-zA-Z0-9_]/g, "_");
634
1082
  return /^[0-9]/.test(r) && (r = `${t}_${r}`), (!r || r.length === 0) && (r = `${t}_${n}`), r;
635
1083
  }
636
- function F(e) {
637
- let { sense: t, objective: n, constraints: r, variables: i } = e, a = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Set();
638
- for (let e of i) {
639
- let t = P(e.name, "x", e.id), n = t, r = 1;
640
- for (; c.has(n);) n = `${t}_${r++}`;
641
- c.add(n), o.set(e, n), a.set(n, e);
1084
+ function ye(e) {
1085
+ 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();
1086
+ for (let e = 0; e < t.length; e++) {
1087
+ let n = t[e];
1088
+ s.set(n, e);
1089
+ let a = L(n.name, "x", e), o = a, l = 1;
1090
+ for (; c.has(o);) o = `${a}_${l++}`;
1091
+ c.add(o), i.set(n, o), r.set(o, n);
1092
+ }
1093
+ let l = /* @__PURE__ */ new Set();
1094
+ for (let e = 0; e < n.length; e++) {
1095
+ let t = n[e], r = L(t.name, "c", e + 1), i = r, s = 1;
1096
+ for (; l.has(i);) i = `${r}_${s++}`;
1097
+ l.add(i), o.set(t, i), a.set(i, t);
642
1098
  }
643
- let l = /* @__PURE__ */ new Set(), u = /* @__PURE__ */ new Map();
644
- for (let e of r) {
645
- let t = P(e.name, "c", e.id), n = t, r = 1;
646
- for (; l.has(n);) n = `${t}_${r++}`;
647
- l.add(n), u.set(e, n), s.set(n, e);
648
- }
649
- let d = [];
650
- d.push(t && t.toLowerCase() === "maximize" ? "Maximize" : "Minimize");
651
- let f = [], p = !0, m = n instanceof x ? n.linearTerms : n.terms;
652
- if (m) for (let [e, t] of m) {
653
- if (Math.abs(t) < 1e-12) continue;
654
- let n = o.get(e), r = t >= 0 ? p ? "" : "+ " : "- ", i = Math.abs(t), a = i === 1 ? "" : `${i} `;
655
- f.push(`${r}${a}${n}`), p = !1;
1099
+ return {
1100
+ nameToVar: r,
1101
+ varToName: i,
1102
+ nameToConstraint: a,
1103
+ constraintToName: o,
1104
+ constrToName: o,
1105
+ varOrder: s
1106
+ };
1107
+ }
1108
+ function R(e) {
1109
+ let { sense: t, objective: n, constraints: r, variables: i } = e, { nameToVar: a, varToName: o, nameToConstraint: s, constrToName: c, varOrder: l } = ye(e), u = [];
1110
+ u.push(t && t.toLowerCase() === "maximize" ? "Maximize" : "Minimize");
1111
+ let d = [], f = !0, p = n instanceof x ? n.linearTerms : n.terms;
1112
+ if (p) for (let [e, t] of p) {
1113
+ if (t === 0) continue;
1114
+ let n = o.get(e), r = t >= 0 ? f ? "" : "+ " : "- ", i = Math.abs(t), a = i === 1 ? "" : `${i} `;
1115
+ d.push(`${r}${a}${n}`), f = !1;
656
1116
  }
657
1117
  if (n instanceof x && n.quadTerms.size > 0) {
658
- let e = [], t = !0;
659
- for (let r of n.quadTerms.values()) {
660
- if (Math.abs(r.coeff) < 1e-12) continue;
661
- let n = 2 * r.coeff, i = n >= 0 ? t ? "" : "+ " : "- ", a = Math.abs(n), s = a === 1 ? "" : `${a} `, c = o.get(r.var1), l = o.get(r.var2);
662
- r.var1 === r.var2 ? e.push(`${i}${s}${c} ^ 2`) : e.push(`${i}${s}${c} * ${l}`), t = !1;
1118
+ let e = [], t = !0, r = Array.from(n.quadTerms.values()).filter((e) => e.coeff !== 0).map((e) => {
1119
+ let t = l.get(e.var1) ?? e.var1.id, n = l.get(e.var2) ?? e.var2.id, [r, i, a, o] = t <= n ? [
1120
+ e.var1,
1121
+ e.var2,
1122
+ t,
1123
+ n
1124
+ ] : [
1125
+ e.var2,
1126
+ e.var1,
1127
+ n,
1128
+ t
1129
+ ];
1130
+ return {
1131
+ firstVar: r,
1132
+ secondVar: i,
1133
+ firstIdx: a,
1134
+ secondIdx: o,
1135
+ coeff: e.coeff
1136
+ };
1137
+ }).sort((e, t) => e.firstIdx - t.firstIdx || e.secondIdx - t.secondIdx);
1138
+ for (let n of r) {
1139
+ let r = 2 * n.coeff, i = r >= 0 ? t ? "" : "+ " : "- ", a = Math.abs(r), s = a === 1 ? "" : `${a} `, c = o.get(n.firstVar), l = o.get(n.secondVar);
1140
+ n.firstVar === n.secondVar ? e.push(`${i}${s}${c} ^ 2`) : e.push(`${i}${s}${c} * ${l}`), t = !1;
663
1141
  }
664
1142
  if (e.length > 0) {
665
1143
  let t = `[ ${e.join(" ")} ] / 2`;
666
- f.length > 0 ? f.push("+", t) : f.push(t);
1144
+ d.length > 0 ? d.push("+", t) : d.push(t);
667
1145
  }
668
1146
  }
669
- let h = f.length > 0 ? f.join(" ") : "0";
670
- d.push(` obj: ${h}`), d.push("Subject To");
1147
+ let m = d.length > 0 ? d.join(" ") : "0";
1148
+ u.push(` obj: ${m}`), u.push("Subject To");
671
1149
  for (let e of r) {
672
- let t = u.get(e), n = [], r = !0;
1150
+ let t = c.get(e), n = [], r = !0;
673
1151
  for (let [t, i] of e.lhs.terms) {
674
- if (Math.abs(i) < 1e-12) continue;
1152
+ if (i === 0) continue;
675
1153
  let e = o.get(t), a = i >= 0 ? r ? "" : "+ " : "- ", s = Math.abs(i), c = s === 1 ? "" : `${s} `;
676
1154
  n.push(`${a}${c}${e}`), r = !1;
677
1155
  }
678
1156
  let i = n.length > 0 ? n.join(" ") : "0", a = e.sense === "==" ? "=" : e.sense;
679
- d.push(` ${t}: ${i} ${a} ${e.rhs}`);
1157
+ u.push(` ${t}: ${i} ${a} ${e.rhs}`);
680
1158
  }
681
- d.push("Bounds");
682
- let g = [], _ = [];
1159
+ u.push("Bounds");
1160
+ let h = [], g = [];
683
1161
  for (let e of i) {
684
1162
  let t = o.get(e);
685
1163
  if (e.type === "Binary") {
686
- _.push(t);
1164
+ g.push(t);
687
1165
  continue;
688
1166
  }
689
- e.type === "Integer" && g.push(t);
1167
+ e.type === "Integer" && h.push(t);
690
1168
  let n = e.lb === -Infinity, r = e.ub === Infinity;
691
- n && r ? d.push(` ${t} free`) : n && !r ? d.push(` -infinity <= ${t} <= ${e.ub}`) : !n && r ? e.lb !== 0 && d.push(` ${t} >= ${e.lb}`) : d.push(` ${e.lb} <= ${t} <= ${e.ub}`);
1169
+ n && r ? u.push(` ${t} free`) : n && !r ? u.push(` -infinity <= ${t} <= ${e.ub}`) : !n && r ? (e.lb !== 0 || e.initialValue !== void 0) && u.push(` ${t} >= ${e.lb}`) : u.push(` ${e.lb} <= ${t} <= ${e.ub}`);
692
1170
  }
693
- if (g.length > 0) {
694
- d.push("Generals");
695
- for (let e = 0; e < g.length; e += 10) d.push(" " + g.slice(e, e + 10).join(" "));
1171
+ if (h.length > 0) {
1172
+ u.push("Generals");
1173
+ for (let e = 0; e < h.length; e += 10) u.push(" " + h.slice(e, e + 10).join(" "));
696
1174
  }
697
- if (_.length > 0) {
698
- d.push("Binary");
699
- for (let e = 0; e < _.length; e += 10) d.push(" " + _.slice(e, e + 10).join(" "));
1175
+ if (g.length > 0) {
1176
+ u.push("Binary");
1177
+ for (let e = 0; e < g.length; e += 10) u.push(" " + g.slice(e, e + 10).join(" "));
700
1178
  }
701
- return d.push("End"), {
702
- lpString: d.join("\n") + "\n",
1179
+ return u.push("End"), {
1180
+ lpString: u.join("\n") + "\n",
703
1181
  nameToVar: a,
704
1182
  nameToConstraint: s
705
1183
  };
706
1184
  }
707
1185
  //#endregion
708
1186
  //#region src/modeler/solvers/registry.ts
709
- var I = Symbol.for("washi.solverRegistry"), L = Symbol.for("washi.defaultSolverFactory"), R = globalThis;
710
- R[I] || (R[I] = /* @__PURE__ */ new Map());
711
- var z = R[I];
712
- function B(e, t) {
713
- z.set(e.toLowerCase(), t);
1187
+ var be = Symbol.for("washi.solverRegistry"), xe = Symbol.for("washi.defaultSolverFactory"), z = globalThis;
1188
+ z[be] || (z[be] = /* @__PURE__ */ new Map());
1189
+ var B = z[be];
1190
+ function V(e, t) {
1191
+ B.set(e.toLowerCase(), t);
714
1192
  }
715
- function V(e) {
1193
+ function Se(e) {
716
1194
  if (typeof e == "string") {
717
- let t = z.get(e.toLowerCase());
1195
+ let t = B.get(e.toLowerCase());
718
1196
  if (!t) throw Error(`Solver '${e}' is not registered.`);
719
- R[L] = t;
720
- } else R[L] = typeof e == "function" ? e : () => e;
1197
+ z[xe] = t;
1198
+ } else z[xe] = typeof e == "function" ? e : () => e;
721
1199
  }
722
1200
  function H(e, t) {
723
1201
  if (!e) {
724
- let e = R[L] || z.get("highs");
1202
+ let e = z[xe] || B.get("highs");
725
1203
  if (!e) throw Error(`No default solver available. Available solvers: ${U().join(", ")}`);
726
1204
  return e(t);
727
1205
  }
728
1206
  if (typeof e == "string") {
729
- let n = e.toLowerCase(), r = z.get(n);
1207
+ let n = e.toLowerCase(), r = B.get(n);
730
1208
  if (!r) throw Error(n === "glpk" ? `Solver 'glpk' not found. Available solvers: ${U().join(", ")}. Please import '@ohtaman/washi/solvers/glpk' before selecting this solver.` : n === "z3" ? `Solver 'z3' not found. Available solvers: ${U().join(", ")}. Please import '@ohtaman/washi/solvers/z3' before selecting this solver.` : `Solver '${e}' not found. Available solvers: ${U().join(", ")}`);
731
1209
  return r(t);
732
1210
  }
733
1211
  return e;
734
1212
  }
735
1213
  function U() {
736
- return Array.from(z.keys());
1214
+ return Array.from(B.keys());
737
1215
  }
738
1216
  //#endregion
739
1217
  //#region \0washi-browser-worker-factory
740
- function W() {
1218
+ function Ce() {
741
1219
  let e = r("highs-worker.js");
742
1220
  if (new URL(e).origin === window.location.origin) return new Worker(e, { type: "module" });
743
1221
  let t = URL.createObjectURL(new Blob(["import " + JSON.stringify(e) + ";"], { type: "text/javascript" }));
@@ -750,73 +1228,198 @@ function W() {
750
1228
  }
751
1229
  //#endregion
752
1230
  //#region src/modeler/solvers/highs/worker-client.ts
753
- var G = null, K = 0, q = /* @__PURE__ */ new Map();
754
- function J() {
1231
+ var W = null, we = 0, G = /* @__PURE__ */ new Map();
1232
+ function K(e, t) {
1233
+ return e ? {
1234
+ Status: "Interrupted",
1235
+ StatusCode: 17,
1236
+ HasPrimalSolution: !0,
1237
+ IsMip: !0,
1238
+ ObjectiveValue: e.objectiveValue,
1239
+ ObjectiveOffsetApplied: !0,
1240
+ ...t?.objectiveBound === void 0 ? e.mipDualBound === void 0 ? {} : { MipDualBound: e.mipDualBound } : { MipDualBound: t.objectiveBound },
1241
+ ...t?.relGap === void 0 ? e.mipGap === void 0 ? {} : { MipGap: e.mipGap } : { MipGap: t.relGap },
1242
+ Columns: e.columns,
1243
+ Rows: []
1244
+ } : {
1245
+ Status: "Interrupted",
1246
+ StatusCode: 17,
1247
+ HasPrimalSolution: !1,
1248
+ ObjectiveValue: NaN,
1249
+ ObjectiveOffsetApplied: !0,
1250
+ ...t?.objectiveBound === void 0 ? {} : { MipDualBound: t.objectiveBound },
1251
+ ...t?.relGap === void 0 ? {} : { MipGap: t.relGap },
1252
+ Columns: {},
1253
+ Rows: []
1254
+ };
1255
+ }
1256
+ function q(e) {
1257
+ let t = G.values().next().value;
1258
+ if (t && !t.dispatched) {
1259
+ t.dispatched = !0;
1260
+ try {
1261
+ e.postMessage({
1262
+ type: "SOLVE",
1263
+ id: t.id,
1264
+ lpString: t.lpString,
1265
+ options: t.workerOptions,
1266
+ ...t.reportProgress ? { reportProgress: !0 } : {}
1267
+ });
1268
+ } catch (n) {
1269
+ G.delete(t.id), t.cleanupAbort?.(), t.reject(n), q(e);
1270
+ }
1271
+ }
1272
+ }
1273
+ function Te() {
1274
+ let e = W;
1275
+ e && (W = null, e.terminate());
1276
+ for (let e of G.values()) e.dispatched = !1;
1277
+ if (G.size > 0) {
1278
+ let e = Ee();
1279
+ if (e) q(e);
1280
+ else {
1281
+ let e = /* @__PURE__ */ Error("[WaSHI] Failed to recreate HiGHS Worker after interruption.");
1282
+ for (let t of Array.from(G.values())) G.delete(t.id), t.cleanupAbort?.(), t.reject(e);
1283
+ }
1284
+ }
1285
+ }
1286
+ function Ee() {
755
1287
  if (typeof window > "u" || typeof Worker > "u") return null;
756
- if (!G) try {
757
- G = W();
758
- let e = G;
759
- G.onmessage = (e) => {
760
- let { type: t, id: n, result: r, runTime: i, error: a } = e.data, o = q.get(n);
761
- o && (q.delete(n), t === "SOLVE_SUCCESS" ? o.resolve({
762
- result: r,
763
- runTime: i
764
- }) : o.reject(Error(a || "Worker solve failed")));
1288
+ if (!W) try {
1289
+ W = Ce();
1290
+ let e = W;
1291
+ W.onmessage = (t) => {
1292
+ let { type: n, id: r, result: i, runTime: a, error: o, progress: s, incumbent: c } = t.data, l = G.get(r);
1293
+ if (l) {
1294
+ if (n === "SOLVE_PROGRESS") {
1295
+ if (s) {
1296
+ l.lastProgress = s;
1297
+ try {
1298
+ l.onProgress?.(s);
1299
+ } catch (e) {
1300
+ let t = G.get(r);
1301
+ if (t) {
1302
+ let n = G.keys().next().value;
1303
+ G.delete(r), t.cleanupAbort?.(), n === r && Te(), t.reject(e);
1304
+ }
1305
+ }
1306
+ }
1307
+ return;
1308
+ }
1309
+ if (n === "SOLVE_INCUMBENT") {
1310
+ c && (l.lastIncumbent = c);
1311
+ return;
1312
+ }
1313
+ G.delete(r), l.cleanupAbort?.(), n === "SOLVE_SUCCESS" ? l.resolve({
1314
+ result: i,
1315
+ runTime: a
1316
+ }) : l.reject(Error(o || "Worker solve failed")), W === e && q(e);
1317
+ }
765
1318
  };
766
1319
  let t = (t) => {
767
- if (G === e) {
768
- G = null, e.terminate();
769
- for (let e of q.values()) e.reject(t);
770
- q.clear();
1320
+ if (W === e) {
1321
+ W = null, e.terminate();
1322
+ for (let e of G.values()) e.cleanupAbort?.(), e.reject(t);
1323
+ G.clear();
771
1324
  }
772
1325
  };
773
- G.onerror = (e) => t(/* @__PURE__ */ Error(`[WaSHI] HiGHS Worker failed to load or execute. Check Worker assets, CORS and CSP. ${e?.message || ""}`)), G.onmessageerror = () => t(/* @__PURE__ */ Error("[WaSHI] Could not deserialize the HiGHS Worker response."));
1326
+ W.onerror = (e) => t(/* @__PURE__ */ Error(`[WaSHI] HiGHS Worker failed to load or execute. Check Worker assets, CORS and CSP. ${e?.message || ""}`)), W.onmessageerror = () => t(/* @__PURE__ */ Error("[WaSHI] Could not deserialize the HiGHS Worker response."));
774
1327
  } catch {
775
- G = null;
776
- }
777
- return G;
778
- }
779
- async function Y(e, n = {}) {
780
- let r = await a(n.wasmUrl), i = n.useWorker ?? typeof window < "u" ? J() : null;
781
- if (!i) return t(e, n);
782
- let o = {
783
- ...n,
784
- wasmUrl: r
785
- }, s = `req_${++K}`;
786
- return new Promise((t, n) => {
787
- q.set(s, {
788
- resolve: t,
1328
+ W = null;
1329
+ }
1330
+ return W;
1331
+ }
1332
+ async function De(t, n = {}) {
1333
+ if (n.signal?.aborted) return {
1334
+ result: K(),
1335
+ runTime: 0
1336
+ };
1337
+ let r = await i(n.wasmUrl);
1338
+ if (n.signal?.aborted) return {
1339
+ result: K(),
1340
+ runTime: 0
1341
+ };
1342
+ let a = n.useWorker ?? typeof window < "u" ? Ee() : null;
1343
+ if (!a) return e(t, n);
1344
+ let { signal: o, onProgress: s, ...c } = n, l = {};
1345
+ for (let [e, t] of Object.entries(c)) typeof t != "function" && (l[e] = t);
1346
+ l.wasmUrl = r;
1347
+ let u = !!(s || o), d = `req_${++we}`, f = performance.now();
1348
+ return new Promise((e, n) => {
1349
+ let r = {
1350
+ id: d,
1351
+ lpString: t,
1352
+ workerOptions: l,
1353
+ reportProgress: u,
1354
+ startTime: f,
1355
+ onProgress: s,
1356
+ resolve: e,
789
1357
  reject: n
790
- });
791
- try {
792
- i.postMessage({
793
- type: "SOLVE",
794
- id: s,
795
- lpString: e,
796
- options: o
797
- });
798
- } catch (e) {
799
- q.delete(s), n(e);
1358
+ };
1359
+ if (o) {
1360
+ let e = () => {
1361
+ let e = G.get(d);
1362
+ if (!e) return;
1363
+ let t = G.keys().next().value;
1364
+ G.delete(d), e.cleanupAbort?.(), t === d && Te();
1365
+ let n = (performance.now() - e.startTime) / 1e3;
1366
+ e.resolve({
1367
+ result: K(e.lastIncumbent, e.lastProgress),
1368
+ runTime: n
1369
+ });
1370
+ };
1371
+ o.addEventListener("abort", e, { once: !0 }), r.cleanupAbort = () => o.removeEventListener("abort", e);
800
1372
  }
1373
+ G.set(d, r), q(a);
801
1374
  });
802
1375
  }
803
1376
  //#endregion
804
1377
  //#region src/modeler/solvers/highs/solver.ts
805
- function X(e) {
1378
+ function Oe(e, t) {
1379
+ if (typeof t == "number") switch (t) {
1380
+ case 0: return "NotSolved";
1381
+ case 1:
1382
+ case 2:
1383
+ case 3:
1384
+ case 4:
1385
+ case 5: return "Error";
1386
+ case 6: return "Unknown";
1387
+ case 7: return "Optimal";
1388
+ case 8: return "Infeasible";
1389
+ case 9: return "InfeasibleOrUnbounded";
1390
+ case 10: return "Unbounded";
1391
+ case 11: return "ObjectiveBound";
1392
+ case 12: return "ObjectiveTarget";
1393
+ case 13: return "TimeLimit";
1394
+ case 14: return "IterationLimit";
1395
+ case 15: return "Unknown";
1396
+ case 16: return "SolutionLimit";
1397
+ case 17: return "Interrupted";
1398
+ case 18: return "MemoryLimit";
1399
+ }
806
1400
  if (typeof e != "string") return "Unknown";
807
- let t = e.trim();
808
- if (!t) return "Unknown";
809
- switch (t) {
1401
+ let n = e.trim();
1402
+ if (!n) return "Unknown";
1403
+ switch (n) {
810
1404
  case "Optimal": return "Optimal";
811
1405
  case "Infeasible": return "Infeasible";
812
1406
  case "Unbounded": return "Unbounded";
813
- case "Primal infeasible or unbounded": return "Unknown";
1407
+ case "Primal infeasible or unbounded":
1408
+ case "InfeasibleOrUnbounded": return "InfeasibleOrUnbounded";
814
1409
  case "Time limit reached":
815
1410
  case "TimeLimit": return "TimeLimit";
816
1411
  case "Iteration limit reached":
817
1412
  case "IterationLimit": return "IterationLimit";
1413
+ case "Solution limit reached":
1414
+ case "SolutionLimit": return "SolutionLimit";
1415
+ case "Memory limit reached":
1416
+ case "MemoryLimit": return "MemoryLimit";
818
1417
  case "Bound on objective reached":
1418
+ case "ObjectiveBound": return "ObjectiveBound";
819
1419
  case "Target for objective reached":
1420
+ case "ObjectiveTarget": return "ObjectiveTarget";
1421
+ case "Interrupted":
1422
+ case "User interrupt": return "Interrupted";
820
1423
  case "Feasible": return "Feasible";
821
1424
  case "Not Set": return "NotSolved";
822
1425
  case "Load error":
@@ -828,13 +1431,32 @@ function X(e) {
828
1431
  case "Empty":
829
1432
  case "Unknown": return "Unknown";
830
1433
  }
831
- let n = t.toLowerCase();
832
- return n.includes("infeasible") && n.includes("unbounded") ? "Unknown" : n.includes("infeasible") ? "Infeasible" : n.includes("unbounded") ? "Unbounded" : n.includes("time limit") || n.includes("timelimit") ? "TimeLimit" : n.includes("iteration limit") || n.includes("iterationlimit") ? "IterationLimit" : n.includes("error") ? "Error" : n === "optimal" ? "Optimal" : "Unknown";
1434
+ let r = n.toLowerCase();
1435
+ return r.includes("infeasible") && r.includes("unbounded") ? "InfeasibleOrUnbounded" : r.includes("infeasible") ? "Infeasible" : r.includes("unbounded") ? "Unbounded" : r.includes("time limit") || r.includes("timelimit") ? "TimeLimit" : r.includes("iteration limit") || r.includes("iterationlimit") ? "IterationLimit" : r.includes("solution limit") || r.includes("solutionlimit") ? "SolutionLimit" : r.includes("memory limit") || r.includes("memorylimit") ? "MemoryLimit" : r.includes("bound on objective") ? "ObjectiveBound" : r.includes("target for objective") ? "ObjectiveTarget" : r.includes("interrupt") ? "Interrupted" : r.includes("error") ? "Error" : r === "optimal" ? "Optimal" : "Unknown";
833
1436
  }
834
- var Z = class e {
1437
+ function ke(e, t, n) {
1438
+ switch (Oe(e, t)) {
1439
+ case "Optimal": return "Optimal";
1440
+ case "Infeasible": return "Infeasible";
1441
+ case "Unbounded": return "Unbounded";
1442
+ case "TimeLimit": return "TimeLimit";
1443
+ case "IterationLimit": return "IterationLimit";
1444
+ case "Feasible": return "Feasible";
1445
+ case "ObjectiveBound":
1446
+ case "ObjectiveTarget":
1447
+ case "SolutionLimit": return n === !1 ? "Unknown" : "Feasible";
1448
+ case "MemoryLimit":
1449
+ case "Interrupted": return n ? "Feasible" : "Unknown";
1450
+ case "NotSolved": return "NotSolved";
1451
+ case "Error": return "Error";
1452
+ default: return "Unknown";
1453
+ }
1454
+ }
1455
+ var J = class e {
835
1456
  static capabilities = {
836
1457
  supportsMIP: !0,
837
- supportsQP: !0
1458
+ supportsQP: !0,
1459
+ supportsWarmStart: !0
838
1460
  };
839
1461
  name = "HiGHS";
840
1462
  version = "1.15.3";
@@ -843,119 +1465,750 @@ var Z = class e {
843
1465
  constructor(e = {}) {
844
1466
  this.options = e;
845
1467
  }
846
- async solve(e, t, n = {}) {
847
- let r = {
1468
+ async solve(e, t = {}, n = {}) {
1469
+ let r = I(t), i = r ? n : t, a = {
848
1470
  ...this.options,
849
- ...n
850
- }, { lpString: i, nameToVar: a, nameToConstraint: o } = t, { result: s, runTime: c } = await Y(i, r), l = X(s?.Status);
851
- if (l === "Infeasible" || l === "Unbounded") return {
852
- status: l,
1471
+ ...i
1472
+ }, o = e.objective.constant || 0;
1473
+ for (let t of e.variables) A(t);
1474
+ let { lpString: s, nameToVar: c, nameToConstraint: l } = r ? t : R(e), u = Object.create(null), d = !1, f = /* @__PURE__ */ new Set();
1475
+ for (let [e, t] of c.entries()) {
1476
+ f.add(t);
1477
+ let n = ie(t);
1478
+ n !== void 0 && (u[e] = n, d = !0);
1479
+ }
1480
+ for (let t = 0; t < e.variables.length; t++) {
1481
+ let n = e.variables[t];
1482
+ if (!f.has(n)) {
1483
+ let e = ie(n);
1484
+ if (e !== void 0) {
1485
+ let r = L(n.name, "x", t);
1486
+ Object.prototype.hasOwnProperty.call(u, r) || (u[r] = e, d = !0);
1487
+ }
1488
+ }
1489
+ }
1490
+ let p = a;
1491
+ (o !== 0 || d || p.initialValues !== void 0) && (p = {
1492
+ ...p,
1493
+ ...o === 0 ? {} : { objectiveOffset: o },
1494
+ initialValues: d ? u : void 0
1495
+ });
1496
+ let { result: m, runTime: h } = await De(s, p), g = m?.ObjectiveOffsetApplied ? 0 : o, _ = Oe(m?.Status, m?.StatusCode), v = typeof m?.IsMip == "boolean" ? m.IsMip : e.variables.some((e) => e.type === "Integer" || e.type === "Binary"), y;
1497
+ if (typeof m?.HasPrimalSolution == "boolean") y = m.HasPrimalSolution;
1498
+ else {
1499
+ let e = m?.Columns ? Object.values(m.Columns) : [], t = e.length > 0 && e.some((e) => typeof e?.Primal == "number" && Number.isFinite(e.Primal)), n = typeof m?.ObjectiveValue == "number" && Number.isFinite(m.ObjectiveValue);
1500
+ y = _ === "Infeasible" || _ === "Unbounded" || _ === "InfeasibleOrUnbounded" || _ === "Error" || _ === "NotSolved" ? !1 : _ === "Optimal" || _ === "Feasible" ? n || t : n && (e.length === 0 || t);
1501
+ }
1502
+ let b = ke(m?.Status, m?.StatusCode, y), x = null;
1503
+ y && typeof m?.ObjectiveValue == "number" && Number.isFinite(m.ObjectiveValue) && (x = m.ObjectiveValue + g);
1504
+ let S, C, w;
1505
+ if (typeof m?.MipDualBound == "number" && Number.isFinite(m.MipDualBound) ? S = m.MipDualBound + g : !v && _ === "Optimal" && x !== null && (S = x), x !== null && S !== void 0 && Number.isFinite(S) && (w = Math.abs(x - S)), g !== 0 && v && x !== null && S !== void 0 && w !== void 0) {
1506
+ if (w === 0) C = 0;
1507
+ else if (Math.abs(x) > 0) {
1508
+ let e = w / Math.abs(x);
1509
+ C = Number.isFinite(e) ? e : void 0;
1510
+ } else C = void 0;
1511
+ } else typeof m?.MipGap == "number" && Number.isFinite(m.MipGap) ? C = m.MipGap : v && _ === "Optimal" && y && (w === void 0 || w === 0) && (C = 0);
1512
+ if (!y) return {
1513
+ status: b,
1514
+ terminationStatus: _,
1515
+ hasPrimalSolution: !1,
853
1516
  objectiveValue: null,
1517
+ ...S === void 0 ? {} : { objectiveBound: S },
1518
+ ...C === void 0 ? {} : {
1519
+ relGap: C,
1520
+ mipGap: C
1521
+ },
1522
+ ...w === void 0 ? {} : { absGap: w },
854
1523
  values: {},
855
1524
  reducedCosts: {},
856
1525
  duals: {},
857
1526
  slacks: {},
858
- runTime: c,
859
- raw: s
1527
+ runTime: h,
1528
+ raw: m
860
1529
  };
861
- let u = s.ObjectiveValue, d = typeof u == "number" ? u + e.objective.constant : null, f = {}, p = {}, m = {}, h = {};
862
- if (s.Columns) for (let [e, t] of Object.entries(s.Columns)) {
863
- let n = a.get(e);
1530
+ let T = (e, t, n) => {
1531
+ Object.defineProperty(e, t, {
1532
+ value: n,
1533
+ enumerable: !0,
1534
+ writable: !0,
1535
+ configurable: !0
1536
+ });
1537
+ }, E = {}, D = {}, O = {}, k = {};
1538
+ if (m.Columns) for (let [e, t] of Object.entries(m.Columns)) {
1539
+ let n = c.get(e);
864
1540
  if (n) {
865
- let e = Math.abs(t.Primal) < 1e-9 ? 0 : t.Primal;
866
- f[n.name] = e, t.Dual !== void 0 && (m[n.name] = Math.abs(t.Dual) < 1e-9 ? 0 : t.Dual);
1541
+ if (typeof t.Primal == "number" && Number.isFinite(t.Primal)) {
1542
+ let e = Math.abs(t.Primal) < 1e-9 ? 0 : t.Primal;
1543
+ T(E, n.name, e);
1544
+ }
1545
+ typeof t.Dual == "number" && Number.isFinite(t.Dual) && T(O, n.name, Math.abs(t.Dual) < 1e-9 ? 0 : t.Dual);
867
1546
  }
868
1547
  }
869
- if (s.Rows) {
870
- let e = Array.isArray(s.Rows) ? s.Rows : Object.values(s.Rows);
1548
+ if (m.Rows) {
1549
+ let e = Array.isArray(m.Rows) ? m.Rows : Object.values(m.Rows);
871
1550
  for (let t of e) {
872
- let e = t.Name, n = o.get(e);
873
- if (n && (t.Dual !== void 0 && (p[n.name] = Math.abs(t.Dual) < 1e-9 ? 0 : t.Dual), t.Primal !== void 0)) {
1551
+ let e = t.Name, n = e ? l.get(e) : void 0;
1552
+ if (n && (typeof t.Dual == "number" && Number.isFinite(t.Dual) && T(D, n.name, Math.abs(t.Dual) < 1e-9 ? 0 : t.Dual), typeof t.Primal == "number" && Number.isFinite(t.Primal))) {
874
1553
  let e = n.rhs - t.Primal;
875
- h[n.name] = Math.abs(e) < 1e-9 ? 0 : e;
1554
+ T(k, n.name, Math.abs(e) < 1e-9 ? 0 : e);
876
1555
  }
877
1556
  }
878
1557
  }
879
1558
  return {
880
- status: l,
881
- objectiveValue: d,
882
- values: f,
883
- reducedCosts: m,
884
- duals: p,
885
- slacks: h,
886
- runTime: c,
887
- raw: s
1559
+ status: b,
1560
+ terminationStatus: _,
1561
+ hasPrimalSolution: !0,
1562
+ objectiveValue: x,
1563
+ ...S === void 0 ? {} : { objectiveBound: S },
1564
+ ...C === void 0 ? {} : {
1565
+ relGap: C,
1566
+ mipGap: C
1567
+ },
1568
+ ...w === void 0 ? {} : { absGap: w },
1569
+ values: E,
1570
+ reducedCosts: O,
1571
+ duals: D,
1572
+ slacks: k,
1573
+ runTime: h,
1574
+ raw: m
888
1575
  };
889
1576
  }
890
- }, Q = Z;
891
- function ne() {
892
- B("highs", (e) => new Z(e)), V((e) => new Z(e));
1577
+ }, Ae = J;
1578
+ function je() {
1579
+ V("highs", (e) => new J(e)), Se((e) => new J(e));
893
1580
  }
894
- ne();
1581
+ je();
895
1582
  //#endregion
896
1583
  //#region src/modeler/solvers/mock.ts
897
- var $ = class {
1584
+ var Me = class {
898
1585
  name;
899
1586
  capabilities;
900
1587
  options;
901
1588
  constructor(e = {}) {
902
1589
  this.name = e.name || "MockSolver", this.capabilities = e.capabilities || {
903
1590
  supportsMIP: !0,
904
- supportsQP: !0
1591
+ supportsQP: !0,
1592
+ supportsWarmStart: !0
905
1593
  }, this.options = e;
906
1594
  }
907
- async solve(e, t, n = {}) {
908
- let r = {
1595
+ async solve(e, t = {}, n = {}) {
1596
+ let r = I(t) ? n : t, i = {
909
1597
  ...this.options,
910
- ...n
1598
+ ...r
1599
+ }, a = !!(i.values && Object.keys(i.values).length > 0 || i.boolValues && Object.keys(i.boolValues).length > 0);
1600
+ if (i.signal?.aborted) return {
1601
+ status: "Unknown",
1602
+ terminationStatus: "Interrupted",
1603
+ hasPrimalSolution: !1,
1604
+ objectiveValue: null,
1605
+ values: {},
1606
+ runTime: 0,
1607
+ raw: { mock: !0 }
911
1608
  };
912
- return r.delayMs && r.delayMs > 0 && await new Promise((e) => setTimeout(e, r.delayMs)), {
913
- status: r.status || "Optimal",
914
- objectiveValue: r.objectiveValue ?? 0,
915
- values: r.values || {},
916
- reducedCosts: r.reducedCosts,
917
- duals: r.duals,
918
- slacks: r.slacks,
1609
+ if (i.delayMs && i.delayMs > 0 && (await new Promise((e) => {
1610
+ let t = setTimeout(() => {
1611
+ i.signal?.removeEventListener("abort", n), e();
1612
+ }, i.delayMs), n = () => {
1613
+ clearTimeout(t), e();
1614
+ };
1615
+ i.signal?.addEventListener("abort", n, { once: !0 });
1616
+ }), i.signal?.aborted)) {
1617
+ let e = !!(i.hasPrimalSolution ?? a);
1618
+ return {
1619
+ status: e ? "Feasible" : "Unknown",
1620
+ terminationStatus: "Interrupted",
1621
+ hasPrimalSolution: e,
1622
+ objectiveValue: e ? i.objectiveValue ?? 0 : null,
1623
+ ...i.objectiveBound === void 0 ? {} : { objectiveBound: i.objectiveBound },
1624
+ ...i.relGap === void 0 ? i.mipGap === void 0 ? {} : {
1625
+ relGap: i.mipGap,
1626
+ mipGap: i.mipGap
1627
+ } : {
1628
+ relGap: i.relGap,
1629
+ mipGap: i.relGap
1630
+ },
1631
+ ...i.absGap === void 0 ? {} : { absGap: i.absGap },
1632
+ values: e && i.values || {},
1633
+ ...e && i.boolValues !== void 0 ? { boolValues: i.boolValues } : {},
1634
+ runTime: .001,
1635
+ raw: { mock: !0 }
1636
+ };
1637
+ }
1638
+ if (i.onProgress && Array.isArray(i.progressEvents)) for (let e of i.progressEvents) {
1639
+ if (i.signal?.aborted) break;
1640
+ i.onProgress(e);
1641
+ }
1642
+ let o = i.status || "Optimal", s = i.terminationStatus ?? o, c = i.hasPrimalSolution === void 0 ? o === "Optimal" || o === "Feasible" || o === "Sat" || (o === "TimeLimit" || o === "IterationLimit" || s === "Interrupted") && a : i.hasPrimalSolution;
1643
+ if (i.onProgress && !Array.isArray(i.progressEvents) && !i.signal?.aborted) {
1644
+ let e = c ? i.objectiveValue ?? 0 : void 0;
1645
+ i.onProgress({
1646
+ elapsedTime: .001,
1647
+ elapsedSeconds: .001,
1648
+ ...typeof e == "number" && Number.isFinite(e) ? { objectiveValue: e } : {},
1649
+ ...i.objectiveBound === void 0 ? {} : { objectiveBound: i.objectiveBound },
1650
+ ...i.relGap === void 0 ? i.mipGap === void 0 ? {} : {
1651
+ relGap: i.mipGap,
1652
+ mipGap: i.mipGap
1653
+ } : {
1654
+ relGap: i.relGap,
1655
+ mipGap: i.relGap
1656
+ },
1657
+ ...i.absGap === void 0 ? {} : { absGap: i.absGap }
1658
+ });
1659
+ }
1660
+ return i.signal?.aborted && (s = "Interrupted", c = i.hasPrimalSolution === void 0 ? a : i.hasPrimalSolution, o = c ? "Feasible" : "Unknown"), {
1661
+ status: o,
1662
+ terminationStatus: s,
1663
+ hasPrimalSolution: c,
1664
+ objectiveValue: c ? i.objectiveValue ?? 0 : null,
1665
+ ...i.objectiveBound === void 0 ? {} : { objectiveBound: i.objectiveBound },
1666
+ ...i.relGap === void 0 ? i.mipGap === void 0 ? {} : {
1667
+ relGap: i.mipGap,
1668
+ mipGap: i.mipGap
1669
+ } : {
1670
+ relGap: i.relGap,
1671
+ mipGap: i.relGap
1672
+ },
1673
+ ...i.absGap === void 0 ? {} : { absGap: i.absGap },
1674
+ values: c && i.values || {},
1675
+ ...c && i.boolValues !== void 0 ? { boolValues: i.boolValues } : {},
1676
+ reducedCosts: i.reducedCosts,
1677
+ duals: i.duals,
1678
+ slacks: i.slacks,
919
1679
  runTime: .001,
920
1680
  raw: { mock: !0 }
921
1681
  };
922
1682
  }
923
1683
  };
924
- function re() {
925
- B("mock", (e) => new $(e));
1684
+ function Ne() {
1685
+ V("mock", (e) => new Me(e));
1686
+ }
1687
+ Ne();
1688
+ //#endregion
1689
+ //#region src/modeler/solution-validator.ts
1690
+ function Pe(e, t) {
1691
+ if (e === void 0) return 1e-6;
1692
+ if (typeof e != "number" || !Number.isFinite(e) || e < 0) throw Error(`Invalid ${t}: expected a finite non-negative number, received ${String(e)}.`);
1693
+ return e;
1694
+ }
1695
+ function Fe(e, t = {}) {
1696
+ let n = Pe(t.feasibilityTolerance, "feasibilityTolerance"), r = Pe(t.integralityTolerance, "integralityTolerance"), i = t.evaluateLogicalConstraints === void 0 || !!t.evaluateLogicalConstraints, a = t.values !== void 0 && t.values !== null;
1697
+ if (t.values !== void 0 && (typeof t.values != "object" || t.values === null || Array.isArray(t.values))) throw Error("Invalid values option: expected a plain object mapping variable names to values.");
1698
+ let o = t.values, s = [], c = Object.create(null), l = [], f = [], p = 0, m = (e) => {
1699
+ typeof e == "number" && !Number.isNaN(e) && e > p && (p = e);
1700
+ }, h = (e) => {
1701
+ s.push(e), Object.prototype.hasOwnProperty.call(c, e.name) || (c[e.name] = []), c[e.name].push(e), e.type !== "missing_value" && e.type !== "unchecked_constraint" && m(e.violation);
1702
+ }, g = /* @__PURE__ */ new Map(), _ = /* @__PURE__ */ new Map(), y = 0, b = 0, x = 0, S = 0;
1703
+ for (let t of e.variables) {
1704
+ let e = a ? Object.prototype.hasOwnProperty.call(o, t.name) ? o[t.name] : void 0 : t.value;
1705
+ if (e == null) {
1706
+ f.push(t.name), h({
1707
+ type: "missing_value",
1708
+ target: "variable",
1709
+ name: t.name,
1710
+ actual: e ?? null,
1711
+ violation: Infinity,
1712
+ message: `Variable "${t.name}" has no assigned solution value.`
1713
+ });
1714
+ continue;
1715
+ }
1716
+ if (typeof e != "number" || !Number.isFinite(e)) {
1717
+ h({
1718
+ type: "non_finite_value",
1719
+ target: "variable",
1720
+ name: t.name,
1721
+ actual: e,
1722
+ violation: Infinity,
1723
+ message: `Variable "${t.name}" has non-finite or non-numeric value: ${String(e)}.`
1724
+ });
1725
+ continue;
1726
+ }
1727
+ g.set(t, e), y++;
1728
+ let i = t.type === "Binary" || t.type === "BINARY", s = i || t.type === "Integer" || t.type === "INTEGER", c = i ? Math.max(0, t.lb) : t.lb, l = i ? Math.min(1, t.ub) : t.ub, u = typeof t.lb == "number" && !Number.isNaN(t.lb) && t.lb !== Infinity, d = typeof t.ub == "number" && !Number.isNaN(t.ub) && t.ub !== -Infinity;
1729
+ if (!u) h({
1730
+ type: "lower_bound",
1731
+ target: "variable",
1732
+ name: t.name,
1733
+ actual: e,
1734
+ limit: t.lb,
1735
+ violation: Infinity,
1736
+ message: `Variable "${t.name}" has invalid lower bound (${String(t.lb)}).`
1737
+ });
1738
+ else if (c !== -Infinity) {
1739
+ let r = e - c, i = Math.max(0, c - e);
1740
+ m(i), i > n && h({
1741
+ type: "lower_bound",
1742
+ target: "variable",
1743
+ name: t.name,
1744
+ actual: e,
1745
+ limit: c,
1746
+ violation: i,
1747
+ signedViolation: r,
1748
+ message: `Variable "${t.name}" value (${e}) violates lower bound (${c}) by ${i}.`
1749
+ });
1750
+ }
1751
+ if (!d) h({
1752
+ type: "upper_bound",
1753
+ target: "variable",
1754
+ name: t.name,
1755
+ actual: e,
1756
+ limit: t.ub,
1757
+ violation: Infinity,
1758
+ message: `Variable "${t.name}" has invalid upper bound (${String(t.ub)}).`
1759
+ });
1760
+ else if (l !== Infinity) {
1761
+ let r = e - l, i = Math.max(0, e - l);
1762
+ m(i), i > n && h({
1763
+ type: "upper_bound",
1764
+ target: "variable",
1765
+ name: t.name,
1766
+ actual: e,
1767
+ limit: l,
1768
+ violation: i,
1769
+ signedViolation: r,
1770
+ message: `Variable "${t.name}" value (${e}) violates upper bound (${l}) by ${i}.`
1771
+ });
1772
+ }
1773
+ if (u && d && c > l && c - e <= n && e - l <= n) {
1774
+ let n = c - l;
1775
+ h({
1776
+ type: "lower_bound",
1777
+ target: "variable",
1778
+ name: t.name,
1779
+ actual: e,
1780
+ limit: c,
1781
+ violation: n,
1782
+ signedViolation: e - c,
1783
+ message: `Variable "${t.name}" has inverted bounds [${c}, ${l}].`
1784
+ });
1785
+ }
1786
+ if (s) {
1787
+ let n = Math.round(e), a = e - n, o = Math.abs(a);
1788
+ m(o), o > r && h({
1789
+ type: "integrality",
1790
+ target: "variable",
1791
+ name: t.name,
1792
+ varType: i ? "Binary" : "Integer",
1793
+ actual: e,
1794
+ nearestInteger: n,
1795
+ violation: o,
1796
+ signedViolation: a,
1797
+ message: `${i ? "Binary" : "Integer"} variable "${t.name}" value (${e}) violates integrality by ${o} (nearest integer: ${n}).`
1798
+ });
1799
+ }
1800
+ }
1801
+ for (let t of e.boolVars) {
1802
+ let e = a ? Object.prototype.hasOwnProperty.call(o, t.name) ? o[t.name] : void 0 : t.value;
1803
+ if (e == null) {
1804
+ f.push(t.name), h({
1805
+ type: "missing_value",
1806
+ target: "bool_variable",
1807
+ name: t.name,
1808
+ actual: e ?? null,
1809
+ violation: Infinity,
1810
+ message: `Boolean variable "${t.name}" has no assigned solution value.`
1811
+ });
1812
+ continue;
1813
+ }
1814
+ if (typeof e != "boolean") {
1815
+ h({
1816
+ type: "invalid_boolean_value",
1817
+ target: "bool_variable",
1818
+ name: t.name,
1819
+ actual: e,
1820
+ violation: Infinity,
1821
+ message: `Boolean variable "${t.name}" has non-boolean value: ${String(e)}.`
1822
+ });
1823
+ continue;
1824
+ }
1825
+ _.set(t, e), b++;
1826
+ }
1827
+ let C = (e) => {
1828
+ let t = e.constant === void 0 ? 0 : e.constant;
1829
+ if (typeof t != "number" || !Number.isFinite(t)) return {
1830
+ ok: !1,
1831
+ reason: `non-finite constant (${String(t)})`
1832
+ };
1833
+ let n = t;
1834
+ for (let [t, r] of e.terms) {
1835
+ if (typeof r != "number" || !Number.isFinite(r)) return {
1836
+ ok: !1,
1837
+ reason: `non-finite coefficient (${String(r)}) for variable "${t.name}"`
1838
+ };
1839
+ if (r !== 0) {
1840
+ if (!g.has(t)) return {
1841
+ ok: !1,
1842
+ reason: `missing or invalid value for variable "${t.name}"`
1843
+ };
1844
+ if (n += r * g.get(t), !Number.isFinite(n)) return {
1845
+ ok: !1,
1846
+ reason: "linear expression evaluation overflowed finite range"
1847
+ };
1848
+ }
1849
+ }
1850
+ return {
1851
+ ok: !0,
1852
+ value: n
1853
+ };
1854
+ }, w = (e, t = n) => {
1855
+ if (typeof e.rhs != "number" || !Number.isFinite(e.rhs)) return {
1856
+ status: "unchecked",
1857
+ reason: `non-finite right-hand side (${String(e.rhs)})`
1858
+ };
1859
+ let r = C(e.lhs);
1860
+ if (!r.ok) return {
1861
+ status: "unchecked",
1862
+ reason: r.reason
1863
+ };
1864
+ let i = r.value, a = i - e.rhs;
1865
+ if (!Number.isFinite(a)) return {
1866
+ status: "unchecked",
1867
+ reason: "constraint residual overflowed finite range"
1868
+ };
1869
+ let o = e.sense === "<=" ? Math.max(0, a) : e.sense === ">=" ? Math.max(0, -a) : Math.abs(a);
1870
+ return o > t ? {
1871
+ status: "violated",
1872
+ lhsVal: i,
1873
+ signed: a,
1874
+ violation: o,
1875
+ message: `Constraint "${e.name}" (${i} ${e.sense} ${e.rhs}) violated by ${o}.`
1876
+ } : {
1877
+ status: "satisfied",
1878
+ lhsVal: i,
1879
+ signed: a,
1880
+ violation: o
1881
+ };
1882
+ };
1883
+ for (let t of e.constraints) {
1884
+ let e = w(t);
1885
+ e.status === "unchecked" ? (l.push(t.name), h({
1886
+ type: "unchecked_constraint",
1887
+ target: "constraint",
1888
+ name: t.name,
1889
+ sense: t.sense,
1890
+ limit: t.rhs,
1891
+ rhs: t.rhs,
1892
+ violation: Infinity,
1893
+ message: `Constraint "${t.name}" could not be evaluated: ${e.reason}.`
1894
+ })) : e.status === "violated" ? (x++, h({
1895
+ type: "linear_constraint",
1896
+ target: "constraint",
1897
+ name: t.name,
1898
+ sense: t.sense,
1899
+ actual: e.lhsVal,
1900
+ limit: t.rhs,
1901
+ rhs: t.rhs,
1902
+ violation: e.violation,
1903
+ signedViolation: e.signed,
1904
+ message: e.message
1905
+ })) : (x++, m(e.violation));
1906
+ }
1907
+ let T = (e) => {
1908
+ if (e instanceof u) return _.has(e) ? {
1909
+ ok: !0,
1910
+ value: _.get(e),
1911
+ violation: 0
1912
+ } : {
1913
+ ok: !1,
1914
+ reason: `missing or invalid value for Boolean variable "${e.name}"`
1915
+ };
1916
+ if (e instanceof v) {
1917
+ let t = w(e, 0);
1918
+ if (t.status === "unchecked") return {
1919
+ ok: !1,
1920
+ reason: t.reason
1921
+ };
1922
+ if (n > 0) {
1923
+ let r = Math.abs(t.lhsVal - e.rhs);
1924
+ if (r > 0 && r <= n) return {
1925
+ ok: !1,
1926
+ reason: `numeric comparison "${e.name}" lies within feasibilityTolerance (${n}) of boundary (${t.lhsVal} vs ${e.rhs})`
1927
+ };
1928
+ }
1929
+ return {
1930
+ ok: !0,
1931
+ value: t.status === "satisfied",
1932
+ violation: 0
1933
+ };
1934
+ }
1935
+ if (e instanceof d) switch (e.op) {
1936
+ case "not": {
1937
+ let t = T(e.children[0]);
1938
+ return t.ok ? {
1939
+ ok: !0,
1940
+ value: !t.value,
1941
+ violation: 0
1942
+ } : t;
1943
+ }
1944
+ case "and": {
1945
+ let t = 0;
1946
+ for (let n of e.children) {
1947
+ let e = T(n);
1948
+ if (!e.ok) return e;
1949
+ if (!e.value) return {
1950
+ ok: !0,
1951
+ value: !1,
1952
+ violation: 0
1953
+ };
1954
+ e.violation > t && (t = e.violation);
1955
+ }
1956
+ return {
1957
+ ok: !0,
1958
+ value: !0,
1959
+ violation: t
1960
+ };
1961
+ }
1962
+ case "or": {
1963
+ let t = !1, n = Infinity;
1964
+ for (let r of e.children) {
1965
+ let e = T(r);
1966
+ if (!e.ok) return e;
1967
+ e.value && (t = !0, e.violation < n && (n = e.violation));
1968
+ }
1969
+ return {
1970
+ ok: !0,
1971
+ value: t,
1972
+ violation: t ? n : 0
1973
+ };
1974
+ }
1975
+ case "implies": {
1976
+ let t = T(e.children[0]);
1977
+ if (!t.ok) return t;
1978
+ if (!t.value) return {
1979
+ ok: !0,
1980
+ value: !0,
1981
+ violation: 0
1982
+ };
1983
+ let n = T(e.children[1]);
1984
+ return n.ok ? {
1985
+ ok: !0,
1986
+ value: n.value,
1987
+ violation: n.value ? n.violation : 0
1988
+ } : n;
1989
+ }
1990
+ case "xor": {
1991
+ let t = !1, n = 0;
1992
+ for (let r of e.children) {
1993
+ let e = T(r);
1994
+ if (!e.ok) return e;
1995
+ e.value && (t = !t, e.violation > n && (n = e.violation));
1996
+ }
1997
+ return {
1998
+ ok: !0,
1999
+ value: t,
2000
+ violation: t ? n : 0
2001
+ };
2002
+ }
2003
+ default: return {
2004
+ ok: !1,
2005
+ reason: `unsupported logical operator "${String(e.op)}"`
2006
+ };
2007
+ }
2008
+ return {
2009
+ ok: !1,
2010
+ reason: "unsupported logical expression node"
2011
+ };
2012
+ };
2013
+ for (let t = 0; t < e.logicalConstraints.length; t++) {
2014
+ let n = e.logicalConstraints[t], r = n.name || `lc_${t + 1}`;
2015
+ if (!i) {
2016
+ l.push(r), h({
2017
+ type: "unchecked_constraint",
2018
+ target: "logical_constraint",
2019
+ name: r,
2020
+ violation: Infinity,
2021
+ message: `Logical constraint "${r}" was not evaluated because evaluateLogicalConstraints is false.`
2022
+ });
2023
+ continue;
2024
+ }
2025
+ let a = T(n.expr);
2026
+ if (!a.ok) {
2027
+ l.push(r), h({
2028
+ type: "unchecked_constraint",
2029
+ target: "logical_constraint",
2030
+ name: r,
2031
+ violation: Infinity,
2032
+ message: `Logical constraint "${r}" could not be evaluated: ${a.reason}.`
2033
+ });
2034
+ continue;
2035
+ }
2036
+ S++, a.value ? m(a.violation) : h({
2037
+ type: "logical_constraint",
2038
+ target: "logical_constraint",
2039
+ name: r,
2040
+ actual: !1,
2041
+ limit: !0,
2042
+ violation: 1,
2043
+ signedViolation: -1,
2044
+ message: `Logical constraint "${r}" evaluated to false.`
2045
+ });
2046
+ }
2047
+ let E = y === e.variables.length && b === e.boolVars.length && x === e.constraints.length && S === e.logicalConstraints.length && l.length === 0 && f.length === 0;
2048
+ return {
2049
+ valid: E && s.length === 0,
2050
+ complete: E,
2051
+ violations: s,
2052
+ violationsByName: c,
2053
+ uncheckedConstraints: l,
2054
+ missingVariables: f,
2055
+ maxViolation: p,
2056
+ summary: {
2057
+ checkedVariables: y,
2058
+ totalVariables: e.variables.length,
2059
+ checkedBoolVariables: b,
2060
+ totalBoolVariables: e.boolVars.length,
2061
+ checkedLinearConstraints: x,
2062
+ totalLinearConstraints: e.constraints.length,
2063
+ checkedLogicalConstraints: S,
2064
+ totalLogicalConstraints: e.logicalConstraints.length
2065
+ }
2066
+ };
2067
+ }
2068
+ //#endregion
2069
+ //#region src/modeler/big-m.ts
2070
+ function Ie(e, t) {
2071
+ if (typeof e != "number" || !Number.isFinite(e) || e <= 0) throw Error(`Invalid Big-M value for indicator constraint "${t}": expected a finite positive number, received ${e}.`);
2072
+ }
2073
+ function Y(e, t, n) {
2074
+ let r = e.constant === void 0 ? 0 : e.constant;
2075
+ if (!Number.isFinite(t) || !Number.isFinite(r)) throw Error(`Cannot derive safe Big-M for indicator constraint "${n}": constraint contains non-finite constant (${r}) or right-hand side (${t}).`);
2076
+ let i = t - r;
2077
+ if (!Number.isFinite(i)) throw Error(`Cannot derive safe Big-M for indicator constraint "${n}": effective right-hand side (${t} - ${r}) overflows finite floating-point range. Please rescale coefficients/constants.`);
2078
+ for (let [t, r] of e.terms) {
2079
+ if (!Number.isFinite(r)) throw Error(`Cannot derive safe Big-M for indicator constraint "${n}": variable "${t.name}" has non-finite coefficient (${r}).`);
2080
+ if (Number.isNaN(t.lb) || Number.isNaN(t.ub) || t.lb === Infinity || t.ub === -Infinity || t.lb > t.ub) throw Error(`Cannot derive safe Big-M for indicator constraint "${n}": variable "${t.name}" has invalid bounds [${t.lb}, ${t.ub}].`);
2081
+ }
2082
+ return i;
2083
+ }
2084
+ function Le(e, t, n) {
2085
+ let r = Y(e, t, n), i = 0;
2086
+ for (let [t, r] of e.terms) {
2087
+ if (r === 0) continue;
2088
+ let e;
2089
+ if (r > 0) {
2090
+ if (!Number.isFinite(t.ub)) throw Error(`Cannot derive safe Big-M for indicator constraint "${n}": variable "${t.name}" lacks a finite upper bound (required because coefficient ${r} is positive in <= constraint). Please set an explicit finite upper bound on the variable or specify { M } explicitly in indicator options.`);
2091
+ e = t.ub;
2092
+ } else {
2093
+ if (!Number.isFinite(t.lb)) throw Error(`Cannot derive safe Big-M for indicator constraint "${n}": variable "${t.name}" lacks a finite lower bound (required because coefficient ${r} is negative in <= constraint). Please set an explicit finite lower bound on the variable or specify { M } explicitly in indicator options.`);
2094
+ e = t.lb;
2095
+ }
2096
+ let a = r * e;
2097
+ if (i += a, !Number.isFinite(a) || !Number.isFinite(i)) throw Error(`Cannot derive safe Big-M for indicator constraint "${n}": numeric overflow while bounding contribution of variable "${t.name}". Please rescale coefficients/bounds or specify a finite { M } explicitly.`);
2098
+ }
2099
+ let a = i - r;
2100
+ if (!Number.isFinite(a)) throw Error(`Cannot derive safe Big-M for indicator constraint "${n}": numeric overflow while computing Big-M (${i} - ${r}). Please rescale coefficients/bounds or specify a finite { M } explicitly.`);
2101
+ let o = Math.max(0, a);
2102
+ if (!Number.isFinite(o) || !Number.isFinite(r + o)) throw Error(`Cannot derive safe Big-M for indicator constraint "${n}": derived Big-M (${o}) or shifted right-hand side overflows finite floating-point range. Please rescale coefficients/bounds or specify a finite { M } explicitly.`);
2103
+ return o;
2104
+ }
2105
+ function Re(e, t, n) {
2106
+ let r = Y(e, t, n), i = 0;
2107
+ for (let [t, r] of e.terms) {
2108
+ if (r === 0) continue;
2109
+ let e;
2110
+ if (r > 0) {
2111
+ if (!Number.isFinite(t.lb)) throw Error(`Cannot derive safe Big-M for indicator constraint "${n}": variable "${t.name}" lacks a finite lower bound (required because coefficient ${r} is positive in >= constraint). Please set an explicit finite lower bound on the variable or specify { M } explicitly in indicator options.`);
2112
+ e = t.lb;
2113
+ } else {
2114
+ if (!Number.isFinite(t.ub)) throw Error(`Cannot derive safe Big-M for indicator constraint "${n}": variable "${t.name}" lacks a finite upper bound (required because coefficient ${r} is negative in >= constraint). Please set an explicit finite upper bound on the variable or specify { M } explicitly in indicator options.`);
2115
+ e = t.ub;
2116
+ }
2117
+ let a = r * e;
2118
+ if (i += a, !Number.isFinite(a) || !Number.isFinite(i)) throw Error(`Cannot derive safe Big-M for indicator constraint "${n}": numeric overflow while bounding contribution of variable "${t.name}". Please rescale coefficients/bounds or specify a finite { M } explicitly.`);
2119
+ }
2120
+ let a = r - i;
2121
+ if (!Number.isFinite(a)) throw Error(`Cannot derive safe Big-M for indicator constraint "${n}": numeric overflow while computing Big-M (${r} - ${i}). Please rescale coefficients/bounds or specify a finite { M } explicitly.`);
2122
+ let o = Math.max(0, a);
2123
+ if (!Number.isFinite(o) || !Number.isFinite(r - o)) throw Error(`Cannot derive safe Big-M for indicator constraint "${n}": derived Big-M (${o}) or shifted right-hand side overflows finite floating-point range. Please rescale coefficients/bounds or specify a finite { M } explicitly.`);
2124
+ return o;
2125
+ }
2126
+ function X(e, t, n, r, i, a, o) {
2127
+ if (!(e instanceof N && (e.type === "Binary" || e.type === "Integer" && e.lb === 0 && e.ub === 1))) throw Error(`Indicator variable "${e?.name ?? String(e)}" must be a Binary variable (or Integer variable with bounds [0, 1]), received "${e?.type ?? typeof e}" with bounds [${e?.lb}, ${e?.ub}].`);
2128
+ if (!Number.isFinite(e.lb) || !Number.isFinite(e.ub) || e.lb < 0 || e.ub > 1 || e.lb > e.ub) throw Error(`Binary indicator variable "${e.name}" has invalid bounds [${e.lb}, ${e.ub}]. Binary variables must stay within [0, 1] with lb <= ub.`);
2129
+ let s = Y(t, r, o);
2130
+ if (i !== 0 && i !== 1) throw Error(`Invalid activeValue: ${i}. Indicator activation value must be 0 or 1.`);
2131
+ if (n !== "<=" && n !== ">=") throw Error(`Invalid indicator sense for "${o}": expected "<=" or ">=", received "${String(n)}".`);
2132
+ if (!Number.isFinite(a) || a < 0) throw Error(`Invalid Big-M value for indicator constraint "${o}": expected a finite non-negative number, received ${a}.`);
2133
+ let c = t.terms.get(e);
2134
+ if (c !== void 0 && c !== 0) throw Error(`Indicator constraint "${o}" cannot include its own binary activation variable "${e.name}" in the linear expression.`);
2135
+ let l, u;
2136
+ if (i === 1 ? n === "<=" ? (l = a, u = s + a) : (l = -a, u = s - a) : n === "<=" ? (l = -a, u = s) : (l = a, u = s), !Number.isFinite(u)) throw Error(`Cannot construct finite indicator constraint "${o}": shifted right-hand side overflows finite floating-point range. Please rescale coefficients/bounds or specify a smaller finite { M }.`);
2137
+ let d = /* @__PURE__ */ new Map();
2138
+ for (let [e, n] of t.terms) n !== 0 && d.set(e, n);
2139
+ return l !== 0 && d.set(e, l), {
2140
+ lhs: new S(d, 0),
2141
+ rhs: u
2142
+ };
926
2143
  }
927
- re();
928
2144
  //#endregion
929
2145
  //#region src/modeler/model.ts
930
- var ie = class {
2146
+ function Z(e) {
2147
+ return Array.isArray(e) || typeof e == "object" && !!e && !(e instanceof String) && typeof e[Symbol.iterator] == "function";
2148
+ }
2149
+ function Q(e) {
2150
+ return typeof e == "object" && !!e && !Array.isArray(e) && !(e instanceof String) && typeof e[Symbol.iterator] != "function";
2151
+ }
2152
+ function $(e, t) {
2153
+ if (!Number.isInteger(e) || e < 0) throw Error(`Invalid count (${e}) for ${t} variable creation: count must be a non-negative integer.`);
2154
+ }
2155
+ var ze = class {
931
2156
  name;
932
- sense;
933
- objective;
2157
+ _sense;
2158
+ _objectiveExpr;
934
2159
  variables = [];
935
2160
  constraints = [];
936
2161
  boolVars = [];
937
2162
  logicalConstraints = [];
938
- unsatCore = [];
2163
+ unsatCore = void 0;
939
2164
  values = /* @__PURE__ */ new Map();
940
2165
  varMapByName = /* @__PURE__ */ new Map();
2166
+ varPrefixCounters = /* @__PURE__ */ new Map();
2167
+ boolVarMapByName = /* @__PURE__ */ new Map();
2168
+ boolVarPrefixCounters = /* @__PURE__ */ new Map();
941
2169
  constrMapByName = /* @__PURE__ */ new Map();
2170
+ constrPrefixCounters = /* @__PURE__ */ new Map();
2171
+ logicalConstrMapByName = /* @__PURE__ */ new Map();
2172
+ logicalConstrCounter = 0;
942
2173
  status = "NotSolved";
2174
+ terminationStatus = "NotSolved";
2175
+ hasPrimalSolution = !1;
943
2176
  objectiveValue = null;
2177
+ objectiveBound = void 0;
2178
+ relGap = void 0;
2179
+ mipGap = void 0;
2180
+ absGap = void 0;
944
2181
  runTime = 0;
945
2182
  solution = null;
946
2183
  solver = null;
2184
+ hasSolutionState = !1;
2185
+ modelRevision = 0;
2186
+ activeOperationId = 0;
2187
+ batchDepth = 0;
2188
+ batchMutated = !1;
2189
+ batchUndoActions = [];
947
2190
  constructor(e = {}, t) {
948
2191
  let n;
949
2192
  n = typeof e == "string" ? {
950
2193
  ...t,
951
2194
  name: e
952
- } : e, this.name = n.name || "OptimizationModel", this.sense = l(n.sense), this.objective = new S(), n.solver && (this.solver = H(n.solver));
2195
+ } : e, this.name = n.name || "OptimizationModel", this._sense = o(n.sense), this._objectiveExpr = new S(), n.solver && (this.solver = H(n.solver));
953
2196
  }
954
- get vars() {
955
- return this.variables;
2197
+ get sense() {
2198
+ return this._sense;
956
2199
  }
957
- get constrs() {
958
- return this.constraints;
2200
+ set sense(e) {
2201
+ let t = o(e);
2202
+ this._sense !== t && (this._sense = t, this.recordModelMutation());
2203
+ }
2204
+ get objectiveExpr() {
2205
+ return this._objectiveExpr.clone();
2206
+ }
2207
+ set objectiveExpr(e) {
2208
+ this.applyObjective(e);
2209
+ }
2210
+ get conflicts() {
2211
+ return this.unsatCore;
959
2212
  }
960
2213
  get numVars() {
961
2214
  return this.variables.length;
@@ -963,50 +2216,123 @@ var ie = class {
963
2216
  get numConstrs() {
964
2217
  return this.constraints.length;
965
2218
  }
966
- createDomainVars(e, t, n, r) {
967
- if (typeof n == "number" || Array.isArray(n)) {
968
- let i = typeof t == "number" ? Array.from({ length: t }, (e, t) => t) : t, a = typeof n == "number" ? Array.from({ length: n }, (e, t) => t) : n, o = r || {}, s = o.prefix || o.name || "x", c = {};
969
- for (let t of i) {
970
- c[t] = {};
971
- for (let n of a) c[t][n] = this.addVar({
972
- ...o,
973
- type: e,
974
- name: `${s}_${t}_${n}`
975
- });
2219
+ clearSolutionFields() {
2220
+ if (this.hasSolutionState || this.status !== "NotSolved" || this.terminationStatus !== "NotSolved" || this.hasPrimalSolution || this.solution !== null || this.unsatCore !== void 0) {
2221
+ this.hasSolutionState = !1, this.status = "NotSolved", this.terminationStatus = "NotSolved", this.hasPrimalSolution = !1, this.objectiveValue = null, this.objectiveBound = void 0, this.relGap = void 0, this.mipGap = void 0, this.absGap = void 0, this.solution = null, this.runTime = 0, this.unsatCore = void 0, this.values.clear();
2222
+ for (let e of this.variables) e.value = null, e.reducedCost = null;
2223
+ for (let e of this.boolVars) e.value = null;
2224
+ for (let e of this.constraints) e.dual = null, e.slack = null;
2225
+ }
2226
+ }
2227
+ recordModelMutation() {
2228
+ if (this.batchDepth > 0) {
2229
+ this.batchMutated = !0;
2230
+ return;
2231
+ }
2232
+ this.modelRevision++, this.clearSolutionFields();
2233
+ }
2234
+ notifyMutation() {
2235
+ this.recordModelMutation();
2236
+ }
2237
+ resetSolutionState() {
2238
+ this.recordModelMutation();
2239
+ }
2240
+ runTransactionalBatch(e) {
2241
+ if (this.batchDepth > 0) return e();
2242
+ let t = this.variables.length, n = this.boolVars.length, r = this.constraints.length, i = this.logicalConstraints.length, a = new Map(this.varPrefixCounters), o = new Map(this.boolVarPrefixCounters), s = new Map(this.constrPrefixCounters), c = this.logicalConstrCounter;
2243
+ this.batchDepth = 1, this.batchMutated = !1, this.batchUndoActions = [];
2244
+ try {
2245
+ let t = e();
2246
+ this.batchDepth = 0;
2247
+ let n = this.batchMutated;
2248
+ return this.batchMutated = !1, this.batchUndoActions = [], n && this.recordModelMutation(), t;
2249
+ } catch (e) {
2250
+ this.batchDepth = 0, this.batchMutated = !1;
2251
+ for (let e = t; e < this.variables.length; e++) {
2252
+ let t = this.variables[e];
2253
+ this.varMapByName.delete(t.name), t.ownerModel = null, te(t, this);
2254
+ }
2255
+ this.variables.length = t;
2256
+ for (let e = n; e < this.boolVars.length; e++) {
2257
+ let t = this.boolVars[e];
2258
+ this.boolVarMapByName.delete(t.name);
2259
+ }
2260
+ this.boolVars.length = n;
2261
+ for (let e = r; e < this.constraints.length; e++) {
2262
+ let t = this.constraints[e];
2263
+ this.constrMapByName.delete(t.name);
976
2264
  }
977
- return c;
2265
+ this.constraints.length = r;
2266
+ for (let e = i; e < this.logicalConstraints.length; e++) {
2267
+ let t = this.logicalConstraints[e];
2268
+ this.logicalConstrMapByName.delete(t.name);
2269
+ }
2270
+ this.logicalConstraints.length = i;
2271
+ for (let e = this.batchUndoActions.length - 1; e >= 0; e--) this.batchUndoActions[e]();
2272
+ throw this.batchUndoActions = [], this.varPrefixCounters = a, this.boolVarPrefixCounters = o, this.constrPrefixCounters = s, this.logicalConstrCounter = c, e;
978
2273
  }
979
- if (typeof t == "string") {
980
- let r = n || {};
2274
+ }
2275
+ createDomainVars(e, t, n, r) {
2276
+ let i = e.toLowerCase(), a = typeof t == "number", o = typeof n == "number", s = Z(t), c = Z(n);
2277
+ if (o || c) {
2278
+ if (a && $(t, i), o && $(n, i), !a && !s || r !== void 0 && !Q(r)) throw Error(`Invalid arguments provided for ${i} variable creation`);
2279
+ let c = a ? Array.from({ length: t }, (e, t) => t) : Array.from(t), l = o ? Array.from({ length: n }, (e, t) => t) : n === t ? c : Array.from(n), u = r || {}, d = u.prefix || u.name || "x";
2280
+ return this.runTransactionalBatch(() => {
2281
+ let t = a ? [] : {};
2282
+ for (let n of c) {
2283
+ let r = o ? [] : {};
2284
+ for (let t of l) r[t] = this.addVar({
2285
+ ...u,
2286
+ type: e,
2287
+ name: `${d}_${n}_${t}`
2288
+ });
2289
+ t[n] = r;
2290
+ }
2291
+ return t;
2292
+ });
2293
+ }
2294
+ if (typeof t == "string" || t === void 0) {
2295
+ if (n !== void 0 && !Q(n)) throw Error(`Invalid arguments provided for ${i} variable creation`);
2296
+ let r = n || {}, a = typeof t == "string" ? t : r.name;
981
2297
  return this.addVar({
982
2298
  ...r,
983
2299
  type: e,
984
- name: t
2300
+ name: a
985
2301
  });
986
2302
  }
987
- let i = n || {}, a = i.prefix || i.name;
988
- if (typeof t == "number") {
989
- let n = [], r = a || "x";
990
- for (let a = 0; a < t; a++) n.push(this.addVar({
991
- ...i,
2303
+ if (Q(t)) {
2304
+ if (n !== void 0) throw Error(`Invalid arguments provided for ${i} variable creation`);
2305
+ let r = t;
2306
+ return this.addVar({
2307
+ ...r,
2308
+ type: e
2309
+ });
2310
+ }
2311
+ if (n !== void 0 && !Q(n)) throw Error(`Invalid arguments provided for ${i} variable creation`);
2312
+ let l = n || {}, u = l.prefix || l.name;
2313
+ if (typeof t == "number") return $(t, i), this.runTransactionalBatch(() => {
2314
+ let n = [], r = u || "x";
2315
+ for (let i = 0; i < t; i++) n.push(this.addVar({
2316
+ ...l,
992
2317
  type: e,
993
- name: `${r}_${a}`
2318
+ name: void 0,
2319
+ prefix: r
994
2320
  }));
995
2321
  return n;
996
- }
997
- if (Array.isArray(t) || t && typeof t[Symbol.iterator] == "function") {
2322
+ });
2323
+ if (Z(t)) return this.runTransactionalBatch(() => {
998
2324
  let n = {};
999
2325
  for (let r of t) {
1000
- let t = a ? `${a}_${r}` : `${r}`;
2326
+ let t = u ? `${u}_${r}` : `${r}`;
1001
2327
  n[r] = this.addVar({
1002
- ...i,
2328
+ ...l,
1003
2329
  type: e,
1004
2330
  name: t
1005
2331
  });
1006
2332
  }
1007
2333
  return n;
1008
- }
1009
- throw Error(`Invalid arguments provided for ${e.toLowerCase()} variable creation`);
2334
+ });
2335
+ throw Error(`Invalid arguments provided for ${i} variable creation`);
1010
2336
  }
1011
2337
  binary(e, t, n) {
1012
2338
  return this.createDomainVars("Binary", e, t, n);
@@ -1017,89 +2343,142 @@ var ie = class {
1017
2343
  continuous(e, t, n) {
1018
2344
  return this.createDomainVars("Continuous", e, t, n);
1019
2345
  }
1020
- addVar(e, t, n, r) {
1021
- let i;
1022
- i = typeof e == "string" ? {
1023
- name: e,
1024
- type: t,
1025
- lb: n,
1026
- ub: r
1027
- } : e || {};
1028
- let a = new E(i);
1029
- if (this.varMapByName.has(a.name)) throw Error(`Variable with name '${a.name}' already exists in model`);
1030
- return this.variables.push(a), this.varMapByName.set(a.name, a), a;
1031
- }
1032
- addVars(e, t, n) {
1033
- if (typeof t == "number" || Array.isArray(t)) {
1034
- let r = typeof e == "number" ? Array.from({ length: e }, (e, t) => t) : e, i = typeof t == "number" ? Array.from({ length: t }, (e, t) => t) : t, a = n || {}, o = a.prefix || a.name || "x", s = {};
1035
- for (let e of r) {
1036
- s[e] = {};
1037
- for (let t of i) s[e][t] = this.addVar({
1038
- ...a,
1039
- name: `${o}_${e}_${t}`
1040
- });
1041
- }
1042
- return s;
2346
+ bool(e, t, n) {
2347
+ let r = typeof e == "number", i = typeof t == "number", a = Z(e), o = Z(t);
2348
+ if (i || o) {
2349
+ if (r && $(e, "boolean"), i && $(t, "boolean"), !r && !a || n !== void 0 && !Q(n)) throw Error("Invalid arguments provided for boolean variable creation");
2350
+ let o = r ? Array.from({ length: e }, (e, t) => t) : Array.from(e), s = i ? Array.from({ length: t }, (e, t) => t) : t === e ? o : Array.from(t), c = n || {}, l = c.prefix || c.name || "p";
2351
+ return this.runTransactionalBatch(() => {
2352
+ let e = r ? [] : {};
2353
+ for (let t of o) {
2354
+ let n = i ? [] : {};
2355
+ for (let e of s) n[e] = this.createBoolVar(`${l}_${t}_${e}`);
2356
+ e[t] = n;
2357
+ }
2358
+ return e;
2359
+ });
1043
2360
  }
1044
- let r = t || {}, i = r.prefix || r.name;
1045
- if (typeof e == "number") {
1046
- let t = [], n = i || "x";
1047
- for (let i = 0; i < e; i++) t.push(this.addVar({
1048
- ...r,
1049
- name: `${n}_${i}`
1050
- }));
1051
- return t;
2361
+ if (typeof e == "string" || e === void 0) {
2362
+ if (t !== void 0 && !Q(t)) throw Error("Invalid arguments provided for boolean variable creation");
2363
+ let n = t || {}, r = typeof e == "string" ? e : n.name;
2364
+ return this.createBoolVar(r, n.prefix);
2365
+ }
2366
+ if (Q(e)) {
2367
+ if (t !== void 0) throw Error("Invalid arguments provided for boolean variable creation");
2368
+ let n = e;
2369
+ return this.createBoolVar(n.name, n.prefix);
1052
2370
  }
1053
- {
2371
+ if (t !== void 0 && !Q(t)) throw Error("Invalid arguments provided for boolean variable creation");
2372
+ let s = t || {}, c = s.prefix || s.name;
2373
+ if (typeof e == "number") return $(e, "boolean"), this.runTransactionalBatch(() => {
2374
+ let t = [], n = c || "p";
2375
+ for (let r = 0; r < e; r++) t.push(this.createBoolVar(void 0, n));
2376
+ return t;
2377
+ });
2378
+ if (Z(e)) return this.runTransactionalBatch(() => {
1054
2379
  let t = {};
1055
2380
  for (let n of e) {
1056
- let e = i ? `${i}_${n}` : `${n}`;
1057
- t[n] = this.addVar({
1058
- ...r,
1059
- name: e
1060
- });
2381
+ let e = c ? `${c}_${n}` : `${n}`;
2382
+ t[n] = this.createBoolVar(e);
1061
2383
  }
1062
2384
  return t;
1063
- }
1064
- }
1065
- addVars2D(e, t, n) {
1066
- let r = {}, i = n?.name || "x";
1067
- for (let a of e) {
1068
- r[a] = {};
1069
- for (let e of t) r[a][e] = this.addVar({
1070
- ...n,
1071
- name: `${i}_${a}_${e}`
2385
+ });
2386
+ throw Error("Invalid arguments provided for boolean variable creation");
2387
+ }
2388
+ hasVarName(e) {
2389
+ return this.varMapByName.has(e) || this.boolVarMapByName.has(e);
2390
+ }
2391
+ getNextVarName(e) {
2392
+ let t = this.varPrefixCounters.get(e) ?? 0, n = `${e}_${t}`;
2393
+ for (; this.hasVarName(n);) t++, n = `${e}_${t}`;
2394
+ return this.varPrefixCounters.set(e, t + 1), n;
2395
+ }
2396
+ getNextBoolVarName(e) {
2397
+ let t = this.boolVarPrefixCounters.get(e) ?? 0, n = `${e}_${t}`;
2398
+ for (; this.hasVarName(n) || this.hasConstraintName(n);) t++, n = `${e}_${t}`;
2399
+ return this.boolVarPrefixCounters.set(e, t + 1), n;
2400
+ }
2401
+ hasConstraintName(e) {
2402
+ return this.constrMapByName.has(e) || this.logicalConstrMapByName.has(e);
2403
+ }
2404
+ hasBoolVarName(e) {
2405
+ return this.boolVarMapByName.has(e);
2406
+ }
2407
+ getNextConstrName(e = "c") {
2408
+ let t = this.constrPrefixCounters.get(e) ?? 1, n = `${e}_${t}`;
2409
+ for (; this.hasConstraintName(n) || this.hasBoolVarName(n);) t++, n = `${e}_${t}`;
2410
+ return this.constrPrefixCounters.set(e, t + 1), n;
2411
+ }
2412
+ getNextLogicalConstrName() {
2413
+ let e = this.logicalConstrCounter + 1, t = `lc_${e}`;
2414
+ for (; this.hasConstraintName(t) || this.hasBoolVarName(t);) e++, t = `lc_${e}`;
2415
+ return this.logicalConstrCounter = e, t;
2416
+ }
2417
+ createBoolVar(e, t) {
2418
+ let n = typeof e == "string" ? e.trim() : void 0, r = typeof t == "string" ? t.trim() : "", i = r.length > 0 ? r : "p", a = n && n.length > 0 ? n : this.getNextBoolVarName(i);
2419
+ if (this.hasVarName(a)) throw Error(`Variable with name '${a}' already exists in model`);
2420
+ if (this.hasConstraintName(a)) throw Error(`Boolean variable name '${a}' conflicts with an existing constraint in model`);
2421
+ let o = new u(a);
2422
+ return this.boolVars.push(o), this.boolVarMapByName.set(o.name, o), this.recordModelMutation(), o;
2423
+ }
2424
+ addVar(e) {
2425
+ let t = e || {}, n = typeof t.name == "string" ? t.name.trim() : void 0, r = n && n.length > 0 ? n : void 0, i = typeof t.prefix == "string" ? t.prefix.trim() : "", a = i.length > 0 ? i : "x", o = this.varPrefixCounters.get(a), s = r ?? this.getNextVarName(a);
2426
+ try {
2427
+ if (this.hasVarName(s)) throw Error(`Variable with name '${s}' already exists in model`);
2428
+ let e = new N({
2429
+ ...t,
2430
+ name: s,
2431
+ prefix: a
1072
2432
  });
2433
+ return e.hasExplicitName = r !== void 0, A(e), k(e, this), e.ownerModel = this, this.variables.push(e), this.varMapByName.set(e.name, e), this.recordModelMutation(), e;
2434
+ } catch (e) {
2435
+ throw r === void 0 && (o === void 0 ? this.varPrefixCounters.delete(a) : this.varPrefixCounters.set(a, o)), e;
1073
2436
  }
1074
- return r;
1075
2437
  }
1076
2438
  getVarByName(e) {
1077
2439
  return this.varMapByName.get(e);
1078
2440
  }
2441
+ getBoolVarByName(e) {
2442
+ return this.boolVarMapByName.get(e);
2443
+ }
1079
2444
  getConstraintByName(e) {
1080
2445
  return this.constrMapByName.get(e);
1081
2446
  }
1082
- setObjective(e, t) {
1083
- if (e instanceof y) throw Error("Cannot set objective to a constraint comparison (<=, >=, ==). Did you mean to use m.constrain(expr`... <= ...`)? For objectives, supply an unconstrained expression like expr`45 * ${x} + 80 * ${y}`.");
1084
- t && (this.sense = l(t)), this.objective = typeof e == "string" ? k(e, (e) => this.getVarByName(e)) : typeof e == "number" ? new S(/* @__PURE__ */ new Map(), e) : e instanceof x || "terms" in e ? e.clone() : new S(/* @__PURE__ */ new Map([[e, 1]]), 0);
1085
- }
1086
- maximize(e) {
1087
- this.setObjective(e, "Maximize");
1088
- }
1089
- minimize(e) {
1090
- this.setObjective(e, "Minimize");
1091
- }
1092
- constrain(e, t) {
1093
- if (Array.isArray(e)) {
1094
- for (let t of e) this.constrain(t);
2447
+ getLogicalConstraintByName(e) {
2448
+ return this.logicalConstrMapByName.get(e);
2449
+ }
2450
+ objective(e) {
2451
+ return e ? (this.applyObjective(e.expr, e.sense), this) : this.objectiveExpr;
2452
+ }
2453
+ applyObjective(e, t) {
2454
+ if (e instanceof v || typeof e == "object" && e && "sense" in e && "lhs" in e && "rhs" in e || typeof e == "string" && /(<=|>=|==|=<|=>|=)/.test(e)) throw Error(typeof e == "string" ? "Cannot use a comparison constraint as an objective. Did you mean to use m.constraint(expr`... <= ...`)?" : "Cannot use a Constraint as an objective. Did you mean to use m.constraint(expr`... <= ...`)? For objectives, supply an unconstrained expression like expr`45 * ${x} + 80 * ${y}`.");
2455
+ let n = t === void 0 ? this.sense : o(t), r;
2456
+ if (typeof e == "string") r = fe(e, (e) => this.getVarByName(e));
2457
+ else if (typeof e == "number") r = new S(/* @__PURE__ */ new Map(), e);
2458
+ else if (e instanceof x) r = e.clone();
2459
+ else if (e instanceof S) r = e.clone();
2460
+ else if (typeof e == "object" && e && "quadTerms" in e && e.quadTerms instanceof Map && "linearTerms" in e && e.linearTerms instanceof Map && typeof e.constant == "number") {
2461
+ let t = e;
2462
+ r = new x(t.linearTerms, t.quadTerms, t.constant);
2463
+ } else if (typeof e == "object" && e && "terms" in e && e.terms instanceof Map && typeof e.constant == "number") {
2464
+ let t = e;
2465
+ r = new S(t.terms, t.constant);
2466
+ } else if (e instanceof N || typeof e == "object" && e && "id" in e && "name" in e && "lb" in e && "ub" in e) r = new S(/* @__PURE__ */ new Map([[e, 1]]), 0);
2467
+ else throw Error("Invalid objective expression: expected a finite number, Var, LinExpr, QuadExpr, or arithmetic expression string.");
2468
+ ae(r, this.name, (e) => this.varMapByName.get(e.name) === e), this._sense = n, this._objectiveExpr = r, this.recordModelMutation();
2469
+ }
2470
+ constraint(e, t) {
2471
+ if (Array.isArray(e)) return this.runTransactionalBatch(() => {
2472
+ for (let t of e) this.constraint(t);
1095
2473
  return this;
1096
- }
1097
- if (e instanceof S) throw Error("Cannot add a linear expression without a comparison operator (<=, >=, ==) as a constraint. Use expr`... <= ...` or .le(), .ge(), .eq().");
2474
+ });
2475
+ if (e instanceof x || typeof e == "object" && e && "quadTerms" in e) throw Error("Constraint must contain a comparison operator (<=, >=, ==): cannot add a quadratic expression as a constraint.");
2476
+ if (e instanceof S || typeof e == "object" && e && "terms" in e) throw Error("Constraint must contain a comparison operator (<=, >=, ==): cannot add a linear expression without a comparison operator. Use expr`... <= ...` or .le(), .ge(), .eq().");
1098
2477
  return this.addConstraint(e, t), this;
1099
2478
  }
1100
2479
  getValues(e) {
1101
2480
  if (e == null) return null;
1102
- if (e instanceof E) return e.value;
2481
+ if (e instanceof N || e instanceof u) return e.value;
1103
2482
  if (Array.isArray(e)) return e.map((e) => this.getValues(e));
1104
2483
  if (typeof e == "object") {
1105
2484
  let t = {};
@@ -1108,168 +2487,345 @@ var ie = class {
1108
2487
  }
1109
2488
  return null;
1110
2489
  }
1111
- addBoolVar(e) {
1112
- let t = new f(e);
1113
- return this.boolVars.push(t), t;
1114
- }
1115
2490
  addConstraint(e, t) {
1116
- if (e instanceof S) throw Error("Cannot add a linear expression without a comparison operator (<=, >=, ==) as a constraint. Use expr`... <= ...` or .le(), .ge(), .eq().");
1117
- if (e instanceof d) {
1118
- let n = t || `lc_${this.logicalConstraints.length + 1}`;
1119
- return this.logicalConstraints.push({
1120
- name: n,
2491
+ if (e instanceof x || typeof e == "object" && e && "quadTerms" in e) throw Error("Constraint must contain a comparison operator (<=, >=, ==): cannot add a quadratic expression as a constraint.");
2492
+ if (e instanceof S || typeof e == "object" && e && "terms" in e) throw Error("Constraint must contain a comparison operator (<=, >=, ==): cannot add a linear expression without a comparison operator. Use expr`... <= ...` or .le(), .ge(), .eq().");
2493
+ if (e instanceof l) {
2494
+ let n = typeof t == "string" && t.trim().length > 0 ? t.trim() : void 0;
2495
+ if (n !== void 0) {
2496
+ if (this.hasConstraintName(n)) throw Error(`Constraint with name '${n}' already exists in model`);
2497
+ if (this.hasBoolVarName(n)) throw Error(`Constraint name '${n}' conflicts with an existing Boolean variable in model`);
2498
+ }
2499
+ let r = this.logicalConstrCounter, i = n ?? this.getNextLogicalConstrName();
2500
+ try {
2501
+ M(e, i, this.name, (e) => this.varMapByName.get(e.name) === e, (e) => this.boolVarMapByName.get(e.name) === e, this);
2502
+ } catch (e) {
2503
+ throw n === void 0 && (this.logicalConstrCounter = r), e;
2504
+ }
2505
+ O(e, this, this.batchDepth > 0 ? this.batchUndoActions : void 0);
2506
+ let a = {
2507
+ name: i,
1121
2508
  expr: e
1122
- }), e;
2509
+ };
2510
+ return this.logicalConstraints.push(a), this.logicalConstrMapByName.set(i, a), this.recordModelMutation(), e;
1123
2511
  }
1124
- let n;
1125
- if (typeof e == "string" ? n = A(e, (e) => this.getVarByName(e), t) : (n = e, t && (n.name = t)), this.constrMapByName.has(n.name)) throw Error(`Constraint with name '${n.name}' already exists in model`);
1126
- return this.constraints.push(n), this.constrMapByName.set(n.name, n), n;
1127
- }
1128
- addConstr(e, t) {
1129
- return this.addConstraint(e, t);
1130
- }
1131
- addConstrs(e) {
1132
- return e.map((e) => this.addConstraint(e));
1133
- }
1134
- add(e, t) {
1135
- if (Array.isArray(e)) {
1136
- for (let t of e) this.add(t);
1137
- return this;
2512
+ let n = typeof e == "string" ? me(e, (e) => this.getVarByName(e), t) : e;
2513
+ n.ownerModel && n.ownerModel !== this && (j(n, this.name, (e) => this.varMapByName.get(e.name) === e, n.name), ee(n, this));
2514
+ let r = typeof t == "string" && t.trim().length > 0 ? t.trim() : n.hasExplicitName ? n.name.trim() : void 0;
2515
+ if (r !== void 0) {
2516
+ if (this.hasConstraintName(r)) throw Error(`Constraint with name '${r}' already exists in model`);
2517
+ if (this.hasBoolVarName(r)) throw Error(`Constraint name '${r}' conflicts with an existing Boolean variable in model`);
1138
2518
  }
1139
- return e instanceof E ? (this.varMapByName.has(e.name) || (this.variables.push(e), this.varMapByName.set(e.name, e)), this) : (this.addConstraint(e, t), this);
2519
+ let i = this.constrPrefixCounters.get("c"), a = r ?? this.getNextConstrName("c"), o;
2520
+ try {
2521
+ j(n, this.name, (e) => this.varMapByName.get(e.name) === e, a), o = ee(n, this);
2522
+ } catch (e) {
2523
+ throw r === void 0 && (i === void 0 ? this.constrPrefixCounters.delete("c") : this.constrPrefixCounters.set("c", i)), e;
2524
+ }
2525
+ let s = n.name, c = n.hasExplicitName, u = n.ownerModel, d = o;
2526
+ return this.batchUndoActions.push(() => {
2527
+ n.ownerModel = u, n._assignModelName(s, c), ne(n, this, d);
2528
+ }), this.batchDepth === 0 && (this.batchUndoActions = []), n._assignModelName(a, !0), n.ownerModel = this, this.constraints.push(n), this.constrMapByName.set(n.name, n), this.recordModelMutation(), n;
1140
2529
  }
1141
2530
  indicator(e, t, n = {}) {
2531
+ if (!(e instanceof N)) throw Error("Invalid binary variable passed to indicator(). Expected a binary Var instance.");
2532
+ if (!(e.type === "Binary" || e.type === "Integer" && e.lb === 0 && e.ub === 1)) throw Error(`Invalid variable "${e.name}" passed to indicator(): variable has type "${e.type}" with bounds [${e.lb}, ${e.ub}]. Indicator activation variable must be a Binary variable (or Integer variable with bounds [0, 1]).`);
2533
+ if (t instanceof x) throw Error("Quadratic expressions or constraints are not supported in indicator constraints (only linear constraints are allowed).");
1142
2534
  if (!("sense" in t) || !("lhs" in t)) throw Error("Invalid constraint expression passed to indicator(). Expression must include a comparison operator (<=, >=, ==).");
1143
- let r = t, i = n.activeValue === void 0 ? 1 : n.activeValue, a = n.M === void 0 ? 1e5 : n.M, o = n.name || `ind_${e.name}_${r.name}`, s = [], { lhs: c, sense: l, rhs: u } = r;
1144
- if (i === 1) {
1145
- if (l === "<=" || l === "==") {
1146
- let t = this.addConstraint(c.add(e.mul(a)).le(u + a), l === "==" ? `${o}_le` : o);
1147
- s.push(t);
1148
- }
1149
- if (l === ">=" || l === "==") {
1150
- let t = this.addConstraint(c.sub(e.mul(a)).ge(u - a), l === "==" ? `${o}_ge` : o);
1151
- s.push(t);
1152
- }
1153
- } else {
1154
- if (l === "<=" || l === "==") {
1155
- let t = this.addConstraint(c.sub(e.mul(a)).le(u), l === "==" ? `${o}_le` : o);
1156
- s.push(t);
2535
+ let r = t, i = n.activeValue === void 0 ? 1 : n.activeValue;
2536
+ if (i !== 0 && i !== 1) throw Error(`Invalid activeValue: ${i}. Indicator activeValue must be 0 or 1.`);
2537
+ if (this.varMapByName.get(e.name) !== e) throw Error(`Variable "${e.name}" passed to indicator() does not belong to model "${this.name}". All variables must belong to the model by object identity.`);
2538
+ let a = typeof n.name == "string" && n.name.trim().length > 0 ? n.name.trim() : void 0, o = this.constrPrefixCounters.get("c"), { lhs: s, sense: c, rhs: l } = r, u, d, f;
2539
+ if (a !== void 0) u = a, d = c === "==" ? `${u}_le` : u, f = c === "==" ? `${u}_ge` : u;
2540
+ else if (r.hasExplicitName) u = `ind_${e.name}_${r.name.trim()}`, d = c === "==" ? `${u}_le` : u, f = c === "==" ? `${u}_ge` : u;
2541
+ else do {
2542
+ let t = this.getNextConstrName("c");
2543
+ u = `ind_${e.name}_${t}`, d = c === "==" ? `${u}_le` : u, f = c === "==" ? `${u}_ge` : u;
2544
+ } while ((c === "<=" || c === "==") && (this.hasConstraintName(d) || this.hasBoolVarName(d)) || (c === ">=" || c === "==") && (this.hasConstraintName(f) || this.hasBoolVarName(f)));
2545
+ let p = [];
2546
+ try {
2547
+ if ((c === "<=" || c === "==") && (this.hasConstraintName(d) || this.hasBoolVarName(d))) throw Error(this.hasBoolVarName(d) ? `Constraint name '${d}' conflicts with an existing Boolean variable in model` : `Constraint with name '${d}' already exists in model`);
2548
+ if ((c === ">=" || c === "==") && (this.hasConstraintName(f) || this.hasBoolVarName(f))) throw Error(this.hasBoolVarName(f) ? `Constraint name '${f}' conflicts with an existing Boolean variable in model` : `Constraint with name '${f}' already exists in model`);
2549
+ n.M !== void 0 && Ie(n.M, u);
2550
+ let t = (e) => this.varMapByName.get(e.name) === e;
2551
+ for (let [e] of s.terms) if (!t(e)) throw Error(`Variable "${e.name}" in constraint "${u}" does not belong to model "${this.name}". All variables must belong to the model by object identity.`);
2552
+ if (c === "<=" || c === "==") {
2553
+ let t = X(e, s, "<=", l, i, n.M === void 0 ? Le(s, l, d) : n.M, d);
2554
+ p.push([new v(t.lhs, "<=", t.rhs, d), d]);
1157
2555
  }
1158
- if (l === ">=" || l === "==") {
1159
- let t = this.addConstraint(c.add(e.mul(a)).ge(u), l === "==" ? `${o}_ge` : o);
1160
- s.push(t);
2556
+ if (c === ">=" || c === "==") {
2557
+ let t = X(e, s, ">=", l, i, n.M === void 0 ? Re(s, l, f) : n.M, f);
2558
+ p.push([new v(t.lhs, ">=", t.rhs, f), f]);
1161
2559
  }
2560
+ for (let [e, n] of p) j(e, this.name, t, n);
2561
+ } catch (e) {
2562
+ throw o === void 0 ? this.constrPrefixCounters.delete("c") : this.constrPrefixCounters.set("c", o), e;
1162
2563
  }
1163
- return s;
1164
- }
1165
- addIndicator(e, t, n = {}) {
1166
- return this.indicator(e, t, n);
2564
+ return this.runTransactionalBatch(() => p.map(([e, t]) => this.addConstraint(e, t)));
1167
2565
  }
1168
2566
  toLpString() {
1169
- let { lpString: e } = F({
2567
+ this.validateIntegrity();
2568
+ let { lpString: e } = R({
1170
2569
  name: this.name,
1171
2570
  sense: this.sense,
1172
- objective: this.objective,
2571
+ objective: this.objectiveExpr,
1173
2572
  constraints: this.constraints,
1174
2573
  variables: this.variables
1175
2574
  });
1176
2575
  return e;
1177
2576
  }
1178
- async optimize(e, t) {
1179
- let n, r;
1180
- if (e && typeof e == "object" && "solve" in e && typeof e.solve == "function" ? (n = e, r = t || {}) : typeof e == "function" ? (n = new e(t), r = t || {}) : typeof e == "string" ? (n = H(e, t), r = t || {}) : (r = { ...e }, r.solver ? (n = H(r.solver, r), delete r.solver) : n = this.solver || H()), this.objective instanceof x && this.objective.quadTerms.size > 0 && n.capabilities?.supportsQP === !1) throw Error(`Solver "${n.name}" does not support quadratic objectives (QP). Please use a QP-capable solver such as HiGHS or Clarabel.`);
1181
- let i = {
2577
+ validateIntegrity() {
2578
+ oe(this);
2579
+ }
2580
+ ensureExecutionValid(e, t, n) {
2581
+ if (this.modelRevision !== e) throw this.activeOperationId === t && this.clearSolutionFields(), Error(`Model "${this.name}" was modified while ${n}() was in progress; stale solver result was discarded.`);
2582
+ if (this.activeOperationId !== t) throw Error(`Stale ${n}() result discarded because a newer solve or check operation was started on model "${this.name}".`);
2583
+ }
2584
+ wrapSolveOptions(e, t, n) {
2585
+ let r = e.onProgress;
2586
+ return r ? {
2587
+ ...e,
2588
+ onProgress: (e) => {
2589
+ this.activeOperationId === n && this.modelRevision === t && r(e);
2590
+ }
2591
+ } : e;
2592
+ }
2593
+ hasPrimalFromResult(e) {
2594
+ if (typeof e.hasPrimalSolution == "boolean") return e.hasPrimalSolution;
2595
+ let t = e.status, n = e.terminationStatus ?? t, r = e.values || {}, i = e.boolValues || {};
2596
+ return t !== "Infeasible" && t !== "Unsat" && t !== "Unbounded" && t !== "Error" && t !== "NotSolved" && n !== "Infeasible" && n !== "Unsat" && n !== "Unbounded" && n !== "InfeasibleOrUnbounded" && n !== "Error" && n !== "NotSolved" && (t === "Optimal" || t === "Feasible" || t === "Sat" || n === "Optimal" || n === "Feasible" || n === "Sat" || typeof e.objectiveValue == "number" && Number.isFinite(e.objectiveValue) || Object.values(r).some((e) => typeof e == "number" && Number.isFinite(e) || typeof e == "boolean") || Object.values(i).some((e) => typeof e == "boolean"));
2597
+ }
2598
+ resolveSolverAndOptions(e, t) {
2599
+ if (e && typeof e == "object" && "solve" in e && typeof e.solve == "function") {
2600
+ let { solver: n, ...r } = t || {};
2601
+ return {
2602
+ activeSolver: e,
2603
+ activeOptions: r
2604
+ };
2605
+ }
2606
+ if (typeof e == "function") {
2607
+ let { solver: n, ...r } = t || {};
2608
+ return {
2609
+ activeSolver: new e(r),
2610
+ activeOptions: r
2611
+ };
2612
+ }
2613
+ if (typeof e == "string") {
2614
+ let { solver: n, ...r } = t || {};
2615
+ return {
2616
+ activeSolver: H(e, r),
2617
+ activeOptions: r
2618
+ };
2619
+ }
2620
+ let { solver: n, ...r } = e || {};
2621
+ return n ? {
2622
+ activeSolver: H(n, r),
2623
+ activeOptions: r
2624
+ } : {
2625
+ activeSolver: this.solver || H(),
2626
+ activeOptions: r
2627
+ };
2628
+ }
2629
+ async solve(e, t) {
2630
+ let n = ++this.activeOperationId, r = this.modelRevision;
2631
+ this.clearSolutionFields(), this.validateIntegrity();
2632
+ let { activeSolver: i, activeOptions: a } = this.resolveSolverAndOptions(e, t);
2633
+ if (this._objectiveExpr instanceof x && this._objectiveExpr.quadTerms.size > 0 && i.capabilities?.supportsQP === !1) throw Error(`Solver "${i.name}" does not support quadratic objectives (QP). Please use a QP-capable solver such as HiGHS or Clarabel.`);
2634
+ if (this.logicalConstraints.length > 0 && i.capabilities?.supportsSMT !== !0) throw Error(`Solver "${i.name}" does not support SMT / logical constraints (or, implies, not, xor). Please use an SMT-capable solver such as Z3.`);
2635
+ if (this.variables.some((e) => e.initialValue !== void 0) && i.capabilities?.supportsWarmStart !== !0) throw Error(`Solver "${i.name}" does not support primal initial values (initialValue). Please use a solver with warm-start support such as HiGHS.`);
2636
+ let o = {
1182
2637
  name: this.name,
1183
2638
  sense: this.sense,
1184
- objective: this.objective,
2639
+ objective: this._objectiveExpr,
1185
2640
  constraints: this.constraints,
1186
2641
  variables: this.variables,
1187
2642
  logicalConstraints: this.logicalConstraints,
1188
2643
  boolVars: this.boolVars
1189
- }, a = F(i), o = await n.solve(i, a, r);
1190
- this.runTime = o.runTime ?? 0, this.status = o.status, this.objectiveValue = o.objectiveValue ?? null;
1191
- let s = {}, c = {}, l = {}, u = {};
1192
- this.values.clear();
1193
- for (let e of this.variables) e.value = null, e.reducedCost = null;
1194
- for (let e of this.boolVars) e.value = null;
1195
- for (let e of this.constraints) e.dual = null, e.slack = null;
1196
- if (a?.nameToVar) for (let e of a.nameToVar.values()) e.value = null, e.reducedCost = null;
1197
- if (a?.nameToConstraint) for (let e of a.nameToConstraint.values()) e.dual = null, e.slack = null;
1198
- if (o.values && o.status !== "Infeasible" && o.status !== "Unbounded") {
1199
- for (let [e, t] of Object.entries(o.values)) if (typeof t == "number" && !Number.isNaN(t)) {
1200
- this.values.set(e, t);
1201
- let n = this.getVarByName(e) || a.nameToVar.get(e);
1202
- n && (n.value = t, s[n.name] = t);
1203
- let r = this.boolVars.find((t) => t.name === e);
1204
- r && (r.value = !!t);
2644
+ }, s = this.wrapSolveOptions(a, r, n), c;
2645
+ try {
2646
+ c = await i.solve(o, s);
2647
+ } catch (e) {
2648
+ throw this.activeOperationId === n && this.clearSolutionFields(), e;
2649
+ }
2650
+ this.ensureExecutionValid(r, n, "solve"), this.runTime = c.runTime ?? 0, this.status = c.status, this.terminationStatus = c.terminationStatus ?? c.status;
2651
+ let l = this.hasPrimalFromResult(c);
2652
+ this.hasPrimalSolution = l, this.objectiveValue = l && typeof c.objectiveValue == "number" && Number.isFinite(c.objectiveValue) ? c.objectiveValue : null, this.objectiveBound = typeof c.objectiveBound == "number" && Number.isFinite(c.objectiveBound) ? c.objectiveBound : void 0;
2653
+ let u = typeof c.relGap == "number" && Number.isFinite(c.relGap) ? c.relGap : typeof c.mipGap == "number" && Number.isFinite(c.mipGap) ? c.mipGap : void 0;
2654
+ this.relGap = u, this.mipGap = u, this.absGap = typeof c.absGap == "number" && Number.isFinite(c.absGap) ? c.absGap : this.objectiveValue !== null && this.objectiveBound !== void 0 ? Math.abs(this.objectiveValue - this.objectiveBound) : void 0;
2655
+ let d = (e, t, n) => {
2656
+ Object.defineProperty(e, t, {
2657
+ value: n,
2658
+ enumerable: !0,
2659
+ writable: !0,
2660
+ configurable: !0
2661
+ });
2662
+ }, f = {}, p = {}, m = {}, h = {};
2663
+ if (this.values.clear(), l && c.values) {
2664
+ for (let [e, t] of Object.entries(c.values)) if (typeof t == "number" && Number.isFinite(t)) {
2665
+ let n = this.getVarByName(e), r = this.getBoolVarByName(e);
2666
+ n && (n.value = t, d(f, n.name, t), this.values.set(n.name, t)), r && (r.value = !!t, this.values.set(r.name, t)), !n && !r && this.values.set(e, t);
2667
+ } else if (typeof t == "boolean") {
2668
+ let n = t, r = this.getBoolVarByName(e);
2669
+ r ? (r.value = n, d(f, r.name, +!!n), this.values.set(r.name, n)) : this.values.set(e, n);
1205
2670
  }
1206
2671
  }
1207
- if (o.reducedCosts) for (let [e, t] of Object.entries(o.reducedCosts)) {
1208
- let n = this.getVarByName(e) || a.nameToVar.get(e);
1209
- n && (n.reducedCost = t, l[n.name] = t);
2672
+ if (l && c.boolValues) for (let [e, t] of Object.entries(c.boolValues)) {
2673
+ if (typeof t != "boolean") continue;
2674
+ let n = this.getBoolVarByName(e);
2675
+ n ? (n.value = t, this.values.has(n.name) || this.values.set(n.name, t)) : this.values.has(e) || this.values.set(e, t);
2676
+ }
2677
+ if (l && c.reducedCosts) for (let [e, t] of Object.entries(c.reducedCosts)) {
2678
+ if (typeof t != "number" || !Number.isFinite(t)) continue;
2679
+ let n = this.getVarByName(e);
2680
+ n && (n.reducedCost = t, d(m, n.name, t));
1210
2681
  }
1211
- if (o.duals) for (let [e, t] of Object.entries(o.duals)) {
1212
- let n = this.getConstraintByName(e) || a.nameToConstraint.get(e);
1213
- n && (n.dual = t, c[n.name] = t);
2682
+ if (c.duals) for (let [e, t] of Object.entries(c.duals)) {
2683
+ if (typeof t != "number" || !Number.isFinite(t)) continue;
2684
+ let n = this.getConstraintByName(e);
2685
+ n && (n.dual = t, d(p, n.name, t));
1214
2686
  }
1215
- if (o.slacks) for (let [e, t] of Object.entries(o.slacks)) {
1216
- let n = this.getConstraintByName(e) || a.nameToConstraint.get(e);
1217
- n && (n.slack = t, u[n.name] = t);
2687
+ if (c.slacks) for (let [e, t] of Object.entries(c.slacks)) {
2688
+ if (typeof t != "number" || !Number.isFinite(t)) continue;
2689
+ let n = this.getConstraintByName(e);
2690
+ n && (n.slack = t, d(h, n.name, t));
1218
2691
  }
1219
2692
  return this.solution = {
1220
2693
  status: this.status,
2694
+ terminationStatus: this.terminationStatus,
2695
+ hasPrimalSolution: this.hasPrimalSolution,
1221
2696
  objectiveValue: this.objectiveValue,
1222
- values: s,
1223
- duals: c,
1224
- reducedCosts: l,
1225
- slacks: u,
2697
+ ...this.objectiveBound === void 0 ? {} : { objectiveBound: this.objectiveBound },
2698
+ ...this.relGap === void 0 ? {} : {
2699
+ relGap: this.relGap,
2700
+ mipGap: this.mipGap
2701
+ },
2702
+ ...this.absGap === void 0 ? {} : { absGap: this.absGap },
2703
+ values: f,
2704
+ duals: p,
2705
+ reducedCosts: m,
2706
+ slacks: h,
1226
2707
  runTime: this.runTime,
1227
- raw: o.raw
1228
- }, this.status;
1229
- }
1230
- async solve(e, t) {
1231
- return this.optimize(e, t);
2708
+ raw: c.raw
2709
+ }, this.hasSolutionState = !0, this.status;
1232
2710
  }
1233
2711
  async check(e, t) {
1234
- let n, r;
1235
- if (e && typeof e == "object" && "solve" in e && typeof e.solve == "function" ? (n = e, r = t || {}) : typeof e == "function" ? (n = new e(t), r = t || {}) : typeof e == "string" ? (n = H(e, t), r = t || {}) : (r = { ...e }, r.solver ? (n = H(r.solver, r), delete r.solver) : n = this.solver || H()), this.logicalConstraints.length > 0 && n.capabilities?.supportsSMT !== !0) throw Error(`Solver "${n.name}" does not support SMT / logical constraints (or, implies, not, xor). Please use an SMT-capable solver such as Z3.`);
1236
- if (typeof n.check == "function") {
1237
- let e = await n.check({
1238
- name: this.name,
1239
- variables: this.variables,
1240
- boolVars: this.boolVars,
1241
- constraints: this.constraints,
1242
- logicalConstraints: this.logicalConstraints
1243
- }, r);
1244
- this.status = e.status, this.runTime = e.runTime ?? 0, this.unsatCore = e.unsatCore || [], this.values.clear();
1245
- for (let e of this.variables) e.value = null;
1246
- for (let e of this.boolVars) e.value = null;
1247
- if (e.values && e.status === "Sat") for (let [t, n] of Object.entries(e.values)) {
1248
- this.values.set(t, n);
1249
- let e = this.getVarByName(t);
1250
- e && typeof n == "number" && (e.value = n);
1251
- let r = this.boolVars.find((e) => e.name === t);
1252
- r && (r.value = !!n);
2712
+ let n = ++this.activeOperationId, r = this.modelRevision;
2713
+ this.clearSolutionFields(), this.validateIntegrity();
2714
+ let { activeSolver: i, activeOptions: a } = this.resolveSolverAndOptions(e, t);
2715
+ if (this.logicalConstraints.length > 0 && i.capabilities?.supportsSMT !== !0) throw Error(`Solver "${i.name}" does not support SMT / logical constraints (or, implies, not, xor). Please use an SMT-capable solver such as Z3.`);
2716
+ if (this.variables.some((e) => e.initialValue !== void 0) && i.capabilities?.supportsWarmStart !== !0) throw Error(`Solver "${i.name}" does not support primal initial values (initialValue). Please use a solver with warm-start support such as HiGHS.`);
2717
+ let o = this.wrapSolveOptions(a, r, n), s = (e, t, n) => {
2718
+ Object.defineProperty(e, t, {
2719
+ value: n,
2720
+ enumerable: !0,
2721
+ writable: !0,
2722
+ configurable: !0
2723
+ });
2724
+ }, c = {
2725
+ name: this.name,
2726
+ variables: this.variables,
2727
+ boolVars: this.boolVars,
2728
+ constraints: this.constraints,
2729
+ logicalConstraints: this.logicalConstraints
2730
+ };
2731
+ if (typeof i.check == "function") {
2732
+ let e;
2733
+ try {
2734
+ e = await i.check(c, o);
2735
+ } catch (e) {
2736
+ throw this.activeOperationId === n && this.clearSolutionFields(), e;
1253
2737
  }
1254
- return e;
1255
- }
1256
- let i = this.objective, a = this.sense;
1257
- try {
1258
- this.objective = new S(), this.sense = "Minimize";
1259
- let e = await this.optimize(n, r), t = e === "Optimal" || e === "Feasible", i = e === "Infeasible", a = {};
1260
- if (t) {
1261
- for (let e of this.variables) e.value !== null && (a[e.name] = e.value);
1262
- for (let e of this.boolVars) e.value !== null && (a[e.name] = e.value);
2738
+ this.ensureExecutionValid(r, n, "check"), this.status = e.status, this.terminationStatus = a.signal?.aborted ? "Interrupted" : e.status, this.hasPrimalSolution = e.status === "Sat", this.runTime = e.runTime ?? 0, this.unsatCore = e.unsatCore === void 0 ? void 0 : Object.freeze([...e.unsatCore]);
2739
+ let t = {};
2740
+ if (e.values && e.status === "Sat") {
2741
+ for (let [n, r] of Object.entries(e.values)) if (typeof r == "number") {
2742
+ if (!Number.isFinite(r)) continue;
2743
+ let e = this.getVarByName(n), i = this.getBoolVarByName(n);
2744
+ e && (e.value = r, this.values.set(e.name, r), s(t, e.name, r)), i && (i.value = !!r, this.values.set(i.name, i.value), s(t, i.name, i.value)), !e && !i && (this.values.set(n, r), s(t, n, r));
2745
+ } else if (typeof r == "boolean") {
2746
+ let e = this.getBoolVarByName(n);
2747
+ e ? (e.value = r, this.values.set(e.name, r), s(t, e.name, r)) : (this.values.set(n, r), s(t, n, r));
2748
+ }
1263
2749
  }
1264
- return {
1265
- status: t ? "Sat" : i ? "Unsat" : "Unknown",
1266
- values: a,
2750
+ return this.hasSolutionState = !0, {
2751
+ status: e.status,
2752
+ values: t,
2753
+ unsatCore: this.unsatCore,
2754
+ conflicts: this.unsatCore,
1267
2755
  runTime: this.runTime
1268
2756
  };
1269
- } finally {
1270
- this.objective = i, this.sense = a;
1271
2757
  }
2758
+ let l = {
2759
+ name: this.name,
2760
+ sense: "Minimize",
2761
+ objective: new S(),
2762
+ constraints: this.constraints,
2763
+ variables: this.variables,
2764
+ logicalConstraints: this.logicalConstraints,
2765
+ boolVars: this.boolVars
2766
+ }, u;
2767
+ try {
2768
+ u = await i.solve(l, o);
2769
+ } catch (e) {
2770
+ throw this.activeOperationId === n && this.clearSolutionFields(), e;
2771
+ }
2772
+ this.ensureExecutionValid(r, n, "check"), this.runTime = u.runTime ?? 0;
2773
+ let d = u.status, f = u.terminationStatus ?? d, p = this.hasPrimalFromResult(u) || d === "Optimal" || d === "Feasible", m = p ? "Sat" : d === "Infeasible" ? "Unsat" : "Unknown";
2774
+ this.status = m, this.terminationStatus = a.signal?.aborted || f === "Interrupted" ? "Interrupted" : f === "Optimal" || f === "Feasible" || f === "Infeasible" ? m : f, this.hasPrimalSolution = p, this.objectiveValue = null, this.objectiveBound = void 0, this.relGap = void 0, this.mipGap = void 0, this.absGap = void 0, this.solution = null, this.values.clear();
2775
+ let h = {};
2776
+ if (p && u.values) {
2777
+ for (let [e, t] of Object.entries(u.values)) if (typeof t == "number" && Number.isFinite(t)) {
2778
+ let n = this.getVarByName(e), r = this.getBoolVarByName(e);
2779
+ n && (n.value = t, this.values.set(n.name, t), s(h, n.name, t)), r && (r.value = !!t, this.values.set(r.name, r.value), s(h, r.name, r.value)), !n && !r && (this.values.set(e, t), s(h, e, t));
2780
+ } else if (typeof t == "boolean") {
2781
+ let n = t, r = this.getBoolVarByName(e);
2782
+ r ? (r.value = n, this.values.set(r.name, n), s(h, r.name, n)) : (this.values.set(e, n), s(h, e, n));
2783
+ }
2784
+ }
2785
+ if (p && u.boolValues) for (let [e, t] of Object.entries(u.boolValues)) {
2786
+ if (typeof t != "boolean") continue;
2787
+ let n = this.getBoolVarByName(e);
2788
+ n ? (n.value = t, this.values.set(n.name, t), s(h, n.name, t)) : (this.values.set(e, t), s(h, e, t));
2789
+ }
2790
+ if (u.slacks) for (let [e, t] of Object.entries(u.slacks)) {
2791
+ if (typeof t != "number" || !Number.isFinite(t)) continue;
2792
+ let n = this.getConstraintByName(e);
2793
+ n && (n.slack = t);
2794
+ }
2795
+ return this.hasSolutionState = !0, {
2796
+ status: m,
2797
+ values: h,
2798
+ runTime: this.runTime
2799
+ };
2800
+ }
2801
+ validateSolution(e = {}) {
2802
+ return Fe(this, e);
1272
2803
  }
1273
2804
  };
1274
2805
  //#endregion
1275
- export { d as BoolExpr, p as BoolOp, f as BoolVar, y as Constraint, Q as HiGHS, Z as HighsSolver, S as LinExpr, $ as MockSolver, ie as Model, c as OFFICIAL_HIGHS_CDN_WASM_URL, x as QuadExpr, E as Var, m as and, M as c, w as dot, N as expr, F as generateLpString, i as getHighs, o as getHighsWasmUrl, H as getSolver, _ as implies, n as isValidWasmHeader, U as listAvailableSolvers, l as normalizeSense, g as not, h as or, A as parseConstraint, k as parseExpr, e as probeWasmUrl, B as registerSolver, a as resolveBrowserWasmUrl, V as setDefaultSolver, s as setHighsWasmUrl, t as solveLpWithHighs, C as sum, v as xor };
2806
+ //#region src/modeler/objective.ts
2807
+ function Be(e, t) {
2808
+ let n = e;
2809
+ if (n instanceof v || typeof n == "object" && n && "sense" in n && "lhs" in n && "rhs" in n) throw Error(`Cannot use a Constraint as an objective in ${t}(). Use model.constraint(expr\`... <= ...\`) for constraints, or supply an unconstrained arithmetic expression to ${t}().`);
2810
+ if (typeof n == "string" && /(<=|>=|==|=<|=>|=)/.test(n)) throw Error(`Cannot use a comparison constraint as an objective in ${t}(). Use model.constraint(expr\`... <= ...\`) for constraints, or supply an unconstrained arithmetic expression to ${t}().`);
2811
+ if (typeof n == "number") {
2812
+ if (!Number.isFinite(n)) throw Error(`Invalid non-finite objective constant "${n}" passed to ${t}().`);
2813
+ return n;
2814
+ }
2815
+ if (typeof n == "string" || n instanceof S || n instanceof x || n instanceof N || typeof n == "object" && n && ("terms" in n && n.terms instanceof Map && typeof n.constant == "number" || "quadTerms" in n && n.quadTerms instanceof Map && "linearTerms" in n && n.linearTerms instanceof Map && typeof n.constant == "number" || "id" in n && "name" in n && "lb" in n && "ub" in n)) return n;
2816
+ throw Error(`Invalid expression passed to ${t}(): expected a finite number, Var, LinExpr, QuadExpr, or arithmetic expression string.`);
2817
+ }
2818
+ function Ve(e) {
2819
+ return {
2820
+ expr: Be(e, "maximize"),
2821
+ sense: "Maximize"
2822
+ };
2823
+ }
2824
+ function He(e) {
2825
+ return {
2826
+ expr: Be(e, "minimize"),
2827
+ sense: "Minimize"
2828
+ };
2829
+ }
2830
+ //#endregion
2831
+ export { l as BoolExpr, d as BoolOp, u as BoolVar, v as Constraint, Ae as HiGHS, J as HighsSolver, S as LinExpr, Me as MockSolver, ze as Model, x as QuadExpr, N as Var, f as and, X as buildIndicatorLinearization, ye as buildLpSymbolMaps, Re as deriveBigMForGe, Le as deriveBigMForLe, w as dot, ve as expr, R as generateLpString, a as getHighs, n as getHighsWasmUrl, H as getSolver, h as implies, I as isLpExportResult, U as listAvailableSolvers, Ve as maximize, He as minimize, o as normalizeSense, m as not, p as or, me as parseConstraint, fe as parseExpr, V as registerSolver, L as sanitizeIdentifier, Se as setDefaultSolver, t as setHighsWasmUrl, e as solveLpWithHighs, C as sum, oe as validateModelIntegrity, Fe as validateModelSolution, Ie as validateUserBigM, g as xor };