@ohtaman/washi 0.7.0 → 0.8.1

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.
Files changed (162) hide show
  1. package/README.ja.md +60 -23
  2. package/README.md +59 -22
  3. package/README.zh-CN.md +62 -25
  4. package/THIRD_PARTY_LICENSES.md +34 -1
  5. package/dist/browser/solvers/glpk.js +289 -238
  6. package/dist/browser/solvers/glpk.min.js +1 -1
  7. package/dist/browser/solvers/highs-__vite-browser-external-Dn-83hnF.js +7 -0
  8. package/dist/browser/solvers/highs-browser-runtime-0Bgq5ksG.js +2021 -0
  9. package/dist/browser/{loader-DewlSho8.js → solvers/highs-loader-BLY3wzDH.js} +190 -149
  10. package/dist/browser/solvers/highs-worker.js +54 -0
  11. package/dist/browser/solvers/highs.js +1414 -0
  12. package/dist/browser/solvers/highs.wasm +0 -0
  13. package/dist/browser/solvers/pumpkin-_washi-pumpkin-node-runtime-CEKUDK6d.js +9 -0
  14. package/dist/browser/solvers/pumpkin-loader-CvdaKxf4.js +320 -0
  15. package/dist/browser/solvers/pumpkin-worker.js +45 -0
  16. package/dist/browser/solvers/pumpkin.js +1777 -0
  17. package/dist/browser/solvers/pumpkin.wasm +0 -0
  18. package/dist/browser/solvers/pumpkin.wasm.LICENSE +21 -0
  19. package/dist/browser/solvers/z3.js +1600 -1335
  20. package/dist/browser/solvers/z3.min.js +2 -2
  21. package/dist/browser/washi.js +3313 -1699
  22. package/dist/browser/washi.min.js +5 -3
  23. package/dist/lib/solvers/__vite-browser-external-oLchLuMx.js +7 -0
  24. package/dist/lib/solvers/glpk.js +174 -132
  25. package/dist/lib/solvers/highs-browser-runtime.js +2413 -0
  26. package/dist/lib/{highs-node-runtime.js → solvers/highs-node-runtime.js} +1 -1
  27. package/dist/lib/solvers/highs-runtime.js +16 -0
  28. package/dist/lib/solvers/highs-worker.js +54 -0
  29. package/dist/lib/solvers/highs.js +410 -0
  30. package/dist/lib/solvers/highs.wasm +0 -0
  31. package/dist/lib/{loader-BZUQNvh4.js → solvers/loader-CDpPXN_F.js} +138 -116
  32. package/dist/lib/solvers/loader-Ow74Fa1B.js +311 -0
  33. package/dist/lib/solvers/lp-writer-CLScMFbS.js +115 -0
  34. package/dist/lib/solvers/pumpkin-assets.js +1 -0
  35. package/dist/lib/solvers/pumpkin-node-runtime.js +15 -0
  36. package/dist/lib/solvers/pumpkin-worker-factory.js +1 -0
  37. package/dist/lib/solvers/pumpkin-worker.js +45 -0
  38. package/dist/lib/solvers/pumpkin.js +868 -0
  39. package/dist/lib/solvers/pumpkin.wasm +0 -0
  40. package/dist/lib/solvers/pumpkin.wasm.LICENSE +21 -0
  41. package/dist/lib/solvers/registry-CrYRRk9q.js +907 -0
  42. package/dist/lib/solvers/rolldown-runtime-DtPi1Y-2.js +13 -0
  43. package/dist/lib/solvers/types-BGqPkxwu.js +12 -0
  44. package/dist/lib/solvers/z3.js +489 -307
  45. package/dist/lib/washi.js +3905 -1410
  46. package/dist/types/decomposition/benders.d.ts.map +1 -1
  47. package/dist/types/index.d.ts +4 -5
  48. package/dist/types/index.d.ts.map +1 -1
  49. package/dist/types/modeler/boolean-expr.d.ts +137 -29
  50. package/dist/types/modeler/boolean-expr.d.ts.map +1 -1
  51. package/dist/types/modeler/constraint.d.ts +40 -4
  52. package/dist/types/modeler/constraint.d.ts.map +1 -1
  53. package/dist/types/modeler/expression.d.ts +129 -2
  54. package/dist/types/modeler/expression.d.ts.map +1 -1
  55. package/dist/types/modeler/index.d.ts +4 -6
  56. package/dist/types/modeler/index.d.ts.map +1 -1
  57. package/dist/types/modeler/linearizer.d.ts +11 -2
  58. package/dist/types/modeler/linearizer.d.ts.map +1 -1
  59. package/dist/types/modeler/lp-writer.d.ts.map +1 -1
  60. package/dist/types/modeler/model.d.ts +86 -126
  61. package/dist/types/modeler/model.d.ts.map +1 -1
  62. package/dist/types/modeler/parser.d.ts.map +1 -1
  63. package/dist/types/modeler/quad-expr.d.ts +8 -2
  64. package/dist/types/modeler/quad-expr.d.ts.map +1 -1
  65. package/dist/types/modeler/solution-validator.d.ts +12 -12
  66. package/dist/types/modeler/solution-validator.d.ts.map +1 -1
  67. package/dist/types/modeler/solvers/glpk/loader.d.ts +4 -0
  68. package/dist/types/modeler/solvers/glpk/loader.d.ts.map +1 -1
  69. package/dist/types/modeler/solvers/glpk/solver.d.ts +2 -2
  70. package/dist/types/modeler/solvers/glpk/solver.d.ts.map +1 -1
  71. package/dist/types/modeler/solvers/highs/driver.d.ts.map +1 -1
  72. package/dist/types/modeler/solvers/highs/loader.d.ts +8 -6
  73. package/dist/types/modeler/solvers/highs/loader.d.ts.map +1 -1
  74. package/dist/types/modeler/solvers/highs/solver.d.ts.map +1 -1
  75. package/dist/types/modeler/solvers/highs/worker-client.d.ts +16 -0
  76. package/dist/types/modeler/solvers/highs/worker-client.d.ts.map +1 -1
  77. package/dist/types/modeler/solvers/index.d.ts +2 -1
  78. package/dist/types/modeler/solvers/index.d.ts.map +1 -1
  79. package/dist/types/modeler/solvers/mock.d.ts +2 -3
  80. package/dist/types/modeler/solvers/mock.d.ts.map +1 -1
  81. package/dist/types/modeler/solvers/pumpkin/assets.d.ts +2 -0
  82. package/dist/types/modeler/solvers/pumpkin/assets.d.ts.map +1 -0
  83. package/dist/types/modeler/solvers/pumpkin/index.d.ts +6 -0
  84. package/dist/types/modeler/solvers/pumpkin/index.d.ts.map +1 -0
  85. package/dist/types/modeler/solvers/pumpkin/loader.d.ts +40 -0
  86. package/dist/types/modeler/solvers/pumpkin/loader.d.ts.map +1 -0
  87. package/dist/types/modeler/solvers/pumpkin/node-runtime.d.ts +3 -0
  88. package/dist/types/modeler/solvers/pumpkin/node-runtime.d.ts.map +1 -0
  89. package/dist/types/modeler/solvers/pumpkin/solver.d.ts +23 -0
  90. package/dist/types/modeler/solvers/pumpkin/solver.d.ts.map +1 -0
  91. package/dist/types/modeler/solvers/pumpkin/worker-client.d.ts +74 -0
  92. package/dist/types/modeler/solvers/pumpkin/worker-client.d.ts.map +1 -0
  93. package/dist/types/modeler/solvers/pumpkin/worker-factory.d.ts +3 -0
  94. package/dist/types/modeler/solvers/pumpkin/worker-factory.d.ts.map +1 -0
  95. package/dist/types/modeler/solvers/pumpkin/worker.d.ts +7 -0
  96. package/dist/types/modeler/solvers/pumpkin/worker.d.ts.map +1 -0
  97. package/dist/types/modeler/solvers/pumpkin.d.ts +2 -0
  98. package/dist/types/modeler/solvers/pumpkin.d.ts.map +1 -0
  99. package/dist/types/modeler/solvers/types.d.ts +7 -9
  100. package/dist/types/modeler/solvers/types.d.ts.map +1 -1
  101. package/dist/types/modeler/solvers/z3/loader.d.ts +4 -0
  102. package/dist/types/modeler/solvers/z3/loader.d.ts.map +1 -1
  103. package/dist/types/modeler/solvers/z3/solver.d.ts +2 -15
  104. package/dist/types/modeler/solvers/z3/solver.d.ts.map +1 -1
  105. package/dist/types/modeler/types.d.ts +12 -15
  106. package/dist/types/modeler/types.d.ts.map +1 -1
  107. package/dist/types/modeler/validation.d.ts +26 -2
  108. package/dist/types/modeler/validation.d.ts.map +1 -1
  109. package/dist/types/modeler/variable.d.ts +9 -5
  110. package/dist/types/modeler/variable.d.ts.map +1 -1
  111. package/docs/examples/diet-sensitivity.md +33 -14
  112. package/docs/examples/ecommerce-coupons.md +17 -9
  113. package/docs/examples/index.md +36 -27
  114. package/docs/examples/knapsack.md +11 -5
  115. package/docs/examples/portfolio-qp.md +21 -8
  116. package/docs/examples/production.md +10 -5
  117. package/docs/examples/project-scheduling.md +119 -0
  118. package/docs/examples/shift-scheduling.md +29 -18
  119. package/docs/examples/smt-security.md +27 -11
  120. package/docs/examples/transportation.md +23 -13
  121. package/docs/examples/traveling-salesperson.md +34 -22
  122. package/docs/examples/unsat-core.md +27 -14
  123. package/docs/examples/vehicle-routing.md +74 -28
  124. package/docs/getting-started/ai-agents.md +190 -82
  125. package/docs/getting-started/coding-agents.md +177 -0
  126. package/docs/getting-started/cross-origin.md +27 -14
  127. package/docs/getting-started/index.md +49 -43
  128. package/docs/getting-started/runtimes.md +94 -40
  129. package/docs/guide/constraint-programming.md +171 -0
  130. package/docs/guide/constraints.md +115 -33
  131. package/docs/guide/decomposition.md +118 -88
  132. package/docs/guide/expressions.md +106 -43
  133. package/docs/guide/formulation-tips.md +47 -28
  134. package/docs/guide/objectives.md +102 -75
  135. package/docs/guide/unsat-core.md +62 -50
  136. package/docs/guide/variables.md +81 -35
  137. package/docs/solvers/custom-solver.md +163 -122
  138. package/docs/solvers/glpk.md +58 -17
  139. package/docs/solvers/highs.md +124 -57
  140. package/docs/solvers/index.md +33 -20
  141. package/docs/solvers/pumpkin.md +171 -0
  142. package/docs/solvers/z3.md +63 -25
  143. package/llms-full.txt +2232 -932
  144. package/llms.txt +144 -91
  145. package/package.json +55 -19
  146. package/skills/washi/SKILL.md +107 -34
  147. package/dist/browser/_washi-node-only-BvRk9kiK.js +0 -0
  148. package/dist/browser/browser-runtime-TLCY0hkc.js +0 -1793
  149. package/dist/browser/highs-worker.js +0 -39
  150. package/dist/browser/highs.wasm +0 -0
  151. package/dist/lib/_washi-node-only-BvRk9kiK.js +0 -0
  152. package/dist/lib/highs-browser-runtime.js +0 -2185
  153. package/dist/lib/highs-runtime.js +0 -2
  154. package/dist/lib/highs-worker.js +0 -39
  155. package/dist/lib/highs.wasm +0 -0
  156. package/dist/lib/registry-D7TZ3Wh_.js +0 -1034
  157. package/docs/ja/getting-started/ai-agents.md +0 -280
  158. package/docs/zh/getting-started/ai-agents.md +0 -280
  159. /package/dist/browser/{highs.wasm.LICENSE → solvers/highs.wasm.LICENSE} +0 -0
  160. /package/dist/lib/{highs-assets.js → solvers/highs-assets.js} +0 -0
  161. /package/dist/lib/{highs-worker-factory.js → solvers/highs-worker-factory.js} +0 -0
  162. /package/dist/lib/{highs.wasm.LICENSE → solvers/highs.wasm.LICENSE} +0 -0
@@ -2,24 +2,11 @@ import type { CheckModelData, SolveModelData, Solver, SolverCapabilities, Solver
2
2
  import { type LpExportResult, type LpModelData } from "../../lp-writer.js";
3
3
  import { resetZ3LoaderCache, killZ3Threads, notifyZ3WorkerError } from "./loader.js";
4
4
  export { resetZ3LoaderCache, killZ3Threads, notifyZ3WorkerError };
5
- /**
6
- * Z3 SMT / SAT / MIP Solver Adapter
7
- *
8
- * Supports:
9
- * - SAT & SMT logical formulas (And, Or, Not, Implies, Xor)
10
- * - Feasibility checking via check()
11
- * - Unsatisfiable core extraction (unsatCore) for conflict diagnosis
12
- * - Mixed-Integer Programming (MIP) optimization via Z3 Optimize
13
- *
14
- * Solution metrics provided by Z3:
15
- * - `status` and `terminationStatus`: `"Optimal" | "Infeasible" | "Error"`
16
- * - `hasPrimalSolution`: `true` when Z3 returns `"sat"` (`status === "Optimal"`), `false` otherwise.
17
- * - `objectiveValue`: Primal objective value when `hasPrimalSolution` is `true`, otherwise `null`.
18
- * - `objectiveBound`, `relGap` (`mipGap`), `absGap`: Z3 Optimize is an SMT optimization engine and does not expose branch-and-bound MIP dual bounds or relative/absolute MIP gaps, so these fields are intentionally left `undefined` rather than returning invented defaults.
19
- */
20
5
  export declare class Z3Solver implements Solver {
21
6
  readonly name = "z3";
7
+ readonly options: SolverOptions;
22
8
  readonly capabilities: SolverCapabilities;
9
+ constructor(options?: SolverOptions);
23
10
  /**
24
11
  * Helper to build AST translations for a given Z3 Context
25
12
  */
@@ -1 +1 @@
1
- {"version":3,"file":"solver.d.ts","sourceRoot":"","sources":["../../../../../src/modeler/solvers/z3/solver.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACV,cAAc,EACd,cAAc,EACd,MAAM,EACN,kBAAkB,EAClB,aAAa,EACb,YAAY,EACZ,iBAAiB,EAClB,MAAM,aAAa,CAAC;AACrB,OAAO,EAEL,KAAK,cAAc,EACnB,KAAK,WAAW,EACjB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAU,kBAAkB,EAAE,aAAa,EAA0B,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAErH,OAAO,EAAE,kBAAkB,EAAE,aAAa,EA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AAE,kBAAkB,CAOvC;IAEF,YAAY,OAAO,GAAE,aAAkB,EAEtC;IAED;;OAEG;IACH,OAAO,CAAC,uBAAuB;IAwc/B,OAAO,CAAC,iBAAiB;YAqBX,wBAAwB;IAgBtC;;;OAGG;IACG,KAAK,CAAC,SAAS,EAAE,cAAc,EAAE,OAAO,GAAE,aAAkB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CA+H9F;IAED;;OAEG;IACG,KAAK,CAAC,SAAS,EAAE,cAAc,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;IACjF,KAAK,CACT,SAAS,EAAE,WAAW,EACtB,QAAQ,EAAE,cAAc,EACxB,OAAO,CAAC,EAAE,aAAa,GACtB,OAAO,CAAC,YAAY,CAAC,CAAC;CAkT1B;AAED;;GAEG;AACH,wBAAgB,gBAAgB,IAAI,IAAI,CAEvC"}
@@ -9,7 +9,7 @@ export type OptimizationSense = "Minimize" | "Maximize" | "minimize" | "maximize
9
9
  * Throws a descriptive error if an invalid sense string is provided at runtime.
10
10
  */
11
11
  export declare function normalizeSense(sense?: OptimizationSense): "Maximize" | "Minimize";
12
- export type ComparisonSense = "<=" | ">=" | "==";
12
+ export type ComparisonSense = "<=" | ">=" | "==" | "!=";
13
13
  export type SolverStatus = "Optimal" | "Feasible" | "Infeasible" | "Unbounded" | "TimeLimit" | "IterationLimit" | "Error" | "NotSolved" | "Sat" | "Unsat" | "Unknown";
14
14
  export type TerminationStatus = "Optimal" | "Feasible" | "Infeasible" | "Unbounded" | "InfeasibleOrUnbounded" | "TimeLimit" | "IterationLimit" | "SolutionLimit" | "MemoryLimit" | "ObjectiveBound" | "ObjectiveTarget" | "Interrupted" | "Error" | "NotSolved" | "Sat" | "Unsat" | "Unknown";
15
15
  export interface SolveProgressEvent {
@@ -24,11 +24,9 @@ export interface SolveProgressEvent {
24
24
  /** Best proven dual / objective bound when available */
25
25
  objectiveBound?: number;
26
26
  /** Relative MIP optimality gap when available */
27
- relGap?: number;
28
- /** Alias for relGap: relative MIP optimality gap when available */
29
- mipGap?: number;
27
+ relativeGap?: number;
30
28
  /** Absolute MIP optimality gap |objectiveValue - objectiveBound| when available */
31
- absGap?: number;
29
+ absoluteGap?: number;
32
30
  /** Explored MIP branch-and-bound node count when available */
33
31
  nodes?: number;
34
32
  /** Solver iteration count (simplex, IPM, or MIP LP iterations) when available */
@@ -41,16 +39,17 @@ export interface SolveOptions {
41
39
  outputFlag?: boolean;
42
40
  presolve?: "on" | "off" | "choose";
43
41
  solver?: any;
44
- relGap?: number;
45
- absGap?: number;
42
+ relativeGap?: number;
43
+ absoluteGap?: number;
46
44
  wasmUrl?: string;
47
45
  /**
48
46
  * AbortSignal to cancel a solve:
49
47
  * - Pre-solve cancellation (`signal.aborted` before execution starts) is supported by all solvers.
50
- * - Preemptive mid-solve cancellation is supported in browser HiGHS via Web Worker termination
51
- * (and cancels queued Worker requests before they are dispatched).
52
- * - Main-thread (Node.js) HiGHS supports cooperative mid-solve cancellation when `abort()` is
53
- * triggered synchronously inside an `onProgress` callback at a solver interrupt checkpoint.
48
+ * - Preemptive mid-solve cancellation is supported in browser HiGHS and Pumpkin via Web Worker
49
+ * termination (and cancels queued Worker requests before they are dispatched).
50
+ * - Main-thread (Node.js) HiGHS and Pumpkin support cooperative mid-solve cancellation (`abort()`
51
+ * triggered synchronously inside an `onProgress` callback for HiGHS; `should_abort` solver
52
+ * interrupt checkpoints for Pumpkin).
54
53
  * - Single-threaded WASM solvers without interrupt hooks (GLPK, Z3) check `signal` before execution starts.
55
54
  */
56
55
  signal?: AbortSignal;
@@ -70,11 +69,9 @@ export interface SolverSolution {
70
69
  /** Best proven dual/objective bound when provided by the solver */
71
70
  objectiveBound?: number;
72
71
  /** Relative MIP optimality gap when provided by the solver */
73
- relGap?: number;
74
- /** Alias for relGap: relative MIP optimality gap when provided by the solver */
75
- mipGap?: number;
72
+ relativeGap?: number;
76
73
  /** Absolute MIP optimality gap |objectiveValue - objectiveBound| when provided by the solver */
77
- absGap?: number;
74
+ absoluteGap?: number;
78
75
  values: Record<string, number>;
79
76
  duals: Record<string, number>;
80
77
  reducedCosts: Record<string, number>;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/modeler/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAElD;;GAEG;AAEH,MAAM,MAAM,OAAO,GACf,YAAY,GACZ,SAAS,GACT,QAAQ,GACR,YAAY,GACZ,SAAS,GACT,QAAQ,GACR,MAAM,GACN,KAAK,GACL,KAAK,CAAC;AAEV,MAAM,MAAM,iBAAiB,GACzB,UAAU,GACV,UAAU,GACV,UAAU,GACV,UAAU,CAAC;AAEf;;;GAGG;AACH,wBAAgB,cAAc,CAAC,KAAK,CAAC,EAAE,iBAAiB,GAAG,UAAU,GAAG,UAAU,CAUjF;AAGD,MAAM,MAAM,eAAe,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC;AAEjD,MAAM,MAAM,YAAY,GACpB,SAAS,GACT,UAAU,GACV,YAAY,GACZ,WAAW,GACX,WAAW,GACX,gBAAgB,GAChB,OAAO,GACP,WAAW,GACX,KAAK,GACL,OAAO,GACP,SAAS,CAAC;AAEd,MAAM,MAAM,iBAAiB,GACzB,SAAS,GACT,UAAU,GACV,YAAY,GACZ,WAAW,GACX,uBAAuB,GACvB,WAAW,GACX,gBAAgB,GAChB,eAAe,GACf,aAAa,GACb,gBAAgB,GAChB,iBAAiB,GACjB,aAAa,GACb,OAAO,GACP,WAAW,GACX,KAAK,GACL,OAAO,GACP,SAAS,CAAC;AAEd,MAAM,WAAW,kBAAkB;IACjC,qCAAqC;IACrC,WAAW,EAAE,MAAM,CAAC;IACpB,4DAA4D;IAC5D,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,sCAAsC;IACtC,KAAK,CAAC,EAAE,SAAS,GAAG,KAAK,GAAG,KAAK,CAAC;IAClC,6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KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CAC/D"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/modeler/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAElD;;GAEG;AAEH,MAAM,MAAM,OAAO,GACf,YAAY,GACZ,SAAS,GACT,QAAQ,GACR,YAAY,GACZ,SAAS,GACT,QAAQ,GACR,MAAM,GACN,KAAK,GACL,KAAK,CAAC;AAEV,MAAM,MAAM,iBAAiB,GACzB,UAAU,GACV,UAAU,GACV,UAAU,GACV,UAAU,CAAC;AAEf;;;GAGG;AACH,wBAAgB,cAAc,CAAC,KAAK,CAAC,EAAE,iBAAiB,GAAG,UAAU,GAAG,UAAU,CAUjF;AAGD,MAAM,MAAM,eAAe,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC;AAExD,MAAM,MAAM,YAAY,GACpB,SAAS,GACT,UAAU,GACV,YAAY,GACZ,WAAW,GACX,WAAW,GACX,gBAAgB,GAChB,OAAO,GACP,WAAW,GACX,KAAK,GACL,OAAO,GACP,SAAS,CAAC;AAEd,MAAM,MAAM,iBAAiB,GACzB,SAAS,GACT,UAAU,GACV,YAAY,GACZ,WAAW,GACX,uBAAuB,GACvB,WAAW,GACX,gBAAgB,GAChB,eAAe,GACf,aAAa,GACb,gBAAgB,GAChB,iBAAiB,GACjB,aAAa,GACb,OAAO,GACP,WAAW,GACX,KAAK,GACL,OAAO,GACP,SAAS,CAAC;AAEd,MAAM,WAAW,kBAAkB;IACjC,qCAAqC;IACrC,WAAW,EAAE,MAAM,CAAC;IACpB,4DAA4D;IAC5D,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,sCAAsC;IACtC,KAAK,CAAC,EAAE,SAAS,GAAG,KAAK,GAAG,KAAK,CAAC;IAClC,6DAA6D;IAC7D,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,wDAAwD;I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AM,CAAC,CAAC;IAC/B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC9B,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACrC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,CAAC,EAAE,GAAG,CAAC;CACX;AAED,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,KAAK,GAAG,OAAO,GAAG,SAAS,CAAC;IACpC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC;IACzC,SAAS,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC9B,mFAAmF;IACnF,SAAS,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC9B,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,iCAAiC;IACjC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;;;;;;OAOG;IACH,YAAY,CAAC,EACT,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GACtB,GAAG,CAAC,UAAU,GAAG,MAAM,EAAE,MAAM,CAAC,GAChC,KAAK,CAAC,CAAC,UAAU,GAAG,MAAM,EAAE,MAAM,CAAC,CAAC,GACpC,KAAK,CAAC;QAAE,UAAU,EAAE,UAAU,GAAG,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CAC/D"}
@@ -1,9 +1,17 @@
1
- import type { Model } from "./model.js";
1
+ import type { Model, LogicalConstraint } from "./model.js";
2
2
  import { Var } from "./variable.js";
3
3
  import { BoolVar, type LogicElement } from "./boolean-expr.js";
4
4
  import { Constraint } from "./constraint.js";
5
5
  import { LinExpr } from "./expression.js";
6
6
  import { QuadExpr } from "./quad-expr.js";
7
+ /**
8
+ * Registers an internal mutation callback for a Model instance.
9
+ */
10
+ export declare function registerModelMutationCallback(model: object, onMutate: () => void): void;
11
+ /**
12
+ * Notifies an owning model that one of its entities was mutated.
13
+ */
14
+ export declare function notifyModelMutation(ownerModel: object | null | undefined): void;
7
15
  /**
8
16
  * Recursively attaches ownerModel mutation tracking to any Constraint nodes embedded in a logical expression.
9
17
  */
@@ -49,8 +57,24 @@ export declare function validateLinearConstraint(c: Constraint, modelName: strin
49
57
  * Validates a logical constraint element recursively.
50
58
  */
51
59
  export declare function validateLogicElement(elem: LogicElement, lcName: string, modelName: string, isOwnedVar: (v: Var) => boolean, isOwnedBoolVar: (bv: BoolVar) => boolean, ownerModel?: Model): void;
60
+ /**
61
+ * Internal snapshot passed to `validateModelIntegrity` so `Model` keeps backing arrays and lookup maps private.
62
+ */
63
+ export interface ModelIntegritySnapshot {
64
+ model: Model;
65
+ name: string;
66
+ variables: readonly Var[];
67
+ boolVars: readonly BoolVar[];
68
+ constraints: readonly Constraint[];
69
+ logicalConstraints: readonly LogicalConstraint[];
70
+ objectiveExpr: LinExpr | QuadExpr;
71
+ getVarByName: (name: string) => Var | undefined;
72
+ getBoolVarByName: (name: string) => BoolVar | undefined;
73
+ getConstraintByName: (name: string) => Constraint | undefined;
74
+ getLogicalConstraintByName: (name: string) => LogicalConstraint | undefined;
75
+ }
52
76
  /**
53
77
  * Validates the structural and numerical integrity of an optimization model.
54
78
  */
55
- export declare function validateModelIntegrity(model: Model): void;
79
+ export declare function validateModelIntegrity(snapshot: ModelIntegritySnapshot): void;
56
80
  //# sourceMappingURL=validation.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../../src/modeler/validation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EA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,EAAE,MAAM,EACjB,UAAU,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,OAAO,EAC/B,cAAc,EAAE,CAAC,EAAE,EAAE,OAAO,KAAK,OAAO,EACxC,UAAU,CAAC,EAAE,KAAK,GACjB,IAAI,CA6EN;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC,KAAK,EAAE,KAAK,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,SAAS,GAAG,EAAE,CAAC;IAC1B,QAAQ,EAAE,SAAS,OAAO,EAAE,CAAC;IAC7B,WAAW,EAAE,SAAS,UAAU,EAAE,CAAC;IACnC,kBAAkB,EAAE,SAAS,iBAAiB,EAAE,CAAC;IACjD,aAAa,EAAE,OAAO,GAAG,QAAQ,CAAC;IAClC,YAAY,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,GAAG,GAAG,SAAS,CAAC;IAChD,gBAAgB,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,GAAG,SAAS,CAAC;IACxD,mBAAmB,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,UAAU,GAAG,SAAS,CAAC;IAC9D,0BAA0B,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,iBAAiB,GAAG,SAAS,CAAC;CAC7E;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,sBAAsB,GAAG,IAAI,CAmG7E"}
@@ -11,7 +11,7 @@ export interface VarOptions {
11
11
  /**
12
12
  * Optional finite primal initial value (MIP start / warm-start hint), distinct from solved `Var.value`.
13
13
  * Validated within tolerance `1e-6` against `[lb, ub]` and integer/binary domains.
14
- * Supported by HiGHS (`supportsWarmStart: true`); throws on solvers without warm-start support (GLPK, Z3).
14
+ * Supported by HiGHS and Pumpkin (`supportsWarmStart: true`); throws on solvers without warm-start support (GLPK, Z3).
15
15
  */
16
16
  initialValue?: number;
17
17
  }
@@ -33,9 +33,7 @@ export declare class Var {
33
33
  private _name;
34
34
  readonly prefix: string;
35
35
  hasExplicitName: boolean;
36
- ownerModel: {
37
- notifyMutation?: () => void;
38
- } | null;
36
+ ownerModel: object | null;
39
37
  private _type;
40
38
  private _lb;
41
39
  private _ub;
@@ -54,7 +52,7 @@ export declare class Var {
54
52
  set ub(val: number);
55
53
  /**
56
54
  * Optional finite primal initial value (MIP start / warm-start hint), distinct from solved `Var.value`.
57
- * Supported by HiGHS (`supportsWarmStart: true`); throws on solvers without warm-start support (GLPK, Z3).
55
+ * Supported by HiGHS and Pumpkin (`supportsWarmStart: true`); throws on solvers without warm-start support (GLPK, Z3).
58
56
  */
59
57
  get initialValue(): number | undefined;
60
58
  set initialValue(val: number | undefined);
@@ -82,6 +80,12 @@ export declare class Var {
82
80
  ge(rhs: number | Var | LinExpr | QuadExpr | Constraint, name?: string): Constraint;
83
81
  /** Create an equality constraint: this == rhs */
84
82
  eq(rhs: number | Var | LinExpr | QuadExpr | Constraint, name?: string): Constraint;
83
+ /** Create a discrete strict less-than constraint: this < rhs (normalized to this <= rhs - 1) */
84
+ lt(rhs: number | Var | LinExpr | QuadExpr | Constraint, name?: string): Constraint;
85
+ /** Create a discrete strict greater-than constraint: this > rhs (normalized to this >= rhs + 1) */
86
+ gt(rhs: number | Var | LinExpr | QuadExpr | Constraint, name?: string): Constraint;
87
+ /** Create a discrete not-equal constraint: this != rhs */
88
+ ne(rhs: number | Var | LinExpr | QuadExpr | Constraint, name?: string): Constraint;
85
89
  toString(): string;
86
90
  }
87
91
  //# sourceMappingURL=variable.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"variable.d.ts","sourceRoot":"","sources":["../../../src/modeler/variable.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAC1C,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAA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AAM,CAEjB;CACF"}
@@ -2,17 +2,17 @@
2
2
 
3
3
  | Problem Class | Solver | Key APIs | Links |
4
4
  | :--- | :--- | :--- | :--- |
5
- | Linear Programming (LP) | HiGHS (default) | `m.continuous()`, `m.constraint()`, `expr` (`dot`), `minimize` | <ul><li><a href="https://washi.ohtaman.dev/demo/?preset=diet_sensitivity">Open in Playground</a></li><li><a href="https://github.com/ohtaman/washi/blob/main/examples/node/10-diet-sensitivity.ts">Source Code</a></li></ul> |
5
+ | Linear Programming (LP) | HiGHS (default) | `m.continuous()`, `m.constraint()`, `constr.dual`, `constr.slack`, `var.reducedCost` | <ul><li><a href="https://washi.ohtaman.dev/demo/?preset=diet_sensitivity">Open in Playground</a></li><li><a href="https://github.com/ohtaman/washi/blob/main/examples/node/10-diet-sensitivity.ts">Source Code</a></li></ul> |
6
6
 
7
- When solving a Linear Programming model, the optimal variable values (`var.value`) are only half the story. In practice, decision-makers need to know:
7
+ After solving a Linear Programming (LP) model, **sensitivity analysis** shows how the optimal cost responds to changes in requirements, caps, or unit prices:
8
8
 
9
- 1. **Bottlenecks (`constr.dual` — Shadow Price):** Which minimum requirements are actively driving up the total cost, and by how much per unit?
10
- 2. **Surpluses (`constr.slack`):** Which constraints have room to spare?
11
- 3. **Reduced Costs (`var.reducedCost`):** For items not selected in the optimal plan, how much cheaper would they need to become before entering the solution?
9
+ 1. **Shadow Price (`constr.dual`):** Marginal change in optimal cost when a constraint's right-hand side increases by 1 unit.
10
+ 2. **Slack (`constr.slack`):** Spare margin (or surplus) on a constraint, where `0` means the constraint is binding.
11
+ 3. **Reduced Cost (`var.reducedCost`):** Marginal change in optimal cost when a variable fixed at its lower or upper bound is increased by 1 unit.
12
12
 
13
13
  ## Problem Inputs
14
14
 
15
- We choose daily servings ($0 \le x_i \le 4$) among 6 food items to meet 4 daily nutritional minimums (`Calories >= 2000 kcal`, `Protein >= 105 g`, `Calcium >= 1000 mg`, `Iron >= 18 mg`) at minimum cost:
15
+ We determine daily servings ($0 \le x_i \le 4$, continuous) for each of 6 foods to minimize daily cost. The plan must meet 4 daily minimums: at least `2000 kcal` of calories, `105 g` of protein, `1000 mg` of calcium, and `18 mg` of iron.
16
16
 
17
17
  | Food Item | Cost / Serving | Calories (kcal) | Protein (g) | Calcium (mg) | Iron (mg) | Max Servings |
18
18
  | :--- | :---: | :---: | :---: | :---: | :---: | :---: |
@@ -42,7 +42,7 @@ const foods = [
42
42
  async function runDietSensitivity() {
43
43
  const m = new Model("DietSensitivity");
44
44
 
45
- // Continuous servings per day (0 to 4 servings max per food)
45
+ // Continuous servings per day (0 to 4 servings per food)
46
46
  const srv = m.continuous(foods.map((f) => f.name), { lb: 0, ub: 4 });
47
47
 
48
48
  const costs = Object.fromEntries(foods.map((f) => [f.name, f.cost]));
@@ -51,10 +51,10 @@ async function runDietSensitivity() {
51
51
  const calcs = Object.fromEntries(foods.map((f) => [f.name, f.calcium]));
52
52
  const irons = Object.fromEntries(foods.map((f) => [f.name, f.iron]));
53
53
 
54
- // Objective: Minimize total daily cost via dot() inside expr
54
+ // Objective: Minimize total daily cost
55
55
  m.objective(minimize(expr`dot(${costs}, ${srv})`));
56
56
 
57
- // Nutritional constraints defined declaratively with expr and constraint()
57
+ // Nutritional minimum constraints
58
58
  const nutConstraints = [
59
59
  m.constraint(expr`dot(${cals}, ${srv}) >= 2000`, "min_calories"),
60
60
  m.constraint(expr`dot(${prots}, ${srv}) >= 105`, "min_protein"),
@@ -83,9 +83,28 @@ runDietSensitivity();
83
83
 
84
84
  ## Interpretation of Results
85
85
 
86
- - **Bottleneck constraints (`slack == 0`, `dual > 0`):** Calories (`2000 kcal`) and Iron (`18 mg`) are tight at their lower bounds. Increasing the daily Iron requirement by 1 mg increases the minimum daily diet cost by **$0.3241**.
87
- - **Constraints with surplus (`slack > 0`, `dual == 0`):** Protein (`+46.46 g` surplus) and Calcium (`+483.02 mg` surplus) are already exceeded with room to spare, so their shadow prices are `$0`.
88
- - **Reduced cost at lower bound (`Salmon`):** Unselected `Salmon` (`0.00` servings) has a reduced cost of `+1.5125`, meaning its price per serving would need to drop by at least **$1.51** before it enters the optimal daily menu.
89
- - **Negative reduced cost at upper bound (`Oats`, `Eggs`):** Both `Oats` (`reducedCost = -0.2581`) and `Eggs` (`reducedCost = -0.0887`) are pinned against their upper serving cap (`ub = 4.0`). In a minimization LP, a negative reduced cost on an upper-bounded variable tells you how much the total objective would **decrease** per unit increase in `ub`—raising the daily cap on `Oats` from 4 to 5 servings would save **$0.26/day**.
90
- - **Basis stability range:** Shadow prices (`dual`) and reduced costs (`reducedCost`) represent local marginal rates of change within the current optimal simplex basis. If a right-hand side or bound changes enough to trigger a basis change (for example, once another variable hits its bound), the marginal rate shifts to a new piece of the piecewise-linear optimal cost curve.
86
+ HiGHS returns `"Optimal"` with a minimum daily cost of **`$13.66`** (`Oats`, `Eggs`, and `Milk` at `4.00` servings each, `Chicken` at `1.55`, `Spinach` at `0.50`, and `Salmon` at `0.00`).
91
87
 
88
+ ### Bottleneck Constraints (`slack == 0`, `dual > 0`)
89
+
90
+ Both `min_calories` and `min_iron` are binding at their lower bounds, with `dual = $0.0091/kcal` for calories and `dual = $0.3241/mg` for iron. Raising the daily iron target by `1 mg` increases the minimum daily cost by `$0.3241`.
91
+
92
+ ### Constraints with Surplus (`|slack| > 0`, `dual == 0`)
93
+
94
+ For `>=` constraints, HiGHS reports `slack` as `rhs - lhs`, returning negative values (`-46.46` for protein and `-483.02` for calcium). The code prints `Math.abs(c.slack)` as the surplus (`46.46 g` of protein and `483.02 mg` of calcium). Because both targets have spare room, their shadow prices are `0`.
95
+
96
+ ### Reduced Cost at the Lower Bound (`Salmon`)
97
+
98
+ `Salmon` is unselected (`value = 0.00`) with `reducedCost = +1.5125`. Its price per serving would need to drop by at least `$1.5125` (about `$1.51`) before entering the optimal menu.
99
+
100
+ ### Negative Reduced Cost at the Upper Bound (`Oats`, `Eggs`, `Milk`)
101
+
102
+ Three foods hit the daily cap of `ub = 4.0`. In a minimization LP, an upper-bounded variable has a non-positive reduced cost, indicating the marginal cost change per unit increase in `ub` (its magnitude is the saving per added serving):
103
+
104
+ - `Oats`: `reducedCost = -1.3546` (raising `ub` by 1 serving saves about `$1.35/day`)
105
+ - `Eggs`: `reducedCost = -1.0307` (raising `ub` by 1 serving saves about `$1.03/day`)
106
+ - `Milk`: `reducedCost = -0.2294` (raising `ub` by 1 serving saves about `$0.23/day`)
107
+
108
+ ### Basis Stability Range
109
+
110
+ Both `dual` and `reducedCost` are local marginal rates valid within the current optimal simplex basis. If a bound or right-hand side changes enough to switch which constraints or bounds are active, these marginal rates shift to new values.
@@ -4,17 +4,22 @@
4
4
  | :--- | :--- | :--- | :--- |
5
5
  | Mixed-Integer Programming (MIP) | HiGHS (default) | `m.binary()`, `m.constraint()`, `m.indicator()`, `expr`, `minimize` | <ul><li><a href="https://washi.ohtaman.dev/demo/?preset=cart_coupon">Open in Playground</a></li><li><a href="https://github.com/ohtaman/washi/blob/main/examples/node/12-ecommerce-coupon-optimizer.ts">Source Code</a></li></ul> |
6
6
 
7
- Modern e-commerce checkouts often feature complex promotional rules: minimum spend thresholds, category-specific discounts, stackable vs. mutually exclusive coupons, and maximum coupon limits per order. Running an optimizer directly in the browser (or an edge worker) lets the checkout UI automatically apply the best coupon combination—and even recommend a small "filler" item when crossing a spend threshold lowers the customer's net bill.
7
+ E-commerce checkouts often combine minimum spend thresholds, category discounts, mutually exclusive coupons, and per-order coupon caps. Formulating these rules as a single MIP model selects the best coupon set and simultaneously determines whether adding a low-cost filler item lowers the final bill.
8
8
 
9
- This example demonstrates **`m.indicator(binVar, constraint, { activeValue, M? })`**, which automatically linearizes conditional *"if coupon is selected, minimum spend must hold"* constraints using Big-M reformulation for MIP solvers like HiGHS. When `M` is omitted, WaSHI automatically derives safe, tight Big-M values from finite variable bounds.
9
+ This example uses `m.indicator(binVar, constraint, { activeValue, M? })` to model conditional rules such as "if a coupon is applied, minimum spend must hold." For MIP solvers like HiGHS, WaSHI applies **Big-M linearization**, relaxing the inequality by a constant `M` when the binary indicator is inactive.
10
+
11
+ > [!IMPORTANT]
12
+ > **Precondition for omitting `M`**: WaSHI derives a tight Big-M automatically only when every variable in the constrained inequality has **finite** `lb` and `ub` bounds (binary variables are automatically bounded in `[0, 1]`). If any variable is unbounded, pass an explicit `M` or set finite bounds first.
10
13
 
11
14
  <EcommerceCouponDemo />
12
15
 
13
16
  ## Scenario & Promotion Rules
14
17
 
15
- A customer's shopping cart currently contains **$242** of merchandise (**$210** in Tech items and **$32** in Apparel):
16
- - **Optional Filler Add-on:** A **$10** Braided USB-C Cable (Tech) can be added to bring the cart total from `$242` to `$252`.
17
- - **Available Coupons** (maximum **2 coupons** total per checkout; at most **1 Exclusive** coupon):
18
+ While the interactive widget above lets you explore a multi-item catalog, the runnable walkthrough below uses a compact single-cart scenario (`$242`, 4 coupons, and 1 optional add-on).
19
+
20
+ A customer's shopping cart currently contains `$242` of merchandise (`$210` in Tech items and `$32` in Apparel):
21
+ - **Optional Filler Add-on**: Adding a `$10` Braided USB-C Cable (Tech) raises the cart subtotal from `$242` to `$252` (and Tech spend from `$210` to `$220`).
22
+ - **Available Coupons**: At most 2 coupons per checkout; exclusive coupons are mutually exclusive with each other (at most 1), though still combinable with stackable coupons.
18
23
 
19
24
  | Coupon Code | Discount | Condition | Stacking Rule |
20
25
  | :--- | :---: | :--- | :--- |
@@ -23,9 +28,6 @@ A customer's shopping cart currently contains **$242** of merchandise (**$210**
23
28
  | `MEGA45_250` | **-$45** | Total cart spend $\ge \$250$ | **Exclusive** (cannot combine with `TECH30`) |
24
29
  | `MEMBER10` | **-$10** | Total cart spend $\ge \$150$ | **Stackable** |
25
30
 
26
- Without adding the `$10` cable, the cart (`$242`) cannot use `MEGA45_250` and pays `$242 - $30 (TECH30) - $15 (WELCOME15) = $197`.
27
- By adding the `$10` cable (`$252` gross), the customer unlocks `MEGA45_250` (`-$45`) + `WELCOME15` (`-$15`) for a net total of **$192**—saving an extra **$5** while getting an extra item!
28
-
29
31
  ## Implementation in WaSHI
30
32
 
31
33
  <CodeRunner preset="cart_coupon">
@@ -58,7 +60,7 @@ async function optimizeCheckoutCart() {
58
60
  const techSpend = expr`${baseTechSubtotal} + ${usbCablePrice} * ${addUsbCable}`;
59
61
 
60
62
  // 1. Indicator constraints: If useCoupon[id] == 1, minimum spend threshold must hold.
61
- // Omitting `M` lets WaSHI automatically derive the tightest Big-M from variable bounds!
63
+ // Omitting `M` lets WaSHI automatically derive a tight Big-M from variable bounds.
62
64
  for (const cpn of coupons) {
63
65
  if (cpn.minCart > 0) {
64
66
  m.indicator(useCoupon[cpn.id], expr`${merchSpend} >= ${cpn.minCart}`, {
@@ -97,3 +99,9 @@ optimizeCheckoutCart();
97
99
  ```
98
100
 
99
101
  </CodeRunner>
102
+
103
+ ## Key Takeaways
104
+
105
+ - **Without the add-on (`$197`)**: At `$242`, `MEGA45_250` is unavailable, so the best valid pair is `TECH30` (`-$30`) and `WELCOME15` (`-$15`), leaving `$197`.
106
+ - **With the `$10` USB-C cable (`$192`)**: Raising gross spend to `$252` unlocks `MEGA45_250` (`-$45`) alongside `WELCOME15` (`-$15`), bringing the net bill to `$192`—saving `$5` while including the cable.
107
+ - **Why `m.indicator()` fits**: Modeling `addUsbCable` and `useCoupon` in one MIP avoids enumerating cart combinations manually and lets the solver weigh add-on costs against unlocked discounts directly.
@@ -1,46 +1,54 @@
1
1
  # Examples & API Finder
2
2
 
3
- Explore runnable optimization and logic formulations built with WaSHI. Every example can be executed directly inside its documentation page, or edited in the [Playground](https://washi.ohtaman.dev/demo/).
3
+ Explore runnable optimization and logic formulations built with WaSHI. Every example runs directly on its documentation page, and most can also be edited and run in the [Playground](https://washi.ohtaman.dev/demo/).
4
4
 
5
5
  ## Examples
6
6
 
7
- | Example | Problem Class | Solver | Key WaSHI APIs | Description | Code Editor |
7
+ | Example | Problem Class | Solver | Key WaSHI APIs | Description | Playground / Guide |
8
8
  | :--- | :--- | :--- | :--- | :--- | :--- |
9
- | **[Production Planning](./production.md)** | LP / MIP | HiGHS | `m.integer()`, `m.constraint()`, `expr` | Basic variables, tagged template literal equations, and maximizing profit under resource limits. | [Playground](https://washi.ohtaman.dev/demo/?preset=production) |
10
- | **[0-1 Knapsack](./knapsack.md)** | Binary MIP | HiGHS | `m.binary(keys)`, `expr` (`dot`), `m.constraint()` | 1D dictionary of `Binary` variables and linear aggregation over item arrays. | [Playground](https://washi.ohtaman.dev/demo/?preset=knapsack) |
11
- | **[Transportation Network](./transportation.md)** | LP | HiGHS | `m.continuous(rows, cols)`, `expr` (`sum`, `dot`), `m.constraint()` | 2D matrix indexing (`ship[factory][store]`) and row/column flow balance constraints. | [Playground](https://washi.ohtaman.dev/demo/?preset=transportation) |
12
- | **[Diet & Sensitivity Analysis](./diet-sensitivity.md)** | LP | HiGHS | `constr.dual`, `constr.slack`, `var.reducedCost` | Reading shadow prices (bottlenecks), constraint surpluses, and reduced costs after solving. | [Playground](https://washi.ohtaman.dev/demo/?preset=diet_sensitivity) |
13
- | **[Staff Shift Scheduling](./shift-scheduling.md)** | Binary MIP + Unsat Core | HiGHS + Z3 | `m.binary(staff, days)`, `check()`, `unsatCore` | 5×7 weekly roster planner (coverage, senior duty, max shifts, day-off requests) and diagnosing conflicting vacation requests with Z3 `unsatCore`. | [Playground](https://washi.ohtaman.dev/demo/?preset=shift_scheduling) |
14
- | **[E-Commerce Coupon Optimizer](./ecommerce-coupons.md)** | Indicator MIP | HiGHS | `m.indicator()`, `expr` (`sum`, `dot`), `m.constraint()` | Checkout optimizer using `m.indicator()` for minimum spend thresholds, exclusive vs. stackable rules, and smart filler add-on suggestions. | [Playground](https://washi.ohtaman.dev/demo/?preset=cart_coupon) |
15
- | **[Portfolio Optimization](./portfolio-qp.md)** | Convex QP | HiGHS | `m.continuous()`, `m.constraint()`, `dot()`, `QuadExpr` | Markowitz mean-variance risk minimization ($w^T \Sigma w$) using `QuadExpr` subject to return targets. | [Playground](https://washi.ohtaman.dev/demo/?preset=portfolio_qp) |
16
- | **[SMT Security Policies](./smt-security.md)** | SMT Logic | Z3 | `m.bool()`, `m.constraint()`, `m.check()` | Propositional variables (`BoolVar`) and logical connectives (`implies`, `and`, `not`, `xor`) mixed with integer bounds. | [Playground](https://washi.ohtaman.dev/demo/?preset=smt_security) |
17
- | **[Conflict Diagnosis (`unsatCore`)](./unsat-core.md)** | Unsat Core | Z3 | `m.constraint()`, `m.check()`, `m.unsatCore` | Extracting a conflicting constraint subset (`unsatCore`) when staffing and budget rules contradict each other. | [Playground](https://washi.ohtaman.dev/demo/?preset=conflict_diagnosis_unsat) |
18
- | **[Vehicle Routing (CVRP)](./vehicle-routing.md)** | Combinatorial OR / Dantzig-Wolfe | HiGHS | `m.solveColumnGeneration()`, `pricing` | Dynamic route column generation from customer dual shadow prices to minimize total fleet vehicles. | [Decomposition Guide](../guide/decomposition.md) |
19
- | **[Traveling Salesperson (TSP)](./traveling-salesperson.md)** | Combinatorial OR / Benders | HiGHS | `m.solveBenders()`, `subproblem` | Subtour elimination on degree-assignment relaxation using dynamic Benders feasibility cuts. | [Decomposition Guide](../guide/decomposition.md) |
9
+ | **[Production Planning](./production.md)** | Integer Programming (MIP) | HiGHS | `m.integer()`, `m.constraint()`, `expr` | Basic integer variables, tagged template equations, and profit maximization under resource caps. | [Playground](https://washi.ohtaman.dev/demo/?preset=production) |
10
+ | **[0-1 Knapsack Problem](./knapsack.md)** | Binary MIP | HiGHS | `m.binary(keys)`, `expr` (`dot`), `m.constraint()` | 1D dictionary of binary variables and weighted aggregation over item arrays. | [Playground](https://washi.ohtaman.dev/demo/?preset=knapsack) |
11
+ | **[Transportation & Allocation Network](./transportation.md)** | LP | HiGHS | `m.continuous(rows, cols)`, `expr` (`sum`, `dot`), `m.constraint()` | 2D matrix indexing (`ship[factory][store]`) and row/column supply-cap and demand-satisfaction constraints. | [Playground](https://washi.ohtaman.dev/demo/?preset=transportation) |
12
+ | **[Diet Problem & Sensitivity Analysis](./diet-sensitivity.md)** | LP | HiGHS | `constr.dual`, `constr.slack`, `var.reducedCost` | Reading dual values (shadow prices), constraint slacks, and reduced costs after solving. | [Playground](https://washi.ohtaman.dev/demo/?preset=diet_sensitivity) |
13
+ | **[Staff Shift Scheduling](./shift-scheduling.md)** | Binary MIP + Unsat Core | HiGHS + Z3* | `m.binary(staff, days)`, `m.check()`, `m.unsatCore` | 5×7 weekly roster optimization and diagnosing conflicting day-off requests with Z3 `unsatCore`. | [Playground](https://washi.ohtaman.dev/demo/?preset=shift_scheduling) |
14
+ | **[E-Commerce Cart & Coupon Optimizer](./ecommerce-coupons.md)** | Indicator MIP | HiGHS | `m.indicator()`, `expr` (`sum`, `dot`), `m.constraint()` | Checkout optimization with minimum spend rules, exclusive coupons, and optional add-on evaluation. | [Playground](https://washi.ohtaman.dev/demo/?preset=cart_coupon) |
15
+ | **[Portfolio Optimization (QP)](./portfolio-qp.md)** | Convex QP | HiGHS | `m.continuous()`, `m.constraint()`, `dot()`, `QuadExpr` | Markowitz mean-variance risk minimization ($\mathbf{w}^T \boldsymbol{\Sigma} \mathbf{w}$) with `QuadExpr`. | [Playground](https://washi.ohtaman.dev/demo/?preset=portfolio_qp) |
16
+ | **[Project Scheduling (CP)](./project-scheduling.md)** | CP + Unsat Core | Pumpkin | `m.integer()`, `disjoint()`, `cumulative()`, `max()`, `m.check()` | 6-task resource-constrained schedule with a shared rig, 3-engineer crew cap, and deadline conflict diagnosis. | [Playground](https://washi.ohtaman.dev/demo/?preset=project_scheduling) |
17
+ | **[SMT Security Policies](./smt-security.md)** | SMT | Z3* | `m.bool()`, `implies()`, `.and()`, `not()`, `m.check()` | Propositional `BoolVar` rules (`implies()`, `.and()`, `not()`) combined with integer sensor bounds. | [Playground](https://washi.ohtaman.dev/demo/?preset=smt_security) |
18
+ | **[Conflict Diagnosis (Unsat Core)](./unsat-core.md)** | SMT / MIP + Unsat Core | Z3* + HiGHS | `m.constraint()`, `m.check()`, `m.conflicts` | Extracting a conflicting constraint subset when staffing and budget rules contradict each other. | [Playground](https://washi.ohtaman.dev/demo/?preset=conflict_diagnosis_unsat) |
19
+ | **[Vehicle Routing (CVRP)](./vehicle-routing.md)** | Combinatorial OR / Dantzig-Wolfe | HiGHS | `m.solveColumnGeneration()`, `pricing`, `constr.dual` | Dynamic route generation from dual values (shadow prices) to minimize fleet size. | [Decomposition Methods](../guide/decomposition.md) |
20
+ | **[Traveling Salesperson (TSP)](./traveling-salesperson.md)** | Combinatorial OR / Benders | HiGHS | `m.solveBenders()`, `subproblem` | Subtour elimination on the degree-assignment relaxation using dynamic Benders feasibility cuts. | [Decomposition Methods](../guide/decomposition.md) |
21
+
22
+ > [!NOTE]
23
+ > **Solver Preconditions**: HiGHS and Pumpkin are bundled with WaSHI. Examples marked `Z3*` require the optional peer package `z3-solver` (and `COOP`/`COEP` headers in browsers). Examples using GLPK require `glpk.js` (GPL-3.0 license).
20
24
 
21
25
  ## Find by WaSHI API (Reverse Index)
22
26
 
23
- Looking for a specific modeling method or solver capability? Jump straight to the example that demonstrates it:
27
+ Looking for a specific modeling method or solver feature? Jump directly to the matching example or guide:
24
28
 
25
- | Feature / API | Description | Example Page | Node.js Source |
29
+ | Feature / API | Description | Example / Guide | Node.js Source |
26
30
  | :--- | :--- | :--- | :--- |
27
31
  | ``m.constraint(expr`...`)`` | Tagged template literal constraints | [Production Planning](./production.md) | [`01-production-planning.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/01-production-planning.ts) |
28
- | `m.binary(keys)` / `m.continuous(keys)` | 1D dictionary of indexed variables | [0-1 Knapsack](./knapsack.md) | [`02-knapsack-problem.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/02-knapsack-problem.ts) |
29
- | `m.continuous(rows, cols)` / `m.binary(rows, cols)` | 2D matrix of indexed variables | [Transportation Network](./transportation.md), [Shift Scheduling](./shift-scheduling.md) | [`09-transportation-problem.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/09-transportation-problem.ts) |
30
- | ``expr`dot(${A}, ${X})` `` / `dot(A, X)` | Fast vector/matrix dot product | [Transportation Network](./transportation.md), [Portfolio Optimization](./portfolio-qp.md) | [`09-transportation-problem.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/09-transportation-problem.ts) |
31
- | `constr.dual` & `constr.slack` | Shadow prices & constraint surpluses | [Diet & Sensitivity Analysis](./diet-sensitivity.md) | [`10-diet-sensitivity.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/10-diet-sensitivity.ts) |
32
- | `var.reducedCost` | Variable reduced costs in LP | [Diet & Sensitivity Analysis](./diet-sensitivity.md) | [`10-diet-sensitivity.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/10-diet-sensitivity.ts) |
33
- | `m.indicator()` | Big-M conditional constraints (`binVar => constr`) | [E-Commerce Coupon Optimizer](./ecommerce-coupons.md) | [`12-ecommerce-coupon-optimizer.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/12-ecommerce-coupon-optimizer.ts) |
34
- | `new QuadExpr()` | Convex quadratic objectives ($x^T Q x$) | [Portfolio Optimization](./portfolio-qp.md) | [`03-portfolio-qp.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/03-portfolio-qp.ts) |
32
+ | `m.binary(keys)` / `m.continuous(keys)` | 1D dictionary of indexed variables | [0-1 Knapsack Problem](./knapsack.md) | [`02-knapsack-problem.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/02-knapsack-problem.ts) |
33
+ | `m.continuous(rows, cols)` / `m.binary(rows, cols)` | 2D matrix of indexed variables | [Transportation & Allocation Network](./transportation.md), [Staff Shift Scheduling](./shift-scheduling.md) | [`09-transportation-problem.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/09-transportation-problem.ts), [`11-shift-scheduling.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/11-shift-scheduling.ts) |
34
+ | ``expr`dot(${A}, ${X})` `` / `dot(A, X)` | Vector and matrix dot product | [0-1 Knapsack Problem](./knapsack.md), [Transportation & Allocation Network](./transportation.md), [Portfolio Optimization (QP)](./portfolio-qp.md) | [`02-knapsack-problem.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/02-knapsack-problem.ts), [`09-transportation-problem.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/09-transportation-problem.ts) |
35
+ | `constr.dual` & `constr.slack` | Dual values (shadow prices) & constraint slacks | [Diet Problem & Sensitivity Analysis](./diet-sensitivity.md) | [`10-diet-sensitivity.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/10-diet-sensitivity.ts) |
36
+ | `var.reducedCost` | Variable reduced costs in LP | [Diet Problem & Sensitivity Analysis](./diet-sensitivity.md) | [`10-diet-sensitivity.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/10-diet-sensitivity.ts) |
37
+ | `m.indicator()` | Big-M conditional constraints (`binVar => constr`) | [E-Commerce Cart & Coupon Optimizer](./ecommerce-coupons.md) | [`12-ecommerce-coupon-optimizer.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/12-ecommerce-coupon-optimizer.ts) |
38
+ | `new QuadExpr()` | Quadratic objectives ($\mathbf{x}^T \mathbf{Q} \mathbf{x}$, for convex QP) | [Portfolio Optimization (QP)](./portfolio-qp.md) | [`03-portfolio-qp.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/03-portfolio-qp.ts) |
35
39
  | `m.bool()` & `implies()` | Propositional logic & SMT rules | [SMT Security Policies](./smt-security.md) | [`04-smt-security-rules.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/04-smt-security-rules.ts) |
36
- | `m.check()` & `m.unsatCore` | Feasibility check & conflict diagnosis | [Conflict Diagnosis](./unsat-core.md), [Shift Scheduling](./shift-scheduling.md) | [`05-unsat-core-diagnosis.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/05-unsat-core-diagnosis.ts) |
37
- | `m.solveColumnGeneration()` | Dantzig-Wolfe Column Generation | [Vehicle Routing](./vehicle-routing.md) | [`13-vehicle-routing-column-generation.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/13-vehicle-routing-column-generation.ts) |
38
- | `m.solveBenders()` | Benders Decomposition & cut separation | [Traveling Salesperson](./traveling-salesperson.md) | [`14-traveling-salesperson-benders.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/14-traveling-salesperson-benders.ts) |
39
- | `solver: "glpk"` | Solving LP/MIP with GLPK plugin | [GLPK Solver Guide](../solvers/glpk.md) | [`08-glpk-solver-plugin.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/08-glpk-solver-plugin.ts) |
40
+ | `m.check()` & `m.conflicts` | Feasibility check & conflict diagnosis (`conflicts` / `unsatCore`) | [Conflict Diagnosis (Unsat Core)](./unsat-core.md), [Staff Shift Scheduling](./shift-scheduling.md) | [`05-unsat-core-diagnosis.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/05-unsat-core-diagnosis.ts), [`11-shift-scheduling.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/11-shift-scheduling.ts) |
41
+ | Hybrid Z3 SMT + HiGHS MIP | Policy check with Z3 before MIP capacity allocation | [Z3 Solver Guide](../solvers/z3.md) | [`07-hybrid-smt-mip.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/07-hybrid-smt-mip.ts) |
42
+ | `m.solveColumnGeneration()` | Dantzig-Wolfe Column Generation | [Vehicle Routing (CVRP)](./vehicle-routing.md) | [`13-vehicle-routing-column-generation.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/13-vehicle-routing-column-generation.ts) |
43
+ | `m.solveBenders()` | Benders Decomposition & lazy cuts | [Traveling Salesperson (TSP)](./traveling-salesperson.md) | [`14-traveling-salesperson-benders.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/14-traveling-salesperson-benders.ts) |
44
+ | `disjoint()` / `cumulative()` / `max()` | Scheduling global constraints & makespan objective | [Project Scheduling (CP)](./project-scheduling.md), [Constraint Programming (CP) Modeling](../guide/constraint-programming.md) | [`15-project-scheduling-cp.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/15-project-scheduling-cp.ts) |
45
+ | `distinct()` / `allowed()` / `at()` | CP value-domain, table, and element constraints | [Constraint Programming (CP) Modeling](../guide/constraint-programming.md), [Expressions](../guide/expressions.md) | — |
46
+ | `solver: "pumpkin"` | Bundled CP scheduling & discrete `unsatCore` | [Project Scheduling (CP)](./project-scheduling.md), [Pumpkin Guide](../solvers/pumpkin.md) | [`15-project-scheduling-cp.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/15-project-scheduling-cp.ts) |
47
+ | `solver: "glpk"` | LP/MIP via optional `glpk.js` plugin (GPL-3.0) | [GLPK Guide](../solvers/glpk.md) | [`08-glpk-solver-plugin.ts`](https://github.com/ohtaman/washi/blob/main/examples/node/08-glpk-solver-plugin.ts) |
40
48
 
41
49
  ## Running Examples Locally in Node.js
42
50
 
43
- All standalone scripts are located in [`examples/node/`](https://github.com/ohtaman/washi/tree/main/examples/node):
51
+ All standalone scripts are located in [`examples/node/`](https://github.com/ohtaman/washi/tree/main/examples/node) (examples using Z3 or GLPK require the optional `z3-solver` or `glpk.js` package):
44
52
 
45
53
  ```bash
46
54
  npx tsx examples/node/01-production-planning.ts
@@ -56,4 +64,5 @@ npx tsx examples/node/11-shift-scheduling.ts
56
64
  npx tsx examples/node/12-ecommerce-coupon-optimizer.ts
57
65
  npx tsx examples/node/13-vehicle-routing-column-generation.ts
58
66
  npx tsx examples/node/14-traveling-salesperson-benders.ts
67
+ npx tsx examples/node/15-project-scheduling-cp.ts
59
68
  ```
@@ -4,16 +4,16 @@
4
4
  | :--- | :--- | :--- | :--- |
5
5
  | 0-1 Binary MIP | HiGHS (default) | `m.binary()`, `m.constraint()`, `expr` (`dot`), `maximize` | <ul><li><a href="https://washi.ohtaman.dev/demo/?preset=knapsack">Open in Playground</a></li><li><a href="https://github.com/ohtaman/washi/blob/main/examples/node/02-knapsack-problem.ts">Source Code</a></li></ul> |
6
6
 
7
- The 0-1 Knapsack problem is an essential combinatorial optimization problem: given a set of items, each with a weight and a value, determine which items to include in a collection so that the total weight does not exceed a given limit and the total value is maximized.
7
+ The 0-1 Knapsack problem is a fundamental combinatorial optimization problem. Given items with weights and values, choose a subset that maximizes total value without exceeding the backpack's weight limit.
8
8
 
9
9
  ## Mathematical Formulation
10
10
 
11
- Let $x_i \in \{0, 1\}$ be binary decision variables indicating whether item $i$ is selected ($x_i = 1$) or not ($x_i = 0$):
11
+ Let $v_i$ and $w_i$ be the value and weight of item $i$, $W$ be the weight capacity, and $x_i \in \{0, 1\}$ be a binary decision variable where $x_i = 1$ selects the item and $x_i = 0$ leaves it out:
12
12
 
13
13
  $$\begin{aligned}
14
14
  \text{Maximize} \quad & \sum_{i} v_i x_i & (\text{Total Value}) \\
15
15
  \text{Subject to} \quad & \sum_{i} w_i x_i \le W & (\text{Weight Capacity}) \\
16
- & x_i \in \{0, 1\} & (\text{Binary Decisions})
16
+ & x_i \in \{0, 1\} \quad \forall i & (\text{Binary Decisions})
17
17
  \end{aligned}$$
18
18
 
19
19
  ## Implementation in WaSHI
@@ -26,6 +26,7 @@ import { Model, maximize, expr } from "@ohtaman/washi";
26
26
  async function solveKnapsack() {
27
27
  const m = new Model("Knapsack");
28
28
 
29
+ // weight in kg, value in $
29
30
  const items = [
30
31
  { name: "Laptop", weight: 3, value: 50 },
31
32
  { name: "Camera", weight: 2, value: 40 },
@@ -41,10 +42,10 @@ async function solveKnapsack() {
41
42
  const values = Object.fromEntries(items.map((it) => [it.name, it.value]));
42
43
  const weights = Object.fromEntries(items.map((it) => [it.name, it.weight]));
43
44
 
44
- // Maximize total value via dot() inside expr
45
+ // Objective: Maximize total value
45
46
  m.objective(maximize(expr`dot(${values}, ${x})`));
46
47
 
47
- // Weight capacity constraint using expr and constraint()
48
+ // Weight capacity constraint
48
49
  m.constraint(expr`dot(${weights}, ${x}) <= ${maxCapacity}`, "capacity_limit");
49
50
 
50
51
  const status = await m.solve();
@@ -59,3 +60,8 @@ solveKnapsack();
59
60
  ```
60
61
 
61
62
  </CodeRunner>
63
+
64
+ ## Key Takeaways
65
+
66
+ - **Optimal Result**: Packing `Camera` (`2 kg`, `$40`), `Drone` (`4 kg`, `$65`), and `FirstAid` (`1 kg`, `$15`) reaches the exact `7 kg` capacity with a maximum value of `$120`.
67
+ - **Concise Modeling with `m.binary(names)` and `dot()`**: Passing item names to `m.binary()` creates a keyed dictionary of 0-1 variables in one call. Pairing that dictionary with `dot()` inside `expr` computes weighted sums directly without manual loops.