@ohtaman/washi 0.8.2 → 0.9.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.
- package/README.md +3 -0
- package/dist/browser/solvers/{highs-__vite-browser-external-Dn-83hnF.js → highs-__vite-browser-external-xcxOpLMD.js} +1 -1
- package/dist/browser/solvers/{highs-browser-runtime-0Bgq5ksG.js → highs-browser-runtime-ccRmE4qj.js} +1 -1
- package/dist/browser/solvers/highs-inline.js +51 -0
- package/dist/{lib/solvers/loader-CDpPXN_F.js → browser/solvers/highs-loader-Csn3-0tq.js} +241 -149
- package/dist/browser/solvers/highs-worker.js +17 -16
- package/dist/browser/solvers/highs.js +295 -255
- package/dist/browser/solvers/pumpkin-inline.js +51 -0
- package/dist/{lib/solvers/loader-Ow74Fa1B.js → browser/solvers/pumpkin-loader-BI3n3dWO.js} +145 -55
- package/dist/browser/solvers/pumpkin-worker.js +24 -23
- package/dist/browser/solvers/pumpkin.js +307 -288
- package/dist/browser/washi.js +365 -316
- package/dist/browser/washi.min.js +4 -4
- package/dist/lib/solvers/embedded-wasm-CSFlLV5N.js +49 -0
- package/dist/lib/solvers/highs-node-runtime.js +1 -7
- package/dist/lib/solvers/highs-worker.js +19 -17
- package/dist/lib/solvers/highs.js +153 -112
- package/dist/{browser/solvers/highs-loader-BLY3wzDH.js → lib/solvers/loader-DCtyxsfF.js} +56 -36
- package/dist/{browser/solvers/pumpkin-loader-CvdaKxf4.js → lib/solvers/loader-b5PNpCmX.js} +58 -33
- package/dist/lib/solvers/pumpkin-worker.js +25 -23
- package/dist/lib/solvers/pumpkin.js +212 -192
- package/dist/lib/washi.js +365 -316
- package/dist/types/modeler/model.d.ts +7 -0
- package/dist/types/modeler/model.d.ts.map +1 -1
- package/dist/types/modeler/solvers/embedded-wasm.d.ts +36 -0
- package/dist/types/modeler/solvers/embedded-wasm.d.ts.map +1 -0
- package/dist/types/modeler/solvers/highs/loader.d.ts.map +1 -1
- package/dist/types/modeler/solvers/highs/node-runtime.d.ts.map +1 -1
- package/dist/types/modeler/solvers/highs/worker-client.d.ts.map +1 -1
- package/dist/types/modeler/solvers/inline-pack.d.ts +6 -0
- package/dist/types/modeler/solvers/inline-pack.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/loader.d.ts +1 -1
- package/dist/types/modeler/solvers/pumpkin/loader.d.ts.map +1 -1
- package/dist/types/modeler/solvers/pumpkin/solver.d.ts.map +1 -1
- package/dist/types/modeler/solvers/pumpkin/worker-client.d.ts.map +1 -1
- package/dist/types/modeler/solvers/pumpkin/worker.d.ts +1 -0
- package/dist/types/modeler/solvers/pumpkin/worker.d.ts.map +1 -1
- package/docs/getting-started/ai-agents.md +56 -1
- package/docs/getting-started/coding-agents.md +38 -5
- package/docs/getting-started/runtimes.md +63 -1
- package/docs/solvers/highs.md +2 -0
- package/docs/solvers/pumpkin.md +2 -0
- package/llms-full.txt +123 -2
- package/llms.txt +56 -1
- package/package.json +12 -1
- package/skills/washi/SKILL.md +75 -14
- package/skills/washi/references/single-html.md +330 -0
package/llms-full.txt
CHANGED
|
@@ -62,7 +62,9 @@ Classic `<script>` tags load immediately invoked function expression (`IIFE`) bu
|
|
|
62
62
|
| --- | --- | --- |
|
|
63
63
|
| **Core (HiGHS & Pumpkin)** | `import { Model, expr, maximize } from "./washi/washi.js";` | `<script src="./washi/washi.min.js"></script>` (global `Washi`) |
|
|
64
64
|
| **HiGHS** *(subpath API)* | `import { HighsSolver, setHighsWasmUrl } from "./washi/solvers/highs.js";` | Bundled in `washi.min.js` (pass `{ wasmUrl }` per call) |
|
|
65
|
+
| **HiGHS Inline WASM** *(no `fetch`)* | `import "./washi/solvers/highs-inline.js";` | `<script src="./washi/solvers/highs-inline.js"></script>` (`@ohtaman/washi/browser/solvers/highs-inline`) |
|
|
65
66
|
| **Pumpkin** *(subpath API)* | `import { PumpkinSolver, setPumpkinWasmUrl } from "./washi/solvers/pumpkin.js";` | Bundled in `washi.min.js` (pass `{ wasmUrl }` per call) |
|
|
67
|
+
| **Pumpkin Inline WASM** *(no `fetch`)* | `import "./washi/solvers/pumpkin-inline.js";` | `<script src="./washi/solvers/pumpkin-inline.js"></script>` (`@ohtaman/washi/browser/solvers/pumpkin-inline`) |
|
|
66
68
|
| **GLPK** *(optional)* | `import "./washi/solvers/glpk.js";` | `<script src="./washi/solvers/glpk.min.js"></script>` (`WashiGLPK`) |
|
|
67
69
|
| **Z3** *(optional)* | `import "./washi/solvers/z3.js";` | `<script src="./washi/solvers/z3.min.js"></script>` (`WashiZ3`) |
|
|
68
70
|
|
|
@@ -74,6 +76,45 @@ Classic `<script>` tags load immediately invoked function expression (`IIFE`) bu
|
|
|
74
76
|
- **Browser self-hosting**: Deploy `dist/browser/` intact (entry scripts, `solvers/` runtime/worker/WASM assets, and chunks). Requests that hit missing URLs or HTML SPA fallbacks fail immediately and report the attempted URL.
|
|
75
77
|
- **Custom browser URLs**: Call `setHighsWasmUrl(url)` (from `@ohtaman/washi/solvers/highs`) or `setPumpkinWasmUrl(url)` (from `@ohtaman/washi/solvers/pumpkin`), or pass `{ wasmUrl }` to `m.solve()` / `m.check()`. This override is ignored for HiGHS in Node.js, while Pumpkin accepts `file://` URLs in Node.js. For Z3, call `setZ3Assets({ jsUrl, wasmUrl })` (from `@ohtaman/washi/solvers/z3`) or pass `{ z3JsUrl, z3WasmUrl }`.
|
|
76
78
|
|
|
79
|
+
#### Fetch-Free Loading in CSP-Restricted Pages (Inline WASM)
|
|
80
|
+
|
|
81
|
+
- **When `fetch()` is blocked (`connect-src 'none'`)**: Hosted AI sandboxes—including **Claude.ai Artifacts** (edit and published links), **ChatGPT inline visualizations**, and **Codex inline visualizations**—allow CDN scripts (`script-src`) while blocking `fetch()` via `connect-src` and blocking Web Workers via `worker-src`. Load the self-registering inline WASM packs (`solvers/highs-inline.js` / `@ohtaman/washi/browser/solvers/highs-inline` and `solvers/pumpkin-inline.js` / `@ohtaman/washi/browser/solvers/pumpkin-inline`) **before** your first `solve()`, `check()`, or `warmup*()` call—zero base64 or binary-setter boilerplate is required.
|
|
82
|
+
- **Dual ESM & Classic `<script>` support (prefer ESM)**: Each `*-inline.js` file has no `import`/`export` statements, so the exact same file works via ESM `import` and classic `<script src="...">`, and default loaders (`washi.js`, `washi.min.js`, `dist/lib/*`) never reference it. Prefer **ESM (`<script type="module">`)** in dynamically injected sandboxes (such as ChatGPT inline visualizations), where classic `<script src>` tags can race with inline scripts on cold loads.
|
|
83
|
+
- **Initialization semantics**: Load the inline pack **before** the first solver initialization (`solve()`, `check()`, or `warmup*()`). Before first initialization, load order between core (`washi.js` / `washi.min.js`) and `*-inline.js` does not matter. Once a solver's default WASM source is selected on first initialization, WaSHI latches it and does not switch on late pack loads (keeping main-thread and Worker behavior consistent). Explicit `set*WasmUrl()` / `{ wasmUrl }` still override the embedded pack when provided.
|
|
84
|
+
- **Worker behavior & `useWorker: false` trade-offs**: WaSHI keeps `useWorker: true` as the default on `http(s):` origins (including regular web apps, Gemini Canvas, and Google Sites, where `*-inline.js` transfers embedded bytes via `postMessage` without calling `fetch()`). In Worker-restricted sandboxes (Claude Artifacts, ChatGPT/Codex inline visualizations, or `file://`), pass `{ useWorker: false }` along with a `timeLimit` (main-thread execution blocks UI rendering, prevents immediate mid-solve `AbortSignal` interruption and live `onProgress` repaints, and lacks Worker crash isolation). Embedded previews whose CSP omits `'wasm-unsafe-eval'` (such as Antigravity's HTML artifact preview) cannot compile WebAssembly; open exported HTML in Chrome/Safari or via a local server.
|
|
85
|
+
|
|
86
|
+
```html
|
|
87
|
+
<!-- Recommended: ESM (<script type="module">) + { useWorker: false } -->
|
|
88
|
+
<!-- Works across Claude Artifacts, ChatGPT/Codex inline visualizations, Gemini Canvas, Google Sites & file:// -->
|
|
89
|
+
<script type="module">
|
|
90
|
+
import { Model, expr, maximize } from "https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/washi.js";
|
|
91
|
+
import "https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/highs-inline.js";
|
|
92
|
+
// import "https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/pumpkin-inline.js";
|
|
93
|
+
|
|
94
|
+
const m = new Model("CspFreeEsm");
|
|
95
|
+
const x = m.integer("x", { lb: 0 });
|
|
96
|
+
m.objective(maximize(x));
|
|
97
|
+
m.constraint(expr`${x} <= 10`, "cap");
|
|
98
|
+
await m.solve({ useWorker: false, timeLimit: 5 }); // Omit useWorker: false where Workers are allowed (e.g. Gemini Canvas, self-hosted)
|
|
99
|
+
</script>
|
|
100
|
+
|
|
101
|
+
<!-- Classic <script> fallback (prefer ESM above in dynamically injected sandboxes) -->
|
|
102
|
+
<script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/washi.min.js"></script>
|
|
103
|
+
<script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/highs-inline.js"></script>
|
|
104
|
+
<script>
|
|
105
|
+
(async () => {
|
|
106
|
+
const { Model, expr, maximize } = Washi;
|
|
107
|
+
const m = new Model("CspFreeClassic");
|
|
108
|
+
const x = m.integer("x", { lb: 0 });
|
|
109
|
+
m.objective(maximize(x));
|
|
110
|
+
m.constraint(expr`${x} <= 10`, "cap");
|
|
111
|
+
await m.solve({ useWorker: false, timeLimit: 5 });
|
|
112
|
+
})();
|
|
113
|
+
</script>
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
> **Single-file offline HTML (`file://` with npm packages)**: Use the CDN `*-inline.js` packs above for zero-build hosted CSP sandboxes (`connect-src 'none'`). When building a zero-network, double-clickable offline `file://` HTML app that bundles `@ohtaman/washi` (HiGHS / Pumpkin) together with third-party npm packages (React, KaTeX, Chart.js, Tailwind) via Vite + `vite-plugin-singlefile` and gzipped WASM, follow [`skills/washi/references/single-html.md`](https://github.com/ohtaman/washi/blob/main/skills/washi/references/single-html.md) (included inside the unified `washi` skill at `node_modules/@ohtaman/washi/skills/washi/references/single-html.md`).
|
|
117
|
+
|
|
77
118
|
#### Browser Security & Cross-Origin Isolation
|
|
78
119
|
|
|
79
120
|
- **HiGHS, Pumpkin, and GLPK**: Run in standard browser contexts without `SharedArrayBuffer` or isolation headers.
|
|
@@ -178,7 +219,7 @@ m.indicator(b, expr`${x} <= 50`, { activeValue: 1, name: "cap_when_b" });
|
|
|
178
219
|
- **Return value**: Returns `SolverStatus` (`"Optimal"`, `"Feasible"`, `"Infeasible"`, `"Unbounded"`, `"TimeLimit"`, `"IterationLimit"`, `"Error"`, `"Unknown"`, `"NotSolved"`, `"Sat"`, or `"Unsat"`).
|
|
179
220
|
- **Status semantics**: Optimization solvers return `"Feasible"` when a limit or abort terminates the solve with a valid primal incumbent. `"Sat"` and `"Unsat"` apply to satisfiability or custom solver adapters.
|
|
180
221
|
- **Reading results**: Check `m.hasPrimalSolution`, then read `x.value`, `` expr`...`.value ``, `m.objectiveValue`, `constr.dual`, `constr.slack`, and `x.reducedCost`. `m.values` (like `m.variables` and `m.constraints`) is a `ReadonlyMap<string, number | boolean>` (use `.get(name)`), unlike `CheckResult.values` and `m.solution.values` (`Record`).
|
|
181
|
-
- **Independent verification**: `m.verify()` checks primal feasibility—finiteness, bounds, integrality, and constraints—returning `VerificationReport` (`{ valid, complete, maxViolation, violations }`) without recomputing `m.objectiveValue`.
|
|
222
|
+
- **Independent verification**: `m.verify()` checks primal feasibility—finiteness, bounds, integrality, and constraints—returning `VerificationReport` (`{ valid, complete, maxViolation, violations }`) without recomputing `m.objectiveValue`. On decomposed models, `master.verify()` checks only the master model's constraints and integrality, so end-to-end invariants outside the master (such as TSP single-tour connectivity or full subproblem demand satisfaction) still require domain assertions.
|
|
182
223
|
|
|
183
224
|
#### Satisfiability & Conflict Diagnosis (`await m.check(options?)`)
|
|
184
225
|
|
|
@@ -191,6 +232,20 @@ m.indicator(b, expr`${x} <= 50`, { activeValue: 1, name: "cap_when_b" });
|
|
|
191
232
|
|
|
192
233
|
- Mutating variables, constraints, or the objective while an `m.solve()` or `m.check()` call is pending invalidates the in-flight run and rejects the stale promise.
|
|
193
234
|
|
|
235
|
+
### Decomposition APIs (Column Generation & Benders)
|
|
236
|
+
|
|
237
|
+
For full examples, see [`docs/guide/decomposition.md`](https://github.com/ohtaman/washi/blob/main/docs/guide/decomposition.md). `master.solveColumnGeneration(...)`, `m.relax()`, and `master.solveBenders(...)` auto-cut `sub.link(...)` mode require an LP-dual solver (**HiGHS** or **GLPK**; `Pumpkin` and `Z3` do not support LP duals or `relax: true`); manual `BendersCut` mode only calls `master.solve()`. Both helpers are also exported standalone.
|
|
238
|
+
|
|
239
|
+
- **Column Generation**: `await master.solveColumnGeneration({ pricing, finalizeAsMIP?, reducedCostTolerance?, maxIterations?, solver?, solverOptions?, onIteration? })`
|
|
240
|
+
- `pricing` receives `{ duals, getDual, iteration, master, masterSolution, reducedCostTolerance }` and returns `ColumnProposal | ColumnProposal[] | null`.
|
|
241
|
+
- Reduced cost (`cost - sum(pi_i * a_i)`) is computed and filtered automatically when `proposal.reducedCost` is omitted; `proposal.type` defaults to `"Continuous"`.
|
|
242
|
+
- Initial RMP LP relaxation must be feasible and bounded (no Phase-I artificials); `finalizeAsMIP: true` (default) runs a root-node Price-and-Branch heuristic rather than exact Branch-and-Price.
|
|
243
|
+
- **Benders Decomposition**: `await master.solveBenders({ subproblem, theta?, gapTolerance?, maxIterations?, solver?, solverOptions?, onIteration? })`
|
|
244
|
+
- `subproblem` receives `{ iteration, masterValues, getMasterValue, masterObjective, lowerBound, upperBound, master }` and returns either a continuous LP `Model` using `sub.link(constraint)` (optimality cuts only; throws on infeasible subproblem) or manual `BendersCut | BendersCut[] | null` (`{ kind: "optimality" | "feasibility", constraint, name?, subproblemObjective? }`).
|
|
245
|
+
- Auto-`theta` assumes a `Minimize` master with `lb: 0`; for `Maximize` or negative subproblem costs, declare `theta` on `master`, include it in `master.objective(...)`, and pass `{ theta }`.
|
|
246
|
+
- Manual `BendersCut.subproblemObjective` expects **Stage-1 direct cost (excluding `theta`) + Stage-2 cost**, not Stage-2 subproblem cost alone.
|
|
247
|
+
- **Low-level primitives**: `await m.relax()`, `c.dual` / `m.solution?.duals[name]`, `m.column({ cost, coefficients, type?, lb?, ub?, name? })`, and `c.addTerm(v, coeff)` / `c.setTerm(v, coeff)`.
|
|
248
|
+
|
|
194
249
|
---
|
|
195
250
|
|
|
196
251
|
## 3. Executable Examples
|
|
@@ -739,6 +794,7 @@ On standard `http://` and `https://` origins, browsers block direct `new Worker(
|
|
|
739
794
|
| **CDN scripts** | Loading `washi.min.js` from an HTTPS CDN includes both HiGHS and Pumpkin and works by double-clicking a local `.html` file (`file://`) with no local server. |
|
|
740
795
|
| **Opaque origins (`window.origin === "null"`)** | On `file://` pages or sandboxed `<iframe>` embeds without `allow-same-origin`, WaSHI runs HiGHS and Pumpkin on the main thread when `useWorker` is omitted. |
|
|
741
796
|
| **Self-hosted `.wasm` & Z3** | Local `.wasm` files require `http://` or `https://` because browsers block `fetch()` on `file://`. Z3 also requires Cross-Origin Isolation (see below). |
|
|
797
|
+
| **Offline single-file HTML + npm libs** | To bundle `@ohtaman/washi` (HiGHS & Pumpkin) alongside third-party npm packages into one zero-network `index.html` for `file://`, follow [`skills/washi/references/single-html.md`](https://github.com/ohtaman/washi/blob/main/skills/washi/references/single-html.md) inside the `washi` Agent Skill. |
|
|
742
798
|
|
|
743
799
|
---
|
|
744
800
|
|
|
@@ -766,7 +822,68 @@ Copy `node_modules/@ohtaman/washi/dist/browser/` to your static origin as a comp
|
|
|
766
822
|
| `worker-src` | `'self' blob:` | Allows default Web Workers and CDN `Blob` worker bootstraps. |
|
|
767
823
|
| `worker-src` (if `blob:` is disallowed) | `'self'` only | Serve `solvers/highs-worker.js` / `solvers/pumpkin-worker.js` from `'self'`, or pass `{ useWorker: false }`. |
|
|
768
824
|
|
|
769
|
-
### 3.
|
|
825
|
+
### 3. CSP-Restricted Pages Without `fetch()` (`connect-src 'none'`) {#csp-fetch-free}
|
|
826
|
+
|
|
827
|
+
Hosted AI sandboxes—such as **Claude.ai Artifacts** (both edit views and published share links), **ChatGPT inline visualizations**, and **Codex inline visualizations**—allow scripts from CDNs (`script-src`) while blocking `.wasm` `fetch()` (`connect-src 'none'`) and blocking Web Workers (`worker-src`). In these environments, default HiGHS and Pumpkin initialization fails because they fetch `.wasm` files.
|
|
828
|
+
|
|
829
|
+
Load the self-registering inline WASM packs (`solvers/highs-inline.js` / `@ohtaman/washi/browser/solvers/highs-inline` and `solvers/pumpkin-inline.js` / `@ohtaman/washi/browser/solvers/pumpkin-inline`) **before** your first `solve()`, `check()`, or `warmup*()` call—no manual base64 decoding or binary setter calls are required. Each inline pack contains no `import` or `export` statements, so the exact same file works in both ESM and classic `<script>` mode, and default loaders never reference it:
|
|
830
|
+
|
|
831
|
+
```html
|
|
832
|
+
<!-- Option A (Recommended): ESM (<script type="module">) + { useWorker: false } -->
|
|
833
|
+
<script type="module">
|
|
834
|
+
import { Model, expr, maximize } from "https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/washi.js";
|
|
835
|
+
import "https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/highs-inline.js";
|
|
836
|
+
import "https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/pumpkin-inline.js";
|
|
837
|
+
|
|
838
|
+
const m = new Model("CspFreeExample");
|
|
839
|
+
const x = m.integer("x", { lb: 0 });
|
|
840
|
+
const y = m.integer("y", { lb: 0 });
|
|
841
|
+
m.objective(maximize(expr`30 * ${x} + 50 * ${y}`));
|
|
842
|
+
m.constraint(expr`${x} + 2 * ${y} <= 40`);
|
|
843
|
+
m.constraint(expr`2 * ${x} + ${y} <= 50`);
|
|
844
|
+
await m.solve({ useWorker: false, timeLimit: 5 }); // Works across all supported AI sandboxes
|
|
845
|
+
</script>
|
|
846
|
+
|
|
847
|
+
<!-- Option B: Classic <script> (prefer Option A in dynamically injected sandboxes) -->
|
|
848
|
+
<script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/washi.min.js"></script>
|
|
849
|
+
<script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/highs-inline.js"></script>
|
|
850
|
+
<script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/pumpkin-inline.js"></script>
|
|
851
|
+
<script>
|
|
852
|
+
(async () => {
|
|
853
|
+
const { Model, expr, maximize } = Washi;
|
|
854
|
+
const m = new Model("CspFreeClassic");
|
|
855
|
+
const x = m.integer("x", { lb: 0 });
|
|
856
|
+
const y = m.integer("y", { lb: 0 });
|
|
857
|
+
m.objective(maximize(expr`30 * ${x} + 50 * ${y}`));
|
|
858
|
+
m.constraint(expr`${x} + 2 * ${y} <= 40`);
|
|
859
|
+
m.constraint(expr`2 * ${x} + ${y} <= 50`);
|
|
860
|
+
await m.solve({ useWorker: false, timeLimit: 5 }); // Zero fetch(), zero Blob URLs, zero Workers
|
|
861
|
+
})();
|
|
862
|
+
</script>
|
|
863
|
+
```
|
|
864
|
+
|
|
865
|
+
#### Hosted AI Sandbox Compatibility (Empirically Verified)
|
|
866
|
+
|
|
867
|
+
| Environment | `fetch(.wasm)` | `*-inline.js` + `{ useWorker: false }` | `useWorker: true` | Recommended Setup |
|
|
868
|
+
| --- | :---: | :---: | :---: | --- |
|
|
869
|
+
| **Claude.ai Artifacts** *(edit & published links)* | Blocked | Works *(ESM & Classic)* | Blocked | Option A (ESM + `*-inline.js` + `{ useWorker: false }`) |
|
|
870
|
+
| **ChatGPT Inline Visualization** *(`codex-inline-visualization-*`)* | Blocked | Works *(ESM reliable; Classic can race on cold load)* | Blocked | Option A (ESM + `*-inline.js` + `{ useWorker: false }`) |
|
|
871
|
+
| **Codex Inline Visualization** *(`codex-sandbox://...`)* | Blocked | Works *(ESM & Classic)* | Blocked | Option A (ESM + `*-inline.js` + `{ useWorker: false }`) |
|
|
872
|
+
| **Gemini Canvas & Google Sites** *(1-click publish)* | Works | Works *(ESM & Classic)* | Works | Option A works everywhere; default `useWorker: true` also works |
|
|
873
|
+
| **Antigravity HTML Artifact Preview** | Blocked *(CDN blocked)* | Blocked *(CSP omits `'wasm-unsafe-eval'`)* | Blocked | Preview via local server + built-in browser or open `.html` in Chrome/Safari |
|
|
874
|
+
| **Local Browser `file://`** *(Chrome / Safari)* | Works via HTTPS CDN | Works *(CDN or Vite offline bundle)* | Auto main-thread | CDN Option A or offline [`skills/washi/references/single-html.md`](https://github.com/ohtaman/washi/blob/main/skills/washi/references/single-html.md) |
|
|
875
|
+
|
|
876
|
+
- **Why prefer Option A (ESM) over Option B (Classic `<script>`)**: Sandboxes that dynamically inject HTML into the DOM (such as ChatGPT inline visualizations) may evaluate inline classic `<script>` blocks before external `<script src>` downloads complete or after `window` `load` has already fired. ESM `<script type="module">` `import` statements strictly wait for all dependencies before executing.
|
|
877
|
+
- **Initialization semantics**: Load `*-inline.js` before the first solver initialization (`solve()`, `check()`, or `warmup*()`). Load order between core (`washi.js` / `washi.min.js`) and `*-inline.js` does not matter before first initialization. Once a solver's default WASM source is selected on first initialization, WaSHI latches it and ignores late pack registration so main-thread and Worker behavior stay consistent.
|
|
878
|
+
- **Web Worker mode**: When `useWorker: true`, WaSHI transfers the embedded bytes once per Worker instance via `postMessage` so Worker startup does not call `fetch()`. If `worker-src` blocks `blob:` or Workers, pass `{ useWorker: false }`.
|
|
879
|
+
- **GLPK & Z3**: **GLPK** (`solvers/glpk.js`) already embeds its WASM inline without `fetch()`. **Z3** requires `SharedArrayBuffer` and Cross-Origin Isolation headers (below).
|
|
880
|
+
|
|
881
|
+
> [!TIP]
|
|
882
|
+
> **Choosing Between `useWorker: true` and `useWorker: false`**:
|
|
883
|
+
> - **Use `useWorker: true` (default on standard `http(s):` origins)** for regular web apps, Vite/Next.js bundles, Gemini Canvas, and Google Sites. Running in a Web Worker keeps the browser UI responsive, allows immediate `AbortSignal` worker termination, supports live `onProgress` UI repaints, and isolates solver crashes.
|
|
884
|
+
> - **Use `{ useWorker: false }`** only where Web Workers are disallowed (Claude.ai Artifacts, ChatGPT/Codex inline visualizations, or `file://` pages). Main-thread solving blocks UI painting and user clicks while running, prevents mid-solve `AbortSignal` interruption and live progress updates, and shares the UI thread on crash—so always pass a sensible `timeLimit` (for example, `timeLimit: 5`) and yield a frame before showing a loading spinner.
|
|
885
|
+
|
|
886
|
+
### 4. Enable Cross-Origin Isolation (Z3 Only)
|
|
770
887
|
|
|
771
888
|
HiGHS, Pumpkin, and GLPK run in standard browser contexts without isolation headers. Z3 uses `SharedArrayBuffer` multithreading, so browsers require a **Secure Context** (`https://` or `http://localhost`). You must also serve `Cross-Origin-Opener-Policy` and `Cross-Origin-Embedder-Policy` headers (`COOP`/`COEP`); see the [Cross-Origin Isolation Guide](docs/getting-started/cross-origin.md).
|
|
772
889
|
|
|
@@ -3052,6 +3169,8 @@ const syncModel = new Model({
|
|
|
3052
3169
|
|
|
3053
3170
|
If `highs.wasm` fails to load or returns an HTML error page, WaSHI throws an error with the attempted URL instead of silently falling back to an external CDN.
|
|
3054
3171
|
|
|
3172
|
+
For CSP-restricted pages where `fetch()` is blocked (`connect-src 'none'`), load the self-registering inline WASM pack (`@ohtaman/washi/browser/solvers/highs-inline` / `solvers/highs-inline.js`, usable via ESM `import` or a classic `<script>` tag) before the first `solve()` or `warmupHighs()` call—this works in both main-thread and Web Worker mode without `fetch()` (pass `{ useWorker: false }` if Workers/`blob:` are blocked). See [Runtime Environments & Deployment](docs/getting-started/runtimes.md#csp-fetch-free).
|
|
3173
|
+
|
|
3055
3174
|
---
|
|
3056
3175
|
|
|
3057
3176
|
## Architecture & WebAssembly Runtime
|
|
@@ -3245,6 +3364,8 @@ const syncModel = new Model({ solver: new PumpkinSolver({ useWorker: false }) })
|
|
|
3245
3364
|
|
|
3246
3365
|
If `pumpkin.wasm` fails to load or returns an HTML fallback page, WaSHI throws an error with the attempted URL instead of silently falling back to a CDN.
|
|
3247
3366
|
|
|
3367
|
+
For CSP-restricted pages where `fetch()` is blocked (`connect-src 'none'`), load the self-registering inline WASM pack (`@ohtaman/washi/browser/solvers/pumpkin-inline` / `solvers/pumpkin-inline.js`, usable via ESM `import` or a classic `<script>` tag) before the first `solve()`, `check()`, or `warmupPumpkin()` call—this works in both main-thread and Web Worker mode without `fetch()` (pass `{ useWorker: false }` if Workers/`blob:` are blocked). See [Runtime Environments & Deployment](docs/getting-started/runtimes.md#csp-fetch-free).
|
|
3368
|
+
|
|
3248
3369
|
---
|
|
3249
3370
|
|
|
3250
3371
|
## References
|
package/llms.txt
CHANGED
|
@@ -56,7 +56,9 @@ Classic `<script>` tags load immediately invoked function expression (`IIFE`) bu
|
|
|
56
56
|
| --- | --- | --- |
|
|
57
57
|
| **Core (HiGHS & Pumpkin)** | `import { Model, expr, maximize } from "./washi/washi.js";` | `<script src="./washi/washi.min.js"></script>` (global `Washi`) |
|
|
58
58
|
| **HiGHS** *(subpath API)* | `import { HighsSolver, setHighsWasmUrl } from "./washi/solvers/highs.js";` | Bundled in `washi.min.js` (pass `{ wasmUrl }` per call) |
|
|
59
|
+
| **HiGHS Inline WASM** *(no `fetch`)* | `import "./washi/solvers/highs-inline.js";` | `<script src="./washi/solvers/highs-inline.js"></script>` (`@ohtaman/washi/browser/solvers/highs-inline`) |
|
|
59
60
|
| **Pumpkin** *(subpath API)* | `import { PumpkinSolver, setPumpkinWasmUrl } from "./washi/solvers/pumpkin.js";` | Bundled in `washi.min.js` (pass `{ wasmUrl }` per call) |
|
|
61
|
+
| **Pumpkin Inline WASM** *(no `fetch`)* | `import "./washi/solvers/pumpkin-inline.js";` | `<script src="./washi/solvers/pumpkin-inline.js"></script>` (`@ohtaman/washi/browser/solvers/pumpkin-inline`) |
|
|
60
62
|
| **GLPK** *(optional)* | `import "./washi/solvers/glpk.js";` | `<script src="./washi/solvers/glpk.min.js"></script>` (`WashiGLPK`) |
|
|
61
63
|
| **Z3** *(optional)* | `import "./washi/solvers/z3.js";` | `<script src="./washi/solvers/z3.min.js"></script>` (`WashiZ3`) |
|
|
62
64
|
|
|
@@ -68,6 +70,45 @@ Classic `<script>` tags load immediately invoked function expression (`IIFE`) bu
|
|
|
68
70
|
- **Browser self-hosting**: Deploy `dist/browser/` intact (entry scripts, `solvers/` runtime/worker/WASM assets, and chunks). Requests that hit missing URLs or HTML SPA fallbacks fail immediately and report the attempted URL.
|
|
69
71
|
- **Custom browser URLs**: Call `setHighsWasmUrl(url)` (from `@ohtaman/washi/solvers/highs`) or `setPumpkinWasmUrl(url)` (from `@ohtaman/washi/solvers/pumpkin`), or pass `{ wasmUrl }` to `m.solve()` / `m.check()`. This override is ignored for HiGHS in Node.js, while Pumpkin accepts `file://` URLs in Node.js. For Z3, call `setZ3Assets({ jsUrl, wasmUrl })` (from `@ohtaman/washi/solvers/z3`) or pass `{ z3JsUrl, z3WasmUrl }`.
|
|
70
72
|
|
|
73
|
+
#### Fetch-Free Loading in CSP-Restricted Pages (Inline WASM)
|
|
74
|
+
|
|
75
|
+
- **When `fetch()` is blocked (`connect-src 'none'`)**: Hosted AI sandboxes—including **Claude.ai Artifacts** (edit and published links), **ChatGPT inline visualizations**, and **Codex inline visualizations**—allow CDN scripts (`script-src`) while blocking `fetch()` via `connect-src` and blocking Web Workers via `worker-src`. Load the self-registering inline WASM packs (`solvers/highs-inline.js` / `@ohtaman/washi/browser/solvers/highs-inline` and `solvers/pumpkin-inline.js` / `@ohtaman/washi/browser/solvers/pumpkin-inline`) **before** your first `solve()`, `check()`, or `warmup*()` call—zero base64 or binary-setter boilerplate is required.
|
|
76
|
+
- **Dual ESM & Classic `<script>` support (prefer ESM)**: Each `*-inline.js` file has no `import`/`export` statements, so the exact same file works via ESM `import` and classic `<script src="...">`, and default loaders (`washi.js`, `washi.min.js`, `dist/lib/*`) never reference it. Prefer **ESM (`<script type="module">`)** in dynamically injected sandboxes (such as ChatGPT inline visualizations), where classic `<script src>` tags can race with inline scripts on cold loads.
|
|
77
|
+
- **Initialization semantics**: Load the inline pack **before** the first solver initialization (`solve()`, `check()`, or `warmup*()`). Before first initialization, load order between core (`washi.js` / `washi.min.js`) and `*-inline.js` does not matter. Once a solver's default WASM source is selected on first initialization, WaSHI latches it and does not switch on late pack loads (keeping main-thread and Worker behavior consistent). Explicit `set*WasmUrl()` / `{ wasmUrl }` still override the embedded pack when provided.
|
|
78
|
+
- **Worker behavior & `useWorker: false` trade-offs**: WaSHI keeps `useWorker: true` as the default on `http(s):` origins (including regular web apps, Gemini Canvas, and Google Sites, where `*-inline.js` transfers embedded bytes via `postMessage` without calling `fetch()`). In Worker-restricted sandboxes (Claude Artifacts, ChatGPT/Codex inline visualizations, or `file://`), pass `{ useWorker: false }` along with a `timeLimit` (main-thread execution blocks UI rendering, prevents immediate mid-solve `AbortSignal` interruption and live `onProgress` repaints, and lacks Worker crash isolation). Embedded previews whose CSP omits `'wasm-unsafe-eval'` (such as Antigravity's HTML artifact preview) cannot compile WebAssembly; open exported HTML in Chrome/Safari or via a local server.
|
|
79
|
+
|
|
80
|
+
```html
|
|
81
|
+
<!-- Recommended: ESM (<script type="module">) + { useWorker: false } -->
|
|
82
|
+
<!-- Works across Claude Artifacts, ChatGPT/Codex inline visualizations, Gemini Canvas, Google Sites & file:// -->
|
|
83
|
+
<script type="module">
|
|
84
|
+
import { Model, expr, maximize } from "https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/washi.js";
|
|
85
|
+
import "https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/highs-inline.js";
|
|
86
|
+
// import "https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/pumpkin-inline.js";
|
|
87
|
+
|
|
88
|
+
const m = new Model("CspFreeEsm");
|
|
89
|
+
const x = m.integer("x", { lb: 0 });
|
|
90
|
+
m.objective(maximize(x));
|
|
91
|
+
m.constraint(expr`${x} <= 10`, "cap");
|
|
92
|
+
await m.solve({ useWorker: false, timeLimit: 5 }); // Omit useWorker: false where Workers are allowed (e.g. Gemini Canvas, self-hosted)
|
|
93
|
+
</script>
|
|
94
|
+
|
|
95
|
+
<!-- Classic <script> fallback (prefer ESM above in dynamically injected sandboxes) -->
|
|
96
|
+
<script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/washi.min.js"></script>
|
|
97
|
+
<script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/highs-inline.js"></script>
|
|
98
|
+
<script>
|
|
99
|
+
(async () => {
|
|
100
|
+
const { Model, expr, maximize } = Washi;
|
|
101
|
+
const m = new Model("CspFreeClassic");
|
|
102
|
+
const x = m.integer("x", { lb: 0 });
|
|
103
|
+
m.objective(maximize(x));
|
|
104
|
+
m.constraint(expr`${x} <= 10`, "cap");
|
|
105
|
+
await m.solve({ useWorker: false, timeLimit: 5 });
|
|
106
|
+
})();
|
|
107
|
+
</script>
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
> **Single-file offline HTML (`file://` with npm packages)**: Use the CDN `*-inline.js` packs above for zero-build hosted CSP sandboxes (`connect-src 'none'`). When building a zero-network, double-clickable offline `file://` HTML app that bundles `@ohtaman/washi` (HiGHS / Pumpkin) together with third-party npm packages (React, KaTeX, Chart.js, Tailwind) via Vite + `vite-plugin-singlefile` and gzipped WASM, follow [`skills/washi/references/single-html.md`](https://github.com/ohtaman/washi/blob/main/skills/washi/references/single-html.md) (included inside the unified `washi` skill at `node_modules/@ohtaman/washi/skills/washi/references/single-html.md`).
|
|
111
|
+
|
|
71
112
|
#### Browser Security & Cross-Origin Isolation
|
|
72
113
|
|
|
73
114
|
- **HiGHS, Pumpkin, and GLPK**: Run in standard browser contexts without `SharedArrayBuffer` or isolation headers.
|
|
@@ -172,7 +213,7 @@ m.indicator(b, expr`${x} <= 50`, { activeValue: 1, name: "cap_when_b" });
|
|
|
172
213
|
- **Return value**: Returns `SolverStatus` (`"Optimal"`, `"Feasible"`, `"Infeasible"`, `"Unbounded"`, `"TimeLimit"`, `"IterationLimit"`, `"Error"`, `"Unknown"`, `"NotSolved"`, `"Sat"`, or `"Unsat"`).
|
|
173
214
|
- **Status semantics**: Optimization solvers return `"Feasible"` when a limit or abort terminates the solve with a valid primal incumbent. `"Sat"` and `"Unsat"` apply to satisfiability or custom solver adapters.
|
|
174
215
|
- **Reading results**: Check `m.hasPrimalSolution`, then read `x.value`, `` expr`...`.value ``, `m.objectiveValue`, `constr.dual`, `constr.slack`, and `x.reducedCost`. `m.values` (like `m.variables` and `m.constraints`) is a `ReadonlyMap<string, number | boolean>` (use `.get(name)`), unlike `CheckResult.values` and `m.solution.values` (`Record`).
|
|
175
|
-
- **Independent verification**: `m.verify()` checks primal feasibility—finiteness, bounds, integrality, and constraints—returning `VerificationReport` (`{ valid, complete, maxViolation, violations }`) without recomputing `m.objectiveValue`.
|
|
216
|
+
- **Independent verification**: `m.verify()` checks primal feasibility—finiteness, bounds, integrality, and constraints—returning `VerificationReport` (`{ valid, complete, maxViolation, violations }`) without recomputing `m.objectiveValue`. On decomposed models, `master.verify()` checks only the master model's constraints and integrality, so end-to-end invariants outside the master (such as TSP single-tour connectivity or full subproblem demand satisfaction) still require domain assertions.
|
|
176
217
|
|
|
177
218
|
#### Satisfiability & Conflict Diagnosis (`await m.check(options?)`)
|
|
178
219
|
|
|
@@ -185,6 +226,20 @@ m.indicator(b, expr`${x} <= 50`, { activeValue: 1, name: "cap_when_b" });
|
|
|
185
226
|
|
|
186
227
|
- Mutating variables, constraints, or the objective while an `m.solve()` or `m.check()` call is pending invalidates the in-flight run and rejects the stale promise.
|
|
187
228
|
|
|
229
|
+
### Decomposition APIs (Column Generation & Benders)
|
|
230
|
+
|
|
231
|
+
For full examples, see [`docs/guide/decomposition.md`](https://github.com/ohtaman/washi/blob/main/docs/guide/decomposition.md). `master.solveColumnGeneration(...)`, `m.relax()`, and `master.solveBenders(...)` auto-cut `sub.link(...)` mode require an LP-dual solver (**HiGHS** or **GLPK**; `Pumpkin` and `Z3` do not support LP duals or `relax: true`); manual `BendersCut` mode only calls `master.solve()`. Both helpers are also exported standalone.
|
|
232
|
+
|
|
233
|
+
- **Column Generation**: `await master.solveColumnGeneration({ pricing, finalizeAsMIP?, reducedCostTolerance?, maxIterations?, solver?, solverOptions?, onIteration? })`
|
|
234
|
+
- `pricing` receives `{ duals, getDual, iteration, master, masterSolution, reducedCostTolerance }` and returns `ColumnProposal | ColumnProposal[] | null`.
|
|
235
|
+
- Reduced cost (`cost - sum(pi_i * a_i)`) is computed and filtered automatically when `proposal.reducedCost` is omitted; `proposal.type` defaults to `"Continuous"`.
|
|
236
|
+
- Initial RMP LP relaxation must be feasible and bounded (no Phase-I artificials); `finalizeAsMIP: true` (default) runs a root-node Price-and-Branch heuristic rather than exact Branch-and-Price.
|
|
237
|
+
- **Benders Decomposition**: `await master.solveBenders({ subproblem, theta?, gapTolerance?, maxIterations?, solver?, solverOptions?, onIteration? })`
|
|
238
|
+
- `subproblem` receives `{ iteration, masterValues, getMasterValue, masterObjective, lowerBound, upperBound, master }` and returns either a continuous LP `Model` using `sub.link(constraint)` (optimality cuts only; throws on infeasible subproblem) or manual `BendersCut | BendersCut[] | null` (`{ kind: "optimality" | "feasibility", constraint, name?, subproblemObjective? }`).
|
|
239
|
+
- Auto-`theta` assumes a `Minimize` master with `lb: 0`; for `Maximize` or negative subproblem costs, declare `theta` on `master`, include it in `master.objective(...)`, and pass `{ theta }`.
|
|
240
|
+
- Manual `BendersCut.subproblemObjective` expects **Stage-1 direct cost (excluding `theta`) + Stage-2 cost**, not Stage-2 subproblem cost alone.
|
|
241
|
+
- **Low-level primitives**: `await m.relax()`, `c.dual` / `m.solution?.duals[name]`, `m.column({ cost, coefficients, type?, lb?, ub?, name? })`, and `c.addTerm(v, coeff)` / `c.setTerm(v, coeff)`.
|
|
242
|
+
|
|
188
243
|
---
|
|
189
244
|
|
|
190
245
|
## 3. Common Pitfalls & Retired Aliases
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ohtaman/washi",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "WaSHI: TypeScript & JavaScript algebraic modeler for Mathematical Optimization & Constraint Programming (LP, MILP, QP, CP, SMT) powered by WebAssembly (HiGHS, Pumpkin, Z3, GLPK)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/lib/washi.js",
|
|
@@ -47,11 +47,21 @@
|
|
|
47
47
|
"import": "./dist/browser/solvers/highs.js",
|
|
48
48
|
"default": "./dist/browser/solvers/highs.js"
|
|
49
49
|
},
|
|
50
|
+
"./browser/solvers/highs-inline": {
|
|
51
|
+
"types": "./dist/types/modeler/solvers/inline-pack.d.ts",
|
|
52
|
+
"import": "./dist/browser/solvers/highs-inline.js",
|
|
53
|
+
"default": "./dist/browser/solvers/highs-inline.js"
|
|
54
|
+
},
|
|
50
55
|
"./browser/solvers/pumpkin": {
|
|
51
56
|
"types": "./dist/types/modeler/solvers/pumpkin/index.d.ts",
|
|
52
57
|
"import": "./dist/browser/solvers/pumpkin.js",
|
|
53
58
|
"default": "./dist/browser/solvers/pumpkin.js"
|
|
54
59
|
},
|
|
60
|
+
"./browser/solvers/pumpkin-inline": {
|
|
61
|
+
"types": "./dist/types/modeler/solvers/inline-pack.d.ts",
|
|
62
|
+
"import": "./dist/browser/solvers/pumpkin-inline.js",
|
|
63
|
+
"default": "./dist/browser/solvers/pumpkin-inline.js"
|
|
64
|
+
},
|
|
55
65
|
"./browser/solvers/z3": {
|
|
56
66
|
"types": "./dist/types/modeler/solvers/z3/index.d.ts",
|
|
57
67
|
"import": "./dist/browser/solvers/z3.js",
|
|
@@ -75,6 +85,7 @@
|
|
|
75
85
|
"docs/solvers",
|
|
76
86
|
"docs/examples",
|
|
77
87
|
"skills/washi/SKILL.md",
|
|
88
|
+
"skills/washi/references/single-html.md",
|
|
78
89
|
"llms.txt",
|
|
79
90
|
"llms-full.txt",
|
|
80
91
|
"README.md",
|
package/skills/washi/SKILL.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: washi
|
|
3
|
-
description: Formulate, inspect, solve, and
|
|
3
|
+
description: Formulate, inspect, solve, verify, and package mathematical optimization (LP, MILP/MIP, convex QP, CP/LCG, column generation, Benders decomposition) and SMT satisfiability or unsat-core models in TypeScript, JavaScript, and offline single-file HTML apps using @ohtaman/washi (HiGHS, Pumpkin, Z3, GLPK) for shift scheduling, timetabling, resource allocation, bin packing, routing, portfolio optimization, and rule conflict diagnosis.
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# WaSHI Modeling & Verification Skill
|
|
@@ -20,11 +20,14 @@ Select the solver engine and execution method that match the problem class:
|
|
|
20
20
|
| **SMT Propositional Logic** (`BoolVar`, `and`, `or`, `not`, `implies`, `xor`) | **HiGHS** (auto 0-1 MIP), **Pumpkin** (bounded int), or **Z3** (native SMT incl. `Continuous`) | Bundled for HiGHS & Pumpkin; `npm install z3-solver` + `import "@ohtaman/washi/solvers/z3"` for Z3 | `await m.solve()` or `await m.check()` |
|
|
21
21
|
| **Infeasibility / Conflict Diagnosis** (`conflicts` / `unsatCore`) | **HiGHS** (relaxation `conflicts`), **Pumpkin** (bounded int/bool `unsatCore`), or **Z3** (`unsatCore`) | Bundled for HiGHS & Pumpkin; `import "@ohtaman/washi/solvers/z3"` for Z3 | `await m.check()` |
|
|
22
22
|
| **LP / MIP with GLPK** (GPL-3.0; no QP, `initialValue`, or `useWorker: true`) | **GLPK** plugin | `npm install glpk.js` + `import "@ohtaman/washi/solvers/glpk"` | `await m.solve({ solver: "glpk" })` |
|
|
23
|
+
| **Decomposition** (`solveColumnGeneration`, `solveBenders`, `m.relax()`) | **HiGHS** (default) or **GLPK** (required for LP duals / `relax: true` in `solveColumnGeneration`, `m.relax()`, and auto-cut `sub.link()`; `Pumpkin` and `Z3` lack LP duals) | Bundled for HiGHS; `import "@ohtaman/washi/solvers/glpk"` for GLPK | `await master.solveColumnGeneration(...)` / `await master.solveBenders(...)` |
|
|
23
24
|
|
|
24
25
|
- **Browser runtime notes**:
|
|
25
26
|
- **HiGHS** and **Pumpkin** load bundled `.wasm` and Worker assets relative to the bundle (no external CDN fallback; `useWorker: false` by default on opaque origins such as `file://`).
|
|
26
27
|
- **HiGHS**, **Pumpkin**, and **GLPK** all run without `COOP`/`COEP` headers (**GLPK** is an optional peer dependency that rejects `useWorker: true`).
|
|
27
28
|
- **Z3** uses `SharedArrayBuffer` pthreads and requires `window.isSecureContext === true` and `window.crossOriginIsolated === true` (`Cross-Origin-Opener-Policy: same-origin` and `Cross-Origin-Embedder-Policy: require-corp`).
|
|
29
|
+
- **Single-file / CSP-restricted AI sandboxes (blocked `fetch()`)**: Environments such as Claude.ai Artifacts (edit & published links), ChatGPT inline visualizations, and Codex inline visualizations allow CDN scripts (`script-src`) while blocking `fetch()` (`connect-src 'none'`) and Web Workers (`worker-src`). Prefer **ESM `<script type="module">` + `solvers/highs-inline.js` / `solvers/pumpkin-inline.js` + `{ useWorker: false }`** (with a `timeLimit` on non-trivial solves) before the first `solve()` / `check()` call (see Recipe 5 below). Note that embedded previews omitting `'wasm-unsafe-eval'` from `script-src` (such as Antigravity's HTML artifact preview) cannot compile WebAssembly; export `.html` and open in Chrome/Safari or via a local server.
|
|
30
|
+
- **Single-file offline HTML + npm packages (`file://`)**: To bundle `@ohtaman/washi` (HiGHS and/or Pumpkin) alongside arbitrary third-party npm packages (React, Chart.js, KaTeX, Tailwind) into one zero-network, double-clickable offline `file://` HTML file using Vite + `vite-plugin-singlefile` + gzipped WASM, follow [`references/single-html.md`](./references/single-html.md) (`node_modules/@ohtaman/washi/skills/washi/references/single-html.md`).
|
|
28
31
|
|
|
29
32
|
---
|
|
30
33
|
|
|
@@ -50,14 +53,29 @@ Select the solver engine and execution method that match the problem class:
|
|
|
50
53
|
- Named constraints: Register every constraint with a descriptive name via `const limit = m.constraint(c, "resource_limit")` (returns the registered `Constraint` / `BoolExpr`, or an array for batch inputs). Names are essential for LP inspection and conflict diagnosis (`conflicts` / `unsatCore`).
|
|
51
54
|
- Indicator constraints: Register conditional constraints via `m.indicator(binVar, c, { activeValue: 1, name: "cap_when_open" })`. When `options.M` is omitted, HiGHS/GLPK and `toLpString()` derive a tight Big-M from finite bounds, while Pumpkin lowers natively via `implied_by` and Z3 via `ctx.Implies`.
|
|
52
55
|
|
|
56
|
+
### Decomposition API Contracts (Column Generation & Benders)
|
|
57
|
+
|
|
58
|
+
For full examples, see [`docs/guide/decomposition.md`](https://github.com/ohtaman/washi/blob/main/docs/guide/decomposition.md) (`node_modules/@ohtaman/washi/docs/guide/decomposition.md`). `solveColumnGeneration()`, `m.relax()`, and `solveBenders()` auto-cut `sub.link(...)` mode require an LP-dual solver (**HiGHS** or **GLPK**; `Pumpkin` and `Z3` lack LP duals); manual `BendersCut` mode only calls `master.solve()`. Both helpers are also exported standalone.
|
|
59
|
+
|
|
60
|
+
- **Column Generation**: `await master.solveColumnGeneration({ pricing, finalizeAsMIP?, reducedCostTolerance?, maxIterations?, solver?, solverOptions?, onIteration? })`
|
|
61
|
+
- `pricing` receives `{ duals, getDual, iteration, master, masterSolution, reducedCostTolerance }` and returns `ColumnProposal | ColumnProposal[] | null` (`{ cost, coefficients, type?, lb?, ub?, name?, reducedCost?, meta? }`).
|
|
62
|
+
- Reduced cost (`cost - sum(pi_i * a_i)`) is checked automatically when `proposal.reducedCost` is omitted; `proposal.type` defaults to `"Continuous"`, so pass `type: "Integer"` or `"Binary"` for integer columns.
|
|
63
|
+
- Initial RMP LP relaxation must be feasible and bounded (no Phase-I artificials); `finalizeAsMIP: true` (default) runs a root-node Price-and-Branch heuristic rather than exact Branch-and-Price.
|
|
64
|
+
- **Benders Decomposition**: `await master.solveBenders({ subproblem, theta?, gapTolerance?, maxIterations?, solver?, solverOptions?, onIteration? })`
|
|
65
|
+
- `subproblem` receives `{ iteration, masterValues, getMasterValue, masterObjective, lowerBound, upperBound, master }` and returns a continuous LP `Model` with `sub.link(constraint)` or manual `BendersCut | BendersCut[] | null` (`{ kind: "optimality" | "feasibility", constraint, name?, subproblemObjective? }`).
|
|
66
|
+
- Auto-cut `sub.link(...)` mode generates optimality cuts only and throws if the LP subproblem is infeasible; add master feasibility guards or return manual `kind: "feasibility"` cuts.
|
|
67
|
+
- Auto-`theta` assumes a `Minimize` master with `lb: 0` (injecting `__benders_theta` unless `theta` exists); for `Maximize` or negative subproblem costs, declare `theta` on `master`, add it to `master.objective(...)`, and pass `{ theta }`.
|
|
68
|
+
- Manual `BendersCut.subproblemObjective` expects **Stage-1 direct cost (excluding `theta`) + Stage-2 cost**, not Stage-2 subproblem cost alone.
|
|
69
|
+
- **Low-level primitives**: `await m.relax()` (LP relaxation for duals), `c.dual` (or `m.solution?.duals[name]`), `m.column({ cost, coefficients, type?, lb?, ub?, name? })`, and `c.addTerm(v, coeff)` / `c.setTerm(v, coeff)`.
|
|
70
|
+
|
|
53
71
|
---
|
|
54
72
|
|
|
55
73
|
## 3. Model Inspection (`toLpString`)
|
|
56
74
|
|
|
57
|
-
|
|
75
|
+
`m.solve()` and `m.check()` already run model integrity validation automatically (variable ownership across `Model` instances, `lb <= ub`, finite `initialValue` within bounds/integrality tolerance `1e-6`, and finite coefficients), so `m.toLpString()` is an **optional debugging utility** rather than a routine step in every solve:
|
|
58
76
|
|
|
59
|
-
- **
|
|
60
|
-
- **
|
|
77
|
+
- **Avoid dumping large models**: Do **not** print `m.toLpString()` to stdout or return `lpText` unconditionally on realistic or large models (hundreds or thousands of variables/constraints)—full LP dumps bloat your context window. For production-sized models, read the TypeScript modeling code directly and rely on `m.verify()` and `m.check()`.
|
|
78
|
+
- **Targeted debugging on small reproductions**: Use `m.toLpString()` selectively on a **small toy reproduction** (or write it to a file and inspect specific lines) when diagnosing subtle objective sense, sign, coefficient, or RHS bugs via CPLEX LP format (`Maximize` / `Minimize`, `Subject To`, `Bounds`, `General`, `Binary`, `End`).
|
|
61
79
|
- **Scope caveat**: Propositional `BoolVar`s and logical/CP constraints (`BoolExpr`) are omitted from CPLEX LP output.
|
|
62
80
|
|
|
63
81
|
---
|
|
@@ -70,7 +88,7 @@ Never report a model solution as final without executing the verification step:
|
|
|
70
88
|
- Verify `status === "Optimal"` (or `status === "Feasible"` / `m.hasPrimalSolution === true` when solver limits apply).
|
|
71
89
|
- Call `const report = m.verify()` and assert `report.valid === true`.
|
|
72
90
|
- If `report.valid === false`, inspect `report.violations`, `report.missingVariables`, `report.uncheckedConstraints`, and `report.maxViolation`.
|
|
73
|
-
- Note: `m.verify()` independently checks variable finiteness, variable bounds (`lb`/`ub`), integrality (`Integer`, `Binary`, `BoolVar`), linear constraint residuals, and logical constraints. It does **not** recompute `m.objectiveValue` or verify natural-language problem coverage.
|
|
91
|
+
- Note: `m.verify()` independently checks variable finiteness, variable bounds (`lb`/`ub`), integrality (`Integer`, `Binary`, `BoolVar`), linear constraint residuals, and logical constraints. It does **not** recompute `m.objectiveValue` or verify natural-language problem coverage. On decomposed models, `master.verify()` checks only the master model's constraints, bounds, and integrality; verify end-to-end invariants outside the master (e.g., TSP single-tour connectivity or full subproblem demand satisfaction) with explicit domain assertions.
|
|
74
92
|
2. **When a model is infeasible (`"Infeasible"` or `"Unsat"`)**:
|
|
75
93
|
- Run `const res = await m.check()` for built-in HiGHS diagnosis (single-solve elastic slack relaxation), `await m.check({ solver: "pumpkin" })` for bounded integer/boolean `unsatCore`, or `await m.check({ solver: "z3" })` for exact SMT `unsatCore` including `Continuous`/fractional constraints.
|
|
76
94
|
- When `res.status === "Unsat"`, inspect `res.conflicts` (also stored on `m.conflicts`). On Z3 and Pumpkin, both `res.unsatCore` and `res.conflicts` report a contradictory subset of constraints.
|
|
@@ -81,7 +99,7 @@ Never report a model solution as final without executing the verification step:
|
|
|
81
99
|
|
|
82
100
|
## 5. Executable Workflow Recipes
|
|
83
101
|
|
|
84
|
-
### Recipe 1: MIP Formulation
|
|
102
|
+
### Recipe 1: MIP Formulation & `verify()` Verification
|
|
85
103
|
|
|
86
104
|
```ts
|
|
87
105
|
import { Model, expr, dot, sum, maximize } from "@ohtaman/washi";
|
|
@@ -104,18 +122,14 @@ export async function solveAndVerifyMip() {
|
|
|
104
122
|
name: "closed_line_zero_output",
|
|
105
123
|
});
|
|
106
124
|
|
|
107
|
-
// 1.
|
|
108
|
-
const lpText = m.toLpString();
|
|
109
|
-
|
|
110
|
-
// 2. Solve with default HiGHS solver
|
|
125
|
+
// 1. Solve with default HiGHS solver (runs integrity checks automatically)
|
|
111
126
|
const status = await m.solve();
|
|
112
127
|
|
|
113
|
-
//
|
|
128
|
+
// 2. Mandatory independent solution verification
|
|
114
129
|
const report = m.verify();
|
|
115
130
|
|
|
116
131
|
return {
|
|
117
132
|
status,
|
|
118
|
-
lpText,
|
|
119
133
|
objectiveValue: m.objectiveValue,
|
|
120
134
|
quantities: { desk: qty.desk.value, chair: qty.chair.value },
|
|
121
135
|
lineOpen: lineOpen.value,
|
|
@@ -138,13 +152,11 @@ export async function solveAndVerifyQp() {
|
|
|
138
152
|
m.objective(minimize(expr`${x}^2 + ${y}^2`));
|
|
139
153
|
m.constraint(expr`${x} + ${y} >= 4`, "target_total");
|
|
140
154
|
|
|
141
|
-
const lpText = m.toLpString();
|
|
142
155
|
const status = await m.solve();
|
|
143
156
|
const report = m.verify();
|
|
144
157
|
|
|
145
158
|
return {
|
|
146
159
|
status,
|
|
147
|
-
lpText,
|
|
148
160
|
objectiveValue: m.objectiveValue,
|
|
149
161
|
x: x.value,
|
|
150
162
|
y: y.value,
|
|
@@ -245,6 +257,55 @@ export async function solveAndDiagnoseWithPumpkin() {
|
|
|
245
257
|
}
|
|
246
258
|
```
|
|
247
259
|
|
|
260
|
+
### Recipe 5: Single-File HTML & CSP-Restricted AI Sandboxes (No `fetch`)
|
|
261
|
+
|
|
262
|
+
Hosted AI preview environments—including **Claude.ai Artifacts** (edit and published links), **ChatGPT inline visualizations**, and **Codex inline visualizations**—allow CDN scripts (`script-src`) while blocking `.wasm` `fetch()` (`connect-src 'none'`) and Web Workers (`worker-src`). Meanwhile, **Gemini Canvas** and **Google Sites** allow both `fetch()` and Web Workers, while embedded previews that omit `'wasm-unsafe-eval'` from `script-src` (such as Antigravity's HTML artifact preview) cannot run WebAssembly at all.
|
|
263
|
+
|
|
264
|
+
Load the self-registering inline WASM packs (`solvers/highs-inline.js` / `@ohtaman/washi/browser/solvers/highs-inline` and `solvers/pumpkin-inline.js` / `@ohtaman/washi/browser/solvers/pumpkin-inline`) **before** the first `solve()`, `check()`, or `warmup*()` call—no manual base64 decoding or binary setters are needed, and the same `*-inline.js` file works in both ESM and classic `<script>` mode:
|
|
265
|
+
|
|
266
|
+
```html
|
|
267
|
+
<!-- Recommended: ESM (<script type="module">) + { useWorker: false } -->
|
|
268
|
+
<!-- Works across Claude Artifacts, ChatGPT/Codex inline visualizations, Gemini Canvas, Google Sites & file:// -->
|
|
269
|
+
<script type="module">
|
|
270
|
+
import { Model, expr, maximize } from "https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/washi.js";
|
|
271
|
+
import "https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/highs-inline.js";
|
|
272
|
+
import "https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/pumpkin-inline.js";
|
|
273
|
+
|
|
274
|
+
const m = new Model("CspSafeMIP");
|
|
275
|
+
const x = m.integer("x", { lb: 0 });
|
|
276
|
+
const y = m.integer("y", { lb: 0 });
|
|
277
|
+
m.objective(maximize(expr`30 * ${x} + 50 * ${y}`));
|
|
278
|
+
m.constraint(expr`${x} + 2 * ${y} <= 40`, "wood");
|
|
279
|
+
m.constraint(expr`2 * ${x} + ${y} <= 50`, "labor");
|
|
280
|
+
await m.solve({ useWorker: false, timeLimit: 5 });
|
|
281
|
+
|
|
282
|
+
const cp = new Model("CspSafeCP", { solver: "pumpkin" });
|
|
283
|
+
const k = cp.integer("k", { lb: 0, ub: 6 });
|
|
284
|
+
cp.objective(maximize(expr`10 * ${k}`));
|
|
285
|
+
await cp.solve({ useWorker: false, timeLimit: 5 });
|
|
286
|
+
</script>
|
|
287
|
+
|
|
288
|
+
<!-- Classic <script> fallback (prefer ESM above: dynamically injected sandboxes can race external <script src> tags) -->
|
|
289
|
+
<script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/washi.min.js"></script>
|
|
290
|
+
<script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/highs-inline.js"></script>
|
|
291
|
+
<script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/pumpkin-inline.js"></script>
|
|
292
|
+
<script>
|
|
293
|
+
(async () => {
|
|
294
|
+
const { Model, expr, maximize } = Washi;
|
|
295
|
+
const m = new Model("CspSafeClassic");
|
|
296
|
+
const x = m.integer("x", { lb: 0 });
|
|
297
|
+
m.objective(maximize(x));
|
|
298
|
+
m.constraint(expr`${x} <= 10`, "cap");
|
|
299
|
+
await m.solve({ useWorker: false, timeLimit: 5 }); // Zero fetch(), zero Blob URLs, zero Workers
|
|
300
|
+
})();
|
|
301
|
+
</script>
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
- **Why ESM (`<script type="module">`) is recommended over Classic `<script>`**: In dynamically injected sandboxes (such as ChatGPT inline visualizations), classic `<script src="...">` tags may race with subsequent inline `<script>` blocks or miss `window` `load`, whereas ESM `import` strictly awaits all module dependencies before running.
|
|
305
|
+
- **`useWorker: true` vs. `useWorker: false` trade-off**: WaSHI keeps `useWorker: true` as the default on standard `http(s):` origins (including regular web apps, Gemini Canvas, and Google Sites) because Web Workers keep the UI responsive, support immediate `AbortSignal` termination, allow live `onProgress` repainting, and isolate crashes. Pass `{ useWorker: false }` (and set a `timeLimit` to prevent long UI freezes) only in Worker-restricted sandboxes (Claude Artifacts, ChatGPT/Codex inline visualizations, or `file://`).
|
|
306
|
+
- **Initialization semantics without `fetch()`**: Load `*-inline.js` before the first `solve()`, `check()`, or `warmup*()` (order between core and `*-inline.js` before init does not matter; late registration after first init is ignored to keep main-thread and Worker behavior consistent). In Web Worker mode (`useWorker: true`), WaSHI passes the embedded bytes via `postMessage` once per Worker instance without calling `fetch()`.
|
|
307
|
+
- **Other solvers**: **GLPK** (`solvers/glpk.js`) already embeds its WASM inline and requires no extra setup (GPL-3.0). **Z3** requires `SharedArrayBuffer` / `COOP`+`COEP` headers and is out of scope for standalone CSP-restricted artifacts.
|
|
308
|
+
|
|
248
309
|
---
|
|
249
310
|
|
|
250
311
|
## 6. Guardrails & Common Pitfalls
|