@ohtaman/washi 0.7.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +60 -23
- package/README.md +59 -22
- package/README.zh-CN.md +62 -25
- package/THIRD_PARTY_LICENSES.md +34 -1
- package/dist/browser/solvers/glpk.js +289 -238
- package/dist/browser/solvers/glpk.min.js +1 -1
- package/dist/browser/solvers/highs-__vite-browser-external-Dn-83hnF.js +7 -0
- package/dist/browser/solvers/highs-browser-runtime-0Bgq5ksG.js +2021 -0
- package/dist/browser/{loader-DewlSho8.js → solvers/highs-loader-BLY3wzDH.js} +190 -149
- package/dist/browser/solvers/highs-worker.js +54 -0
- package/dist/browser/solvers/highs.js +1414 -0
- package/dist/browser/solvers/highs.wasm +0 -0
- package/dist/browser/solvers/pumpkin-_washi-pumpkin-node-runtime-CEKUDK6d.js +9 -0
- package/dist/browser/solvers/pumpkin-loader-CvdaKxf4.js +320 -0
- package/dist/browser/solvers/pumpkin-worker.js +45 -0
- package/dist/browser/solvers/pumpkin.js +1777 -0
- package/dist/browser/solvers/pumpkin.wasm +0 -0
- package/dist/browser/solvers/pumpkin.wasm.LICENSE +21 -0
- package/dist/browser/solvers/z3.js +1600 -1335
- package/dist/browser/solvers/z3.min.js +2 -2
- package/dist/browser/washi.js +3313 -1699
- package/dist/browser/washi.min.js +5 -3
- package/dist/lib/solvers/__vite-browser-external-oLchLuMx.js +7 -0
- package/dist/lib/solvers/glpk.js +174 -132
- package/dist/lib/solvers/highs-browser-runtime.js +2413 -0
- package/dist/lib/{highs-node-runtime.js → solvers/highs-node-runtime.js} +1 -1
- package/dist/lib/solvers/highs-runtime.js +16 -0
- package/dist/lib/solvers/highs-worker.js +54 -0
- package/dist/lib/solvers/highs.js +410 -0
- package/dist/lib/solvers/highs.wasm +0 -0
- package/dist/lib/{loader-BZUQNvh4.js → solvers/loader-CDpPXN_F.js} +138 -116
- package/dist/lib/solvers/loader-Ow74Fa1B.js +311 -0
- package/dist/lib/solvers/lp-writer-CLScMFbS.js +115 -0
- package/dist/lib/solvers/pumpkin-assets.js +1 -0
- package/dist/lib/solvers/pumpkin-node-runtime.js +15 -0
- package/dist/lib/solvers/pumpkin-worker-factory.js +1 -0
- package/dist/lib/solvers/pumpkin-worker.js +45 -0
- package/dist/lib/solvers/pumpkin.js +868 -0
- package/dist/lib/solvers/pumpkin.wasm +0 -0
- package/dist/lib/solvers/pumpkin.wasm.LICENSE +21 -0
- package/dist/lib/solvers/registry-CrYRRk9q.js +907 -0
- package/dist/lib/solvers/rolldown-runtime-DtPi1Y-2.js +13 -0
- package/dist/lib/solvers/types-BGqPkxwu.js +12 -0
- package/dist/lib/solvers/z3.js +489 -307
- package/dist/lib/washi.js +3905 -1410
- package/dist/types/decomposition/benders.d.ts.map +1 -1
- package/dist/types/index.d.ts +4 -5
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/modeler/boolean-expr.d.ts +137 -29
- package/dist/types/modeler/boolean-expr.d.ts.map +1 -1
- package/dist/types/modeler/constraint.d.ts +40 -4
- package/dist/types/modeler/constraint.d.ts.map +1 -1
- package/dist/types/modeler/expression.d.ts +129 -2
- package/dist/types/modeler/expression.d.ts.map +1 -1
- package/dist/types/modeler/index.d.ts +4 -6
- package/dist/types/modeler/index.d.ts.map +1 -1
- package/dist/types/modeler/linearizer.d.ts +11 -2
- package/dist/types/modeler/linearizer.d.ts.map +1 -1
- package/dist/types/modeler/lp-writer.d.ts.map +1 -1
- package/dist/types/modeler/model.d.ts +86 -126
- package/dist/types/modeler/model.d.ts.map +1 -1
- package/dist/types/modeler/parser.d.ts.map +1 -1
- package/dist/types/modeler/quad-expr.d.ts +8 -2
- package/dist/types/modeler/quad-expr.d.ts.map +1 -1
- package/dist/types/modeler/solution-validator.d.ts +12 -12
- package/dist/types/modeler/solution-validator.d.ts.map +1 -1
- package/dist/types/modeler/solvers/glpk/loader.d.ts +4 -0
- package/dist/types/modeler/solvers/glpk/loader.d.ts.map +1 -1
- package/dist/types/modeler/solvers/glpk/solver.d.ts +2 -2
- package/dist/types/modeler/solvers/glpk/solver.d.ts.map +1 -1
- package/dist/types/modeler/solvers/highs/driver.d.ts.map +1 -1
- package/dist/types/modeler/solvers/highs/loader.d.ts +8 -6
- package/dist/types/modeler/solvers/highs/loader.d.ts.map +1 -1
- package/dist/types/modeler/solvers/highs/solver.d.ts.map +1 -1
- package/dist/types/modeler/solvers/highs/worker-client.d.ts +16 -0
- package/dist/types/modeler/solvers/highs/worker-client.d.ts.map +1 -1
- package/dist/types/modeler/solvers/index.d.ts +2 -1
- package/dist/types/modeler/solvers/index.d.ts.map +1 -1
- package/dist/types/modeler/solvers/mock.d.ts +2 -3
- package/dist/types/modeler/solvers/mock.d.ts.map +1 -1
- package/dist/types/modeler/solvers/pumpkin/assets.d.ts +2 -0
- package/dist/types/modeler/solvers/pumpkin/assets.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/index.d.ts +6 -0
- package/dist/types/modeler/solvers/pumpkin/index.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/loader.d.ts +40 -0
- package/dist/types/modeler/solvers/pumpkin/loader.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/node-runtime.d.ts +3 -0
- package/dist/types/modeler/solvers/pumpkin/node-runtime.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/solver.d.ts +23 -0
- package/dist/types/modeler/solvers/pumpkin/solver.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/worker-client.d.ts +74 -0
- package/dist/types/modeler/solvers/pumpkin/worker-client.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/worker-factory.d.ts +3 -0
- package/dist/types/modeler/solvers/pumpkin/worker-factory.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/worker.d.ts +7 -0
- package/dist/types/modeler/solvers/pumpkin/worker.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin.d.ts +2 -0
- package/dist/types/modeler/solvers/pumpkin.d.ts.map +1 -0
- package/dist/types/modeler/solvers/types.d.ts +7 -9
- package/dist/types/modeler/solvers/types.d.ts.map +1 -1
- package/dist/types/modeler/solvers/z3/loader.d.ts +4 -0
- package/dist/types/modeler/solvers/z3/loader.d.ts.map +1 -1
- package/dist/types/modeler/solvers/z3/solver.d.ts +2 -15
- package/dist/types/modeler/solvers/z3/solver.d.ts.map +1 -1
- package/dist/types/modeler/types.d.ts +12 -15
- package/dist/types/modeler/types.d.ts.map +1 -1
- package/dist/types/modeler/validation.d.ts +26 -2
- package/dist/types/modeler/validation.d.ts.map +1 -1
- package/dist/types/modeler/variable.d.ts +9 -5
- package/dist/types/modeler/variable.d.ts.map +1 -1
- package/docs/examples/diet-sensitivity.md +33 -14
- package/docs/examples/ecommerce-coupons.md +17 -9
- package/docs/examples/index.md +36 -27
- package/docs/examples/knapsack.md +11 -5
- package/docs/examples/portfolio-qp.md +21 -8
- package/docs/examples/production.md +10 -5
- package/docs/examples/project-scheduling.md +119 -0
- package/docs/examples/shift-scheduling.md +29 -18
- package/docs/examples/smt-security.md +27 -11
- package/docs/examples/transportation.md +23 -13
- package/docs/examples/traveling-salesperson.md +34 -22
- package/docs/examples/unsat-core.md +27 -14
- package/docs/examples/vehicle-routing.md +74 -28
- package/docs/getting-started/ai-agents.md +190 -82
- package/docs/getting-started/coding-agents.md +177 -0
- package/docs/getting-started/cross-origin.md +27 -14
- package/docs/getting-started/index.md +49 -43
- package/docs/getting-started/runtimes.md +94 -40
- package/docs/guide/constraint-programming.md +171 -0
- package/docs/guide/constraints.md +115 -33
- package/docs/guide/decomposition.md +118 -88
- package/docs/guide/expressions.md +106 -43
- package/docs/guide/formulation-tips.md +47 -28
- package/docs/guide/objectives.md +102 -75
- package/docs/guide/unsat-core.md +62 -50
- package/docs/guide/variables.md +81 -35
- package/docs/solvers/custom-solver.md +163 -122
- package/docs/solvers/glpk.md +58 -17
- package/docs/solvers/highs.md +124 -57
- package/docs/solvers/index.md +33 -20
- package/docs/solvers/pumpkin.md +171 -0
- package/docs/solvers/z3.md +63 -25
- package/llms-full.txt +2232 -932
- package/llms.txt +144 -91
- package/package.json +55 -19
- package/skills/washi/SKILL.md +107 -34
- package/dist/browser/_washi-node-only-BvRk9kiK.js +0 -0
- package/dist/browser/browser-runtime-TLCY0hkc.js +0 -1793
- package/dist/browser/highs-worker.js +0 -39
- package/dist/browser/highs.wasm +0 -0
- package/dist/lib/_washi-node-only-BvRk9kiK.js +0 -0
- package/dist/lib/highs-browser-runtime.js +0 -2185
- package/dist/lib/highs-runtime.js +0 -2
- package/dist/lib/highs-worker.js +0 -39
- package/dist/lib/highs.wasm +0 -0
- package/dist/lib/registry-D7TZ3Wh_.js +0 -1034
- package/docs/ja/getting-started/ai-agents.md +0 -280
- package/docs/zh/getting-started/ai-agents.md +0 -280
- /package/dist/browser/{highs.wasm.LICENSE → solvers/highs.wasm.LICENSE} +0 -0
- /package/dist/lib/{highs-assets.js → solvers/highs-assets.js} +0 -0
- /package/dist/lib/{highs-worker-factory.js → solvers/highs-worker-factory.js} +0 -0
- /package/dist/lib/{highs.wasm.LICENSE → solvers/highs.wasm.LICENSE} +0 -0
|
@@ -1,31 +1,39 @@
|
|
|
1
1
|
# Cross-Origin Isolation (Z3 & Browsers)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Running the Z3 solver in a browser requires two browser security conditions: a **Secure Context** and **Cross-Origin Isolation**. Together, they enable `SharedArrayBuffer` for Z3's multithreaded WebAssembly build.
|
|
4
4
|
|
|
5
5
|
> [!TIP]
|
|
6
|
-
> **Not needed for HiGHS, GLPK, or Node.js**:
|
|
6
|
+
> **Not needed for HiGHS, Pumpkin, GLPK, or Node.js**: Bundled HiGHS and Pumpkin, as well as the GLPK plugin, never use `SharedArrayBuffer` and work in standard browser contexts. Running Z3 in Node.js also requires no HTTP headers. You only need this guide when using Z3 in a browser.
|
|
7
7
|
|
|
8
8
|
---
|
|
9
9
|
|
|
10
|
-
## Why
|
|
10
|
+
## Why Are These Preconditions Required?
|
|
11
11
|
|
|
12
|
-
Z3's WebAssembly
|
|
12
|
+
Z3's WebAssembly binary uses POSIX threads (`pthreads`) backed by `SharedArrayBuffer` and Web Workers. To mitigate Spectre-style side-channel attacks, modern browsers disable `SharedArrayBuffer` by default.
|
|
13
|
+
|
|
14
|
+
Browsers re-enable `SharedArrayBuffer` only when both conditions hold on the top-level document:
|
|
15
|
+
|
|
16
|
+
1. **Secure Context**: The page must load over `https://` or `http://localhost` (including `127.0.0.1` and `[::1]`) so `window.isSecureContext === true`. Plain `http://` LAN IP addresses do not qualify.
|
|
17
|
+
2. **Cross-Origin Isolation**: The document must opt into isolation headers so `window.crossOriginIsolated === true`.
|
|
13
18
|
|
|
14
19
|
---
|
|
15
20
|
|
|
16
21
|
## Required HTTP Response Headers
|
|
17
22
|
|
|
18
|
-
Serve the top-level HTML document with:
|
|
23
|
+
Serve the top-level HTML document with these two response headers:
|
|
24
|
+
|
|
25
|
+
- **Cross-Origin-Opener-Policy (`COOP`)**: `same-origin`
|
|
26
|
+
- **Cross-Origin-Embedder-Policy (`COEP`)**: `require-corp`
|
|
19
27
|
|
|
20
28
|
```http
|
|
21
29
|
Cross-Origin-Opener-Policy: same-origin
|
|
22
30
|
Cross-Origin-Embedder-Policy: require-corp
|
|
23
31
|
```
|
|
24
32
|
|
|
25
|
-
If your page loads
|
|
33
|
+
If your page loads scripts, images, or `.wasm` files from another origin, those responses must include `Cross-Origin-Resource-Policy: cross-origin` (`CORP`) or valid CORS headers (`Access-Control-Allow-Origin`), depending on the request mode.
|
|
26
34
|
|
|
27
35
|
> [!NOTE]
|
|
28
|
-
> Chromium and Firefox also
|
|
36
|
+
> Chromium and Firefox also accept `Cross-Origin-Embedder-Policy: credentialless`, which relaxes `CORP` requirements on cross-origin subresources. **Safari (WebKit) does not support `credentialless`**, so use `require-corp` if you support Safari or iOS.
|
|
29
37
|
|
|
30
38
|
---
|
|
31
39
|
|
|
@@ -33,7 +41,7 @@ If your page loads cross-origin subresources (such as scripts, images, or WASM f
|
|
|
33
41
|
|
|
34
42
|
### 1. Vite (`vite.config.ts`)
|
|
35
43
|
|
|
36
|
-
Configure both the
|
|
44
|
+
Configure both the `vite` dev server and `vite preview` server:
|
|
37
45
|
|
|
38
46
|
```typescript
|
|
39
47
|
import { defineConfig } from "vite";
|
|
@@ -56,7 +64,7 @@ export default defineConfig({
|
|
|
56
64
|
|
|
57
65
|
### 2. Cloudflare Pages / Netlify (`public/_headers`)
|
|
58
66
|
|
|
59
|
-
Place a `_headers` file in `public/` so
|
|
67
|
+
Place a `_headers` file in `public/` so your build copies it to the site root:
|
|
60
68
|
|
|
61
69
|
```text
|
|
62
70
|
/*
|
|
@@ -85,7 +93,7 @@ const nextConfig = {
|
|
|
85
93
|
module.exports = nextConfig;
|
|
86
94
|
```
|
|
87
95
|
|
|
88
|
-
### 4. Custom Node.js
|
|
96
|
+
### 4. Express (Custom Node.js Server)
|
|
89
97
|
|
|
90
98
|
```javascript
|
|
91
99
|
app.use((req, res, next) => {
|
|
@@ -97,19 +105,24 @@ app.use((req, res, next) => {
|
|
|
97
105
|
|
|
98
106
|
### 5. Static Hosting without Header Control (e.g., GitHub Pages)
|
|
99
107
|
|
|
100
|
-
Platforms like GitHub Pages do not
|
|
108
|
+
Platforms like GitHub Pages do not let you customize HTTP response headers. You can use **`coi-serviceworker`** to inject `COOP` and `COEP` headers from a client-side Service Worker. Note that Service Workers themselves also require a Secure Context (`https://` or `http://localhost`).
|
|
101
109
|
|
|
102
|
-
1.
|
|
110
|
+
1. Copy `coi-serviceworker.js` from [`coi-serviceworker`](https://github.com/gzuidhof/coi-serviceworker) or WaSHI's reference [`public/coi-serviceworker.js`](https://github.com/ohtaman/washi/blob/main/public/coi-serviceworker.js) to your site root.
|
|
103
111
|
2. Load it in your HTML `<head>`:
|
|
104
112
|
|
|
105
113
|
```html
|
|
106
114
|
<script src="/coi-serviceworker.js"></script>
|
|
107
115
|
```
|
|
108
116
|
|
|
109
|
-
On the first visit, the Service Worker registers and reloads the page once
|
|
117
|
+
On the first visit, the Service Worker registers and reloads the page once so `window.crossOriginIsolated` becomes `true`.
|
|
110
118
|
|
|
111
119
|
---
|
|
112
120
|
|
|
113
121
|
## Runtime Check in WaSHI
|
|
114
122
|
|
|
115
|
-
|
|
123
|
+
Before initializing Z3 in a browser, WaSHI validates both preconditions in order:
|
|
124
|
+
|
|
125
|
+
| Check | Failure Condition | Result |
|
|
126
|
+
| --- | --- | --- |
|
|
127
|
+
| **Secure Context** | `!window.isSecureContext` and hostname is not `localhost`, `127.0.0.1`, or `[::1]` | `solve()` / `check()` immediately rejects with an HTTPS or `localhost` requirement error. |
|
|
128
|
+
| **Cross-Origin Isolation** | `SharedArrayBuffer` is missing or `window.crossOriginIsolated !== true` | `solve()` / `check()` immediately rejects listing the required `COOP` and `COEP` headers instead of hanging. |
|
|
@@ -1,35 +1,19 @@
|
|
|
1
1
|
# Introduction & Installation
|
|
2
2
|
|
|
3
|
-
**WaSHI** (WebAssembly-backed, Solver-independent, High-level modeling Interface) brings
|
|
4
|
-
|
|
5
|
-
```typescript
|
|
6
|
-
import { Model, expr, maximize } from "@ohtaman/washi";
|
|
7
|
-
|
|
8
|
-
const m = new Model("production");
|
|
9
|
-
|
|
10
|
-
// 1. Define decision variables with realistic finite bounds (lb / ub)
|
|
11
|
-
const desks = m.integer("desks", { lb: 0, ub: 100 });
|
|
12
|
-
const chairs = m.integer("chairs", { lb: 0, ub: 100 });
|
|
13
|
-
|
|
14
|
-
// 2. Set the objective function and constraints
|
|
15
|
-
m.objective(maximize(expr`30 * ${desks} + 50 * ${chairs}`));
|
|
16
|
-
m.constraint(expr`${desks} + 2 * ${chairs} <= 40`, "wood_capacity");
|
|
17
|
-
m.constraint(expr`2 * ${desks} + ${chairs} <= 50`, "labor_capacity");
|
|
18
|
-
|
|
19
|
-
// 3. Solve (built-in HiGHS WebAssembly) and validate the solution
|
|
20
|
-
const status = await m.solve();
|
|
21
|
-
if (m.hasPrimalSolution) {
|
|
22
|
-
const report = m.validateSolution();
|
|
23
|
-
console.log(status, report.valid); // "Optimal", true
|
|
24
|
-
console.log(`Profit: ${m.objectiveValue} (desks=${desks.value}, chairs=${chairs.value})`); // 1100 (desks=20, chairs=10)
|
|
25
|
-
}
|
|
26
|
-
```
|
|
3
|
+
**WaSHI** (WebAssembly-backed, Solver-independent, High-level modeling Interface) brings Python PuLP-style algebraic modeling to TypeScript and JavaScript. You can write linear, quadratic, and logical expressions with tagged templates (`expr`) and model mathematical optimization and constraint programming problems directly in **Node.js (18+)** and modern browsers.
|
|
27
4
|
|
|
28
5
|
---
|
|
29
6
|
|
|
30
7
|
## 📦 Installation
|
|
31
8
|
|
|
32
|
-
|
|
9
|
+
Installing `@ohtaman/washi` bundles two zero-dependency solvers out of the box, with two additional solvers available as optional dependencies:
|
|
10
|
+
|
|
11
|
+
| Solver | Install | Problem Classes & Capabilities |
|
|
12
|
+
| --- | --- | --- |
|
|
13
|
+
| **HiGHS** *(default)* | Bundled in `@ohtaman/washi` (no extra import; subpath `@ohtaman/washi/solvers/highs`) | Linear, mixed-integer, and convex quadratic programming — `LP`, `MIP`, and `QP`. |
|
|
14
|
+
| **Pumpkin** | Bundled in `@ohtaman/washi` (`{ solver: "pumpkin" }`, no extra import; subpath `@ohtaman/washi/solvers/pumpkin`) | Constraint Programming (`CP`) and exact `unsatCore` conflict diagnosis on bounded integers and booleans. |
|
|
15
|
+
| **Z3** | Optional (`z3-solver`) | Satisfiability Modulo Theories (`SMT`), continuous logic, and exact `unsatCore` diagnosis (including continuous variables). |
|
|
16
|
+
| **GLPK** | Optional (`glpk.js`) | Alternative `LP` and `MIP` solver under the GPL-3.0 license. |
|
|
33
17
|
|
|
34
18
|
::: code-group
|
|
35
19
|
|
|
@@ -51,37 +35,59 @@ bun add @ohtaman/washi
|
|
|
51
35
|
|
|
52
36
|
:::
|
|
53
37
|
|
|
54
|
-
Install optional
|
|
38
|
+
Install optional dependencies when you need **Z3** or **GLPK**:
|
|
55
39
|
|
|
56
40
|
```bash
|
|
57
|
-
npm install z3-solver #
|
|
58
|
-
npm install glpk.js #
|
|
41
|
+
npm install z3-solver # Optional: SMT logic and unsatCore diagnosis for models with continuous variables
|
|
42
|
+
npm install glpk.js # Optional: GLPK solver (GPL-3.0)
|
|
59
43
|
```
|
|
60
44
|
|
|
45
|
+
In browsers, Z3 requires a Secure Context and Cross-Origin Isolation headers — `Cross-Origin-Opener-Policy` and `Cross-Origin-Embedder-Policy`, abbreviated `COOP`/`COEP`. See [Cross-Origin Isolation](./cross-origin.md) for server setup.
|
|
46
|
+
|
|
61
47
|
---
|
|
62
48
|
|
|
63
|
-
##
|
|
49
|
+
## 🚀 Quick Start
|
|
64
50
|
|
|
65
|
-
|
|
66
|
-
- **Lightweight embedding with standard Web APIs & UIs**: Go beyond local analysis scripts by shipping **interactive Web tools, mini-games, or Chrome extensions** that anyone can open via a URL with zero backend hosting cost. You can wire mathematical optimization directly into drag-and-drop UIs or **standard browser capabilities**—such as screen/camera capture and speech recognition (Web Speech API)—with no network round-trips or external data transfer.
|
|
67
|
-
- **End-to-end type safety from app data to model**: Prevent common indexing and dictionary-key mistakes with TypeScript's type system. Typed variable dictionaries (`Record<K, Var>`) and variable-reference interpolation (`` expr`30 * ${qty.desk} <= 40` ``) connect directly to your application schemas, preserving compile-time variable/key checks, IDE autocompletion, and symbol renaming.
|
|
68
|
-
- **Strong synergy with AI development & LLM workflows**: Keeping your application and optimization model in a **single TypeScript codebase** lets you validate everything in one test runner—without crossing a Python service boundary—and deploy easily as static assets or a single service. That tight test-and-deploy loop fits AI-assisted coding naturally, while at runtime you can pair flexible LLM extraction with exact solver feasibility and conflict diagnosis (`unsatCore`) in the same runtime.
|
|
51
|
+
The production-planning model below maximizes profit from desks and chairs under wood and labor limits:
|
|
69
52
|
|
|
70
|
-
|
|
53
|
+
```typescript
|
|
54
|
+
import { Model, expr, maximize } from "@ohtaman/washi";
|
|
71
55
|
|
|
72
|
-
|
|
56
|
+
const m = new Model("production");
|
|
57
|
+
|
|
58
|
+
// 1. Define decision variables with realistic finite bounds (lb / ub)
|
|
59
|
+
const desks = m.integer("desks", { lb: 0, ub: 100 });
|
|
60
|
+
const chairs = m.integer("chairs", { lb: 0, ub: 100 });
|
|
61
|
+
|
|
62
|
+
// 2. Set the objective function and constraints
|
|
63
|
+
m.objective(maximize(expr`30 * ${desks} + 50 * ${chairs}`));
|
|
64
|
+
m.constraint(expr`${desks} + 2 * ${chairs} <= 40`, "wood_capacity");
|
|
65
|
+
m.constraint(expr`2 * ${desks} + ${chairs} <= 50`, "labor_capacity");
|
|
73
66
|
|
|
74
|
-
|
|
67
|
+
// 3. Solve (bundled HiGHS WebAssembly) and verify the solution
|
|
68
|
+
const status = await m.solve();
|
|
69
|
+
if (m.hasPrimalSolution) {
|
|
70
|
+
const report = m.verify();
|
|
71
|
+
console.log(status, report.valid); // "Optimal", true
|
|
72
|
+
console.log(`Profit: ${m.objectiveValue} (desks=${desks.value}, chairs=${chairs.value})`); // 1100 (desks=20, chairs=10)
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
75
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
76
|
+
- **Finite bounds**: Supply finite `lb` and `ub` bounds on `m.continuous()` and `m.integer()` variables whenever known; they tighten MIP formulations and are required by `m.indicator()` when `M` is omitted.
|
|
77
|
+
- **Explicit registration & naming**: Constructing an expression with `` expr`...` `` does not attach it to the model until you call `m.objective()` or `m.constraint()`. Passing descriptive constraint names makes `m.check()` conflict reports readable.
|
|
78
|
+
- **Safe reading & verification**: Check `m.hasPrimalSolution` before reading `.value`, then call `m.verify()` to independently confirm bounds, integrality, and constraints.
|
|
79
79
|
|
|
80
80
|
---
|
|
81
81
|
|
|
82
82
|
## 🧭 Next Steps
|
|
83
83
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
- **[
|
|
87
|
-
- **[
|
|
84
|
+
### Choose Your Workflow
|
|
85
|
+
|
|
86
|
+
- **Build with an AI Coding Agent**: [Coding with AI Agents](./coding-agents.md) — Skill setup via `npx skills add ohtaman/washi`, `llms.txt` / Context7, and ready-to-use prompt templates.
|
|
87
|
+
- **Write Models in TypeScript**: [Modeling Guide](../guide/variables.md) — Variable containers, `sum` and `dot` aggregations, logical constraints, and conflict diagnosis.
|
|
88
|
+
|
|
89
|
+
### Reference & Examples
|
|
90
|
+
|
|
91
|
+
- **[Examples & API Finder](../examples/)**: 12 runnable optimization, CP, and SMT recipes—from shift scheduling to portfolio QP.
|
|
92
|
+
- **[Solvers Overview](../solvers/)**: Side-by-side comparison of bundled HiGHS and Pumpkin against optional Z3 and GLPK.
|
|
93
|
+
- **[Runtime Environments](./runtimes.md)**: Setup details for Node.js, Vite/Next.js, CDNs, standalone HTML, and desktop/mobile WebViews.
|
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
# Runtime Environments & Deployment
|
|
2
2
|
|
|
3
|
-
WaSHI runs across JavaScript and TypeScript runtimes
|
|
3
|
+
WaSHI runs across JavaScript and TypeScript runtimes and deployment targets without native solver compilation. Supported targets include Node.js, Bun, Deno, modern bundlers, static hosting, classic `<script>` tags, and offline WebView apps.
|
|
4
|
+
|
|
5
|
+
| Target Environment | Entry Point | Worker & Header Notes |
|
|
6
|
+
| --- | --- | --- |
|
|
7
|
+
| **Node.js (v18+), Bun, Deno** | `@ohtaman/washi` (`npm:@ohtaman/washi` in Deno) | Loads bundled `.wasm` from disk; no HTTP headers required. |
|
|
8
|
+
| **Vite, Next.js, Webpack, Rollup** | `@ohtaman/washi` | Runs HiGHS and Pumpkin in Web Workers by default. |
|
|
9
|
+
| **Tauri, Capacitor, PWA** | `@ohtaman/washi` (standard Vite build) | Runs offline; allow `'wasm-unsafe-eval'` in `script-src` and `worker-src 'self' blob:` (or pass `{ useWorker: false }`) in your Content Security Policy (`CSP`). |
|
|
10
|
+
| **Browser ESM (`<script type="module">`)** | `dist/browser/washi.js` | Keep all `dist/browser/` files together when self-hosting. |
|
|
11
|
+
| **Standalone HTML / CDN (`<script>`)** | `dist/browser/washi.min.js` | Exposes `window.Washi`; CDN builds also run directly on `file://`. |
|
|
4
12
|
|
|
5
13
|
---
|
|
6
14
|
|
|
@@ -8,7 +16,7 @@ WaSHI runs across JavaScript and TypeScript runtimes—from Node.js, Bun, and De
|
|
|
8
16
|
|
|
9
17
|
### Node.js
|
|
10
18
|
|
|
11
|
-
WaSHI supports Node.js
|
|
19
|
+
WaSHI supports Node.js 18+ with standard ECMAScript Modules (`ESM`). Both bundled WebAssembly engines—HiGHS (default) and Pumpkin (`{ solver: "pumpkin" }`)—load directly from `solvers/` inside the installed package with zero extra imports:
|
|
12
20
|
|
|
13
21
|
```typescript
|
|
14
22
|
import { Model, expr, maximize } from "@ohtaman/washi";
|
|
@@ -27,7 +35,7 @@ if (m.hasPrimalSolution) {
|
|
|
27
35
|
}
|
|
28
36
|
```
|
|
29
37
|
|
|
30
|
-
Run directly with `tsx` or `node
|
|
38
|
+
Run TypeScript files directly with `tsx` (or compiled `.js` with `node`):
|
|
31
39
|
|
|
32
40
|
```bash
|
|
33
41
|
npx tsx app.ts
|
|
@@ -61,10 +69,10 @@ if (m.hasPrimalSolution) {
|
|
|
61
69
|
|
|
62
70
|
### Modern Bundlers & Static Deployment
|
|
63
71
|
|
|
64
|
-
|
|
72
|
+
The package root export (`@ohtaman/washi`) resolves `import` and `default` to `./dist/lib/washi.js` (and `types` to `./dist/types/index.d.ts`). That unified ESM bundle detects Node.js versus browser runtimes automatically at execution time:
|
|
65
73
|
|
|
66
74
|
```typescript
|
|
67
|
-
import { Model,
|
|
75
|
+
import { Model, maximize } from "@ohtaman/washi";
|
|
68
76
|
|
|
69
77
|
export async function runOptimization() {
|
|
70
78
|
const m = new Model({ solver: "highs" });
|
|
@@ -75,35 +83,42 @@ export async function runOptimization() {
|
|
|
75
83
|
}
|
|
76
84
|
```
|
|
77
85
|
|
|
78
|
-
Because
|
|
86
|
+
Because the modeler (JS/TS) and bundled solvers (WebAssembly) execute entirely on the client, static builds from `vite build` deploy directly to **GitHub Pages, Cloudflare Pages, Vercel, or Netlify** without a backend server. HiGHS, Pumpkin, and GLPK need no special HTTP headers; for Z3's browser headers, see **Offline, Intranet & Strict CSP Environments** below.
|
|
79
87
|
|
|
80
88
|
#### Background Web Worker Execution (Default)
|
|
81
|
-
In browsers, HiGHS automatically runs inside a dedicated **Web Worker** so LP/MIP/QP solves never freeze the main UI thread, animations, or user input, while enabling real-time progress events (`onProgress`) and cancellation (`AbortSignal`).
|
|
82
89
|
|
|
83
|
-
|
|
90
|
+
In browsers, **HiGHS** and **Pumpkin** run inside dedicated Web Workers by default. This keeps the main UI thread responsive during `LP`, `MIP`, `QP`, and `CP` solves while enabling `onProgress` callbacks and `AbortSignal` cancellation.
|
|
91
|
+
|
|
92
|
+
Pass `useWorker: false` if your environment prohibits Web Workers or you prefer main-thread execution:
|
|
84
93
|
|
|
85
94
|
```typescript
|
|
86
95
|
await m.solve({ useWorker: false });
|
|
87
96
|
```
|
|
88
97
|
|
|
89
|
-
#### Customizing
|
|
90
|
-
|
|
98
|
+
#### Customizing WASM Asset URLs
|
|
99
|
+
|
|
100
|
+
By default, WaSHI resolves `solvers/highs.wasm` and `solvers/pumpkin.wasm` relative to the bundle via `import.meta.url`. It **never** falls back to external CDNs (`jsDelivr`, `unpkg`, etc.).
|
|
91
101
|
|
|
92
|
-
If your
|
|
102
|
+
If your browser host serves `.wasm` files from a custom path, override the URL globally or per call. For HiGHS, `setHighsWasmUrl()` (from `@ohtaman/washi/solvers/highs`) and `{ wasmUrl }` are browser-only (ignored in Node.js); Pumpkin (`setPumpkinWasmUrl()` from `@ohtaman/washi/solvers/pumpkin`) also accepts `file://` URLs in Node.js, and Z3 uses `setZ3Assets()` or `{ z3JsUrl, z3WasmUrl }`:
|
|
93
103
|
|
|
94
104
|
```typescript
|
|
95
|
-
import { setHighsWasmUrl } from "@ohtaman/washi";
|
|
105
|
+
import { setHighsWasmUrl } from "@ohtaman/washi/solvers/highs";
|
|
106
|
+
import { setPumpkinWasmUrl } from "@ohtaman/washi/solvers/pumpkin";
|
|
107
|
+
import { setZ3Assets } from "@ohtaman/washi/solvers/z3"; // Optional Z3 plugin
|
|
96
108
|
|
|
97
109
|
// Option A: Configure globally before solving
|
|
98
110
|
setHighsWasmUrl("/assets/wasm/highs.wasm");
|
|
111
|
+
setPumpkinWasmUrl("/assets/wasm/pumpkin.wasm");
|
|
112
|
+
setZ3Assets({ jsUrl: "/assets/z3/z3-built.js", wasmUrl: "/assets/z3/z3-built.wasm" });
|
|
99
113
|
|
|
100
|
-
// Option B: Configure per solve call
|
|
114
|
+
// Option B: Configure per solve / check call
|
|
101
115
|
await m.solve({ wasmUrl: "/assets/wasm/highs.wasm" });
|
|
116
|
+
// For Z3: await m.check({ solver: "z3", z3JsUrl: "...", z3WasmUrl: "..." });
|
|
102
117
|
```
|
|
103
118
|
|
|
104
119
|
### Next.js & Server-Side Rendering (SSR)
|
|
105
120
|
|
|
106
|
-
In Next.js App Router or Pages Router,
|
|
121
|
+
In Next.js App Router or Pages Router, models run either on the server (Server Actions / API Routes) or in the browser inside `"use client"` components. Below is a client component example:
|
|
107
122
|
|
|
108
123
|
```tsx
|
|
109
124
|
"use client";
|
|
@@ -133,19 +148,37 @@ export default function OptimizerComponent() {
|
|
|
133
148
|
}
|
|
134
149
|
```
|
|
135
150
|
|
|
151
|
+
### Desktop & Mobile WebView Apps (Tauri / Capacitor / PWA)
|
|
152
|
+
|
|
153
|
+
Standard Vite frontends bundle **HiGHS** and **Pumpkin** as static assets for offline execution. They run in Tauri (`tauri://localhost` or `http://tauri.localhost`), Capacitor mobile WebViews, and offline PWAs with no native solver build step.
|
|
154
|
+
|
|
155
|
+
In `tauri.conf.json`, include `'wasm-unsafe-eval'` and `worker-src 'self' blob:` in your `CSP` string. If you run on the main thread with `{ useWorker: false }`, you can omit `worker-src` (see the full CSP table below):
|
|
156
|
+
|
|
157
|
+
```json
|
|
158
|
+
{
|
|
159
|
+
"app": {
|
|
160
|
+
"security": {
|
|
161
|
+
"csp": "default-src 'self'; script-src 'self' 'wasm-unsafe-eval'; worker-src 'self' blob:"
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Prefer bundled HiGHS and Pumpkin in WebViews to avoid Z3's [Cross-Origin Isolation](./cross-origin.md) requirements.
|
|
168
|
+
|
|
136
169
|
---
|
|
137
170
|
|
|
138
171
|
## 📦 Browser ESM (`<script type="module">`)
|
|
139
172
|
|
|
140
|
-
Without a bundler,
|
|
173
|
+
Without a bundler, import the pre-built browser ESM module from your own static server or an npm CDN:
|
|
141
174
|
|
|
142
175
|
```html
|
|
143
176
|
<script type="module">
|
|
144
|
-
// From an npm CDN (or "./dist/browser/washi.js" when self-hosted):
|
|
145
|
-
import { Model, expr, maximize } from "https://cdn.jsdelivr.net/npm/@ohtaman/washi
|
|
177
|
+
// From an npm CDN (or "./dist/browser/washi.js" when self-hosted; includes HiGHS and Pumpkin):
|
|
178
|
+
import { Model, expr, maximize } from "https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/washi.js";
|
|
146
179
|
// Optional solver plugins:
|
|
147
|
-
// import "https://cdn.jsdelivr.net/npm/@ohtaman/washi
|
|
148
|
-
// import "https://cdn.jsdelivr.net/npm/@ohtaman/washi
|
|
180
|
+
// import "https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/glpk.js";
|
|
181
|
+
// import "https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/z3.js";
|
|
149
182
|
|
|
150
183
|
const m = new Model("BrowserESM");
|
|
151
184
|
const x = m.integer("x", { lb: 0, ub: 100 });
|
|
@@ -166,7 +199,7 @@ Without a bundler, you can import the pre-built browser ESM module directly from
|
|
|
166
199
|
|
|
167
200
|
## 🟨 Classic `<script>` Tags (IIFE / Standalone HTML / CDN)
|
|
168
201
|
|
|
169
|
-
For single-file HTML pages, CodePen, or JSFiddle, load `washi.min.js` via a
|
|
202
|
+
For single-file HTML pages, CodePen, or JSFiddle, load `washi.min.js` via a classic `<script>` tag. This immediately invoked function expression (`IIFE`) bundle exposes the core API on the global `window.Washi` object:
|
|
170
203
|
|
|
171
204
|
```html
|
|
172
205
|
<!DOCTYPE html>
|
|
@@ -179,11 +212,11 @@ For single-file HTML pages, CodePen, or JSFiddle, load `washi.min.js` via a stan
|
|
|
179
212
|
<h1>WaSHI Browser Optimization</h1>
|
|
180
213
|
<pre id="output">Solving...</pre>
|
|
181
214
|
|
|
182
|
-
<!-- Core WaSHI + HiGHS (MIT) via CDN (or "./dist/browser/washi.min.js" when self-hosted) -->
|
|
183
|
-
<script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi
|
|
184
|
-
<!-- Optional plugins (auto-register
|
|
185
|
-
<!-- <script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi
|
|
186
|
-
<!-- <script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi
|
|
215
|
+
<!-- Core WaSHI + bundled HiGHS & Pumpkin (MIT) via CDN (or "./dist/browser/washi.min.js" when self-hosted) -->
|
|
216
|
+
<script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/washi.min.js"></script>
|
|
217
|
+
<!-- Optional plugins (load after washi.min.js; auto-register when window.Washi is present): -->
|
|
218
|
+
<!-- <script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/glpk.min.js"></script> -->
|
|
219
|
+
<!-- <script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/z3.min.js"></script> -->
|
|
187
220
|
|
|
188
221
|
<script>
|
|
189
222
|
(async () => {
|
|
@@ -210,23 +243,44 @@ For single-file HTML pages, CodePen, or JSFiddle, load `washi.min.js` via a stan
|
|
|
210
243
|
</html>
|
|
211
244
|
```
|
|
212
245
|
|
|
213
|
-
|
|
214
|
-
|
|
246
|
+
### Cross-Origin CDN Workers
|
|
247
|
+
|
|
248
|
+
On standard `http://` and `https://` origins, browsers block direct `new Worker("https://cdn...")` calls. WaSHI automatically bridges cross-origin CDN workers with a small ES module `Blob` bootstrap.
|
|
249
|
+
|
|
250
|
+
### Opening Local `.html` Files Directly (`file://`)
|
|
251
|
+
|
|
252
|
+
| Scenario | Behavior & Requirement |
|
|
253
|
+
| --- | --- |
|
|
254
|
+
| **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. |
|
|
255
|
+
| **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. |
|
|
256
|
+
| **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). |
|
|
215
257
|
|
|
216
258
|
---
|
|
217
259
|
|
|
218
260
|
## 🔒 Offline, Intranet & Strict CSP Environments
|
|
219
261
|
|
|
220
|
-
WaSHI never phones home or falls back to public CDNs. To self-host in an air-gapped intranet or under a strict
|
|
221
|
-
|
|
222
|
-
1.
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
262
|
+
WaSHI never phones home or falls back to public CDNs. To self-host in an air-gapped intranet or under a strict `CSP`, configure three areas:
|
|
263
|
+
|
|
264
|
+
### 1. Deploy `dist/browser/` Intact
|
|
265
|
+
|
|
266
|
+
Copy `node_modules/@ohtaman/washi/dist/browser/` to your static origin as a complete directory. Even the classic IIFE bundle (`washi.min.js`) spawns `solvers/highs-worker.js` and `solvers/pumpkin-worker.js` as Web Workers by default unless `{ useWorker: false }` is passed. Both the ESM entry points and these worker scripts import hashed `*.js` chunks under `dist/browser/` via relative paths.
|
|
267
|
+
|
|
268
|
+
| Bundle | Required Files under `dist/browser/` |
|
|
269
|
+
| --- | --- |
|
|
270
|
+
| **Core** | `washi.js` / `washi.min.js` |
|
|
271
|
+
| **Bundled HiGHS** | `solvers/highs.js`, `solvers/highs.wasm`, `solvers/highs-worker.js`, and `solvers/highs-*.js` chunks |
|
|
272
|
+
| **Bundled Pumpkin** | `solvers/pumpkin.js`, `solvers/pumpkin.wasm`, `solvers/pumpkin-worker.js`, and `solvers/pumpkin-*.js` chunks |
|
|
273
|
+
| **Optional GLPK** | `solvers/glpk.js` / `solvers/glpk.min.js` |
|
|
274
|
+
| **Optional Z3** | `solvers/z3.js` / `solvers/z3.min.js`, `solvers/z3-built.js`, `solvers/z3-built.wasm` |
|
|
275
|
+
|
|
276
|
+
### 2. Configure CSP Directives
|
|
277
|
+
|
|
278
|
+
| Directive | Required Value | Purpose |
|
|
279
|
+
| --- | --- | --- |
|
|
280
|
+
| `script-src` | `'wasm-unsafe-eval'` | Allows WebAssembly module compilation. |
|
|
281
|
+
| `worker-src` | `'self' blob:` | Allows default Web Workers and CDN `Blob` worker bootstraps. |
|
|
282
|
+
| `worker-src` (if `blob:` is disallowed) | `'self'` only | Serve `solvers/highs-worker.js` / `solvers/pumpkin-worker.js` from `'self'`, or pass `{ useWorker: false }`. |
|
|
283
|
+
|
|
284
|
+
### 3. Enable Cross-Origin Isolation (Z3 Only)
|
|
285
|
+
|
|
286
|
+
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](./cross-origin.md).
|