@ohtaman/washi 0.1.0 → 0.1.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.ja.md +72 -4
- package/README.md +72 -4
- package/README.zh-CN.md +72 -4
- package/dist/browser/_washi-optional-engine-282Ug7Gw.js +7 -0
- package/dist/browser/highs-browser-runtime-D13gHOT5.js +4189 -0
- package/dist/browser/highs-loader-T47YOyos.js +58 -0
- package/dist/browser/highs-worker.js +21 -0
- package/dist/browser/highs.wasm +0 -0
- package/dist/browser/highs.wasm.LICENSE +22 -0
- package/dist/browser/washi.js +1547 -0
- package/dist/browser/washi.min.js +5 -0
- package/dist/lib/{glpk-BC32ZaPG.js → glpk-5cUpjyb9.js} +14 -5
- package/dist/lib/highs-assets.js +1 -0
- package/dist/lib/highs-browser-runtime.js +4189 -0
- package/dist/lib/highs-loader-Blb99785.js +50 -0
- package/dist/lib/highs-node-runtime.js +1 -0
- package/dist/lib/highs-worker-factory.js +1 -0
- package/dist/lib/highs-worker.js +21 -0
- package/dist/lib/highs.wasm +0 -0
- package/dist/lib/solvers/glpk.js +1 -1
- package/dist/lib/solvers/z3.js +2 -2
- package/dist/lib/washi.js +92 -91
- package/dist/lib/z3-C6GeIicJ.js +509 -0
- package/dist/types/glpk-loader.d.ts.map +1 -1
- package/dist/types/highs-assets.d.ts +3 -0
- package/dist/types/highs-assets.d.ts.map +1 -0
- package/dist/types/highs-browser-runtime.d.ts +2 -0
- package/dist/types/highs-browser-runtime.d.ts.map +1 -0
- package/dist/types/highs-loader.d.ts +17 -1
- package/dist/types/highs-loader.d.ts.map +1 -1
- package/dist/types/highs-node-runtime.d.ts +2 -0
- package/dist/types/highs-node-runtime.d.ts.map +1 -0
- package/dist/types/highs-worker-client.d.ts.map +1 -1
- package/dist/types/highs-worker-factory.d.ts +3 -0
- package/dist/types/highs-worker-factory.d.ts.map +1 -0
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/modeler/solvers/highs.d.ts +6 -0
- package/dist/types/modeler/solvers/highs.d.ts.map +1 -1
- package/dist/types/modeler/solvers/types.d.ts +1 -1
- package/dist/types/modeler/solvers/types.d.ts.map +1 -1
- package/dist/types/modeler/solvers/z3.d.ts +4 -5
- package/dist/types/modeler/solvers/z3.d.ts.map +1 -1
- package/package.json +20 -7
- package/dist/lib/assets/highs-worker-D57nd9KC.js +0 -3
- package/dist/lib/z3-C5DfllfD.js +0 -562
package/README.ja.md
CHANGED
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
<a href="./README.zh-CN.md">简体中文</a>
|
|
15
15
|
</p>
|
|
16
16
|
<p>
|
|
17
|
-
<a href="https://ohtaman.github.io/washi/"><img src="https://img.shields.io/badge/
|
|
17
|
+
<a href="https://ohtaman.github.io/washi/"><img src="https://img.shields.io/badge/docs-online-blue" alt="docs" /></a>
|
|
18
|
+
<a href="https://ohtaman.github.io/washi/demo/"><img src="https://img.shields.io/badge/demo-playground-brightgreen" alt="live demo" /></a>
|
|
18
19
|
<a href="https://www.npmjs.com/package/@ohtaman/washi"><img src="https://img.shields.io/npm/v/@ohtaman/washi?color=crimson&label=npm" alt="npm version" /></a>
|
|
19
20
|
<a href="https://bundlephobia.com/package/@ohtaman/washi"><img src="https://img.shields.io/badge/bundle%20size-7.2%20kB%20(gzip)-blue" alt="bundle size" /></a>
|
|
20
21
|
<a href="./LICENSE"><img src="https://img.shields.io/badge/license-MIT-green" alt="license" /></a>
|
|
@@ -50,7 +51,7 @@ console.log(`生産量: x=${x.value}, y=${y.value} (最大利益: $${m.objective
|
|
|
50
51
|
2. 📦 **ゼロ依存・単一パッケージでの圧倒的な配布容易性**: Python のランタイム管理や仮想環境、OS 別の C/C++ ネイティブソルバーバイナリの同梱に悩まされることはありません。`npm install @ohtaman/washi` だけで WebAssembly ソルバーを含めて即座に動作します。
|
|
51
52
|
3. ✍️ **直感的な代数モデリング DX(PuLP / python-mip 互換)**: 泥臭い行列のインデックス操作は不要。テンプレートリテラル記法(`c`${x} + 2 * ${y} <= 40``)や、Python 最適化開発者におなじみの構文(`LpProblem`, `lpSum`)で、教科書そのままの数式を記述できます。
|
|
52
53
|
4. 🔄 **統一マルチソルバー API(HiGHS / Z3 / GLPK)**: 定式化したモデルコードはそのまま、産業用 MIP/QP に強い **HiGHS**、論理制約・SMT に強い **Z3**、学術用途の **GLPK** をオプション1つで柔軟に切り替え可能です。
|
|
53
|
-
5. 🔍 **実行不可能(Infeasible)時の矛盾原因自動診断**:
|
|
54
|
+
5. 🔍 **実行不可能(Infeasible)時の矛盾原因自動診断**: ルール同士が矛盾して解なしになった場合、衝突している制約の部分集合(Unsat Core)を Z3 エンジン経由で特定・診断します。
|
|
54
55
|
6. ⚡ **WebAssembly 上での本格的な産業規模パフォーマンス**: 線形時間 $O(N)$ の高速式ビルダーにより、20,000 変数を超える大規模 MIP を約100msで定式化し、WebAssembly 版 HiGHS で約15秒で高速に求解します。
|
|
55
56
|
|
|
56
57
|
---
|
|
@@ -228,7 +229,7 @@ console.log(`サイレン作動? ${m.values.get("siren")}`); // true
|
|
|
228
229
|
|
|
229
230
|
### 4. 矛盾原因の自動特定 (`unsatCore`)
|
|
230
231
|
|
|
231
|
-
|
|
232
|
+
業務ルールや制約群が互いに矛盾している場合、**矛盾を引き起こしている制約の部分集合**(Unsat Core。最小性は保証しません)を特定してデバッグ(Z3 が必要):
|
|
232
233
|
|
|
233
234
|
```ts
|
|
234
235
|
import { Model } from "@ohtaman/washi";
|
|
@@ -297,6 +298,72 @@ console.log(`x = ${value(x)}, y = ${value(y)}`);
|
|
|
297
298
|
|
|
298
299
|
---
|
|
299
300
|
|
|
301
|
+
## 🌐 ブラウザ & Web Worker での利用方法
|
|
302
|
+
|
|
303
|
+
HiGHS はデフォルトでパッケージ同梱 WASM を使います。**CDN への自動フォールバックはありません。** Node.js はインストールされた HiGHS を使い、Vite / webpack 5 はパッケージ内の WASM・Worker 参照から配布ファイルを生成します。
|
|
304
|
+
|
|
305
|
+
### npm とバンドラー
|
|
306
|
+
|
|
307
|
+
```typescript
|
|
308
|
+
import { Model } from "@ohtaman/washi";
|
|
309
|
+
const model = new Model();
|
|
310
|
+
// 変数・目的関数・制約を定義
|
|
311
|
+
await model.solve();
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
検証対象の Vite / webpack ビルドでは WASM の手動コピー・URL 指定は不要です。その他のバンドラーや SSR は個別に確認し、ブラウザの求解はクライアントで実行してください。ブラウザでは Worker をデフォルトで使用します。簡単なデモは `model.solve({ useWorker: false })` でも動きますが、長い計算中は UI が止まります。
|
|
315
|
+
|
|
316
|
+
### ブラウザ標準 ESM(利用者側のビルド不要)
|
|
317
|
+
|
|
318
|
+
`dist/browser` 一式(JS の分割ファイル・`highs.wasm`・Worker を含む)を HTTP 配信します。Import Maps は不要です。
|
|
319
|
+
|
|
320
|
+
```html
|
|
321
|
+
<script type="module">
|
|
322
|
+
import { Model } from "./washi/washi.js";
|
|
323
|
+
const model = new Model();
|
|
324
|
+
// 変数・目的関数・制約を定義
|
|
325
|
+
await model.solve({ useWorker: false });
|
|
326
|
+
</script>
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
### 通常の script / CodePen
|
|
330
|
+
|
|
331
|
+
`dist/browser/washi.min.js` はグローバル API `Washi` を公開します。CodePen の External Scripts にその URL を追加するか、次のように読み込みます。
|
|
332
|
+
|
|
333
|
+
```html
|
|
334
|
+
<script src="./washi/washi.min.js"></script>
|
|
335
|
+
<script>
|
|
336
|
+
const model = new Washi.Model();
|
|
337
|
+
// 変数・目的関数・制約を定義
|
|
338
|
+
model.solve({ useWorker: false }).then(status => console.log(status));
|
|
339
|
+
</script>
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
CDN から直接読み込む場合は、URL を `https://cdn.jsdelivr.net/npm/@ohtaman/washi@0.1.2/dist/browser/washi.min.js`(`0.1.2` 以降のバージョンを固定)に指定できます。ESM は同じ版の `dist/browser/washi.js` を指定します。CDN の URL を選ぶことが明示的な CDN 利用になり、ローカル配信が勝手に CDN へ切り替わることはありません。
|
|
343
|
+
|
|
344
|
+
ブラウザ用ビルドに含まれるエンジンは HiGHS です。GLPK / Z3 エンジンの連携には npm パッケージと任意依存を使ってください。完結した利用例は `examples/distribution/` にあります。
|
|
345
|
+
|
|
346
|
+
### 工場内・オフライン配布、Worker、自前配信
|
|
347
|
+
|
|
348
|
+
アプリのビルド出力一式、または `dist/browser` 一式をローカル HTTP サーバー・社内サーバーに配布します。必要なアセットがローカルにあれば、ブラウザのキャッシュが空でも外部ネットワークは不要です。HTML や最初の JS だけでは不足します。`file://` 実行や PWA の自動キャッシュは対象外です。
|
|
349
|
+
|
|
350
|
+
ESM はモジュール URL、script 版は読み込み時に保持した script URL からアセットを解決します。CDN の Worker は Blob のモジュールから起動します。配信元の CORS と、CSP の script・通信先許可が必要です。CDN Worker では `worker-src blob:` も許可してください。使えない場合は `useWorker: false` を指定します。Worker の読み込み・メッセージ処理に失敗した場合は、求解 Promise がエラーになります。
|
|
351
|
+
|
|
352
|
+
自前 URL は `setHighsWasmUrl(url)` / `options.wasmUrl` で指定できます。従来の HiGHS CDN を明示的に選ぶ場合:
|
|
353
|
+
|
|
354
|
+
```typescript
|
|
355
|
+
import { setHighsWasmUrl, OFFICIAL_HIGHS_CDN_WASM_URL } from "@ohtaman/washi";
|
|
356
|
+
setHighsWasmUrl(OFFICIAL_HIGHS_CDN_WASM_URL);
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
初期化に失敗すると取得先を含むエラーを返し、別ホストには切り替えません。従来のオリジンルート / CDN 自動フォールバックを利用していた場合は、生成アセットを配布するか URL を明示してください。
|
|
360
|
+
|
|
361
|
+
### 配布物の検証
|
|
362
|
+
|
|
363
|
+
`npm run test:distribution` は実際の npm tarball をインストールし、任意依存なしの Vite / webpack アプリをビルドして、生成 JS と実 WASM を VM で実行します。ブラウザ・CORS の検証とは別です。`npx playwright install chromium` 後に `RUN_DISTRIBUTION_BROWSER=1 npm run test:distribution` で Chromium の検証も実行できます。CI はそのモードを使い、別オリジン配信と外部ネットワーク遮断も確認します。
|
|
364
|
+
|
|
365
|
+
---
|
|
366
|
+
|
|
300
367
|
## 💻 サンプルスクリプト & インタラクティブ Web デモ
|
|
301
368
|
|
|
302
369
|
`examples/node/` 配下に実行可能なスタンドアロンスクリプトを用意しています:
|
|
@@ -314,7 +381,8 @@ npx tsx examples/node/06-pulp-compatibility.ts
|
|
|
314
381
|
|
|
315
382
|
```bash
|
|
316
383
|
npm run dev
|
|
317
|
-
#
|
|
384
|
+
# ドキュメント: http://localhost:5173/
|
|
385
|
+
# インタラクティブデモ: http://localhost:5173/demo/
|
|
318
386
|
```
|
|
319
387
|
|
|
320
388
|
---
|
package/README.md
CHANGED
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
<a href="./README.zh-CN.md">简体中文</a>
|
|
15
15
|
</p>
|
|
16
16
|
<p>
|
|
17
|
-
<a href="https://ohtaman.github.io/washi/"><img src="https://img.shields.io/badge/
|
|
17
|
+
<a href="https://ohtaman.github.io/washi/"><img src="https://img.shields.io/badge/docs-online-blue" alt="docs" /></a>
|
|
18
|
+
<a href="https://ohtaman.github.io/washi/demo/"><img src="https://img.shields.io/badge/demo-playground-brightgreen" alt="live demo" /></a>
|
|
18
19
|
<a href="https://www.npmjs.com/package/@ohtaman/washi"><img src="https://img.shields.io/npm/v/@ohtaman/washi?color=crimson&label=npm" alt="npm version" /></a>
|
|
19
20
|
<a href="https://bundlephobia.com/package/@ohtaman/washi"><img src="https://img.shields.io/badge/bundle%20size-7.2%20kB%20(gzip)-blue" alt="bundle size" /></a>
|
|
20
21
|
<a href="./LICENSE"><img src="https://img.shields.io/badge/license-MIT-green" alt="license" /></a>
|
|
@@ -50,7 +51,7 @@ console.log(`Produce: x=${x.value}, y=${y.value} (Profit: $${m.objectiveValue})`
|
|
|
50
51
|
2. 📦 **Frictionless Zero-Dependency Distribution**: No Python runtimes, no C/C++ compilation, and no OS-dependent native solver binaries to bundle. Just `npm install @ohtaman/washi`—everything (including WebAssembly solvers) works instantly out of the box.
|
|
51
52
|
3. ✍️ **Familiar Algebraic Modeling (PuLP & python-mip DX)**: Never struggle with low-level matrix indices. Formulate models naturally using tagged template literals (`c`${x} + 2 * ${y} <= 40``) or drop-in PuLP/python-mip syntax (`LpProblem`, `lpSum`).
|
|
52
53
|
4. 🔄 **Pluggable Solvers & Unified API**: Formulate your mathematical model once; solve with **HiGHS** (industrial MIP/QP), **Z3** (SMT logic & Unsat Core diagnosis), or **GLPK** with a single parameter.
|
|
53
|
-
5. 🔍 **Automatic Conflict Diagnosis (`unsatCore`)**: When constraints are contradictory (Infeasible), diagnose root causes
|
|
54
|
+
5. 🔍 **Automatic Conflict Diagnosis (`unsatCore`)**: When constraints are contradictory (Infeasible), diagnose root causes by extracting a conflicting constraint subset (unsatisfiable core) via the Z3 engine.
|
|
54
55
|
6. ⚡ **Industrial Scale Performance in WebAssembly**: Built with an $O(N)$ linear-time expression builder. Formulate 20,000+ variable MIPs in ~100ms and solve them in ~15 seconds right inside WebAssembly via HiGHS.
|
|
55
56
|
|
|
56
57
|
---
|
|
@@ -228,7 +229,7 @@ console.log(`Siren active? ${m.values.get("siren")}`); // true
|
|
|
228
229
|
|
|
229
230
|
### 4. Conflict Diagnosis (`unsatCore`)
|
|
230
231
|
|
|
231
|
-
When business rules or constraints contradict each other, extract
|
|
232
|
+
When business rules or constraints contradict each other, extract a **conflicting constraint subset** (unsatisfiable core; minimality is not guaranteed) for troubleshooting (requires Z3):
|
|
232
233
|
|
|
233
234
|
```ts
|
|
234
235
|
import { Model } from "@ohtaman/washi";
|
|
@@ -297,6 +298,72 @@ console.log(`x = ${value(x)}, y = ${value(y)}`);
|
|
|
297
298
|
|
|
298
299
|
---
|
|
299
300
|
|
|
301
|
+
## 🌐 Browser & Web Worker Usage
|
|
302
|
+
|
|
303
|
+
The default HiGHS engine uses the package's WASM asset. There is **no automatic CDN fallback**. Node.js uses the installed HiGHS runtime; Vite and webpack 5 can discover the package-relative WASM and Worker references when building a browser application.
|
|
304
|
+
|
|
305
|
+
### npm with a bundler
|
|
306
|
+
|
|
307
|
+
```typescript
|
|
308
|
+
import { Model } from "@ohtaman/washi";
|
|
309
|
+
const model = new Model();
|
|
310
|
+
// Define variables, objective and constraints.
|
|
311
|
+
await model.solve();
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
No manual WASM copy or URL configuration is needed in the tested Vite / webpack builds. Other bundlers and SSR need their own integration checks; call the browser solver on the client. Worker execution is the browser default. Use `model.solve({ useWorker: false })` for a simple main-thread demo; a long solve will block the UI.
|
|
315
|
+
|
|
316
|
+
### Browser ESM without an application build
|
|
317
|
+
|
|
318
|
+
Serve the **entire `dist/browser` directory**, including its JS chunks, `highs.wasm` and Worker. No import map is needed:
|
|
319
|
+
|
|
320
|
+
```html
|
|
321
|
+
<script type="module">
|
|
322
|
+
import { Model } from "./washi/washi.js";
|
|
323
|
+
const model = new Model();
|
|
324
|
+
// Define variables, objective and constraints.
|
|
325
|
+
await model.solve({ useWorker: false });
|
|
326
|
+
</script>
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
### Plain script / CodePen
|
|
330
|
+
|
|
331
|
+
The package also includes `dist/browser/washi.min.js`, which exports the `Washi` global. Put that file's URL in CodePen's External Scripts, or use a script tag:
|
|
332
|
+
|
|
333
|
+
```html
|
|
334
|
+
<script src="./washi/washi.min.js"></script>
|
|
335
|
+
<script>
|
|
336
|
+
const model = new Washi.Model();
|
|
337
|
+
// Define variables, objective and constraints.
|
|
338
|
+
model.solve({ useWorker: false }).then(status => console.log(status));
|
|
339
|
+
</script>
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
For CDN usage, replace the local URL with `https://cdn.jsdelivr.net/npm/@ohtaman/washi@0.1.2/dist/browser/washi.min.js` (or pin your desired release version `>= 0.1.2`). For ESM use the corresponding `dist/browser/washi.js` URL. Selecting the CDN URL is an explicit choice; local copies never switch to a CDN.
|
|
343
|
+
|
|
344
|
+
The browser builds include the HiGHS engine, not the optional GLPK/Z3 engines. Use the npm package and optional dependencies for those integrations. `examples/distribution/` contains complete ESM and script examples.
|
|
345
|
+
|
|
346
|
+
### Offline distribution, Workers and custom hosting
|
|
347
|
+
|
|
348
|
+
Deploy the full application build or the entire browser distribution to a local HTTP / intranet server. With all assets locally available, an empty browser cache does not require access to the internet. Saving only the HTML or the first JS file is insufficient. `file://` execution and automatic PWA caching are not supported by this distribution.
|
|
349
|
+
|
|
350
|
+
ESM assets resolve relative to their module URL; the script build captures its own script URL at load time. CDN Workers use a Blob module bootstrap. The server must permit cross-origin JS / WASM loading; CSP must permit the selected script / connection origins and `worker-src blob:` for CDN Workers. If that is unavailable, use `useWorker: false`. Worker load / message failures reject pending solves rather than leaving them unresolved.
|
|
351
|
+
|
|
352
|
+
For custom hosting, `setHighsWasmUrl(url)` and `options.wasmUrl` remain available. To explicitly use the existing HiGHS CDN:
|
|
353
|
+
|
|
354
|
+
```typescript
|
|
355
|
+
import { setHighsWasmUrl, OFFICIAL_HIGHS_CDN_WASM_URL } from "@ohtaman/washi";
|
|
356
|
+
setHighsWasmUrl(OFFICIAL_HIGHS_CDN_WASM_URL);
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
Failed initialization reports the attempted URL and does not try a different host. This changes the previous local-root/CDN fallback behavior: deploy the generated assets or configure an explicit URL.
|
|
360
|
+
|
|
361
|
+
### Verify the distributable
|
|
362
|
+
|
|
363
|
+
`npm run test:distribution` packs and installs the actual npm tarball, builds Vite / webpack consumers without optional peers, and runs their emitted JS with real WASM in a VM. This is not a browser/CORS test. For Playwright Chromium checks, run `npx playwright install chromium` and `RUN_DISTRIBUTION_BROWSER=1 npm run test:distribution`. CI also runs that browser mode, including separate-origin browser bundles with external network requests blocked.
|
|
364
|
+
|
|
365
|
+
---
|
|
366
|
+
|
|
300
367
|
## 💻 CLI & Interactive Demos
|
|
301
368
|
|
|
302
369
|
Ready-to-run standalone examples are available in `examples/node/`:
|
|
@@ -314,7 +381,8 @@ To run the interactive browser showcase (`examples/web-demo`):
|
|
|
314
381
|
|
|
315
382
|
```bash
|
|
316
383
|
npm run dev
|
|
317
|
-
#
|
|
384
|
+
# Documentation: http://localhost:5173/
|
|
385
|
+
# Interactive Demo: http://localhost:5173/demo/
|
|
318
386
|
```
|
|
319
387
|
|
|
320
388
|
---
|
package/README.zh-CN.md
CHANGED
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
<a href="./README.zh-CN.md">简体中文</a>
|
|
15
15
|
</p>
|
|
16
16
|
<p>
|
|
17
|
-
<a href="https://ohtaman.github.io/washi/"><img src="https://img.shields.io/badge/
|
|
17
|
+
<a href="https://ohtaman.github.io/washi/"><img src="https://img.shields.io/badge/docs-online-blue" alt="docs" /></a>
|
|
18
|
+
<a href="https://ohtaman.github.io/washi/demo/"><img src="https://img.shields.io/badge/demo-playground-brightgreen" alt="live demo" /></a>
|
|
18
19
|
<a href="https://www.npmjs.com/package/@ohtaman/washi"><img src="https://img.shields.io/npm/v/@ohtaman/washi?color=crimson&label=npm" alt="npm version" /></a>
|
|
19
20
|
<a href="https://bundlephobia.com/package/@ohtaman/washi"><img src="https://img.shields.io/badge/bundle%20size-7.2%20kB%20(gzip)-blue" alt="bundle size" /></a>
|
|
20
21
|
<a href="./LICENSE"><img src="https://img.shields.io/badge/license-MIT-green" alt="license" /></a>
|
|
@@ -50,7 +51,7 @@ console.log(`生产计划: x=${x.value}, y=${y.value} (最大利润: $${m.object
|
|
|
50
51
|
2. 📦 **零依赖・单一包分发**:告别 Python 环境配置与繁重的跨平台 C/C++ 原生求解器编译绑定。只需 `npm install @ohtaman/washi`,包括 WebAssembly 求解器在内的所有组件即可开箱即用。
|
|
51
52
|
3. ✍️ **极简现代代数建模 DX(兼容 PuLP / python-mip)**:无需编写晦涩低级的矩阵索引。使用直观的模板字面量(`c`${x} + 2 * ${y} <= 40``)或熟悉的 Python 风格抽象(`LpProblem`, `lpSum`),像在草稿纸上写公式一样自然。
|
|
52
53
|
4. 🔄 **统一多求解器接口(HiGHS / Z3 / GLPK)**:模型代码一次编写,即可通过统一 API 自由切换:擅长工业级 MIP/QP 的 **HiGHS**、擅长逻辑求解与冲突分析的 **Z3**,以及经典学术求解器 **GLPK**。
|
|
53
|
-
5. 🔍 **不可行模型(Infeasible)冲突根因诊断**:当业务规则出现矛盾导致无解时,可通过 Z3
|
|
54
|
+
5. 🔍 **不可行模型(Infeasible)冲突根因诊断**:当业务规则出现矛盾导致无解时,可通过 Z3 引擎快速定位并提取冲突约束子集(Unsat Core)。
|
|
54
55
|
6. ⚡ **WebAssembly 原生工业级大规模性能**:搭载 $O(N)$ 线性时间高速表达式构建器,仅需约 100ms 即可构建 20,000+ 变量的大规模 MIP,并在浏览器 WebAssembly 中由 HiGHS 在约 15 秒内高效求解。
|
|
55
56
|
|
|
56
57
|
---
|
|
@@ -228,7 +229,7 @@ console.log(`警报响起? ${m.values.get("siren")}`); // true
|
|
|
228
229
|
|
|
229
230
|
### 4. 约束冲突根因诊断 (`unsatCore`)
|
|
230
231
|
|
|
231
|
-
当规则或约束之间存在矛盾时,Z3
|
|
232
|
+
当规则或约束之间存在矛盾时,Z3 能够提取引发冲突的**矛盾约束子集**(Unsat Core,不保证最小性;需使用 Z3):
|
|
232
233
|
|
|
233
234
|
```ts
|
|
234
235
|
import { Model } from "@ohtaman/washi";
|
|
@@ -297,6 +298,72 @@ console.log(`x = ${value(x)}, y = ${value(y)}`);
|
|
|
297
298
|
|
|
298
299
|
---
|
|
299
300
|
|
|
301
|
+
## 🌐 浏览器与 Web Worker 使用指南
|
|
302
|
+
|
|
303
|
+
HiGHS 默认使用包内的 WASM,**不会自动回退到 CDN**。Node.js 使用已安装的 HiGHS;Vite / webpack 5 可以根据包内的 WASM 和 Worker 引用生成部署文件。
|
|
304
|
+
|
|
305
|
+
### npm 与构建工具
|
|
306
|
+
|
|
307
|
+
```typescript
|
|
308
|
+
import { Model } from "@ohtaman/washi";
|
|
309
|
+
const model = new Model();
|
|
310
|
+
// 定义变量、目标函数和约束
|
|
311
|
+
await model.solve();
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
在验证的 Vite / webpack 构建中,不需要手动复制 WASM 或配置 URL。其他构建工具及 SSR 需要单独验证;浏览器求解应在客户端执行。浏览器默认使用 Worker。简单演示可以使用 `model.solve({ useWorker: false })`,但较长的计算会阻塞 UI。
|
|
315
|
+
|
|
316
|
+
### 浏览器原生 ESM(无需应用构建)
|
|
317
|
+
|
|
318
|
+
通过 HTTP 提供整个 `dist/browser` 目录,包括 JS 分块、`highs.wasm` 和 Worker。不需要 Import Maps。
|
|
319
|
+
|
|
320
|
+
```html
|
|
321
|
+
<script type="module">
|
|
322
|
+
import { Model } from "./washi/washi.js";
|
|
323
|
+
const model = new Model();
|
|
324
|
+
// 定义变量、目标函数和约束
|
|
325
|
+
await model.solve({ useWorker: false });
|
|
326
|
+
</script>
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
### 普通 script / CodePen
|
|
330
|
+
|
|
331
|
+
`dist/browser/washi.min.js` 导出全局 API `Washi`。将其 URL 添加到 CodePen 的 External Scripts,或使用:
|
|
332
|
+
|
|
333
|
+
```html
|
|
334
|
+
<script src="./washi/washi.min.js"></script>
|
|
335
|
+
<script>
|
|
336
|
+
const model = new Washi.Model();
|
|
337
|
+
// 定义变量、目标函数和约束
|
|
338
|
+
model.solve({ useWorker: false }).then(status => console.log(status));
|
|
339
|
+
</script>
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
若通过 CDN 加载,可使用 `https://cdn.jsdelivr.net/npm/@ohtaman/washi@0.1.2/dist/browser/washi.min.js`(请固定 `0.1.2` 及以上版本号)。ESM 使用同版本的 `dist/browser/washi.js`。选择 CDN URL 就是显式使用 CDN;本地部署不会自动改用外部 CDN。
|
|
343
|
+
|
|
344
|
+
浏览器构建包含 HiGHS 引擎,不包含可选的 GLPK / Z3 引擎。其他引擎集成请使用 npm 包及可选依赖。完整示例位于 `examples/distribution/`。
|
|
345
|
+
|
|
346
|
+
### 工厂内网、离线部署、Worker 与自定义 URL
|
|
347
|
+
|
|
348
|
+
将应用构建产物或整个 `dist/browser` 目录部署到本地 HTTP / 内网服务器。所有资源可在本地访问时,即使浏览器缓存为空也不需要外部网络。仅保存 HTML 或第一个 JS 文件是不够的。不保证 `file://` 执行,也不自动提供 PWA 缓存。
|
|
349
|
+
|
|
350
|
+
ESM 根据模块 URL 解析资源;script 构建在加载时保存自身的 URL。CDN Worker 使用 Blob 模块启动,需要服务端的跨域资源许可及相应 CSP。CDN Worker 还需允许 `worker-src blob:`。不支持时可使用 `useWorker: false`。Worker 加载或消息处理失败会拒绝求解 Promise。
|
|
351
|
+
|
|
352
|
+
自定义地址仍可通过 `setHighsWasmUrl(url)` 或 `options.wasmUrl` 指定。显式选择现有 HiGHS CDN:
|
|
353
|
+
|
|
354
|
+
```typescript
|
|
355
|
+
import { setHighsWasmUrl, OFFICIAL_HIGHS_CDN_WASM_URL } from "@ohtaman/washi";
|
|
356
|
+
setHighsWasmUrl(OFFICIAL_HIGHS_CDN_WASM_URL);
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
初始化失败会返回包含请求地址的错误,不会尝试其他主机。依赖旧的域名根目录 / CDN 自动回退的应用需部署生成资源或显式配置 URL。
|
|
360
|
+
|
|
361
|
+
### 验证发布产物
|
|
362
|
+
|
|
363
|
+
`npm run test:distribution` 安装实际 npm tarball,在没有可选依赖时构建 Vite / webpack 应用,并通过 VM 执行生成的 JS 和真实 WASM。这不等同于浏览器 / CORS 验证。安装 `npx playwright install chromium` 后,运行 `RUN_DISTRIBUTION_BROWSER=1 npm run test:distribution` 以验证 Chromium。CI 使用该模式,验证跨域部署及外部网络请求阻断。
|
|
364
|
+
|
|
365
|
+
---
|
|
366
|
+
|
|
300
367
|
## 💻 本地脚本与交互式 Web 演示
|
|
301
368
|
|
|
302
369
|
在 `examples/node/` 中提供了即开即用的独立脚本:
|
|
@@ -314,7 +381,8 @@ npx tsx examples/node/06-pulp-compatibility.ts
|
|
|
314
381
|
|
|
315
382
|
```bash
|
|
316
383
|
npm run dev
|
|
317
|
-
#
|
|
384
|
+
# 文档主页: http://localhost:5173/
|
|
385
|
+
# 交互式 Demo: http://localhost:5173/demo/
|
|
318
386
|
```
|
|
319
387
|
|
|
320
388
|
---
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
//#region \0washi-optional-engine
|
|
2
|
+
function e() {
|
|
3
|
+
throw Error("[WaSHI] Optional GLPK/Z3 engines are not included in the browser bundle. Use the npm package with the optional dependency installed.");
|
|
4
|
+
}
|
|
5
|
+
var t = e;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { e as default, t as init };
|