@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,162 +1,104 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Decomposition Methods (`solveColumnGeneration` & `solveBenders`)
|
|
2
2
|
|
|
3
|
-
Mixed-Integer Programming (MIP)
|
|
4
|
-
WaSHI provides decomposition orchestrators (`master.solveColumnGeneration` and `master.solveBenders`) along with lower-level modeling primitives for dual inspection and dynamic model modification.
|
|
3
|
+
When monolithic Mixed-Integer Programming (MIP) models have exponentially many candidate patterns or large multi-stage subproblems, WaSHI provides two high-level decomposition orchestrators—`master.solveColumnGeneration()` and `master.solveBenders()`—along with low-level primitives for dual inspection and dynamic model updates.
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
There are two common problem structures where monolithic MIP formulations struggle:
|
|
11
|
-
|
|
12
|
-
| Problem Structure | Monolithic MIP Bottleneck | Technique | WaSHI API |
|
|
13
|
-
| :--- | :--- | :--- | :--- |
|
|
14
|
-
| **Symmetry & Loose Continuous Bounds**<br>*(Cutting Stock, Vehicle Routing/VRP, Crew Rostering)* | Symmetric permutations and a weak continuous relaxation enlarge the branch-and-bound tree. | **Column Generation**<br>*(Dantzig-Wolfe Decomposition)* | [`master.solveColumnGeneration()`](#column-generation-solvecolumngeneration) |
|
|
15
|
-
| **Complicating Decisions & Multi-Stage Structure**<br>*(Traveling Salesperson/TSP, Facility Location with Routing, Two-Stage Stochastic Planning)* | Coupling master decisions with large scenario or operational subproblems increases the matrix size. | **Benders Decomposition** | [`master.solveBenders()`](#benders-decomposition-solvebenders) |
|
|
16
|
-
|
|
17
|
-
---
|
|
18
|
-
|
|
19
|
-
## Foundational Primitives
|
|
20
|
-
|
|
21
|
-
WaSHI provides fine-grained modeling primitives to inspect dual information and dynamically modify models:
|
|
22
|
-
|
|
23
|
-
### 1. Constraint Duals (`dual`)
|
|
24
|
-
Access the dual values (marginal impact on the objective per unit change in RHS) after an LP solve:
|
|
25
|
-
|
|
26
|
-
```typescript
|
|
27
|
-
m.constraint(expr`${x} + ${y} <= 10`, "capacity");
|
|
28
|
-
await m.solve();
|
|
29
|
-
|
|
30
|
-
const c = m.getConstraintByName("capacity")!;
|
|
31
|
-
console.log(c.dual); // Dual value / shadow price
|
|
32
|
-
console.log(m.solution?.duals.capacity);
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
### 2. Continuous LP Relaxation (`model.relax()`)
|
|
36
|
-
Decomposition algorithms require solving the continuous LP relaxation to extract dual values. `model.relax()` temporarily relaxes all `Integer` and `Binary` variables to continuous variables within their bounds:
|
|
37
|
-
|
|
38
|
-
```typescript
|
|
39
|
-
// Solves LP relaxation and populates shadow prices in m.solution.duals
|
|
40
|
-
const status = await m.relax();
|
|
41
|
-
console.log("Theoretical dual bound:", m.objectiveValue);
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
### 3. Dynamic Column Injection (`model.addColumn()`)
|
|
45
|
-
Inject a new decision variable along with its objective coefficient and constraint contributions in a single call:
|
|
46
|
-
|
|
47
|
-
```typescript
|
|
48
|
-
const col = m.addColumn({
|
|
49
|
-
name: "pattern_1",
|
|
50
|
-
type: "Integer", // or "Continuous"
|
|
51
|
-
lb: 0,
|
|
52
|
-
cost: 1, // objective coefficient
|
|
53
|
-
coefficients: {
|
|
54
|
-
demand_itemA: 2,
|
|
55
|
-
demand_itemB: 1,
|
|
56
|
-
},
|
|
57
|
-
});
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
### 4. Dynamic Constraint Modification (`addTerm` / `setTerm`)
|
|
61
|
-
Update the coefficients of individual variables in an existing constraint:
|
|
62
|
-
|
|
63
|
-
```typescript
|
|
64
|
-
const c = m.getConstraintByName("budget")!;
|
|
65
|
-
c.addTerm(newVar, 5); // Increments newVar's coefficient by 5
|
|
66
|
-
c.setTerm(newVar, 0); // Setting to 0 cleanly removes the term
|
|
67
|
-
```
|
|
5
|
+
| Decomposition Method | WaSHI Orchestrator | Typical Use Case |
|
|
6
|
+
| :--- | :--- | :--- |
|
|
7
|
+
| **Column Generation** *(Dantzig-Wolfe)* | [`master.solveColumnGeneration()`](#column-generation-solvecolumngeneration) | Models with exponentially many patterns, routes, or schedules (e.g., Cutting Stock, Vehicle Routing, Crew Rostering). |
|
|
8
|
+
| **Benders Decomposition** *(and Lazy Cuts)* | [`master.solveBenders()`](#benders-decomposition-solvebenders) | Two-stage or complicating-variable problems (e.g., Facility Location + Dispatch, Stochastic Planning, TSP Subtour Elimination). |
|
|
68
9
|
|
|
69
10
|
---
|
|
70
11
|
|
|
71
12
|
## Column Generation (`solveColumnGeneration`)
|
|
72
13
|
|
|
73
|
-
|
|
74
|
-
1.
|
|
75
|
-
2.
|
|
76
|
-
3.
|
|
77
|
-
4. Repeats until no
|
|
78
|
-
5.
|
|
14
|
+
`master.solveColumnGeneration(options)` starts from a **Restricted Master Problem (RMP)** containing an initial feasible subset of columns and executes the column generation loop:
|
|
15
|
+
1. Solves the continuous LP relaxation of the master problem via `master.relax()`.
|
|
16
|
+
2. Invokes your `pricing` callback with the current dual shadow prices (`duals`, `getDual`).
|
|
17
|
+
3. Evaluates candidate columns (`ColumnProposal` or `ColumnProposal[]`), dynamically injecting those with improving reduced costs ($\bar{c} = \text{cost} - \sum_i \pi_i a_i < 0$ when minimizing).
|
|
18
|
+
4. Repeats until `pricing` returns `null` / `[]` or no column improves the objective beyond `reducedCostTolerance`.
|
|
19
|
+
5. Optionally solves the final master model as a MIP (`finalizeAsMIP: true`, the default) to obtain an integer solution over all generated columns.
|
|
79
20
|
|
|
80
|
-
###
|
|
81
|
-
|
|
82
|
-
Provide a `pricing` callback that receives constraint shadow prices (duals), solves the subproblem (using a WaSHI model or any custom algorithm), and returns new column proposals. WaSHI automatically evaluates reduced costs and injects improving columns:
|
|
21
|
+
### Pricing with a WaSHI Subproblem Model
|
|
83
22
|
|
|
84
23
|
```typescript
|
|
85
24
|
import { Model, expr, minimize, maximize } from "@ohtaman/washi";
|
|
86
25
|
|
|
87
26
|
const rollWidth = 100;
|
|
88
|
-
const items = [
|
|
89
|
-
{ id: "A", width: 45, demand: 10 },
|
|
90
|
-
{ id: "B", width: 36, demand: 14 },
|
|
91
|
-
{ id: "C", width: 16, demand: 20 },
|
|
92
|
-
];
|
|
93
27
|
|
|
94
|
-
// 1.
|
|
28
|
+
// 1. Restricted Master Problem (RMP) with simple single-item initial patterns
|
|
95
29
|
const master = new Model("CuttingStockMaster");
|
|
96
|
-
|
|
97
|
-
const
|
|
98
|
-
const
|
|
99
|
-
const pC = master.integer("init_C", { lb: 0 }); // 6 of C per roll (16*6 = 96 <= 100)
|
|
30
|
+
const pA = master.integer("init_A", { lb: 0 }); // 2 of A (width 45) per roll
|
|
31
|
+
const pB = master.integer("init_B", { lb: 0 }); // 2 of B (width 36) per roll
|
|
32
|
+
const pC = master.integer("init_C", { lb: 0 }); // 6 of C (width 16) per roll
|
|
100
33
|
|
|
101
34
|
master.objective(minimize(expr`${pA} + ${pB} + ${pC}`));
|
|
102
|
-
master.constraint(expr`2 * ${pA} >= 10`, "demand_A");
|
|
103
|
-
master.constraint(expr`2 * ${pB} >= 14`, "demand_B");
|
|
104
|
-
master.constraint(expr`6 * ${pC} >= 20`, "demand_C");
|
|
105
|
-
const [demandA, demandB, demandC] = master.constraints;
|
|
35
|
+
const demandA = master.constraint(expr`2 * ${pA} >= 10`, "demand_A");
|
|
36
|
+
const demandB = master.constraint(expr`2 * ${pB} >= 14`, "demand_B");
|
|
37
|
+
const demandC = master.constraint(expr`6 * ${pC} >= 20`, "demand_C");
|
|
106
38
|
|
|
107
|
-
// 2.
|
|
39
|
+
// 2. Run Column Generation
|
|
108
40
|
const result = await master.solveColumnGeneration({
|
|
109
41
|
finalizeAsMIP: true,
|
|
110
42
|
pricing: async () => {
|
|
111
|
-
//
|
|
43
|
+
// Knapsack pricing subproblem: find a pattern maximizing total dual value
|
|
112
44
|
const sub = new Model("KnapsackPricing");
|
|
113
|
-
const
|
|
114
|
-
const
|
|
115
|
-
const
|
|
45
|
+
const aA = sub.integer("a_A", { lb: 0, ub: 2 });
|
|
46
|
+
const aB = sub.integer("a_B", { lb: 0, ub: 2 });
|
|
47
|
+
const aC = sub.integer("a_C", { lb: 0, ub: 6 });
|
|
116
48
|
|
|
117
|
-
// Maximize shadow price collection
|
|
118
49
|
const dualA = demandA.dual ?? 0;
|
|
119
50
|
const dualB = demandB.dual ?? 0;
|
|
120
51
|
const dualC = demandC.dual ?? 0;
|
|
121
|
-
sub.objective(maximize(expr`${dualA} * ${
|
|
122
|
-
sub.constraint(expr`45 * ${
|
|
52
|
+
sub.objective(maximize(expr`${dualA} * ${aA} + ${dualB} * ${aB} + ${dualC} * ${aC}`));
|
|
53
|
+
sub.constraint(expr`45 * ${aA} + 36 * ${aB} + 16 * ${aC} <= ${rollWidth}`);
|
|
123
54
|
|
|
124
55
|
await sub.solve();
|
|
125
56
|
|
|
126
|
-
// Return the generated cutting pattern
|
|
127
57
|
return {
|
|
128
58
|
cost: 1,
|
|
129
59
|
type: "Integer",
|
|
130
60
|
coefficients: {
|
|
131
|
-
demand_A: Math.round(
|
|
132
|
-
demand_B: Math.round(
|
|
133
|
-
demand_C: Math.round(
|
|
61
|
+
demand_A: Math.round(aA.value ?? 0),
|
|
62
|
+
demand_B: Math.round(aB.value ?? 0),
|
|
63
|
+
demand_C: Math.round(aC.value ?? 0),
|
|
134
64
|
},
|
|
135
65
|
};
|
|
136
66
|
},
|
|
137
67
|
});
|
|
138
68
|
|
|
139
|
-
console.log("Optimal rolls
|
|
140
|
-
console.log("Iterations
|
|
141
|
-
console.log("
|
|
69
|
+
console.log("Optimal rolls:", result.objectiveValue); // 14
|
|
70
|
+
console.log("Iterations:", result.iterations);
|
|
71
|
+
console.log("Columns added:", result.columnsAdded);
|
|
142
72
|
```
|
|
143
73
|
|
|
144
|
-
###
|
|
74
|
+
### Pricing with a Custom Algorithm (`getDual`)
|
|
145
75
|
|
|
146
|
-
Because `pricing` is a standard callback
|
|
76
|
+
Because `pricing` is a standard callback receiving `PricingContext`, you can also solve the pricing problem directly in TypeScript (e.g., via combinatorial search, dynamic programming, or shortest-path routines) instead of constructing a subproblem `Model`:
|
|
147
77
|
|
|
148
78
|
```typescript
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
};
|
|
79
|
+
const customResult = await master.solveColumnGeneration({
|
|
80
|
+
maxIterations: 100,
|
|
81
|
+
reducedCostTolerance: 1e-6,
|
|
82
|
+
pricing: ({ getDual }) => {
|
|
83
|
+
const dA = getDual("demand_A");
|
|
84
|
+
const dB = getDual("demand_B");
|
|
85
|
+
const dC = getDual("demand_C");
|
|
86
|
+
|
|
87
|
+
// Search feasible patterns (45*aA + 36*aB + 16*aC <= rollWidth) for negative reduced cost (1 - dualSum < -1e-6)
|
|
88
|
+
let bestDualSum = 1 + 1e-6;
|
|
89
|
+
let best: { demand_A: number; demand_B: number; demand_C: number } | null = null;
|
|
90
|
+
for (let aA = 0; 45 * aA <= rollWidth; aA++) {
|
|
91
|
+
for (let aB = 0; 45 * aA + 36 * aB <= rollWidth; aB++) {
|
|
92
|
+
for (let aC = 0; 45 * aA + 36 * aB + 16 * aC <= rollWidth; aC++) {
|
|
93
|
+
const dualSum = dA * aA + dB * aB + dC * aC;
|
|
94
|
+
if (dualSum > bestDualSum) {
|
|
95
|
+
bestDualSum = dualSum;
|
|
96
|
+
best = { demand_A: aA, demand_B: aB, demand_C: aC };
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return best ? { cost: 1, type: "Integer", coefficients: best } : null;
|
|
160
102
|
},
|
|
161
103
|
});
|
|
162
104
|
```
|
|
@@ -165,33 +107,29 @@ const result = await master.solveColumnGeneration({
|
|
|
165
107
|
|
|
166
108
|
## Benders Decomposition (`solveBenders`)
|
|
167
109
|
|
|
168
|
-
|
|
169
|
-
- **Master Problem**: Contains complicating variables (e.g., 0-1 facility opening decisions $y$) and an auxiliary variable $\theta \ge 0$ representing estimated subproblem costs.
|
|
170
|
-
- **Subproblem**: Solves operational decisions given fixed master values $y^*$, feeding back **Optimality Cuts** to the master.
|
|
110
|
+
`master.solveBenders(options)` partitions a problem into a **Master Problem** (holding first-stage or complicating variables) and a **Subproblem** evaluated at each master candidate solution to generate **Optimality Cuts** or **Feasibility Cuts**.
|
|
171
111
|
|
|
172
|
-
###
|
|
112
|
+
### Automatic Cut Generation (`sub.link()`)
|
|
173
113
|
|
|
174
|
-
Return
|
|
114
|
+
Return a continuous LP subproblem `Model` from `subproblem` and declare cross-model constraints via `sub.link(...)`. WaSHI automatically manages the auxiliary cost estimator variable $\theta$, substitutes master candidate values, extracts subproblem duals, and adds Benders cuts until convergence:
|
|
175
115
|
|
|
176
116
|
```typescript
|
|
177
117
|
import { Model, expr, minimize } from "@ohtaman/washi";
|
|
178
118
|
|
|
179
|
-
// 1. Master problem:
|
|
119
|
+
// 1. Master problem: binary facility opening decisions
|
|
180
120
|
const master = new Model("FacilityMaster");
|
|
181
121
|
const y1 = master.binary("open_F1");
|
|
182
122
|
const y2 = master.binary("open_F2");
|
|
183
123
|
|
|
184
|
-
//
|
|
124
|
+
// Fixed opening costs (theta for operational subproblem cost is managed automatically)
|
|
185
125
|
master.objective(minimize(expr`100 * ${y1} + 120 * ${y2}`));
|
|
186
|
-
|
|
187
|
-
master.constraint(expr`50 * ${y1} + 60 * ${y2} >= 75`, "capacity_check");
|
|
126
|
+
master.constraint(expr`50 * ${y1} + 60 * ${y2} >= 75`, "total_capacity_floor");
|
|
188
127
|
|
|
189
|
-
// 2. Solve
|
|
128
|
+
// 2. Solve with automatic Benders cut generation
|
|
190
129
|
const result = await master.solveBenders({
|
|
191
130
|
gapTolerance: 1e-4,
|
|
192
131
|
subproblem: () => {
|
|
193
|
-
|
|
194
|
-
const sub = new Model("TransportSubproblem");
|
|
132
|
+
const sub = new Model("DispatchSubproblem");
|
|
195
133
|
const x11 = sub.continuous("x_11", { lb: 0 });
|
|
196
134
|
const x12 = sub.continuous("x_12", { lb: 0 });
|
|
197
135
|
const x13 = sub.continuous("x_13", { lb: 0 });
|
|
@@ -205,195 +143,125 @@ const result = await master.solveBenders({
|
|
|
205
143
|
)
|
|
206
144
|
);
|
|
207
145
|
|
|
208
|
-
// Customer demand constraints
|
|
209
146
|
sub.constraint(expr`${x11} + ${x21} >= 20`, "demand_C1");
|
|
210
147
|
sub.constraint(expr`${x12} + ${x22} >= 25`, "demand_C2");
|
|
211
148
|
sub.constraint(expr`${x13} + ${x23} >= 30`, "demand_C3");
|
|
212
149
|
|
|
213
|
-
// Link subproblem flows
|
|
150
|
+
// Link subproblem dispatch flows to master binary variables y1, y2
|
|
214
151
|
sub.link(expr`${x11} + ${x12} + ${x13} <= 50 * ${y1}`);
|
|
215
152
|
sub.link(expr`${x21} + ${x22} + ${x23} <= 60 * ${y2}`);
|
|
216
153
|
|
|
217
|
-
return sub;
|
|
154
|
+
return sub;
|
|
218
155
|
},
|
|
219
156
|
});
|
|
220
157
|
|
|
221
158
|
console.log("Optimal total cost:", result.objectiveValue); // 390
|
|
222
|
-
console.log("Iterations:", result.iterations);
|
|
223
159
|
console.log("Cuts added:", result.cutsAdded);
|
|
224
|
-
console.log("
|
|
225
|
-
F1: result.values.open_F1,
|
|
226
|
-
F2: result.values.open_F2,
|
|
227
|
-
});
|
|
160
|
+
console.log("Open facilities:", result.values);
|
|
228
161
|
```
|
|
229
162
|
|
|
230
|
-
|
|
163
|
+
> [!NOTE]
|
|
164
|
+
> **`theta` Lower Bound for Negative Subproblem Objectives**: When `sub.link(...)` is used without an explicit `theta` variable, WaSHI injects `__benders_theta` with default `lb: 0` (assuming non-negative subproblem costs). If your subproblem objective can take negative values (e.g., rebate or profit terms), declare `const theta = master.continuous("theta", { lb: -1e6 })`, include `+ ${theta}` in `master.objective(...)`, and pass `{ theta }` in `solveBenders(options)`.
|
|
231
165
|
|
|
232
|
-
|
|
166
|
+
### Manual Cut Generation (`BendersCut` / Lazy Constraints)
|
|
167
|
+
|
|
168
|
+
For combinatorial separation (such as lazy subtour elimination) or custom analytical cuts, return one `BendersCut` object, an array `BendersCut[]`, or `null` when converged:
|
|
233
169
|
|
|
234
170
|
```typescript
|
|
171
|
+
import { Model, expr, minimize } from "@ohtaman/washi";
|
|
172
|
+
|
|
173
|
+
const master = new Model("ManualBenders");
|
|
174
|
+
const y = master.binary("y");
|
|
175
|
+
const theta = master.continuous("theta", { lb: 0 });
|
|
176
|
+
master.objective(minimize(expr`40 * ${y} + ${theta}`));
|
|
177
|
+
|
|
235
178
|
const result = await master.solveBenders({
|
|
179
|
+
theta,
|
|
236
180
|
gapTolerance: 1e-4,
|
|
237
|
-
subproblem:
|
|
238
|
-
const
|
|
239
|
-
const
|
|
240
|
-
const estTheta = theta.value ?? 0;
|
|
241
|
-
|
|
242
|
-
const sub = new Model("TransportSubproblem");
|
|
243
|
-
// ...
|
|
244
|
-
await sub.solve();
|
|
181
|
+
subproblem: ({ getMasterValue }) => {
|
|
182
|
+
const yVal = getMasterValue(y);
|
|
183
|
+
const estTheta = getMasterValue(theta);
|
|
245
184
|
|
|
246
|
-
|
|
247
|
-
|
|
185
|
+
// Operational cost is 100 when y = 0, or 30 when y = 1
|
|
186
|
+
const actualSubCost = 100 - 70 * yVal;
|
|
187
|
+
if (estTheta < actualSubCost - 1e-4) {
|
|
248
188
|
return {
|
|
249
|
-
kind: "optimality",
|
|
250
|
-
constraint: expr`${theta}
|
|
251
|
-
subproblemObjective:
|
|
189
|
+
kind: "optimality", // or "feasibility" for lazy combinatorial cuts
|
|
190
|
+
constraint: expr`${theta} >= 100 - 70 * ${y}`,
|
|
191
|
+
subproblemObjective: 40 * yVal + actualSubCost,
|
|
252
192
|
};
|
|
253
193
|
}
|
|
254
194
|
return null; // Converged
|
|
255
195
|
},
|
|
256
196
|
});
|
|
197
|
+
|
|
198
|
+
console.log(result.objectiveValue); // 70 (y = 1, theta = 30)
|
|
257
199
|
```
|
|
258
200
|
|
|
259
201
|
---
|
|
260
202
|
|
|
261
|
-
##
|
|
262
|
-
|
|
263
|
-
Two common applications of decomposition methods:
|
|
203
|
+
## Low-Level Primitives (Custom Loops)
|
|
264
204
|
|
|
265
|
-
|
|
266
|
-
*(Full executable source: [`examples/node/13-vehicle-routing-column-generation.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/13-vehicle-routing-column-generation.ts))*
|
|
205
|
+
The two orchestrators above are built on four lower-level primitives that you can also call directly when writing your own cutting-plane or column-generation loops:
|
|
267
206
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
* **Master Problem**: Set covering model that selects the minimum number of vehicle routes such that each customer is served.
|
|
271
|
-
* **Pricing Subproblem**: Knapsack model that dynamically generates a capacity-feasible route (column) with negative reduced cost based on customer dual shadow prices.
|
|
207
|
+
### 1. Constraint Duals (`c.dual`)
|
|
208
|
+
After solving a continuous linear program (LP), read the shadow price (marginal impact on the objective per unit change in RHS) of each constraint:
|
|
272
209
|
|
|
273
210
|
```typescript
|
|
274
|
-
import { Model, expr,
|
|
275
|
-
|
|
276
|
-
const vehicleCapacity = 8;
|
|
277
|
-
const customers = [
|
|
278
|
-
{ id: "C1", demand: 4 },
|
|
279
|
-
{ id: "C2", demand: 3 },
|
|
280
|
-
{ id: "C3", demand: 5 },
|
|
281
|
-
{ id: "C4", demand: 2 },
|
|
282
|
-
{ id: "C5", demand: 4 },
|
|
283
|
-
];
|
|
284
|
-
|
|
285
|
-
// 1. Master Problem: Customer coverage
|
|
286
|
-
const master = new Model("CVRP_Master");
|
|
287
|
-
const initialRoutes: Var[] = [];
|
|
288
|
-
for (const cust of customers) {
|
|
289
|
-
initialRoutes.push(master.integer(`init_${cust.id}`, { lb: 0, ub: 1 }));
|
|
290
|
-
}
|
|
291
|
-
master.objective(minimize(initialRoutes.reduce((acc, r) => acc.add(r), new LinExpr())));
|
|
292
|
-
|
|
293
|
-
for (let i = 0; i < customers.length; i++) {
|
|
294
|
-
master.constraint(expr`${initialRoutes[i]} >= 1`, `visit_${customers[i].id}`);
|
|
295
|
-
}
|
|
296
|
-
const visitConstrs = master.constraints;
|
|
297
|
-
|
|
298
|
-
// 2. Solve via Column Generation
|
|
299
|
-
const result = await master.solveColumnGeneration({
|
|
300
|
-
finalizeAsMIP: true,
|
|
301
|
-
pricing: async () => {
|
|
302
|
-
const sub = new Model("RoutePricing");
|
|
303
|
-
const a: Record<string, Var> = {};
|
|
304
|
-
const obj = new LinExpr();
|
|
305
|
-
|
|
306
|
-
for (let i = 0; i < customers.length; i++) {
|
|
307
|
-
const cust = customers[i];
|
|
308
|
-
a[cust.id] = sub.binary(`v_${cust.id}`);
|
|
309
|
-
obj.addTerm(a[cust.id], visitConstrs[i].dual ?? 0);
|
|
310
|
-
}
|
|
211
|
+
import { Model, expr, maximize } from "@ohtaman/washi";
|
|
311
212
|
|
|
312
|
-
|
|
213
|
+
const m = new Model();
|
|
214
|
+
const x = m.continuous("x", { lb: 0 });
|
|
215
|
+
const y = m.continuous("y", { lb: 0 });
|
|
313
216
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
217
|
+
m.objective(maximize(expr`3 * ${x} + 2 * ${y}`));
|
|
218
|
+
const c = m.constraint(expr`${x} + ${y} <= 10`, "capacity");
|
|
219
|
+
await m.solve();
|
|
317
220
|
|
|
318
|
-
|
|
221
|
+
console.log(c.dual); // Dual value / shadow price
|
|
222
|
+
console.log(m.solution?.duals.capacity); // Lookup by constraint name
|
|
223
|
+
```
|
|
319
224
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
coefficients[`visit_${cust.id}`] = Math.round(a[cust.id].value ?? 0);
|
|
323
|
-
}
|
|
225
|
+
### 2. Continuous LP Relaxation (`m.relax()`)
|
|
226
|
+
`m.relax()` temporarily relaxes all `Integer` and `Binary` variables to continuous variables within their `[lb, ub]` bounds and solves the LP relaxation so constraint duals are populated:
|
|
324
227
|
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
228
|
+
```typescript
|
|
229
|
+
// Solves the continuous LP relaxation and populates m.solution.duals
|
|
230
|
+
const status = await m.relax();
|
|
231
|
+
console.log("LP relaxation bound:", m.objectiveValue);
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
### 3. Dynamic Column Injection (`m.column()`)
|
|
235
|
+
Create a new decision variable, set its objective coefficient, and attach its non-zero coefficients to existing named constraints in a single call:
|
|
236
|
+
|
|
237
|
+
```typescript
|
|
238
|
+
const col = m.column({
|
|
239
|
+
name: "pattern_1",
|
|
240
|
+
type: "Integer", // or "Continuous" / "Binary"
|
|
241
|
+
lb: 0,
|
|
242
|
+
cost: 1, // Objective coefficient
|
|
243
|
+
coefficients: {
|
|
244
|
+
capacity: 2,
|
|
330
245
|
},
|
|
331
246
|
});
|
|
332
|
-
|
|
333
|
-
console.log("Optimal fleet size:", result.objectiveValue); // 3 vehicles
|
|
334
247
|
```
|
|
335
248
|
|
|
336
|
-
###
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
* **Problem Structure**: Preventing isolated sub-loops (subtours) requires Subtour Elimination Constraints (SECs), which grow as $O(2^n)$ constraints for $n$ cities.
|
|
340
|
-
* **Benders Decomposition Formulation**:
|
|
341
|
-
* **Master Problem**: Solves the relaxed assignment problem (each city has 1 incoming and 1 outgoing edge).
|
|
342
|
-
* **Subproblem**: Evaluates the candidate tour graph for disconnected components. If isolated subtours exist, it lazily injects a Benders feasibility cut ($\sum_{i,j \in S} x_{ij} \le |S| - 1$) cutting off that cycle.
|
|
249
|
+
### 4. Dynamic Constraint Term Updates (`c.addTerm()` / `c.setTerm()`)
|
|
250
|
+
Increment or overwrite the coefficient of a variable inside an existing linear constraint:
|
|
343
251
|
|
|
344
252
|
```typescript
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
const n = cities.length;
|
|
348
|
-
const master = new Model("TSP_Master");
|
|
349
|
-
const x: Record<string, Var> = {};
|
|
350
|
-
const objExpr = new LinExpr();
|
|
351
|
-
|
|
352
|
-
for (let i = 0; i < n; i++) {
|
|
353
|
-
for (let j = 0; j < n; j++) {
|
|
354
|
-
if (i !== j) {
|
|
355
|
-
const v = master.binary(`x_${i}_${j}`);
|
|
356
|
-
x[`${i}_${j}`] = v;
|
|
357
|
-
objExpr.addTerm(v, dist(i, j));
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
master.objective(minimize(objExpr));
|
|
362
|
-
|
|
363
|
-
// Degree constraints
|
|
364
|
-
for (let i = 0; i < n; i++) {
|
|
365
|
-
const outE = new LinExpr();
|
|
366
|
-
const inE = new LinExpr();
|
|
367
|
-
for (let j = 0; j < n; j++) {
|
|
368
|
-
if (i !== j) {
|
|
369
|
-
outE.addTerm(x[`${i}_${j}`], 1);
|
|
370
|
-
inE.addTerm(x[`${j}_${i}`], 1);
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
master.constraint(expr`${outE} == 1`, `out_${i}`);
|
|
374
|
-
master.constraint(expr`${inE} == 1`, `in_${i}`);
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
// Solve with Benders lazy subtour elimination
|
|
378
|
-
const result = await master.solveBenders({
|
|
379
|
-
subproblem: () => {
|
|
380
|
-
const cycles = findCycles(n, (i, j) => (x[`${i}_${j}`].value ?? 0) > 0.5);
|
|
381
|
-
if (cycles.length === 1 && cycles[0].length === n) {
|
|
382
|
-
return null; // Single tour covers all cities
|
|
383
|
-
}
|
|
253
|
+
const extra = m.continuous("extra", { lb: 0 });
|
|
254
|
+
const budget = m.constraint(expr`${x} + ${y} <= 20`, "budget");
|
|
384
255
|
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
const secExpr = new LinExpr();
|
|
388
|
-
for (const i of S) {
|
|
389
|
-
for (const j of S) {
|
|
390
|
-
if (i !== j) secExpr.addTerm(x[`${i}_${j}`], 1);
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
return {
|
|
394
|
-
kind: "feasibility",
|
|
395
|
-
constraint: expr`${secExpr} <= ${S.length - 1}`,
|
|
396
|
-
};
|
|
397
|
-
},
|
|
398
|
-
});
|
|
256
|
+
budget.addTerm(extra, 5); // Adds +5 * extra to the LHS
|
|
257
|
+
budget.setTerm(extra, 0); // Setting 0 removes extra from the constraint
|
|
399
258
|
```
|
|
259
|
+
|
|
260
|
+
---
|
|
261
|
+
|
|
262
|
+
## Related Examples & Formulation Tips
|
|
263
|
+
|
|
264
|
+
- **Full Decomposition Walkthroughs**:
|
|
265
|
+
- [Vehicle Routing Problem (`solveColumnGeneration`)](../examples/vehicle-routing.md)
|
|
266
|
+
- [Traveling Salesperson Problem (`solveBenders` lazy subtour elimination)](../examples/traveling-salesperson.md)
|
|
267
|
+
- **Formulation Tips**: See [Formulation Patterns & Optimization Tips](./formulation-tips.md#_5-decomposition-formulation-tips) for Set Covering (`>= 1`) vs. Set Partitioning (`== 1`), Price-and-Branch (`finalizeAsMIP`), and multi-cut separation.
|