@ohtaman/washi 0.8.0 → 0.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -34
- package/dist/browser/{__vite-browser-external-rTVKos2U.js → solvers/highs-__vite-browser-external-Dn-83hnF.js} +1 -1
- package/dist/browser/{browser-runtime-AG5QFtKf.js → solvers/highs-browser-runtime-0Bgq5ksG.js} +1 -1
- package/dist/browser/{loader-i3FnqMes.js → solvers/highs-loader-BLY3wzDH.js} +25 -18
- package/dist/browser/{highs-worker.js → solvers/highs-worker.js} +1 -1
- package/dist/browser/solvers/highs.js +1414 -0
- package/dist/browser/washi.js +1090 -1473
- package/dist/browser/washi.min.js +4 -4
- package/dist/lib/solvers/glpk.js +34 -32
- package/dist/lib/{highs-node-runtime.js → solvers/highs-node-runtime.js} +1 -1
- package/dist/lib/solvers/highs.js +410 -0
- package/dist/lib/solvers/lp-writer-CLScMFbS.js +115 -0
- package/dist/lib/solvers/pumpkin-node-runtime.js +1 -1
- package/dist/lib/solvers/pumpkin-worker.js +1 -1
- package/dist/lib/solvers/pumpkin.js +247 -1146
- package/dist/lib/solvers/registry-CrYRRk9q.js +907 -0
- package/dist/lib/solvers/types-BGqPkxwu.js +12 -0
- package/dist/lib/solvers/z3.js +125 -123
- package/dist/lib/washi.js +2426 -1340
- package/dist/types/index.d.ts +1 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/modeler/index.d.ts +0 -1
- package/dist/types/modeler/index.d.ts.map +1 -1
- package/dist/types/modeler/model.d.ts +6 -7
- package/dist/types/modeler/model.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/registry.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 +34 -30
- 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 +25 -12
- package/docs/examples/shift-scheduling.md +27 -19
- package/docs/examples/smt-security.md +27 -12
- package/docs/examples/transportation.md +23 -13
- package/docs/examples/traveling-salesperson.md +34 -22
- package/docs/examples/unsat-core.md +27 -15
- package/docs/examples/vehicle-routing.md +74 -28
- package/docs/getting-started/ai-agents.md +129 -90
- package/docs/getting-started/coding-agents.md +60 -41
- package/docs/getting-started/cross-origin.md +27 -14
- package/docs/getting-started/index.md +23 -16
- package/docs/getting-started/runtimes.md +70 -40
- package/docs/guide/constraint-programming.md +74 -44
- package/docs/guide/constraints.md +71 -39
- package/docs/guide/decomposition.md +72 -42
- package/docs/guide/expressions.md +79 -48
- package/docs/guide/formulation-tips.md +46 -27
- package/docs/guide/objectives.md +55 -31
- package/docs/guide/unsat-core.md +56 -53
- package/docs/guide/variables.md +61 -28
- package/docs/solvers/custom-solver.md +155 -119
- package/docs/solvers/glpk.md +58 -17
- package/docs/solvers/highs.md +115 -43
- package/docs/solvers/index.md +33 -26
- package/docs/solvers/pumpkin.md +71 -63
- package/docs/solvers/z3.md +63 -25
- package/llms-full.txt +1623 -953
- package/llms.txt +125 -93
- package/package.json +29 -22
- package/skills/washi/SKILL.md +43 -22
- package/README.ja.md +0 -248
- package/README.zh-CN.md +0 -248
- package/dist/browser/solvers/pumpkin.min.js +0 -1
- package/dist/lib/registry-dx7x4e5c.js +0 -1694
- /package/dist/browser/{highs.wasm → solvers/highs.wasm} +0 -0
- /package/dist/browser/{highs.wasm.LICENSE → solvers/highs.wasm.LICENSE} +0 -0
- /package/dist/lib/{__vite-browser-external-oLchLuMx.js → solvers/__vite-browser-external-oLchLuMx.js} +0 -0
- /package/dist/lib/{highs-assets.js → solvers/highs-assets.js} +0 -0
- /package/dist/lib/{highs-browser-runtime.js → solvers/highs-browser-runtime.js} +0 -0
- /package/dist/lib/{highs-runtime.js → solvers/highs-runtime.js} +0 -0
- /package/dist/lib/{highs-worker-factory.js → solvers/highs-worker-factory.js} +0 -0
- /package/dist/lib/{highs-worker.js → solvers/highs-worker.js} +0 -0
- /package/dist/lib/{highs.wasm → solvers/highs.wasm} +0 -0
- /package/dist/lib/{highs.wasm.LICENSE → solvers/highs.wasm.LICENSE} +0 -0
- /package/dist/lib/{loader-CDpPXN_F.js → solvers/loader-CDpPXN_F.js} +0 -0
- /package/dist/lib/solvers/{pumpkin-loader-Ow74Fa1B.js → loader-Ow74Fa1B.js} +0 -0
- /package/dist/lib/{rolldown-runtime-DtPi1Y-2.js → solvers/rolldown-runtime-DtPi1Y-2.js} +0 -0
package/docs/solvers/pumpkin.md
CHANGED
|
@@ -1,37 +1,38 @@
|
|
|
1
1
|
# Pumpkin Constraint Programming Solver
|
|
2
2
|
|
|
3
|
-
**Pumpkin** is
|
|
3
|
+
**Pumpkin** is an open-source Constraint Programming (CP) solver written in Rust and maintained by [ConSol-Lab/Pumpkin](https://github.com/ConSol-Lab/Pumpkin). WaSHI bundles Pumpkin (`v0.5.0`, `pumpkin.wasm`, ~2.1 MB) under the MIT license alongside HiGHS with zero extra packages—pass `{ solver: "pumpkin" }` out of the box without any extra plugin import (advanced APIs such as `PumpkinSolver` and `setPumpkinWasmUrl()` are available from `@ohtaman/washi/solvers/pumpkin`).
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Because Pumpkin runs single-threaded, browsers do not need `SharedArrayBuffer` or `COOP`/`COEP` headers (see [Cross-Origin Isolation](../getting-started/cross-origin.md)).
|
|
6
6
|
|
|
7
7
|
---
|
|
8
8
|
|
|
9
|
-
##
|
|
9
|
+
## When to Choose Pumpkin
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
| Problem Type | Recommended Solver | Why |
|
|
12
|
+
| :--- | :--- | :--- |
|
|
13
|
+
| **Scheduling, resource caps, & discrete global constraints** | **Pumpkin** (`{ solver: "pumpkin" }`) | Native propagation for `disjoint`, `cumulative`, `distinct`, and `allowed` with zero extra setup. |
|
|
14
|
+
| **Browser `unsatCore` without `COOP`/`COEP`** | **Pumpkin** (`m.check({ solver: "pumpkin" })`) | Extracts conflicting named constraints on bounded integer and boolean models in any static site or iframe. |
|
|
15
|
+
| **Linear, Mixed-Integer, & Convex Quadratic Programming** | **[HiGHS](./highs.md)** (default) | Supports continuous variables, duals and slacks, convex quadratic objectives, and continuous relaxation. |
|
|
16
|
+
| **Logical rules with `Continuous` variables** | **[Z3](./z3.md)** | Handles real-valued inequalities inside logical operators and mixed continuous/integer `unsatCore`. |
|
|
17
17
|
|
|
18
18
|
---
|
|
19
19
|
|
|
20
|
-
## Supported
|
|
20
|
+
## Supported Modeling Primitives
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
> - Use **Pumpkin** (`{ solver: "pumpkin" }`) for disjunctive scheduling, CP global constraints, bounded integer + propositional logic models, and zero-`COOP`/`COEP` `unsatCore` extraction.
|
|
26
|
-
> - Use **Z3** (`{ solver: "z3" }`) when you need continuous variables inside `implies(...)` / `not(...)` or arbitrary real-valued SMT arithmetic.
|
|
22
|
+
- **Native CP & global constraints**: `disjoint()`, `cumulative()`, `distinct()`, `allowed()`, `at()`, `max()`, `min()`, `abs()`, discrete `!=`, and `m.indicator()`.
|
|
23
|
+
- **Native propositional logic**: `model.bool()`, `and()`, `or()`, `not()`, `implies()`, and `xor()`.
|
|
24
|
+
- **Warm starts (`initialValue`)**: Accepts starting value hints on `model.integer()` and `model.binary()` variables.
|
|
27
25
|
|
|
28
|
-
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Usage Examples
|
|
29
|
+
|
|
30
|
+
Pumpkin is pre-registered in `@ohtaman/washi` with no extra import required—just pass `{ solver: "pumpkin" }` to `new Model()`, `model.solve()`, or `model.check()`. Use `model.solve()` to optimize an integer objective over bounded integer and boolean variables, or use `model.check()` to verify satisfiability and inspect conflicting constraints.
|
|
29
31
|
|
|
30
|
-
|
|
32
|
+
### 1. Scheduling & Global Constraints (`model.solve()`)
|
|
31
33
|
|
|
32
34
|
```typescript
|
|
33
35
|
import { Model, disjoint, expr, implies, minimize, not } from "@ohtaman/washi";
|
|
34
|
-
import "@ohtaman/washi/solvers/pumpkin";
|
|
35
36
|
|
|
36
37
|
const model = new Model("SchedulingStep", { solver: "pumpkin" });
|
|
37
38
|
|
|
@@ -46,6 +47,7 @@ model.constraint(
|
|
|
46
47
|
]),
|
|
47
48
|
"single_machine"
|
|
48
49
|
);
|
|
50
|
+
// Optional: reify the chosen order in job1First (disjoint already enforces non-overlap)
|
|
49
51
|
model.constraint(implies(job1First, expr`${s1} + 6 <= ${s2}`), "job1_before_job2");
|
|
50
52
|
model.constraint(implies(not(job1First), expr`${s2} + 4 <= ${s1}`), "job2_before_job1");
|
|
51
53
|
|
|
@@ -53,15 +55,13 @@ model.objective(minimize(expr`${s1} + ${s2}`));
|
|
|
53
55
|
await model.solve();
|
|
54
56
|
|
|
55
57
|
console.log(`Status: ${model.status}, s1=${s1.value}, s2=${s2.value}`);
|
|
58
|
+
// Status: Optimal, s1=4, s2=0
|
|
56
59
|
```
|
|
57
60
|
|
|
58
|
-
### 2.
|
|
59
|
-
|
|
60
|
-
Call `await model.check()` to verify satisfiability over integer linear constraints, CP global constraints, and nested propositional logic (`and`, `or`, `not`, `implies`, `xor`), and isolate conflicting named rules (`conflicts` / `unsatCore`) when unsatisfiable:
|
|
61
|
+
### 2. Conflict Diagnosis (`model.check()`)
|
|
61
62
|
|
|
62
63
|
```typescript
|
|
63
64
|
import { Model, expr, implies } from "@ohtaman/washi";
|
|
64
|
-
import "@ohtaman/washi/solvers/pumpkin";
|
|
65
65
|
|
|
66
66
|
const model = new Model("PolicyConflict", { solver: "pumpkin" });
|
|
67
67
|
const vip = model.bool("vip");
|
|
@@ -73,7 +73,8 @@ model.constraint(expr`${seats} <= 5`, "room_cap");
|
|
|
73
73
|
|
|
74
74
|
const result = await model.check();
|
|
75
75
|
if (result.status === "Unsat") {
|
|
76
|
-
console.log("Conflicting rules:", result.conflicts); //
|
|
76
|
+
console.log("Conflicting rules:", result.conflicts); // or result.unsatCore / model.unsatCore
|
|
77
|
+
// ["vip_active", "vip_seats", "room_cap"]
|
|
77
78
|
}
|
|
78
79
|
```
|
|
79
80
|
|
|
@@ -81,23 +82,41 @@ if (result.status === "Unsat") {
|
|
|
81
82
|
|
|
82
83
|
## Solver Options
|
|
83
84
|
|
|
84
|
-
Pass
|
|
85
|
+
Pass execution options into `model.solve()`, `model.check()`, or `new PumpkinSolver(options)`:
|
|
86
|
+
|
|
87
|
+
| Option | Type | Description |
|
|
88
|
+
| :--- | :--- | :--- |
|
|
89
|
+
| `timeLimit` | `number` | Maximum solve or check time in seconds. |
|
|
90
|
+
| `useWorker` | `boolean` | Runs Pumpkin in a browser Web Worker (`true` by default in browsers; falls back to `false` on `file:` URLs or opaque origins). Passing `useWorker: true` in Node.js throws an `Error`. |
|
|
91
|
+
| `wasmUrl` | `string \| null` | Overrides the `pumpkin.wasm` asset URL (`null` uses the bundled binary; call `setPumpkinWasmUrl()` to set a global URL). |
|
|
92
|
+
| `signal` | `AbortSignal` | Cancels an in-flight call. In a browser Web Worker, aborts preemptively and preserves the last captured incumbent if available; on the main thread or in Node.js, cancels cooperatively at checkpoints. |
|
|
93
|
+
| `onProgress` | `(ev) => void` | Receives live elapsed time and incumbent objective updates (`solve()` only; ignored by `check()`). |
|
|
94
|
+
|
|
95
|
+
> [!NOTE]
|
|
96
|
+
> Pumpkin is a discrete CP solver: LP sensitivity fields (`c.dual`, `c.slack`, `v.reducedCost`) remain `null`, and MIP bound/gap fields (`objectiveBound`, `relativeGap`, `absoluteGap`) remain `undefined`.
|
|
85
97
|
|
|
86
98
|
```typescript
|
|
99
|
+
import { Model, expr, minimize } from "@ohtaman/washi";
|
|
100
|
+
|
|
101
|
+
const model = new Model("TimedSearch", { solver: "pumpkin" });
|
|
102
|
+
const x = model.integer("x", { lb: 0, ub: 100 });
|
|
103
|
+
const y = model.integer("y", { lb: 0, ub: 100 });
|
|
104
|
+
model.constraint(expr`${x} + ${y} >= 40`, "min_total");
|
|
105
|
+
model.objective(minimize(expr`2 * ${x} + 3 * ${y}`));
|
|
106
|
+
|
|
87
107
|
const controller = new AbortController();
|
|
88
108
|
|
|
89
109
|
await model.solve({
|
|
90
|
-
timeLimit: 30.0,
|
|
110
|
+
timeLimit: 30.0,
|
|
91
111
|
signal: controller.signal,
|
|
92
112
|
onProgress: (ev) => {
|
|
93
113
|
console.log(`Elapsed: ${ev.elapsedTime.toFixed(1)}s, Best: ${ev.objectiveValue}`);
|
|
94
114
|
},
|
|
95
115
|
});
|
|
96
116
|
|
|
97
|
-
// Always check hasPrimalSolution when timeLimit or AbortSignal is used:
|
|
98
117
|
if (model.hasPrimalSolution) {
|
|
99
118
|
console.log(
|
|
100
|
-
`
|
|
119
|
+
`Solution (${model.status}, termination: ${model.terminationStatus}):`,
|
|
101
120
|
`obj = ${model.objectiveValue}`
|
|
102
121
|
);
|
|
103
122
|
}
|
|
@@ -105,59 +124,48 @@ if (model.hasPrimalSolution) {
|
|
|
105
124
|
|
|
106
125
|
---
|
|
107
126
|
|
|
108
|
-
##
|
|
127
|
+
## Requirements & Limitations {#domain-api-preconditions}
|
|
109
128
|
|
|
110
|
-
Pumpkin
|
|
129
|
+
Pumpkin solves **bounded integer and boolean** models only. Unsupported variables, values, or options fail fast by throwing a runtime `Error`:
|
|
111
130
|
|
|
112
|
-
- **Bounded
|
|
113
|
-
- **Integer
|
|
114
|
-
- **
|
|
131
|
+
- **Bounded integers only**: Every `Integer` variable must have finite integer bounds in $[-(2^{30} - 1),\, 2^{30} - 1]$ (`Binary` variables are bounded to 0/1); `Continuous` variables and infinite bounds on `Integer` variables throw an `Error`.
|
|
132
|
+
- **Integer coefficients & scaled bounds**: Constraint coefficients, adjusted right-hand sides (`rhs - lhs.constant`), and scaled variable bounds must be exact integers in $[-(2^{30} - 1),\, 2^{30} - 1]$.
|
|
133
|
+
- **Constraint magnitude sum**: In each constraint, the adjusted right-hand side magnitude plus the sum of worst-case term magnitudes ($|\text{coeff}| \times \max(|\mathrm{lb}|, |\mathrm{ub}|)$) must not exceed $2^{31} - 2$.
|
|
134
|
+
- **Stricter objective bound**: Objective coefficients must be integers whose worst-case scaled term magnitudes sum to at most $2^{30} - 1$; objective constants may be fractional.
|
|
135
|
+
- **Unsupported model features**: `QuadExpr` objectives or constraints and `relax: true` throw a runtime `Error`.
|
|
136
|
+
- **Unsupported options**: `relativeGap`, `absoluteGap`, `presolve`, `threads`, `verbose`, `outputFlag`, and unknown option keys throw a runtime `Error`.
|
|
115
137
|
|
|
116
138
|
---
|
|
117
139
|
|
|
118
|
-
## Worker & WASM
|
|
140
|
+
## Browser Worker & WASM Hosting
|
|
119
141
|
|
|
120
|
-
By default in browsers,
|
|
142
|
+
By default in browsers, Pumpkin loads `pumpkin.wasm` inside a dedicated Web Worker. On `file:` URLs or sandboxed iframes without an origin, it falls back to the main thread automatically.
|
|
121
143
|
|
|
122
|
-
To
|
|
144
|
+
To force main-thread execution or serve `pumpkin.wasm` from a custom URL:
|
|
123
145
|
|
|
124
146
|
```typescript
|
|
125
|
-
import { Model } from "@ohtaman/washi";
|
|
147
|
+
import { Model, expr, minimize } from "@ohtaman/washi";
|
|
126
148
|
import { PumpkinSolver, setPumpkinWasmUrl } from "@ohtaman/washi/solvers/pumpkin";
|
|
127
149
|
|
|
128
|
-
//
|
|
129
|
-
await model.solve({ useWorker: false });
|
|
130
|
-
|
|
131
|
-
// Option 2: When instantiating the solver
|
|
132
|
-
const syncModel = new Model({
|
|
133
|
-
solver: new PumpkinSolver({ useWorker: false }),
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
// Host pumpkin.wasm at a custom path or CDN (globally or per call via { wasmUrl }):
|
|
150
|
+
// Set a custom WASM URL globally (or pass { wasmUrl } to solve() / check())
|
|
137
151
|
setPumpkinWasmUrl("/assets/wasm/pumpkin.wasm");
|
|
138
|
-
```
|
|
139
152
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
153
|
+
const model = new Model({ solver: "pumpkin" });
|
|
154
|
+
const x = model.integer("x", { lb: 0, ub: 10 });
|
|
155
|
+
model.objective(minimize(expr`${x}`));
|
|
143
156
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
WaSHI compiles its Rust WebAssembly wrapper (`wasm/pumpkin`, linking `pumpkin-core`, `pumpkin-constraints`, `pumpkin-propagators`, and `pumpkin-conflict-resolvers` `v0.5.0` to `wasm32-unknown-unknown`) directly to `pumpkin.wasm` with `wasm-bindgen` bindings.
|
|
147
|
-
|
|
148
|
-
Bundled `pumpkin.wasm` and `runtime.js` / `runtime.d.ts` artifacts are tracked and verified via SHA-256 checksums in `pumpkin.config.json`. Maintainers rebuilding Pumpkin from source can follow the instructions in [`CONTRIBUTING.md`](https://github.com/ohtaman/washi/blob/main/CONTRIBUTING.md#4-rebuilding-webassembly-assets--release-workflow).
|
|
157
|
+
// Option 1: Force main-thread execution for a single call
|
|
158
|
+
await model.solve({ useWorker: false });
|
|
149
159
|
|
|
150
|
-
|
|
160
|
+
// Option 2: Configure defaults on a PumpkinSolver instance
|
|
161
|
+
const syncModel = new Model({ solver: new PumpkinSolver({ useWorker: false }) });
|
|
162
|
+
```
|
|
151
163
|
|
|
152
|
-
|
|
153
|
-
During `solve()`, Pumpkin emits real-time incumbent progress callbacks (`onProgress`, providing `elapsedTime` and `objectiveValue`) and honors `AbortSignal`: browser Web Worker execution supports preemptive mid-solve Worker termination and queue cancellation, while main-thread and Node.js runs poll cooperative `should_abort` checkpoints across branching and termination hooks.
|
|
154
|
-
- **Exact Discrete Optimality**:
|
|
155
|
-
As an exact LCG solver over finite integer domains, Pumpkin proves optimality via nogood clause learning and objective-bound strengthening (`LinearSatUnsat`) rather than LP relaxation tolerances (`relativeGap`, `absoluteGap`, or `presolve`).
|
|
164
|
+
If `pumpkin.wasm` fails to load or returns an HTML fallback page, WaSHI throws an error with the attempted URL instead of silently falling back to a CDN.
|
|
156
165
|
|
|
157
166
|
---
|
|
158
167
|
|
|
159
|
-
## References
|
|
168
|
+
## References
|
|
160
169
|
|
|
161
|
-
-
|
|
162
|
-
-
|
|
163
|
-
- **Rust Crate & API Docs**: [docs.rs/pumpkin-core](https://docs.rs/pumpkin-core) · [crates.io/crates/pumpkin-core](https://crates.io/crates/pumpkin-core)
|
|
170
|
+
- [Constraint Programming (CP) Modeling](../guide/constraint-programming.md) · [Resource-Constrained Project Scheduling (RCPSP)](../examples/project-scheduling.md)
|
|
171
|
+
- [ConSol-Lab/Pumpkin](https://github.com/ConSol-Lab/Pumpkin) · [`CONTRIBUTING.md` (rebuilding WASM)](https://github.com/ohtaman/washi/blob/main/CONTRIBUTING.md#4-rebuilding-webassembly-assets--release-workflow)
|
package/docs/solvers/z3.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
# Z3 SMT Solver Engine
|
|
2
2
|
|
|
3
|
-
**Z3** is
|
|
3
|
+
**Z3** is an open-source theorem prover and Satisfiability Modulo Theories (SMT) solver from Microsoft Research. In WaSHI, Z3 handles logical rules involving `Continuous` variables or decimal coefficients/constants, exact arithmetic, and `unsatCore` conflict diagnosis.
|
|
4
4
|
|
|
5
5
|
---
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
`z3-solver` is an optional peer dependency:
|
|
10
10
|
|
|
11
11
|
```bash
|
|
12
12
|
npm install z3-solver
|
|
@@ -14,45 +14,57 @@ npm install z3-solver
|
|
|
14
14
|
|
|
15
15
|
---
|
|
16
16
|
|
|
17
|
-
## When to
|
|
17
|
+
## When to Choose Z3
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
| Goal | Recommended Solver | Why |
|
|
20
|
+
| :--- | :--- | :--- |
|
|
21
|
+
| **Logical rules with `Continuous` or decimal values** | **Z3** (`m.check({ solver: "z3" })`) | Evaluates real-valued inequalities natively inside `implies`, `not`, `and`, `or`, and `xor`. |
|
|
22
|
+
| **Conflict diagnosis (`unsatCore`) with `Continuous` vars** | **Z3** (`m.check({ solver: "z3" })`) | Extracts scale-invariant `unsatCore` subsets across mixed continuous and integer models. |
|
|
23
|
+
| **Bounded integer/boolean Constraint Programming** | **[Pumpkin](./pumpkin.md)** | Bundled with zero dependencies and needs no Cross-Origin Isolation headers. |
|
|
24
|
+
| **Numerical linear, mixed-integer, or convex quadratic optimization** | **[HiGHS](./highs.md)** (default) | Built-in simplex, interior-point, and branch-and-cut solver; much faster for objective optimization. |
|
|
25
25
|
|
|
26
26
|
> [!TIP]
|
|
27
|
-
>
|
|
27
|
+
> A common workflow is to verify rules and diagnose conflicts via **Z3** `model.check()`, then optimize numerical costs via **HiGHS** `model.solve()`.
|
|
28
28
|
|
|
29
29
|
---
|
|
30
30
|
|
|
31
|
-
## Basic Usage
|
|
31
|
+
## Basic Usage (`model.solve()` vs. `model.check()`)
|
|
32
|
+
|
|
33
|
+
Import `@ohtaman/washi/solvers/z3` once to register the `"z3"` solver. Z3 supports two execution modes on the same `Model`:
|
|
34
|
+
|
|
35
|
+
- **`model.check()`**: Checks satisfiability without an objective and returns `"Sat"`, `"Unsat"` (with `conflicts` / `unsatCore`), or `"Unknown"`.
|
|
36
|
+
- **`model.solve()`**: Optimizes a linear objective via Z3 Optimize. When no objective is set, `solve()` finds any feasible assignment and reports `"Optimal"` (or `"Feasible"` if `signal` was aborted while the call was completing).
|
|
37
|
+
|
|
38
|
+
The example below finds a feasible assignment for a continuous threshold rule controlled by a boolean flag:
|
|
32
39
|
|
|
33
40
|
```typescript
|
|
34
|
-
import { Model, expr } from "@ohtaman/washi";
|
|
41
|
+
import { Model, expr, implies } from "@ohtaman/washi";
|
|
35
42
|
import "@ohtaman/washi/solvers/z3";
|
|
36
43
|
|
|
37
44
|
const model = new Model({ solver: "z3" });
|
|
38
45
|
|
|
39
|
-
const
|
|
40
|
-
const
|
|
46
|
+
const temp = model.continuous("temp", { lb: 0, ub: 100 });
|
|
47
|
+
const coolingOn = model.bool("cooling_on");
|
|
41
48
|
|
|
42
|
-
model.constraint(
|
|
43
|
-
model.constraint(expr`${
|
|
49
|
+
model.constraint(coolingOn, "enable_cooling");
|
|
50
|
+
model.constraint(implies(coolingOn, expr`${temp} <= 22.5`), "cooling_target");
|
|
44
51
|
|
|
52
|
+
// Without an objective, solve() finds any feasible assignment.
|
|
53
|
+
// Call model.check() instead for "Sat" / "Unsat" and unsatCore diagnostics.
|
|
45
54
|
await model.solve();
|
|
46
|
-
console.log(`
|
|
55
|
+
console.log(`Status: ${model.status}, temp = ${temp.value}`);
|
|
47
56
|
```
|
|
48
57
|
|
|
49
58
|
---
|
|
50
59
|
|
|
51
|
-
##
|
|
60
|
+
## Conflict Diagnosis (`model.check()`)
|
|
52
61
|
|
|
53
|
-
|
|
62
|
+
Call `await model.check()` to verify satisfiability across continuous, integer, or mixed rules. When the model is `"Unsat"`, Z3 returns a conflicting subset of named constraints via `result.conflicts` (also available as `result.unsatCore` and `model.unsatCore`):
|
|
54
63
|
|
|
55
64
|
```typescript
|
|
65
|
+
import { Model, expr } from "@ohtaman/washi";
|
|
66
|
+
import "@ohtaman/washi/solvers/z3";
|
|
67
|
+
|
|
56
68
|
const model = new Model({ solver: "z3" });
|
|
57
69
|
const a = model.continuous("a");
|
|
58
70
|
|
|
@@ -62,20 +74,46 @@ model.constraint(expr`${a} <= 5`, "req_B");
|
|
|
62
74
|
const result = await model.check();
|
|
63
75
|
|
|
64
76
|
if (result.status === "Unsat") {
|
|
65
|
-
console.log("Conflicting constraints:", result.conflicts);
|
|
77
|
+
console.log("Conflicting constraints:", result.conflicts);
|
|
66
78
|
// ["req_A", "req_B"]
|
|
67
79
|
}
|
|
68
80
|
```
|
|
69
81
|
|
|
70
82
|
---
|
|
71
83
|
|
|
72
|
-
##
|
|
84
|
+
## Supported & Unsupported Options
|
|
85
|
+
|
|
86
|
+
Z3 validates options strictly and throws a runtime `Error` when unsupported options or model features are supplied.
|
|
73
87
|
|
|
74
|
-
|
|
88
|
+
### Supported Options
|
|
75
89
|
|
|
76
|
-
|
|
90
|
+
- `z3JsUrl` / `z3WasmUrl`: Override browser asset URLs for `z3-built.js` and `z3-built.wasm` (or configure globally with `setZ3Assets()`).
|
|
91
|
+
- `signal`: `AbortSignal` checked **before** Z3 starts solving (pre-solve cancellation only; it cannot interrupt an active Z3 call).
|
|
92
|
+
- `onProgress`: Applies to `solve()` only (ignored by `check()`). Invoked once after `solve()` completes with elapsed time and, when a solution exists, the final objective value.
|
|
77
93
|
|
|
78
|
-
|
|
94
|
+
### Unsupported Options & Features (Throw at Runtime)
|
|
79
95
|
|
|
80
|
-
|
|
96
|
+
- **Unsupported tuning options**: `timeLimit`, `relativeGap`, `absoluteGap`, `presolve`, `threads`, `verbose`, `outputFlag`, and unknown option keys throw an `Error`.
|
|
97
|
+
- **Unsupported modes & features**: `relax: true`, `useWorker: true`, `initialValue` warm starts, and `QuadExpr` quadratic objectives throw an `Error`.
|
|
81
98
|
|
|
99
|
+
> [!NOTE]
|
|
100
|
+
> Z3 Optimize is an SMT engine without LP sensitivity or branch-and-bound dual bounds: `c.dual`, `c.slack`, and `v.reducedCost` remain `null`, and `objectiveBound`, `relativeGap`, and `absoluteGap` remain `undefined`.
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
## Browser Requirements & Thread Cleanup
|
|
105
|
+
|
|
106
|
+
In browsers, Z3 uses `SharedArrayBuffer` and Emscripten pthreads. Your page must satisfy two browser preconditions:
|
|
107
|
+
|
|
108
|
+
1. **Secure Context**: Served over `HTTPS` or `localhost`.
|
|
109
|
+
2. **Cross-Origin Isolation (`COOP`/`COEP`)**: Document responses must send `Cross-Origin-Opener-Policy: same-origin` and `Cross-Origin-Embedder-Policy: require-corp`.
|
|
110
|
+
|
|
111
|
+
See the [Cross-Origin Isolation Guide](../getting-started/cross-origin.md) for server setup. If you cannot configure these headers and only need bounded integers and booleans, use [Pumpkin](./pumpkin.md) instead.
|
|
112
|
+
|
|
113
|
+
If a Z3 worker hits a fatal error, the pending promise rejects and the broken runtime is discarded so the next call starts cleanly. To shut down Z3 pthread workers explicitly—both in Node.js (so CLI scripts and processes exit cleanly) and in browsers (for example, when unmounting a view)—call:
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
import { killZ3Threads } from "@ohtaman/washi/solvers/z3";
|
|
117
|
+
|
|
118
|
+
await killZ3Threads();
|
|
119
|
+
```
|