@ohtaman/washi 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/README.ja.md +100 -107
  2. package/README.md +98 -105
  3. package/README.zh-CN.md +101 -108
  4. package/dist/browser/highs-worker.js +21 -3
  5. package/dist/browser/loader-CtaDHef_.js +273 -0
  6. package/dist/browser/solvers/glpk.js +433 -286
  7. package/dist/browser/solvers/glpk.min.js +1 -1
  8. package/dist/browser/solvers/z3.js +1002 -981
  9. package/dist/browser/solvers/z3.min.js +2 -2
  10. package/dist/browser/washi.js +2129 -676
  11. package/dist/browser/washi.min.js +4 -4
  12. package/dist/lib/highs-worker.js +21 -3
  13. package/dist/lib/loader-DmNC_UNk.js +279 -0
  14. package/dist/lib/registry-BEfvpZW3.js +951 -0
  15. package/dist/lib/solvers/glpk.js +201 -90
  16. package/dist/lib/solvers/z3.js +399 -259
  17. package/dist/lib/washi.js +1627 -988
  18. package/dist/types/index.d.ts +4 -4
  19. package/dist/types/index.d.ts.map +1 -1
  20. package/dist/types/modeler/big-m.d.ts +75 -0
  21. package/dist/types/modeler/big-m.d.ts.map +1 -0
  22. package/dist/types/modeler/boolean-expr.d.ts +103 -14
  23. package/dist/types/modeler/boolean-expr.d.ts.map +1 -1
  24. package/dist/types/modeler/constraint.d.ts +36 -9
  25. package/dist/types/modeler/constraint.d.ts.map +1 -1
  26. package/dist/types/modeler/expression.d.ts +113 -28
  27. package/dist/types/modeler/expression.d.ts.map +1 -1
  28. package/dist/types/modeler/index.d.ts +10 -6
  29. package/dist/types/modeler/index.d.ts.map +1 -1
  30. package/dist/types/modeler/lp-writer.d.ts +27 -2
  31. package/dist/types/modeler/lp-writer.d.ts.map +1 -1
  32. package/dist/types/modeler/model.d.ts +478 -131
  33. package/dist/types/modeler/model.d.ts.map +1 -1
  34. package/dist/types/modeler/objective.d.ts +59 -0
  35. package/dist/types/modeler/objective.d.ts.map +1 -0
  36. package/dist/types/modeler/parser.d.ts +34 -10
  37. package/dist/types/modeler/parser.d.ts.map +1 -1
  38. package/dist/types/modeler/quad-expr.d.ts +26 -7
  39. package/dist/types/modeler/quad-expr.d.ts.map +1 -1
  40. package/dist/types/modeler/solution-validator.d.ts +93 -0
  41. package/dist/types/modeler/solution-validator.d.ts.map +1 -0
  42. package/dist/types/modeler/solvers/glpk/solver.d.ts +26 -2
  43. package/dist/types/modeler/solvers/glpk/solver.d.ts.map +1 -1
  44. package/dist/types/modeler/solvers/highs/loader.d.ts +36 -11
  45. package/dist/types/modeler/solvers/highs/loader.d.ts.map +1 -1
  46. package/dist/types/modeler/solvers/highs/solver.d.ts +11 -4
  47. package/dist/types/modeler/solvers/highs/solver.d.ts.map +1 -1
  48. package/dist/types/modeler/solvers/highs/worker-client.d.ts.map +1 -1
  49. package/dist/types/modeler/solvers/mock.d.ts +14 -5
  50. package/dist/types/modeler/solvers/mock.d.ts.map +1 -1
  51. package/dist/types/modeler/solvers/types.d.ts +71 -11
  52. package/dist/types/modeler/solvers/types.d.ts.map +1 -1
  53. package/dist/types/modeler/solvers/z3/loader.d.ts +2 -3
  54. package/dist/types/modeler/solvers/z3/loader.d.ts.map +1 -1
  55. package/dist/types/modeler/solvers/z3/solver.d.ts +15 -5
  56. package/dist/types/modeler/solvers/z3/solver.d.ts.map +1 -1
  57. package/dist/types/modeler/types.d.ts +59 -1
  58. package/dist/types/modeler/types.d.ts.map +1 -1
  59. package/dist/types/modeler/validation.d.ts +56 -0
  60. package/dist/types/modeler/validation.d.ts.map +1 -0
  61. package/dist/types/modeler/variable.d.ts +48 -14
  62. package/dist/types/modeler/variable.d.ts.map +1 -1
  63. package/docs/examples/diet-sensitivity.md +90 -0
  64. package/docs/examples/ecommerce-coupons.md +106 -0
  65. package/docs/examples/index.md +53 -0
  66. package/docs/examples/knapsack.md +60 -0
  67. package/docs/examples/portfolio-qp.md +83 -0
  68. package/docs/examples/production.md +51 -0
  69. package/docs/examples/shift-scheduling.md +175 -0
  70. package/docs/examples/smt-security.md +74 -0
  71. package/docs/examples/transportation.md +90 -0
  72. package/docs/examples/unsat-core.md +64 -0
  73. package/docs/getting-started/ai-agents.md +267 -0
  74. package/docs/getting-started/cross-origin.md +87 -0
  75. package/docs/getting-started/index.md +116 -0
  76. package/docs/getting-started/runtimes.md +227 -0
  77. package/docs/guide/constraints.md +177 -0
  78. package/docs/guide/expressions.md +204 -0
  79. package/docs/guide/objectives.md +170 -0
  80. package/docs/guide/unsat-core.md +147 -0
  81. package/docs/guide/variables.md +188 -0
  82. package/docs/ja/getting-started/ai-agents.md +266 -0
  83. package/docs/solvers/custom-solver.md +525 -0
  84. package/docs/solvers/glpk.md +66 -0
  85. package/docs/solvers/highs.md +93 -0
  86. package/docs/solvers/index.md +42 -0
  87. package/docs/solvers/z3.md +78 -0
  88. package/docs/zh/getting-started/ai-agents.md +266 -0
  89. package/llms-full.txt +3249 -0
  90. package/llms.txt +197 -0
  91. package/package.json +18 -5
  92. package/skills/washi/SKILL.md +197 -0
  93. package/dist/browser/loader-CVt4CloI.js +0 -58
  94. package/dist/lib/async-mutex-Duo4jwzn.js +0 -195
  95. package/dist/lib/constraint-BRuB3ooC.js +0 -90
  96. package/dist/lib/loader-Dti0v9yC.js +0 -50
  97. package/dist/lib/registry-DksqCSjI.js +0 -32
@@ -1,114 +1,98 @@
1
- import { i as e, r as t, t as n } from "../constraint-BRuB3ooC.js";
2
- import { r } from "../registry-DksqCSjI.js";
3
- import { getBundledZ3AssetUrls as i } from "./z3-assets.js";
1
+ import { D as e, O as t, T as n, r, s as i } from "../registry-BEfvpZW3.js";
2
+ import { getBundledZ3AssetUrls as a } from "./z3-assets.js";
4
3
  //#region src/modeler/solvers/z3/loader.ts
5
- var a = null, o = null, s = null, c = null, l = /* @__PURE__ */ new Set(), u = /* @__PURE__ */ new Set(), d = /* @__PURE__ */ new Set();
6
- function f(e) {
7
- let t = e?.Semaphore || e?.default?.Semaphore;
8
- if (!t || t.__washi_hooked) return;
9
- t.__washi_hooked = !0;
10
- let n = t.prototype.acquire;
11
- t.prototype.acquire = function(...e) {
12
- return d.add(this), n.apply(this, e);
13
- };
14
- }
15
- import("../async-mutex-Duo4jwzn.js").then(f).catch(() => {});
16
- function p(e) {
17
- for (let e of Array.from(d)) try {
18
- if (typeof e.isLocked == "function" && e.isLocked()) {
19
- if (typeof e.cancel == "function") try {
20
- e.cancel();
21
- } catch {}
22
- if (typeof e.release == "function") try {
23
- e.release();
24
- } catch {}
25
- }
4
+ var o = null, s = null, c = null, l = null, u = /* @__PURE__ */ new WeakMap();
5
+ function d(e, t) {
6
+ if (e.error) return;
7
+ let n = t instanceof Error ? t : /* @__PURE__ */ Error(`[WaSHI] Z3 internal worker error: ${typeof t == "object" && t && "message" in t ? String(t.message) : String(t)}`);
8
+ e.error = n;
9
+ for (let t of e.asyncRejects) t(n);
10
+ e.asyncRejects.clear(), l === e && _();
11
+ try {
12
+ e.instance?.em?.PThread?.terminateAllThreads();
26
13
  } catch {}
14
+ e.bootstrapUrl && URL.revokeObjectURL(e.bootstrapUrl);
15
+ for (let t of Array.from(e.errorHandlers)) try {
16
+ t(n);
17
+ } catch {}
18
+ e.errorHandlers.clear();
27
19
  }
28
- function m(e) {
20
+ function f(e, t) {
29
21
  if (!e || e.__washi_async_call_hooked) return;
30
22
  e.__washi_async_call_hooked = !0;
31
- let t = e.async_call;
32
- typeof t == "function" && (e.async_call = function(e, ...n) {
33
- let r, i = new Promise((e, t) => {
34
- r = t, u.add(t);
23
+ let n = e.async_call;
24
+ typeof n == "function" && (e.async_call = function(e, ...r) {
25
+ if (t.error) return Promise.reject(t.error);
26
+ let i, a = new Promise((e, n) => {
27
+ i = n, t.asyncRejects.add(n);
35
28
  });
36
- i.catch(() => {});
29
+ a.catch(() => {});
37
30
  try {
38
- let a = t.call(this, e, ...n);
39
- return Promise.race([a, i]).finally(() => {
40
- u.delete(r);
31
+ let o = n.call(this, e, ...r);
32
+ return Promise.race([o, a]).finally(() => {
33
+ t.asyncRejects.delete(i);
41
34
  });
42
35
  } catch (e) {
43
- throw u.delete(r), g(e), e;
36
+ throw t.asyncRejects.delete(i), d(t, e), e;
44
37
  }
45
38
  });
46
39
  }
47
- function h(e) {
48
- return l.add(e), () => {
49
- l.delete(e);
50
- };
40
+ function p(e, t) {
41
+ let n = t ? u.get(t) : l;
42
+ return n ? n.error ? (e(n.error), () => {}) : (n.errorHandlers.add(e), () => {
43
+ n.errorHandlers.delete(e);
44
+ }) : () => {};
51
45
  }
52
- function g(e) {
53
- let t = e instanceof Error ? e : /* @__PURE__ */ Error(`[WaSHI] Z3 internal worker error: ${e?.message || (typeof e == "string" ? e : "Worker failed to initialize or execute")}`);
54
- for (let e of Array.from(u)) try {
55
- e(t);
56
- } catch {}
57
- if (u.clear(), p(t), o?.em?.PThread?.terminateAllThreads) try {
58
- o.em.PThread.terminateAllThreads();
59
- } catch {}
60
- y();
61
- for (let e of Array.from(l)) try {
62
- e(t);
63
- } catch {}
46
+ function m(e) {
47
+ l && d(l, e);
64
48
  }
65
- function _(e) {
49
+ function h(e, t) {
66
50
  if (!e || !e.PThread || e.PThread.__washi_monitored) return;
67
51
  e.PThread.__washi_monitored = !0;
68
- let t = (e) => {
52
+ let n = (e) => {
69
53
  e && (e.onerror = (e) => {
70
- g(e);
54
+ d(t, e);
71
55
  }, typeof e.addEventListener == "function" && e.addEventListener("error", (e) => {
72
- g(e), typeof e.preventDefault == "function" && e.preventDefault();
56
+ d(t, e), typeof e.preventDefault == "function" && e.preventDefault();
73
57
  }));
74
58
  };
75
- Array.isArray(e.PThread.unusedWorkers) && e.PThread.unusedWorkers.forEach(t), Array.isArray(e.PThread.runningWorkers) && e.PThread.runningWorkers.forEach(t);
76
- let n = e.PThread.loadWasmModuleToWorker;
77
- typeof n == "function" && (e.PThread.loadWasmModuleToWorker = function(e) {
59
+ Array.isArray(e.PThread.unusedWorkers) && e.PThread.unusedWorkers.forEach(n), Array.isArray(e.PThread.runningWorkers) && e.PThread.runningWorkers.forEach(n);
60
+ let r = e.PThread.loadWasmModuleToWorker;
61
+ typeof r == "function" && (e.PThread.loadWasmModuleToWorker = function(e) {
78
62
  try {
79
- let t = n.call(this, e);
63
+ let n = r.call(this, e);
80
64
  return e.onerror = (e) => {
81
- g(e);
82
- }, t;
65
+ d(t, e);
66
+ }, n;
83
67
  } catch (e) {
84
- throw g(e), e;
68
+ throw d(t, e), e;
85
69
  }
86
70
  });
87
- let r = e.PThread.allocateUnusedWorker;
88
- typeof r == "function" && (e.PThread.allocateUnusedWorker = function() {
71
+ let i = e.PThread.allocateUnusedWorker;
72
+ typeof i == "function" && (e.PThread.allocateUnusedWorker = function() {
89
73
  try {
90
- r.call(this);
74
+ i.call(this);
91
75
  let e = this.unusedWorkers?.[this.unusedWorkers.length - 1];
92
- e && t(e);
76
+ e && n(e);
93
77
  } catch (e) {
94
- throw g(e), e;
78
+ throw d(t, e), e;
95
79
  }
96
80
  });
97
81
  }
98
- function v(e) {
99
- e.jsUrl !== void 0 && (s = e.jsUrl), e.wasmUrl !== void 0 && (c = e.wasmUrl), y();
82
+ function g(e) {
83
+ e.jsUrl !== void 0 && (s = e.jsUrl), e.wasmUrl !== void 0 && (c = e.wasmUrl), _();
100
84
  }
101
- function y() {
102
- a = null, o = null;
85
+ function _() {
86
+ o = null, l = null;
103
87
  }
104
- function b() {
88
+ function v() {
105
89
  if (typeof window > "u") return;
106
90
  let e = window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1" || window.location.hostname === "[::1]";
107
91
  if (!window.isSecureContext && !e) throw Error(`[WaSHI] Z3 solver requires a Secure Context (HTTPS or localhost). Current protocol: "${window.location.protocol}".`);
108
92
  let t = typeof SharedArrayBuffer < "u", n = window.crossOriginIsolated === !0;
109
93
  if (!t || !n) throw Error("[WaSHI] Z3 solver requires SharedArrayBuffer and crossOriginIsolated in browser environments.\nEnsure your server sends the following delivery headers on the document response:\n Cross-Origin-Opener-Policy: same-origin\n Cross-Origin-Embedder-Policy: require-corp\n(Note: Standard CORS headers alone are not sufficient, and setting 'useWorker: false' does not remove Z3's internal multithreading requirement.)");
110
94
  }
111
- async function x(e, t) {
95
+ async function y(e, t) {
112
96
  try {
113
97
  let n = null;
114
98
  try {
@@ -125,7 +109,7 @@ async function x(e, t) {
125
109
  throw Error(`[WaSHI] Failed to load Z3 asset (${t}) from "${e}": ${n.message || String(n)}. Deploy local assets with correct MIME types; no CDN fallback is attempted.`, { cause: n });
126
110
  }
127
111
  }
128
- function S(e = {}) {
112
+ function b(e = {}) {
129
113
  if (e.z3JsUrl && e.z3WasmUrl) return {
130
114
  jsUrl: e.z3JsUrl,
131
115
  wasmUrl: e.z3WasmUrl
@@ -134,13 +118,13 @@ function S(e = {}) {
134
118
  jsUrl: e.z3JsUrl || s,
135
119
  wasmUrl: e.z3WasmUrl || c
136
120
  };
137
- let t = i();
121
+ let t = a();
138
122
  return {
139
123
  jsUrl: e.z3JsUrl || s || t.jsUrl,
140
124
  wasmUrl: e.z3WasmUrl || c || t.wasmUrl
141
125
  };
142
126
  }
143
- async function C(e) {
127
+ async function x(e) {
144
128
  let t = globalThis;
145
129
  if (t.global === void 0 && (t.global = t), typeof t.initZ3 == "function") return t.initZ3;
146
130
  if (typeof document < "u") await new Promise((n, r) => {
@@ -172,165 +156,183 @@ async function C(e) {
172
156
  if (typeof t.initZ3 != "function") throw Error(`[WaSHI] Script loaded from "${e}" did not define global initZ3.`);
173
157
  return t.initZ3;
174
158
  }
175
- async function w(e = {}) {
176
- if (a) return a;
177
- let t = typeof process < "u" && process.versions && process.versions.node && process.type !== "renderer", n = (async () => {
178
- if (t) try {
179
- let e = await import("z3-solver");
159
+ async function S(e = {}) {
160
+ if (o) return o;
161
+ let t = {
162
+ instance: null,
163
+ error: null,
164
+ errorHandlers: /* @__PURE__ */ new Set(),
165
+ asyncRejects: /* @__PURE__ */ new Set()
166
+ };
167
+ l = t;
168
+ async function n(e) {
169
+ if (t.error) throw t.error;
170
+ let n = await e();
171
+ if (t.instance = n, u.set(n, t), t.error) {
180
172
  try {
181
- f(await import("../async-mutex-Duo4jwzn.js"));
173
+ n.em?.PThread?.terminateAllThreads();
182
174
  } catch {}
183
- let t = await e.init();
184
- return o = t, t && t.em && (_(t.em), m(t.em)), t;
175
+ throw t.error;
176
+ }
177
+ return n.em && (h(n.em, t), f(n.em, t)), n;
178
+ }
179
+ let r, i = new Promise((e, t) => {
180
+ r = t;
181
+ });
182
+ t.errorHandlers.add(r);
183
+ let a = typeof process < "u" && process.versions && process.versions.node && process.type !== "renderer", s = (async () => {
184
+ if (a) try {
185
+ let e = await import("z3-solver");
186
+ return await n(() => e.init());
185
187
  } catch (e) {
186
188
  throw Error(`[WaSHI] Failed to load z3-solver. Ensure the optional dependency is installed (npm install z3-solver) and supported in this environment. Error: ${e instanceof Error ? e.message : String(e)}`, { cause: e });
187
189
  }
188
- b();
189
- let { jsUrl: n, wasmUrl: r } = S(e);
190
- await x(n, "JavaScript (z3-built.js)"), await x(r, "WebAssembly (z3-built.wasm)"), await C(n);
191
- let i = n;
192
- if (typeof window < "u" && new URL(n).origin !== window.location.origin) {
193
- let e = new Blob([`importScripts(${JSON.stringify(n)});`], { type: "application/javascript" });
194
- i = URL.createObjectURL(e);
195
- }
196
- let a = {
197
- locateFile: (e) => e.endsWith(".wasm") ? r : new URL(e, n).href,
198
- mainScriptUrlOrBlob: i
190
+ v();
191
+ let { jsUrl: r, wasmUrl: i } = b(e);
192
+ if (await y(r, "JavaScript (z3-built.js)"), await y(i, "WebAssembly (z3-built.wasm)"), await x(r), t.error) throw t.error;
193
+ let o = r;
194
+ if (typeof window < "u" && new URL(r).origin !== window.location.origin) {
195
+ let e = new Blob([`importScripts(${JSON.stringify(r)});`], { type: "application/javascript" });
196
+ o = URL.createObjectURL(e), t.bootstrapUrl = o;
197
+ }
198
+ let s = {
199
+ locateFile: (e) => e.endsWith(".wasm") ? i : new URL(e, r).href,
200
+ mainScriptUrlOrBlob: o
199
201
  };
200
202
  try {
201
203
  let e = await import("z3-solver");
202
- try {
203
- f(await import("../async-mutex-Duo4jwzn.js"));
204
- } catch {}
205
- let t, n = await new Promise((n, r) => {
206
- t = h(r), e.init(a).then(n, r);
207
- }).finally(() => t?.());
208
- return o = n, n && n.em && (_(n.em), m(n.em)), n;
204
+ return await n(() => e.init(s));
209
205
  } catch (e) {
210
- throw y(), Error(`[WaSHI] Failed to initialize Z3 in browser: ${e?.message || String(e)}`, { cause: e });
206
+ throw Error(`[WaSHI] Failed to initialize Z3 in browser: ${e?.message || String(e)}`, { cause: e });
211
207
  }
212
- })();
213
- return n.catch(() => {
214
- a === n && y();
215
- }), a = n, n;
208
+ })(), c = Promise.race([s, i]).finally(() => {
209
+ t.errorHandlers.delete(r);
210
+ });
211
+ return c.catch((e) => d(t, e)), o = c, c;
216
212
  }
217
- async function T(e) {
218
- let t = e || o || (a ? await a.catch(() => null) : null);
219
- p();
220
- for (let e of Array.from(u)) try {
221
- e(/* @__PURE__ */ Error("[WaSHI] Z3 threads terminated"));
222
- } catch {}
223
- if (u.clear(), !t || !t.em) {
224
- y();
225
- return;
226
- }
227
- try {
213
+ async function C(e) {
214
+ let t = e ? u.get(e) : l, n = e || t?.instance;
215
+ if (t && d(t, /* @__PURE__ */ Error("[WaSHI] Z3 threads terminated")), n?.em) try {
228
216
  let { killThreads: e } = await import("z3-solver");
229
- typeof e == "function" ? await e(t.em) : t.em?.PThread?.terminateAllThreads && t.em.PThread.terminateAllThreads();
217
+ typeof e == "function" ? await e(n.em) : n.em.PThread?.terminateAllThreads();
230
218
  } catch {
231
- t.em?.PThread?.terminateAllThreads && t.em.PThread.terminateAllThreads();
232
- } finally {
233
- y();
219
+ n.em.PThread?.terminateAllThreads();
234
220
  }
235
221
  }
236
222
  //#endregion
237
223
  //#region src/modeler/solvers/z3/solver.ts
238
- var E = class {
224
+ function w(e, t, n) {
225
+ Object.defineProperty(e, t, {
226
+ value: n,
227
+ enumerable: !0,
228
+ writable: !0,
229
+ configurable: !0
230
+ });
231
+ }
232
+ var T = class {
239
233
  name = "z3";
240
234
  capabilities = {
241
235
  supportsMIP: !0,
242
236
  supportsQP: !1,
243
237
  supportsSMT: !0,
244
- supportsUnsatCore: !0
238
+ supportsUnsatCore: !0,
239
+ supportsWarmStart: !1
245
240
  };
246
241
  createContextTranslator(r) {
247
- let i = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new Map();
248
- function o(e) {
249
- if (!i.has(e.name)) {
250
- let t = e.type === "Continuous" || e.type === "CONTINUOUS" ? r.Real.const(e.name) : r.Int.const(e.name);
251
- i.set(e.name, t);
242
+ let i = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Set();
243
+ function s(e, t) {
244
+ let n = e || t, r = n, i = 1;
245
+ for (; o.has(r);) r = `${n}__${i++}`;
246
+ return o.add(r), r;
247
+ }
248
+ function c(e) {
249
+ let t = i.get(e);
250
+ if (!t) {
251
+ let n = s(e.name, `v_${e.id ?? i.size}`);
252
+ t = e.type === "Continuous" || e.type === "CONTINUOUS" ? r.Real.const(n) : r.Int.const(n), i.set(e, t);
252
253
  }
253
- return i.get(e.name);
254
+ return t;
254
255
  }
255
- function s(e) {
256
- if (!a.has(e.name)) {
257
- let t = r.Bool.const(e.name);
258
- a.set(e.name, t);
256
+ function l(e) {
257
+ let t = a.get(e);
258
+ if (!t) {
259
+ let n = s(e.name, `b_${e.id ?? a.size}`);
260
+ t = r.Bool.const(n), a.set(e, t);
259
261
  }
260
- return a.get(e.name);
262
+ return t;
261
263
  }
262
- function c(e) {
264
+ function u(e) {
263
265
  return typeof r.isInt == "function" && r.isInt(e) ? r.ToReal(e) : e;
264
266
  }
265
- function l(e, t) {
267
+ function d(e, t) {
266
268
  let n = typeof r.isInt == "function" && r.isInt(e), i = typeof r.isInt == "function" && r.isInt(t);
267
- return n && i ? e.add(t) : c(e).add(c(t));
269
+ return n && i ? e.add(t) : u(e).add(u(t));
268
270
  }
269
- function u(e, t) {
270
- return typeof r.isInt == "function" && r.isInt(e) && Number.isInteger(t) ? e.mul(r.Int.val(t)) : c(e).mul(r.Real.val(t));
271
+ function f(e, t) {
272
+ return typeof r.isInt == "function" && r.isInt(e) && Number.isInteger(t) ? e.mul(r.Int.val(t)) : u(e).mul(r.Real.val(t));
271
273
  }
272
- function d(e, t) {
274
+ function p(e, t) {
273
275
  let n = typeof r.isInt == "function" && r.isInt(e), i = typeof r.isInt == "function" && r.isInt(t);
274
- return n && i ? e.mul(t) : c(e).mul(c(t));
276
+ return n && i ? e.mul(t) : u(e).mul(u(t));
275
277
  }
276
- function f(e, t) {
278
+ function m(e, t) {
277
279
  return t && Number.isInteger(e) ? r.Int.val(e) : r.Real.val(e);
278
280
  }
279
- function p(e, t, n) {
280
- let i = typeof r.isInt == "function" && r.isInt(e) && Number.isInteger(n), a = i ? e : c(e), o = i ? r.Int.val(n) : r.Real.val(n);
281
+ function h(e, t, n) {
282
+ let i = typeof r.isInt == "function" && r.isInt(e) && Number.isInteger(n), a = i ? e : u(e), o = i ? r.Int.val(n) : r.Real.val(n);
281
283
  return t === "<=" ? a.le(o) : t === ">=" ? a.ge(o) : a.eq(o);
282
284
  }
283
- function m(e) {
285
+ function g(e) {
284
286
  let t = null;
285
287
  if (e && e.terms) for (let [n, r] of e.terms.entries()) {
286
- let e = u(o(n), r);
287
- t = t === null ? e : l(t, e);
288
+ let e = f(c(n), r);
289
+ t = t === null ? e : d(t, e);
288
290
  }
289
291
  let n = e && typeof e.constant == "number" ? e.constant : 0;
290
292
  if (n !== 0 || t === null) {
291
- let e = f(n, t === null || typeof r.isInt == "function" && r.isInt(t));
292
- t = t === null ? e : l(t, e);
293
+ let e = m(n, t === null || typeof r.isInt == "function" && r.isInt(t));
294
+ t = t === null ? e : d(t, e);
293
295
  }
294
296
  return t;
295
297
  }
296
- function h(e) {
298
+ function _(e) {
297
299
  let t = null;
298
300
  if (e.linearTerms) for (let [n, r] of e.linearTerms.entries()) {
299
- let e = u(o(n), r);
300
- t = t === null ? e : l(t, e);
301
+ let e = f(c(n), r);
302
+ t = t === null ? e : d(t, e);
301
303
  }
302
304
  if (e.quadTerms) for (let n of e.quadTerms.values()) {
303
- let e = u(d(o(n.var1), o(n.var2)), n.coeff);
304
- t = t === null ? e : l(t, e);
305
+ let e = f(p(c(n.var1), c(n.var2)), n.coeff);
306
+ t = t === null ? e : d(t, e);
305
307
  }
306
308
  let n = e && typeof e.constant == "number" ? e.constant : 0;
307
309
  if (n !== 0 || t === null) {
308
- let e = f(n, t === null || typeof r.isInt == "function" && r.isInt(t));
309
- t = t === null ? e : l(t, e);
310
+ let e = m(n, t === null || typeof r.isInt == "function" && r.isInt(t));
311
+ t = t === null ? e : d(t, e);
310
312
  }
311
313
  return t;
312
314
  }
313
- function g(e) {
314
- let t = e.lhs instanceof Object && "quadTerms" in e.lhs ? h(e.lhs) : m(e.lhs), n = typeof e.rhs == "number" ? e.rhs : 0;
315
- return p(t, e.sense, n);
315
+ function v(e) {
316
+ let t = e.lhs instanceof Object && "quadTerms" in e.lhs ? _(e.lhs) : g(e.lhs), n = typeof e.rhs == "number" ? e.rhs : 0;
317
+ return h(t, e.sense, n);
316
318
  }
317
- function _(i) {
318
- if (i instanceof e || i && typeof i == "object" && "name" in i && typeof i.id == "number" && !("op" in i) && !("lhs" in i)) return s(i);
319
- if (i instanceof n || i && typeof i == "object" && "lhs" in i && "sense" in i) return g(i);
320
- if (i instanceof t || i && typeof i == "object" && "op" in i && Array.isArray(i.children)) {
319
+ function y(i) {
320
+ if (i instanceof t || i && typeof i == "object" && "name" in i && typeof i.id == "number" && !("op" in i) && !("lhs" in i)) return l(i);
321
+ if (i instanceof n || i && typeof i == "object" && "lhs" in i && "sense" in i) return v(i);
322
+ if (i instanceof e || i && typeof i == "object" && "op" in i && Array.isArray(i.children)) {
321
323
  let e = i;
322
324
  switch (e.op) {
323
- case "and": return r.And(...e.children.map(_));
324
- case "or": return r.Or(...e.children.map(_));
325
- case "not": return r.Not(_(e.children[0]));
326
- case "implies": return r.Implies(_(e.children[0]), _(e.children[1]));
327
- case "xor": return r.Xor(_(e.children[0]), _(e.children[1]));
325
+ case "and": return r.And(...e.children.map(y));
326
+ case "or": return r.Or(...e.children.map(y));
327
+ case "not": return r.Not(y(e.children[0]));
328
+ case "implies": return r.Implies(y(e.children[0]), y(e.children[1]));
329
+ case "xor": return e.children.length === 0 ? r.Bool.val(!1) : e.children.length === 1 ? y(e.children[0]) : e.children.slice(1).reduce((e, t) => r.Xor(e, y(t)), y(e.children[0]));
328
330
  default: throw Error(`Unsupported boolean operation: ${e.op}`);
329
331
  }
330
332
  }
331
333
  throw Error(`Unknown logic element: ${String(i)}`);
332
334
  }
333
- function v(e) {
335
+ function b(e) {
334
336
  if (!e) return 0;
335
337
  if (typeof e.asNumber == "function") return e.asNumber();
336
338
  let t = e.toString();
@@ -341,60 +343,85 @@ var E = class {
341
343
  return Number(t);
342
344
  }
343
345
  return {
344
- getVarAst: o,
345
- getBoolVarAst: s,
346
- compareAstWithNumber: p,
347
- translateLinExpr: m,
348
- translateQuadExpr: h,
349
- translateConstraint: g,
350
- translateBoolExpr: _,
351
- parseAstValue: v,
346
+ getVarAst: c,
347
+ getBoolVarAst: l,
348
+ compareAstWithNumber: h,
349
+ translateLinExpr: g,
350
+ translateQuadExpr: _,
351
+ translateConstraint: v,
352
+ translateBoolExpr: y,
353
+ parseAstValue: b,
352
354
  varAstMap: i,
353
355
  boolVarAstMap: a
354
356
  };
355
357
  }
356
- async check(e, t = {}) {
357
- let { Context: n } = await w(t), r = new n(`check_${Date.now()}_${Math.random().toString(36).substring(2, 7)}`), i = new r.Solver(), a = this.createContextTranslator(r), o = e.variables || e.vars || [], s = e.boolVars || [], c = e.constraints || [], l = e.logicalConstraints || [];
358
- for (let e of o) {
359
- let t = a.getVarAst(e);
360
- e.lb !== -Infinity && i.add(a.compareAstWithNumber(t, ">=", e.lb)), e.ub !== Infinity && i.add(a.compareAstWithNumber(t, "<=", e.ub)), (e.type === "Binary" || e.type === "BINARY") && (i.add(a.compareAstWithNumber(t, ">=", 0)), i.add(a.compareAstWithNumber(t, "<=", 1)));
361
- }
362
- for (let e of c) {
363
- let t = a.translateConstraint(e), n = r.Bool.const(e.name);
364
- i.addAndTrack(t, n);
365
- }
366
- for (let e = 0; e < l.length; e++) {
367
- let t = l[e], n = t && t.name || `lc_${e + 1}`, o = t && t.expr || t, s = a.translateBoolExpr(o), c = r.Bool.const(n);
368
- i.addAndTrack(s, c);
358
+ addVariableBounds(e, t, n) {
359
+ for (let r of t) {
360
+ let t = n.getVarAst(r);
361
+ r.lb !== -Infinity && e.add(n.compareAstWithNumber(t, ">=", r.lb)), r.ub !== Infinity && e.add(n.compareAstWithNumber(t, "<=", r.ub)), (r.type === "Binary" || r.type === "BINARY") && (e.add(n.compareAstWithNumber(t, ">=", 0)), e.add(n.compareAstWithNumber(t, "<=", 1)));
369
362
  }
370
- let u, d;
363
+ }
364
+ async runZ3CheckWithErrorGuard(e, t) {
365
+ let n;
371
366
  try {
372
- let e = i.check();
373
- d = await new Promise((t, n) => {
374
- u = h(n), e.then(t, n);
367
+ let r = e.check();
368
+ return await new Promise((e, i) => {
369
+ n = p(i, t), r.then(e, i);
375
370
  });
376
371
  } finally {
377
- u?.();
378
- }
379
- let f = d.toString();
380
- if (f === "sat") {
381
- let e = i.model(), t = {};
382
- for (let n of o) {
383
- let r = a.getVarAst(n), i = e.eval(r);
384
- t[n.name] = a.parseAstValue(i);
385
- }
372
+ n?.();
373
+ }
374
+ }
375
+ async check(e, t = {}) {
376
+ if (e?.variables?.some((e) => e.initialValue !== void 0)) throw Error(`Solver "${this.name}" does not support primal initial values (initialValue). Please use a solver with warm-start support such as HiGHS.`);
377
+ if (t.signal?.aborted) return {
378
+ status: "Unknown",
379
+ values: {}
380
+ };
381
+ let n = await S(t), { Context: r } = n;
382
+ if (t.signal?.aborted) return {
383
+ status: "Unknown",
384
+ values: {}
385
+ };
386
+ let i = new r(`check_${Date.now()}_${Math.random().toString(36).substring(2, 7)}`), a = new i.Solver(), o = this.createContextTranslator(i), s = e.variables || [], c = e.boolVars || [], l = e.constraints || [], u = e.logicalConstraints || [];
387
+ this.addVariableBounds(a, s, o);
388
+ let d = new Set(c.map((e) => e.name)), f = /* @__PURE__ */ new Map(), p = 0, m = (e) => {
389
+ let t = `__washi_track_${++p}`;
390
+ for (; d.has(t) || f.has(t);) t = `__washi_track_${++p}`;
391
+ let n = i.Bool.const(t);
392
+ return f.set(t, e), f.set(n.toString(), e), n;
393
+ };
394
+ for (let e of l) a.addAndTrack(o.translateConstraint(e), m(e.name));
395
+ for (let e = 0; e < u.length; e++) {
396
+ let t = u[e], n = "name" in t && typeof t.name == "string" ? t.name : `lc_${e + 1}`, r = "expr" in t ? t.expr : t;
397
+ a.addAndTrack(o.translateBoolExpr(r), m(n));
398
+ }
399
+ if (t.signal?.aborted) return {
400
+ status: "Unknown",
401
+ values: {}
402
+ };
403
+ let h = (await this.runZ3CheckWithErrorGuard(a, n)).toString();
404
+ if (h === "sat") {
405
+ let e = a.model(), t = {};
386
406
  for (let n of s) {
387
- let r = a.getBoolVarAst(n), i = e.eval(r);
388
- t[n.name] = i.toString() === "true";
407
+ let r = o.getVarAst(n), i = e.eval(r);
408
+ w(t, n.name, o.parseAstValue(i));
409
+ }
410
+ for (let n of c) {
411
+ let r = o.getBoolVarAst(n), i = e.eval(r);
412
+ w(t, n.name, i.toString() === "true");
389
413
  }
390
414
  return {
391
415
  status: "Sat",
392
416
  values: t
393
417
  };
394
418
  }
395
- if (f === "unsat") {
396
- let e = i.unsatCore(), t = [], n = typeof e.length == "function" ? e.length() : e.length;
397
- for (let r = 0; r < n; r++) t.push(e.get(r).toString());
419
+ if (h === "unsat") {
420
+ let e = a.unsatCore(), t = [], n = typeof e.length == "function" ? e.length() : e.length;
421
+ for (let r = 0; r < n; r++) {
422
+ let n = e.get(r).toString();
423
+ t.push(f.get(n) ?? n);
424
+ }
398
425
  return {
399
426
  status: "Unsat",
400
427
  values: {},
@@ -406,71 +433,184 @@ var E = class {
406
433
  values: {}
407
434
  };
408
435
  }
409
- async solve(e, t, n = {}) {
410
- if (!(e.objective !== null && e.objective !== void 0 && ("terms" in e.objective && e.objective.terms.size > 0 || "linearTerms" in e.objective && e.objective.linearTerms.size > 0 || "quadTerms" in e.objective && e.objective.quadTerms.length > 0 || e.objective.offset && e.objective.offset !== 0))) {
411
- let t = await this.check(e, n), r = {};
412
- for (let [e, n] of Object.entries(t.values)) r[e] = typeof n == "boolean" ? +!!n : n;
436
+ async solve(e, t, n) {
437
+ let r = (i(t) ? n : t) ?? {};
438
+ if (e?.variables?.some((e) => e.initialValue !== void 0)) throw Error(`Solver "${this.name}" does not support primal initial values (initialValue). Please use a solver with warm-start support such as HiGHS.`);
439
+ if (r.signal?.aborted) return {
440
+ status: "Unknown",
441
+ terminationStatus: "Interrupted",
442
+ hasPrimalSolution: !1,
443
+ objectiveValue: null,
444
+ values: {}
445
+ };
446
+ let a = performance.now(), o = e.objective;
447
+ if (!(o != null && ("quadTerms" in o ? o.linearTerms.size > 0 || o.quadTerms.size > 0 : "terms" in o && o.terms.size > 0))) {
448
+ let t = await this.check(e, r), n = {}, i = {};
449
+ for (let [e, r] of Object.entries(t.values)) typeof r == "boolean" ? (w(n, e, +!!r), w(i, e, r)) : w(n, e, r);
450
+ let s = t.status === "Sat", c = s ? o?.constant ?? 0 : null;
451
+ if (r.onProgress && !r.signal?.aborted) {
452
+ let e = (performance.now() - a) / 1e3;
453
+ r.onProgress({
454
+ elapsedTime: e,
455
+ elapsedSeconds: e,
456
+ ...typeof c == "number" && Number.isFinite(c) ? { objectiveValue: c } : {}
457
+ });
458
+ }
459
+ if (r.signal?.aborted) return {
460
+ status: s ? "Feasible" : "Unknown",
461
+ terminationStatus: "Interrupted",
462
+ hasPrimalSolution: s,
463
+ objectiveValue: c,
464
+ values: s ? n : {},
465
+ ...s && Object.keys(i).length > 0 ? { boolValues: i } : {}
466
+ };
467
+ let l = s ? "Optimal" : t.status === "Unsat" ? "Infeasible" : t.status === "Unknown" ? "Unknown" : "Error";
413
468
  return {
414
- status: t.status === "Sat" ? "Optimal" : t.status === "Unsat" ? "Infeasible" : "Error",
415
- objectiveValue: 0,
416
- values: r
469
+ status: l,
470
+ terminationStatus: l,
471
+ hasPrimalSolution: s,
472
+ objectiveValue: c,
473
+ values: s ? n : {},
474
+ ...s && Object.keys(i).length > 0 ? { boolValues: i } : {}
417
475
  };
418
476
  }
419
- let { Context: r } = await w(n), i = new r(`opt_${Date.now()}_${Math.random().toString(36).substring(2, 7)}`), a = new i.Optimize(), o = this.createContextTranslator(i), s = e.variables || e.vars || [], c = e.boolVars || [], l = e.constraints || [], u = e.logicalConstraints || [];
420
- for (let e of s) {
421
- let t = o.getVarAst(e);
422
- e.lb !== -Infinity && a.add(o.compareAstWithNumber(t, ">=", e.lb)), e.ub !== Infinity && a.add(o.compareAstWithNumber(t, "<=", e.ub)), (e.type === "Binary" || e.type === "BINARY") && (a.add(o.compareAstWithNumber(t, ">=", 0)), a.add(o.compareAstWithNumber(t, "<=", 1)));
423
- }
424
- for (let e of l) {
425
- let t = o.translateConstraint(e);
426
- a.add(t);
427
- }
428
- for (let e of u) {
429
- let t = e && e.expr || e, n = o.translateBoolExpr(t);
430
- a.add(n);
431
- }
432
- let d = e.objective instanceof Object && "quadTerms" in e.objective ? o.translateQuadExpr(e.objective) : o.translateLinExpr(e.objective);
433
- (e.sense || "MINIMIZE").toUpperCase() === "MAXIMIZE" ? a.maximize(d) : a.minimize(d);
434
- let f, p;
435
- try {
436
- let e = a.check();
437
- p = await new Promise((t, n) => {
438
- f = h(n), e.then(t, n);
439
- });
440
- } finally {
441
- f?.();
442
- }
443
- let m = p.toString();
444
- if (m === "sat") {
445
- let e = a.model(), t = {};
446
- for (let n of s) {
447
- let r = o.getVarAst(n), i = e.eval(r);
448
- t[n.name] = o.parseAstValue(i);
477
+ let s = await S(r), { Context: c } = s;
478
+ if (r.signal?.aborted) return {
479
+ status: "Unknown",
480
+ terminationStatus: "Interrupted",
481
+ hasPrimalSolution: !1,
482
+ objectiveValue: null,
483
+ values: {}
484
+ };
485
+ let l = new c(`opt_${Date.now()}_${Math.random().toString(36).substring(2, 7)}`), u = new l.Optimize(), d = this.createContextTranslator(l), f = e.variables || [], p = e.boolVars || [], m = e.constraints || [], h = e.logicalConstraints || [];
486
+ this.addVariableBounds(u, f, d);
487
+ for (let e of m) {
488
+ let t = d.translateConstraint(e);
489
+ u.add(t);
490
+ }
491
+ for (let e of h) {
492
+ let t = "expr" in e ? e.expr : e, n = d.translateBoolExpr(t);
493
+ u.add(n);
494
+ }
495
+ let g = "quadTerms" in o ? d.translateQuadExpr(o) : d.translateLinExpr(o), _ = (e.sense || "MINIMIZE").toUpperCase(), v = _ === "MAXIMIZE" ? u.maximize(g) : u.minimize(g);
496
+ if (r.signal?.aborted) return {
497
+ status: "Unknown",
498
+ terminationStatus: "Interrupted",
499
+ hasPrimalSolution: !1,
500
+ objectiveValue: null,
501
+ values: {}
502
+ };
503
+ let y = (await this.runZ3CheckWithErrorGuard(u, s)).toString();
504
+ if (y === "sat") {
505
+ let e = u.model(), t = {}, n = {};
506
+ for (let n of f) {
507
+ let r = d.getVarAst(n), i = e.eval(r);
508
+ w(t, n.name, d.parseAstValue(i));
449
509
  }
450
- for (let n of c) {
451
- let r = o.getBoolVarAst(n), i = e.eval(r);
452
- t[n.name] = +(i.toString() === "true");
510
+ for (let r of p) {
511
+ let i = d.getBoolVarAst(r), a = e.eval(i).toString() === "true";
512
+ w(t, r.name, +!!a), w(n, r.name, a);
513
+ }
514
+ let i = d.parseAstValue(e.eval(g)), o = !1, s = !1;
515
+ try {
516
+ let e = _ === "MAXIMIZE" ? typeof u.getUpper == "function" ? u.getUpper(v) : void 0 : typeof u.getLower == "function" ? u.getLower(v) : void 0, t = e ? e.toString() : "";
517
+ t.includes("oo") ? o = !0 : t.includes("epsilon") && (s = !0);
518
+ } catch {}
519
+ if (o) {
520
+ if (r.onProgress) {
521
+ let e = (performance.now() - a) / 1e3;
522
+ r.onProgress({
523
+ elapsedTime: e,
524
+ elapsedSeconds: e,
525
+ ...Number.isFinite(i) ? { objectiveValue: i } : {}
526
+ });
527
+ }
528
+ return {
529
+ status: "Unbounded",
530
+ terminationStatus: "Unbounded",
531
+ hasPrimalSolution: !0,
532
+ objectiveValue: i,
533
+ values: t,
534
+ ...Object.keys(n).length > 0 ? { boolValues: n } : {}
535
+ };
536
+ }
537
+ if (s) {
538
+ if (r.onProgress) {
539
+ let e = (performance.now() - a) / 1e3;
540
+ r.onProgress({
541
+ elapsedTime: e,
542
+ elapsedSeconds: e
543
+ });
544
+ }
545
+ return {
546
+ status: "Unknown",
547
+ terminationStatus: "Unknown",
548
+ hasPrimalSolution: !1,
549
+ objectiveValue: null,
550
+ values: {}
551
+ };
552
+ }
553
+ if (r.onProgress) {
554
+ let e = (performance.now() - a) / 1e3;
555
+ r.onProgress({
556
+ elapsedTime: e,
557
+ elapsedSeconds: e,
558
+ ...Number.isFinite(i) ? { objectiveValue: i } : {}
559
+ });
453
560
  }
454
561
  return {
455
562
  status: "Optimal",
456
- objectiveValue: o.parseAstValue(e.eval(d)),
457
- values: t
563
+ terminationStatus: "Optimal",
564
+ hasPrimalSolution: !0,
565
+ objectiveValue: i,
566
+ values: t,
567
+ ...Object.keys(n).length > 0 ? { boolValues: n } : {}
458
568
  };
459
569
  }
460
- return m === "unsat" ? {
461
- status: "Infeasible",
462
- objectiveValue: null,
463
- values: {}
464
- } : {
570
+ if (y === "unsat") {
571
+ if (r.onProgress) {
572
+ let e = (performance.now() - a) / 1e3;
573
+ r.onProgress({
574
+ elapsedTime: e,
575
+ elapsedSeconds: e
576
+ });
577
+ }
578
+ return {
579
+ status: "Infeasible",
580
+ terminationStatus: "Infeasible",
581
+ hasPrimalSolution: !1,
582
+ objectiveValue: null,
583
+ values: {}
584
+ };
585
+ }
586
+ if (y === "unknown") {
587
+ if (r.onProgress) {
588
+ let e = (performance.now() - a) / 1e3;
589
+ r.onProgress({
590
+ elapsedTime: e,
591
+ elapsedSeconds: e
592
+ });
593
+ }
594
+ return {
595
+ status: "Unknown",
596
+ terminationStatus: "Unknown",
597
+ hasPrimalSolution: !1,
598
+ objectiveValue: null,
599
+ values: {}
600
+ };
601
+ }
602
+ return {
465
603
  status: "Error",
604
+ terminationStatus: "Error",
605
+ hasPrimalSolution: !1,
466
606
  objectiveValue: null,
467
607
  values: {}
468
608
  };
469
609
  }
470
610
  };
471
- function D() {
472
- r("z3", () => new E());
611
+ function E() {
612
+ r("z3", () => new T());
473
613
  }
474
- D();
614
+ E();
475
615
  //#endregion
476
- export { E as Z3Solver, C as ensureInitZ3Loaded, T as killZ3Threads, w as loadZ3, g as notifyZ3WorkerError, x as probeAssetUrl, h as registerZ3ErrorHandler, D as registerZ3Solver, y as resetZ3LoaderCache, S as resolveZ3AssetUrls, v as setZ3Assets, p as unlockAllZ3Semaphores, b as validateZ3BrowserEnvironment };
616
+ export { T as Z3Solver, x as ensureInitZ3Loaded, C as killZ3Threads, S as loadZ3, m as notifyZ3WorkerError, y as probeAssetUrl, p as registerZ3ErrorHandler, E as registerZ3Solver, _ as resetZ3LoaderCache, b as resolveZ3AssetUrls, g as setZ3Assets, v as validateZ3BrowserEnvironment };