@ohtaman/washi 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ja.md +139 -418
- package/README.md +139 -418
- package/README.zh-CN.md +139 -418
- package/THIRD_PARTY_LICENSES.md +39 -4
- package/dist/browser/__vite-browser-external-rTVKos2U.js +7 -0
- package/dist/browser/browser-runtime-AG5QFtKf.js +2021 -0
- package/dist/browser/highs-worker.js +26 -11
- package/dist/browser/highs.wasm +0 -0
- package/dist/browser/{loader-DewlSho8.js → loader-i3FnqMes.js} +183 -149
- package/dist/browser/solvers/glpk.js +289 -238
- package/dist/browser/solvers/glpk.min.js +1 -1
- package/dist/browser/solvers/pumpkin-_washi-pumpkin-node-runtime-CEKUDK6d.js +9 -0
- package/dist/browser/solvers/pumpkin-loader-CvdaKxf4.js +320 -0
- package/dist/browser/solvers/pumpkin-worker.js +45 -0
- package/dist/browser/solvers/pumpkin.js +1777 -0
- package/dist/browser/solvers/pumpkin.min.js +1 -0
- package/dist/browser/solvers/pumpkin.wasm +0 -0
- package/dist/browser/solvers/pumpkin.wasm.LICENSE +21 -0
- package/dist/browser/solvers/z3.js +1600 -1335
- package/dist/browser/solvers/z3.min.js +2 -2
- package/dist/browser/washi.js +3828 -1315
- package/dist/browser/washi.min.js +5 -3
- package/dist/lib/__vite-browser-external-oLchLuMx.js +7 -0
- package/dist/lib/highs-browser-runtime.js +1846 -1618
- package/dist/lib/highs-runtime.js +16 -2
- package/dist/lib/highs-worker.js +26 -11
- package/dist/lib/highs.wasm +0 -0
- package/dist/lib/{loader-BZUQNvh4.js → loader-CDpPXN_F.js} +138 -116
- package/dist/lib/registry-dx7x4e5c.js +1694 -0
- package/dist/lib/rolldown-runtime-DtPi1Y-2.js +13 -0
- package/dist/lib/solvers/glpk.js +161 -121
- package/dist/lib/solvers/pumpkin-assets.js +1 -0
- package/dist/lib/solvers/pumpkin-loader-Ow74Fa1B.js +311 -0
- package/dist/lib/solvers/pumpkin-node-runtime.js +15 -0
- package/dist/lib/solvers/pumpkin-worker-factory.js +1 -0
- package/dist/lib/solvers/pumpkin-worker.js +45 -0
- package/dist/lib/solvers/pumpkin.js +1767 -0
- package/dist/lib/solvers/pumpkin.wasm +0 -0
- package/dist/lib/solvers/pumpkin.wasm.LICENSE +21 -0
- package/dist/lib/solvers/z3.js +486 -306
- package/dist/lib/washi.js +3054 -1202
- package/dist/types/decomposition/benders.d.ts.map +1 -1
- package/dist/types/index.d.ts +3 -3
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/modeler/boolean-expr.d.ts +137 -29
- package/dist/types/modeler/boolean-expr.d.ts.map +1 -1
- package/dist/types/modeler/constraint.d.ts +40 -4
- package/dist/types/modeler/constraint.d.ts.map +1 -1
- package/dist/types/modeler/expression.d.ts +176 -28
- package/dist/types/modeler/expression.d.ts.map +1 -1
- package/dist/types/modeler/index.d.ts +5 -6
- package/dist/types/modeler/index.d.ts.map +1 -1
- package/dist/types/modeler/linearizer.d.ts +11 -2
- package/dist/types/modeler/linearizer.d.ts.map +1 -1
- package/dist/types/modeler/lp-writer.d.ts.map +1 -1
- package/dist/types/modeler/model.d.ts +97 -130
- package/dist/types/modeler/model.d.ts.map +1 -1
- package/dist/types/modeler/parser.d.ts +25 -5
- package/dist/types/modeler/parser.d.ts.map +1 -1
- package/dist/types/modeler/quad-expr.d.ts +8 -2
- package/dist/types/modeler/quad-expr.d.ts.map +1 -1
- package/dist/types/modeler/solution-validator.d.ts +12 -12
- package/dist/types/modeler/solution-validator.d.ts.map +1 -1
- package/dist/types/modeler/solvers/glpk/loader.d.ts +4 -0
- package/dist/types/modeler/solvers/glpk/loader.d.ts.map +1 -1
- package/dist/types/modeler/solvers/glpk/solver.d.ts +2 -2
- package/dist/types/modeler/solvers/glpk/solver.d.ts.map +1 -1
- package/dist/types/modeler/solvers/highs/driver.d.ts.map +1 -1
- package/dist/types/modeler/solvers/highs/loader.d.ts +8 -6
- package/dist/types/modeler/solvers/highs/loader.d.ts.map +1 -1
- package/dist/types/modeler/solvers/highs/solver.d.ts.map +1 -1
- package/dist/types/modeler/solvers/highs/worker-client.d.ts +16 -0
- package/dist/types/modeler/solvers/highs/worker-client.d.ts.map +1 -1
- package/dist/types/modeler/solvers/mock.d.ts +2 -3
- package/dist/types/modeler/solvers/mock.d.ts.map +1 -1
- package/dist/types/modeler/solvers/pumpkin/assets.d.ts +2 -0
- package/dist/types/modeler/solvers/pumpkin/assets.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/index.d.ts +6 -0
- package/dist/types/modeler/solvers/pumpkin/index.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/loader.d.ts +40 -0
- package/dist/types/modeler/solvers/pumpkin/loader.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/node-runtime.d.ts +3 -0
- package/dist/types/modeler/solvers/pumpkin/node-runtime.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/solver.d.ts +23 -0
- package/dist/types/modeler/solvers/pumpkin/solver.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/worker-client.d.ts +74 -0
- package/dist/types/modeler/solvers/pumpkin/worker-client.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/worker-factory.d.ts +3 -0
- package/dist/types/modeler/solvers/pumpkin/worker-factory.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin/worker.d.ts +7 -0
- package/dist/types/modeler/solvers/pumpkin/worker.d.ts.map +1 -0
- package/dist/types/modeler/solvers/pumpkin.d.ts +2 -0
- package/dist/types/modeler/solvers/pumpkin.d.ts.map +1 -0
- package/dist/types/modeler/solvers/registry.d.ts.map +1 -1
- package/dist/types/modeler/solvers/types.d.ts +9 -9
- package/dist/types/modeler/solvers/types.d.ts.map +1 -1
- package/dist/types/modeler/solvers/z3/loader.d.ts +4 -0
- package/dist/types/modeler/solvers/z3/loader.d.ts.map +1 -1
- package/dist/types/modeler/solvers/z3/solver.d.ts +2 -15
- package/dist/types/modeler/solvers/z3/solver.d.ts.map +1 -1
- package/dist/types/modeler/types.d.ts +12 -15
- package/dist/types/modeler/types.d.ts.map +1 -1
- package/dist/types/modeler/validation.d.ts +26 -2
- package/dist/types/modeler/validation.d.ts.map +1 -1
- package/dist/types/modeler/variable.d.ts +9 -5
- package/dist/types/modeler/variable.d.ts.map +1 -1
- package/docs/examples/diet-sensitivity.md +15 -14
- package/docs/examples/ecommerce-coupons.md +15 -22
- package/docs/examples/index.md +20 -13
- package/docs/examples/knapsack.md +8 -7
- package/docs/examples/portfolio-qp.md +11 -4
- package/docs/examples/production.md +4 -4
- package/docs/examples/project-scheduling.md +106 -0
- package/docs/examples/shift-scheduling.md +23 -7
- package/docs/examples/smt-security.md +17 -4
- package/docs/examples/transportation.md +46 -19
- package/docs/examples/traveling-salesperson.md +29 -45
- package/docs/examples/unsat-core.md +16 -5
- package/docs/examples/vehicle-routing.md +10 -8
- package/docs/getting-started/ai-agents.md +114 -34
- package/docs/getting-started/coding-agents.md +158 -0
- package/docs/getting-started/cross-origin.md +56 -28
- package/docs/getting-started/index.md +36 -66
- package/docs/getting-started/runtimes.md +98 -69
- package/docs/guide/constraint-programming.md +141 -0
- package/docs/guide/constraints.md +100 -44
- package/docs/guide/decomposition.md +141 -273
- package/docs/guide/expressions.md +125 -42
- package/docs/guide/formulation-tips.md +212 -0
- package/docs/guide/objectives.md +70 -64
- package/docs/guide/unsat-core.md +44 -23
- package/docs/guide/variables.md +56 -48
- package/docs/solvers/custom-solver.md +18 -13
- package/docs/solvers/highs.md +41 -25
- package/docs/solvers/index.md +13 -7
- package/docs/solvers/pumpkin.md +163 -0
- package/docs/solvers/z3.md +4 -1
- package/llms-full.txt +1732 -860
- package/llms.txt +59 -36
- package/package.json +40 -13
- package/skills/washi/SKILL.md +91 -35
- package/dist/browser/_washi-node-only-BvRk9kiK.js +0 -0
- package/dist/browser/browser-runtime-TLCY0hkc.js +0 -1793
- package/dist/lib/_washi-node-only-BvRk9kiK.js +0 -0
- package/dist/lib/registry-C20sxTwm.js +0 -964
- package/docs/ja/getting-started/ai-agents.md +0 -270
- package/docs/zh/getting-started/ai-agents.md +0 -270
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
# Constraints (`m.constraint` & `m.indicator`)
|
|
2
2
|
|
|
3
3
|
Constraints restrict the feasible solution space of your mathematical or logical model.
|
|
4
|
-
In WaSHI, both linear arithmetic constraints and propositional
|
|
4
|
+
In WaSHI, both linear arithmetic constraints and propositional logic formulas are registered using **`m.constraint()`**:
|
|
5
5
|
|
|
6
6
|
```typescript
|
|
7
|
-
m.constraint(expr`${
|
|
7
|
+
m.constraint(expr`${x} + 2 * ${y} <= 40`, "wood_limit");
|
|
8
8
|
```
|
|
9
9
|
|
|
10
10
|
---
|
|
11
11
|
|
|
12
|
-
##
|
|
12
|
+
## Algebraic Linear Constraints (`expr`)
|
|
13
13
|
|
|
14
|
-
The `expr` tagged template literal
|
|
14
|
+
The `expr` tagged template literal allows writing equations and inequalities with standard mathematical notation:
|
|
15
15
|
|
|
16
16
|
```typescript
|
|
17
17
|
import { Model, expr } from "@ohtaman/washi";
|
|
@@ -30,13 +30,16 @@ m.constraint(expr`3 * ${x} + ${y} >= 20`, "min_demand");
|
|
|
30
30
|
m.constraint(expr`${x} + ${y} == 30`, "exact_quota");
|
|
31
31
|
```
|
|
32
32
|
|
|
33
|
+
`m.constraint()` returns the registered `Constraint` (or `BoolExpr` for logical formulas), or an array when passed a batch array.
|
|
34
|
+
|
|
33
35
|
### Supported Comparison Operators
|
|
34
36
|
- `<=`: Upper bound constraint
|
|
35
37
|
- `>=`: Lower bound constraint
|
|
36
38
|
- `==` or `=`: Exact equality constraint
|
|
39
|
+
- `<`, `>`, `!=`: Discrete strict inequality / disequality constraints (integer-valued expressions only; see [Discrete Comparisons](#discrete-comparisons))
|
|
37
40
|
|
|
38
41
|
### Batch Registration
|
|
39
|
-
|
|
42
|
+
Pass an array of constraints to `m.constraint([...])` to register them transactionally in a single call:
|
|
40
43
|
|
|
41
44
|
```typescript
|
|
42
45
|
m.constraint([expr`${x} <= 10`, expr`${y} <= 20`]);
|
|
@@ -44,50 +47,68 @@ m.constraint([expr`${x} <= 10`, expr`${y} <= 20`]);
|
|
|
44
47
|
|
|
45
48
|
---
|
|
46
49
|
|
|
47
|
-
##
|
|
50
|
+
## Naming Constraints
|
|
48
51
|
|
|
49
|
-
|
|
52
|
+
Pass a unique, descriptive name as the second argument of `m.constraint(c, name)`:
|
|
50
53
|
|
|
51
54
|
```typescript
|
|
52
55
|
m.constraint(expr`${x} + ${y} <= 100`, "budget_cap");
|
|
53
56
|
```
|
|
54
57
|
|
|
55
58
|
### Why Name Constraints?
|
|
56
|
-
1. **Model Inspection (`m.toLpString()`)**: Exported CPLEX LP strings retain these descriptive names (sanitized if needed) so you can inspect the
|
|
57
|
-
2. **Conflict Diagnosis (`conflicts` / `unsatCore`)**: When constraints contradict each other,
|
|
58
|
-
3. **Direct Lookup (`m.
|
|
59
|
+
1. **Model Inspection (`m.toLpString()`)**: Exported CPLEX LP strings retain these descriptive names (sanitized if needed) so you can inspect the generated formulation.
|
|
60
|
+
2. **Conflict Diagnosis (`conflicts` / `unsatCore`)**: When constraints contradict each other, `m.check()` returns the conflicting constraint names (via built-in HiGHS elastic relaxation, bundled Pumpkin `unsatCore`, or Z3 `unsatCore`).
|
|
61
|
+
3. **Direct Lookup (`m.constraints.get()`)**: Retrieve any registered constraint by name after solving to inspect its `dual` or `slack` value.
|
|
59
62
|
|
|
60
63
|
---
|
|
61
64
|
|
|
62
|
-
##
|
|
65
|
+
## Aggregated Constraints (`sum` & `dot`)
|
|
63
66
|
|
|
64
|
-
Use `sum()`
|
|
67
|
+
Use `sum(...)` and `dot(..., ...)` directly inside `expr` to express linear aggregations over arrays and dictionaries cleanly:
|
|
65
68
|
|
|
66
69
|
```typescript
|
|
67
|
-
import { Model,
|
|
70
|
+
import { Model, expr } from "@ohtaman/washi";
|
|
68
71
|
|
|
69
|
-
const
|
|
72
|
+
const m = new Model();
|
|
73
|
+
const items = ["A", "B", "C"] as const;
|
|
70
74
|
const weights = { A: 5, B: 10, C: 15 };
|
|
71
75
|
const selected = m.binary(items);
|
|
72
76
|
|
|
77
|
+
// Select at most 2 items
|
|
78
|
+
m.constraint(expr`sum(${selected}) <= 2`, "max_items");
|
|
79
|
+
|
|
73
80
|
// Total weight cannot exceed capacity 25
|
|
74
|
-
m.constraint(
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
81
|
+
m.constraint(expr`dot(${weights}, ${selected}) <= 25`, "capacity_limit");
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
## Dual Values, Shadow Prices & Lookups
|
|
87
|
+
|
|
88
|
+
For continuous Linear Programming (LP) problems, query the shadow price (`dual`) and surplus (`slack`) on each constraint (returned by `m.constraint()` or looked up via `m.constraints.get()`):
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
const woodConstraint = m.constraint(expr`${x} + 2 * ${y} <= 40`, "wood");
|
|
92
|
+
|
|
93
|
+
await m.solve();
|
|
94
|
+
|
|
95
|
+
console.log("Shadow price (dual):", woodConstraint.dual);
|
|
96
|
+
console.log("Slack surplus:", woodConstraint.slack);
|
|
97
|
+
console.log("Looked-up dual:", m.constraints.get("wood")?.dual); // number | null (null for MIP, CP, or logical constraints)
|
|
98
|
+
console.log("Total constraints:", m.constraints.size);
|
|
78
99
|
```
|
|
79
100
|
|
|
80
101
|
---
|
|
81
102
|
|
|
82
|
-
##
|
|
103
|
+
## Conditional / Indicator Constraints (`m.indicator`)
|
|
83
104
|
|
|
84
|
-
When a linear constraint should only take effect when a binary decision variable
|
|
105
|
+
When a linear constraint should only take effect when a binary decision variable (`m.binary()`) equals `1` (or `0`), use `m.indicator()`:
|
|
85
106
|
|
|
86
107
|
```typescript
|
|
87
108
|
const useDiscount = m.binary("use_discount");
|
|
88
109
|
const cartTotal = m.continuous("cart_total", { lb: 0, ub: 10_000 });
|
|
89
110
|
|
|
90
|
-
// If useDiscount == 1,
|
|
111
|
+
// If useDiscount == 1, enforce cartTotal >= 100
|
|
91
112
|
// WaSHI automatically derives a safe, tight Big-M from the variable bounds
|
|
92
113
|
m.indicator(useDiscount, expr`${cartTotal} >= 100`, {
|
|
93
114
|
activeValue: 1,
|
|
@@ -97,16 +118,19 @@ m.indicator(useDiscount, expr`${cartTotal} >= 100`, {
|
|
|
97
118
|
|
|
98
119
|
### Safe Automatic Big-M Derivation & Bounds
|
|
99
120
|
|
|
100
|
-
When `options.M` is omitted, WaSHI automatically derives a
|
|
121
|
+
When `options.M` is omitted, WaSHI automatically derives a minimal valid Big-M value from finite variable bounds (`lb` and `ub`):
|
|
101
122
|
- For `<=` constraints, variables with positive coefficients require finite upper bounds (`ub`), and negative coefficients require finite lower bounds (`lb`).
|
|
102
123
|
- For `>=` constraints, variables with positive coefficients require finite lower bounds (`lb`), and negative coefficients require finite upper bounds (`ub`).
|
|
103
|
-
- For `==` constraints,
|
|
124
|
+
- For `==` constraints, bounds are derived separately for both the `<=` and `>=` directions (generating `name_le` and `name_ge`).
|
|
125
|
+
|
|
126
|
+
If any referenced variable lacks the required finite bound, `m.indicator()` throws an error prompting for finite bounds or an explicit `options.M`.
|
|
104
127
|
|
|
105
|
-
|
|
128
|
+
> [!NOTE]
|
|
129
|
+
> **Native Indicator Lowering on Pumpkin & Z3**: When `options.M` is omitted, `m.indicator()` still returns Big-M-linearized `Constraint[]` rows (so `m.toLpString()`, HiGHS, and GLPK stay identical and backward compatible) while annotating those rows so **Pumpkin** lowers natively via `implied_by` and **Z3** lowers natively via `ctx.Implies` without Big-M numerical degradation. Mutating a returned row clears that metadata so subsequent edits take effect directly.
|
|
106
130
|
|
|
107
|
-
### Explicit Override
|
|
131
|
+
### Explicit `M` Override
|
|
108
132
|
|
|
109
|
-
You can override the derived Big-M by
|
|
133
|
+
You can override the derived Big-M by passing `{ M: number }`:
|
|
110
134
|
|
|
111
135
|
```typescript
|
|
112
136
|
m.indicator(useDiscount, expr`${cartTotal} >= 100`, {
|
|
@@ -116,15 +140,13 @@ m.indicator(useDiscount, expr`${cartTotal} >= 100`, {
|
|
|
116
140
|
```
|
|
117
141
|
|
|
118
142
|
> [!TIP]
|
|
119
|
-
> **
|
|
120
|
-
> An $M$ value that is too small can inadvertently cut off valid solutions. Conversely, an excessively large $M$ (e.g., $10^9$) causes poor matrix conditioning and numerical instability in MIP solvers.
|
|
121
|
-
> By setting explicit, realistic variable bounds (`lb` and `ub`), WaSHI derives the tightest possible safe Big-M automatically.
|
|
143
|
+
> **Prefer Finite Variable Bounds Over Arbitrary Large `M`**: An $M$ value that is too small can cut off valid feasible solutions, whereas an excessively large $M$ (e.g., $10^9$) degrades numerical conditioning in MIP solvers. Setting realistic `lb` and `ub` bounds on variables lets WaSHI compute the tightest safe Big-M automatically.
|
|
122
144
|
|
|
123
145
|
---
|
|
124
146
|
|
|
125
|
-
##
|
|
147
|
+
## Propositional Logic & SMT Constraints (`BoolVar`, `and`, `or`, `not`, `implies`, `xor`)
|
|
126
148
|
|
|
127
|
-
|
|
149
|
+
WaSHI provides first-class logical operators to combine propositional boolean variables (`m.bool()`) and linear constraints (`Constraint`). Logical formulas are solved natively by **Pumpkin** (`@ohtaman/washi/solvers/pumpkin`, for bounded integer & boolean models) and **Z3** (`@ohtaman/washi/solvers/z3`), and automatically linearized to 0-1 MIP constraints on **HiGHS** and **GLPK**:
|
|
128
150
|
|
|
129
151
|
```typescript
|
|
130
152
|
import { Model, expr, and, or, not, implies, xor } from "@ohtaman/washi";
|
|
@@ -133,14 +155,14 @@ import "@ohtaman/washi/solvers/z3";
|
|
|
133
155
|
const m = new Model({ solver: "z3" });
|
|
134
156
|
const isAdmin = m.bool("isAdmin");
|
|
135
157
|
const hasMfa = m.bool("hasMfa");
|
|
136
|
-
const
|
|
158
|
+
const clearance = m.integer("clearance", { lb: 0, ub: 5 });
|
|
137
159
|
|
|
138
160
|
// Method syntax on BoolVar / BoolOp:
|
|
139
161
|
m.constraint(isAdmin.implies(hasMfa), "admin_requires_mfa");
|
|
140
162
|
|
|
141
163
|
// Mixing boolean propositions with linear arithmetic comparisons:
|
|
142
164
|
m.constraint(
|
|
143
|
-
implies(isAdmin, expr`${
|
|
165
|
+
implies(isAdmin, expr`${clearance} >= 4`),
|
|
144
166
|
"admin_clearance_level"
|
|
145
167
|
);
|
|
146
168
|
|
|
@@ -148,30 +170,64 @@ m.constraint(
|
|
|
148
170
|
m.constraint(and(or(isAdmin, hasMfa), not(xor(isAdmin, hasMfa))), "policy");
|
|
149
171
|
```
|
|
150
172
|
|
|
173
|
+
All named constraints—both linear `Constraint`s and logical/CP `LogicalConstraint`s (`{ kind: "logical", name, expr, dual: null, slack: null }`)—are stored uniformly in `m.constraints` (`ReadonlyMap<string, Constraint | LogicalConstraint>`) and can be looked up by name via `m.constraints.get(name)`.
|
|
174
|
+
|
|
151
175
|
### Choosing Between `m.indicator()` and `implies()`
|
|
152
176
|
|
|
153
177
|
| Feature | `m.indicator(binVar, constraint, opts)` | `implies(a, b)` / `a.implies(b)` |
|
|
154
178
|
| :--- | :--- | :--- |
|
|
155
179
|
| **Trigger variable** | Numeric `Binary` variable (`m.binary()`) | `BoolVar` (`m.bool()`), `BoolOp`, or `Constraint` |
|
|
156
180
|
| **Consequent** | Linear `Constraint` (`<=`, `>=`, `==`) | `BoolVar`, `BoolOp`, or linear `Constraint` |
|
|
157
|
-
| **Target Solvers** | **All MIP/LP solvers** (HiGHS, GLPK
|
|
158
|
-
| **
|
|
181
|
+
| **Target Solvers** | **All MIP/LP solvers** (HiGHS, GLPK via Big-M; Pumpkin/Z3 natively when `M` omitted) | **All solvers** (HiGHS and GLPK via automatic 0-1 MIP linearization, Pumpkin and Z3 natively) |
|
|
182
|
+
| **Primary Use Case** | MIP optimization models with arithmetic binary variables (`m.solve()`) | Logical rule systems and SMT feasibility (`m.check()`) |
|
|
183
|
+
|
|
184
|
+
> [!NOTE]
|
|
185
|
+
> **Comparison Constraints in Negated Positions (`implies` Antecedents, `not`, `xor`)**: Linear comparison constraints whose referenced variables are all `Integer` or `Binary` with integer coefficients and integer right-hand sides (plus required finite bounds for Big-M indicators, such as ``implies(expr`${clearance} >= 4`, hasMfa)`` above) are linearized automatically on **HiGHS** and **GLPK** via exact $\pm 1$ integer shifts ($\neg(a^\top x \le b) \iff a^\top x \ge b + 1$, and $a^\top x \ne b \iff a^\top x \le b - 1 \lor a^\top x \ge b + 1$), and solved natively on **Pumpkin** and **Z3**. When a comparison constraint containing `Continuous` variables or non-integer coefficients/thresholds appears in a negated position, MIP `solve()` / `check()` throws an error because strict-inequality negation forms an open half-space—use `{ solver: "z3" }`, or write an explicit closed-tolerance disjunction such as ``or(expr`... >= ... + eps`, ...)`` or an `m.bool()` proposition.
|
|
159
186
|
|
|
160
187
|
---
|
|
161
188
|
|
|
162
|
-
##
|
|
189
|
+
## Discrete Comparisons (`!=`, `<`, `>`)
|
|
163
190
|
|
|
164
|
-
|
|
191
|
+
WaSHI supports discrete comparisons `!=`, `<`, and `>` in `expr` (and fluent `.ne()`, `.lt()`, `.gt()` on `Var`, `LinExpr`, and `QuadExpr`) strictly for **integer-valued expressions** (`Integer` or `Binary` variables with safe-integer coefficients and constants):
|
|
165
192
|
|
|
166
193
|
```typescript
|
|
167
|
-
m.
|
|
194
|
+
const rankA = m.integer("rankA", { lb: 1, ub: 4 });
|
|
195
|
+
const rankB = m.integer("rankB", { lb: 1, ub: 4 });
|
|
196
|
+
const roomA = m.integer("roomA", { lb: 1, ub: 3 });
|
|
197
|
+
const roomB = m.integer("roomB", { lb: 1, ub: 3 });
|
|
168
198
|
|
|
169
|
-
|
|
199
|
+
m.constraint(expr`${rankA} < ${rankB}`, "strict_order"); // lowered via rankA - rankB <= -1
|
|
200
|
+
m.constraint(expr`${roomA} != ${roomB}`, "diff_room"); // native not_equals on Pumpkin, neq on Z3, single-binary MIP on HiGHS/GLPK
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
Expressions involving `Continuous` variables or fractional numbers in `!=`, `<`, or `>` are rejected immediately because open inequalities ($x < b$, $x \ne b$) are not closed polyhedra on continuous domains.
|
|
204
|
+
|
|
205
|
+
---
|
|
206
|
+
|
|
207
|
+
## CP Global Constraints (`distinct`, `disjoint`, `cumulative`, `allowed`)
|
|
208
|
+
|
|
209
|
+
WaSHI provides backend-agnostic Constraint Programming predicates returning composable `BoolExpr` objects (combinable with `and`, `or`, `not`, `implies`, `xor`). For interval scheduling, symmetry breaking, and solver modeling patterns with **Pumpkin**, see the dedicated [Constraint Programming (CP) Modeling Guide](./constraint-programming.md).
|
|
210
|
+
|
|
211
|
+
| Predicate | Semantics | HiGHS / GLPK | Pumpkin | Z3 |
|
|
212
|
+
| :--- | :--- | :--- | :--- | :--- |
|
|
213
|
+
| `distinct(...args)` | Pairwise `a != b` across integer expressions / 1D–2D containers (`AllDifferent`) | Pairwise `!=` MIP | `all_different` | `ctx.Distinct` |
|
|
214
|
+
| `disjoint(tasks)` | Non-overlapping intervals `[start, start + duration)` on unary resource (`NoOverlap` / `Disjunctive`; `duration >= 0` integer; duration-0 only at boundaries) | Pairwise binary precedence + Big-M | `disjunctive_strict` (Vilím edge-finding) | Pairwise `Or` disjunctions |
|
|
215
|
+
| `cumulative(tasks, capacity)` | Resource usage $\le$ `capacity` across all time points (`duration >= 0`, `demand >= 0` integer) | Time-indexed MIP (throws horizon guard error above 5,000 span / 25,000 pulse vars recommending `{ solver: "pumpkin" }`) | Native `cumulative` time-table filtering | Start-event load assertions |
|
|
216
|
+
| `allowed(vars, tuples)` | Tuple `(vars[0], ...) in tuples` (`Table` / `AllowedAssignments`; use `not(allowed(...))` for forbidden tuples) | Tuple selector binaries (`not` emits 0-binary cuts on 2-valued vars) | `table` / `negative_table` | Tuple `Or` / `And` |
|
|
170
217
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
218
|
+
```typescript
|
|
219
|
+
import { Model, expr, distinct, disjoint, cumulative, allowed, not, minimize } from "@ohtaman/washi";
|
|
220
|
+
|
|
221
|
+
const m = new Model("ScheduleAndAssign");
|
|
222
|
+
const s = m.integer(2, { lb: 0, ub: 10, prefix: "s" });
|
|
223
|
+
const room = m.integer(2, { lb: 1, ub: 2, prefix: "room" });
|
|
224
|
+
|
|
225
|
+
m.constraint(distinct(room), "unique_rooms");
|
|
226
|
+
m.constraint(disjoint([{ start: s[0], duration: 4 }, { start: s[1], duration: 3 }]), "machine_1");
|
|
227
|
+
m.constraint(not(allowed([room[0], room[1]], [[1, 2]])), "forbid_1_2");
|
|
228
|
+
m.constraint(cumulative([{ start: s[0], duration: 4, demand: 1 }, { start: s[1], duration: 3, demand: 1 }], 2), "workers");
|
|
229
|
+
m.objective(minimize(expr`${s[0]} + ${s[1]}`));
|
|
175
230
|
```
|
|
176
231
|
|
|
177
|
-
|
|
232
|
+
> [!TIP]
|
|
233
|
+
> For modeling techniques such as **soft constraints with slack variables** and **symmetry breaking**, see [Formulation Patterns & Optimization Tips](./formulation-tips.md#_3-constraint-design-numerical-conditioning).
|