@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
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
# Runtime Environments & Deployment
|
|
2
2
|
|
|
3
|
-
WaSHI runs across JavaScript and TypeScript runtimes—from Node.js, Bun, and Deno to modern bundlers, vanilla browser `<script>` tags
|
|
3
|
+
WaSHI runs across JavaScript and TypeScript runtimes—from Node.js, Bun, and Deno to modern bundlers, static hosting, and vanilla browser `<script>` tags—without native C/C++ compilation.
|
|
4
4
|
|
|
5
5
|
---
|
|
6
6
|
|
|
7
|
-
## 🟢 Node.js
|
|
7
|
+
## 🟢 Node.js, Bun & Deno
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
### Node.js
|
|
10
|
+
|
|
11
|
+
WaSHI supports Node.js (v18+) out of the box with standard ECMAScript Modules (ESM). Both the built-in HiGHS WebAssembly binary and the bundled Pumpkin WebAssembly binary (`@ohtaman/washi/solvers/pumpkin`) are loaded directly from the installed package:
|
|
10
12
|
|
|
11
13
|
```typescript
|
|
12
14
|
import { Model, expr, maximize } from "@ohtaman/washi";
|
|
@@ -19,8 +21,10 @@ m.objective(maximize(expr`2 * ${x} + 3 * ${y}`));
|
|
|
19
21
|
m.constraint(expr`${x} + ${y} <= 15`, "capacity");
|
|
20
22
|
|
|
21
23
|
await m.solve();
|
|
22
|
-
|
|
23
|
-
console.log(`
|
|
24
|
+
if (m.hasPrimalSolution) {
|
|
25
|
+
console.log(`Status: ${m.status}, Objective: ${m.objectiveValue}`);
|
|
26
|
+
console.log(`x = ${x.value}, y = ${y.value}`);
|
|
27
|
+
}
|
|
24
28
|
```
|
|
25
29
|
|
|
26
30
|
Run directly with `tsx` or `node`:
|
|
@@ -29,11 +33,33 @@ Run directly with `tsx` or `node`:
|
|
|
29
33
|
npx tsx app.ts
|
|
30
34
|
```
|
|
31
35
|
|
|
36
|
+
### Bun & Deno
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
# Bun
|
|
40
|
+
bun add @ohtaman/washi
|
|
41
|
+
bun run app.ts
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
In Deno (v1.30+ with `npm:` specifiers):
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { Model, maximize } from "npm:@ohtaman/washi";
|
|
48
|
+
|
|
49
|
+
const m = new Model();
|
|
50
|
+
const x = m.continuous("x", { lb: 0, ub: 10 });
|
|
51
|
+
m.objective(maximize(x));
|
|
52
|
+
await m.solve();
|
|
53
|
+
if (m.hasPrimalSolution) {
|
|
54
|
+
console.log("x =", x.value);
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
32
58
|
---
|
|
33
59
|
|
|
34
|
-
## 🌐 Bundlers (Vite, Webpack, Rollup
|
|
60
|
+
## 🌐 Bundlers & Static Hosting (Vite, Next.js, Webpack, Rollup)
|
|
35
61
|
|
|
36
|
-
### Modern Bundlers
|
|
62
|
+
### Modern Bundlers & Static Deployment
|
|
37
63
|
|
|
38
64
|
When importing `@ohtaman/washi` in a browser bundler, the `browser` export condition resolves automatically:
|
|
39
65
|
|
|
@@ -45,31 +71,35 @@ export async function runOptimization() {
|
|
|
45
71
|
const x = m.continuous("x", { lb: 0, ub: 100 });
|
|
46
72
|
m.objective(maximize(x));
|
|
47
73
|
await m.solve();
|
|
48
|
-
return x.value;
|
|
74
|
+
return m.hasPrimalSolution ? x.value : null;
|
|
49
75
|
}
|
|
50
76
|
```
|
|
51
77
|
|
|
78
|
+
Because both the modeler and solver run on WebAssembly, the static output of `vite build` (or your bundler's production build) can be deployed directly to **GitHub Pages, Cloudflare Pages, Vercel, or Netlify** with no backend server required. (Bundled **HiGHS** and **Pumpkin**, as well as **GLPK**, require no special HTTP headers; if you use Z3 in the browser, see [Cross-Origin Isolation](./cross-origin.md).)
|
|
79
|
+
|
|
52
80
|
#### Background Web Worker Execution (Default)
|
|
53
|
-
In browsers, HiGHS automatically
|
|
81
|
+
In browsers, both **HiGHS** and **Pumpkin** automatically run inside dedicated **Web Workers** so LP/MIP/QP/CP solves never freeze the main UI thread, animations, or user input, while enabling real-time progress events (`onProgress`) and cancellation (`AbortSignal`).
|
|
54
82
|
|
|
55
|
-
If your environment prohibits Web Workers or you explicitly want
|
|
83
|
+
If your environment prohibits Web Workers or you explicitly want main-thread execution, pass `useWorker: false`:
|
|
56
84
|
|
|
57
85
|
```typescript
|
|
58
86
|
await m.solve({ useWorker: false });
|
|
59
87
|
```
|
|
60
88
|
|
|
61
|
-
#### Customizing
|
|
62
|
-
By default, WaSHI resolves `highs.wasm` relative to the bundle (`new URL("./highs.wasm", import.meta.url)`). It **never** makes implicit requests to external CDNs (`jsdelivr`, `unpkg`, etc.).
|
|
89
|
+
#### Customizing WASM Asset URLs
|
|
90
|
+
By default, WaSHI resolves `highs.wasm` and `pumpkin.wasm` relative to the bundle (`new URL("./highs.wasm", import.meta.url)` / `new URL("./pumpkin.wasm", import.meta.url)`). It **never** makes implicit requests to external CDNs (`jsdelivr`, `unpkg`, etc.).
|
|
63
91
|
|
|
64
|
-
If your bundler or static server hosts `.wasm` files under a custom path, configure the URL globally or per solve:
|
|
92
|
+
If your bundler or static server hosts `.wasm` files under a custom path, configure the URL globally or per solve/check:
|
|
65
93
|
|
|
66
94
|
```typescript
|
|
67
95
|
import { setHighsWasmUrl } from "@ohtaman/washi";
|
|
96
|
+
import { setPumpkinWasmUrl } from "@ohtaman/washi/solvers/pumpkin";
|
|
68
97
|
|
|
69
98
|
// Option A: Configure globally before solving
|
|
70
99
|
setHighsWasmUrl("/assets/wasm/highs.wasm");
|
|
100
|
+
setPumpkinWasmUrl("/assets/wasm/pumpkin.wasm");
|
|
71
101
|
|
|
72
|
-
// Option B: Configure per solve call
|
|
102
|
+
// Option B: Configure per solve / check call
|
|
73
103
|
await m.solve({ wasmUrl: "/assets/wasm/highs.wasm" });
|
|
74
104
|
```
|
|
75
105
|
|
|
@@ -91,7 +121,9 @@ export default function OptimizerComponent() {
|
|
|
91
121
|
const x = m.continuous("x", { lb: 0, ub: 50 });
|
|
92
122
|
m.objective(maximize(x));
|
|
93
123
|
await m.solve();
|
|
94
|
-
|
|
124
|
+
if (m.hasPrimalSolution) {
|
|
125
|
+
setVal(x.value);
|
|
126
|
+
}
|
|
95
127
|
};
|
|
96
128
|
|
|
97
129
|
return (
|
|
@@ -103,40 +135,59 @@ export default function OptimizerComponent() {
|
|
|
103
135
|
}
|
|
104
136
|
```
|
|
105
137
|
|
|
138
|
+
### Desktop & Mobile WebView Apps (Tauri / Capacitor / PWA)
|
|
139
|
+
|
|
140
|
+
Standard Vite frontends bundle and run **HiGHS** and **Pumpkin** (`@ohtaman/washi/solvers/pumpkin`) completely offline inside **Tauri** (`tauri://localhost` / `http://tauri.localhost`), **Capacitor** mobile WebViews, and offline **PWAs** with no native C/C++ solver compilation.
|
|
141
|
+
|
|
142
|
+
In `tauri.conf.json`, allow WebAssembly compilation (`'wasm-unsafe-eval'` in `script-src`) and Web Workers (`worker-src 'self' blob:`), or pass `{ useWorker: false }`:
|
|
143
|
+
|
|
144
|
+
```json
|
|
145
|
+
{
|
|
146
|
+
"app": {
|
|
147
|
+
"security": {
|
|
148
|
+
"csp": "default-src 'self'; script-src 'self' 'wasm-unsafe-eval'; worker-src 'self' blob:"
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Bundled **HiGHS** and **Pumpkin** are recommended over **Z3** in desktop and mobile WebViews because they do not require `SharedArrayBuffer` or `COOP`/`COEP` headers.
|
|
155
|
+
|
|
106
156
|
---
|
|
107
157
|
|
|
108
158
|
## 📦 Browser ESM (`<script type="module">`)
|
|
109
159
|
|
|
110
|
-
|
|
160
|
+
Without a bundler, you can import the pre-built browser ESM module directly from your own static server or an npm CDN:
|
|
111
161
|
|
|
112
162
|
```html
|
|
113
163
|
<script type="module">
|
|
114
|
-
//
|
|
115
|
-
import { Model, expr, maximize } from "
|
|
116
|
-
//
|
|
117
|
-
// import
|
|
118
|
-
//
|
|
119
|
-
// import "
|
|
120
|
-
// import "./dist/browser/solvers/z3.js";
|
|
164
|
+
// From an npm CDN (or "./dist/browser/washi.js" when self-hosted):
|
|
165
|
+
import { Model, expr, maximize } from "https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/washi.js";
|
|
166
|
+
// Bundled / optional solver plugins:
|
|
167
|
+
// import "https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/pumpkin.js";
|
|
168
|
+
// import "https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/glpk.js";
|
|
169
|
+
// import "https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/z3.js";
|
|
121
170
|
|
|
122
171
|
const m = new Model("BrowserESM");
|
|
123
|
-
const x = m.integer("x", { lb: 0 });
|
|
124
|
-
const y = m.integer("y", { lb: 0 });
|
|
172
|
+
const x = m.integer("x", { lb: 0, ub: 100 });
|
|
173
|
+
const y = m.integer("y", { lb: 0, ub: 100 });
|
|
125
174
|
|
|
126
175
|
m.objective(maximize(expr`30 * ${x} + 50 * ${y}`));
|
|
127
176
|
m.constraint(expr`${x} + 2 * ${y} <= 40`, "wood");
|
|
128
177
|
m.constraint(expr`2 * ${x} + ${y} <= 50`, "labor");
|
|
129
178
|
|
|
130
179
|
await m.solve();
|
|
131
|
-
|
|
180
|
+
if (m.hasPrimalSolution) {
|
|
181
|
+
console.log("Optimal Objective:", m.objectiveValue);
|
|
182
|
+
}
|
|
132
183
|
</script>
|
|
133
184
|
```
|
|
134
185
|
|
|
135
186
|
---
|
|
136
187
|
|
|
137
|
-
## 🟨 Classic `<script>` Tags (IIFE / Standalone
|
|
188
|
+
## 🟨 Classic `<script>` Tags (IIFE / Standalone HTML / CDN)
|
|
138
189
|
|
|
139
|
-
For
|
|
190
|
+
For single-file HTML pages, CodePen, or JSFiddle, load `washi.min.js` via a standard `<script>` tag. All exports are attached to the global `window.Washi` object:
|
|
140
191
|
|
|
141
192
|
```html
|
|
142
193
|
<!DOCTYPE html>
|
|
@@ -149,13 +200,12 @@ For static HTML pages, CodePen, or JSFiddle, load `washi.min.js` via a standard
|
|
|
149
200
|
<h1>WaSHI Browser Optimization</h1>
|
|
150
201
|
<pre id="output">Solving...</pre>
|
|
151
202
|
|
|
152
|
-
<!-- Core WaSHI + HiGHS (MIT)
|
|
153
|
-
<script src="
|
|
154
|
-
<!--
|
|
155
|
-
<!-- <script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi
|
|
156
|
-
<!--
|
|
157
|
-
<!-- <script src="
|
|
158
|
-
<!-- <script src="./dist/browser/solvers/z3.min.js"></script> -->
|
|
203
|
+
<!-- Core WaSHI + HiGHS (MIT) via CDN (or "./dist/browser/washi.min.js" when self-hosted) -->
|
|
204
|
+
<script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/washi.min.js"></script>
|
|
205
|
+
<!-- Bundled / optional plugins (auto-register on window.Washi): -->
|
|
206
|
+
<!-- <script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/pumpkin.min.js"></script> -->
|
|
207
|
+
<!-- <script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/glpk.min.js"></script> -->
|
|
208
|
+
<!-- <script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/z3.min.js"></script> -->
|
|
159
209
|
|
|
160
210
|
<script>
|
|
161
211
|
(async () => {
|
|
@@ -170,10 +220,12 @@ For static HTML pages, CodePen, or JSFiddle, load `washi.min.js` via a standard
|
|
|
170
220
|
|
|
171
221
|
await m.solve();
|
|
172
222
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
223
|
+
if (m.hasPrimalSolution) {
|
|
224
|
+
document.getElementById("output").textContent =
|
|
225
|
+
`Status: ${m.status}\n` +
|
|
226
|
+
`Objective: ${m.objectiveValue}\n` +
|
|
227
|
+
`x = ${x.value}, y = ${y.value}`;
|
|
228
|
+
}
|
|
177
229
|
})();
|
|
178
230
|
</script>
|
|
179
231
|
</body>
|
|
@@ -181,7 +233,8 @@ For static HTML pages, CodePen, or JSFiddle, load `washi.min.js` via a standard
|
|
|
181
233
|
```
|
|
182
234
|
|
|
183
235
|
> [!NOTE]
|
|
184
|
-
>
|
|
236
|
+
> - **Cross-origin CDN workers**: On standard web origins (`https://` / `http://`), browsers block direct `new Worker("https://cdn...")`, so WaSHI automatically bridges cross-origin workers via a lightweight ES module `Blob` bootstrap (`import "https://..."`).
|
|
237
|
+
> - **Opening local `.html` files directly (`file://`)**: CDN-loaded `washi.min.js` and `solvers/pumpkin.min.js` work directly when opening a local `.html` via `file://` (double-click, no local server)—on opaque effective origins (`window.origin === "null"`, such as `file:` URLs or `sandbox="allow-scripts"` `<iframe>` embeds without `allow-same-origin`), WaSHI automatically runs HiGHS and Pumpkin on the main thread when `useWorker` is omitted. Self-hosted local `.wasm` files (blocked on `file://` by browser `fetch()` CORS rules) and **Z3** (`COOP`/`COEP`) require serving over `http://` or `https://`.
|
|
185
238
|
|
|
186
239
|
---
|
|
187
240
|
|
|
@@ -191,37 +244,13 @@ WaSHI never phones home or falls back to public CDNs. To self-host in an air-gap
|
|
|
191
244
|
|
|
192
245
|
1. **Copy Static Assets**: Serve the files from `node_modules/@ohtaman/washi/dist/browser/` on your own origin:
|
|
193
246
|
- `washi.js` / `washi.min.js`, `highs.wasm`, and `highs-worker.js`
|
|
247
|
+
- Bundled Pumpkin: `solvers/pumpkin.js` / `solvers/pumpkin.min.js`, `solvers/pumpkin.wasm`, `solvers/pumpkin-worker.js` (plus `solvers/pumpkin-*.js` chunks)
|
|
194
248
|
- Optional GLPK: `solvers/glpk.js` / `solvers/glpk.min.js`
|
|
195
249
|
- Optional Z3: `solvers/z3.js` / `solvers/z3.min.js`, `solvers/z3-built.js`, `solvers/z3-built.wasm`
|
|
196
250
|
2. **Content Security Policy (CSP) Headers**:
|
|
197
251
|
- Allow WebAssembly compilation (`'wasm-unsafe-eval'` in `script-src`).
|
|
198
|
-
- If using the default Web Worker or cross-origin Blob worker
|
|
199
|
-
- If `blob:` workers are prohibited by policy, either serve `highs-worker.js` from `'self'` or run on the main thread with `await m.solve({ useWorker: false })`.
|
|
252
|
+
- If using the default Web Worker or cross-origin Blob worker bootstrap, allow `worker-src 'self' blob:;`.
|
|
253
|
+
- If `blob:` workers are prohibited by policy, either serve `highs-worker.js` / `solvers/pumpkin-worker.js` from `'self'` or run on the main thread with `await m.solve({ useWorker: false })`.
|
|
200
254
|
3. **Cross-Origin Isolation (Z3 Only)**:
|
|
201
|
-
- HiGHS and GLPK run in standard browser contexts without special headers.
|
|
202
|
-
- Z3 WASM uses pthreads + `SharedArrayBuffer` and requires `Cross-Origin-Opener-Policy: same-origin` and `Cross-Origin-Embedder-Policy: require-corp` (
|
|
203
|
-
|
|
204
|
-
---
|
|
205
|
-
|
|
206
|
-
## ⚡ Bun & Deno
|
|
207
|
-
|
|
208
|
-
### Bun
|
|
209
|
-
|
|
210
|
-
```bash
|
|
211
|
-
bun add @ohtaman/washi
|
|
212
|
-
bun run app.ts
|
|
213
|
-
```
|
|
214
|
-
|
|
215
|
-
### Deno
|
|
216
|
-
|
|
217
|
-
In Deno (v1.30+ with `npm:` specifiers):
|
|
218
|
-
|
|
219
|
-
```typescript
|
|
220
|
-
import { Model, maximize } from "npm:@ohtaman/washi";
|
|
221
|
-
|
|
222
|
-
const m = new Model();
|
|
223
|
-
const x = m.continuous("x", { lb: 0, ub: 10 });
|
|
224
|
-
m.objective(maximize(x));
|
|
225
|
-
await m.solve();
|
|
226
|
-
console.log("x =", x.value);
|
|
227
|
-
```
|
|
255
|
+
- HiGHS, Pumpkin, and GLPK run in standard browser contexts without special headers.
|
|
256
|
+
- Z3 WASM uses pthreads + `SharedArrayBuffer` and requires `Cross-Origin-Opener-Policy: same-origin` and `Cross-Origin-Embedder-Policy: require-corp` (`credentialless` works in Chromium/Firefox, whereas Safari requires `require-corp`). See the [Cross-Origin Isolation Guide](./cross-origin.md) for setup details.
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# Constraint Programming (CP) Modeling
|
|
2
|
+
|
|
3
|
+
WaSHI allows you to formulate both **Mathematical Optimization (LP / MIP / Convex QP)** and **Constraint Programming (CP)** problems through the same TypeScript algebraic modeling API (`Model`, `expr`, `sum`, `dot`). When you solve with bundled **Pumpkin** (`@ohtaman/washi/solvers/pumpkin`), global predicates (`distinct`, `disjoint`, `cumulative`, `allowed`) and piecewise/element primitives (`max`, `min`, `abs`, `at`) run on native Lazy Clause Generation (LCG) propagators instead of Big-M or time-indexed 0-1 MIP linearizations.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 1. MIP vs. CP Modeling Mindset
|
|
8
|
+
|
|
9
|
+
In **MIP (HiGHS / GLPK)**, combinatorial decisions are typically encoded with large matrices of 0-1 binary variables $x_{i,t} \in \{0, 1\}$ and Big-M inequalities so a simplex/branch-and-cut solver can tighten continuous LP relaxations.
|
|
10
|
+
|
|
11
|
+
In **CP (Pumpkin)**, you model **what** each entity chooses directly using finite-domain integers (`m.integer(keys, { lb, ub })`) and declarative **global constraints**:
|
|
12
|
+
|
|
13
|
+
| Modeling Dimension | MIP Pattern (`HiGHS` / `GLPK`) | CP Pattern (`Pumpkin`) | SMT (`Z3`) |
|
|
14
|
+
| :--- | :--- | :--- | :--- |
|
|
15
|
+
| **Start times / Slots** | Time-indexed binaries $x_{i,t}$ or pairwise Big-M precedence binaries | Bounded integer start variables `start[i]` in `[0, H]` | Unbounded/bounded integer or real variables |
|
|
16
|
+
| **Machine / Worker capacity** | $\sum_i d_i x_{i,t} \le C$ for every discrete time slot $t$ | Single horizon-independent `cumulative(tasks, C)` | Event-point load formulas |
|
|
17
|
+
| **Unary non-overlap** | Pairwise Big-M disjunctions ($y_{ij} \in \{0,1\}$) | Native `disjoint(tasks)` edge-finding propagator | Pairwise `Or` disjunctions |
|
|
18
|
+
| **Permutation / Matching** | Doubly stochastic binary assignment matrix $x_{i,j} \in \{0, 1\}$ | Integer slot variables `machine[i]` in `[0, N-1]` + `distinct(machine)` | Native `ctx.Distinct` |
|
|
19
|
+
| **Cost / Tuple lookup** | $\sum_k c_k b_k$ with $\sum_k b_k = 1$ convex-hull binaries | Native `at(costs, indexVar)` & `allowed` / `not(allowed)` tables | Nested `If` expressions |
|
|
20
|
+
| **When to choose** | Continuous flows, blending, LP duals/shadow prices, quadratic objectives (`QuadExpr`), tight LP relaxations | Scheduling horizons, cumulative/disjunctive resources, permutations, table compatibility, zero-`COOP`/`COEP` `unsatCore` | Continuous non-integer logic (`Continuous` inside `not`/`implies`), formal proofs |
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## 2. Interval & Resource Scheduling (`disjoint`, `cumulative`, `max`)
|
|
25
|
+
|
|
26
|
+
Time-indexed MIP formulations create $O(|T| \cdot H)$ binary variables and capacity rows, which quickly blows up when scheduling horizons grow (WaSHI's MIP lowering rejects horizons exceeding 5,000 time steps or 25,000 pulse variables and recommends `{ solver: "pumpkin" }`).
|
|
27
|
+
|
|
28
|
+
In CP, each task is just **one bounded integer variable** `start[task]` paired with `disjoint` (exclusive unary machines) and `cumulative` (renewable worker/power pools). Because interval propagators reason directly about earliest/latest start intervals and energetic overload, scheduling models stay compact regardless of horizon granularity.
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { Model, expr, disjoint, cumulative, max, minimize } from "@ohtaman/washi";
|
|
32
|
+
import "@ohtaman/washi/solvers/pumpkin";
|
|
33
|
+
|
|
34
|
+
const m = new Model("IntervalSchedulingCP", { solver: "pumpkin" });
|
|
35
|
+
const tasks = ["prep", "mill", "drill", "coat"] as const;
|
|
36
|
+
const duration = { prep: 3, mill: 4, drill: 3, coat: 2 };
|
|
37
|
+
const workers = { prep: 1, mill: 1, drill: 1, coat: 2 };
|
|
38
|
+
const start = m.integer(tasks, { lb: 0, ub: 20, prefix: "start" });
|
|
39
|
+
|
|
40
|
+
// Precedence rules: prep -> (mill, drill) -> coat
|
|
41
|
+
m.constraint(expr`${start.prep} + 3 <= ${start.mill}`, "mill_after_prep");
|
|
42
|
+
m.constraint(expr`${start.prep} + 3 <= ${start.drill}`, "drill_after_prep");
|
|
43
|
+
m.constraint(expr`${start.mill} + 4 <= ${start.coat}`, "coat_after_mill");
|
|
44
|
+
m.constraint(expr`${start.drill} + 3 <= ${start.coat}`, "coat_after_drill");
|
|
45
|
+
|
|
46
|
+
// Single CNC spindle shared between mill and drill
|
|
47
|
+
m.constraint(
|
|
48
|
+
disjoint([
|
|
49
|
+
{ start: start.mill, duration: duration.mill },
|
|
50
|
+
{ start: start.drill, duration: duration.drill },
|
|
51
|
+
]),
|
|
52
|
+
"single_cnc"
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
// At most 2 operators working simultaneously across all tasks
|
|
56
|
+
m.constraint(
|
|
57
|
+
cumulative(
|
|
58
|
+
tasks.map((t) => ({ start: start[t], duration: duration[t], demand: workers[t] })),
|
|
59
|
+
2
|
|
60
|
+
),
|
|
61
|
+
"operator_pool"
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
m.objective(minimize(max(tasks.map((t) => expr`${start[t]} + ${duration[t]}`))));
|
|
65
|
+
await m.solve();
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
See the full runnable project scheduling walkthrough in [Resource-Constrained Project Scheduling (RCPSP)](../examples/project-scheduling.md).
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## 3. Combinatorial Assignment, Table Rules & Element Lookup (`distinct`, `allowed`, `at`)
|
|
73
|
+
|
|
74
|
+
Instead of an $N \times M$ binary matrix $x_{j,m} \in \{0, 1\}$, assign each job a single bounded integer variable `machine[job]` with domain $\{0, \dots, M-1\}$:
|
|
75
|
+
|
|
76
|
+
- **`distinct(machine)`**: Forces every job onto a separate machine using Pumpkin's `all_different` propagator.
|
|
77
|
+
- **`allowed(vars, tuples)` / `not(allowed(vars, tuples))`**: Encodes arbitrary compatibility or incompatibility rules between discrete decisions without manual Big-M algebra.
|
|
78
|
+
- **`at(costs, indexVar)`**: Looks up a numeric cost from `costs[indexVar]` using Pumpkin's native `element` propagator—avoiding $M$ selector binary variables per job.
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import { Model, expr, distinct, allowed, not, at, minimize } from "@ohtaman/washi";
|
|
82
|
+
import "@ohtaman/washi/solvers/pumpkin";
|
|
83
|
+
|
|
84
|
+
const m = new Model("MachineAssignmentCP", { solver: "pumpkin" });
|
|
85
|
+
const jobs = ["jobA", "jobB", "jobC"] as const;
|
|
86
|
+
const machine = m.integer(jobs, { lb: 0, ub: 2, prefix: "machine" });
|
|
87
|
+
|
|
88
|
+
const costA = [5, 10, 40];
|
|
89
|
+
const costB = [20, 15, 30];
|
|
90
|
+
const costC = [45, 30, 25];
|
|
91
|
+
|
|
92
|
+
// Each job runs on a distinct machine (0, 1, or 2)
|
|
93
|
+
m.constraint(distinct(machine), "one_job_per_machine");
|
|
94
|
+
|
|
95
|
+
// Safety interlock: Job A on Machine 0 cannot pair with Job B on Machine 1
|
|
96
|
+
m.constraint(not(allowed([machine.jobA, machine.jobB], [[0, 1]])), "interlock_pair");
|
|
97
|
+
|
|
98
|
+
// Variable-indexed cost lookup via 0-based Element primitive at(table, indexVar)
|
|
99
|
+
m.objective(
|
|
100
|
+
minimize(
|
|
101
|
+
expr`at(${costA}, ${machine.jobA}) + at(${costB}, ${machine.jobB}) + at(${costC}, ${machine.jobC})`
|
|
102
|
+
)
|
|
103
|
+
);
|
|
104
|
+
await m.solve();
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## 4. Domain Tightening, Symmetry Breaking & Conflict Diagnosis
|
|
110
|
+
|
|
111
|
+
### Keep Integer Domains Finite and Tight
|
|
112
|
+
Pumpkin uses signed 32-bit integer propagators and rejects `Continuous`, fractional, or unbounded variables (`lb: -Infinity` or `ub: Infinity`). Every `m.integer()` variable on Pumpkin must have finite integer `[lb, ub]` within $[-(2^{30} - 1),\, 2^{30} - 1]$ (see [Pumpkin Domain Preconditions](../solvers/pumpkin.md#domain-api-preconditions)). Tight horizons directly shrink propagator domains and accelerate conflict learning.
|
|
113
|
+
|
|
114
|
+
### Break Permutation Symmetry with Strict Ordering (`<`)
|
|
115
|
+
When selecting $k$ items into $k$ interchangeable slots (`slot[0]`, ..., `slot[k-1]`), all $k!$ permutations have identical cost, forcing a branch-and-bound or CP search tree to explore symmetric duplicates. Adding strict inequality ordering `slot[0] < slot[1] < ...` simultaneously enforces all-different and collapses all $k!$ permutations into a single canonical sequence:
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
import { Model, expr, at, minimize } from "@ohtaman/washi";
|
|
119
|
+
import "@ohtaman/washi/solvers/pumpkin";
|
|
120
|
+
|
|
121
|
+
const m = new Model("SymmetryBreakingCP", { solver: "pumpkin" });
|
|
122
|
+
const latencyMs = [600, 380, 420, 470, 550];
|
|
123
|
+
const slot = m.integer(3, { lb: 0, ub: 4, prefix: "slot" });
|
|
124
|
+
|
|
125
|
+
// Strict ascending order enforces all-different AND eliminates 3! = 6 permutation symmetries
|
|
126
|
+
m.constraint([
|
|
127
|
+
expr`${slot[0]} < ${slot[1]}`,
|
|
128
|
+
expr`${slot[1]} < ${slot[2]}`,
|
|
129
|
+
]);
|
|
130
|
+
|
|
131
|
+
m.objective(
|
|
132
|
+
minimize(
|
|
133
|
+
expr`at(${latencyMs}, ${slot[0]}) + at(${latencyMs}, ${slot[1]}) + at(${latencyMs}, ${slot[2]})`
|
|
134
|
+
)
|
|
135
|
+
);
|
|
136
|
+
await m.solve();
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Warm Starts & Zero-`COOP`/`COEP` Conflict Diagnosis
|
|
140
|
+
- **Warm Starts (`initialValue`)**: Provide feasible domain hints via `{ initialValue: val }` on `m.integer()` or `m.binary()` to seed Pumpkin's value selection (`WarmStart` brancher).
|
|
141
|
+
- **Conflict Diagnosis (`m.check()`)**: If a CP schedule or rule set returns `"Infeasible"`, calling `await m.check({ solver: "pumpkin" })` (without changing any constraints or adding `COOP`/`COEP` headers in browsers) extracts `unsatCore` / `conflicts` identifying which named precedence, resource, or deadline rules collide—see [Conflict Diagnosis (`unsatCore`)](./unsat-core.md).
|