← 全部教程 · Twinworks

第 7 篇,共 12 篇

使用 API / MCP 自动化

高级 15 分钟

通过脚本和 AI 智能体运行 Twinworks 引擎:用真实的 JSON 请求和响应来仿真模型、为瓶颈排名、扫描并优化缓冲区、平衡产线以及核对排队公式。

工作区中的仿真引擎与驱动 usefulweb API 和 MCP 服务器上六个工具的 TypeScript 代码完全相同;第七个工具 物料运输调度器 在共享车辆、运输调度与模型检查中介绍。结果是确定性的:同一模型和同一种子无论在应用中还是在服务器上,都会得到相同的数值。

工具 ID(REST)MCP 名称功能说明
twinworks-simulatetwinworks_simulate模型 + 运行时长 + 种子 + 重复次数 → 带置信区间的 KPI、各工位统计、瓶颈、成本
twinworks-bottleneck-analysistwinworks_bottleneck_analysis瓶颈排名(活跃期法)+ 假设分析收益 + 改进建议
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,后者是通过 文件 → 导出 → 模型 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 将测得的在制品数量与第一次重复运行的产能 × 交付周期进行比较(利特尔法则),可以快速检查模型是否合理。openInAppUrl 会在工作区中打开该模型。

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" } ]
}

model 字段是文本(以字符串表示的 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."(两台机器:每小时 90 个合格件(单次运行),平均交付周期 240 s,在制品 6 个;瓶颈:机器 M2。)

这与确定性产线的理论值完全一致:产能 = 1 / 瓶颈节拍时间 = 3600 / 40 = 90 件/小时。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 }

将其 POST 到 /api/v1/tools/twinworks-buffer-sweep/calculate:

{
  "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 }

将其 POST 到 /api/v1/tools/twinworks-buffer-allocation/calculate:

{
  "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 % 收益的最小容量是多少。"

限制

单次重复运行最多可处理 600 万个事件。API 对 twinworks-simulate 最多接受 30 次重复运行,对分析和扫描工具每个点最多接受 20 次,运行时长最长 2,400 小时。更大规模的研究请使用工作区(它在 Web Worker 中运行),或将工作拆分为多次调用。

准备好试试了吗?

启动 Twinworks →