cuopt-server-api-python
NVIDIA/skills
部署一个 cuOpt REST 服务器,并使用 Python 或 curl 解决路由优化问题。
...展开全部cuOpt 服务器 — 部署与客户端(Python/curl)
本技能涵盖服务器和 客户端示例(curl、Python)的启动。服务器没有单独的 C 语言 API(客户端可以使用任何编程语言)。
启动服务器
# 开发环境
python -m cuopt_server.cuopt_service --ip 0.0.0.0 --port 8000
# Docker
docker run --gpus all -d -p 8000:8000 -e CUOPT_SERVER_PORT=8000 \
nvidia/cuopt:latest-cuda12.9-py3.13
验证
curl http://localhost:8000/cuopt/health
工作流
- 向
/cuopt/request发送 POST 请求 → 获取reqId - 轮询
/cuopt/solution/{reqId}直到解决方案就绪 - 解析响应
Python 客户端(路由)
import requests, time
SERVER = "http://localhost:8000"
HEADERS = {"Content-Type": "application/json", "CLIENT-VERSION": "custom"}
payload = {
"cost_matrix_data": {"data": {"0": [[0,10,15],[10,0,12],[15,12,0]]}},
"travel_time_matrix_data": {"data": {"0": [[0,10,15],[10,0,12],[15,12,0]]}},
"task_data": {"task_locations": [1, 2], "demand": [[10, 20]], "task_time_windows": [[0,100],[0,100]], "service_times": [5, 5]},
"fleet_data": {"vehicle_locations": [[0, 0]], "capacities": [[50]], "vehicle_time_windows": [[0, 200]]},
"solver_config": {"time_limit": 5}
}
r = requests.post(f"{SERVER}/cuopt/request", json=payload, headers=HEADERS)
req_id = r.json()["reqId"]
# 查询:GET /cuopt/solution/{req_id}
术语说明:REST 与 Python API
| Python API | REST |
|---|---|
| order_locations | 任务位置 |
| set_order_time_windows() | 任务时间段 |
| service_times | 服务时间 |
请使用travel_time_matrix_data(而非 transit_time_matrix_data)。容量应为[[50, 50]],而非[[50], [50]]。
调试 (422 / 有效载荷)
验证错误:请对照 OpenAPI(/cuopt.yaml)检查字段名称。 常见错误:transit_time_matrix_data→travel_time_matrix_data;各维度的容量应为[[50, 50]],而非每辆车的[[50], [50]]。记录失败请求的reqId和响应正文。
可运行的资产
从每个资产目录运行(服务器必须正在运行;若服务器不可达,脚本将以退出代码 0 结束)。均使用 Pythonrequests 模块:
- assets/vrp_simple/ — 基础VRP(无时间窗口)
- assets/vrp_basic/ — 带时间窗口的 VRP
- assets/pdp_basic/ — 取件与送件
- assets/lp_basic/ — 基于 REST 的 LP(CSR 格式)
- assets/milp_basic/ — 通过 REST 接口实现的 MILP
概述请参见 assets/README.md。
问题升级
如需贡献代码或从源代码构建,请参阅开发者指南。
---
name: cuopt-server-api-python
description: Deploy a cuOpt REST server and solve routing optimization problems using Python or curl.
license: Apache-2.0
---
# cuOpt Server — Deploy and client (Python/curl)
This skill covers **starting the server** and **client examples** (curl, Python). Server has no separate C API (clients can be any language).
## Start server
```bash
# Development
python -m cuopt_server.cuopt_service --ip 0.0.0.0 --port 8000
# Docker
docker run --gpus all -d -p 8000:8000 -e CUOPT_SERVER_PORT=8000 \
nvidia/cuopt:latest-cuda12.9-py3.13
```
## Verify
```bash
curl http://localhost:8000/cuopt/health
```
## Workflow
1. POST to `/cuopt/request` → get `reqId`
2. Poll `/cuopt/solution/{reqId}` until solution ready
3. Parse response
## Python client (routing)
```python
import requests, time
SERVER = "http://localhost:8000"
HEADERS = {"Content-Type": "application/json", "CLIENT-VERSION": "custom"}
payload = {
"cost_matrix_data": {"data": {"0": [[0,10,15],[10,0,12],[15,12,0]]}},
"travel_time_matrix_data": {"data": {"0": [[0,10,15],[10,0,12],[15,12,0]]}},
"task_data": {"task_locations": [1, 2], "demand": [[10, 20]], "task_time_windows": [[0,100],[0,100]], "service_times": [5, 5]},
"fleet_data": {"vehicle_locations": [[0, 0]], "capacities": [[50]], "vehicle_time_windows": [[0, 200]]},
"solver_config": {"time_limit": 5}
}
r = requests.post(f"{SERVER}/cuopt/request", json=payload, headers=HEADERS)
req_id = r.json()["reqId"]
# Poll: GET /cuopt/solution/{req_id}
```
## Terminology: REST vs Python API
| Python API | REST |
|------------|------|
| order_locations | task_locations |
| set_order_time_windows() | task_time_windows |
| service_times | service_times |
Use `travel_time_matrix_data` (not transit_time_matrix_data). Capacities: `[[50, 50]]` not `[[50], [50]]`.
## Debugging (422 / payload)
**Validation errors:** Check field names against OpenAPI (`/cuopt.yaml`). Common mistakes: `transit_time_matrix_data` → `travel_time_matrix_data`; capacities per dimension `[[50, 50]]` not per vehicle `[[50], [50]]`. Capture `reqId` and response body for failed requests.
## Runnable assets
Run from each asset directory (server must be running; scripts exit 0 if server unreachable). All use Python `requests`:
- [assets/vrp_simple/](assets/vrp_simple/) — Basic VRP (no time windows)
- [assets/vrp_basic/](assets/vrp_basic/) — VRP with time windows
- [assets/pdp_basic/](assets/pdp_basic/) — Pickup and delivery
- [assets/lp_basic/](assets/lp_basic/) — LP via REST (CSR format)
- [assets/milp_basic/](assets/milp_basic/) — MILP via REST
See [assets/README.md](assets/README.md) for overview.
## Escalate
For contribution or build-from-source, see the developer skill.





首页
