@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.
- package/README.ja.md +60 -23
- package/README.md +59 -22
- package/README.zh-CN.md +62 -25
- package/THIRD_PARTY_LICENSES.md +34 -1
- package/dist/browser/solvers/glpk.js +289 -238
- package/dist/browser/solvers/glpk.min.js +1 -1
- package/dist/browser/solvers/highs-__vite-browser-external-Dn-83hnF.js +7 -0
- package/dist/browser/solvers/highs-browser-runtime-0Bgq5ksG.js +2021 -0
- package/dist/browser/{loader-DewlSho8.js → solvers/highs-loader-BLY3wzDH.js} +190 -149
- package/dist/browser/solvers/highs-worker.js +54 -0
- package/dist/browser/solvers/highs.js +1414 -0
- package/dist/browser/solvers/highs.wasm +0 -0
- package/dist/browser/solvers/pumpkin-_washi-pumpkin-node-runtime-CEKUDK6d.js +9 -0
- package/dist/browser/solvers/pumpkin-loader-CvdaKxf4.js +320 -0
- package/dist/browser/solvers/pumpkin-worker.js +45 -0
- package/dist/browser/solvers/pumpkin.js +1777 -0
- package/dist/browser/solvers/pumpkin.wasm +0 -0
- package/dist/browser/solvers/pumpkin.wasm.LICENSE +21 -0
- package/dist/browser/solvers/z3.js +1600 -1335
- package/dist/browser/solvers/z3.min.js +2 -2
- package/dist/browser/washi.js +3313 -1699
- package/dist/browser/washi.min.js +5 -3
- package/dist/lib/solvers/__vite-browser-external-oLchLuMx.js +7 -0
- package/dist/lib/solvers/glpk.js +174 -132
- package/dist/lib/solvers/highs-browser-runtime.js +2413 -0
- package/dist/lib/{highs-node-runtime.js → solvers/highs-node-runtime.js} +1 -1
- package/dist/lib/solvers/highs-runtime.js +16 -0
- package/dist/lib/solvers/highs-worker.js +54 -0
- package/dist/lib/solvers/highs.js +410 -0
- package/dist/lib/solvers/highs.wasm +0 -0
- package/dist/lib/{loader-BZUQNvh4.js → solvers/loader-CDpPXN_F.js} +138 -116
- package/dist/lib/solvers/loader-Ow74Fa1B.js +311 -0
- package/dist/lib/solvers/lp-writer-CLScMFbS.js +115 -0
- package/dist/lib/solvers/pumpkin-assets.js +1 -0
- package/dist/lib/solvers/pumpkin-node-runtime.js +15 -0
- package/dist/lib/solvers/pumpkin-worker-factory.js +1 -0
- package/dist/lib/solvers/pumpkin-worker.js +45 -0
- package/dist/lib/solvers/pumpkin.js +868 -0
- package/dist/lib/solvers/pumpkin.wasm +0 -0
- package/dist/lib/solvers/pumpkin.wasm.LICENSE +21 -0
- package/dist/lib/solvers/registry-CrYRRk9q.js +907 -0
- package/dist/lib/solvers/rolldown-runtime-DtPi1Y-2.js +13 -0
- package/dist/lib/solvers/types-BGqPkxwu.js +12 -0
- package/dist/lib/solvers/z3.js +489 -307
- package/dist/lib/washi.js +3905 -1410
- package/dist/types/decomposition/benders.d.ts.map +1 -1
- package/dist/types/index.d.ts +4 -5
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/modeler/boolean-expr.d.ts +137 -29
- package/dist/types/modeler/boolean-expr.d.ts.map +1 -1
- package/dist/types/modeler/constraint.d.ts +40 -4
- package/dist/types/modeler/constraint.d.ts.map +1 -1
- package/dist/types/modeler/expression.d.ts +129 -2
- package/dist/types/modeler/expression.d.ts.map +1 -1
- package/dist/types/modeler/index.d.ts +4 -6
- package/dist/types/modeler/index.d.ts.map +1 -1
- package/dist/types/modeler/linearizer.d.ts +11 -2
- package/dist/types/modeler/linearizer.d.ts.map +1 -1
- package/dist/types/modeler/lp-writer.d.ts.map +1 -1
- package/dist/types/modeler/model.d.ts +86 -126
- package/dist/types/modeler/model.d.ts.map +1 -1
- package/dist/types/modeler/parser.d.ts.map +1 -1
- package/dist/types/modeler/quad-expr.d.ts +8 -2
- package/dist/types/modeler/quad-expr.d.ts.map +1 -1
- package/dist/types/modeler/solution-validator.d.ts +12 -12
- package/dist/types/modeler/solution-validator.d.ts.map +1 -1
- package/dist/types/modeler/solvers/glpk/loader.d.ts +4 -0
- package/dist/types/modeler/solvers/glpk/loader.d.ts.map +1 -1
- package/dist/types/modeler/solvers/glpk/solver.d.ts +2 -2
- package/dist/types/modeler/solvers/glpk/solver.d.ts.map +1 -1
- package/dist/types/modeler/solvers/highs/driver.d.ts.map +1 -1
- package/dist/types/modeler/solvers/highs/loader.d.ts +8 -6
- package/dist/types/modeler/solvers/highs/loader.d.ts.map +1 -1
- package/dist/types/modeler/solvers/highs/solver.d.ts.map +1 -1
- package/dist/types/modeler/solvers/highs/worker-client.d.ts +16 -0
- package/dist/types/modeler/solvers/highs/worker-client.d.ts.map +1 -1
- package/dist/types/modeler/solvers/index.d.ts +2 -1
- package/dist/types/modeler/solvers/index.d.ts.map +1 -1
- package/dist/types/modeler/solvers/mock.d.ts +2 -3
- package/dist/types/modeler/solvers/mock.d.ts.map +1 -1
- package/dist/types/modeler/solvers/pumpkin/assets.d.ts +2 -0
- package/dist/types/modeler/solvers/pumpkin/assets.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/index.d.ts +6 -0
- package/dist/types/modeler/solvers/pumpkin/index.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/loader.d.ts +40 -0
- package/dist/types/modeler/solvers/pumpkin/loader.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/node-runtime.d.ts +3 -0
- package/dist/types/modeler/solvers/pumpkin/node-runtime.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/solver.d.ts +23 -0
- package/dist/types/modeler/solvers/pumpkin/solver.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/worker-client.d.ts +74 -0
- package/dist/types/modeler/solvers/pumpkin/worker-client.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/worker-factory.d.ts +3 -0
- package/dist/types/modeler/solvers/pumpkin/worker-factory.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/worker.d.ts +7 -0
- package/dist/types/modeler/solvers/pumpkin/worker.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin.d.ts +2 -0
- package/dist/types/modeler/solvers/pumpkin.d.ts.map +1 -0
- package/dist/types/modeler/solvers/types.d.ts +7 -9
- package/dist/types/modeler/solvers/types.d.ts.map +1 -1
- package/dist/types/modeler/solvers/z3/loader.d.ts +4 -0
- package/dist/types/modeler/solvers/z3/loader.d.ts.map +1 -1
- package/dist/types/modeler/solvers/z3/solver.d.ts +2 -15
- package/dist/types/modeler/solvers/z3/solver.d.ts.map +1 -1
- package/dist/types/modeler/types.d.ts +12 -15
- package/dist/types/modeler/types.d.ts.map +1 -1
- package/dist/types/modeler/validation.d.ts +26 -2
- package/dist/types/modeler/validation.d.ts.map +1 -1
- package/dist/types/modeler/variable.d.ts +9 -5
- package/dist/types/modeler/variable.d.ts.map +1 -1
- package/docs/examples/diet-sensitivity.md +33 -14
- package/docs/examples/ecommerce-coupons.md +17 -9
- package/docs/examples/index.md +36 -27
- package/docs/examples/knapsack.md +11 -5
- package/docs/examples/portfolio-qp.md +21 -8
- package/docs/examples/production.md +10 -5
- package/docs/examples/project-scheduling.md +119 -0
- package/docs/examples/shift-scheduling.md +29 -18
- package/docs/examples/smt-security.md +27 -11
- package/docs/examples/transportation.md +23 -13
- package/docs/examples/traveling-salesperson.md +34 -22
- package/docs/examples/unsat-core.md +27 -14
- package/docs/examples/vehicle-routing.md +74 -28
- package/docs/getting-started/ai-agents.md +190 -82
- package/docs/getting-started/coding-agents.md +177 -0
- package/docs/getting-started/cross-origin.md +27 -14
- package/docs/getting-started/index.md +49 -43
- package/docs/getting-started/runtimes.md +94 -40
- package/docs/guide/constraint-programming.md +171 -0
- package/docs/guide/constraints.md +115 -33
- package/docs/guide/decomposition.md +118 -88
- package/docs/guide/expressions.md +106 -43
- package/docs/guide/formulation-tips.md +47 -28
- package/docs/guide/objectives.md +102 -75
- package/docs/guide/unsat-core.md +62 -50
- package/docs/guide/variables.md +81 -35
- package/docs/solvers/custom-solver.md +163 -122
- package/docs/solvers/glpk.md +58 -17
- package/docs/solvers/highs.md +124 -57
- package/docs/solvers/index.md +33 -20
- package/docs/solvers/pumpkin.md +171 -0
- package/docs/solvers/z3.md +63 -25
- package/llms-full.txt +2232 -932
- package/llms.txt +144 -91
- package/package.json +55 -19
- package/skills/washi/SKILL.md +107 -34
- package/dist/browser/_washi-node-only-BvRk9kiK.js +0 -0
- package/dist/browser/browser-runtime-TLCY0hkc.js +0 -1793
- package/dist/browser/highs-worker.js +0 -39
- package/dist/browser/highs.wasm +0 -0
- package/dist/lib/_washi-node-only-BvRk9kiK.js +0 -0
- package/dist/lib/highs-browser-runtime.js +0 -2185
- package/dist/lib/highs-runtime.js +0 -2
- package/dist/lib/highs-worker.js +0 -39
- package/dist/lib/highs.wasm +0 -0
- package/dist/lib/registry-D7TZ3Wh_.js +0 -1034
- package/docs/ja/getting-started/ai-agents.md +0 -280
- package/docs/zh/getting-started/ai-agents.md +0 -280
- /package/dist/browser/{highs.wasm.LICENSE → solvers/highs.wasm.LICENSE} +0 -0
- /package/dist/lib/{highs-assets.js → solvers/highs-assets.js} +0 -0
- /package/dist/lib/{highs-worker-factory.js → solvers/highs-worker-factory.js} +0 -0
- /package/dist/lib/{highs.wasm.LICENSE → solvers/highs.wasm.LICENSE} +0 -0
|
@@ -0,0 +1,1414 @@
|
|
|
1
|
+
import { _ as e, a as t, c as n, d as r, f as i, g as a, h as o, i as s, l as c, m as l, n as u, o as d, p as f, r as p, s as m, t as h, u as g } from "./highs-loader-BLY3wzDH.js";
|
|
2
|
+
//#region src/modeler/constraint.ts
|
|
3
|
+
var _ = 0, v = class e {
|
|
4
|
+
kind = "constraint";
|
|
5
|
+
id;
|
|
6
|
+
_lhs;
|
|
7
|
+
_sense;
|
|
8
|
+
_rhs;
|
|
9
|
+
_name;
|
|
10
|
+
hasExplicitName;
|
|
11
|
+
ownerModel = null;
|
|
12
|
+
_indicatorOrigin;
|
|
13
|
+
_indicatorSnapshot;
|
|
14
|
+
_discreteVars;
|
|
15
|
+
slack = null;
|
|
16
|
+
dual = null;
|
|
17
|
+
addTerm(e, t) {
|
|
18
|
+
return t === 0 ? this : (this._indicatorOrigin = void 0, this._indicatorSnapshot = void 0, this._lhs.addTerm(e, t), w(this.ownerModel), this);
|
|
19
|
+
}
|
|
20
|
+
setTerm(e, t) {
|
|
21
|
+
return this._indicatorOrigin = void 0, this._indicatorSnapshot = void 0, this._lhs.setTerm(e, t), w(this.ownerModel), this;
|
|
22
|
+
}
|
|
23
|
+
constructor(e, t, n, r) {
|
|
24
|
+
this.id = ++_, this._lhs = e, this._sense = t, this._rhs = n;
|
|
25
|
+
let i = typeof r == "string" ? r.trim() : void 0;
|
|
26
|
+
i && i.length > 0 ? (this._name = i, this.hasExplicitName = !0) : (this._name = `c_${this.id}`, this.hasExplicitName = !1);
|
|
27
|
+
}
|
|
28
|
+
get lhs() {
|
|
29
|
+
return this._lhs;
|
|
30
|
+
}
|
|
31
|
+
set lhs(e) {
|
|
32
|
+
this._lhs !== e && (this._indicatorOrigin = void 0, this._indicatorSnapshot = void 0, this._lhs = e, w(this.ownerModel));
|
|
33
|
+
}
|
|
34
|
+
get sense() {
|
|
35
|
+
return this._sense;
|
|
36
|
+
}
|
|
37
|
+
set sense(e) {
|
|
38
|
+
if (e === "!=" && !Number.isSafeInteger(this._rhs)) throw Error(`Discrete "!=" in constraint "${this._name}" requires a safe integer RHS, received ${this._rhs}.`);
|
|
39
|
+
this._sense !== e && (this._indicatorOrigin = void 0, this._indicatorSnapshot = void 0, this._sense = e, w(this.ownerModel));
|
|
40
|
+
}
|
|
41
|
+
get rhs() {
|
|
42
|
+
return this._rhs;
|
|
43
|
+
}
|
|
44
|
+
set rhs(e) {
|
|
45
|
+
if (this._sense === "!=" && !Number.isSafeInteger(e)) throw Error(`Discrete "!=" in constraint "${this._name}" requires a safe integer RHS, received ${e}.`);
|
|
46
|
+
Object.is(this._rhs, e) || (this._indicatorOrigin = void 0, this._indicatorSnapshot = void 0, this._rhs = e, w(this.ownerModel));
|
|
47
|
+
}
|
|
48
|
+
get name() {
|
|
49
|
+
return this._name;
|
|
50
|
+
}
|
|
51
|
+
set name(e) {
|
|
52
|
+
this._name !== e && (this._name = e, w(this.ownerModel)), this.hasExplicitName = !0;
|
|
53
|
+
}
|
|
54
|
+
get _indicatorMeta() {
|
|
55
|
+
let e = this._indicatorOrigin, t = this._indicatorSnapshot;
|
|
56
|
+
if (!e || !t) return;
|
|
57
|
+
if (this._lhs !== t.lhsRef || this._sense !== t.sense || !Object.is(this._rhs, t.rhs) || !Object.is(this._lhs.constant, t.constant) || this._lhs.nonlinearTerms.length > 0 || this._lhs.terms.size !== t.terms.size) {
|
|
58
|
+
this._indicatorOrigin = void 0, this._indicatorSnapshot = void 0;
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
for (let [e, n] of t.terms) if (!this._lhs.terms.has(e) || !Object.is(this._lhs.terms.get(e), n)) {
|
|
62
|
+
this._indicatorOrigin = void 0, this._indicatorSnapshot = void 0;
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
let n = e.targetRhs - (e.targetLhs.constant ?? 0), r = 0;
|
|
66
|
+
for (let [t, n] of e.targetLhs.terms) {
|
|
67
|
+
if (n === 0) continue;
|
|
68
|
+
let i = e.targetSense === "<=" ? n > 0 ? t.ub : t.lb : n > 0 ? t.lb : t.ub;
|
|
69
|
+
if (!Number.isFinite(i) || (r += n * i, !Number.isFinite(r))) return;
|
|
70
|
+
}
|
|
71
|
+
if (!((e.targetSense === "<=" ? r - n : n - r) > e.derivedM + 1e-9)) return e;
|
|
72
|
+
}
|
|
73
|
+
_setIndicatorOrigin(e) {
|
|
74
|
+
if (!e) {
|
|
75
|
+
this._indicatorOrigin = void 0, this._indicatorSnapshot = void 0;
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
this._indicatorOrigin = e, this._indicatorSnapshot = {
|
|
79
|
+
lhsRef: this._lhs,
|
|
80
|
+
terms: new Map(this._lhs.terms),
|
|
81
|
+
constant: this._lhs.constant,
|
|
82
|
+
sense: this._sense,
|
|
83
|
+
rhs: this._rhs
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
get _requiredDiscreteVars() {
|
|
87
|
+
return this._discreteVars;
|
|
88
|
+
}
|
|
89
|
+
_setDiscreteVars(e) {
|
|
90
|
+
this._discreteVars = e;
|
|
91
|
+
}
|
|
92
|
+
_assignModelName(e, t) {
|
|
93
|
+
this._name = e, this.hasExplicitName = t;
|
|
94
|
+
}
|
|
95
|
+
and(e) {
|
|
96
|
+
return new S("and", [this, e]);
|
|
97
|
+
}
|
|
98
|
+
or(e) {
|
|
99
|
+
return new S("or", [this, e]);
|
|
100
|
+
}
|
|
101
|
+
implies(e) {
|
|
102
|
+
return new S("implies", [this, e]);
|
|
103
|
+
}
|
|
104
|
+
xor(e) {
|
|
105
|
+
return new S("xor", [this, e]);
|
|
106
|
+
}
|
|
107
|
+
not() {
|
|
108
|
+
return new S("not", [this]);
|
|
109
|
+
}
|
|
110
|
+
add(e) {
|
|
111
|
+
throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
112
|
+
}
|
|
113
|
+
sub(e) {
|
|
114
|
+
throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
115
|
+
}
|
|
116
|
+
mul(e) {
|
|
117
|
+
throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
118
|
+
}
|
|
119
|
+
div(e) {
|
|
120
|
+
throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
121
|
+
}
|
|
122
|
+
neg() {
|
|
123
|
+
throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
124
|
+
}
|
|
125
|
+
le(e, t) {
|
|
126
|
+
throw Error("Cannot compare a Constraint in (<=): expected a number, Var, or LinExpr.");
|
|
127
|
+
}
|
|
128
|
+
ge(e, t) {
|
|
129
|
+
throw Error("Cannot compare a Constraint in (>=): expected a number, Var, or LinExpr.");
|
|
130
|
+
}
|
|
131
|
+
eq(e, t) {
|
|
132
|
+
throw Error("Cannot compare a Constraint in (==): expected a number, Var, or LinExpr.");
|
|
133
|
+
}
|
|
134
|
+
lt(e, t) {
|
|
135
|
+
throw Error("Cannot compare a Constraint in (<): expected a number, Var, or LinExpr.");
|
|
136
|
+
}
|
|
137
|
+
gt(e, t) {
|
|
138
|
+
throw Error("Cannot compare a Constraint in (>): expected a number, Var, or LinExpr.");
|
|
139
|
+
}
|
|
140
|
+
ne(e, t) {
|
|
141
|
+
throw Error("Cannot compare a Constraint in (!=): expected a number, Var, or LinExpr.");
|
|
142
|
+
}
|
|
143
|
+
get value() {
|
|
144
|
+
throw Error("Cannot read numeric value of a Constraint: expected a Var, LinExpr, or QuadExpr.");
|
|
145
|
+
}
|
|
146
|
+
get constant() {
|
|
147
|
+
throw Error("Cannot read numeric constant of a Constraint: expected a LinExpr or QuadExpr.");
|
|
148
|
+
}
|
|
149
|
+
clone() {
|
|
150
|
+
let t = new e(this._lhs.clone(), this._sense, this._rhs, this.hasExplicitName ? this._name : void 0);
|
|
151
|
+
return this._discreteVars && t._setDiscreteVars(new Set(this._discreteVars)), t;
|
|
152
|
+
}
|
|
153
|
+
toString() {
|
|
154
|
+
let e = this.sense === "==" ? "=" : this.sense;
|
|
155
|
+
return `${this.name}: ${this.lhs.toString()} ${e} ${this.rhs}`;
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
function y(e) {
|
|
159
|
+
return e instanceof v || typeof e == "object" && !!e && "sense" in e && "lhs" in e && "rhs" in e;
|
|
160
|
+
}
|
|
161
|
+
//#endregion
|
|
162
|
+
//#region src/modeler/boolean-expr.ts
|
|
163
|
+
function b(e, t) {
|
|
164
|
+
if (e instanceof x || e instanceof v) return e;
|
|
165
|
+
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.`);
|
|
166
|
+
}
|
|
167
|
+
var x = class {
|
|
168
|
+
and(e) {
|
|
169
|
+
return new S("and", [this, b(e, "and")]);
|
|
170
|
+
}
|
|
171
|
+
or(e) {
|
|
172
|
+
return new S("or", [this, b(e, "or")]);
|
|
173
|
+
}
|
|
174
|
+
implies(e) {
|
|
175
|
+
return new S("implies", [this, b(e, "implies")]);
|
|
176
|
+
}
|
|
177
|
+
xor(e) {
|
|
178
|
+
return new S("xor", [this, b(e, "xor")]);
|
|
179
|
+
}
|
|
180
|
+
not() {
|
|
181
|
+
return new S("not", [this]);
|
|
182
|
+
}
|
|
183
|
+
}, S = class extends x {
|
|
184
|
+
op;
|
|
185
|
+
children;
|
|
186
|
+
constructor(e, t) {
|
|
187
|
+
super(), this.op = e, this.children = t.map((t) => b(t, e));
|
|
188
|
+
}
|
|
189
|
+
toString() {
|
|
190
|
+
return this.op === "not" ? `not(${this.children[0].toString()})` : `(${this.children.map((e) => e.toString()).join(` ${this.op} `)})`;
|
|
191
|
+
}
|
|
192
|
+
}, C = /* @__PURE__ */ new WeakMap();
|
|
193
|
+
function w(e) {
|
|
194
|
+
e && C.get(e)?.();
|
|
195
|
+
}
|
|
196
|
+
var T = 1e-6;
|
|
197
|
+
function E(e) {
|
|
198
|
+
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).`);
|
|
199
|
+
if (e.lb === Infinity) throw Error(`Variable "${e.name}" has invalid lower bound: +Infinity is not allowed as a lower bound.`);
|
|
200
|
+
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).`);
|
|
201
|
+
if (e.ub === -Infinity) throw Error(`Variable "${e.name}" has invalid upper bound: -Infinity is not allowed as an upper bound.`);
|
|
202
|
+
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}).`);
|
|
203
|
+
if (e.initialValue !== void 0) {
|
|
204
|
+
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).`);
|
|
205
|
+
let t = e.type === "Binary" ? Math.max(0, e.lb) : e.lb, n = e.type === "Binary" ? Math.min(1, e.ub) : e.ub;
|
|
206
|
+
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 ${T}).`);
|
|
207
|
+
if (e.type === "Integer" || e.type === "Binary") {
|
|
208
|
+
let r = Math.round(e.initialValue);
|
|
209
|
+
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 ${T} of an integer in [${t}, ${n}]).`);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
function ee(e) {
|
|
214
|
+
if (e.initialValue === void 0) return;
|
|
215
|
+
if (E(e), e.type === "Integer" || e.type === "Binary") {
|
|
216
|
+
let t = Math.round(e.initialValue);
|
|
217
|
+
return Object.is(t, -0) ? 0 : t;
|
|
218
|
+
}
|
|
219
|
+
let t = Math.min(e.ub, Math.max(e.lb, e.initialValue));
|
|
220
|
+
return Object.is(t, -0) ? 0 : t;
|
|
221
|
+
}
|
|
222
|
+
//#endregion
|
|
223
|
+
//#region src/modeler/variable.ts
|
|
224
|
+
var D = 0;
|
|
225
|
+
function O(e) {
|
|
226
|
+
if (e === void 0) return "Continuous";
|
|
227
|
+
if (typeof e == "string") {
|
|
228
|
+
let t = e.toLowerCase();
|
|
229
|
+
if (t === "binary" || t === "bin") return "Binary";
|
|
230
|
+
if (t === "integer" || t === "int") return "Integer";
|
|
231
|
+
if (t === "continuous" || t === "cont") return "Continuous";
|
|
232
|
+
}
|
|
233
|
+
throw Error(`Invalid variable type "${String(e)}": expected "Continuous", "Integer", or "Binary".`);
|
|
234
|
+
}
|
|
235
|
+
var k = class {
|
|
236
|
+
id;
|
|
237
|
+
_name;
|
|
238
|
+
prefix;
|
|
239
|
+
hasExplicitName;
|
|
240
|
+
ownerModel = null;
|
|
241
|
+
_type;
|
|
242
|
+
_lb;
|
|
243
|
+
_ub;
|
|
244
|
+
_initialValue;
|
|
245
|
+
value = null;
|
|
246
|
+
reducedCost = null;
|
|
247
|
+
constructor(e = {}) {
|
|
248
|
+
this.id = ++D;
|
|
249
|
+
let t = typeof e.name == "string" ? e.name.trim() : "", n = typeof e.prefix == "string" ? e.prefix.trim() : "";
|
|
250
|
+
this.prefix = n.length > 0 ? n : "x", this.hasExplicitName = t.length > 0, this._name = this.hasExplicitName ? t : `${this.prefix}_${this.id}`, this._type = O(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 && E(this);
|
|
251
|
+
}
|
|
252
|
+
get name() {
|
|
253
|
+
return this._name;
|
|
254
|
+
}
|
|
255
|
+
get type() {
|
|
256
|
+
return this._type;
|
|
257
|
+
}
|
|
258
|
+
set type(e) {
|
|
259
|
+
let t = O(e);
|
|
260
|
+
this._type !== t && (this._type = t, w(this.ownerModel));
|
|
261
|
+
}
|
|
262
|
+
get lb() {
|
|
263
|
+
return this._lb;
|
|
264
|
+
}
|
|
265
|
+
set lb(e) {
|
|
266
|
+
Object.is(this._lb, e) || (this._lb = e, w(this.ownerModel));
|
|
267
|
+
}
|
|
268
|
+
get ub() {
|
|
269
|
+
return this._ub;
|
|
270
|
+
}
|
|
271
|
+
set ub(e) {
|
|
272
|
+
Object.is(this._ub, e) || (this._ub = e, w(this.ownerModel));
|
|
273
|
+
}
|
|
274
|
+
get initialValue() {
|
|
275
|
+
return this._initialValue;
|
|
276
|
+
}
|
|
277
|
+
set initialValue(e) {
|
|
278
|
+
let t = e !== void 0 && Object.is(e, -0) ? 0 : e;
|
|
279
|
+
Object.is(this._initialValue, t) || (this._initialValue = t, w(this.ownerModel));
|
|
280
|
+
}
|
|
281
|
+
_assignModelName(e) {
|
|
282
|
+
this._name = e;
|
|
283
|
+
}
|
|
284
|
+
mul(e) {
|
|
285
|
+
return typeof e == "number" ? new P(/* @__PURE__ */ new Map([[this, 1]]), 0).mul(e) : new I(/* @__PURE__ */ new Map([[this, 1]]), /* @__PURE__ */ new Map(), 0).mul(e);
|
|
286
|
+
}
|
|
287
|
+
add(e) {
|
|
288
|
+
return e instanceof I ? e.add(this) : new P(/* @__PURE__ */ new Map([[this, 1]]), 0).add(e);
|
|
289
|
+
}
|
|
290
|
+
sub(e) {
|
|
291
|
+
return e instanceof I ? e.neg().add(this) : new P(/* @__PURE__ */ new Map([[this, 1]]), 0).sub(e);
|
|
292
|
+
}
|
|
293
|
+
neg() {
|
|
294
|
+
return this.mul(-1);
|
|
295
|
+
}
|
|
296
|
+
le(e, t) {
|
|
297
|
+
return new P(/* @__PURE__ */ new Map([[this, 1]]), 0).le(e, t);
|
|
298
|
+
}
|
|
299
|
+
ge(e, t) {
|
|
300
|
+
return new P(/* @__PURE__ */ new Map([[this, 1]]), 0).ge(e, t);
|
|
301
|
+
}
|
|
302
|
+
eq(e, t) {
|
|
303
|
+
return new P(/* @__PURE__ */ new Map([[this, 1]]), 0).eq(e, t);
|
|
304
|
+
}
|
|
305
|
+
lt(e, t) {
|
|
306
|
+
return new P(/* @__PURE__ */ new Map([[this, 1]]), 0).lt(e, t);
|
|
307
|
+
}
|
|
308
|
+
gt(e, t) {
|
|
309
|
+
return new P(/* @__PURE__ */ new Map([[this, 1]]), 0).gt(e, t);
|
|
310
|
+
}
|
|
311
|
+
ne(e, t) {
|
|
312
|
+
return new P(/* @__PURE__ */ new Map([[this, 1]]), 0).ne(e, t);
|
|
313
|
+
}
|
|
314
|
+
toString() {
|
|
315
|
+
return this.name;
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
//#endregion
|
|
319
|
+
//#region src/modeler/expression.ts
|
|
320
|
+
function A(e) {
|
|
321
|
+
switch (e.kind) {
|
|
322
|
+
case "abs": return {
|
|
323
|
+
kind: "abs",
|
|
324
|
+
arg: e.arg.clone()
|
|
325
|
+
};
|
|
326
|
+
case "max": return {
|
|
327
|
+
kind: "max",
|
|
328
|
+
args: e.args.map((e) => e.clone())
|
|
329
|
+
};
|
|
330
|
+
case "min": return {
|
|
331
|
+
kind: "min",
|
|
332
|
+
args: e.args.map((e) => e.clone())
|
|
333
|
+
};
|
|
334
|
+
case "at": return {
|
|
335
|
+
kind: "at",
|
|
336
|
+
array: e.array.map((e) => e.clone()),
|
|
337
|
+
index: e.index.clone(),
|
|
338
|
+
discreteVars: new Set(e.discreteVars)
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
function j(e) {
|
|
343
|
+
switch (e.kind) {
|
|
344
|
+
case "abs": return `abs(${e.arg.toString()})`;
|
|
345
|
+
case "max": return `max(${e.args.map((e) => e.toString()).join(", ")})`;
|
|
346
|
+
case "min": return `min(${e.args.map((e) => e.toString()).join(", ")})`;
|
|
347
|
+
case "at": return `at([${e.array.map((e) => e.toString()).join(", ")}], ${e.index.toString()})`;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
function M(e) {
|
|
351
|
+
switch (e.kind) {
|
|
352
|
+
case "abs": {
|
|
353
|
+
let t = e.arg.value;
|
|
354
|
+
return t === null ? null : Math.abs(t);
|
|
355
|
+
}
|
|
356
|
+
case "max": {
|
|
357
|
+
let t = -Infinity;
|
|
358
|
+
for (let n of e.args) {
|
|
359
|
+
let e = n.value;
|
|
360
|
+
if (e === null) return null;
|
|
361
|
+
e > t && (t = e);
|
|
362
|
+
}
|
|
363
|
+
return t;
|
|
364
|
+
}
|
|
365
|
+
case "min": {
|
|
366
|
+
let t = Infinity;
|
|
367
|
+
for (let n of e.args) {
|
|
368
|
+
let e = n.value;
|
|
369
|
+
if (e === null) return null;
|
|
370
|
+
e < t && (t = e);
|
|
371
|
+
}
|
|
372
|
+
return t;
|
|
373
|
+
}
|
|
374
|
+
case "at": {
|
|
375
|
+
let t = e.index.value;
|
|
376
|
+
if (t === null || !Number.isFinite(t)) return null;
|
|
377
|
+
let n = Math.round(t);
|
|
378
|
+
return Math.abs(t - n) > 1e-6 || n < 0 || n >= e.array.length ? null : e.array[n].value;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
function te(e) {
|
|
383
|
+
return e instanceof k || typeof e == "object" && !!e && !y(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";
|
|
384
|
+
}
|
|
385
|
+
function N(e, t, n = /* @__PURE__ */ new Set()) {
|
|
386
|
+
if (typeof e == "number") {
|
|
387
|
+
if (!Number.isSafeInteger(e)) throw Error(`${t} requires Integer or Binary variables and integer constants/coefficients, but received constant ${e}.`);
|
|
388
|
+
return n;
|
|
389
|
+
}
|
|
390
|
+
if (y(e)) throw Error(`${t} cannot accept a Constraint comparison.`);
|
|
391
|
+
if (e instanceof I || "quadTerms" in e && e.quadTerms instanceof Map && "linearTerms" in e && e.linearTerms instanceof Map && typeof e.constant == "number") {
|
|
392
|
+
let r = e;
|
|
393
|
+
if (r.quadTerms.size > 0) throw Error(`${t} does not support quadratic expressions.`);
|
|
394
|
+
return N(new P(r.linearTerms, r.constant, r.nonlinearTerms ?? []), t, n);
|
|
395
|
+
}
|
|
396
|
+
if (te(e)) {
|
|
397
|
+
if (e.type !== "Integer" && e.type !== "Binary") throw Error(`${t} requires Integer or Binary variables, but involves Continuous variable "${e.name}".`);
|
|
398
|
+
return n.add(e), n;
|
|
399
|
+
}
|
|
400
|
+
if ("terms" in e && e.terms instanceof Map) {
|
|
401
|
+
let r = e, i = r.constant ?? 0;
|
|
402
|
+
if (!Number.isSafeInteger(i)) throw Error(`${t} requires Integer or Binary variables and integer constants/coefficients, but received constant ${i}.`);
|
|
403
|
+
for (let [e, i] of r.terms) if (i !== 0) {
|
|
404
|
+
if (e.type !== "Integer" && e.type !== "Binary") throw Error(`${t} requires Integer or Binary variables, but involves Continuous variable "${e.name}".`);
|
|
405
|
+
if (!Number.isSafeInteger(i)) throw Error(`${t} requires integer coefficients, but variable "${e.name}" has coefficient ${i}.`);
|
|
406
|
+
n.add(e);
|
|
407
|
+
}
|
|
408
|
+
for (let e of r.nonlinearTerms ?? []) {
|
|
409
|
+
if (e.coeff === 0) continue;
|
|
410
|
+
if (!Number.isSafeInteger(e.coeff)) throw Error(`${t} requires integer coefficients on nonlinear expressions, received ${e.coeff}.`);
|
|
411
|
+
let r = e.atom;
|
|
412
|
+
if (r.kind === "abs") N(r.arg, t, n);
|
|
413
|
+
else if (r.kind === "max" || r.kind === "min") for (let e of r.args) N(e, t, n);
|
|
414
|
+
else if (r.kind === "at") {
|
|
415
|
+
N(r.index, t, n);
|
|
416
|
+
for (let e of r.array) N(e, t, n);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return n;
|
|
420
|
+
}
|
|
421
|
+
throw Error(`${t} expected an integer-valued scalar expression.`);
|
|
422
|
+
}
|
|
423
|
+
var P = class e {
|
|
424
|
+
kind = "linear";
|
|
425
|
+
terms;
|
|
426
|
+
constant;
|
|
427
|
+
nonlinearTerms;
|
|
428
|
+
constructor(e = /* @__PURE__ */ new Map(), t = 0, n = []) {
|
|
429
|
+
this.terms = new Map(e), this.constant = t, this.nonlinearTerms = n.filter((e) => e.coeff !== 0).map((e) => ({
|
|
430
|
+
atom: A(e.atom),
|
|
431
|
+
coeff: e.coeff
|
|
432
|
+
}));
|
|
433
|
+
}
|
|
434
|
+
hasNonlinearTerms() {
|
|
435
|
+
return this.nonlinearTerms.length > 0;
|
|
436
|
+
}
|
|
437
|
+
setTerm(e, t) {
|
|
438
|
+
return t === 0 ? this.terms.delete(e) : this.terms.set(e, t), this;
|
|
439
|
+
}
|
|
440
|
+
addTerm(e, t) {
|
|
441
|
+
let n = (this.terms.get(e) ?? 0) + t;
|
|
442
|
+
return n === 0 ? this.terms.delete(e) : this.terms.set(e, n), this;
|
|
443
|
+
}
|
|
444
|
+
clone() {
|
|
445
|
+
return new e(this.terms, this.constant, this.nonlinearTerms);
|
|
446
|
+
}
|
|
447
|
+
add(e) {
|
|
448
|
+
if (y(e)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
449
|
+
if (e instanceof I) return e.add(this);
|
|
450
|
+
let t = this.clone();
|
|
451
|
+
if (typeof e == "number") t.constant += e;
|
|
452
|
+
else if ("terms" in e) {
|
|
453
|
+
for (let [n, r] of e.terms) {
|
|
454
|
+
let e = (t.terms.get(n) ?? 0) + r;
|
|
455
|
+
e === 0 ? t.terms.delete(n) : t.terms.set(n, e);
|
|
456
|
+
}
|
|
457
|
+
if (t.constant += e.constant, Array.isArray(e.nonlinearTerms)) for (let n of e.nonlinearTerms) n.coeff !== 0 && t.nonlinearTerms.push({
|
|
458
|
+
atom: A(n.atom),
|
|
459
|
+
coeff: n.coeff
|
|
460
|
+
});
|
|
461
|
+
} else {
|
|
462
|
+
let n = (t.terms.get(e) ?? 0) + 1;
|
|
463
|
+
n === 0 ? t.terms.delete(e) : t.terms.set(e, n);
|
|
464
|
+
}
|
|
465
|
+
return t;
|
|
466
|
+
}
|
|
467
|
+
sub(e) {
|
|
468
|
+
if (y(e)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
469
|
+
return e instanceof I ? e.neg().add(this) : typeof e == "number" ? this.add(-e) : this.add(e.mul(-1));
|
|
470
|
+
}
|
|
471
|
+
mul(t) {
|
|
472
|
+
if (y(t)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
473
|
+
if (typeof t == "number") {
|
|
474
|
+
let n = this.terms.size > 0 || this.nonlinearTerms.length > 0, r = this.constant === 0 && n ? 0 : this.constant * t;
|
|
475
|
+
if (this.constant !== 0 && t !== 0 && r === 0) throw Error("Constant multiplication underflows to zero.");
|
|
476
|
+
let i = new e(/* @__PURE__ */ new Map(), r);
|
|
477
|
+
for (let [e, n] of this.terms) {
|
|
478
|
+
let r = n * t;
|
|
479
|
+
if (n !== 0 && t !== 0 && r === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
480
|
+
r !== 0 && i.terms.set(e, r);
|
|
481
|
+
}
|
|
482
|
+
if (t !== 0) for (let e of this.nonlinearTerms) {
|
|
483
|
+
let n = e.coeff * t;
|
|
484
|
+
if (e.coeff !== 0 && n === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
485
|
+
n !== 0 && i.nonlinearTerms.push({
|
|
486
|
+
atom: A(e.atom),
|
|
487
|
+
coeff: n
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
return i;
|
|
491
|
+
}
|
|
492
|
+
if (t instanceof k || typeof t == "object" && t && "id" in t && !("terms" in t) && !("quadTerms" in t)) {
|
|
493
|
+
if (this.hasNonlinearTerms()) throw Error("Cannot multiply nonlinear expression (max, min, abs, or at) by a variable expression: only scalar multiplication is supported.");
|
|
494
|
+
return I.multiplyLinearTerms(this.constant, this.terms, 0, /* @__PURE__ */ new Map([[t, 1]]));
|
|
495
|
+
}
|
|
496
|
+
if (t instanceof I) {
|
|
497
|
+
let e = t.linearTerms.size > 0 || t.quadTerms.size > 0 || t.hasNonlinearTerms();
|
|
498
|
+
if (this.hasNonlinearTerms()) {
|
|
499
|
+
if (!e) return this.mul(t.constant);
|
|
500
|
+
throw Error("Cannot multiply nonlinear expression (max, min, abs, or at) by a variable expression: only scalar multiplication is supported.");
|
|
501
|
+
}
|
|
502
|
+
if (t.hasNonlinearTerms()) {
|
|
503
|
+
if (this.terms.size === 0) return t.mul(this.constant);
|
|
504
|
+
throw Error("Cannot multiply nonlinear expression (max, min, abs, or at) by a variable expression: only scalar multiplication is supported.");
|
|
505
|
+
}
|
|
506
|
+
return this.terms.size === 0 ? t.mul(this.constant) : new I(this.terms, /* @__PURE__ */ new Map(), this.constant).mul(t);
|
|
507
|
+
}
|
|
508
|
+
let n = Array.isArray(t.nonlinearTerms) && t.nonlinearTerms.length > 0;
|
|
509
|
+
if (this.hasNonlinearTerms()) {
|
|
510
|
+
if (t.terms.size === 0 && !n) return this.mul(t.constant);
|
|
511
|
+
throw Error("Cannot multiply nonlinear expression (max, min, abs, or at) by a variable expression: only scalar multiplication is supported.");
|
|
512
|
+
}
|
|
513
|
+
if (n) {
|
|
514
|
+
if (this.terms.size === 0) return t.mul(this.constant);
|
|
515
|
+
throw Error("Cannot multiply nonlinear expression (max, min, abs, or at) by a variable expression: only scalar multiplication is supported.");
|
|
516
|
+
}
|
|
517
|
+
return this.terms.size === 0 ? t.mul(this.constant) : t.terms.size === 0 ? this.mul(t.constant) : I.multiplyLinearTerms(this.constant, this.terms, t.constant, t.terms);
|
|
518
|
+
}
|
|
519
|
+
div(e) {
|
|
520
|
+
let t = I.extractScalarDivisor(e, "LinExpr.div()");
|
|
521
|
+
return this.mul(1 / t);
|
|
522
|
+
}
|
|
523
|
+
neg() {
|
|
524
|
+
return this.mul(-1);
|
|
525
|
+
}
|
|
526
|
+
get value() {
|
|
527
|
+
let e = this.constant;
|
|
528
|
+
for (let [t, n] of this.terms) {
|
|
529
|
+
if (t.value === null) return null;
|
|
530
|
+
e += n * t.value;
|
|
531
|
+
}
|
|
532
|
+
for (let t of this.nonlinearTerms) {
|
|
533
|
+
let n = M(t.atom);
|
|
534
|
+
if (n === null) return null;
|
|
535
|
+
e += t.coeff * n;
|
|
536
|
+
}
|
|
537
|
+
return e;
|
|
538
|
+
}
|
|
539
|
+
le(e, t) {
|
|
540
|
+
return this.toConstraint("<=", e, t);
|
|
541
|
+
}
|
|
542
|
+
ge(e, t) {
|
|
543
|
+
return this.toConstraint(">=", e, t);
|
|
544
|
+
}
|
|
545
|
+
eq(e, t) {
|
|
546
|
+
return this.toConstraint("==", e, t);
|
|
547
|
+
}
|
|
548
|
+
lt(e, t) {
|
|
549
|
+
return this.toDiscreteComparison("<", e, t);
|
|
550
|
+
}
|
|
551
|
+
gt(e, t) {
|
|
552
|
+
return this.toDiscreteComparison(">", e, t);
|
|
553
|
+
}
|
|
554
|
+
ne(e, t) {
|
|
555
|
+
return this.toDiscreteComparison("!=", e, t);
|
|
556
|
+
}
|
|
557
|
+
toDiscreteComparison(e, t, n) {
|
|
558
|
+
if (t instanceof v || typeof t == "object" && t && "sense" in t && "lhs" in t) throw Error(`Cannot compare against a Constraint in (${e}): expected a finite integer, discrete Var, or discrete LinExpr.`);
|
|
559
|
+
let r = `discrete ${e}`, i = /* @__PURE__ */ new Set();
|
|
560
|
+
N(this, r, i), N(t, r, i);
|
|
561
|
+
let a = this.toConstraint(e === "<" ? "<=" : e === ">" ? ">=" : "!=", t, n);
|
|
562
|
+
if (e === "<") {
|
|
563
|
+
let e = a.rhs - 1;
|
|
564
|
+
if (!Number.isSafeInteger(e)) throw Error(`Discrete comparison (<) adjusted right-hand side (${e}) exceeds safe integer range.`);
|
|
565
|
+
a.rhs = e;
|
|
566
|
+
} else if (e === ">") {
|
|
567
|
+
let e = a.rhs + 1;
|
|
568
|
+
if (!Number.isSafeInteger(e)) throw Error(`Discrete comparison (>) adjusted right-hand side (${e}) exceeds safe integer range.`);
|
|
569
|
+
a.rhs = e;
|
|
570
|
+
} else if (!Number.isSafeInteger(a.rhs)) throw Error(`Discrete comparison (!=) adjusted right-hand side (${a.rhs}) exceeds safe integer range.`);
|
|
571
|
+
return a._setDiscreteVars(i), a;
|
|
572
|
+
}
|
|
573
|
+
toConstraint(t, n, r) {
|
|
574
|
+
let i = this.clone(), a = 0;
|
|
575
|
+
if (typeof n == "number") a = n;
|
|
576
|
+
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.`);
|
|
577
|
+
else if (n instanceof I || typeof n == "object" && n && "quadTerms" in n) {
|
|
578
|
+
let r = n;
|
|
579
|
+
if (r.quadTerms.size > 0) throw Error(`Quadratic expressions are not supported in linear constraints (${t}): quadratic terms are only allowed in objectives.`);
|
|
580
|
+
i = i.sub(new e(r.linearTerms, 0, r.nonlinearTerms ?? [])), a = r.constant;
|
|
581
|
+
} else "terms" in n ? (i = i.sub(new e(n.terms, 0, n.nonlinearTerms ?? [])), a = n.constant) : i = i.sub(n);
|
|
582
|
+
let o = a - i.constant;
|
|
583
|
+
return i.constant = 0, new v(i, t, o, r);
|
|
584
|
+
}
|
|
585
|
+
and(e) {
|
|
586
|
+
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==, !=, <, >) in logical 'and': expected a BoolVar, Constraint, or BoolOp.");
|
|
587
|
+
}
|
|
588
|
+
or(e) {
|
|
589
|
+
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==, !=, <, >) in logical 'or': expected a BoolVar, Constraint, or BoolOp.");
|
|
590
|
+
}
|
|
591
|
+
implies(e) {
|
|
592
|
+
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==, !=, <, >) in logical 'implies': expected a BoolVar, Constraint, or BoolOp.");
|
|
593
|
+
}
|
|
594
|
+
xor(e) {
|
|
595
|
+
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==, !=, <, >) in logical 'xor': expected a BoolVar, Constraint, or BoolOp.");
|
|
596
|
+
}
|
|
597
|
+
not() {
|
|
598
|
+
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==, !=, <, >) in logical 'not': expected a BoolVar, Constraint, or BoolOp.");
|
|
599
|
+
}
|
|
600
|
+
toString() {
|
|
601
|
+
let e = [], t = !0;
|
|
602
|
+
for (let [n, r] of this.terms) {
|
|
603
|
+
let i = r >= 0 ? t ? "" : "+ " : "- ", a = Math.abs(r), o = a === 1 ? "" : `${a} `;
|
|
604
|
+
e.push(`${i}${o}${n.name}`), t = !1;
|
|
605
|
+
}
|
|
606
|
+
for (let n of this.nonlinearTerms) {
|
|
607
|
+
let r = n.coeff, i = r >= 0 ? t ? "" : "+ " : "- ", a = Math.abs(r), o = a === 1 ? "" : `${a} `;
|
|
608
|
+
e.push(`${i}${o}${j(n.atom)}`), t = !1;
|
|
609
|
+
}
|
|
610
|
+
if (this.constant !== 0 || e.length === 0) {
|
|
611
|
+
let n = this.constant >= 0 ? t ? "" : "+ " : "- ";
|
|
612
|
+
e.push(`${n}${Math.abs(this.constant)}`);
|
|
613
|
+
}
|
|
614
|
+
return e.join(" ") || "0";
|
|
615
|
+
}
|
|
616
|
+
};
|
|
617
|
+
//#endregion
|
|
618
|
+
//#region src/modeler/quad-expr.ts
|
|
619
|
+
function F(e, t) {
|
|
620
|
+
let n = e.id, r = t.id;
|
|
621
|
+
return n <= r ? `${n}:${r}` : `${r}:${n}`;
|
|
622
|
+
}
|
|
623
|
+
var I = class e {
|
|
624
|
+
kind = "quadratic";
|
|
625
|
+
linearTerms;
|
|
626
|
+
quadTerms;
|
|
627
|
+
constant;
|
|
628
|
+
nonlinearTerms;
|
|
629
|
+
constructor(e = /* @__PURE__ */ new Map(), t = /* @__PURE__ */ new Map(), n = 0, r = []) {
|
|
630
|
+
this.linearTerms = new Map(e), this.quadTerms = /* @__PURE__ */ new Map();
|
|
631
|
+
for (let [e, n] of t) this.quadTerms.set(e, { ...n });
|
|
632
|
+
this.constant = n, this.nonlinearTerms = r.filter((e) => e.coeff !== 0).map((e) => ({
|
|
633
|
+
atom: A(e.atom),
|
|
634
|
+
coeff: e.coeff
|
|
635
|
+
}));
|
|
636
|
+
}
|
|
637
|
+
hasNonlinearTerms() {
|
|
638
|
+
return this.nonlinearTerms.length > 0;
|
|
639
|
+
}
|
|
640
|
+
clone() {
|
|
641
|
+
return new e(this.linearTerms, this.quadTerms, this.constant, this.nonlinearTerms);
|
|
642
|
+
}
|
|
643
|
+
getQuadCoeff(e, t) {
|
|
644
|
+
let n = F(e, t), r = this.quadTerms.get(n);
|
|
645
|
+
return r ? r.coeff : 0;
|
|
646
|
+
}
|
|
647
|
+
add(t) {
|
|
648
|
+
if (y(t)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
649
|
+
let n = this.clone();
|
|
650
|
+
if (typeof t == "number") return n.constant += t, n;
|
|
651
|
+
if (t instanceof e) {
|
|
652
|
+
for (let [e, r] of t.quadTerms) {
|
|
653
|
+
let t = n.quadTerms.get(e), i = (t ? t.coeff : 0) + r.coeff;
|
|
654
|
+
i === 0 ? n.quadTerms.delete(e) : n.quadTerms.set(e, {
|
|
655
|
+
var1: r.var1,
|
|
656
|
+
var2: r.var2,
|
|
657
|
+
coeff: i
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
for (let [e, r] of t.linearTerms) {
|
|
661
|
+
let t = (n.linearTerms.get(e) ?? 0) + r;
|
|
662
|
+
t === 0 ? n.linearTerms.delete(e) : n.linearTerms.set(e, t);
|
|
663
|
+
}
|
|
664
|
+
if (n.constant += t.constant, Array.isArray(t.nonlinearTerms)) for (let e of t.nonlinearTerms) e.coeff !== 0 && n.nonlinearTerms.push({
|
|
665
|
+
atom: A(e.atom),
|
|
666
|
+
coeff: e.coeff
|
|
667
|
+
});
|
|
668
|
+
return n;
|
|
669
|
+
}
|
|
670
|
+
if ("terms" in t && t.terms instanceof Map) {
|
|
671
|
+
for (let [e, r] of t.terms) {
|
|
672
|
+
let t = (n.linearTerms.get(e) ?? 0) + r;
|
|
673
|
+
t === 0 ? n.linearTerms.delete(e) : n.linearTerms.set(e, t);
|
|
674
|
+
}
|
|
675
|
+
if (n.constant += t.constant, Array.isArray(t.nonlinearTerms)) for (let e of t.nonlinearTerms) e.coeff !== 0 && n.nonlinearTerms.push({
|
|
676
|
+
atom: A(e.atom),
|
|
677
|
+
coeff: e.coeff
|
|
678
|
+
});
|
|
679
|
+
return n;
|
|
680
|
+
}
|
|
681
|
+
let r = t, i = (n.linearTerms.get(r) ?? 0) + 1;
|
|
682
|
+
return i === 0 ? n.linearTerms.delete(r) : n.linearTerms.set(r, i), n;
|
|
683
|
+
}
|
|
684
|
+
sub(e) {
|
|
685
|
+
if (y(e)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
686
|
+
return typeof e == "number" ? this.add(-e) : this.add(e.mul(-1));
|
|
687
|
+
}
|
|
688
|
+
mul(t) {
|
|
689
|
+
if (y(t)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
690
|
+
if (typeof t == "number") {
|
|
691
|
+
let n = this.linearTerms.size > 0 || this.quadTerms.size > 0 || this.nonlinearTerms.length > 0, r = this.constant === 0 && n ? 0 : this.constant * t;
|
|
692
|
+
if (this.constant !== 0 && t !== 0 && r === 0) throw Error("Constant multiplication underflows to zero.");
|
|
693
|
+
let i = new e(/* @__PURE__ */ new Map(), /* @__PURE__ */ new Map(), r);
|
|
694
|
+
for (let [e, n] of this.linearTerms) {
|
|
695
|
+
let r = n * t;
|
|
696
|
+
if (n !== 0 && t !== 0 && r === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
697
|
+
r !== 0 && i.linearTerms.set(e, r);
|
|
698
|
+
}
|
|
699
|
+
for (let [e, n] of this.quadTerms) {
|
|
700
|
+
let r = n.coeff * t;
|
|
701
|
+
if (n.coeff !== 0 && t !== 0 && r === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
702
|
+
r !== 0 && i.quadTerms.set(e, {
|
|
703
|
+
var1: n.var1,
|
|
704
|
+
var2: n.var2,
|
|
705
|
+
coeff: r
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
if (t !== 0) for (let e of this.nonlinearTerms) {
|
|
709
|
+
let n = e.coeff * t;
|
|
710
|
+
if (e.coeff !== 0 && n === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
711
|
+
n !== 0 && i.nonlinearTerms.push({
|
|
712
|
+
atom: A(e.atom),
|
|
713
|
+
coeff: n
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
return i;
|
|
717
|
+
}
|
|
718
|
+
let n = "id" in t && "name" in t && !("terms" in t) && !("quadTerms" in t), r = "quadTerms" in t ? t.quadTerms.size : 0, i = !n && "nonlinearTerms" in t && Array.isArray(t.nonlinearTerms) ? t.nonlinearTerms.length : 0, a = n ? /* @__PURE__ */ new Map([[t, 1]]) : "quadTerms" in t ? t.linearTerms : t.terms, o = n ? 0 : t.constant;
|
|
719
|
+
if (this.nonlinearTerms.length > 0) {
|
|
720
|
+
if (!n && r === 0 && a.size === 0 && i === 0) return this.mul(o);
|
|
721
|
+
throw Error("Cannot multiply nonlinear expression (max, min, abs, or at) by a variable expression: only scalar multiplication is supported.");
|
|
722
|
+
}
|
|
723
|
+
if (i > 0) {
|
|
724
|
+
if (this.quadTerms.size === 0 && this.linearTerms.size === 0) {
|
|
725
|
+
let n = t.mul(this.constant);
|
|
726
|
+
return "quadTerms" in n ? n : new e(n.terms, /* @__PURE__ */ new Map(), n.constant, n.nonlinearTerms);
|
|
727
|
+
}
|
|
728
|
+
throw Error("Cannot multiply nonlinear expression (max, min, abs, or at) by a variable expression: only scalar multiplication is supported.");
|
|
729
|
+
}
|
|
730
|
+
if (this.quadTerms.size > 0 && (r > 0 || a.size > 0) || r > 0 && this.linearTerms.size > 0) throw Error("Multiplication creates a non-linear term of degree > 2 (not allowed in MILP/LP/QP)");
|
|
731
|
+
if (r === 0 && a.size === 0) return this.mul(o);
|
|
732
|
+
if (this.quadTerms.size === 0 && this.linearTerms.size === 0 && !n) {
|
|
733
|
+
let n = t.mul(this.constant);
|
|
734
|
+
return "quadTerms" in n ? n : new e(n.terms, /* @__PURE__ */ new Map(), n.constant, n.nonlinearTerms);
|
|
735
|
+
}
|
|
736
|
+
return e.multiplyLinearTerms(this.constant, this.linearTerms, o, a);
|
|
737
|
+
}
|
|
738
|
+
div(t) {
|
|
739
|
+
let n = e.extractScalarDivisor(t, "QuadExpr.div()");
|
|
740
|
+
return this.mul(1 / n);
|
|
741
|
+
}
|
|
742
|
+
neg() {
|
|
743
|
+
return this.mul(-1);
|
|
744
|
+
}
|
|
745
|
+
get value() {
|
|
746
|
+
let e = this.constant;
|
|
747
|
+
for (let [t, n] of this.linearTerms) {
|
|
748
|
+
if (t.value === null) return null;
|
|
749
|
+
e += n * t.value;
|
|
750
|
+
}
|
|
751
|
+
for (let t of this.quadTerms.values()) {
|
|
752
|
+
if (t.var1.value === null || t.var2.value === null) return null;
|
|
753
|
+
e += t.coeff * t.var1.value * t.var2.value;
|
|
754
|
+
}
|
|
755
|
+
for (let t of this.nonlinearTerms) {
|
|
756
|
+
let n = M(t.atom);
|
|
757
|
+
if (n === null) return null;
|
|
758
|
+
e += t.coeff * n;
|
|
759
|
+
}
|
|
760
|
+
return e;
|
|
761
|
+
}
|
|
762
|
+
le(e, t) {
|
|
763
|
+
return this.toConstraint("<=", e, t);
|
|
764
|
+
}
|
|
765
|
+
ge(e, t) {
|
|
766
|
+
return this.toConstraint(">=", e, t);
|
|
767
|
+
}
|
|
768
|
+
eq(e, t) {
|
|
769
|
+
return this.toConstraint("==", e, t);
|
|
770
|
+
}
|
|
771
|
+
lt(e, t) {
|
|
772
|
+
return this.toDiscreteComparison("<", e, t);
|
|
773
|
+
}
|
|
774
|
+
gt(e, t) {
|
|
775
|
+
return this.toDiscreteComparison(">", e, t);
|
|
776
|
+
}
|
|
777
|
+
ne(e, t) {
|
|
778
|
+
return this.toDiscreteComparison("!=", e, t);
|
|
779
|
+
}
|
|
780
|
+
toDiscreteComparison(e, t, n) {
|
|
781
|
+
if (this.quadTerms.size > 0) throw Error(`Quadratic expressions are not supported in discrete comparisons (${e}): quadratic terms are only allowed in objectives.`);
|
|
782
|
+
let r = new P(this.linearTerms, this.constant, this.nonlinearTerms);
|
|
783
|
+
return e === "<" ? r.lt(t, n) : e === ">" ? r.gt(t, n) : r.ne(t, n);
|
|
784
|
+
}
|
|
785
|
+
toConstraint(e, t, n) {
|
|
786
|
+
if (this.quadTerms.size > 0) throw Error(`Quadratic expressions are not supported in linear constraints (${e}): quadratic terms are only allowed in objectives.`);
|
|
787
|
+
let r = new P(this.linearTerms, this.constant, this.nonlinearTerms);
|
|
788
|
+
return e === "<=" ? r.le(t, n) : e === ">=" ? r.ge(t, n) : r.eq(t, n);
|
|
789
|
+
}
|
|
790
|
+
and(e) {
|
|
791
|
+
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'and': expected a BoolVar, Constraint, or BoolOp.");
|
|
792
|
+
}
|
|
793
|
+
or(e) {
|
|
794
|
+
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'or': expected a BoolVar, Constraint, or BoolOp.");
|
|
795
|
+
}
|
|
796
|
+
implies(e) {
|
|
797
|
+
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'implies': expected a BoolVar, Constraint, or BoolOp.");
|
|
798
|
+
}
|
|
799
|
+
xor(e) {
|
|
800
|
+
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'xor': expected a BoolVar, Constraint, or BoolOp.");
|
|
801
|
+
}
|
|
802
|
+
not() {
|
|
803
|
+
throw Error("Cannot use an arithmetic expression without a comparison operator (<=, >=, ==) in logical 'not': expected a BoolVar, Constraint, or BoolOp.");
|
|
804
|
+
}
|
|
805
|
+
toString() {
|
|
806
|
+
let e = [], t = !0;
|
|
807
|
+
for (let n of this.quadTerms.values()) {
|
|
808
|
+
let r = n.coeff, i = r >= 0 ? t ? "" : "+ " : "- ", a = Math.abs(r), o = a === 1 ? "" : `${a} `;
|
|
809
|
+
n.var1 === n.var2 ? e.push(`${i}${o}${n.var1.name}^2`) : e.push(`${i}${o}${n.var1.name} * ${n.var2.name}`), t = !1;
|
|
810
|
+
}
|
|
811
|
+
for (let [n, r] of this.linearTerms) {
|
|
812
|
+
let i = r >= 0 ? t ? "" : "+ " : "- ", a = Math.abs(r), o = a === 1 ? "" : `${a} `;
|
|
813
|
+
e.push(`${i}${o}${n.name}`), t = !1;
|
|
814
|
+
}
|
|
815
|
+
if (this.nonlinearTerms.length > 0) {
|
|
816
|
+
let n = new P(/* @__PURE__ */ new Map(), 0, this.nonlinearTerms).toString();
|
|
817
|
+
n !== "0" && (e.push(t ? n : n.startsWith("-") ? `- ${n.slice(2)}` : `+ ${n}`), t = !1);
|
|
818
|
+
}
|
|
819
|
+
if (this.constant !== 0 || e.length === 0) {
|
|
820
|
+
let n = this.constant >= 0 ? t ? "" : "+ " : "- ";
|
|
821
|
+
e.push(`${n}${Math.abs(this.constant)}`);
|
|
822
|
+
}
|
|
823
|
+
return e.join(" ") || "0";
|
|
824
|
+
}
|
|
825
|
+
static fromProduct(t, n, r = 1) {
|
|
826
|
+
let i = new e();
|
|
827
|
+
if (r === 0) return i;
|
|
828
|
+
let a = F(t, n);
|
|
829
|
+
return i.quadTerms.set(a, {
|
|
830
|
+
var1: t.id <= n.id ? t : n,
|
|
831
|
+
var2: t.id <= n.id ? n : t,
|
|
832
|
+
coeff: r
|
|
833
|
+
}), i;
|
|
834
|
+
}
|
|
835
|
+
static multiplyLinearTerms(t, n, r, i) {
|
|
836
|
+
let a = n.size > 0 || i.size > 0, o = (t === 0 || r === 0) && a ? 0 : t * r;
|
|
837
|
+
if (t !== 0 && r !== 0 && o === 0) throw Error("Constant multiplication underflows to zero.");
|
|
838
|
+
let s = new e(/* @__PURE__ */ new Map(), /* @__PURE__ */ new Map(), o === 0 ? 0 : o);
|
|
839
|
+
if (r !== 0) for (let [e, t] of n) {
|
|
840
|
+
let n = t * r;
|
|
841
|
+
if (t !== 0 && n === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
842
|
+
n !== 0 && s.linearTerms.set(e, n);
|
|
843
|
+
}
|
|
844
|
+
if (t !== 0) for (let [e, n] of i) {
|
|
845
|
+
let r = s.linearTerms.get(e) ?? 0, i = n * t;
|
|
846
|
+
if (n !== 0 && i === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
847
|
+
let a = r + i;
|
|
848
|
+
a === 0 ? s.linearTerms.delete(e) : s.linearTerms.set(e, a);
|
|
849
|
+
}
|
|
850
|
+
for (let [t, r] of n) for (let [n, a] of i) {
|
|
851
|
+
let i = r * a;
|
|
852
|
+
if (r !== 0 && a !== 0 && i === 0) throw Error("Coefficient multiplication underflows to zero.");
|
|
853
|
+
s = s.add(e.fromProduct(t, n, i));
|
|
854
|
+
}
|
|
855
|
+
return s;
|
|
856
|
+
}
|
|
857
|
+
static extractScalarDivisor(e, t) {
|
|
858
|
+
if (y(e)) throw Error("Cannot perform arithmetic on a Constraint: expected a number, Var, LinExpr, or QuadExpr.");
|
|
859
|
+
if (typeof e == "number") {
|
|
860
|
+
if (e === 0) throw Error(`Division by zero in ${t}`);
|
|
861
|
+
return e;
|
|
862
|
+
}
|
|
863
|
+
let n = "quadTerms" in e ? e.linearTerms.size : e.terms.size, r = "quadTerms" in e ? e.quadTerms.size : 0, i = "nonlinearTerms" in e && Array.isArray(e.nonlinearTerms) ? e.nonlinearTerms.length : 0;
|
|
864
|
+
if (n > 0 || r > 0 || i > 0) throw Error("Cannot divide by variable in expression");
|
|
865
|
+
if (e.constant === 0) throw Error(`Division by zero in ${t}`);
|
|
866
|
+
return e.constant;
|
|
867
|
+
}
|
|
868
|
+
};
|
|
869
|
+
//#endregion
|
|
870
|
+
//#region src/modeler/lp-writer.ts
|
|
871
|
+
function ne(e) {
|
|
872
|
+
return typeof e == "object" && !!e && typeof e.lpString == "string" && e.nameToVar instanceof Map && e.nameToConstraint instanceof Map;
|
|
873
|
+
}
|
|
874
|
+
function re(e, t = "x", n = 0) {
|
|
875
|
+
let r = e.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
876
|
+
return /^[0-9]/.test(r) && (r = `${t}_${r}`), (!r || r.length === 0) && (r = `${t}_${n}`), r;
|
|
877
|
+
}
|
|
878
|
+
function ie(e) {
|
|
879
|
+
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();
|
|
880
|
+
for (let e = 0; e < t.length; e++) {
|
|
881
|
+
let n = t[e];
|
|
882
|
+
s.set(n, e);
|
|
883
|
+
let a = re(n.name, "x", e), o = a, l = 1;
|
|
884
|
+
for (; c.has(o);) o = `${a}_${l++}`;
|
|
885
|
+
c.add(o), i.set(n, o), r.set(o, n);
|
|
886
|
+
}
|
|
887
|
+
let l = /* @__PURE__ */ new Set();
|
|
888
|
+
for (let e = 0; e < n.length; e++) {
|
|
889
|
+
let t = n[e], r = re(t.name, "c", e + 1), i = r, s = 1;
|
|
890
|
+
for (; l.has(i);) i = `${r}_${s++}`;
|
|
891
|
+
l.add(i), o.set(t, i), a.set(i, t);
|
|
892
|
+
}
|
|
893
|
+
return {
|
|
894
|
+
nameToVar: r,
|
|
895
|
+
varToName: i,
|
|
896
|
+
nameToConstraint: a,
|
|
897
|
+
constraintToName: o,
|
|
898
|
+
constrToName: o,
|
|
899
|
+
varOrder: s
|
|
900
|
+
};
|
|
901
|
+
}
|
|
902
|
+
function ae(e, t) {
|
|
903
|
+
let { sense: n, objective: r, constraints: i, variables: a } = e;
|
|
904
|
+
if (typeof r.hasNonlinearTerms == "function" && r.hasNonlinearTerms()) throw Error("generateLpString() received an unlowered nonlinear objective (max/min/abs/at). Lower via Model.toLpString() or linearizeModelForMip() first.");
|
|
905
|
+
let o = t?.relax === !0, { nameToVar: s, varToName: c, nameToConstraint: l, constrToName: u, varOrder: d } = ie(e), f = [];
|
|
906
|
+
f.push(n && n.toLowerCase() === "maximize" ? "Maximize" : "Minimize");
|
|
907
|
+
let p = [], m = !0, h = r instanceof I ? r.linearTerms : r.terms;
|
|
908
|
+
if (h) for (let [e, t] of h) {
|
|
909
|
+
if (t === 0) continue;
|
|
910
|
+
let n = c.get(e), r = t >= 0 ? m ? "" : "+ " : "- ", i = Math.abs(t), a = i === 1 ? "" : `${i} `;
|
|
911
|
+
p.push(`${r}${a}${n}`), m = !1;
|
|
912
|
+
}
|
|
913
|
+
if (r instanceof I && r.quadTerms.size > 0) {
|
|
914
|
+
let e = [], t = !0, n = Array.from(r.quadTerms.values()).filter((e) => e.coeff !== 0).map((e) => {
|
|
915
|
+
let t = d.get(e.var1) ?? e.var1.id, n = d.get(e.var2) ?? e.var2.id, [r, i, a, o] = t <= n ? [
|
|
916
|
+
e.var1,
|
|
917
|
+
e.var2,
|
|
918
|
+
t,
|
|
919
|
+
n
|
|
920
|
+
] : [
|
|
921
|
+
e.var2,
|
|
922
|
+
e.var1,
|
|
923
|
+
n,
|
|
924
|
+
t
|
|
925
|
+
];
|
|
926
|
+
return {
|
|
927
|
+
firstVar: r,
|
|
928
|
+
secondVar: i,
|
|
929
|
+
firstIdx: a,
|
|
930
|
+
secondIdx: o,
|
|
931
|
+
coeff: e.coeff
|
|
932
|
+
};
|
|
933
|
+
}).sort((e, t) => e.firstIdx - t.firstIdx || e.secondIdx - t.secondIdx);
|
|
934
|
+
for (let r of n) {
|
|
935
|
+
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);
|
|
936
|
+
r.firstVar === r.secondVar ? e.push(`${i}${o}${s} ^ 2`) : e.push(`${i}${o}${s} * ${l}`), t = !1;
|
|
937
|
+
}
|
|
938
|
+
if (e.length > 0) {
|
|
939
|
+
let t = `[ ${e.join(" ")} ] / 2`;
|
|
940
|
+
p.length > 0 ? p.push("+", t) : p.push(t);
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
let g = p.length > 0 ? p.join(" ") : "0";
|
|
944
|
+
f.push(` obj: ${g}`), f.push("Subject To");
|
|
945
|
+
for (let e of i) {
|
|
946
|
+
if (e.sense === "!=" || e.lhs.hasNonlinearTerms()) throw Error(`generateLpString() received unlowered constraint "${e.name}" (${e.sense === "!=" ? "discrete !=" : "nonlinear max/min/abs/at"}). Lower via Model.toLpString() or linearizeModelForMip() first.`);
|
|
947
|
+
let t = u.get(e), n = [], r = !0;
|
|
948
|
+
for (let [t, i] of e.lhs.terms) {
|
|
949
|
+
if (i === 0) continue;
|
|
950
|
+
let e = c.get(t), a = i >= 0 ? r ? "" : "+ " : "- ", o = Math.abs(i), s = o === 1 ? "" : `${o} `;
|
|
951
|
+
n.push(`${a}${s}${e}`), r = !1;
|
|
952
|
+
}
|
|
953
|
+
let i = n.length > 0 ? n.join(" ") : "0", a = e.sense === "==" ? "=" : e.sense;
|
|
954
|
+
f.push(` ${t}: ${i} ${a} ${e.rhs}`);
|
|
955
|
+
}
|
|
956
|
+
f.push("Bounds");
|
|
957
|
+
let _ = [], v = [];
|
|
958
|
+
for (let e of a) {
|
|
959
|
+
let t = c.get(e);
|
|
960
|
+
if (!o && e.type === "Binary") {
|
|
961
|
+
v.push(t);
|
|
962
|
+
continue;
|
|
963
|
+
}
|
|
964
|
+
!o && e.type === "Integer" && _.push(t);
|
|
965
|
+
let n = e.lb === -Infinity, r = e.ub === Infinity;
|
|
966
|
+
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}`);
|
|
967
|
+
}
|
|
968
|
+
if (_.length > 0) {
|
|
969
|
+
f.push("Generals");
|
|
970
|
+
for (let e = 0; e < _.length; e += 10) f.push(" " + _.slice(e, e + 10).join(" "));
|
|
971
|
+
}
|
|
972
|
+
if (v.length > 0) {
|
|
973
|
+
f.push("Binary");
|
|
974
|
+
for (let e = 0; e < v.length; e += 10) f.push(" " + v.slice(e, e + 10).join(" "));
|
|
975
|
+
}
|
|
976
|
+
return f.push("End"), {
|
|
977
|
+
lpString: f.join("\n") + "\n",
|
|
978
|
+
nameToVar: s,
|
|
979
|
+
nameToConstraint: l
|
|
980
|
+
};
|
|
981
|
+
}
|
|
982
|
+
//#endregion
|
|
983
|
+
//#region \0washi-browser-worker-factory
|
|
984
|
+
function L() {
|
|
985
|
+
let e = a("highs-worker.js"), t = typeof window.origin == "string" ? window.origin : window.location?.origin;
|
|
986
|
+
if (new URL(e).origin === t) return new Worker(e, { type: "module" });
|
|
987
|
+
let n = URL.createObjectURL(new Blob(["import " + JSON.stringify(e) + ";"], { type: "text/javascript" }));
|
|
988
|
+
try {
|
|
989
|
+
let e = new Worker(n, { type: "module" }), t = () => URL.revokeObjectURL(n);
|
|
990
|
+
return e.addEventListener("message", t, { once: !0 }), e.addEventListener("error", t, { once: !0 }), e;
|
|
991
|
+
} catch (e) {
|
|
992
|
+
throw URL.revokeObjectURL(n), e;
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
//#endregion
|
|
996
|
+
//#region src/modeler/solvers/highs/worker-client.ts
|
|
997
|
+
var R = null, z = null, B = null, V = 0, H = /* @__PURE__ */ new Map();
|
|
998
|
+
function U(e, t) {
|
|
999
|
+
return e ? {
|
|
1000
|
+
Status: "Interrupted",
|
|
1001
|
+
StatusCode: 17,
|
|
1002
|
+
HasPrimalSolution: !0,
|
|
1003
|
+
IsMip: !0,
|
|
1004
|
+
ObjectiveValue: e.objectiveValue,
|
|
1005
|
+
ObjectiveOffsetApplied: !0,
|
|
1006
|
+
...t?.objectiveBound === void 0 ? e.mipDualBound === void 0 ? {} : { MipDualBound: e.mipDualBound } : { MipDualBound: t.objectiveBound },
|
|
1007
|
+
...t?.relativeGap === void 0 ? e.mipGap === void 0 ? {} : { MipGap: e.mipGap } : { MipGap: t.relativeGap },
|
|
1008
|
+
Columns: e.columns,
|
|
1009
|
+
Rows: []
|
|
1010
|
+
} : {
|
|
1011
|
+
Status: "Interrupted",
|
|
1012
|
+
StatusCode: 17,
|
|
1013
|
+
HasPrimalSolution: !1,
|
|
1014
|
+
ObjectiveValue: NaN,
|
|
1015
|
+
ObjectiveOffsetApplied: !0,
|
|
1016
|
+
...t?.objectiveBound === void 0 ? {} : { MipDualBound: t.objectiveBound },
|
|
1017
|
+
...t?.relativeGap === void 0 ? {} : { MipGap: t.relativeGap },
|
|
1018
|
+
Columns: {},
|
|
1019
|
+
Rows: []
|
|
1020
|
+
};
|
|
1021
|
+
}
|
|
1022
|
+
function W(e) {
|
|
1023
|
+
let t = H.values().next().value;
|
|
1024
|
+
if (t && !t.dispatched) {
|
|
1025
|
+
t.dispatched = !0;
|
|
1026
|
+
try {
|
|
1027
|
+
t.kind === "WARMUP" ? e.postMessage({
|
|
1028
|
+
type: "WARMUP",
|
|
1029
|
+
id: t.id,
|
|
1030
|
+
options: t.workerOptions
|
|
1031
|
+
}) : e.postMessage({
|
|
1032
|
+
type: "SOLVE",
|
|
1033
|
+
id: t.id,
|
|
1034
|
+
lpString: t.lpString,
|
|
1035
|
+
options: t.workerOptions,
|
|
1036
|
+
...t.reportProgress ? { reportProgress: !0 } : {}
|
|
1037
|
+
});
|
|
1038
|
+
} catch (n) {
|
|
1039
|
+
H.delete(t.id), t.cleanupAbort?.(), t.reject(n), W(e);
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
function G() {
|
|
1044
|
+
let e = R;
|
|
1045
|
+
e && (R = null, z = null, B = null, e.terminate());
|
|
1046
|
+
for (let e of H.values()) e.dispatched = !1;
|
|
1047
|
+
if (H.size > 0) try {
|
|
1048
|
+
let e = q();
|
|
1049
|
+
if (e) W(e);
|
|
1050
|
+
else {
|
|
1051
|
+
let e = /* @__PURE__ */ Error("[WaSHI] Failed to recreate HiGHS Worker after interruption.");
|
|
1052
|
+
for (let t of Array.from(H.values())) H.delete(t.id), t.cleanupAbort?.(), t.reject(e);
|
|
1053
|
+
}
|
|
1054
|
+
} catch (e) {
|
|
1055
|
+
for (let t of Array.from(H.values())) H.delete(t.id), t.cleanupAbort?.(), t.reject(e);
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
function K() {
|
|
1059
|
+
return typeof window < "u" && typeof Worker < "u" && typeof Worker?.prototype?.unref != "function";
|
|
1060
|
+
}
|
|
1061
|
+
function q() {
|
|
1062
|
+
if (!K()) return null;
|
|
1063
|
+
if (!R) {
|
|
1064
|
+
let e;
|
|
1065
|
+
try {
|
|
1066
|
+
e = L(), e.onmessage = (t) => {
|
|
1067
|
+
let { type: n, id: r, result: i, runTime: a, error: o, progress: s, incumbent: c } = t.data, l = H.get(r);
|
|
1068
|
+
if (l) {
|
|
1069
|
+
if (n === "SOLVE_PROGRESS") {
|
|
1070
|
+
if (l.kind === "SOLVE" && s) {
|
|
1071
|
+
l.lastProgress = s;
|
|
1072
|
+
try {
|
|
1073
|
+
l.onProgress?.(s);
|
|
1074
|
+
} catch (e) {
|
|
1075
|
+
let t = H.get(r);
|
|
1076
|
+
if (t) {
|
|
1077
|
+
let n = H.keys().next().value;
|
|
1078
|
+
H.delete(r), t.cleanupAbort?.(), n === r && G(), t.reject(e);
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
return;
|
|
1083
|
+
}
|
|
1084
|
+
if (n === "SOLVE_INCUMBENT") {
|
|
1085
|
+
l.kind === "SOLVE" && c && (l.lastIncumbent = c);
|
|
1086
|
+
return;
|
|
1087
|
+
}
|
|
1088
|
+
H.delete(r), l.cleanupAbort?.(), l.kind === "WARMUP" ? n === "WARMUP_SUCCESS" ? (R === e && (z = e, B = l.wasmUrl), l.resolve()) : l.reject(Error(o || "Worker warmup failed")) : n === "SOLVE_SUCCESS" ? (R === e && (z = e, B = l.wasmUrl), l.resolve({
|
|
1089
|
+
result: i,
|
|
1090
|
+
runTime: a
|
|
1091
|
+
})) : l.reject(Error(o || "Worker solve failed")), R === e && W(e);
|
|
1092
|
+
}
|
|
1093
|
+
};
|
|
1094
|
+
let t = (t) => {
|
|
1095
|
+
if (R === e) {
|
|
1096
|
+
R = null, z = null, B = null, e.terminate();
|
|
1097
|
+
for (let e of H.values()) e.cleanupAbort?.(), e.reject(t);
|
|
1098
|
+
H.clear();
|
|
1099
|
+
}
|
|
1100
|
+
};
|
|
1101
|
+
e.onerror = (e) => t(/* @__PURE__ */ Error(`[WaSHI] HiGHS Worker failed to load or execute. Check Worker assets, CORS and CSP. ${e?.message || ""}`)), e.onmessageerror = () => t(/* @__PURE__ */ Error("[WaSHI] Could not deserialize the HiGHS Worker response.")), R = e;
|
|
1102
|
+
} catch (e) {
|
|
1103
|
+
throw R = null, e;
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
return R;
|
|
1107
|
+
}
|
|
1108
|
+
function J(e) {
|
|
1109
|
+
let t = typeof window < "u" ? typeof window.origin == "string" ? window.origin : window.location?.origin : void 0;
|
|
1110
|
+
return e.useWorker ?? (typeof window < "u" && t !== "null" && !t?.startsWith("file:"));
|
|
1111
|
+
}
|
|
1112
|
+
function Y(e) {
|
|
1113
|
+
if (e.useWorker === !0 && !K()) throw Error("[WaSHI] Web Worker execution was requested (useWorker: true), but browser Web Workers are unavailable in this environment.");
|
|
1114
|
+
return J(e) ? q() : null;
|
|
1115
|
+
}
|
|
1116
|
+
function oe(e, t) {
|
|
1117
|
+
let { signal: n, onProgress: r, onIncumbent: i, ...a } = e, o = {};
|
|
1118
|
+
for (let [e, t] of Object.entries(a)) {
|
|
1119
|
+
if (typeof t == "function") throw Error(`[WaSHI] HiGHS Worker option "${e}" cannot be a function (functions cannot be cloned across Web Worker boundaries).`);
|
|
1120
|
+
o[e] = t;
|
|
1121
|
+
}
|
|
1122
|
+
return o.wasmUrl = t, o;
|
|
1123
|
+
}
|
|
1124
|
+
async function se(e = {}) {
|
|
1125
|
+
if (e.useWorker === !0 && !K()) return !1;
|
|
1126
|
+
if (!J(e) || !K()) return p(e);
|
|
1127
|
+
if (!R || !z || z !== R) return !1;
|
|
1128
|
+
let n = await t(e.wasmUrl);
|
|
1129
|
+
return B === n;
|
|
1130
|
+
}
|
|
1131
|
+
function ce(e, t, n) {
|
|
1132
|
+
if (!e) return;
|
|
1133
|
+
let r = () => {
|
|
1134
|
+
let e = H.get(t.id);
|
|
1135
|
+
if (!e) return;
|
|
1136
|
+
let r = H.keys().next().value;
|
|
1137
|
+
H.delete(t.id), e.cleanupAbort?.(), r === t.id && G(), n();
|
|
1138
|
+
};
|
|
1139
|
+
e.addEventListener("abort", r, { once: !0 }), t.cleanupAbort = () => e.removeEventListener("abort", r);
|
|
1140
|
+
}
|
|
1141
|
+
async function le(e = {}) {
|
|
1142
|
+
n(e);
|
|
1143
|
+
let { signal: r } = e;
|
|
1144
|
+
if (r?.aborted) return { cached: !1 };
|
|
1145
|
+
let i = await t(e.wasmUrl);
|
|
1146
|
+
if (r?.aborted) return { cached: !1 };
|
|
1147
|
+
let a = Y(e);
|
|
1148
|
+
if (!a) {
|
|
1149
|
+
let t = await p(e);
|
|
1150
|
+
return t || r?.aborted ? { cached: t } : r ? (await new Promise((t, n) => {
|
|
1151
|
+
let i = !1, a = () => {
|
|
1152
|
+
i || (i = !0, t());
|
|
1153
|
+
};
|
|
1154
|
+
r.addEventListener("abort", a, { once: !0 }), h(e).then(() => {
|
|
1155
|
+
r.removeEventListener("abort", a), i || (i = !0, t());
|
|
1156
|
+
}, (e) => {
|
|
1157
|
+
r.removeEventListener("abort", a), i || (i = !0, n(e));
|
|
1158
|
+
});
|
|
1159
|
+
}), { cached: t }) : (await h(e), { cached: t });
|
|
1160
|
+
}
|
|
1161
|
+
if (z === a && B === i) return { cached: !0 };
|
|
1162
|
+
let o = oe(e, i), s = `req_${++V}`;
|
|
1163
|
+
return await new Promise((e, t) => {
|
|
1164
|
+
let n = {
|
|
1165
|
+
kind: "WARMUP",
|
|
1166
|
+
id: s,
|
|
1167
|
+
workerOptions: o,
|
|
1168
|
+
wasmUrl: i,
|
|
1169
|
+
resolve: e,
|
|
1170
|
+
reject: t
|
|
1171
|
+
};
|
|
1172
|
+
ce(r, n, e), H.set(s, n), W(a);
|
|
1173
|
+
}), { cached: !1 };
|
|
1174
|
+
}
|
|
1175
|
+
async function ue(e, r = {}) {
|
|
1176
|
+
if (n(r), r.signal?.aborted) return {
|
|
1177
|
+
result: U(),
|
|
1178
|
+
runTime: 0
|
|
1179
|
+
};
|
|
1180
|
+
let i = await t(r.wasmUrl);
|
|
1181
|
+
if (r.signal?.aborted) return {
|
|
1182
|
+
result: U(),
|
|
1183
|
+
runTime: 0
|
|
1184
|
+
};
|
|
1185
|
+
let a = Y(r);
|
|
1186
|
+
if (!a) return m(e, r);
|
|
1187
|
+
let { signal: o, onProgress: s } = r, c = oe(r, i), l = !!(s || o), u = `req_${++V}`, d = performance.now();
|
|
1188
|
+
return new Promise((t, n) => {
|
|
1189
|
+
let r = {
|
|
1190
|
+
kind: "SOLVE",
|
|
1191
|
+
id: u,
|
|
1192
|
+
lpString: e,
|
|
1193
|
+
workerOptions: c,
|
|
1194
|
+
wasmUrl: i,
|
|
1195
|
+
reportProgress: l,
|
|
1196
|
+
startTime: d,
|
|
1197
|
+
onProgress: s,
|
|
1198
|
+
resolve: t,
|
|
1199
|
+
reject: n
|
|
1200
|
+
};
|
|
1201
|
+
ce(o, r, () => {
|
|
1202
|
+
let e = (performance.now() - r.startTime) / 1e3;
|
|
1203
|
+
r.resolve({
|
|
1204
|
+
result: U(r.lastIncumbent, r.lastProgress),
|
|
1205
|
+
runTime: e
|
|
1206
|
+
});
|
|
1207
|
+
}), H.set(u, r), W(a);
|
|
1208
|
+
});
|
|
1209
|
+
}
|
|
1210
|
+
//#endregion
|
|
1211
|
+
//#region src/modeler/solvers/registry.ts
|
|
1212
|
+
var X = Symbol.for("washi.solverRegistry"), de = Symbol.for("washi.defaultSolverFactory"), Z = globalThis;
|
|
1213
|
+
Z[X] || (Z[X] = /* @__PURE__ */ new Map());
|
|
1214
|
+
var fe = Z[X];
|
|
1215
|
+
function pe(e, t) {
|
|
1216
|
+
fe.set(e.toLowerCase(), t);
|
|
1217
|
+
}
|
|
1218
|
+
function me(e) {
|
|
1219
|
+
if (typeof e == "string") {
|
|
1220
|
+
let t = fe.get(e.toLowerCase());
|
|
1221
|
+
if (!t) throw Error(`Solver '${e}' is not registered.`);
|
|
1222
|
+
Z[de] = t;
|
|
1223
|
+
} else Z[de] = typeof e == "function" ? e : () => e;
|
|
1224
|
+
}
|
|
1225
|
+
//#endregion
|
|
1226
|
+
//#region src/modeler/solvers/highs/solver.ts
|
|
1227
|
+
function Q(e, t) {
|
|
1228
|
+
if (typeof t == "number") switch (t) {
|
|
1229
|
+
case 0: return "NotSolved";
|
|
1230
|
+
case 1:
|
|
1231
|
+
case 2:
|
|
1232
|
+
case 3:
|
|
1233
|
+
case 4:
|
|
1234
|
+
case 5: return "Error";
|
|
1235
|
+
case 6: return "Unknown";
|
|
1236
|
+
case 7: return "Optimal";
|
|
1237
|
+
case 8: return "Infeasible";
|
|
1238
|
+
case 9: return "InfeasibleOrUnbounded";
|
|
1239
|
+
case 10: return "Unbounded";
|
|
1240
|
+
case 11: return "ObjectiveBound";
|
|
1241
|
+
case 12: return "ObjectiveTarget";
|
|
1242
|
+
case 13: return "TimeLimit";
|
|
1243
|
+
case 14: return "IterationLimit";
|
|
1244
|
+
case 15: return "Unknown";
|
|
1245
|
+
case 16: return "SolutionLimit";
|
|
1246
|
+
case 17: return "Interrupted";
|
|
1247
|
+
case 18: return "MemoryLimit";
|
|
1248
|
+
}
|
|
1249
|
+
if (typeof e != "string") return "Unknown";
|
|
1250
|
+
let n = e.trim();
|
|
1251
|
+
if (!n) return "Unknown";
|
|
1252
|
+
switch (n) {
|
|
1253
|
+
case "Optimal": return "Optimal";
|
|
1254
|
+
case "Infeasible": return "Infeasible";
|
|
1255
|
+
case "Unbounded": return "Unbounded";
|
|
1256
|
+
case "Primal infeasible or unbounded":
|
|
1257
|
+
case "InfeasibleOrUnbounded": return "InfeasibleOrUnbounded";
|
|
1258
|
+
case "Time limit reached":
|
|
1259
|
+
case "TimeLimit": return "TimeLimit";
|
|
1260
|
+
case "Iteration limit reached":
|
|
1261
|
+
case "IterationLimit": return "IterationLimit";
|
|
1262
|
+
case "Solution limit reached":
|
|
1263
|
+
case "SolutionLimit": return "SolutionLimit";
|
|
1264
|
+
case "Memory limit reached":
|
|
1265
|
+
case "MemoryLimit": return "MemoryLimit";
|
|
1266
|
+
case "Bound on objective reached":
|
|
1267
|
+
case "ObjectiveBound": return "ObjectiveBound";
|
|
1268
|
+
case "Target for objective reached":
|
|
1269
|
+
case "ObjectiveTarget": return "ObjectiveTarget";
|
|
1270
|
+
case "Interrupted":
|
|
1271
|
+
case "User interrupt": return "Interrupted";
|
|
1272
|
+
case "Feasible": return "Feasible";
|
|
1273
|
+
case "Not Set": return "NotSolved";
|
|
1274
|
+
case "Load error":
|
|
1275
|
+
case "Model error":
|
|
1276
|
+
case "Presolve error":
|
|
1277
|
+
case "Solve error":
|
|
1278
|
+
case "Postsolve error":
|
|
1279
|
+
case "Error": return "Error";
|
|
1280
|
+
case "Empty":
|
|
1281
|
+
case "Unknown": return "Unknown";
|
|
1282
|
+
}
|
|
1283
|
+
return "Unknown";
|
|
1284
|
+
}
|
|
1285
|
+
function he(e, t, n) {
|
|
1286
|
+
switch (Q(e, t)) {
|
|
1287
|
+
case "Optimal": return "Optimal";
|
|
1288
|
+
case "Infeasible": return "Infeasible";
|
|
1289
|
+
case "Unbounded": return "Unbounded";
|
|
1290
|
+
case "TimeLimit": return "TimeLimit";
|
|
1291
|
+
case "IterationLimit": return "IterationLimit";
|
|
1292
|
+
case "Feasible": return "Feasible";
|
|
1293
|
+
case "ObjectiveBound":
|
|
1294
|
+
case "ObjectiveTarget":
|
|
1295
|
+
case "SolutionLimit": return n === !1 ? "Unknown" : "Feasible";
|
|
1296
|
+
case "MemoryLimit":
|
|
1297
|
+
case "Interrupted": return n ? "Feasible" : "Unknown";
|
|
1298
|
+
case "NotSolved": return "NotSolved";
|
|
1299
|
+
case "Error": return "Error";
|
|
1300
|
+
default: return "Unknown";
|
|
1301
|
+
}
|
|
1302
|
+
}
|
|
1303
|
+
var $ = class e {
|
|
1304
|
+
static capabilities = {
|
|
1305
|
+
supportsMIP: !0,
|
|
1306
|
+
supportsQP: !0,
|
|
1307
|
+
supportsWarmStart: !0
|
|
1308
|
+
};
|
|
1309
|
+
name = "HiGHS";
|
|
1310
|
+
version = "1.15.1";
|
|
1311
|
+
capabilities = e.capabilities;
|
|
1312
|
+
options;
|
|
1313
|
+
constructor(e = {}) {
|
|
1314
|
+
this.options = e;
|
|
1315
|
+
}
|
|
1316
|
+
async solve(e, t = {}, r = {}) {
|
|
1317
|
+
let i = ne(t), a = i ? r : t, o = {
|
|
1318
|
+
...this.options,
|
|
1319
|
+
...a
|
|
1320
|
+
};
|
|
1321
|
+
n(o);
|
|
1322
|
+
let s = e.objective?.constant ?? 0;
|
|
1323
|
+
if (typeof s != "number" || !Number.isFinite(s)) throw Error(`Solver "${this.name}" encountered non-finite objective constant: ${String(s)}.`);
|
|
1324
|
+
let c = s === 0 ? 0 : s;
|
|
1325
|
+
for (let t of e.variables) E(t);
|
|
1326
|
+
let { lpString: l, nameToVar: u, nameToConstraint: d } = i ? t : ae(e, { relax: o.relax }), f = Object.create(null), p = !1, m = /* @__PURE__ */ new Set();
|
|
1327
|
+
for (let [e, t] of u.entries()) {
|
|
1328
|
+
m.add(t);
|
|
1329
|
+
let n = ee(t);
|
|
1330
|
+
n !== void 0 && (f[e] = n, p = !0);
|
|
1331
|
+
}
|
|
1332
|
+
for (let t of e.variables) if (!m.has(t)) throw Error(`LP export symbol map (nameToVar) is missing model variable "${t.name}".`);
|
|
1333
|
+
let h = o;
|
|
1334
|
+
(c !== 0 || p || h.initialValues !== void 0) && (h = {
|
|
1335
|
+
...h,
|
|
1336
|
+
...c === 0 ? {} : { objectiveOffset: c },
|
|
1337
|
+
...p ? { initialValues: f } : {}
|
|
1338
|
+
});
|
|
1339
|
+
let { result: g, runTime: _ } = await ue(l, h), v = g?.ObjectiveOffsetApplied ? 0 : c, y = Q(g?.Status, g?.StatusCode), b = o.relax ? !1 : typeof g?.IsMip == "boolean" ? g.IsMip : e.variables.some((e) => e.type === "Integer" || e.type === "Binary"), x = g?.HasPrimalSolution === !0, S = he(g?.Status, g?.StatusCode, x), C = null;
|
|
1340
|
+
x && typeof g?.ObjectiveValue == "number" && Number.isFinite(g.ObjectiveValue) && (C = g.ObjectiveValue + v);
|
|
1341
|
+
let w, T, D;
|
|
1342
|
+
if (typeof g?.MipDualBound == "number" && Number.isFinite(g.MipDualBound) ? w = g.MipDualBound + v : !b && y === "Optimal" && C !== null && (w = C), C !== null && w !== void 0 && Number.isFinite(w) && (D = Math.abs(C - w)), v !== 0 && b && C !== null && w !== void 0 && D !== void 0) {
|
|
1343
|
+
if (D === 0) T = 0;
|
|
1344
|
+
else if (Math.abs(C) > 0) {
|
|
1345
|
+
let e = D / Math.abs(C);
|
|
1346
|
+
T = Number.isFinite(e) ? e : void 0;
|
|
1347
|
+
} else T = void 0;
|
|
1348
|
+
} else typeof g?.MipGap == "number" && Number.isFinite(g.MipGap) ? T = g.MipGap : b && y === "Optimal" && x && (D === void 0 || D === 0) && (T = 0);
|
|
1349
|
+
if (!x) return {
|
|
1350
|
+
status: S,
|
|
1351
|
+
terminationStatus: y,
|
|
1352
|
+
hasPrimalSolution: !1,
|
|
1353
|
+
objectiveValue: null,
|
|
1354
|
+
...w === void 0 ? {} : { objectiveBound: w },
|
|
1355
|
+
...T === void 0 ? {} : { relativeGap: T },
|
|
1356
|
+
...D === void 0 ? {} : { absoluteGap: D },
|
|
1357
|
+
values: {},
|
|
1358
|
+
reducedCosts: {},
|
|
1359
|
+
duals: {},
|
|
1360
|
+
slacks: {},
|
|
1361
|
+
runTime: _,
|
|
1362
|
+
raw: g
|
|
1363
|
+
};
|
|
1364
|
+
let O = (e, t, n) => {
|
|
1365
|
+
Object.defineProperty(e, t, {
|
|
1366
|
+
value: n,
|
|
1367
|
+
enumerable: !0,
|
|
1368
|
+
writable: !0,
|
|
1369
|
+
configurable: !0
|
|
1370
|
+
});
|
|
1371
|
+
}, k = {}, A = {}, j = {}, M = {};
|
|
1372
|
+
if (g.Columns) for (let [e, t] of Object.entries(g.Columns)) {
|
|
1373
|
+
let n = u.get(e);
|
|
1374
|
+
if (n) {
|
|
1375
|
+
if (typeof t.Primal == "number" && Number.isFinite(t.Primal)) {
|
|
1376
|
+
let e = Math.abs(t.Primal) < 1e-9 ? 0 : t.Primal;
|
|
1377
|
+
!o.relax && (n.type === "Integer" || n.type === "Binary") && Math.abs(e - Math.round(e)) <= 1e-5 && (e = Math.round(e) + 0), O(k, n.name, e);
|
|
1378
|
+
}
|
|
1379
|
+
typeof t.Dual == "number" && Number.isFinite(t.Dual) && O(j, n.name, Math.abs(t.Dual) < 1e-9 ? 0 : t.Dual);
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
if (g.Rows) {
|
|
1383
|
+
let e = Array.isArray(g.Rows) ? g.Rows : Object.values(g.Rows);
|
|
1384
|
+
for (let t of e) {
|
|
1385
|
+
let e = t.Name, n = e ? d.get(e) : void 0;
|
|
1386
|
+
if (n && (typeof t.Dual == "number" && Number.isFinite(t.Dual) && O(A, n.name, Math.abs(t.Dual) < 1e-9 ? 0 : t.Dual), typeof t.Primal == "number" && Number.isFinite(t.Primal))) {
|
|
1387
|
+
let e = n.rhs - t.Primal;
|
|
1388
|
+
O(M, n.name, Math.abs(e) < 1e-9 ? 0 : e);
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1392
|
+
return {
|
|
1393
|
+
status: S,
|
|
1394
|
+
terminationStatus: y,
|
|
1395
|
+
hasPrimalSolution: !0,
|
|
1396
|
+
objectiveValue: C,
|
|
1397
|
+
...w === void 0 ? {} : { objectiveBound: w },
|
|
1398
|
+
...T === void 0 ? {} : { relativeGap: T },
|
|
1399
|
+
...D === void 0 ? {} : { absoluteGap: D },
|
|
1400
|
+
values: k,
|
|
1401
|
+
reducedCosts: j,
|
|
1402
|
+
duals: A,
|
|
1403
|
+
slacks: M,
|
|
1404
|
+
runTime: _,
|
|
1405
|
+
raw: g
|
|
1406
|
+
};
|
|
1407
|
+
}
|
|
1408
|
+
}, ge = $;
|
|
1409
|
+
function _e() {
|
|
1410
|
+
pe("highs", (e) => new $(e)), me((e) => new $(e));
|
|
1411
|
+
}
|
|
1412
|
+
_e();
|
|
1413
|
+
//#endregion
|
|
1414
|
+
export { c as HIGHS_CONSTANTS, g as HIGHS_STATUS_NAMES, ge as HiGHS, r as Highs, i as HighsError, f as HighsModel, $ as HighsSolver, l as HighsUnsupportedOptionError, o as bindHighsFunctions, L as createHighsWorker, a as getAssetUrl, e as getBundledWasmUrl, h as getHighs, u as getHighsWasmUrl, p as isHighsCached, se as isHighsWarmed, s as isValidWasmHeader, he as mapHighsStatus, Q as mapHighsTerminationStatus, _e as registerHighsSolver, t as resolveBrowserWasmUrl, d as setHighsWasmUrl, ue as solveLp, m as solveLpWithHighs, n as validateHighsOptions, le as warmupHighs };
|