← すべてのチュートリアル · Twinworks

チュートリアル 7 / 12

API/MCP で自動化

上級 15分

Twinworks のエンジンをスクリプトや AI エージェントから実行します。実際の JSON リクエストとレスポンスを使って、モデルのシミュレーション、ボトルネックの順位付け、バッファのスイープと最適化、ラインバランシング、待ち行列の公式の検証を行います。

ワークスペースのシミュレーションエンジンは、usefulweb の API と MCP サーバーで 6 つのツールを動かしているものと同じ TypeScript コードです。7 つ目のツールである材料搬送スケジューラーについては、共有車両、搬送スケジュール、モデルチェックで説明しています。結果は決定論的です。同じモデルと同じシードからは、アプリでもサーバーでも常に同じ数値が得られます。

ツール ID(REST)MCP 名機能
twinworks-simulatetwinworks_simulateモデル+実行時間+シード+レプリケーション数 → 信頼区間付きの KPI、ステーションごとの統計、ボトルネック、コスト
twinworks-bottleneck-analysistwinworks_bottleneck_analysisボトルネックの順位(アクティブ期間法)+ what-if による改善効果+推奨事項
twinworks-buffer-sweeptwinworks_buffer_sweepバッファサイズ(または別のパラメーター)をスイープ → スループット曲線と変曲点
twinworks-buffer-allocationtwinworks_buffer_allocation決まった数のバッファ枠をスループットが最大になるように配分(貪欲法、共通乱数)
twinworks-line-balancetwinworks_line_balance作業+先行関係+タクト → ステーションへの割り当て、効率、平滑化指数
twinworks-queue-formulastwinworks_queue_formulasM/M/1、M/M/c、M/D/1、M/G/1、Kingman。必要に応じてシミュレーションで検証

ワークスペースでは、ヘルプ → API/MCP で自動化に、現在のモデル用のコピーしてすぐ使えるリクエストが表示されます。

現在のモデル用のリクエストを表示した API ダイアログ

1. テンプレートをシミュレーションする

どのモデル系ツールも、template の ID か model のどちらかを受け付けます。model にはファイル → エクスポート → モデル JSON(アクティブなシナリオ)で書き出した JSON を文字列として渡します。

curl -s -X POST https://twinworks.usefulweb.site/api/v1/tools/twinworks-simulate/calculate \
  -H 'content-type: application/json' \
  -d '{"template":"three-station-line","horizonHours":8,"replications":5,"seed":42}'

レスポンス(抜粋):

{
  "summary": "3-station line: 64.1 ± 3.4 good parts/h (95 % CI, 5 replications), mean lead time 304 s, WIP 5.53; bottleneck: Mill.",
  "result": {
    "replications": 5,
    "seeds": [42, 3029642774, 2605890936, 2032093786, 1137000544],
    "throughputPerHour": { "mean": 64.1333, "lo": 60.7384, "hi": 67.5282, "halfWidth": 3.3949, "n": 5 },
    "little": { "L": 6.8785, "lambdaPerHour": 66.6667, "W": 369.285, "lambdaW": 6.8386, "relError": 0.00581, "ok": true },
    "stations": [
      { "id": "M2", "name": "Mill", "utilization": 0.9126,
        "states": { "busy": 0.9126, "setup": 0, "waitop": 0, "blocked": 0, "idle": 0.0874, "down": 0, "maint": 0, "off": 0 },
        "availability": 1, "performance": 0.903, "quality": 1, "oee": 0.903 }
    ],
    "bottleneck": { "primary": "M2", "ranking": [ { "id": "M2", "name": "Mill", "sole": 0.8252, "shifting": 0.0606, "total": 0.8858 } ] },
    "openInAppUrl": "https://twinworks.usefulweb.site/app#project=…"
  }
}

seeds には各レプリケーションのシードが並んでいるため、どの実行も正確に再現できます。little は、測定された仕掛品(WIP)と、最初のレプリケーションのスループット × リードタイムを比較します(リトルの法則)。モデルの妥当性を手早く確認できます。openInAppUrl を開くと、そのモデルがワークスペースで開きます。

2. 自作モデルを手計算で確認する

決定論的な 2 台の機械のラインです。供給は無制限、M1 = 30 s、3 枠のバッファ、M2 = 40 s とします。これを two-machines.json として保存します。

{
  "name": "Two machines",
  "settings": { "horizon": 7200, "warmup": 600 },
  "nodes": [
    { "id": "S", "type": "source", "arrival": "saturated" },
    { "id": "M1", "type": "machine", "cycle": { "type": "constant", "value": 30 } },
    { "id": "B", "type": "buffer", "capacity": 3 },
    { "id": "M2", "type": "machine", "cycle": { "type": "constant", "value": 40 } },
    { "id": "OUT", "type": "sink" }
  ],
  "links": [ { "from": "S", "to": "M1" }, { "from": "M1", "to": "B" }, { "from": "B", "to": "M2" }, { "from": "M2", "to": "OUT" } ]
}

モデルのフィールドはテキスト(JSON を文字列にしたもの)なので、jq を使ってファイルを埋め込みます。

jq -n --rawfile m two-machines.json '{model: $m, replications: 1}' |
  curl -s -X POST https://twinworks.usefulweb.site/api/v1/tools/twinworks-simulate/calculate \
       -H 'content-type: application/json' --data @-

結果:"summary": "Two machines: 90 good parts/h (single run), mean lead time 240 s, WIP 6; bottleneck: Machine M2."(2 台の機械:良品 90 個/h(単一実行)、平均リードタイム 240 s、WIP 6、ボトルネック:機械 M2)

これは決定論的なラインについて理論が示すとおりの結果です。スループット = 1 / ボトルネックのサイクルタイム = 3600 / 40 = 90 個/h。M1 の稼働率は 30/40 = 75 % で、残りの 25 % はブロックされています。M2 の稼働率は 100 % です。リトルの法則も厳密に成り立ちます。L = λ·W = (90 / 3600 s) × 240 s = システム内に 6 個。

分布の種類:constant {value}(定数)、uniform {min,max}(一様)、triangular {min,mode,max}(三角)、normal {mean,sd}(正規、0 で打ち切り)、exponential {mean}(指数)、lognormal {mean,sd}(対数正規)。完全な形式は /project.schema.json にあります。

3. ボトルネックを順位付けし、対策を試す

curl -s -X POST https://twinworks.usefulweb.site/api/v1/tools/twinworks-bottleneck-analysis/calculate \
  -H 'content-type: application/json' \
  -d '{"template":"bottleneck-buffer-sizing","replications":3}'
{
  "summary": "\"OP30 Hone\" is the main bottleneck (58.3% of the time; best tested fix: Add a parallel server at \"OP30 Hone\" (1 → 2), +7.85 parts/h).",
  "result": {
    "primary": "M3",
    "ranking": [
      { "rank": 1, "id": "M3", "name": "OP30 Hone", "sole": 0.4043, "shifting": 0.1788, "total": 0.5831, "utilization": 0.8477 },
      { "rank": 2, "id": "M2", "name": "OP20 Bore", "sole": 0.2613, "shifting": 0.1628, "total": 0.4241, "utilization": 0.7929 }
    ],
    "recommendations": [ "\"OP30 Hone\" is the bottleneck 58.3% of the time (40.4% sole, 17.9% shifting): …" ]
  }
}

4. バッファをスイープする

{ "template": "bottleneck-buffer-sizing", "target": "B2", "parameter": "capacity", "from": 1, "to": 8, "step": 1, "replications": 4 }

これを /api/v1/tools/twinworks-buffer-sweep/calculate に POST します。

{
  "summary": "Throughput ranges from 99.6 to 105.8 parts/h over buffer capacity 1…8 at B2; knee point at 3.",
  "result": {
    "points": [
      { "value": 1, "throughput": { "mean": 99.5682, "lo": 95.1451, "hi": 103.9913 }, "wip": { "mean": 7.9107 }, "bottleneck": "M3" },
      { "value": 2, "throughput": { "mean": 102.4318, "lo": 97.7379, "hi": 107.1257 }, "wip": { "mean": 8.6199 }, "bottleneck": "M3" },
      { "value": 3, "throughput": { "mean": 103.7273, "lo": 99.2019, "hi": 108.2527 }, "wip": { "mean": 9.285 }, "bottleneck": "M3" }
    ]
  }
}

すべてのスイープ点で同じレプリケーションシードを使うため、曲線はなめらかになり、点同士の差にも意味があります。

5. バッファ配分を最適化する

{ "template": "bottleneck-buffer-sizing", "total": 8, "replications": 3, "horizonHours": 6 }

これを /api/v1/tools/twinworks-buffer-allocation/calculate に POST します。

{
  "summary": "8 buffer places: B1=1, B2=4, B3=2, B4=1 gives 97.13 parts/h (current allocation 95.2, significant gain); 6 well-placed places already reach the current throughput.",
  "result": {
    "best": { "capacities": { "B1": 1, "B2": 4, "B3": 2, "B4": 1 } },
    "gain": { "mean": 1.9333, "lo": 1.6465, "hi": 2.2202 },
    "placesForCurrentThroughput": 6,
    "evaluations": 18
  }
}

result.model は容量を最適化したモデルで、そのまま twinworks-simulate に渡せます。total を省略すると、現在の枠数のまま配分し直します。buffers(例:"B2,B3")を指定すると、最適化の対象を一部のバッファに限定できます。

同じバッファ配分の最適化をワークスペースのダイアログで実行した画面。バッファごとに現在の枠数と最適化後の枠数を表示

6. ラインのバランスを取る

{
  "tasks": [
    { "id": "A", "time": 60, "predecessors": "" },   { "id": "B", "time": 40, "predecessors": "A" },
    { "id": "C", "time": 55, "predecessors": "B" },  { "id": "D", "time": 50, "predecessors": "A" },
    { "id": "E", "time": 30, "predecessors": "D" },  { "id": "F", "time": 45, "predecessors": "D" },
    { "id": "G", "time": 20, "predecessors": "F" },  { "id": "H", "time": 35, "predecessors": "C,E,G" }
  ],
  "availableMinutes": 450, "demand": 270, "method": "best"
}

→ "4 stations for a takt time of 100 s (theoretical minimum 4), efficiency 83.8%, smoothness index 47.7 s."(タクトタイム 100 s で 4 ステーション(理論上の最小値 4)、効率 83.8%、平滑化指数 47.7 s)となり、ステーションは [A,B]、[D,F]、[C,E]、[G,H] です。結果には、バランスを取ったラインをそのままシミュレーションできる model も含まれます。

7. 待ち行列の公式を検証する

{ "queue": "mm1", "arrivalsPerHour": 60, "serviceTime": 48, "verify": true, "replications": 10 }

→ "M/M/1: utilization 80%, mean wait 192 s, mean queue 3.2; simulation confirms the result."(M/M/1:利用率 80%、平均待ち時間 192 s、平均待ち行列長 3.2。シミュレーションで結果を確認済み)。verification ブロックには、シミュレーションで得た平均待ち時間 193.4 s と 95 % 区間 [177.2, 209.6] が入っており、この区間は厳密解の 192 s を含んでいます。

8. AI エージェントから使う(MCP)

MCP エンドポイント https://twinworks.usefulweb.site/mcp を MCP クライアント(Claude Desktop、Cursor など)に追加します。ツール名にはアンダースコアを使います。

{
  "jsonrpc": "2.0", "id": 2, "method": "tools/call",
  "params": {
    "name": "twinworks_bottleneck_analysis",
    "arguments": { "template": "bottleneck-buffer-sizing", "replications": 3 }
  }
}

レスポンスには、テキストの要約と、structuredContent 内の完全な JSON が含まれます。良いプロンプトの例:「これが Twinworks JSON 形式の私のラインです。ボトルネックを見つけ、その手前のバッファを 1 から 8 までスイープして、改善効果の 95 % を得られる最小サイズを教えてください。」

制限

1 回のレプリケーションで扱えるイベントは最大 600 万件です。API では、twinworks-simulate は最大 30 レプリケーション、分析ツールとスイープツールは 1 点あたり最大 20 レプリケーションまで受け付け、実行時間は最大 2,400 時間です。より大規模な検討には、ワークスペース(Web Worker で動作)を使うか、作業を複数の呼び出しに分割してください。

試してみませんか?

Twinworks を起動 →