@ohtaman/washi 0.7.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. package/README.ja.md +60 -23
  2. package/README.md +59 -22
  3. package/README.zh-CN.md +62 -25
  4. package/THIRD_PARTY_LICENSES.md +34 -1
  5. package/dist/browser/solvers/glpk.js +289 -238
  6. package/dist/browser/solvers/glpk.min.js +1 -1
  7. package/dist/browser/solvers/highs-__vite-browser-external-Dn-83hnF.js +7 -0
  8. package/dist/browser/solvers/highs-browser-runtime-0Bgq5ksG.js +2021 -0
  9. package/dist/browser/{loader-DewlSho8.js → solvers/highs-loader-BLY3wzDH.js} +190 -149
  10. package/dist/browser/solvers/highs-worker.js +54 -0
  11. package/dist/browser/solvers/highs.js +1414 -0
  12. package/dist/browser/solvers/highs.wasm +0 -0
  13. package/dist/browser/solvers/pumpkin-_washi-pumpkin-node-runtime-CEKUDK6d.js +9 -0
  14. package/dist/browser/solvers/pumpkin-loader-CvdaKxf4.js +320 -0
  15. package/dist/browser/solvers/pumpkin-worker.js +45 -0
  16. package/dist/browser/solvers/pumpkin.js +1777 -0
  17. package/dist/browser/solvers/pumpkin.wasm +0 -0
  18. package/dist/browser/solvers/pumpkin.wasm.LICENSE +21 -0
  19. package/dist/browser/solvers/z3.js +1600 -1335
  20. package/dist/browser/solvers/z3.min.js +2 -2
  21. package/dist/browser/washi.js +3313 -1699
  22. package/dist/browser/washi.min.js +5 -3
  23. package/dist/lib/solvers/__vite-browser-external-oLchLuMx.js +7 -0
  24. package/dist/lib/solvers/glpk.js +174 -132
  25. package/dist/lib/solvers/highs-browser-runtime.js +2413 -0
  26. package/dist/lib/{highs-node-runtime.js → solvers/highs-node-runtime.js} +1 -1
  27. package/dist/lib/solvers/highs-runtime.js +16 -0
  28. package/dist/lib/solvers/highs-worker.js +54 -0
  29. package/dist/lib/solvers/highs.js +410 -0
  30. package/dist/lib/solvers/highs.wasm +0 -0
  31. package/dist/lib/{loader-BZUQNvh4.js → solvers/loader-CDpPXN_F.js} +138 -116
  32. package/dist/lib/solvers/loader-Ow74Fa1B.js +311 -0
  33. package/dist/lib/solvers/lp-writer-CLScMFbS.js +115 -0
  34. package/dist/lib/solvers/pumpkin-assets.js +1 -0
  35. package/dist/lib/solvers/pumpkin-node-runtime.js +15 -0
  36. package/dist/lib/solvers/pumpkin-worker-factory.js +1 -0
  37. package/dist/lib/solvers/pumpkin-worker.js +45 -0
  38. package/dist/lib/solvers/pumpkin.js +868 -0
  39. package/dist/lib/solvers/pumpkin.wasm +0 -0
  40. package/dist/lib/solvers/pumpkin.wasm.LICENSE +21 -0
  41. package/dist/lib/solvers/registry-CrYRRk9q.js +907 -0
  42. package/dist/lib/solvers/rolldown-runtime-DtPi1Y-2.js +13 -0
  43. package/dist/lib/solvers/types-BGqPkxwu.js +12 -0
  44. package/dist/lib/solvers/z3.js +489 -307
  45. package/dist/lib/washi.js +3905 -1410
  46. package/dist/types/decomposition/benders.d.ts.map +1 -1
  47. package/dist/types/index.d.ts +4 -5
  48. package/dist/types/index.d.ts.map +1 -1
  49. package/dist/types/modeler/boolean-expr.d.ts +137 -29
  50. package/dist/types/modeler/boolean-expr.d.ts.map +1 -1
  51. package/dist/types/modeler/constraint.d.ts +40 -4
  52. package/dist/types/modeler/constraint.d.ts.map +1 -1
  53. package/dist/types/modeler/expression.d.ts +129 -2
  54. package/dist/types/modeler/expression.d.ts.map +1 -1
  55. package/dist/types/modeler/index.d.ts +4 -6
  56. package/dist/types/modeler/index.d.ts.map +1 -1
  57. package/dist/types/modeler/linearizer.d.ts +11 -2
  58. package/dist/types/modeler/linearizer.d.ts.map +1 -1
  59. package/dist/types/modeler/lp-writer.d.ts.map +1 -1
  60. package/dist/types/modeler/model.d.ts +86 -126
  61. package/dist/types/modeler/model.d.ts.map +1 -1
  62. package/dist/types/modeler/parser.d.ts.map +1 -1
  63. package/dist/types/modeler/quad-expr.d.ts +8 -2
  64. package/dist/types/modeler/quad-expr.d.ts.map +1 -1
  65. package/dist/types/modeler/solution-validator.d.ts +12 -12
  66. package/dist/types/modeler/solution-validator.d.ts.map +1 -1
  67. package/dist/types/modeler/solvers/glpk/loader.d.ts +4 -0
  68. package/dist/types/modeler/solvers/glpk/loader.d.ts.map +1 -1
  69. package/dist/types/modeler/solvers/glpk/solver.d.ts +2 -2
  70. package/dist/types/modeler/solvers/glpk/solver.d.ts.map +1 -1
  71. package/dist/types/modeler/solvers/highs/driver.d.ts.map +1 -1
  72. package/dist/types/modeler/solvers/highs/loader.d.ts +8 -6
  73. package/dist/types/modeler/solvers/highs/loader.d.ts.map +1 -1
  74. package/dist/types/modeler/solvers/highs/solver.d.ts.map +1 -1
  75. package/dist/types/modeler/solvers/highs/worker-client.d.ts +16 -0
  76. package/dist/types/modeler/solvers/highs/worker-client.d.ts.map +1 -1
  77. package/dist/types/modeler/solvers/index.d.ts +2 -1
  78. package/dist/types/modeler/solvers/index.d.ts.map +1 -1
  79. package/dist/types/modeler/solvers/mock.d.ts +2 -3
  80. package/dist/types/modeler/solvers/mock.d.ts.map +1 -1
  81. package/dist/types/modeler/solvers/pumpkin/assets.d.ts +2 -0
  82. package/dist/types/modeler/solvers/pumpkin/assets.d.ts.map +1 -0
  83. package/dist/types/modeler/solvers/pumpkin/index.d.ts +6 -0
  84. package/dist/types/modeler/solvers/pumpkin/index.d.ts.map +1 -0
  85. package/dist/types/modeler/solvers/pumpkin/loader.d.ts +40 -0
  86. package/dist/types/modeler/solvers/pumpkin/loader.d.ts.map +1 -0
  87. package/dist/types/modeler/solvers/pumpkin/node-runtime.d.ts +3 -0
  88. package/dist/types/modeler/solvers/pumpkin/node-runtime.d.ts.map +1 -0
  89. package/dist/types/modeler/solvers/pumpkin/solver.d.ts +23 -0
  90. package/dist/types/modeler/solvers/pumpkin/solver.d.ts.map +1 -0
  91. package/dist/types/modeler/solvers/pumpkin/worker-client.d.ts +74 -0
  92. package/dist/types/modeler/solvers/pumpkin/worker-client.d.ts.map +1 -0
  93. package/dist/types/modeler/solvers/pumpkin/worker-factory.d.ts +3 -0
  94. package/dist/types/modeler/solvers/pumpkin/worker-factory.d.ts.map +1 -0
  95. package/dist/types/modeler/solvers/pumpkin/worker.d.ts +7 -0
  96. package/dist/types/modeler/solvers/pumpkin/worker.d.ts.map +1 -0
  97. package/dist/types/modeler/solvers/pumpkin.d.ts +2 -0
  98. package/dist/types/modeler/solvers/pumpkin.d.ts.map +1 -0
  99. package/dist/types/modeler/solvers/types.d.ts +7 -9
  100. package/dist/types/modeler/solvers/types.d.ts.map +1 -1
  101. package/dist/types/modeler/solvers/z3/loader.d.ts +4 -0
  102. package/dist/types/modeler/solvers/z3/loader.d.ts.map +1 -1
  103. package/dist/types/modeler/solvers/z3/solver.d.ts +2 -15
  104. package/dist/types/modeler/solvers/z3/solver.d.ts.map +1 -1
  105. package/dist/types/modeler/types.d.ts +12 -15
  106. package/dist/types/modeler/types.d.ts.map +1 -1
  107. package/dist/types/modeler/validation.d.ts +26 -2
  108. package/dist/types/modeler/validation.d.ts.map +1 -1
  109. package/dist/types/modeler/variable.d.ts +9 -5
  110. package/dist/types/modeler/variable.d.ts.map +1 -1
  111. package/docs/examples/diet-sensitivity.md +33 -14
  112. package/docs/examples/ecommerce-coupons.md +17 -9
  113. package/docs/examples/index.md +36 -27
  114. package/docs/examples/knapsack.md +11 -5
  115. package/docs/examples/portfolio-qp.md +21 -8
  116. package/docs/examples/production.md +10 -5
  117. package/docs/examples/project-scheduling.md +119 -0
  118. package/docs/examples/shift-scheduling.md +29 -18
  119. package/docs/examples/smt-security.md +27 -11
  120. package/docs/examples/transportation.md +23 -13
  121. package/docs/examples/traveling-salesperson.md +34 -22
  122. package/docs/examples/unsat-core.md +27 -14
  123. package/docs/examples/vehicle-routing.md +74 -28
  124. package/docs/getting-started/ai-agents.md +190 -82
  125. package/docs/getting-started/coding-agents.md +177 -0
  126. package/docs/getting-started/cross-origin.md +27 -14
  127. package/docs/getting-started/index.md +49 -43
  128. package/docs/getting-started/runtimes.md +94 -40
  129. package/docs/guide/constraint-programming.md +171 -0
  130. package/docs/guide/constraints.md +115 -33
  131. package/docs/guide/decomposition.md +118 -88
  132. package/docs/guide/expressions.md +106 -43
  133. package/docs/guide/formulation-tips.md +47 -28
  134. package/docs/guide/objectives.md +102 -75
  135. package/docs/guide/unsat-core.md +62 -50
  136. package/docs/guide/variables.md +81 -35
  137. package/docs/solvers/custom-solver.md +163 -122
  138. package/docs/solvers/glpk.md +58 -17
  139. package/docs/solvers/highs.md +124 -57
  140. package/docs/solvers/index.md +33 -20
  141. package/docs/solvers/pumpkin.md +171 -0
  142. package/docs/solvers/z3.md +63 -25
  143. package/llms-full.txt +2232 -932
  144. package/llms.txt +144 -91
  145. package/package.json +55 -19
  146. package/skills/washi/SKILL.md +107 -34
  147. package/dist/browser/_washi-node-only-BvRk9kiK.js +0 -0
  148. package/dist/browser/browser-runtime-TLCY0hkc.js +0 -1793
  149. package/dist/browser/highs-worker.js +0 -39
  150. package/dist/browser/highs.wasm +0 -0
  151. package/dist/lib/_washi-node-only-BvRk9kiK.js +0 -0
  152. package/dist/lib/highs-browser-runtime.js +0 -2185
  153. package/dist/lib/highs-runtime.js +0 -2
  154. package/dist/lib/highs-worker.js +0 -39
  155. package/dist/lib/highs.wasm +0 -0
  156. package/dist/lib/registry-D7TZ3Wh_.js +0 -1034
  157. package/docs/ja/getting-started/ai-agents.md +0 -280
  158. package/docs/zh/getting-started/ai-agents.md +0 -280
  159. /package/dist/browser/{highs.wasm.LICENSE → solvers/highs.wasm.LICENSE} +0 -0
  160. /package/dist/lib/{highs-assets.js → solvers/highs-assets.js} +0 -0
  161. /package/dist/lib/{highs-worker-factory.js → solvers/highs-worker-factory.js} +0 -0
  162. /package/dist/lib/{highs.wasm.LICENSE → solvers/highs.wasm.LICENSE} +0 -0
@@ -1,1034 +0,0 @@
1
- //#region src/modeler/boolean-expr.ts
2
- var e = 0;
3
- function t(e, t) {
4
- if (e instanceof n || e instanceof d) return e;
5
- throw typeof e == "object" && e && ("terms" in e || "quadTerms" in e) ? Error(`Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical operator "${t}". Use a comparison constraint such as expr\`\${x} <= 10\` or a BoolVar/BoolExpr.`) : typeof e == "object" && e && "id" in e && "name" in e && "lb" in e && "ub" in e && !("sense" in e) ? Error(`Invalid operand passed to logical operator "${t}": expected a BoolVar, BoolExpr, or Constraint, but received numeric variable "${e.name}". Use m.bool() for propositional variables, add a comparison such as expr\`\${x} == 1\`, or use m.indicator(binVar, constraint) for 0-1 Big-M indicator constraints.`) : Error(`Invalid operand passed to logical operator "${t}": expected a BoolVar, BoolExpr, or Constraint.`);
6
- }
7
- var n = class {
8
- and(e) {
9
- return new i("and", [this, t(e, "and")]);
10
- }
11
- or(e) {
12
- return new i("or", [this, t(e, "or")]);
13
- }
14
- implies(e) {
15
- return new i("implies", [this, t(e, "implies")]);
16
- }
17
- xor(e) {
18
- return new i("xor", [this, t(e, "xor")]);
19
- }
20
- not() {
21
- return new i("not", [this]);
22
- }
23
- }, r = class extends n {
24
- id;
25
- name;
26
- value = null;
27
- constructor(t) {
28
- super(), this.id = ++e, this.name = t || `p_${this.id}`;
29
- }
30
- toString() {
31
- return this.name;
32
- }
33
- }, i = class extends n {
34
- op;
35
- children;
36
- constructor(e, n) {
37
- super(), this.op = e, this.children = n.map((n) => t(n, e));
38
- }
39
- toString() {
40
- return this.op === "not" ? `not(${this.children[0].toString()})` : `(${this.children.map((e) => e.toString()).join(` ${this.op} `)})`;
41
- }
42
- };
43
- function a(...e) {
44
- if (e.length === 0) throw Error("and() requires at least one expression");
45
- return new i("and", e);
46
- }
47
- function o(...e) {
48
- if (e.length === 0) throw Error("or() requires at least one expression");
49
- return new i("or", e);
50
- }
51
- function s(e) {
52
- return new i("not", [e]);
53
- }
54
- function c(e, t) {
55
- return new i("implies", [e, t]);
56
- }
57
- function l(...e) {
58
- if (e.length === 0) throw Error("xor() requires at least one expression");
59
- return new i("xor", e);
60
- }
61
- //#endregion
62
- //#region src/modeler/constraint.ts
63
- var u = 0, d = class e {
64
- kind = "constraint";
65
- id;
66
- _lhs;
67
- _sense;
68
- _rhs;
69
- _name;
70
- hasExplicitName;
71
- ownerModel = null;
72
- slack = null;
73
- dual = null;
74
- addTerm(e, t) {
75
- return t === 0 ? this : (this._lhs.addTerm(e, t), this.ownerModel?.notifyMutation?.(), this);
76
- }
77
- setTerm(e, t) {
78
- return this._lhs.setTerm(e, t), this.ownerModel?.notifyMutation?.(), this;
79
- }
80
- constructor(e, t, n, r) {
81
- this.id = ++u, this._lhs = e, this._sense = t, this._rhs = n;
82
- let i = typeof r == "string" ? r.trim() : void 0;
83
- i && i.length > 0 ? (this._name = i, this.hasExplicitName = !0) : (this._name = `c_${this.id}`, this.hasExplicitName = !1);
84
- }
85
- get lhs() {
86
- return this._lhs;
87
- }
88
- set lhs(e) {
89
- this._lhs !== e && (this._lhs = e, this.ownerModel?.notifyMutation?.());
90
- }
91
- get sense() {
92
- return this._sense;
93
- }
94
- set sense(e) {
95
- this._sense !== e && (this._sense = e, this.ownerModel?.notifyMutation?.());
96
- }
97
- get rhs() {
98
- return this._rhs;
99
- }
100
- set rhs(e) {
101
- Object.is(this._rhs, e) || (this._rhs = e, this.ownerModel?.notifyMutation?.());
102
- }
103
- get name() {
104
- return this._name;
105
- }
106
- set name(e) {
107
- this._name !== e && (this._name = e, this.ownerModel?.notifyMutation?.()), this.hasExplicitName = !0;
108
- }
109
- _assignModelName(e, t) {
110
- this._name = e, this.hasExplicitName = t;
111
- }
112
- and(e) {
113
- return new i("and", [this, e]);
114
- }
115
- or(e) {
116
- return new i("or", [this, e]);
117
- }
118
- implies(e) {
119
- return new i("implies", [this, e]);
120
- }
121
- xor(e) {
122
- return new i("xor", [this, e]);
123
- }
124
- not() {
125
- return new i("not", [this]);
126
- }
127
- add(e) {
128
- throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
129
- }
130
- sub(e) {
131
- throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
132
- }
133
- mul(e) {
134
- throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
135
- }
136
- div(e) {
137
- throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
138
- }
139
- neg() {
140
- throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
141
- }
142
- le(e, t) {
143
- throw Error("Cannot compare a Constraint in (<=): expected a number, Var, or LinExpr.");
144
- }
145
- ge(e, t) {
146
- throw Error("Cannot compare a Constraint in (>=): expected a number, Var, or LinExpr.");
147
- }
148
- eq(e, t) {
149
- throw Error("Cannot compare a Constraint in (==): expected a number, Var, or LinExpr.");
150
- }
151
- get value() {
152
- throw Error("Cannot read numeric value of a Constraint: expected a Var, LinExpr, or QuadExpr.");
153
- }
154
- get constant() {
155
- throw Error("Cannot read numeric constant of a Constraint: expected a LinExpr or QuadExpr.");
156
- }
157
- clone() {
158
- return new e(this._lhs.clone(), this._sense, this._rhs, this.hasExplicitName ? this._name : void 0);
159
- }
160
- toString() {
161
- let e = this.sense === "==" ? "=" : this.sense;
162
- return `${this.name}: ${this.lhs.toString()} ${e} ${this.rhs}`;
163
- }
164
- };
165
- function f(e) {
166
- return e instanceof d || typeof e == "object" && !!e && "sense" in e && "lhs" in e && "rhs" in e;
167
- }
168
- //#endregion
169
- //#region src/modeler/quad-expr.ts
170
- function p(e, t) {
171
- let n = e.id, r = t.id;
172
- return n <= r ? `${n}:${r}` : `${r}:${n}`;
173
- }
174
- var m = class e {
175
- kind = "quadratic";
176
- linearTerms;
177
- quadTerms;
178
- constant;
179
- constructor(e = /* @__PURE__ */ new Map(), t = /* @__PURE__ */ new Map(), n = 0) {
180
- this.linearTerms = new Map(e), this.quadTerms = /* @__PURE__ */ new Map();
181
- for (let [e, n] of t) this.quadTerms.set(e, { ...n });
182
- this.constant = n;
183
- }
184
- clone() {
185
- return new e(this.linearTerms, this.quadTerms, this.constant);
186
- }
187
- getQuadCoeff(e, t) {
188
- let n = p(e, t), r = this.quadTerms.get(n);
189
- return r ? r.coeff : 0;
190
- }
191
- add(t) {
192
- if (f(t)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
193
- let n = this.clone();
194
- if (typeof t == "number") return n.constant += t, n;
195
- if (t instanceof e) {
196
- for (let [e, r] of t.quadTerms) {
197
- let t = n.quadTerms.get(e), i = (t ? t.coeff : 0) + r.coeff;
198
- i === 0 ? n.quadTerms.delete(e) : n.quadTerms.set(e, {
199
- var1: r.var1,
200
- var2: r.var2,
201
- coeff: i
202
- });
203
- }
204
- for (let [e, r] of t.linearTerms) {
205
- let t = (n.linearTerms.get(e) ?? 0) + r;
206
- t === 0 ? n.linearTerms.delete(e) : n.linearTerms.set(e, t);
207
- }
208
- return n.constant += t.constant, n;
209
- }
210
- if ("terms" in t && t.terms instanceof Map) {
211
- for (let [e, r] of t.terms) {
212
- let t = (n.linearTerms.get(e) ?? 0) + r;
213
- t === 0 ? n.linearTerms.delete(e) : n.linearTerms.set(e, t);
214
- }
215
- return n.constant += t.constant, n;
216
- }
217
- let r = t, i = (n.linearTerms.get(r) ?? 0) + 1;
218
- return i === 0 ? n.linearTerms.delete(r) : n.linearTerms.set(r, i), n;
219
- }
220
- sub(e) {
221
- if (f(e)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
222
- return typeof e == "number" ? this.add(-e) : this.add(e.mul(-1));
223
- }
224
- mul(t) {
225
- if (f(t)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
226
- if (typeof t == "number") {
227
- let n = this.linearTerms.size > 0 || this.quadTerms.size > 0, r = this.constant === 0 && n ? 0 : this.constant * t;
228
- if (this.constant !== 0 && t !== 0 && r === 0) throw Error("Constant multiplication underflows to zero.");
229
- let i = new e(/* @__PURE__ */ new Map(), /* @__PURE__ */ new Map(), r);
230
- for (let [e, n] of this.linearTerms) {
231
- let r = n * t;
232
- if (n !== 0 && t !== 0 && r === 0) throw Error("Coefficient multiplication underflows to zero.");
233
- r !== 0 && i.linearTerms.set(e, r);
234
- }
235
- for (let [e, n] of this.quadTerms) {
236
- let r = n.coeff * t;
237
- if (n.coeff !== 0 && t !== 0 && r === 0) throw Error("Coefficient multiplication underflows to zero.");
238
- r !== 0 && i.quadTerms.set(e, {
239
- var1: n.var1,
240
- var2: n.var2,
241
- coeff: r
242
- });
243
- }
244
- return i;
245
- }
246
- 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;
247
- 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)");
248
- if (r === 0 && i.size === 0) return this.mul(a);
249
- if (this.quadTerms.size === 0 && this.linearTerms.size === 0 && !n) {
250
- let n = t.mul(this.constant);
251
- return "quadTerms" in n ? n : new e(n.terms, /* @__PURE__ */ new Map(), n.constant);
252
- }
253
- return e.multiplyLinearTerms(this.constant, this.linearTerms, a, i);
254
- }
255
- div(t) {
256
- let n = e.extractScalarDivisor(t, "QuadExpr.div()");
257
- return this.mul(1 / n);
258
- }
259
- neg() {
260
- return this.mul(-1);
261
- }
262
- get value() {
263
- let e = this.constant;
264
- for (let [t, n] of this.linearTerms) {
265
- if (t.value === null) return null;
266
- e += n * t.value;
267
- }
268
- for (let t of this.quadTerms.values()) {
269
- if (t.var1.value === null || t.var2.value === null) return null;
270
- e += t.coeff * t.var1.value * t.var2.value;
271
- }
272
- return e;
273
- }
274
- le(e, t) {
275
- return this.toConstraint("<=", e, t);
276
- }
277
- ge(e, t) {
278
- return this.toConstraint(">=", e, t);
279
- }
280
- eq(e, t) {
281
- return this.toConstraint("==", e, t);
282
- }
283
- toConstraint(e, t, n) {
284
- if (this.quadTerms.size > 0) throw Error(`Quadratic expressions are not supported in linear constraints (${e}): quadratic terms are only allowed in objectives.`);
285
- let r = new h(this.linearTerms, this.constant);
286
- return e === "<=" ? r.le(t, n) : e === ">=" ? r.ge(t, n) : r.eq(t, n);
287
- }
288
- and(e) {
289
- throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'and': expected a BoolVar, Constraint, or BoolOp.");
290
- }
291
- or(e) {
292
- throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'or': expected a BoolVar, Constraint, or BoolOp.");
293
- }
294
- implies(e) {
295
- throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'implies': expected a BoolVar, Constraint, or BoolOp.");
296
- }
297
- xor(e) {
298
- throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'xor': expected a BoolVar, Constraint, or BoolOp.");
299
- }
300
- not() {
301
- throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'not': expected a BoolVar, Constraint, or BoolOp.");
302
- }
303
- toString() {
304
- let e = [], t = !0;
305
- for (let n of this.quadTerms.values()) {
306
- let r = n.coeff, i = r >= 0 ? t ? "" : "+ " : "- ", a = Math.abs(r), o = a === 1 ? "" : `${a} `;
307
- n.var1 === n.var2 ? e.push(`${i}${o}${n.var1.name}^2`) : e.push(`${i}${o}${n.var1.name} * ${n.var2.name}`), t = !1;
308
- }
309
- for (let [n, r] of this.linearTerms) {
310
- let i = r >= 0 ? t ? "" : "+ " : "- ", a = Math.abs(r), o = a === 1 ? "" : `${a} `;
311
- e.push(`${i}${o}${n.name}`), t = !1;
312
- }
313
- if (this.constant !== 0 || e.length === 0) {
314
- let n = this.constant >= 0 ? t ? "" : "+ " : "- ";
315
- e.push(`${n}${Math.abs(this.constant)}`);
316
- }
317
- return e.join(" ") || "0";
318
- }
319
- static fromProduct(t, n, r = 1) {
320
- let i = new e();
321
- if (r === 0) return i;
322
- let a = p(t, n);
323
- return i.quadTerms.set(a, {
324
- var1: t.id <= n.id ? t : n,
325
- var2: t.id <= n.id ? n : t,
326
- coeff: r
327
- }), i;
328
- }
329
- static multiplyLinearTerms(t, n, r, i) {
330
- let a = n.size > 0 || i.size > 0, o = (t === 0 || r === 0) && a ? 0 : t * r;
331
- if (t !== 0 && r !== 0 && o === 0) throw Error("Constant multiplication underflows to zero.");
332
- let s = new e(/* @__PURE__ */ new Map(), /* @__PURE__ */ new Map(), o === 0 ? 0 : o);
333
- if (r !== 0) for (let [e, t] of n) {
334
- let n = t * r;
335
- if (t !== 0 && n === 0) throw Error("Coefficient multiplication underflows to zero.");
336
- n !== 0 && s.linearTerms.set(e, n);
337
- }
338
- if (t !== 0) for (let [e, n] of i) {
339
- let r = s.linearTerms.get(e) ?? 0, i = n * t;
340
- if (n !== 0 && i === 0) throw Error("Coefficient multiplication underflows to zero.");
341
- let a = r + i;
342
- a === 0 ? s.linearTerms.delete(e) : s.linearTerms.set(e, a);
343
- }
344
- for (let [t, r] of n) for (let [n, a] of i) {
345
- let i = r * a;
346
- if (r !== 0 && a !== 0 && i === 0) throw Error("Coefficient multiplication underflows to zero.");
347
- s = s.add(e.fromProduct(t, n, i));
348
- }
349
- return s;
350
- }
351
- static extractScalarDivisor(e, t) {
352
- if (f(e)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
353
- if (typeof e == "number") {
354
- if (e === 0) throw Error(`Division by zero in ${t}`);
355
- return e;
356
- }
357
- let n = "quadTerms" in e ? e.linearTerms.size : e.terms.size, r = "quadTerms" in e ? e.quadTerms.size : 0;
358
- if (n > 0 || r > 0) throw Error("Cannot divide by variable in expression");
359
- if (e.constant === 0) throw Error(`Division by zero in ${t}`);
360
- return e.constant;
361
- }
362
- }, h = class e {
363
- kind = "linear";
364
- terms;
365
- constant;
366
- constructor(e = /* @__PURE__ */ new Map(), t = 0) {
367
- this.terms = new Map(e), this.constant = t;
368
- }
369
- setTerm(e, t) {
370
- return t === 0 ? this.terms.delete(e) : this.terms.set(e, t), this;
371
- }
372
- addTerm(e, t) {
373
- let n = (this.terms.get(e) ?? 0) + t;
374
- return n === 0 ? this.terms.delete(e) : this.terms.set(e, n), this;
375
- }
376
- clone() {
377
- return new e(this.terms, this.constant);
378
- }
379
- add(e) {
380
- if (f(e)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
381
- if (e instanceof m) return e.add(this);
382
- let t = this.clone();
383
- if (typeof e == "number") t.constant += e;
384
- else if ("terms" in e) {
385
- for (let [n, r] of e.terms) {
386
- let e = (t.terms.get(n) ?? 0) + r;
387
- e === 0 ? t.terms.delete(n) : t.terms.set(n, e);
388
- }
389
- t.constant += e.constant;
390
- } else {
391
- let n = (t.terms.get(e) ?? 0) + 1;
392
- n === 0 ? t.terms.delete(e) : t.terms.set(e, n);
393
- }
394
- return t;
395
- }
396
- sub(e) {
397
- if (f(e)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
398
- return e instanceof m ? e.neg().add(this) : typeof e == "number" ? this.add(-e) : this.add(e.mul(-1));
399
- }
400
- mul(t) {
401
- if (f(t)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
402
- if (typeof t == "number") {
403
- let n = this.constant === 0 && this.terms.size > 0 ? 0 : this.constant * t;
404
- if (this.constant !== 0 && t !== 0 && n === 0) throw Error("Constant multiplication underflows to zero.");
405
- let r = new e(/* @__PURE__ */ new Map(), n);
406
- for (let [e, n] of this.terms) {
407
- let i = n * t;
408
- if (n !== 0 && t !== 0 && i === 0) throw Error("Coefficient multiplication underflows to zero.");
409
- i !== 0 && r.terms.set(e, i);
410
- }
411
- return r;
412
- }
413
- return t instanceof F || typeof t == "object" && t && "id" in t && !("terms" in t) && !("quadTerms" in t) ? m.multiplyLinearTerms(this.constant, this.terms, 0, /* @__PURE__ */ new Map([[t, 1]])) : t instanceof m ? this.terms.size === 0 ? t.mul(this.constant) : new m(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) : m.multiplyLinearTerms(this.constant, this.terms, t.constant, t.terms);
414
- }
415
- div(e) {
416
- let t = m.extractScalarDivisor(e, "LinExpr.div()");
417
- return this.mul(1 / t);
418
- }
419
- neg() {
420
- return this.mul(-1);
421
- }
422
- get value() {
423
- let e = this.constant;
424
- for (let [t, n] of this.terms) {
425
- if (t.value === null) return null;
426
- e += n * t.value;
427
- }
428
- return e;
429
- }
430
- le(e, t) {
431
- return this.toConstraint("<=", e, t);
432
- }
433
- ge(e, t) {
434
- return this.toConstraint(">=", e, t);
435
- }
436
- eq(e, t) {
437
- return this.toConstraint("==", e, t);
438
- }
439
- toConstraint(t, n, r) {
440
- let i = this.clone(), a = 0;
441
- if (typeof n == "number") a = n;
442
- else if (n instanceof d || 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.`);
443
- else if (n instanceof m || typeof n == "object" && n && "quadTerms" in n) {
444
- let r = n;
445
- if (r.quadTerms.size > 0) throw Error(`Quadratic expressions are not supported in linear constraints (${t}): quadratic terms are only allowed in objectives.`);
446
- i = i.sub(new e(r.linearTerms, 0)), a = r.constant;
447
- } else "terms" in n ? (i = i.sub(new e(n.terms, 0)), a = n.constant) : i = i.sub(n);
448
- let o = a - i.constant;
449
- return i.constant = 0, new d(i, t, o, r);
450
- }
451
- and(e) {
452
- throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'and': expected a BoolVar, Constraint, or BoolOp.");
453
- }
454
- or(e) {
455
- throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'or': expected a BoolVar, Constraint, or BoolOp.");
456
- }
457
- implies(e) {
458
- throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'implies': expected a BoolVar, Constraint, or BoolOp.");
459
- }
460
- xor(e) {
461
- throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'xor': expected a BoolVar, Constraint, or BoolOp.");
462
- }
463
- not() {
464
- throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'not': expected a BoolVar, Constraint, or BoolOp.");
465
- }
466
- toString() {
467
- let e = [], t = !0;
468
- for (let [n, r] of this.terms) {
469
- let i = r >= 0 ? t ? "" : "+ " : "- ", a = Math.abs(r), o = a === 1 ? "" : `${a} `;
470
- e.push(`${i}${o}${n.name}`), t = !1;
471
- }
472
- if (this.constant !== 0 || e.length === 0) {
473
- let n = this.constant >= 0 ? t ? "" : "+ " : "- ";
474
- e.push(`${n}${Math.abs(this.constant)}`);
475
- }
476
- return e.join(" ") || "0";
477
- }
478
- };
479
- function g(e, t) {
480
- let n = /* @__PURE__ */ new Map(), r = 0;
481
- function i(e) {
482
- return e instanceof d || e.kind === "constraint" || "sense" in e && (e.sense === "<=" || e.sense === ">=" || e.sense === "==") && "lhs" in e && "rhs" in e && typeof e.rhs == "number" && typeof e.lhs == "object" && e.lhs !== null && "terms" in e.lhs && e.lhs.terms instanceof Map;
483
- }
484
- function a(e) {
485
- return e instanceof m || "quadTerms" in e && e.quadTerms instanceof Map && "linearTerms" in e && e.linearTerms instanceof Map && typeof e.constant == "number";
486
- }
487
- function o(e) {
488
- return e instanceof h || "terms" in e && e.terms instanceof Map && typeof e.constant == "number";
489
- }
490
- function s(e) {
491
- return e instanceof F || !i(e) && typeof e.id == "number" && typeof e.name == "string" && typeof e.lb == "number" && typeof e.ub == "number" && typeof e.type == "string" && typeof e.mul == "function";
492
- }
493
- function c(e) {
494
- return i(e) || a(e) || o(e) || s(e);
495
- }
496
- function l(e) {
497
- if (typeof e != "object" || !e || Array.isArray(e) || c(e)) return !1;
498
- let t = Object.getPrototypeOf(e);
499
- return t === Object.prototype || t === null;
500
- }
501
- function u(e) {
502
- if (e != null) {
503
- if (typeof e == "number") {
504
- if (!Number.isFinite(e)) throw Error(`Invalid non-finite number "${e}" in sum().`);
505
- let t = r + e;
506
- if (!Number.isFinite(t)) throw Error("Constant accumulation overflows finite floating-point range in sum().");
507
- r = t;
508
- } else if (typeof e == "object" && i(e)) throw Error("Cannot sum a constraint comparison (<=, >=, ==) in sum(): expected a number, Var, or LinExpr.");
509
- else if (typeof e == "object" && a(e)) {
510
- let t = e;
511
- if (t.quadTerms.size > 0) throw Error("Quadratic expressions are not supported in sum(): sum() produces a linear expression (LinExpr).");
512
- if (!Number.isFinite(t.constant)) throw Error(`Invalid non-finite constant "${t.constant}" in sum().`);
513
- for (let [e, r] of t.linearTerms) {
514
- if (!Number.isFinite(r)) throw Error(`Invalid non-finite coefficient "${r}" in sum().`);
515
- let t = (n.get(e) ?? 0) + r;
516
- if (!Number.isFinite(t)) throw Error("Coefficient accumulation overflows finite floating-point range in sum().");
517
- n.set(e, t);
518
- }
519
- let i = r + t.constant;
520
- if (!Number.isFinite(i)) throw Error("Constant accumulation overflows finite floating-point range in sum().");
521
- r = i;
522
- } else if (typeof e == "object" && o(e)) {
523
- let t = e;
524
- if (!Number.isFinite(t.constant)) throw Error(`Invalid non-finite constant "${t.constant}" in sum().`);
525
- for (let [e, r] of t.terms) {
526
- if (!Number.isFinite(r)) throw Error(`Invalid non-finite coefficient "${r}" in sum().`);
527
- let t = (n.get(e) ?? 0) + r;
528
- if (!Number.isFinite(t)) throw Error("Coefficient accumulation overflows finite floating-point range in sum().");
529
- n.set(e, t);
530
- }
531
- let i = r + t.constant;
532
- if (!Number.isFinite(i)) throw Error("Constant accumulation overflows finite floating-point range in sum().");
533
- r = i;
534
- } else if (typeof e == "object" && s(e)) {
535
- let t = e, r = (n.get(t) ?? 0) + 1;
536
- if (!Number.isFinite(r)) throw Error("Coefficient accumulation overflows finite floating-point range in sum().");
537
- n.set(t, r);
538
- } else throw Error("Unsupported item in sum(): expected a number, Var, or LinExpr.");
539
- }
540
- }
541
- function f(e, t = 0) {
542
- if (e != null) {
543
- if (typeof e == "number") {
544
- u(e);
545
- return;
546
- }
547
- if (typeof e == "object") {
548
- if (c(e)) {
549
- u(e);
550
- return;
551
- }
552
- if (!(e instanceof String) && !(e instanceof Map) && Symbol.iterator in e && typeof e[Symbol.iterator] == "function") {
553
- if (t >= 2) throw Error("Nested containers deeper than 2D are not supported in sum().");
554
- for (let n of e) f(n, t + 1);
555
- return;
556
- }
557
- if (l(e)) {
558
- if (t >= 2) throw Error("Nested containers deeper than 2D are not supported in sum().");
559
- if (Object.getOwnPropertySymbols(e).length > 0) throw Error("Symbol keys are not supported in sum().");
560
- for (let n of Object.keys(e)) f(e[n], t + 1);
561
- return;
562
- }
563
- }
564
- u(e);
565
- }
566
- }
567
- if (typeof e == "number") {
568
- if (typeof t != "function") throw Error("sum(count, mapFn) requires a mapping function as the second argument.");
569
- if (!Number.isInteger(e) || e < 0) throw Error("sum(count, mapFn) requires a non-negative integer count.");
570
- let n = e;
571
- for (let e = 0; e < n; e++) f(t(e, e), 0);
572
- } else if (typeof e == "object" && e && !(e instanceof String) && (!(e instanceof Map) || typeof t == "function") && Symbol.iterator in e && typeof e[Symbol.iterator] == "function") {
573
- let n = 0;
574
- for (let r of e) t ? f(t(r, n++), 0) : f(r, 1);
575
- } else if (l(e)) {
576
- if (Object.getOwnPropertySymbols(e).length > 0) throw Error("Symbol keys are not supported in sum().");
577
- let n = 0;
578
- for (let r of Object.keys(e)) {
579
- let i = e[r];
580
- t ? f(t(i, n++), 0) : f(i, 1);
581
- }
582
- } else throw Error("Unsupported item in sum(): expected an Iterable, Record, or (count, mapFn).");
583
- for (let [e, t] of n) t === 0 && n.delete(e);
584
- return new h(n, r);
585
- }
586
- function _(e, t) {
587
- let n = /* @__PURE__ */ new Map(), r = 0;
588
- function i(e) {
589
- return typeof e == "object" && !!e && !(e instanceof d) && 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";
590
- }
591
- function a(e) {
592
- return i(e) || e instanceof h || e instanceof m;
593
- }
594
- function o(e, t, a) {
595
- if (!Number.isFinite(e)) throw Error(`Invalid non-finite coefficient "${e}" in dot() at '${a || "root"}'.`);
596
- if (t instanceof m) throw Error(`Quadratic expressions are not supported in dot() at '${a || "root"}': dot() produces a linear expression (LinExpr).`);
597
- if (t instanceof h) {
598
- if (!Number.isFinite(t.constant)) throw Error(`Invalid non-finite constant "${t.constant}" in dot() at '${a || "root"}'.`);
599
- let i = e === 0 || t.constant === 0 ? 0 : e * t.constant;
600
- if (e !== 0 && t.constant !== 0 && i === 0) throw Error("Constant multiplication underflows to zero.");
601
- if (!Number.isFinite(i)) throw Error(`Coefficient multiplication overflows finite floating-point range in dot() at '${a || "root"}'.`);
602
- let o = r + i;
603
- if (!Number.isFinite(o)) throw Error(`Constant accumulation overflows finite floating-point range in dot() at '${a || "root"}'.`);
604
- r = o;
605
- for (let [r, i] of t.terms) {
606
- if (!Number.isFinite(i)) throw Error(`Invalid non-finite coefficient "${i}" in dot() at '${a || "root"}'.`);
607
- let t = e * i;
608
- if (e !== 0 && i !== 0 && t === 0) throw Error("Coefficient multiplication underflows to zero.");
609
- if (!Number.isFinite(t)) throw Error(`Coefficient multiplication overflows finite floating-point range in dot() at '${a || "root"}'.`);
610
- let o = (n.get(r) ?? 0) + t;
611
- if (!Number.isFinite(o)) throw Error(`Coefficient accumulation overflows finite floating-point range in dot() at '${a || "root"}'.`);
612
- n.set(r, o);
613
- }
614
- } else if (i(t)) {
615
- let r = t, i = (n.get(r) ?? 0) + e;
616
- if (!Number.isFinite(i)) throw Error(`Coefficient accumulation overflows finite floating-point range in dot() at '${a || "root"}'.`);
617
- n.set(r, i);
618
- } else throw Error(`Cannot multiply number by non-variable/expression operand in dot() at '${a || "root"}'.`);
619
- }
620
- function s(e) {
621
- return typeof e == "object" && !!e && !Array.isArray(e) && !a(e) && !(e instanceof d) && e.kind !== "constraint";
622
- }
623
- function c(e, t, n = "") {
624
- if (e === void 0 || t === void 0 || e === null || t === null) throw Error(`Nullish or undefined operand encountered in dot() at '${n || "root"}'.`);
625
- 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.`);
626
- if (typeof e == "number") {
627
- o(e, t, n);
628
- return;
629
- }
630
- if (typeof t == "number") {
631
- o(t, e, n);
632
- return;
633
- }
634
- if (Array.isArray(e) || Array.isArray(t)) {
635
- if (Array.isArray(e) && Array.isArray(t)) {
636
- if (e.length !== t.length) throw Error(`Array length mismatch in dot() at '${n || "root"}': received lengths ${e.length} and ${t.length}.`);
637
- for (let r = 0; r < e.length; r++) c(e[r], t[r], n ? `${n}[${r}]` : `[${r}]`);
638
- return;
639
- }
640
- let r = Array.isArray(e) ? e : t, i = Array.isArray(e) ? t : e;
641
- if (s(i)) {
642
- if (Object.getOwnPropertySymbols(i).length > 0) throw Error(`Symbol keys are not supported in dot() at '${n || "root"}'.`);
643
- let a = Object.keys(i);
644
- if (a.length === r.length && a.every((e) => /^(?:0|[1-9]\d*)$/.test(e))) {
645
- let a = [];
646
- for (let e = 0; e < r.length; e++) Object.prototype.hasOwnProperty.call(i, e) || a.push(e);
647
- if (a.length > 0) throw Error(`Key mismatch in dot() at '${n || "root"}': missing indices [${a.join(", ")}] in numeric record.`);
648
- for (let i = 0; i < r.length; i++) c(e[i], t[i], n ? `${n}[${i}]` : `[${i}]`);
649
- return;
650
- }
651
- 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(", ")}].`);
652
- }
653
- throw Error(`Type mismatch in dot() at '${n || "root"}': one operand is an Array while the other is not.`);
654
- }
655
- if (s(e) && s(t)) {
656
- if (Object.getOwnPropertySymbols(e).length > 0 || Object.getOwnPropertySymbols(t).length > 0) throw Error(`Symbol keys are not supported in dot() at '${n || "root"}'.`);
657
- 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));
658
- if (a.length > 0 || o.length > 0) {
659
- let e = [];
660
- 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("; ")}.`);
661
- }
662
- for (let i of r) c(e[i], t[i], n ? `${n}.${i}` : i);
663
- return;
664
- }
665
- throw Error(`Unsupported operand types in dot() at '${n || "root"}'.`);
666
- }
667
- c(e, t);
668
- for (let [e, t] of n) t === 0 && n.delete(e);
669
- return new h(n, r);
670
- }
671
- //#endregion
672
- //#region src/modeler/validation.ts
673
- var v = /* @__PURE__ */ new WeakMap(), y = /* @__PURE__ */ new WeakMap(), b = /* @__PURE__ */ new WeakMap();
674
- function x(e, t, n) {
675
- if (e instanceof i) for (let r of e.children) x(r, t, n);
676
- else if (e instanceof d) {
677
- let r = e.ownerModel, i = y.get(e);
678
- (r !== t || i !== t) && (n && n.push(() => {
679
- e.ownerModel = r, i ? y.set(e, i) : y.delete(e);
680
- }), e.ownerModel = t, y.set(e, t));
681
- }
682
- }
683
- function S(e, t) {
684
- let n = v.get(e);
685
- if (n && n !== t) throw Error(`Variable "${e.name}" already belongs to model "${n.name}" and cannot be added to model "${t.name}".`);
686
- v.set(e, t);
687
- }
688
- function C(e, t) {
689
- let n = b.get(e);
690
- 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}".`);
691
- let r = y.get(e);
692
- if (r && r !== t) throw Error(`Constraint "${e.name}" already belongs to model "${r.name}" and cannot be added to model "${t.name}".`);
693
- return b.set(e, t), y.set(e, t), r;
694
- }
695
- function w(e, t) {
696
- v.get(e) === t && v.delete(e);
697
- }
698
- function T(e, t, n) {
699
- b.get(e) === t && b.delete(e), y.get(e) === t && (n ? y.set(e, n) : y.delete(e));
700
- }
701
- var E = 1e-6;
702
- function D(e) {
703
- 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).`);
704
- if (e.lb === Infinity) throw Error(`Variable "${e.name}" has invalid lower bound: +Infinity is not allowed as a lower bound.`);
705
- 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).`);
706
- if (e.ub === -Infinity) throw Error(`Variable "${e.name}" has invalid upper bound: -Infinity is not allowed as an upper bound.`);
707
- 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}).`);
708
- if (e.initialValue !== void 0) {
709
- 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).`);
710
- let t = e.type === "Binary" ? Math.max(0, e.lb) : e.lb, n = e.type === "Binary" ? Math.min(1, e.ub) : e.ub;
711
- 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 ${E}).`);
712
- if (e.type === "Integer" || e.type === "Binary") {
713
- let r = Math.round(e.initialValue);
714
- 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 ${E} of an integer in [${t}, ${n}]).`);
715
- }
716
- }
717
- }
718
- function O(e) {
719
- if (e.initialValue === void 0) return;
720
- if (D(e), e.type === "Integer" || e.type === "Binary") {
721
- let t = Math.round(e.initialValue);
722
- return Object.is(t, -0) ? 0 : t;
723
- }
724
- let t = Math.min(e.ub, Math.max(e.lb, e.initialValue));
725
- return Object.is(t, -0) ? 0 : t;
726
- }
727
- function k(e, t, n) {
728
- if (e instanceof m) {
729
- if (!Number.isFinite(e.constant)) throw Error(`Objective function has invalid constant: ${e.constant}. Constant must be a finite number.`);
730
- for (let [r, i] of e.linearTerms) {
731
- 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.`);
732
- if (!Number.isFinite(i)) throw Error(`Variable "${r.name}" in objective function has invalid coefficient: ${i}. Coefficients must be finite numbers.`);
733
- }
734
- for (let [, r] of e.quadTerms) {
735
- 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.`);
736
- 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.`);
737
- 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}.`);
738
- }
739
- } else if ("terms" in e) {
740
- if (!Number.isFinite(e.constant)) throw Error(`Objective function has invalid constant: ${e.constant}. Constant must be a finite number.`);
741
- for (let [r, i] of e.terms) {
742
- 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.`);
743
- if (!Number.isFinite(i)) throw Error(`Variable "${r.name}" in objective function has invalid coefficient: ${i}. Coefficients must be finite numbers.`);
744
- }
745
- }
746
- }
747
- function A(e, t, n, r = e.name) {
748
- if (!Number.isFinite(e.rhs)) throw Error(`Constraint "${r}" has invalid right-hand side (RHS): ${e.rhs}. RHS must be a finite number.`);
749
- 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.`);
750
- for (let [i, a] of e.lhs.terms) {
751
- 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.`);
752
- if (!Number.isFinite(a)) throw Error(`Variable "${i.name}" in constraint "${r}" has invalid coefficient: ${a}. Coefficients must be finite numbers.`);
753
- }
754
- }
755
- function j(e, t, n, a, o, s) {
756
- if (e instanceof r) {
757
- if (!o(e)) throw Error(`Boolean variable "${e.name}" in logical constraint "${t}" does not belong to model "${n}".`);
758
- } else if (e instanceof i) for (let r of e.children) j(r, t, n, a, o, s);
759
- else if (e instanceof d) {
760
- if (s) {
761
- let r = y.get(e) ?? e.ownerModel ?? void 0;
762
- if (r && r !== s) throw Error(`Constraint "${e.name}" embedded in logical constraint "${t}" already belongs to model "${r.name ?? "another model"}" and cannot be added to model "${n}".`);
763
- }
764
- if (!Number.isFinite(e.rhs)) throw Error(`Constraint "${e.name}" embedded in logical constraint "${t}" has invalid RHS: ${e.rhs}.`);
765
- 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}.`);
766
- for (let [r, i] of e.lhs.terms) {
767
- if (!a(r)) throw Error(`Variable "${r.name}" embedded in logical constraint "${t}" does not belong to model "${n}".`);
768
- if (!Number.isFinite(i)) throw Error(`Variable "${r.name}" embedded in logical constraint "${t}" has invalid coefficient: ${i}.`);
769
- }
770
- }
771
- }
772
- function M(e) {
773
- let t = /* @__PURE__ */ new Set(), n = /* @__PURE__ */ new Set();
774
- for (let r of e.variables) {
775
- D(r);
776
- let i = v.get(r);
777
- 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}".`);
778
- t.add(r.name), n.add(r);
779
- }
780
- let r = /* @__PURE__ */ new Set(), i = /* @__PURE__ */ new Set();
781
- for (let t of e.boolVars) {
782
- 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}".`);
783
- r.add(t.name), i.add(t);
784
- }
785
- let a = /* @__PURE__ */ new Set();
786
- for (let t of e.constraints) {
787
- if (r.has(t.name)) throw Error(`Constraint name "${t.name}" conflicts with Boolean variable "${t.name}" in model "${e.name}".`);
788
- if (a.has(t.name)) throw Error(`Duplicate constraint name "${t.name}" in model "${e.name}".`);
789
- a.add(t.name);
790
- }
791
- for (let t of e.logicalConstraints) {
792
- if (r.has(t.name)) throw Error(`Logical constraint name "${t.name}" conflicts with Boolean variable "${t.name}" in model "${e.name}".`);
793
- if (a.has(t.name)) throw Error(`Conflicting constraint name "${t.name}" across arithmetic and logical constraints in model "${e.name}".`);
794
- a.add(t.name);
795
- }
796
- for (let t of e.constraints) {
797
- let n = y.get(t);
798
- if (n && n !== e || e.getConstraintByName(t.name) !== t) throw Error(`Constraint "${t.name}" is duplicated or not canonically registered in model "${e.name}".`);
799
- }
800
- 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}".`);
801
- e.objectiveExpr && k(e.objectiveExpr, e.name, (e) => n.has(e));
802
- for (let t of e.constraints) A(t, e.name, (e) => n.has(e));
803
- for (let t of e.logicalConstraints) j(t.expr, t.name, e.name, (e) => n.has(e), (e) => i.has(e), e);
804
- }
805
- //#endregion
806
- //#region src/modeler/variable.ts
807
- var N = 0;
808
- function P(e) {
809
- if (e === void 0) return "Continuous";
810
- if (typeof e == "string") {
811
- let t = e.toLowerCase();
812
- if (t === "binary" || t === "bin") return "Binary";
813
- if (t === "integer" || t === "int") return "Integer";
814
- if (t === "continuous" || t === "cont") return "Continuous";
815
- }
816
- throw Error(`Invalid variable type "${String(e)}": expected "Continuous", "Integer", or "Binary".`);
817
- }
818
- var F = class {
819
- id;
820
- _name;
821
- prefix;
822
- hasExplicitName;
823
- ownerModel = null;
824
- _type;
825
- _lb;
826
- _ub;
827
- _initialValue;
828
- value = null;
829
- reducedCost = null;
830
- constructor(e = {}) {
831
- this.id = ++N;
832
- let t = typeof e.name == "string" ? e.name.trim() : "", n = typeof e.prefix == "string" ? e.prefix.trim() : "";
833
- this.prefix = n.length > 0 ? n : "x", this.hasExplicitName = t.length > 0, this._name = this.hasExplicitName ? t : `${this.prefix}_${this.id}`, this._type = P(e.type), this._type === "Binary" ? (this._lb = 0, this._ub = 1) : (this._lb = e.lb === void 0 ? 0 : e.lb, this._ub = e.ub === void 0 ? Infinity : e.ub), this._initialValue = e.initialValue === void 0 ? void 0 : Object.is(e.initialValue, -0) ? 0 : e.initialValue, this._initialValue !== void 0 && D(this);
834
- }
835
- get name() {
836
- return this._name;
837
- }
838
- get type() {
839
- return this._type;
840
- }
841
- set type(e) {
842
- let t = P(e);
843
- this._type !== t && (this._type = t, this.ownerModel?.notifyMutation?.());
844
- }
845
- get lb() {
846
- return this._lb;
847
- }
848
- set lb(e) {
849
- Object.is(this._lb, e) || (this._lb = e, this.ownerModel?.notifyMutation?.());
850
- }
851
- get ub() {
852
- return this._ub;
853
- }
854
- set ub(e) {
855
- Object.is(this._ub, e) || (this._ub = e, this.ownerModel?.notifyMutation?.());
856
- }
857
- get initialValue() {
858
- return this._initialValue;
859
- }
860
- set initialValue(e) {
861
- let t = e !== void 0 && Object.is(e, -0) ? 0 : e;
862
- Object.is(this._initialValue, t) || (this._initialValue = t, this.ownerModel?.notifyMutation?.());
863
- }
864
- _assignModelName(e) {
865
- this._name = e;
866
- }
867
- mul(e) {
868
- return typeof e == "number" ? new h(/* @__PURE__ */ new Map([[this, 1]]), 0).mul(e) : new m(/* @__PURE__ */ new Map([[this, 1]]), /* @__PURE__ */ new Map(), 0).mul(e);
869
- }
870
- add(e) {
871
- return e instanceof m ? e.add(this) : new h(/* @__PURE__ */ new Map([[this, 1]]), 0).add(e);
872
- }
873
- sub(e) {
874
- return e instanceof m ? e.neg().add(this) : new h(/* @__PURE__ */ new Map([[this, 1]]), 0).sub(e);
875
- }
876
- neg() {
877
- return this.mul(-1);
878
- }
879
- le(e, t) {
880
- return new h(/* @__PURE__ */ new Map([[this, 1]]), 0).le(e, t);
881
- }
882
- ge(e, t) {
883
- return new h(/* @__PURE__ */ new Map([[this, 1]]), 0).ge(e, t);
884
- }
885
- eq(e, t) {
886
- return new h(/* @__PURE__ */ new Map([[this, 1]]), 0).eq(e, t);
887
- }
888
- toString() {
889
- return this.name;
890
- }
891
- };
892
- //#endregion
893
- //#region src/modeler/lp-writer.ts
894
- function I(e) {
895
- return typeof e == "object" && !!e && typeof e.lpString == "string" && e.nameToVar instanceof Map && e.nameToConstraint instanceof Map;
896
- }
897
- function L(e, t = "x", n = 0) {
898
- let r = e.replace(/[^a-zA-Z0-9_]/g, "_");
899
- return /^[0-9]/.test(r) && (r = `${t}_${r}`), (!r || r.length === 0) && (r = `${t}_${n}`), r;
900
- }
901
- function R(e) {
902
- 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();
903
- for (let e = 0; e < t.length; e++) {
904
- let n = t[e];
905
- s.set(n, e);
906
- let a = L(n.name, "x", e), o = a, l = 1;
907
- for (; c.has(o);) o = `${a}_${l++}`;
908
- c.add(o), i.set(n, o), r.set(o, n);
909
- }
910
- let l = /* @__PURE__ */ new Set();
911
- for (let e = 0; e < n.length; e++) {
912
- let t = n[e], r = L(t.name, "c", e + 1), i = r, s = 1;
913
- for (; l.has(i);) i = `${r}_${s++}`;
914
- l.add(i), o.set(t, i), a.set(i, t);
915
- }
916
- return {
917
- nameToVar: r,
918
- varToName: i,
919
- nameToConstraint: a,
920
- constraintToName: o,
921
- constrToName: o,
922
- varOrder: s
923
- };
924
- }
925
- function z(e, t) {
926
- let { sense: n, objective: r, constraints: i, variables: a } = e, o = t?.relax === !0, { nameToVar: s, varToName: c, nameToConstraint: l, constrToName: u, varOrder: d } = R(e), f = [];
927
- f.push(n && n.toLowerCase() === "maximize" ? "Maximize" : "Minimize");
928
- let p = [], h = !0, g = r instanceof m ? r.linearTerms : r.terms;
929
- if (g) for (let [e, t] of g) {
930
- if (t === 0) continue;
931
- let n = c.get(e), r = t >= 0 ? h ? "" : "+ " : "- ", i = Math.abs(t), a = i === 1 ? "" : `${i} `;
932
- p.push(`${r}${a}${n}`), h = !1;
933
- }
934
- if (r instanceof m && r.quadTerms.size > 0) {
935
- let e = [], t = !0, n = Array.from(r.quadTerms.values()).filter((e) => e.coeff !== 0).map((e) => {
936
- let t = d.get(e.var1) ?? e.var1.id, n = d.get(e.var2) ?? e.var2.id, [r, i, a, o] = t <= n ? [
937
- e.var1,
938
- e.var2,
939
- t,
940
- n
941
- ] : [
942
- e.var2,
943
- e.var1,
944
- n,
945
- t
946
- ];
947
- return {
948
- firstVar: r,
949
- secondVar: i,
950
- firstIdx: a,
951
- secondIdx: o,
952
- coeff: e.coeff
953
- };
954
- }).sort((e, t) => e.firstIdx - t.firstIdx || e.secondIdx - t.secondIdx);
955
- for (let r of n) {
956
- let n = 2 * r.coeff, i = n >= 0 ? t ? "" : "+ " : "- ", a = Math.abs(n), o = a === 1 ? "" : `${a} `, s = c.get(r.firstVar), l = c.get(r.secondVar);
957
- r.firstVar === r.secondVar ? e.push(`${i}${o}${s} ^ 2`) : e.push(`${i}${o}${s} * ${l}`), t = !1;
958
- }
959
- if (e.length > 0) {
960
- let t = `[ ${e.join(" ")} ] / 2`;
961
- p.length > 0 ? p.push("+", t) : p.push(t);
962
- }
963
- }
964
- let _ = p.length > 0 ? p.join(" ") : "0";
965
- f.push(` obj: ${_}`), f.push("Subject To");
966
- for (let e of i) {
967
- let t = u.get(e), n = [], r = !0;
968
- for (let [t, i] of e.lhs.terms) {
969
- if (i === 0) continue;
970
- let e = c.get(t), a = i >= 0 ? r ? "" : "+ " : "- ", o = Math.abs(i), s = o === 1 ? "" : `${o} `;
971
- n.push(`${a}${s}${e}`), r = !1;
972
- }
973
- let i = n.length > 0 ? n.join(" ") : "0", a = e.sense === "==" ? "=" : e.sense;
974
- f.push(` ${t}: ${i} ${a} ${e.rhs}`);
975
- }
976
- f.push("Bounds");
977
- let v = [], y = [];
978
- for (let e of a) {
979
- let t = c.get(e);
980
- if (!o && e.type === "Binary") {
981
- y.push(t);
982
- continue;
983
- }
984
- !o && e.type === "Integer" && v.push(t);
985
- let n = e.lb === -Infinity, r = e.ub === Infinity;
986
- n && r ? f.push(` ${t} free`) : n && !r ? f.push(` -infinity <= ${t} <= ${e.ub}`) : !n && r ? (e.lb !== 0 || e.initialValue !== void 0) && f.push(` ${t} >= ${e.lb}`) : f.push(` ${e.lb} <= ${t} <= ${e.ub}`);
987
- }
988
- if (v.length > 0) {
989
- f.push("Generals");
990
- for (let e = 0; e < v.length; e += 10) f.push(" " + v.slice(e, e + 10).join(" "));
991
- }
992
- if (y.length > 0) {
993
- f.push("Binary");
994
- for (let e = 0; e < y.length; e += 10) f.push(" " + y.slice(e, e + 10).join(" "));
995
- }
996
- return f.push("End"), {
997
- lpString: f.join("\n") + "\n",
998
- nameToVar: s,
999
- nameToConstraint: l
1000
- };
1001
- }
1002
- //#endregion
1003
- //#region src/modeler/solvers/registry.ts
1004
- var B = Symbol.for("washi.solverRegistry"), V = Symbol.for("washi.defaultSolverFactory"), H = globalThis;
1005
- H[B] || (H[B] = /* @__PURE__ */ new Map());
1006
- var U = H[B];
1007
- function W(e, t) {
1008
- U.set(e.toLowerCase(), t);
1009
- }
1010
- function G(e) {
1011
- if (typeof e == "string") {
1012
- let t = U.get(e.toLowerCase());
1013
- if (!t) throw Error(`Solver '${e}' is not registered.`);
1014
- H[V] = t;
1015
- } else H[V] = typeof e == "function" ? e : () => e;
1016
- }
1017
- function K(e, t) {
1018
- if (!e) {
1019
- let e = H[V] || U.get("highs");
1020
- if (!e) throw Error(`No default solver available. Available solvers: ${q().join(", ")}`);
1021
- return e(t);
1022
- }
1023
- if (typeof e == "string") {
1024
- let n = e.toLowerCase(), r = U.get(n);
1025
- if (!r) throw Error(n === "glpk" ? `Solver 'glpk' not found. Available solvers: ${q().join(", ")}. Please import '@ohtaman/washi/solvers/glpk' before selecting this solver.` : n === "z3" ? `Solver 'z3' not found. Available solvers: ${q().join(", ")}. Please import '@ohtaman/washi/solvers/z3' before selecting this solver.` : `Solver '${e}' not found. Available solvers: ${q().join(", ")}`);
1026
- return r(t);
1027
- }
1028
- return e;
1029
- }
1030
- function q() {
1031
- return Array.from(U.keys());
1032
- }
1033
- //#endregion
1034
- export { c as A, g as C, i as D, n as E, o as M, l as N, r as O, _ as S, d as T, j as _, R as a, D as b, L as c, C as d, S as f, A as g, w as h, G as i, s as j, a as k, F as l, T as m, q as n, z as o, O as p, W as r, I as s, K as t, x as u, M as v, m as w, h as x, k as y };