@ohtaman/washi 0.6.0 → 0.8.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.ja.md +139 -418
- package/README.md +139 -418
- package/README.zh-CN.md +139 -418
- package/THIRD_PARTY_LICENSES.md +39 -4
- package/dist/browser/__vite-browser-external-rTVKos2U.js +7 -0
- package/dist/browser/browser-runtime-AG5QFtKf.js +2021 -0
- package/dist/browser/highs-worker.js +26 -11
- package/dist/browser/highs.wasm +0 -0
- package/dist/browser/{loader-DewlSho8.js → loader-i3FnqMes.js} +183 -149
- package/dist/browser/solvers/glpk.js +289 -238
- package/dist/browser/solvers/glpk.min.js +1 -1
- package/dist/browser/solvers/pumpkin-_washi-pumpkin-node-runtime-CEKUDK6d.js +9 -0
- package/dist/browser/solvers/pumpkin-loader-CvdaKxf4.js +320 -0
- package/dist/browser/solvers/pumpkin-worker.js +45 -0
- package/dist/browser/solvers/pumpkin.js +1777 -0
- package/dist/browser/solvers/pumpkin.min.js +1 -0
- package/dist/browser/solvers/pumpkin.wasm +0 -0
- package/dist/browser/solvers/pumpkin.wasm.LICENSE +21 -0
- package/dist/browser/solvers/z3.js +1600 -1335
- package/dist/browser/solvers/z3.min.js +2 -2
- package/dist/browser/washi.js +3828 -1315
- package/dist/browser/washi.min.js +5 -3
- package/dist/lib/__vite-browser-external-oLchLuMx.js +7 -0
- package/dist/lib/highs-browser-runtime.js +1846 -1618
- package/dist/lib/highs-runtime.js +16 -2
- package/dist/lib/highs-worker.js +26 -11
- package/dist/lib/highs.wasm +0 -0
- package/dist/lib/{loader-BZUQNvh4.js → loader-CDpPXN_F.js} +138 -116
- package/dist/lib/registry-dx7x4e5c.js +1694 -0
- package/dist/lib/rolldown-runtime-DtPi1Y-2.js +13 -0
- package/dist/lib/solvers/glpk.js +161 -121
- package/dist/lib/solvers/pumpkin-assets.js +1 -0
- package/dist/lib/solvers/pumpkin-loader-Ow74Fa1B.js +311 -0
- package/dist/lib/solvers/pumpkin-node-runtime.js +15 -0
- package/dist/lib/solvers/pumpkin-worker-factory.js +1 -0
- package/dist/lib/solvers/pumpkin-worker.js +45 -0
- package/dist/lib/solvers/pumpkin.js +1767 -0
- package/dist/lib/solvers/pumpkin.wasm +0 -0
- package/dist/lib/solvers/pumpkin.wasm.LICENSE +21 -0
- package/dist/lib/solvers/z3.js +486 -306
- package/dist/lib/washi.js +3054 -1202
- package/dist/types/decomposition/benders.d.ts.map +1 -1
- package/dist/types/index.d.ts +3 -3
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/modeler/boolean-expr.d.ts +137 -29
- package/dist/types/modeler/boolean-expr.d.ts.map +1 -1
- package/dist/types/modeler/constraint.d.ts +40 -4
- package/dist/types/modeler/constraint.d.ts.map +1 -1
- package/dist/types/modeler/expression.d.ts +176 -28
- package/dist/types/modeler/expression.d.ts.map +1 -1
- package/dist/types/modeler/index.d.ts +5 -6
- package/dist/types/modeler/index.d.ts.map +1 -1
- package/dist/types/modeler/linearizer.d.ts +11 -2
- package/dist/types/modeler/linearizer.d.ts.map +1 -1
- package/dist/types/modeler/lp-writer.d.ts.map +1 -1
- package/dist/types/modeler/model.d.ts +97 -130
- package/dist/types/modeler/model.d.ts.map +1 -1
- package/dist/types/modeler/parser.d.ts +25 -5
- package/dist/types/modeler/parser.d.ts.map +1 -1
- package/dist/types/modeler/quad-expr.d.ts +8 -2
- package/dist/types/modeler/quad-expr.d.ts.map +1 -1
- package/dist/types/modeler/solution-validator.d.ts +12 -12
- package/dist/types/modeler/solution-validator.d.ts.map +1 -1
- package/dist/types/modeler/solvers/glpk/loader.d.ts +4 -0
- package/dist/types/modeler/solvers/glpk/loader.d.ts.map +1 -1
- package/dist/types/modeler/solvers/glpk/solver.d.ts +2 -2
- package/dist/types/modeler/solvers/glpk/solver.d.ts.map +1 -1
- package/dist/types/modeler/solvers/highs/driver.d.ts.map +1 -1
- package/dist/types/modeler/solvers/highs/loader.d.ts +8 -6
- package/dist/types/modeler/solvers/highs/loader.d.ts.map +1 -1
- package/dist/types/modeler/solvers/highs/solver.d.ts.map +1 -1
- package/dist/types/modeler/solvers/highs/worker-client.d.ts +16 -0
- package/dist/types/modeler/solvers/highs/worker-client.d.ts.map +1 -1
- package/dist/types/modeler/solvers/mock.d.ts +2 -3
- package/dist/types/modeler/solvers/mock.d.ts.map +1 -1
- package/dist/types/modeler/solvers/pumpkin/assets.d.ts +2 -0
- package/dist/types/modeler/solvers/pumpkin/assets.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/index.d.ts +6 -0
- package/dist/types/modeler/solvers/pumpkin/index.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/loader.d.ts +40 -0
- package/dist/types/modeler/solvers/pumpkin/loader.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/node-runtime.d.ts +3 -0
- package/dist/types/modeler/solvers/pumpkin/node-runtime.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/solver.d.ts +23 -0
- package/dist/types/modeler/solvers/pumpkin/solver.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/worker-client.d.ts +74 -0
- package/dist/types/modeler/solvers/pumpkin/worker-client.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/worker-factory.d.ts +3 -0
- package/dist/types/modeler/solvers/pumpkin/worker-factory.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/worker.d.ts +7 -0
- package/dist/types/modeler/solvers/pumpkin/worker.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin.d.ts +2 -0
- package/dist/types/modeler/solvers/pumpkin.d.ts.map +1 -0
- package/dist/types/modeler/solvers/registry.d.ts.map +1 -1
- package/dist/types/modeler/solvers/types.d.ts +9 -9
- package/dist/types/modeler/solvers/types.d.ts.map +1 -1
- package/dist/types/modeler/solvers/z3/loader.d.ts +4 -0
- package/dist/types/modeler/solvers/z3/loader.d.ts.map +1 -1
- package/dist/types/modeler/solvers/z3/solver.d.ts +2 -15
- package/dist/types/modeler/solvers/z3/solver.d.ts.map +1 -1
- package/dist/types/modeler/types.d.ts +12 -15
- package/dist/types/modeler/types.d.ts.map +1 -1
- package/dist/types/modeler/validation.d.ts +26 -2
- package/dist/types/modeler/validation.d.ts.map +1 -1
- package/dist/types/modeler/variable.d.ts +9 -5
- package/dist/types/modeler/variable.d.ts.map +1 -1
- package/docs/examples/diet-sensitivity.md +15 -14
- package/docs/examples/ecommerce-coupons.md +15 -22
- package/docs/examples/index.md +20 -13
- package/docs/examples/knapsack.md +8 -7
- package/docs/examples/portfolio-qp.md +11 -4
- package/docs/examples/production.md +4 -4
- package/docs/examples/project-scheduling.md +106 -0
- package/docs/examples/shift-scheduling.md +23 -7
- package/docs/examples/smt-security.md +17 -4
- package/docs/examples/transportation.md +46 -19
- package/docs/examples/traveling-salesperson.md +29 -45
- package/docs/examples/unsat-core.md +16 -5
- package/docs/examples/vehicle-routing.md +10 -8
- package/docs/getting-started/ai-agents.md +114 -34
- package/docs/getting-started/coding-agents.md +158 -0
- package/docs/getting-started/cross-origin.md +56 -28
- package/docs/getting-started/index.md +36 -66
- package/docs/getting-started/runtimes.md +98 -69
- package/docs/guide/constraint-programming.md +141 -0
- package/docs/guide/constraints.md +100 -44
- package/docs/guide/decomposition.md +141 -273
- package/docs/guide/expressions.md +125 -42
- package/docs/guide/formulation-tips.md +212 -0
- package/docs/guide/objectives.md +70 -64
- package/docs/guide/unsat-core.md +44 -23
- package/docs/guide/variables.md +56 -48
- package/docs/solvers/custom-solver.md +18 -13
- package/docs/solvers/highs.md +41 -25
- package/docs/solvers/index.md +13 -7
- package/docs/solvers/pumpkin.md +163 -0
- package/docs/solvers/z3.md +4 -1
- package/llms-full.txt +1732 -860
- package/llms.txt +59 -36
- package/package.json +40 -13
- package/skills/washi/SKILL.md +91 -35
- package/dist/browser/_washi-node-only-BvRk9kiK.js +0 -0
- package/dist/browser/browser-runtime-TLCY0hkc.js +0 -1793
- package/dist/lib/_washi-node-only-BvRk9kiK.js +0 -0
- package/dist/lib/registry-C20sxTwm.js +0 -964
- package/docs/ja/getting-started/ai-agents.md +0 -270
- package/docs/zh/getting-started/ai-agents.md +0 -270
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# Coding with AI Agents
|
|
2
|
+
|
|
3
|
+
This guide shows how to configure AI coding agents (Claude Code, Cursor, Codex, Antigravity, Gemini CLI, Windsurf) to formulate, solve, verify, and package optimization and constraint models with `@ohtaman/washi`.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 1. Setup
|
|
8
|
+
|
|
9
|
+
Setting up WaSHI with a coding agent has two separate parts: installing the runtime library in your project, and giving your agent WaSHI's current API and verification workflow.
|
|
10
|
+
|
|
11
|
+
### Install the Runtime Library
|
|
12
|
+
|
|
13
|
+
Bundled **HiGHS** (LP / MIP / Convex QP) and **Pumpkin** (`@ohtaman/washi/solvers/pumpkin`, CP / LCG) require zero external dependencies:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @ohtaman/washi
|
|
17
|
+
|
|
18
|
+
# Optional peer dependencies if your model requires SMT continuous logic or GLPK:
|
|
19
|
+
# npm install z3-solver
|
|
20
|
+
# npm install glpk.js
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Connect Your Coding Agent
|
|
24
|
+
|
|
25
|
+
Choose the setup method that matches your tool:
|
|
26
|
+
|
|
27
|
+
- **Agent Skill (Recommended — Claude Code, Cursor, Codex, Antigravity, Gemini CLI)**:
|
|
28
|
+
```bash
|
|
29
|
+
npx skills add ohtaman/washi
|
|
30
|
+
```
|
|
31
|
+
This installs `skills/washi/SKILL.md` (also included at `node_modules/@ohtaman/washi/skills/washi/SKILL.md`), which guides the agent through solver selection across HiGHS / Pumpkin / Z3 / GLPK, algebraic `expr` modeling, `m.toLpString()` formulation checks, `m.verify()` solution verification, and `m.check()` conflict diagnosis.
|
|
32
|
+
- **IDE Documentation Indexing (`@Docs` in Cursor, Windsurf, etc.)**:
|
|
33
|
+
- Documentation index: `https://washi.ohtaman.dev/llms.txt`
|
|
34
|
+
- Complete single-file API & example reference: `https://washi.ohtaman.dev/llms-full.txt`
|
|
35
|
+
- **Context7 MCP**:
|
|
36
|
+
- Reference library ID: `/ohtaman/washi`
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## 2. Your First End-to-End Run
|
|
41
|
+
|
|
42
|
+
Copy and paste the starter prompt below into your coding agent inside a Node.js / TypeScript project where `@ohtaman/washi` is installed:
|
|
43
|
+
|
|
44
|
+
```text
|
|
45
|
+
Using @ohtaman/washi, write and run a TypeScript script that schedules 8 internal tech-talk sessions across 2 rooms (Room A, Room B) and 4 time slots.
|
|
46
|
+
Input data: create a sample array of talks with speaker, topic ("infra", "frontend", "data", "security"), and unavailable slots.
|
|
47
|
+
Requirements:
|
|
48
|
+
1. Assign every talk to exactly one room and one time slot, with at most one talk per room-slot.
|
|
49
|
+
2. Respect speaker unavailability and prevent any speaker from giving two talks in the same time slot.
|
|
50
|
+
3. Minimize the number of parallel talks sharing the same topic in the same time slot.
|
|
51
|
+
4. Give every constraint an explicit descriptive name, solve with bundled HiGHS, check m.hasPrimalSolution, and assert m.verify().valid === true before printing the timetable.
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
When you run this prompt, the agent executes three steps locally:
|
|
55
|
+
|
|
56
|
+
1. **Formulates the model**: Creates bounded binary variables (`m.binary`), linear expressions (`` expr`...` ``, `sum`), named constraints (`m.constraint(..., "name")`), and an objective (`m.objective(minimize(...))`).
|
|
57
|
+
2. **Solves in Node.js via WebAssembly**: Executes the script (for example with `npx tsx schedule.ts`) using the bundled HiGHS WebAssembly binary—no Python runtime or native solver installation is needed.
|
|
58
|
+
3. **Independently verifies the solution**: Checks `m.hasPrimalSolution` and calls `m.verify()` so variable bounds, integrality, and constraint residuals are verified before displaying the output.
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
## 3. Problem Classes & Ready-to-Use Prompts
|
|
63
|
+
|
|
64
|
+
Each prompt below targets a specific mathematical problem class supported by WaSHI. Adjust the input schema and rules to match your data.
|
|
65
|
+
|
|
66
|
+
### Event Timetable & Reviewer Assignment (MILP — Bundled HiGHS)
|
|
67
|
+
|
|
68
|
+
```text
|
|
69
|
+
Using @ohtaman/washi (bundled HiGHS), assign 24 pull-request / RFC submissions to 6 engineers so each submission gets 2 distinct reviewers.
|
|
70
|
+
Rules:
|
|
71
|
+
- Authors cannot review their own submissions, and reviewers must match at least one required domain tag on the submission.
|
|
72
|
+
- Respect each engineer's maximum review cap, and minimize the maximum workload assigned to any single engineer (min-max load balancing).
|
|
73
|
+
- Name every constraint, run the script, and verify m.hasPrimalSolution and m.verify().valid === true.
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Budget & Project Portfolio Selection (0-1 MIP — Bundled HiGHS)
|
|
77
|
+
|
|
78
|
+
```text
|
|
79
|
+
Using @ohtaman/washi (bundled HiGHS), select a subset of candidate engineering projects from a JSON list to maximize total expected annual savings.
|
|
80
|
+
Each project has cost, backend_engineers, frontend_engineers, and savings.
|
|
81
|
+
Rules:
|
|
82
|
+
- Stay within a total quarterly budget of 120 and headcount caps of 14 backend and 10 frontend engineers.
|
|
83
|
+
- Dependency: if "billing_v2" is selected, "auth_token_service" must also be selected.
|
|
84
|
+
- Mutual exclusion: select at most one of "legacy_search_tuning" and "vector_search_migration".
|
|
85
|
+
- Give every constraint a clear name and confirm m.verify().valid === true.
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Task Scheduling with Precedence & Shared Resources (CP/LCG — Bundled Pumpkin)
|
|
89
|
+
|
|
90
|
+
```text
|
|
91
|
+
Using @ohtaman/washi and its bundled CP solver (@ohtaman/washi/solvers/pumpkin), schedule 10 release-pipeline tasks with integer start times (lb: 0, ub: 120) and fixed durations to minimize total makespan.
|
|
92
|
+
Rules:
|
|
93
|
+
- Enforce finish-to-start precedence edges between dependent tasks.
|
|
94
|
+
- Tasks using the staging benchmark cluster cannot overlap (use disjoint).
|
|
95
|
+
- All running tasks share at most 4 concurrent CI runners at any time (use cumulative).
|
|
96
|
+
- Solve with { solver: "pumpkin" }, verify m.hasPrimalSolution and m.verify().valid === true, and print start/end times for each task.
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Target-Balancing & Variance Minimization (Convex QP — Bundled HiGHS)
|
|
100
|
+
|
|
101
|
+
Built-in HiGHS solves convex quadratic objectives over continuous variables (`m.continuous`):
|
|
102
|
+
|
|
103
|
+
```text
|
|
104
|
+
Using @ohtaman/washi (bundled HiGHS), allocate a continuous total budget of 1.0 across 5 regional compute pools using m.continuous variables (lb: 0.05, ub: 0.50).
|
|
105
|
+
Target weights are [0.30, 0.25, 0.20, 0.15, 0.10], plus a policy cap that regions 0 and 1 combined cannot exceed 0.48.
|
|
106
|
+
Minimize the sum of squared deviations expr`sum((${w} - ${target})^2)` subject to sum(w) == 1.0 and the regional cap, then verify m.verify().valid === true.
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Rule & Configuration Conflict Diagnosis (Infeasibility / Unsat Core)
|
|
110
|
+
|
|
111
|
+
```text
|
|
112
|
+
Using @ohtaman/washi and @ohtaman/washi/solvers/pumpkin, check whether a set of deployment and feature-flag rules over bounded integer and boolean variables is satisfiable via await m.check({ solver: "pumpkin" }).
|
|
113
|
+
Include a sample test case with contradictory named rules, print res.status and res.unsatCore (or res.conflicts), and show which named rules conflict with each other.
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
> [!NOTE]
|
|
117
|
+
> `await m.check()` works out of the box with default **HiGHS** (reporting conflicting constraint names in `res.conflicts` via elastic slack relaxation). Import bundled `@ohtaman/washi/solvers/pumpkin` when you want `res.unsatCore` on bounded integer/boolean models without `COOP`/`COEP` headers, or install `z3-solver` (`@ohtaman/washi/solvers/z3`) when continuous/fractional constraints require exact SMT `unsatCore` extraction.
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## 4. Packaging & Deployment Prompts (Browser, Desktop & Mobile)
|
|
122
|
+
|
|
123
|
+
Once a CLI script produces verified results, you can ask your coding agent to package the same model into a local browser tool or an offline desktop/mobile application.
|
|
124
|
+
|
|
125
|
+
### Zero-Server Single-File `.html` Tool (`file://` Double-Click)
|
|
126
|
+
|
|
127
|
+
```text
|
|
128
|
+
Package this @ohtaman/washi optimization model into a single standalone .html file with inline HTML, CSS, and JavaScript so team members can open it directly via file:// by double-clicking.
|
|
129
|
+
- Load WaSHI from https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/washi.min.js (global window.Washi).
|
|
130
|
+
- Provide a textarea / CSV file input, a "Solve" button, a table displaying the optimal assignment, and a conflict report using await m.check() if status is "Infeasible".
|
|
131
|
+
- Verify every feasible solve with m.verify().
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
- **Runtime notes for `file://`**:
|
|
135
|
+
- Opening the `.html` file directly via `file://` (no local HTTP server) works out of the box when loading `washi.min.js` (and `solvers/pumpkin.min.js` when using Pumpkin) from an HTTPS CDN, because WaSHI detects opaque origins (`window.origin === "null"`) and automatically runs HiGHS and Pumpkin on the main thread.
|
|
136
|
+
- Self-hosted local `.wasm` files (which browsers block on `file://` under `fetch()` CORS rules) and **Z3** (`SharedArrayBuffer` requiring `COOP`/`COEP` headers) require serving over `http://` or `https://`.
|
|
137
|
+
|
|
138
|
+
### Offline Desktop & Mobile Apps (Vite + Tauri / Capacitor / PWA)
|
|
139
|
+
|
|
140
|
+
```text
|
|
141
|
+
Wrap this Vite + TypeScript @ohtaman/washi application as an offline desktop/mobile app using bundled HiGHS (or @ohtaman/washi/solvers/pumpkin) with zero backend server and zero native C++ solver compilation.
|
|
142
|
+
- Keep all solver execution client-side via @ohtaman/washi.
|
|
143
|
+
- Configure Content Security Policy to allow WebAssembly compilation ('wasm-unsafe-eval' in script-src) and Web Workers (worker-src 'self' blob:).
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
- **Runtime notes for WebViews**: Standard Vite builds bundle `highs.wasm` and `pumpkin.wasm` as static assets that run offline inside Tauri (`tauri://localhost` / `http://tauri.localhost`), Capacitor WebViews, and Service Worker PWAs. See [Runtime Environments](./runtimes.md) for `tauri.conf.json` CSP configuration (`'wasm-unsafe-eval'`).
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
## 5. Iterating & Debugging with Your Agent
|
|
151
|
+
|
|
152
|
+
- **Require named constraints and `m.verify()`**: Always ask your agent to pass a descriptive name as the second argument to `m.constraint(c, "rule_name")` (and `name` in `m.indicator(...)`) and assert `m.verify().valid === true`. Unnamed constraints (`c_0`, `c_1`) make infeasibility reports hard to interpret.
|
|
153
|
+
- **Diagnose `"Infeasible"` results with `m.check()`**: When adding new business rules causes `m.solve()` to return `"Infeasible"`, use a follow-up prompt like:
|
|
154
|
+
```text
|
|
155
|
+
Adding the new constraints made m.solve() return "Infeasible".
|
|
156
|
+
Run await m.check() (or await m.check({ solver: "pumpkin" }) for bounded integer/boolean models), print res.conflicts / res.unsatCore, explain in plain terms which named rules and variable bounds contradict each other, and propose which non-rigid constraints should be converted into soft constraints with non-negative slack penalty variables.
|
|
157
|
+
```
|
|
158
|
+
- **Inspect `m.toLpString()` and variable bounds before production**: Ask your agent to dump `m.toLpString()` on a small fixture and review variable `lb` / `ub` bounds (especially on variables referenced inside `m.indicator()`, which require finite bounds when `M` is omitted), objective sense (`Maximize` vs `Minimize`), and constraint signs (`<=` vs `>=`).
|
|
@@ -1,47 +1,70 @@
|
|
|
1
1
|
# Cross-Origin Isolation (Z3 & Browsers)
|
|
2
2
|
|
|
3
|
-
When running the **Z3** solver
|
|
3
|
+
When running the **Z3** solver inside a web browser, Cross-Origin Isolation HTTP headers are required so the browser enables `SharedArrayBuffer`.
|
|
4
|
+
|
|
5
|
+
> [!TIP]
|
|
6
|
+
> **Not needed for HiGHS, Pumpkin, GLPK, or Node.js**: Bundled **HiGHS** and **Pumpkin** (`@ohtaman/washi/solvers/pumpkin`), as well as the **GLPK** plugin, run in standard browser contexts without Cross-Origin Isolation or `SharedArrayBuffer` (including Pumpkin CP solving, propositional logic, and `unsatCore` conflict diagnosis). Running Z3 in **Node.js** also requires no HTTP headers. You only need this page when using `@ohtaman/washi/solvers/z3` in the browser.
|
|
4
7
|
|
|
5
8
|
---
|
|
6
9
|
|
|
7
10
|
## Why is Cross-Origin Isolation Required?
|
|
8
11
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
In modern browsers (Chrome, Firefox, Safari, Edge), `SharedArrayBuffer` is disabled by default to mitigate Spectre-style timing side-channel attacks. To re-enable it, the hosting page must be **Cross-Origin Isolated**.
|
|
12
|
+
Z3's WebAssembly build uses multithreading (pthreads) backed by `SharedArrayBuffer` and Web Workers. Modern browsers (Chrome, Firefox, Safari, Edge) disable `SharedArrayBuffer` by default to mitigate Spectre-style side-channel attacks unless the top-level document opts into **Cross-Origin Isolation** (`window.crossOriginIsolated === true`).
|
|
12
13
|
|
|
13
14
|
---
|
|
14
15
|
|
|
15
16
|
## Required HTTP Response Headers
|
|
16
17
|
|
|
17
|
-
|
|
18
|
+
Serve the top-level HTML document with:
|
|
18
19
|
|
|
19
20
|
```http
|
|
20
21
|
Cross-Origin-Opener-Policy: same-origin
|
|
21
22
|
Cross-Origin-Embedder-Policy: require-corp
|
|
22
23
|
```
|
|
23
24
|
|
|
24
|
-
|
|
25
|
+
If your page loads cross-origin subresources (such as scripts, images, or WASM files from another domain), those responses must also include `Cross-Origin-Resource-Policy: cross-origin` (or CORS headers).
|
|
25
26
|
|
|
26
|
-
|
|
27
|
-
Cross-Origin-
|
|
28
|
-
```
|
|
27
|
+
> [!NOTE]
|
|
28
|
+
> Chromium and Firefox also support `Cross-Origin-Embedder-Policy: credentialless`, which relaxes subresource CORP requirements, **but Safari (WebKit) does not support `credentialless`**. Use `require-corp` if you support Safari / iOS.
|
|
29
29
|
|
|
30
30
|
---
|
|
31
31
|
|
|
32
|
-
##
|
|
32
|
+
## Setup by Environment
|
|
33
33
|
|
|
34
|
-
### 1.
|
|
34
|
+
### 1. Vite (`vite.config.ts`)
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
36
|
+
Configure both the local dev server (`vite`) and production preview server (`vite preview`):
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { defineConfig } from "vite";
|
|
40
|
+
|
|
41
|
+
export default defineConfig({
|
|
42
|
+
server: {
|
|
43
|
+
headers: {
|
|
44
|
+
"Cross-Origin-Opener-Policy": "same-origin",
|
|
45
|
+
"Cross-Origin-Embedder-Policy": "require-corp",
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
preview: {
|
|
49
|
+
headers: {
|
|
50
|
+
"Cross-Origin-Opener-Policy": "same-origin",
|
|
51
|
+
"Cross-Origin-Embedder-Policy": "require-corp",
|
|
52
|
+
},
|
|
53
|
+
},
|
|
41
54
|
});
|
|
42
55
|
```
|
|
43
56
|
|
|
44
|
-
### 2.
|
|
57
|
+
### 2. Cloudflare Pages / Netlify (`public/_headers`)
|
|
58
|
+
|
|
59
|
+
Place a `_headers` file in `public/` so it is copied to the root of your build output:
|
|
60
|
+
|
|
61
|
+
```text
|
|
62
|
+
/*
|
|
63
|
+
Cross-Origin-Opener-Policy: same-origin
|
|
64
|
+
Cross-Origin-Embedder-Policy: require-corp
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### 3. Next.js (`next.config.js`)
|
|
45
68
|
|
|
46
69
|
```javascript
|
|
47
70
|
/** @type {import('next').NextConfig} */
|
|
@@ -62,26 +85,31 @@ const nextConfig = {
|
|
|
62
85
|
module.exports = nextConfig;
|
|
63
86
|
```
|
|
64
87
|
|
|
65
|
-
###
|
|
88
|
+
### 4. Custom Node.js / Express Server
|
|
89
|
+
|
|
90
|
+
```javascript
|
|
91
|
+
app.use((req, res, next) => {
|
|
92
|
+
res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
|
|
93
|
+
res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
|
|
94
|
+
next();
|
|
95
|
+
});
|
|
96
|
+
```
|
|
66
97
|
|
|
67
|
-
|
|
98
|
+
### 5. Static Hosting without Header Control (e.g., GitHub Pages)
|
|
68
99
|
|
|
69
|
-
|
|
100
|
+
Platforms like GitHub Pages do not allow customizing HTTP response headers directly. You can use **`coi-serviceworker`** to inject the isolation headers via a client-side Service Worker:
|
|
70
101
|
|
|
71
|
-
1.
|
|
72
|
-
2.
|
|
102
|
+
1. Obtain `coi-serviceworker.js` from [`coi-serviceworker`](https://github.com/gzuidhof/coi-serviceworker) (GitHub / npm) or WaSHI's reference [`public/coi-serviceworker.js`](https://github.com/ohtaman/washi/blob/main/public/coi-serviceworker.js), and place it at your site root.
|
|
103
|
+
2. Load it in your HTML `<head>`:
|
|
73
104
|
|
|
74
105
|
```html
|
|
75
106
|
<script src="/coi-serviceworker.js"></script>
|
|
76
107
|
```
|
|
77
108
|
|
|
78
|
-
|
|
109
|
+
On the first visit, the Service Worker registers and reloads the page once to establish `window.crossOriginIsolated === true`.
|
|
79
110
|
|
|
80
111
|
---
|
|
81
112
|
|
|
82
|
-
##
|
|
83
|
-
|
|
84
|
-
WaSHI's Z3 solver adapter automatically inspects the environment at runtime. If Z3 is invoked in a browser lacking cross-origin isolation, it immediately rejects with a clear, informative error message explaining that COOP/COEP headers or a service worker are required, rather than hanging silently.
|
|
113
|
+
## Runtime Check in WaSHI
|
|
85
114
|
|
|
86
|
-
|
|
87
|
-
> The default **HiGHS** solver engine does not require cross-origin isolation or `SharedArrayBuffer`. It works out of the box in standard browser contexts.
|
|
115
|
+
WaSHI's Z3 adapter checks `crossOriginIsolated` before initializing Z3 in the browser. If Cross-Origin Isolation is missing, `solve()` / `check()` immediately rejects with an actionable error explaining that COOP/COEP headers or a Service Worker are required, rather than hanging silently.
|
|
@@ -1,19 +1,12 @@
|
|
|
1
1
|
# Introduction & Installation
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
WaSHI brings the algebraic modeling style of Python's **PuLP** and Julia's **JuMP** to the TypeScript and JavaScript ecosystem. You can formulate mathematical optimization problems using algebraic expressions and solve them locally using WebAssembly solvers (**HiGHS**, **Z3**, and **GLPK**)—without a separate Python service or native C/C++ build steps.
|
|
6
|
-
|
|
7
|
-
### Why TypeScript & WebAssembly?
|
|
8
|
-
|
|
9
|
-
- **Removing the deployment bottleneck**: In traditional optimization workflows (e.g., Python with PuLP or Pyomo), turning a model into an interactive application requires provisioning a backend service and managing native C/C++ solver binaries. As AI-assisted development accelerates UI and application prototyping, backend infrastructure often becomes the primary bottleneck to shipping. Running both the modeler and WebAssembly solvers on standard Web technologies allows an optimization tool to be deployed as static assets (e.g., on GitHub Pages or Cloudflare Pages, serving the generated JS, Worker, and WASM files; with COOP/COEP headers when using Z3)—and makes it straightforward to pair mathematical optimization with in-browser LLMs (e.g., WebGPU / WebLLM) entirely on the client.
|
|
10
|
-
- **Algebraic syntax without operator overloading (`expr`)**: Because JavaScript lacks operator overloading, libraries usually trade off between verbose method chaining (`x.mul(30).add(y.mul(50))`) and untyped strings (`"30 * x + 50 * y"`). WaSHI uses tagged template literals (`` expr`30 * ${x} + 50 * ${y} <= 40` ``) similar to parameterized SQL queries—parsing infix operators while binding `${x}` directly as typed TypeScript object references for static type checking, IDE symbol renaming, and multi-dimensional indexing (`${flow[i][j]}`).
|
|
3
|
+
**WaSHI** (WebAssembly-backed, Solver-independent, High-level modeling Interface) brings the algebraic modeling style of Python's **PuLP** to the TypeScript and JavaScript ecosystem. Write expressions naturally with tagged templates (`expr`) and solve mathematical optimization and constraint programming problems directly in **Node.js and the browser** using WebAssembly solvers (**HiGHS**, **Pumpkin**, **Z3**, and **GLPK**).
|
|
11
4
|
|
|
12
5
|
---
|
|
13
6
|
|
|
14
7
|
## 📦 Installation
|
|
15
8
|
|
|
16
|
-
|
|
9
|
+
Both the default **HiGHS** solver (LP / MIP / Convex QP) and the **Pumpkin** CP/LCG solver (`@ohtaman/washi/solvers/pumpkin`) are bundled with zero external dependencies and work out of the box:
|
|
17
10
|
|
|
18
11
|
::: code-group
|
|
19
12
|
|
|
@@ -35,82 +28,59 @@ bun add @ohtaman/washi
|
|
|
35
28
|
|
|
36
29
|
:::
|
|
37
30
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
WaSHI ships with **HiGHS** built-in with zero configuration. If you need SMT logic / Unsat Core diagnosis via **Z3**, or compatibility with **GLPK**, install their respective peer dependencies:
|
|
31
|
+
Install optional peer dependencies when you need **Z3** (SMT logic and Unsat Core conflict diagnosis) or **GLPK**:
|
|
41
32
|
|
|
42
33
|
```bash
|
|
43
|
-
# For SMT logic and Unsat Core conflict diagnosis
|
|
44
|
-
npm install
|
|
45
|
-
|
|
46
|
-
# For GLPK solver (GPL-3.0)
|
|
47
|
-
npm install glpk.js
|
|
34
|
+
npm install z3-solver # For SMT logic and Unsat Core conflict diagnosis
|
|
35
|
+
npm install glpk.js # For GLPK solver (GPL-3.0)
|
|
48
36
|
```
|
|
49
37
|
|
|
50
|
-
|
|
38
|
+
Using an AI coding agent? See [Coding with AI Agents](./coding-agents.md) for `npx skills add ohtaman/washi`, `llms.txt`, and ready-to-use prompt templates.
|
|
51
39
|
|
|
52
|
-
|
|
40
|
+
---
|
|
53
41
|
|
|
54
|
-
|
|
42
|
+
## 🚀 Quick Start
|
|
55
43
|
|
|
56
|
-
|
|
44
|
+
Here is a complete production-planning model illustrating the three steps of a typical WaSHI workflow:
|
|
57
45
|
|
|
58
46
|
```typescript
|
|
59
47
|
import { Model, expr, maximize } from "@ohtaman/washi";
|
|
60
|
-
```
|
|
61
48
|
|
|
62
|
-
|
|
49
|
+
const m = new Model("production");
|
|
63
50
|
|
|
64
|
-
|
|
65
|
-
const
|
|
66
|
-
|
|
51
|
+
// 1. Define decision variables with realistic finite bounds (lb / ub)
|
|
52
|
+
const desks = m.integer("desks", { lb: 0, ub: 100 });
|
|
53
|
+
const chairs = m.integer("chairs", { lb: 0, ub: 100 });
|
|
67
54
|
|
|
68
|
-
|
|
55
|
+
// 2. Set the objective function and constraints
|
|
56
|
+
m.objective(maximize(expr`30 * ${desks} + 50 * ${chairs}`));
|
|
57
|
+
m.constraint(expr`${desks} + 2 * ${chairs} <= 40`, "wood_capacity");
|
|
58
|
+
m.constraint(expr`2 * ${desks} + ${chairs} <= 50`, "labor_capacity");
|
|
69
59
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const
|
|
74
|
-
|
|
60
|
+
// 3. Solve (built-in HiGHS WebAssembly) and verify the solution
|
|
61
|
+
const status = await m.solve();
|
|
62
|
+
if (m.hasPrimalSolution) {
|
|
63
|
+
const report = m.verify();
|
|
64
|
+
console.log(status, report.valid); // "Optimal", true
|
|
65
|
+
console.log(`Profit: ${m.objectiveValue} (desks=${desks.value}, chairs=${chairs.value})`); // 1100 (desks=20, chairs=10)
|
|
66
|
+
}
|
|
75
67
|
```
|
|
76
68
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
```typescript
|
|
82
|
-
// Maximize 30*x + 50*y
|
|
83
|
-
model.objective(maximize(expr`30 * ${x} + 50 * ${y}`));
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
### 5. Add Constraints
|
|
87
|
-
|
|
88
|
-
Use `model.constraint()` with the `expr` tagged template to formulate equality and inequality constraints:
|
|
89
|
-
|
|
90
|
-
```typescript
|
|
91
|
-
model.constraint(expr`${x} + 2 * ${y} <= 40`, "wood_capacity");
|
|
92
|
-
model.constraint(expr`2 * ${x} + ${y} <= 50`, "labor_capacity");
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
### 6. Solve and Extract Results
|
|
96
|
-
|
|
97
|
-
Solve the model asynchronously via WebAssembly (HiGHS by default):
|
|
98
|
-
|
|
99
|
-
```typescript
|
|
100
|
-
const status = await model.solve();
|
|
101
|
-
|
|
102
|
-
console.log("Status:", status); // "Optimal"
|
|
103
|
-
console.log("Optimal Objective Value:", model.objectiveValue); // 1100
|
|
104
|
-
console.log(`Solution: x = ${x.value}, y = ${y.value}`); // x = 20, y = 10
|
|
105
|
-
```
|
|
69
|
+
1. **Define decision variables (`lb`, `ub`)**: Create variables with `m.continuous()`, `m.integer()`, or `m.binary()`, providing finite `lb` and `ub` bounds when known.
|
|
70
|
+
2. **Describe the objective and constraints (`expr`)**: Formulate equations naturally with `` expr`...` ``, and pass constraint names to make infeasibility debugging straightforward.
|
|
71
|
+
3. **Solve and verify (`solve`, `verify`)**: `await m.solve()` runs the bundled HiGHS WebAssembly solver. Check `m.hasPrimalSolution` before reading `.value`, and call `m.verify()` to independently verify that bounds, integrality, and constraints hold.
|
|
106
72
|
|
|
107
73
|
---
|
|
108
74
|
|
|
109
75
|
## 🧭 Next Steps
|
|
110
76
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
-
|
|
114
|
-
-
|
|
115
|
-
|
|
77
|
+
### Choose Your Workflow
|
|
78
|
+
|
|
79
|
+
- **Build with an AI Coding Agent**: [Coding with AI Agents](./coding-agents.md) — Setup with `npx skills add`, `llms.txt` / Context7, ready-to-use prompt examples across problem types and deployment targets, and conflict debugging
|
|
80
|
+
- **Write Models Directly in TypeScript**: [Modeling Guide](../guide/variables.md) — Array/dictionary variables, aggregations (`sum`, `dot`), logical constraints, and conflict diagnosis
|
|
81
|
+
|
|
82
|
+
### Reference & Examples
|
|
116
83
|
|
|
84
|
+
- **[Examples & API Finder](../examples/)**: 12 runnable optimization and SMT recipes—from shift scheduling and portfolio QP to vehicle routing
|
|
85
|
+
- **[Solvers Overview](../solvers/)**: Compare bundled HiGHS and Pumpkin with optional Z3 and GLPK adapters
|
|
86
|
+
- **[Runtime Environments](./runtimes.md)**: Setup details for Node.js, Vite / Next.js bundlers, CDNs, standalone HTML, and desktop/mobile WebViews
|