@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
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# WaSHI Custom Solver Developer Guide
|
|
2
2
|
|
|
3
|
-
This guide
|
|
3
|
+
This guide explains how to build, test, and distribute custom solver plugins for **WaSHI** (`@ohtaman/washi`).
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Core `@ohtaman/washi` bundles and auto-registers **HiGHS** (default, `@ohtaman/washi/solvers/highs`), **Pumpkin** (`@ohtaman/washi/solvers/pumpkin`), and `MockSolver`, while optional plugins (`@ohtaman/washi/solvers/glpk`, `@ohtaman/washi/solvers/z3`) register when imported. You can register any other backend—WebAssembly, native C++, Web Worker, or cloud REST/RPC—and use it through the same `Model` API.
|
|
6
6
|
|
|
7
7
|
---
|
|
8
8
|
|
|
@@ -11,24 +11,25 @@ WaSHI features a pluggable, decoupled solver architecture. Core `@ohtaman/washi`
|
|
|
11
11
|
```mermaid
|
|
12
12
|
graph TD
|
|
13
13
|
A[WaSHI Model] --> B[Solver Registry Map Symbol.for]
|
|
14
|
-
B --> C[HiGHS WASM
|
|
14
|
+
B --> C[HiGHS WASM Bundled Default @ohtaman/washi/solvers/highs]
|
|
15
|
+
B --> H[Pumpkin WASM Bundled @ohtaman/washi/solvers/pumpkin]
|
|
15
16
|
B --> D[Mock Solver Core]
|
|
16
|
-
B --> H[Pumpkin WASM Plugin @ohtaman/washi/solvers/pumpkin]
|
|
17
17
|
B --> E[GLPK Plugin @ohtaman/washi/solvers/glpk]
|
|
18
18
|
B --> F[Z3 SMT Plugin @ohtaman/washi/solvers/z3]
|
|
19
19
|
B --> G[Your Custom Solver Plugin]
|
|
20
20
|
```
|
|
21
21
|
|
|
22
22
|
### Key Architectural Characteristics
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
|
|
24
|
+
- **Unified global registry**: Stored under `Symbol.for("washi.solverRegistry")` on `globalThis`, so separate ESM chunks, dynamic imports, and `<script>` tags share one solver map.
|
|
25
|
+
- **Cross-runtime symmetry**: Node.js and browser builds share identical solver naming, capability checks, and error messages.
|
|
26
|
+
- **Decoupled bundling**: Core WaSHI never imports optional or custom plugins; each plugin registers itself only when imported.
|
|
26
27
|
|
|
27
28
|
---
|
|
28
29
|
|
|
29
30
|
## 2. API Contracts & TypeScript Interfaces
|
|
30
31
|
|
|
31
|
-
|
|
32
|
+
Every custom solver implements the `Solver` interface exported from `@ohtaman/washi`:
|
|
32
33
|
|
|
33
34
|
```typescript
|
|
34
35
|
import {
|
|
@@ -57,20 +58,20 @@ import {
|
|
|
57
58
|
|
|
58
59
|
```typescript
|
|
59
60
|
export interface Solver {
|
|
60
|
-
/**
|
|
61
|
+
/** Identifier name of the solver engine (e.g. "GLPK", "AcmeCloudSolver") */
|
|
61
62
|
readonly name: string;
|
|
62
63
|
|
|
63
|
-
/**
|
|
64
|
+
/** Version string of the solver (e.g. "1.2.0") */
|
|
64
65
|
readonly version?: string;
|
|
65
66
|
|
|
66
|
-
/**
|
|
67
|
+
/** Feature capabilities of this solver */
|
|
67
68
|
readonly capabilities?: SolverCapabilities;
|
|
68
69
|
|
|
70
|
+
/** Default options configured on this solver instance */
|
|
71
|
+
readonly options?: SolverOptions;
|
|
72
|
+
|
|
69
73
|
/**
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
* @param modelData Typed in-memory model structure (variables, constraints, objective, sense, boolVars, logicalConstraints)
|
|
73
|
-
* @param options Runtime execution options (timeLimit, relativeGap, absoluteGap, verbose, signal, onProgress, etc.)
|
|
74
|
+
* Solve a mathematical programming model (LP, MIP, QP, CP, or SMT).
|
|
74
75
|
*/
|
|
75
76
|
solve(
|
|
76
77
|
modelData: SolveModelData,
|
|
@@ -78,10 +79,7 @@ export interface Solver {
|
|
|
78
79
|
): Promise<SolverResult>;
|
|
79
80
|
|
|
80
81
|
/**
|
|
81
|
-
* Optional
|
|
82
|
-
*
|
|
83
|
-
* @param modelData Objective-free in-memory model structure (variables, boolVars, constraints, logicalConstraints)
|
|
84
|
-
* @param options Runtime execution options
|
|
82
|
+
* Optional objective-free satisfiability / feasibility check (SAT / SMT / CP).
|
|
85
83
|
*/
|
|
86
84
|
check?(
|
|
87
85
|
modelData: CheckModelData,
|
|
@@ -92,176 +90,189 @@ export interface Solver {
|
|
|
92
90
|
|
|
93
91
|
### `CheckModelData` & `SolveModelData`
|
|
94
92
|
|
|
95
|
-
`Model.solve()` and `Model.check()` pass
|
|
93
|
+
`Model.solve()` and `Model.check()` pass structured model snapshots directly to your adapter without serializing a CPLEX LP format string upfront:
|
|
96
94
|
|
|
97
95
|
```typescript
|
|
98
96
|
export interface CheckModelData {
|
|
99
|
-
/** Model name */
|
|
100
97
|
name?: string;
|
|
101
|
-
/** Decision variables registered in the model */
|
|
102
98
|
variables: Var[];
|
|
103
|
-
/** Propositional Boolean variables registered in the model */
|
|
104
99
|
boolVars?: BoolVar[];
|
|
105
|
-
/** Linear arithmetic constraints registered in the model */
|
|
106
100
|
constraints: Constraint[];
|
|
107
|
-
/** Logical / SMT constraints registered in the model */
|
|
108
101
|
logicalConstraints?: LogicalConstraintItem[];
|
|
109
102
|
}
|
|
110
103
|
|
|
111
104
|
export interface SolveModelData extends CheckModelData {
|
|
112
|
-
/** Optimization direction ("Minimize" | "Maximize") */
|
|
113
105
|
sense: OptimizationSense;
|
|
114
|
-
/** Linear or quadratic objective expression */
|
|
115
106
|
objective: LinExpr | QuadExpr;
|
|
116
107
|
}
|
|
117
108
|
```
|
|
118
109
|
|
|
119
110
|
### `SolverCapabilities`
|
|
120
111
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
-
|
|
124
|
-
-
|
|
112
|
+
Declare supported features on `capabilities`. Before calling `solve()` or `check()`, `Model` checks these flags:
|
|
113
|
+
|
|
114
|
+
- **Quadratic objectives (`QuadExpr`)**: Rejected when the model has a quadratic objective and `supportsQP === false`.
|
|
115
|
+
- **Warm starts (`initialValue`)**: Rejected when any variable sets `initialValue` and `supportsWarmStart !== true`.
|
|
116
|
+
- **Logical constraints**: Passed natively in `modelData.logicalConstraints` when `supportsLogicalConstraints === true` or `supportsSMT === true`.
|
|
117
|
+
- **Automatic MIP linearization on `solve()`**: When neither logical flag is `true`, `Model` linearizes `logicalConstraints`, `boolVars`, and nonlinear/CP expressions into equivalent binary variables and linear constraints before calling `solve()`.
|
|
118
|
+
- **`check()` fallback**: When a solver omits `check()`, `Model.check()` falls back to solving an elastic-slack feasibility problem via `solve()` (returning violated constraint names in `conflicts`, with `unsatCore: undefined`).
|
|
125
119
|
|
|
126
120
|
```typescript
|
|
127
121
|
export interface SolverCapabilities {
|
|
128
|
-
/** Supports
|
|
122
|
+
/** Supports Mixed-Integer Programming (integer and binary variables) */
|
|
129
123
|
supportsMIP?: boolean;
|
|
130
124
|
|
|
131
|
-
/** Supports
|
|
125
|
+
/** Supports Quadratic Programming objectives (QP) */
|
|
132
126
|
supportsQP?: boolean;
|
|
133
127
|
|
|
134
|
-
/** Supports
|
|
128
|
+
/** Supports quadratic constraints (QCQP / SOCP) */
|
|
135
129
|
supportsQCQP?: boolean;
|
|
136
130
|
|
|
137
|
-
/** Supports
|
|
131
|
+
/** Supports Satisfiability Modulo Theories (SMT) and logical constraints */
|
|
138
132
|
supportsSMT?: boolean;
|
|
139
133
|
|
|
140
|
-
/** Supports native boolean/propositional logical constraints
|
|
134
|
+
/** Supports native boolean/propositional logical constraints without SMT continuous theories */
|
|
141
135
|
supportsLogicalConstraints?: boolean;
|
|
142
136
|
|
|
143
|
-
/** Supports extracting an unsatisfiable core (conflicting constraint subset) */
|
|
137
|
+
/** Supports extracting an unsatisfiable core (conflicting constraint subset; minimality is not guaranteed) */
|
|
144
138
|
supportsUnsatCore?: boolean;
|
|
145
139
|
|
|
146
|
-
/** Supports primal initial values /
|
|
140
|
+
/** Supports primal initial values / warm-start hints (Var.initialValue) */
|
|
147
141
|
supportsWarmStart?: boolean;
|
|
148
142
|
}
|
|
149
143
|
```
|
|
150
144
|
|
|
151
145
|
### `SolverOptions` & `SolveProgressEvent`
|
|
152
146
|
|
|
153
|
-
Runtime options passed to `solve` and `check`
|
|
147
|
+
Runtime options passed to `solve()` and `check()` follow `SolverOptions`. Note that `Model` strips the routing `solver` field before forwarding options to your adapter.
|
|
154
148
|
|
|
155
149
|
```typescript
|
|
156
150
|
export interface SolverOptions {
|
|
157
|
-
/**
|
|
151
|
+
/** Maximum solve time limit in seconds */
|
|
158
152
|
timeLimit?: number;
|
|
159
153
|
|
|
160
|
-
/**
|
|
154
|
+
/** Presolve mode */
|
|
155
|
+
presolve?: "on" | "off" | "choose";
|
|
156
|
+
|
|
157
|
+
/** Relative MIP gap tolerance (e.g. 0.01 for 1%) */
|
|
161
158
|
relativeGap?: number;
|
|
162
159
|
|
|
163
|
-
/** Absolute MIP
|
|
160
|
+
/** Absolute MIP gap tolerance */
|
|
164
161
|
absoluteGap?: number;
|
|
165
162
|
|
|
166
|
-
/**
|
|
163
|
+
/** Number of parallel threads */
|
|
164
|
+
threads?: number;
|
|
165
|
+
|
|
166
|
+
/** Enable verbose solver logging */
|
|
167
167
|
verbose?: boolean;
|
|
168
168
|
|
|
169
|
-
/**
|
|
169
|
+
/** Solve the continuous LP relaxation by relaxing integer and binary variables */
|
|
170
|
+
relax?: boolean;
|
|
171
|
+
|
|
172
|
+
/** Use Web Worker for non-blocking browser execution */
|
|
173
|
+
useWorker?: boolean;
|
|
174
|
+
|
|
175
|
+
/** AbortSignal to cancel an in-flight solve */
|
|
170
176
|
signal?: AbortSignal;
|
|
171
177
|
|
|
172
|
-
/**
|
|
178
|
+
/** Callback invoked with normalized SolveProgressEvent metrics */
|
|
173
179
|
onProgress?: (progress: SolveProgressEvent) => void;
|
|
174
180
|
|
|
175
181
|
/** Additional solver-specific options */
|
|
176
182
|
[key: string]: any;
|
|
177
183
|
}
|
|
184
|
+
|
|
185
|
+
export interface SolveProgressEvent {
|
|
186
|
+
/** Elapsed solver time in seconds */
|
|
187
|
+
elapsedTime: number;
|
|
188
|
+
/** Alias for elapsedTime: elapsed solver time in seconds */
|
|
189
|
+
elapsedSeconds?: number;
|
|
190
|
+
/** Current solver phase when known */
|
|
191
|
+
phase?: "simplex" | "ipm" | "mip";
|
|
192
|
+
/** Best feasible incumbent objective value when available */
|
|
193
|
+
objectiveValue?: number;
|
|
194
|
+
/** Best proven dual / objective bound when available */
|
|
195
|
+
objectiveBound?: number;
|
|
196
|
+
/** Relative MIP optimality gap when available */
|
|
197
|
+
relativeGap?: number;
|
|
198
|
+
/** Absolute MIP optimality gap |objectiveValue - objectiveBound| when available */
|
|
199
|
+
absoluteGap?: number;
|
|
200
|
+
/** Explored MIP branch-and-bound node count when available */
|
|
201
|
+
nodes?: number;
|
|
202
|
+
/** Solver iteration count (simplex, IPM, or MIP LP iterations) when available */
|
|
203
|
+
iterations?: number;
|
|
204
|
+
/** Optional raw progress message from the solver */
|
|
205
|
+
message?: string;
|
|
206
|
+
}
|
|
178
207
|
```
|
|
179
208
|
|
|
180
|
-
###
|
|
209
|
+
### Direct Model Data vs. LP Helpers
|
|
210
|
+
|
|
211
|
+
Choose how your adapter reads `modelData` based on your backend's input format:
|
|
181
212
|
|
|
182
|
-
|
|
183
|
-
|
|
213
|
+
- **Use `SolveModelData` / `CheckModelData` directly** when your backend accepts structured JS or WebAssembly objects (as `PumpkinSolver`, `GLPKSolver`, and `Z3Solver` do).
|
|
214
|
+
- **Use WaSHI's opt-in LP helpers** when your backend expects CPLEX/HiGHS LP text or restricted ASCII identifiers:
|
|
184
215
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
216
|
+
| Helper | Purpose |
|
|
217
|
+
| :--- | :--- |
|
|
218
|
+
| `generateLpString(modelData, { relax }?)` | Serializes `modelData` into a CPLEX LP string (`lpString`) with collision-safe `nameToVar` and `nameToConstraint` maps; pass `{ relax: true }` for a continuous LP relaxation. Omits `modelData.objective.constant`, so adapters must add it back to `objectiveValue` and `objectiveBound`. |
|
|
219
|
+
| `buildLpSymbolMaps(modelData)` | Builds collision-safe sanitized symbol maps (`nameToVar`, `varToName`, `nameToConstraint`, `constraintToName`) without serializing an LP string. |
|
|
220
|
+
| `isLpExportResult(value)` | Runtime type guard for `LpExportResult` (useful for legacy 3-argument `solve(modelData, lpResult, options)` overloads). |
|
|
221
|
+
| `sanitizeIdentifier(raw, fallbackPrefix = "x", id = 0)` | Converts an arbitrary variable or constraint name into a valid LP identifier. |
|
|
189
222
|
|
|
190
223
|
```typescript
|
|
191
224
|
export interface LpExportResult {
|
|
192
|
-
/** Standard CPLEX/HiGHS LP formatted string */
|
|
193
225
|
lpString: string;
|
|
194
|
-
|
|
195
|
-
/** Map translating sanitized LP variable identifiers back to WaSHI Var objects */
|
|
196
226
|
nameToVar: Map<string, Var>;
|
|
197
|
-
|
|
198
|
-
/** Map translating sanitized LP constraint identifiers back to WaSHI Constraint objects */
|
|
199
227
|
nameToConstraint: Map<string, Constraint>;
|
|
200
228
|
}
|
|
201
229
|
```
|
|
202
230
|
|
|
203
|
-
### `SolverResult`
|
|
231
|
+
### `SolverResult` & `SolverCheckResult` Normalization
|
|
204
232
|
|
|
205
|
-
|
|
233
|
+
`solve()` must key returned `values`, `boolValues`, `reducedCosts`, `duals`, and `slacks` by the model's canonical names (`v.name`, `bv.name`, `c.name`). Likewise, `check()` keys `values`, `unsatCore`, and `conflicts` by canonical names (`v.name`, `bv.name`, `c.name`, `lc.name`). When using LP helpers, decode sanitized LP identifiers back via `nameToVar` and `nameToConstraint`.
|
|
206
234
|
|
|
207
235
|
```typescript
|
|
208
236
|
export interface SolverResult {
|
|
209
|
-
/** Standardized status string */
|
|
210
237
|
status: SolverStatus;
|
|
211
|
-
|
|
212
|
-
/** Exact termination reason independent of whether a feasible incumbent is available (defaults to status) */
|
|
213
238
|
terminationStatus?: TerminationStatus;
|
|
214
|
-
|
|
215
|
-
/** Whether a feasible primal solution / incumbent is available (inferred from status/values if omitted) */
|
|
216
239
|
hasPrimalSolution?: boolean;
|
|
217
|
-
|
|
218
|
-
/** Optimal or best objective value (null if no primal solution is available) */
|
|
219
240
|
objectiveValue: number | null;
|
|
220
|
-
|
|
221
|
-
/** Best proven dual/objective bound when provided by the solver */
|
|
222
241
|
objectiveBound?: number;
|
|
223
|
-
|
|
224
|
-
/** Relative MIP optimality gap when provided by the solver */
|
|
225
242
|
relativeGap?: number;
|
|
226
|
-
|
|
227
|
-
/** Absolute MIP optimality gap |objectiveValue - objectiveBound| (computed automatically when both are finite) */
|
|
228
243
|
absoluteGap?: number;
|
|
229
|
-
|
|
230
|
-
/** Variable primal values keyed by canonical variable name (non-finite values are ignored) */
|
|
231
244
|
values: Record<string, number>;
|
|
232
|
-
|
|
233
|
-
/** Propositional Boolean variable assignments keyed by BoolVar name (optional, SMT/SAT) */
|
|
234
245
|
boolValues?: Record<string, boolean>;
|
|
235
|
-
|
|
236
|
-
/** Variable reduced costs (optional, LP only) */
|
|
237
246
|
reducedCosts?: Record<string, number>;
|
|
238
|
-
|
|
239
|
-
/** Constraint dual values / shadow prices (optional, LP only) */
|
|
240
247
|
duals?: Record<string, number>;
|
|
241
|
-
|
|
242
|
-
/** Constraint slack values (optional) */
|
|
243
248
|
slacks?: Record<string, number>;
|
|
244
|
-
|
|
245
|
-
/** Solve execution duration in seconds */
|
|
246
249
|
runTime?: number;
|
|
247
|
-
|
|
248
|
-
/** Diagnostic solver logs / text outputs */
|
|
249
250
|
logs?: string[];
|
|
251
|
+
raw?: any;
|
|
252
|
+
}
|
|
250
253
|
|
|
251
|
-
|
|
254
|
+
export interface SolverCheckResult {
|
|
255
|
+
status: "Sat" | "Unsat" | "Unknown";
|
|
256
|
+
values: Record<string, number | boolean>;
|
|
257
|
+
unsatCore?: readonly string[];
|
|
258
|
+
/** Alias for unsatCore: subset of conflicting constraints causing infeasibility */
|
|
259
|
+
conflicts?: readonly string[];
|
|
260
|
+
runTime?: number;
|
|
261
|
+
logs?: string[];
|
|
252
262
|
raw?: any;
|
|
253
263
|
}
|
|
254
264
|
```
|
|
255
265
|
|
|
256
266
|
#### Standardized `SolverStatus` Values
|
|
257
|
-
|
|
258
|
-
- `"
|
|
259
|
-
- `"
|
|
267
|
+
|
|
268
|
+
- `"Optimal"`: Optimal solution found (within MIP tolerance, or proven optimal for LP).
|
|
269
|
+
- `"Feasible"`: Feasible incumbent found, but optimality is not yet proven.
|
|
270
|
+
- `"Infeasible"`: Model is proven infeasible.
|
|
260
271
|
- `"Unbounded"`: Model objective is unbounded.
|
|
261
|
-
- `"TimeLimit"`:
|
|
262
|
-
- `"IterationLimit"`:
|
|
263
|
-
- `"Sat"` / `"Unsat"`: Satisfiable / unsatisfiable status
|
|
264
|
-
- `"Error"` / `"NotSolved"` / `"Unknown"`:
|
|
272
|
+
- `"TimeLimit"`: Stopped by `timeLimit` without finding a feasible incumbent.
|
|
273
|
+
- `"IterationLimit"`: Stopped by an iteration limit without finding a feasible incumbent.
|
|
274
|
+
- `"Sat"` / `"Unsat"`: Satisfiable / unsatisfiable status from `check()` or SMT solvers.
|
|
275
|
+
- `"Error"` / `"NotSolved"` / `"Unknown"`: Failure, unsolved, or unrecognized status. Never map `"Unknown"` or `"Error"` to `"Optimal"`.
|
|
265
276
|
|
|
266
277
|
---
|
|
267
278
|
|
|
@@ -269,7 +280,7 @@ export interface SolverResult {
|
|
|
269
280
|
|
|
270
281
|
### Tutorial A: In-House Cloud REST Solver Adapter
|
|
271
282
|
|
|
272
|
-
|
|
283
|
+
This example builds a custom solver that calls an HTTP microservice accepting CPLEX LP strings generated on demand with `generateLpString`:
|
|
273
284
|
|
|
274
285
|
```typescript
|
|
275
286
|
import {
|
|
@@ -294,21 +305,27 @@ export class CloudSolver implements Solver {
|
|
|
294
305
|
supportsMIP: true,
|
|
295
306
|
supportsQP: false,
|
|
296
307
|
};
|
|
308
|
+
public readonly options: SolverOptions;
|
|
297
309
|
|
|
298
310
|
private endpoint: string;
|
|
299
311
|
private apiKey: string;
|
|
300
312
|
|
|
301
313
|
constructor(config: CloudSolverConfig = {}) {
|
|
302
|
-
|
|
303
|
-
this.
|
|
314
|
+
const { endpoint, apiKey, ...defaultOptions } = config;
|
|
315
|
+
this.endpoint = endpoint || "https://solver.mycompany.internal/v1/solve";
|
|
316
|
+
this.apiKey = apiKey || "";
|
|
317
|
+
this.options = defaultOptions;
|
|
304
318
|
}
|
|
305
319
|
|
|
306
320
|
async solve(
|
|
307
321
|
modelData: SolveModelData,
|
|
308
322
|
options: SolverOptions = {}
|
|
309
323
|
): Promise<SolverResult> {
|
|
324
|
+
const mergedOptions: SolverOptions = { ...this.options, ...options };
|
|
310
325
|
const startTime = performance.now();
|
|
311
|
-
const { lpString, nameToVar, nameToConstraint } = generateLpString(modelData
|
|
326
|
+
const { lpString, nameToVar, nameToConstraint } = generateLpString(modelData, {
|
|
327
|
+
relax: mergedOptions.relax,
|
|
328
|
+
});
|
|
312
329
|
|
|
313
330
|
let payload: any;
|
|
314
331
|
try {
|
|
@@ -320,11 +337,11 @@ export class CloudSolver implements Solver {
|
|
|
320
337
|
},
|
|
321
338
|
body: JSON.stringify({
|
|
322
339
|
lp: lpString,
|
|
323
|
-
timeLimit:
|
|
324
|
-
relativeGap:
|
|
325
|
-
absoluteGap:
|
|
340
|
+
timeLimit: mergedOptions.timeLimit,
|
|
341
|
+
relativeGap: mergedOptions.relativeGap,
|
|
342
|
+
absoluteGap: mergedOptions.absoluteGap,
|
|
326
343
|
}),
|
|
327
|
-
signal:
|
|
344
|
+
signal: mergedOptions.signal,
|
|
328
345
|
});
|
|
329
346
|
|
|
330
347
|
if (!response.ok) {
|
|
@@ -333,10 +350,11 @@ export class CloudSolver implements Solver {
|
|
|
333
350
|
|
|
334
351
|
payload = await response.json();
|
|
335
352
|
} catch (err: any) {
|
|
336
|
-
if (
|
|
353
|
+
if (mergedOptions.signal?.aborted || err?.name === "AbortError") {
|
|
337
354
|
return {
|
|
338
355
|
status: "Unknown",
|
|
339
356
|
terminationStatus: "Interrupted",
|
|
357
|
+
hasPrimalSolution: false,
|
|
340
358
|
objectiveValue: null,
|
|
341
359
|
values: {},
|
|
342
360
|
runTime: (performance.now() - startTime) / 1000,
|
|
@@ -374,19 +392,25 @@ export class CloudSolver implements Solver {
|
|
|
374
392
|
UNSAT: "Unsat",
|
|
375
393
|
};
|
|
376
394
|
const rawStatus: SolverStatus = statusMap[String(payload.status).toUpperCase()] ?? "Unknown";
|
|
395
|
+
const offset = modelData.objective?.constant ?? 0;
|
|
396
|
+
const hasFiniteObj =
|
|
397
|
+
typeof payload.objectiveValue === "number" && Number.isFinite(payload.objectiveValue);
|
|
377
398
|
const hasIncumbent =
|
|
378
|
-
|
|
399
|
+
hasFiniteObj ||
|
|
379
400
|
Object.values(values).some((val) => typeof val === "number" && Number.isFinite(val));
|
|
380
401
|
const isLimitStop = rawStatus === "TimeLimit" || rawStatus === "IterationLimit";
|
|
381
402
|
const status: SolverStatus = isLimitStop && hasIncumbent ? "Feasible" : rawStatus;
|
|
403
|
+
const hasPrimalSolution =
|
|
404
|
+
(status === "Optimal" || status === "Feasible") && hasIncumbent;
|
|
382
405
|
|
|
383
406
|
return {
|
|
384
407
|
status,
|
|
385
408
|
terminationStatus: rawStatus,
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
409
|
+
hasPrimalSolution,
|
|
410
|
+
objectiveValue: hasPrimalSolution && hasFiniteObj ? payload.objectiveValue + offset : null,
|
|
411
|
+
values: hasPrimalSolution ? values : {},
|
|
412
|
+
duals: hasPrimalSolution ? duals : {},
|
|
413
|
+
slacks: hasPrimalSolution ? slacks : {},
|
|
390
414
|
runTime: payload.solveDuration || runTime,
|
|
391
415
|
logs: payload.logs || [],
|
|
392
416
|
raw: payload,
|
|
@@ -405,18 +429,26 @@ registerCloudSolver();
|
|
|
405
429
|
|
|
406
430
|
### Tutorial B: Registering and Using the Custom Solver
|
|
407
431
|
|
|
408
|
-
|
|
432
|
+
Choose how to attach your solver based on whether configuration is shared globally or varies per call or per model:
|
|
433
|
+
|
|
434
|
+
| Method | Best For |
|
|
435
|
+
| :--- | :--- |
|
|
436
|
+
| **Registered name string** | Reusable plugins with default settings (`solver: "cloud"`). |
|
|
437
|
+
| **Solver instance** | Per-call or per-model credentials, endpoints, or custom option presets. |
|
|
438
|
+
| **Global default (`setDefaultSolver`)** | Replacing HiGHS across an entire application without updating every `new Model()`. |
|
|
409
439
|
|
|
410
440
|
#### Option 1: By Registered Name String
|
|
441
|
+
|
|
411
442
|
```typescript
|
|
412
443
|
import { Model } from "@ohtaman/washi";
|
|
413
444
|
import "./cloud-solver.js"; // Auto-registers "cloud"
|
|
414
445
|
|
|
415
446
|
const model = new Model({ name: "SupplyChain", solver: "cloud" });
|
|
416
|
-
// Or model.solve("cloud")
|
|
447
|
+
// Or await model.solve({ solver: "cloud" })
|
|
417
448
|
```
|
|
418
449
|
|
|
419
450
|
#### Option 2: By Passing the Solver Instance Directly
|
|
451
|
+
|
|
420
452
|
```typescript
|
|
421
453
|
import { Model } from "@ohtaman/washi";
|
|
422
454
|
import { CloudSolver } from "./cloud-solver.js";
|
|
@@ -431,13 +463,14 @@ const status = await model.solve();
|
|
|
431
463
|
```
|
|
432
464
|
|
|
433
465
|
#### Option 3: Setting Global Default Solver
|
|
466
|
+
|
|
434
467
|
```typescript
|
|
435
468
|
import { setDefaultSolver, Model } from "@ohtaman/washi";
|
|
436
469
|
import { CloudSolver } from "./cloud-solver.js";
|
|
437
470
|
|
|
438
471
|
setDefaultSolver(() => new CloudSolver());
|
|
439
472
|
|
|
440
|
-
//
|
|
473
|
+
// Models without an explicit solver use CloudSolver by default
|
|
441
474
|
const model = new Model("DefaultedModel");
|
|
442
475
|
```
|
|
443
476
|
|
|
@@ -445,9 +478,10 @@ const model = new Model("DefaultedModel");
|
|
|
445
478
|
|
|
446
479
|
## 4. Distribution: Packaging as a Plugin
|
|
447
480
|
|
|
448
|
-
To publish your solver as an independent npm package (
|
|
481
|
+
To publish your solver as an independent npm package (such as `@my-org/washi-solver-cplex`), declare Node ESM and browser entry points in `package.json`:
|
|
449
482
|
|
|
450
483
|
### 1. `package.json` Exports Structure
|
|
484
|
+
|
|
451
485
|
```json
|
|
452
486
|
{
|
|
453
487
|
"name": "@my-org/washi-solver-cplex",
|
|
@@ -469,22 +503,24 @@ To publish your solver as an independent npm package (e.g. `@my-org/washi-solver
|
|
|
469
503
|
"./browser/index.min.js": "./dist/browser/index.min.js"
|
|
470
504
|
},
|
|
471
505
|
"peerDependencies": {
|
|
472
|
-
"@ohtaman/washi": ">=0.
|
|
506
|
+
"@ohtaman/washi": ">=0.8.0"
|
|
473
507
|
}
|
|
474
508
|
}
|
|
475
509
|
```
|
|
476
510
|
|
|
477
|
-
### 2. Browser
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
-
|
|
511
|
+
### 2. Browser Standalone Build (IIFE)
|
|
512
|
+
|
|
513
|
+
When building a browser `.min.js` bundle, configure Vite or Rollup for Immediately Invoked Function Expression (`iife`) output:
|
|
514
|
+
|
|
515
|
+
- **Output format**: `iife`
|
|
516
|
+
- **Global name**: `WashiCplex`
|
|
517
|
+
- **Registry linking**: Because registration uses `Symbol.for("washi.solverRegistry")`, loading `<script src="washi.min.js"></script>` followed by `<script src="washi-cplex.min.js"></script>` connects automatically.
|
|
482
518
|
|
|
483
519
|
---
|
|
484
520
|
|
|
485
521
|
## 5. Testing Your Custom Solver
|
|
486
522
|
|
|
487
|
-
You can test your
|
|
523
|
+
You can test your adapter directly in Vitest or Jest without bundling by mocking network calls:
|
|
488
524
|
|
|
489
525
|
```typescript
|
|
490
526
|
import { describe, it, expect, vi } from "vitest";
|