@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
package/README.md
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
<picture>
|
|
3
3
|
<source media="(prefers-color-scheme: dark)" srcset="./assets/washi-header-dark.png">
|
|
4
4
|
<source media="(prefers-color-scheme: light)" srcset="./assets/washi-header-light.png">
|
|
5
|
-
<img alt="WaSHI -
|
|
5
|
+
<img alt="WaSHI - Mathematical Optimization Modeler in TypeScript" src="./assets/washi-header-light.png" width="420" />
|
|
6
6
|
</picture>
|
|
7
|
-
<p><strong>
|
|
7
|
+
<p><strong>Mathematical Optimization Modeler in TypeScript</strong></p>
|
|
8
8
|
<p>
|
|
9
|
-
<em>Linear Programming (LP) • Mixed-Integer Programming (MIP) • Convex Quadratic Programming (QP) • SMT & Conflict Diagnosis</em>
|
|
9
|
+
<em>Linear Programming (LP) • Mixed-Integer Programming (MIP) • Convex Quadratic Programming (QP) • Constraint Programming (CP) • SMT & Conflict Diagnosis</em>
|
|
10
10
|
</p>
|
|
11
11
|
<p>
|
|
12
12
|
<a href="./README.md">English</a> •
|
|
@@ -16,19 +16,46 @@
|
|
|
16
16
|
<p>
|
|
17
17
|
<a href="https://www.npmjs.com/package/@ohtaman/washi"><img src="https://img.shields.io/npm/v/@ohtaman/washi?color=crimson&label=npm" alt="npm version" /></a>
|
|
18
18
|
<a href="https://github.com/ohtaman/washi/actions/workflows/distribution.yml"><img src="https://github.com/ohtaman/washi/actions/workflows/distribution.yml/badge.svg" alt="CI Status" /></a>
|
|
19
|
-
<a href="https://ohtaman.
|
|
20
|
-
<a href="https://ohtaman.
|
|
19
|
+
<a href="https://washi.ohtaman.dev/"><img src="https://img.shields.io/badge/docs-online-blue" alt="docs" /></a>
|
|
20
|
+
<a href="https://washi.ohtaman.dev/demo/"><img src="https://img.shields.io/badge/demo-playground-brightgreen" alt="live demo" /></a>
|
|
21
21
|
<a href="./LICENSE"><img src="https://img.shields.io/badge/license-MIT-green" alt="license" /></a>
|
|
22
22
|
</p>
|
|
23
23
|
</div>
|
|
24
24
|
|
|
25
25
|
---
|
|
26
26
|
|
|
27
|
-
**WaSHI** (WebAssembly-backed, Solver-independent, High-level modeling Interface) is an algebraic modeler for mathematical optimization and constraint programming in TypeScript and JavaScript, inspired by Python's **PuLP
|
|
27
|
+
**WaSHI** (WebAssembly-backed, Solver-independent, High-level modeling Interface) is an algebraic modeler for mathematical optimization and constraint programming in TypeScript and JavaScript, inspired by Python's **PuLP**. Define variables, objectives, and constraints in application code, and solve them locally in Node.js or the browser using bundled **HiGHS** (default) and **Pumpkin** (`@ohtaman/washi/solvers/pumpkin`), or optional **Z3** and **GLPK** plugins.
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
## ✨ Key Features
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
- **Natural Algebraic Syntax**: Write expressions and constraints directly with `` expr`30 * ${x} + 50 * ${y} <= 40` ``, `sum`, and `dot`.
|
|
32
|
+
- **Runs in Node.js & Browsers**: Powered by WebAssembly—no Python service or native C/C++ build required.
|
|
33
|
+
- **Solver-Independent**: Use bundled zero-dependency **HiGHS** (LP/MIP/QP) and **Pumpkin** (CP/LCG), optional **Z3** and **GLPK** plugins, or custom solver adapters.
|
|
34
|
+
- **Satisfiability & Conflict Diagnosis**: Check logical/arithmetic feasibility and pinpoint contradictory constraints when a model is infeasible.
|
|
35
|
+
- **Decomposition Methods**: Built-in support for Column Generation and Benders Decomposition on large-scale problems.
|
|
36
|
+
|
|
37
|
+
## 📚 Documentation & Links
|
|
38
|
+
|
|
39
|
+
- **[Documentation](https://washi.ohtaman.dev/)**: Modeling guide, solver references, and deployment guides
|
|
40
|
+
- **[Interactive Showcase](https://washi.ohtaman.dev/showcase/) · [Browser Playground](https://washi.ohtaman.dev/demo/) · [Optimization N Exercises](https://washi.ohtaman.dev/exercises/)**: Try live optimization apps, practice interactive modeling workouts, and edit models in the browser
|
|
41
|
+
- **[Examples](https://washi.ohtaman.dev/examples/)**: Problem recipes and runnable Node.js / browser scripts ([`examples/`](examples/README.md))
|
|
42
|
+
- **[Agent Skill (`SKILL.md`)](skills/washi/SKILL.md) · [`llms.txt`](https://washi.ohtaman.dev/llms.txt) / [`llms-full.txt`](https://washi.ohtaman.dev/llms-full.txt)**: Workflow skill and machine-readable docs for AI coding agents
|
|
43
|
+
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
## 📦 Installation & Quick Start
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
npm install @ohtaman/washi
|
|
50
|
+
# HiGHS (LP/MIP/QP) and Pumpkin (CP/LCG, @ohtaman/washi/solvers/pumpkin) are bundled with zero extra dependencies
|
|
51
|
+
|
|
52
|
+
# Optional peer-dependency solver plugins
|
|
53
|
+
npm install z3-solver # For SMT logic & exact real/integer unsat core diagnosis
|
|
54
|
+
npm install glpk.js # For GLPK solver (GPL-3.0)
|
|
55
|
+
|
|
56
|
+
# Optional skill for AI coding agents (Claude Code, Cursor, Codex, etc.)
|
|
57
|
+
npx skills add ohtaman/washi
|
|
58
|
+
```
|
|
32
59
|
|
|
33
60
|
```ts
|
|
34
61
|
import { Model, expr, maximize } from "@ohtaman/washi";
|
|
@@ -47,481 +74,175 @@ console.log(`Produce: x=${x.value}, y=${y.value} (Profit: $${m.objectiveValue})`
|
|
|
47
74
|
|
|
48
75
|
---
|
|
49
76
|
|
|
50
|
-
##
|
|
51
|
-
|
|
52
|
-
1. **Algebraic model construction**: Formulate optimization models using domain-specific variable constructors (`m.binary`, `m.integer`, `m.continuous`), unified tagged template literals (`expr`), vector aggregations (`dot`, `sum`), and first-class constraints (`m.constraint`). Because JavaScript lacks operator overloading, `` expr`...` `` parses infix math (`+`, `-`, `*`, `^2`, `<=`, `>=`, `==`) while binding interpolated `${...}` values directly as typed `Var` or expression references—similar to parameterized SQL templates—preserving static type checking, IDE refactoring, and indexed variable lookups (`${flow[i][j]}`).
|
|
53
|
-
2. **Solver adapters**: Use a common modeling API with HiGHS, Z3, or GLPK, or [register your own adapter](https://github.com/ohtaman/washi/blob/main/docs/solvers/custom-solver.md). Choose an engine that supports the model's problem class; the adapters do not make every solver support every feature.
|
|
54
|
-
3. **Local execution and WebAssembly distribution**: With the local solver adapters, solving runs in the application without a separate Python service or native C/C++ build—removing backend provisioning as a bottleneck when shipping interactive tools or combining mathematical optimization with in-browser LLMs (e.g., WebGPU / WebLLM) on the client. Browser deployments must include the generated JS, Worker, and WASM assets. Offline use also requires those resources to be available locally or cached. Z3 has additional browser isolation requirements.
|
|
55
|
-
4. **Named constraints and conflict diagnosis**: Explicitly check satisfiability (`m.check()`) and inspect conflicting constraints (`conflicts` / `unsatCore`) to investigate infeasibilities: built-in HiGHS isolates an **elastic repair set** (`conflicts`, constraints requiring relaxation to restore feasibility), while Z3 extracts an **unsatisfiable core** (`unsatCore` / `conflicts`, a mutually contradictory constraint subset; minimality is not guaranteed).
|
|
56
|
-
5. **Advanced decomposition methods**: Overcome combinatorial bottlenecks in monolithic MIP formulations (cutting stock, vehicle routing/VRP, traveling salesperson/TSP, and two-stage facility planning) using first-class decomposition orchestrators (`master.solveColumnGeneration`, `master.solveBenders`). WaSHI coordinates dual shadow prices, pricing subproblems, and Benders cut generation via clean callback orchestrators and linking constraints (`sub.link(...)`).
|
|
77
|
+
## ⚙️ Solvers
|
|
57
78
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
---
|
|
61
|
-
|
|
62
|
-
## ⚙️ Solver adapters and capabilities
|
|
63
|
-
|
|
64
|
-
| Adapter | Engine license | Supported problem classes | Engine role | Integration |
|
|
79
|
+
| Solver | License | Problem Classes | Primary Use Case | Setup |
|
|
65
80
|
| :--- | :--- | :--- | :--- | :--- |
|
|
66
|
-
| **[HiGHS](https://github.com/ERGO-Code/HiGHS)** |
|
|
67
|
-
| **[
|
|
68
|
-
| **[
|
|
69
|
-
| **
|
|
81
|
+
| **[HiGHS](https://github.com/ERGO-Code/HiGHS)** | MIT | LP, MIP, Convex QP | Numerical optimization & elastic conflict repair | Built-in default (zero extra deps) |
|
|
82
|
+
| **[Pumpkin](https://github.com/ConSol-Lab/Pumpkin)** | MIT | CP, LCG, Logic, Bounded Integer | Combinatorial scheduling & zero-COOP/COEP `unsatCore` | `import "@ohtaman/washi/solvers/pumpkin"` (bundled WASM, zero deps) |
|
|
83
|
+
| **[Z3](https://github.com/Z3Prover/z3)** | MIT | SMT, SAT, Logic, MIP | Logical rules, satisfiability & exact `unsatCore` | `npm install z3-solver` + `import "@ohtaman/washi/solvers/z3"` |
|
|
84
|
+
| **[GLPK](https://www.gnu.org/software/glpk/)** | GPL-3.0 | LP, MIP | Linear & mixed-integer optimization | `npm install glpk.js` + `import "@ohtaman/washi/solvers/glpk"` |
|
|
85
|
+
| **Mock** | MIT | — | Deterministic unit & integration testing | Built-in (`MockSolver`) |
|
|
70
86
|
|
|
71
|
-
|
|
87
|
+
Pass `{ solver: "pumpkin" }`, `{ solver: "z3" }`, or `{ solver: "glpk" }` to `new Model(...)`, `m.solve(...)`, or `m.check(...)` after importing the plugin.
|
|
72
88
|
|
|
73
|
-
|
|
89
|
+
---
|
|
74
90
|
|
|
75
|
-
|
|
76
|
-
# npm
|
|
77
|
-
npm install @ohtaman/washi
|
|
91
|
+
## 🚀 Usage Examples
|
|
78
92
|
|
|
79
|
-
|
|
80
|
-
pnpm add @ohtaman/washi
|
|
93
|
+
### 1. Sums, Dot Products (`sum` / `dot`) & Conditional Constraints
|
|
81
94
|
|
|
82
|
-
|
|
83
|
-
yarn add @ohtaman/washi
|
|
95
|
+
Combine indexed variables with `dot` / `sum` and express conditional rules with `m.indicator()`:
|
|
84
96
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
```
|
|
97
|
+
```ts
|
|
98
|
+
import { Model, expr, minimize } from "@ohtaman/washi";
|
|
88
99
|
|
|
89
|
-
|
|
100
|
+
const m = new Model("TransportPlanning");
|
|
101
|
+
const factories = ["Tokyo", "Osaka"];
|
|
102
|
+
const stores = ["StoreA", "StoreB"];
|
|
103
|
+
const costs = {
|
|
104
|
+
Tokyo: { StoreA: 4, StoreB: 7 },
|
|
105
|
+
Osaka: { StoreA: 8, StoreB: 3 },
|
|
106
|
+
};
|
|
90
107
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
npm install glpk.js # For GLPK solver (GPL-3.0)
|
|
94
|
-
```
|
|
108
|
+
const open = m.binary(factories);
|
|
109
|
+
const ship = m.continuous(factories, stores, { lb: 0, ub: 500 });
|
|
95
110
|
|
|
96
|
-
|
|
111
|
+
// If Tokyo is closed (open["Tokyo"] == 0), total shipment from Tokyo must be 0
|
|
112
|
+
m.indicator(open["Tokyo"], expr`sum(${ship["Tokyo"]}) <= 0`, { activeValue: 0 });
|
|
113
|
+
m.constraint(expr`${ship["Tokyo"]["StoreA"]} + ${ship["Osaka"]["StoreA"]} >= 100`, "demand_A");
|
|
97
114
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
npx skills add ohtaman/washi
|
|
115
|
+
m.objective(minimize(expr`dot(${costs}, ${ship})`));
|
|
116
|
+
await m.solve();
|
|
101
117
|
|
|
102
|
-
|
|
103
|
-
mkdir -p .agents/skills/washi
|
|
104
|
-
cp node_modules/@ohtaman/washi/skills/washi/SKILL.md .agents/skills/washi/SKILL.md
|
|
118
|
+
console.log("Tokyo -> StoreA:", ship["Tokyo"]["StoreA"].value);
|
|
105
119
|
```
|
|
106
120
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
## 🚀 Usage Guide
|
|
121
|
+
### 2. Convex Quadratic Programming (QP)
|
|
110
122
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
Construct 1D dictionaries or 2D matrices of variables in a single call, express conditional Big-M logic with `m.indicator()`, monitor live progress (`onProgress`), and independently verify solutions with `m.validateSolution()`:
|
|
123
|
+
Minimize a quadratic objective subject to linear constraints:
|
|
114
124
|
|
|
115
125
|
```ts
|
|
116
|
-
import { Model, expr } from "@ohtaman/washi";
|
|
117
|
-
|
|
118
|
-
const m = new Model("FacilityPlanning");
|
|
119
|
-
|
|
120
|
-
// 1D dictionary of binary variables
|
|
121
|
-
const openCity = m.binary(["Tokyo", "Osaka", "Nagoya"]);
|
|
122
|
-
|
|
123
|
-
// 2D transport flow matrix: [Factory] x [Store]
|
|
124
|
-
const flow = m.continuous(["F1", "F2"], ["StoreA", "StoreB"], { lb: 0, ub: 1000 });
|
|
125
|
-
|
|
126
|
-
// 2D coordinate grid (e.g. 8x8 chessboard)
|
|
127
|
-
const board = m.binary(8, 8, { prefix: "queen" });
|
|
128
|
-
|
|
129
|
-
// Conditional Big-M constraint: if openCity["Tokyo"] == 0, flow["F1"]["StoreA"] <= 0
|
|
130
|
-
// (Omit M to let WaSHI automatically derive a tight Big-M from finite variable bounds)
|
|
131
|
-
m.indicator(openCity["Tokyo"], expr`${flow["F1"]["StoreA"]} <= 0`, {
|
|
132
|
-
activeValue: 0,
|
|
133
|
-
name: "tokyo_closed_no_flow",
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
// Solve with optional progress callback & AbortSignal cancellation
|
|
137
|
-
const controller = new AbortController();
|
|
138
|
-
await m.solve({
|
|
139
|
-
signal: controller.signal,
|
|
140
|
-
onProgress: (ev) => {
|
|
141
|
-
// Live branch-and-bound metrics: ev.elapsedTime, ev.objectiveValue, ev.objectiveBound, ev.mipGap
|
|
142
|
-
},
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
// Independently verify variable bounds, integrality, and linear/logical constraints
|
|
146
|
-
const report = m.validateSolution();
|
|
147
|
-
console.log("Solution valid?", report.valid);
|
|
148
|
-
```
|
|
126
|
+
import { Model, expr, minimize } from "@ohtaman/washi";
|
|
149
127
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
128
|
+
const m = new Model("PortfolioQP");
|
|
129
|
+
const x = m.continuous("tech", { lb: 0, ub: 1 });
|
|
130
|
+
const y = m.continuous("bonds", { lb: 0, ub: 1 });
|
|
153
131
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
import { Model, QuadExpr, sum, dot, minimize, expr } from "@ohtaman/washi";
|
|
158
|
-
|
|
159
|
-
const m = new Model("MarkowitzPortfolio");
|
|
160
|
-
const assets = ["Tech", "Bonds", "RealEstate"];
|
|
161
|
-
const returns = [0.14, 0.04, 0.08];
|
|
162
|
-
|
|
163
|
-
// Decision variables: portfolio allocation weights (0 to 1)
|
|
164
|
-
const w = m.continuous(assets, { lb: 0, ub: 1 });
|
|
165
|
-
|
|
166
|
-
// 1. Fully invested budget: sum(w) == 1.0
|
|
167
|
-
m.constraint(expr`${sum(assets, (a) => w[a])} == 1.0`, "budget");
|
|
168
|
-
|
|
169
|
-
// 2. Minimum target return: portfolio return >= 8.5%
|
|
170
|
-
const retMap = Object.fromEntries(assets.map((a, i) => [a, returns[i]]));
|
|
171
|
-
m.constraint(expr`${dot(retMap, w)} >= 0.085`, "min_return");
|
|
172
|
-
|
|
173
|
-
// 3. Covariance risk matrix
|
|
174
|
-
const cov = [
|
|
175
|
-
[0.040, 0.002, 0.015],
|
|
176
|
-
[0.002, 0.005, 0.002],
|
|
177
|
-
[0.015, 0.002, 0.025],
|
|
178
|
-
];
|
|
179
|
-
|
|
180
|
-
// Quadratic objective: w^T * Cov * w
|
|
181
|
-
let risk = new QuadExpr();
|
|
182
|
-
for (let i = 0; i < assets.length; i++) {
|
|
183
|
-
for (let j = 0; j < assets.length; j++) {
|
|
184
|
-
risk = risk.add(w[assets[i]].mul(w[assets[j]]).mul(cov[i][j]));
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
m.objective(minimize(risk));
|
|
132
|
+
m.constraint(expr`${x} + ${y} == 1`, "budget");
|
|
133
|
+
m.constraint(expr`0.14 * ${x} + 0.04 * ${y} >= 0.08`, "min_return");
|
|
134
|
+
m.objective(minimize(expr`0.04 * ${x}^2 + 0.005 * ${y}^2`));
|
|
188
135
|
|
|
189
136
|
await m.solve();
|
|
190
|
-
console.log(`
|
|
191
|
-
assets.forEach((name) => {
|
|
192
|
-
console.log(` ${name}: ${((w[name].value || 0) * 100).toFixed(2)}%`);
|
|
193
|
-
});
|
|
137
|
+
console.log(`Tech: ${x.value?.toFixed(2)}, Bonds: ${y.value?.toFixed(2)}`);
|
|
194
138
|
```
|
|
195
139
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
### 3. SMT Logic & Rule Engines (with Z3)
|
|
140
|
+
### 3. Constraint Programming & Disjunctive Scheduling (Pumpkin)
|
|
199
141
|
|
|
200
|
-
|
|
142
|
+
Use bundled **Pumpkin** (`@ohtaman/washi/solvers/pumpkin`) for bounded integer scheduling and CP global constraints (`disjoint`, `distinct`, `cumulative`, `allowed`) with zero external dependencies:
|
|
201
143
|
|
|
202
144
|
```ts
|
|
203
|
-
import { Model,
|
|
204
|
-
import "@ohtaman/washi/solvers/
|
|
205
|
-
|
|
206
|
-
const m = new Model({
|
|
207
|
-
|
|
208
|
-
const
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
m.constraint(expr`${
|
|
220
|
-
m.constraint(
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
console.log(`Satisfiable? ${res.status === "Sat"}`); // true
|
|
227
|
-
console.log(`Siren active? ${m.values.get("siren")}`); // true
|
|
145
|
+
import { Model, expr, disjoint, distinct, minimize } from "@ohtaman/washi";
|
|
146
|
+
import "@ohtaman/washi/solvers/pumpkin";
|
|
147
|
+
|
|
148
|
+
const m = new Model("WorkshopCP", { solver: "pumpkin" });
|
|
149
|
+
const start = m.integer(["cut", "weld", "paint"] as const, { lb: 0, ub: 20, prefix: "start" });
|
|
150
|
+
const bench = m.integer(["cut", "weld", "paint"] as const, { lb: 1, ub: 3, prefix: "bench" });
|
|
151
|
+
|
|
152
|
+
m.constraint(
|
|
153
|
+
disjoint([
|
|
154
|
+
{ start: start.cut, duration: 4 },
|
|
155
|
+
{ start: start.weld, duration: 3 },
|
|
156
|
+
{ start: start.paint, duration: 2 },
|
|
157
|
+
]),
|
|
158
|
+
"single_line"
|
|
159
|
+
);
|
|
160
|
+
m.constraint(expr`${start.cut} + 4 <= ${start.weld}`, "cut_before_weld");
|
|
161
|
+
m.constraint(expr`${start.weld} + 3 <= ${start.paint}`, "weld_before_paint");
|
|
162
|
+
m.constraint(distinct(bench), "unique_benches");
|
|
163
|
+
|
|
164
|
+
m.objective(minimize(expr`${start.paint} + 2`));
|
|
165
|
+
await m.solve();
|
|
166
|
+
console.log(`Makespan: ${m.objectiveValue}h (paint starts at ${start.paint.value}h)`);
|
|
228
167
|
```
|
|
229
168
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
### 4. Conflict Diagnosis (`conflicts` / `unsatCore`)
|
|
169
|
+
### 4. Logic & Conflict Diagnosis (`m.check`)
|
|
233
170
|
|
|
234
|
-
|
|
235
|
-
- **
|
|
236
|
-
- **
|
|
171
|
+
Express logical implications and diagnose why a model is infeasible:
|
|
172
|
+
- **HiGHS (default)**: Isolates an **elastic repair set** (`res.conflicts`)—constraints whose relaxation restores feasibility.
|
|
173
|
+
- **Pumpkin (bundled)**: Extracts a scale-invariant **unsatisfiable core** (`res.unsatCore` / `res.conflicts`) for bounded integer and boolean models with **zero `COOP`/`COEP` headers**.
|
|
174
|
+
- **Z3**: Extracts an exact SMT **unsatisfiable core** (`res.unsatCore` / `res.conflicts`) including continuous and rational arithmetic.
|
|
237
175
|
|
|
238
176
|
```ts
|
|
239
|
-
import { Model, expr } from "@ohtaman/washi";
|
|
177
|
+
import { Model, implies, expr } from "@ohtaman/washi";
|
|
178
|
+
// import "@ohtaman/washi/solvers/pumpkin"; // Bundled zero-COOP/COEP unsatCore (bounded int/bool)
|
|
179
|
+
// import "@ohtaman/washi/solvers/z3"; // Optional: exact SMT unsatCore including Continuous vars
|
|
240
180
|
|
|
241
|
-
const m = new Model("
|
|
181
|
+
const m = new Model("StaffingRules");
|
|
242
182
|
const engineers = m.integer("engineers", { lb: 0, ub: 20 });
|
|
243
183
|
const budget = m.continuous("budget", { lb: 0, ub: 100000 });
|
|
244
|
-
const
|
|
184
|
+
const rush = m.bool("rush");
|
|
245
185
|
|
|
246
|
-
// Rule 1: Each engineer costs $8,000
|
|
247
186
|
m.constraint(expr`8000 * ${engineers} <= ${budget}`, "budget_limit");
|
|
187
|
+
m.constraint(rush, "rush_required");
|
|
188
|
+
m.constraint(implies(rush, expr`${engineers} >= 8`), "rush_staffing");
|
|
189
|
+
m.constraint(expr`${budget} <= 50000`, "budget_cap"); // Conflict: 8 * $8,000 > $50,000
|
|
190
|
+
m.constraint(expr`${engineers} >= 1`, "min_staff"); // Unrelated rule
|
|
248
191
|
|
|
249
|
-
//
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
// Rule 3: Rush delivery requires at least 8 engineers
|
|
253
|
-
m.constraint(rushDelivery.implies(engineers.ge(8)), "rush_staffing_rule");
|
|
254
|
-
|
|
255
|
-
// Rule 4: Hard board budget cap of $50,000 (Conflict: 8 * $8000 = $64,000 > $50,000!)
|
|
256
|
-
m.constraint(expr`${budget} <= 50000`, "strict_board_budget_cap");
|
|
257
|
-
|
|
258
|
-
// Rule 5: Innocent bystander rule
|
|
259
|
-
m.constraint(expr`${engineers} >= 1`, "minimum_one_employee");
|
|
260
|
-
|
|
261
|
-
// 1. Elastic repair diagnosis with default HiGHS
|
|
262
|
-
const res = await m.check();
|
|
263
|
-
|
|
264
|
-
console.log(`Status: ${res.status}`); // "Unsat"
|
|
265
|
-
console.log("Repair set (relaxed constraints):", res.conflicts);
|
|
266
|
-
// Output with HiGHS: ["rush_required"]
|
|
267
|
-
// (Relaxing rush_required restores feasibility; innocent rules like minimum_one_employee are untouched)
|
|
268
|
-
|
|
269
|
-
// 2. Or exact SMT unsatisfiable core with Z3 (import "@ohtaman/washi/solvers/z3")
|
|
270
|
-
// const z3Res = await m.check({ solver: "z3" });
|
|
271
|
-
// console.log("Contradictory core:", z3Res.unsatCore);
|
|
272
|
-
// Output with Z3: ["budget_limit", "rush_required", "rush_staffing_rule", "strict_board_budget_cap"]
|
|
273
|
-
```
|
|
274
|
-
|
|
275
|
-
---
|
|
276
|
-
|
|
277
|
-
### 5. Solving with GLPK (GPL-3.0)
|
|
278
|
-
|
|
279
|
-
To use the GNU Linear Programming Kit (GLPK) solver plugin:
|
|
280
|
-
|
|
281
|
-
```ts
|
|
282
|
-
import { Model, expr, maximize } from "@ohtaman/washi";
|
|
283
|
-
import "@ohtaman/washi/solvers/glpk";
|
|
284
|
-
|
|
285
|
-
const m = new Model({ solver: "glpk" });
|
|
286
|
-
const x = m.integer("x", { lb: 0 });
|
|
287
|
-
const y = m.integer("y", { lb: 0 });
|
|
288
|
-
|
|
289
|
-
m.objective(maximize(expr`30 * ${x} + 50 * ${y}`));
|
|
290
|
-
m.constraint(expr`${x} + 2 * ${y} <= 40`, "wood_limit");
|
|
291
|
-
m.constraint(expr`2 * ${x} + ${y} <= 50`, "labor_limit");
|
|
292
|
-
|
|
293
|
-
const status = await m.solve();
|
|
294
|
-
console.log(`Status: ${status}`); // "Optimal"
|
|
295
|
-
console.log(`Objective: ${m.objectiveValue}`); // 1100
|
|
296
|
-
```
|
|
297
|
-
|
|
298
|
-
---
|
|
299
|
-
|
|
300
|
-
### 6. Vector Operations & Fast Dot Product
|
|
301
|
-
|
|
302
|
-
Cleanly compute 1D/2D linear combinations without nested loops:
|
|
303
|
-
|
|
304
|
-
```ts
|
|
305
|
-
import { Model, dot, minimize } from "@ohtaman/washi";
|
|
306
|
-
|
|
307
|
-
const m = new Model();
|
|
308
|
-
const factories = ["Tokyo", "Osaka"];
|
|
309
|
-
const stores = ["StoreA", "StoreB"];
|
|
310
|
-
|
|
311
|
-
const costs = {
|
|
312
|
-
Tokyo: { StoreA: 4, StoreB: 7 },
|
|
313
|
-
Osaka: { StoreA: 8, StoreB: 3 },
|
|
314
|
-
};
|
|
315
|
-
|
|
316
|
-
const ship = m.continuous(factories, stores);
|
|
317
|
-
|
|
318
|
-
// Total transportation cost in one line:
|
|
319
|
-
m.objective(minimize(dot(costs, ship)));
|
|
320
|
-
await m.solve();
|
|
321
|
-
|
|
322
|
-
// Extract all optimal variable values as a structured object
|
|
323
|
-
const solution = m.getValues(ship);
|
|
324
|
-
console.log(solution);
|
|
192
|
+
const res = await m.check(); // Or: await m.check({ solver: "z3" })
|
|
193
|
+
console.log(res.status); // "Unsat"
|
|
194
|
+
console.log(res.conflicts); // HiGHS: ["rush_required"]
|
|
325
195
|
```
|
|
326
196
|
|
|
327
197
|
---
|
|
328
198
|
|
|
329
199
|
## 🌐 Browser & Web Worker Usage
|
|
330
200
|
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
### npm with a bundler
|
|
334
|
-
|
|
335
|
-
```typescript
|
|
336
|
-
import { Model } from "@ohtaman/washi";
|
|
337
|
-
const model = new Model();
|
|
338
|
-
// Define variables, objective and constraints.
|
|
339
|
-
await model.solve();
|
|
340
|
-
```
|
|
341
|
-
|
|
342
|
-
No manual WASM copy or URL configuration is needed in the tested Vite / webpack builds. Other bundlers and SSR need their own integration checks; call the browser solver on the client. Worker execution is the browser default. Use `model.solve({ useWorker: false })` for a simple main-thread demo; a long solve will block the UI.
|
|
343
|
-
|
|
344
|
-
### Browser ESM without an application build
|
|
345
|
-
|
|
346
|
-
Serve the **entire `dist/browser` directory**, including its JS chunks, `highs.wasm` and Worker. No import map is needed:
|
|
201
|
+
- **Bundlers (Vite / webpack 5)**: `import { Model } from "@ohtaman/washi"` works out of the box. Both **HiGHS** and **Pumpkin** run in dedicated Web Workers by default (pass `{ useWorker: false }` for main-thread execution) and require **no `COOP`/`COEP` headers**.
|
|
202
|
+
- **Standalone ESM / Script Tag / CDN**: Serve `dist/browser/` (including `highs.wasm`, `solvers/pumpkin.wasm`, and worker chunks) or load from a CDN.
|
|
347
203
|
|
|
348
204
|
```html
|
|
205
|
+
<!-- Browser ESM -->
|
|
349
206
|
<script type="module">
|
|
350
207
|
import { Model } from "./washi/washi.js";
|
|
351
|
-
|
|
352
|
-
//
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
### Plain script / CodePen
|
|
358
|
-
|
|
359
|
-
The package also includes `dist/browser/washi.min.js`, which exports the `Washi` global. Put that file's URL in CodePen's External Scripts, or use a script tag:
|
|
360
|
-
|
|
361
|
-
```html
|
|
362
|
-
<script src="./washi/washi.min.js"></script>
|
|
363
|
-
<script>
|
|
364
|
-
const model = new Washi.Model();
|
|
365
|
-
// Define variables, objective and constraints.
|
|
366
|
-
model.solve({ useWorker: false }).then(status => console.log(status));
|
|
367
|
-
</script>
|
|
368
|
-
```
|
|
369
|
-
|
|
370
|
-
For CDN usage, replace the local URL with `https://cdn.jsdelivr.net/npm/@ohtaman/washi@<version>/dist/browser/washi.min.js` (specifying a compatible release version once published). For ESM use the corresponding `dist/browser/washi.js` URL. Selecting the CDN URL is an explicit choice; local copies never switch to a CDN.
|
|
371
|
-
|
|
372
|
-
### Solver Plugins in the Browser (GLPK & Z3)
|
|
373
|
-
|
|
374
|
-
The browser distribution includes pre-bundled, standalone solver plugins under `dist/browser/solvers/`:
|
|
375
|
-
- **GLPK**: `dist/browser/solvers/glpk.js` (ESM) and `dist/browser/solvers/glpk.min.js` (IIFE, registers global `WashiGLPK`)
|
|
376
|
-
- **Z3**: `dist/browser/solvers/z3.js` (ESM) and `dist/browser/solvers/z3.min.js` (IIFE, registers global `WashiZ3`), accompanied by `z3-built.js` and `z3-built.wasm`
|
|
377
|
-
|
|
378
|
-
#### Browser ESM with GLPK
|
|
379
|
-
```html
|
|
380
|
-
<script type="module">
|
|
381
|
-
import { Model, maximize } from "./washi/washi.js";
|
|
382
|
-
import "./washi/solvers/glpk.js";
|
|
383
|
-
|
|
384
|
-
const model = new Model({ solver: "glpk" });
|
|
385
|
-
const x = model.continuous("x", { lb: 0, ub: 10 });
|
|
386
|
-
model.objective(maximize(x));
|
|
387
|
-
await model.solve();
|
|
388
|
-
console.log("GLPK status:", model.status);
|
|
208
|
+
// Bundled / optional solver plugins:
|
|
209
|
+
// import "./washi/solvers/pumpkin.js"; // Bundled CP/LCG (zero COOP/COEP)
|
|
210
|
+
// import "./washi/solvers/glpk.js"; // Optional GLPK plugin
|
|
211
|
+
// import "./washi/solvers/z3.js"; // Optional Z3 plugin (requires COOP/COEP)
|
|
212
|
+
const m = new Model();
|
|
213
|
+
await m.solve();
|
|
389
214
|
</script>
|
|
390
|
-
```
|
|
391
215
|
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
<script src="
|
|
396
|
-
<script>
|
|
397
|
-
const model = new Washi.Model({ solver: "glpk" });
|
|
398
|
-
const x = model.continuous("x", { lb: 0, ub: 10 });
|
|
399
|
-
model.objective(Washi.maximize(x));
|
|
400
|
-
model.solve().then(status => console.log("GLPK status:", status));
|
|
401
|
-
</script>
|
|
216
|
+
<!-- Classic Script / CodePen (exposes global Washi) -->
|
|
217
|
+
<script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/washi.min.js"></script>
|
|
218
|
+
<!-- Optional solver plugin scripts (auto-register on window.Washi): -->
|
|
219
|
+
<!-- <script src="https://cdn.jsdelivr.net/npm/@ohtaman/washi/dist/browser/solvers/pumpkin.min.js"></script> -->
|
|
402
220
|
```
|
|
403
221
|
|
|
404
222
|
> [!NOTE]
|
|
405
|
-
> **Z3 in Browser
|
|
406
|
-
> Because the Z3 WebAssembly engine uses multithreading (`SharedArrayBuffer`), modern browsers require cross-origin isolation (`Cross-Origin-Opener-Policy: same-origin` and `Cross-Origin-Embedder-Policy: require-corp`). See the [Cross-Origin Isolation Guide](docs/getting-started/cross-origin.md) and [Runtime Environments Guide](docs/getting-started/runtimes.md) for full details on static hosting, `coi-serviceworker.js`, offline/intranet deployment, CSP (`worker-src blob:`), and custom WASM URLs (`setHighsWasmUrl`).
|
|
407
|
-
|
|
408
|
-
Complete standalone examples for ESM, script tags, and CodePen are available in [`examples/distribution/`](examples/distribution/README.md).
|
|
223
|
+
> **Z3 in the Browser**: Because Z3's WebAssembly build uses `SharedArrayBuffer`, browsers require Cross-Origin Isolation (`COOP: same-origin` and `COEP: require-corp`). See the [Cross-Origin Isolation Guide](docs/getting-started/cross-origin.md) and [`examples/distribution/`](examples/distribution/README.md). **HiGHS**, **Pumpkin** (including `unsatCore` on bounded integers/booleans), and **GLPK** require no Cross-Origin Isolation headers.
|
|
409
224
|
|
|
410
225
|
---
|
|
411
226
|
|
|
412
|
-
##
|
|
413
|
-
|
|
414
|
-
WaSHI features a decoupled, pluggable solver architecture. You can easily build and register your own custom solver adapter (e.g., for cloud REST optimizers, WebAssembly engines, or proprietary solvers):
|
|
227
|
+
## 🤝 Contributing
|
|
415
228
|
|
|
416
|
-
|
|
229
|
+
See [CONTRIBUTING.md](./CONTRIBUTING.md) for local development setup, running tests and benchmarks, rebuilding WebAssembly assets, and the release workflow.
|
|
417
230
|
|
|
418
231
|
---
|
|
419
232
|
|
|
420
|
-
##
|
|
233
|
+
## 🙏 Acknowledgments
|
|
421
234
|
|
|
422
|
-
|
|
235
|
+
WaSHI stands on the shoulders of the open-source mathematical optimization and automated reasoning communities:
|
|
423
236
|
|
|
424
|
-
- **
|
|
425
|
-
- **
|
|
426
|
-
- **
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
## 💻 Examples and browser demo
|
|
431
|
-
|
|
432
|
-
Start with the [example gallery](https://github.com/ohtaman/washi/blob/main/examples/README.md) or the [online Example & Feature Finder](https://ohtaman.github.io/washi/examples/), which organize examples by problem category and by WaSHI API feature.
|
|
433
|
-
|
|
434
|
-
| Category | Example | What it demonstrates | Solver |
|
|
435
|
-
| :--- | :--- | :--- | :--- |
|
|
436
|
-
| **Fundamentals** | [Production planning](https://github.com/ohtaman/washi/blob/main/examples/node/01-production-planning.ts) | Integer variables, a profit objective, and resource constraints (`expr`) | HiGHS |
|
|
437
|
-
| **Fundamentals** | [Transportation network](https://github.com/ohtaman/washi/blob/main/examples/node/09-transportation-problem.ts) | 2D origin-destination variable matrix (`continuous(rows, cols)`) and flow balance | HiGHS |
|
|
438
|
-
| **Fundamentals** | [Diet & sensitivity analysis](https://github.com/ohtaman/washi/blob/main/examples/node/10-diet-sensitivity.ts) | Shadow prices (`constr.dual`), surpluses (`constr.slack`), and reduced costs (`var.reducedCost`) | HiGHS |
|
|
439
|
-
| **Practical** | [Staff shift scheduling](https://github.com/ohtaman/washi/blob/main/examples/node/11-shift-scheduling.ts) | Weekly roster optimization + diagnosing conflicting Saturday vacation requests | HiGHS + Z3 |
|
|
440
|
-
| **Practical** | [E-commerce coupon optimizer](https://github.com/ohtaman/washi/blob/main/examples/node/12-ecommerce-coupon-optimizer.ts) | Conditional spend thresholds (`indicator`), coupon exclusivity, and add-on recommendations | HiGHS |
|
|
441
|
-
| **Decomposition** | [Vehicle routing (CVRP)](https://github.com/ohtaman/washi/blob/main/examples/node/13-vehicle-routing-column-generation.ts) | Column Generation (Dantzig-Wolfe): pricing feasible vehicle routes from dual shadow prices | HiGHS |
|
|
442
|
-
| **Decomposition** | [Traveling salesperson (TSP)](https://github.com/ohtaman/washi/blob/main/examples/node/14-traveling-salesperson-benders.ts) | Benders Decomposition (DFJ): lazy subtour elimination constraints via feasibility cuts | HiGHS |
|
|
443
|
-
| **Practical** | [Portfolio allocation](https://github.com/ohtaman/washi/blob/main/examples/node/03-portfolio-qp.ts) | A convex quadratic objective (`QuadExpr`) with allocation constraints | HiGHS |
|
|
444
|
-
| **Logic / SMT** | [Business rule checks](https://github.com/ohtaman/washi/blob/main/examples/node/04-smt-security-rules.ts) | Arithmetic and Boolean constraints (`bool`, `implies`) with satisfiability checking | Z3 |
|
|
445
|
-
| **Logic / SMT** | [Conflict diagnosis](https://github.com/ohtaman/washi/blob/main/examples/node/05-unsat-core-diagnosis.ts) | Named constraints and an unsatisfiable core (`unsatCore`) | Z3 |
|
|
446
|
-
|
|
447
|
-
[Open the hosted demo](https://ohtaman.github.io/washi/demo/) to edit and run models in your browser. For browser ESM, classic script tags, and CodePen, see the [distribution examples](https://github.com/ohtaman/washi/blob/main/examples/distribution/README.md). Z3 requires cross-origin isolation; see the browser requirements above.
|
|
448
|
-
|
|
449
|
-
To run examples locally, clone the repository and install its development dependencies first:
|
|
450
|
-
|
|
451
|
-
```bash
|
|
452
|
-
git clone https://github.com/ohtaman/washi.git
|
|
453
|
-
cd washi
|
|
454
|
-
npm ci
|
|
455
|
-
npx tsx examples/node/01-production-planning.ts
|
|
456
|
-
npx tsx examples/node/09-transportation-problem.ts
|
|
457
|
-
npx tsx examples/node/10-diet-sensitivity.ts
|
|
458
|
-
npx tsx examples/node/11-shift-scheduling.ts
|
|
459
|
-
npx tsx examples/node/12-ecommerce-coupon-optimizer.ts
|
|
460
|
-
npx tsx examples/node/13-vehicle-routing-column-generation.ts
|
|
461
|
-
npx tsx examples/node/14-traveling-salesperson-benders.ts
|
|
462
|
-
|
|
463
|
-
# Local browser demo: http://localhost:5173/demo/
|
|
464
|
-
npm run dev
|
|
465
|
-
```
|
|
466
|
-
|
|
467
|
-
The example sources import the repository's `src/` modules. In an application using the published package, import `@ohtaman/washi` and the required solver plugin instead.
|
|
468
|
-
|
|
469
|
-
---
|
|
470
|
-
|
|
471
|
-
## 🧪 Testing & Reproducible Benchmarks
|
|
472
|
-
|
|
473
|
-
Run the repository's unit and integration tests, and build the distributions:
|
|
474
|
-
|
|
475
|
-
```bash
|
|
476
|
-
# Run all unit and integration tests
|
|
477
|
-
npm test
|
|
478
|
-
|
|
479
|
-
# Build production bundle and TypeScript declarations
|
|
480
|
-
npm run build
|
|
481
|
-
```
|
|
482
|
-
|
|
483
|
-
### Optional Reproducible Benchmarks
|
|
484
|
-
|
|
485
|
-
To measure modeling and solver phases on deterministic `small-lp` (24 variables, 18 constraints) and `scheduling-mip` (315 variables, 231 constraints, including rebuilt `initialValue` warm starts) scenarios without mixing performance timings into correctness test thresholds:
|
|
486
|
-
|
|
487
|
-
```bash
|
|
488
|
-
# Run Node.js in-process WASM benchmarks (HiGHS and GLPK, 5 warm iterations by default)
|
|
489
|
-
npm run bench
|
|
490
|
-
|
|
491
|
-
# Customize iteration count or emit structured JSON
|
|
492
|
-
node scripts/run-benchmarks.mjs --iterations=10 --json
|
|
493
|
-
|
|
494
|
-
# Also measure headless Chromium Web Worker execution (requires Playwright Chromium)
|
|
495
|
-
node scripts/run-benchmarks.mjs --browser
|
|
496
|
-
```
|
|
497
|
-
|
|
498
|
-
- **Explicit phase boundaries**: Reports cold runtime initialization (`coldRuntimeInitMs`, `coldFirstSolveMs`) separately from repeated warm-runtime measurements (`modelBuildMs`, `validationMs`, `lpSerializationMs` for LP-string adapters or `symbolMapMs` for structured adapters, `resultMappingMs` via `MockSolver`, `adapterSolveMs`, and `endToEndSolveMs`). Timings are measured directly per phase and are never subtracted to estimate "native solver time".
|
|
499
|
-
- **Distinct execution environments**: Node.js in-process WASM (`usesBrowserWorker: false`) and Browser Web Worker execution (`usesBrowserWorker: true`, which includes Worker lifecycle and `postMessage` structured-clone transfer) are reported as separate environments, not interchangeable baselines.
|
|
500
|
-
|
|
501
|
-
### Rebuilding HiGHS WebAssembly & Release Workflow
|
|
502
|
-
|
|
503
|
-
1. **Rebuilding / Updating HiGHS WebAssembly Assets (`highs.config.json`)**:
|
|
504
|
-
```bash
|
|
505
|
-
# Compile ERGO-Code/HiGHS to public/highs.wasm + runtime.js and update SHA-256 hashes in highs.config.json
|
|
506
|
-
npm run build:highs-wasm
|
|
507
|
-
|
|
508
|
-
# After merging to main, upload the updated assets to the highs-<version> GitHub Release tag
|
|
509
|
-
# so on-demand downloads (scripts/ensure-highs-wasm.mjs) match highs.config.json SHA-256 hashes:
|
|
510
|
-
gh release upload highs-v1.15.1 public/highs.wasm src/modeler/solvers/highs/runtime.js --clobber
|
|
511
|
-
```
|
|
512
|
-
Alternatively, trigger the **Build HiGHS WebAssembly** workflow (`.github/workflows/build-wasm.yml`) via `workflow_dispatch` with `create_release: true`, then download the `highs-wasm-assets` workflow artifact (`public/highs.wasm`, `src/modeler/solvers/highs/runtime.js`, and `highs.config.json`) and commit those updated files to `main` so repository checksums stay synchronized with the published Release assets.
|
|
513
|
-
|
|
514
|
-
2. **Publishing `@ohtaman/washi` to npm**:
|
|
515
|
-
- Merging to `main` automatically deploys the updated documentation and web demo to GitHub Pages (`.github/workflows/deploy-demo.yml`), but does **not** publish to npm automatically.
|
|
516
|
-
- To publish a new version to npm, bump `"version"` in `package.json` on `main` and publish a GitHub Release (e.g., `gh release create v0.5.1 --generate-notes`). Publishing the GitHub Release triggers `.github/workflows/publish-npm.yml`, which runs unit and Playwright distribution checks before publishing to npm via OIDC Trusted Publishing.
|
|
237
|
+
- **[HiGHS](https://highs.dev/)** ([`ERGO-Code/HiGHS`](https://github.com/ERGO-Code/HiGHS)) — High-performance open-source optimization software for LP, MIP, and Convex QP initiated at the University of Edinburgh by Julian Hall, Qi Huangfu, Ivet Galabova, Leona Gottwald, Michael Feldmeier, and contributors, which powers WaSHI's built-in default solver (*Huangfu & Hall, Mathematical Programming Computation, 2018*, [DOI: 10.1007/s12532-017-0130-5](https://doi.org/10.1007/s12532-017-0130-5)).
|
|
238
|
+
- **[Pumpkin](https://github.com/ConSol-Lab/Pumpkin)** — Lazy Clause Generation (LCG) constraint programming solver developed in Rust by ConSol Lab (TU Delft), powering WaSHI's bundled CP/LCG and zero-`COOP`/`COEP` `unsatCore` engine.
|
|
239
|
+
- **[Z3 Theorem Prover](https://github.com/Z3Prover/z3)** ([`z3-solver`](https://www.npmjs.com/package/z3-solver)) — Developed at Microsoft Research, powering WaSHI's optional SMT satisfiability and exact `unsatCore` conflict diagnosis plugin.
|
|
240
|
+
- **[GLPK](https://www.gnu.org/software/glpk/) & [`glpk.js`](https://github.com/jvail/glpk.js)** — Powering WaSHI's optional GPL-3.0 LP/MIP solver plugin.
|
|
241
|
+
- **[PuLP](https://github.com/coin-or/pulp)** — Inspiring WaSHI's declarative algebraic modeling ergonomics.
|
|
517
242
|
|
|
518
243
|
---
|
|
519
244
|
|
|
520
245
|
## 📄 License
|
|
521
246
|
|
|
522
247
|
- **`@ohtaman/washi`**: [MIT License](./LICENSE)
|
|
523
|
-
- **HiGHS
|
|
524
|
-
- **Z3** (optional plugin via [`z3-solver`](https://www.npmjs.com/package/z3-solver)): MIT License
|
|
525
|
-
- **GLPK** (optional plugin via [`glpk.js`](https://www.npmjs.com/package/glpk.js)): GPL-3.0 License
|
|
526
|
-
|
|
527
|
-
See [THIRD_PARTY_LICENSES.md](./THIRD_PARTY_LICENSES.md) for full attribution and license details of bundled/peer optimization engines.
|
|
248
|
+
- **Bundled / Optional Engines**: HiGHS (MIT), Pumpkin (MIT), Z3 (MIT), GLPK (GPL-3.0). See [THIRD_PARTY_LICENSES.md](./THIRD_PARTY_LICENSES.md) for full details.
|