選項
首頁首頁 Skill 數據科學與機器學習 cuopt-routing-api-python

cuopt-routing-api-python

NVIDIA/skills NVIDIA/skills

利用 NVIDIA cuOpt 的 Python API,解決車輛路線規劃問題(TSP、VRP、PDP),並處理成本矩陣、時間窗、容量限制以及取貨與送貨配對。

...展開全部
0
更新時間 2026-09-25

cuOpt 路線規劃 — Python API

在編寫程式碼之前,請先確認問題類型(TSP、VRP、PDP)及資料(地點、訂單、車隊、限制條件)。

此功能僅支援Python。cuOpt 中沒有 C 語言的路線規劃 API。

最簡 VRP 範例

import cudf
from cuopt import routing

cost_matrix = cudf.DataFrame([...], dtype="float32")
dm = routing.DataModel(n_locations=4, n_fleet=2, n_orders=3)
dm.add_cost_matrix(cost_matrix)
dm.set_order_locations(cudf.Series([1, 2, 3], dtype="int32"))
solution = routing.Solve(dm, routing.SolverSettings())

if solution.get_status() == 0:
    solution.display_routes()

新增約束條件

# 時間窗
dm.add_transit_time_matrix(transit_time_matrix)
dm.set_order_time_windows(earliest_series, latest_series)

# 容量
dm.add_capacity_dimension("weight", demand_series, capacity_series)
dm.set_order_service_times(service_times)
dm.set_vehicle_locations(start_locations, end_locations)
dm.set_vehicle_time_windows(earliest_start, latest_return)

# 取貨-送貨配對
dm.set_pickup_delivery_pairs(取貨索引, 送貨索引)

# 順序關係
dm.add_order_precedence(節點 ID=2, 前置節點=np.array([0, 1]))

解法驗證

status = solution.get_status()  # 0=成功, 1=失敗, 2=超時, 3=空
if status == 0:
    route_df = solution.get_route()
    total_cost = solution.get_total_objective()
else:
    print(solution.get_error_message())
    print(solution.get_infeasible_orders().to_list())

資料類型(請使用顯式 dtype)

cost_matrix = cost_matrix.astype("float32")
order_locations = cudf.Series([...], dtype="int32")
demand = cudf.Series([...], dtype="int32")

求解器設定

ss = routing.SolverSettings()
ss.set_time_limit(30)
ss.set_verbose_mode(True)
ss.set_error_logging_mode(True)

常見問題

問題 解決方法
解為空 擴大時間區間或檢查行車時間
不可行的訂單 增加車隊規模或運能
在時間窗條件下,狀態值不為 0 新增add_transit_time_matrix()函式
成本錯誤 檢查 cost_matrix 是否為對稱矩陣
compute_waypoint_sequence會修改 route_df 它會直接將位置欄位替換為路點 ID — 若您仍需成本矩陣索引(例如按卡車逐一迭代時),請傳入route_df.copy()

除錯

當 status != 0 時: 執行 print(solution.get_error_message())以及print(solution.get_infeasible_orders().to_list()),以查看哪些訂單不可行。

資料類型:請為矩陣和序列使用明確的 dtypes(float32、int32),以避免靜默錯誤。

範例

  • examples.md — VRP、PDP、多倉庫問題
  • server_examples.md — REST 客戶端(curl、Python)
  • 參考模型:此技能的assets/目錄中包含 vrp_basic、pdp_basic。請參閱 assets/README.md。

問題升級

若要貢獻程式碼或從原始碼編譯,請參閱開發者技能頁面。

在 GitHub 上查看
---
name: cuopt-routing-api-python
description: Solve vehicle routing problems (TSP, VRP, PDP) using NVIDIA cuOpt's Python API with cost matrices, time windows, capacity constraints, and pickup-delivery pairs.
license: Apache-2.0
---



# cuOpt Routing — Python API

Confirm problem type (TSP, VRP, PDP) and data (locations, orders, fleet, constraints) before coding.

This skill is **Python only**. Routing has no C API in cuOpt.

## Minimal VRP Example

```python
import cudf
from cuopt import routing

cost_matrix = cudf.DataFrame([...], dtype="float32")
dm = routing.DataModel(n_locations=4, n_fleet=2, n_orders=3)
dm.add_cost_matrix(cost_matrix)
dm.set_order_locations(cudf.Series([1, 2, 3], dtype="int32"))
solution = routing.Solve(dm, routing.SolverSettings())

if solution.get_status() == 0:
    solution.display_routes()
```

## Adding Constraints

```python
# Time windows
dm.add_transit_time_matrix(transit_time_matrix)
dm.set_order_time_windows(earliest_series, latest_series)

# Capacities
dm.add_capacity_dimension("weight", demand_series, capacity_series)
dm.set_order_service_times(service_times)
dm.set_vehicle_locations(start_locations, end_locations)
dm.set_vehicle_time_windows(earliest_start, latest_return)

# Pickup-delivery pairs
dm.set_pickup_delivery_pairs(pickup_indices, delivery_indices)

# Precedence
dm.add_order_precedence(node_id=2, preceding_nodes=np.array([0, 1]))
```

## Solution Checking

```python
status = solution.get_status()  # 0=SUCCESS, 1=FAIL, 2=TIMEOUT, 3=EMPTY
if status == 0:
    route_df = solution.get_route()
    total_cost = solution.get_total_objective()
else:
    print(solution.get_error_message())
    print(solution.get_infeasible_orders().to_list())
```

## Data Types (use explicit dtypes)

```python
cost_matrix = cost_matrix.astype("float32")
order_locations = cudf.Series([...], dtype="int32")
demand = cudf.Series([...], dtype="int32")
```

## Solver Settings

```python
ss = routing.SolverSettings()
ss.set_time_limit(30)
ss.set_verbose_mode(True)
ss.set_error_logging_mode(True)
```

## Common Issues

| Problem | Fix |
|---------|-----|
| Empty solution | Widen time windows or check travel times |
| Infeasible orders | Increase fleet or capacity |
| Status != 0 with time windows | Add `add_transit_time_matrix()` |
| Wrong cost | Check cost_matrix is symmetric |
| `compute_waypoint_sequence` alters route_df | It replaces the `location` column with waypoint ids in place — pass `route_df.copy()` if you still need cost-matrix indices (e.g. when iterating per truck) |

## Debugging

**When status != 0:** `print(solution.get_error_message())` and `print(solution.get_infeasible_orders().to_list())` to see which orders are infeasible.

**Data types:** Use explicit dtypes (float32, int32) for matrices and series to avoid silent errors.

## Examples

- [examples.md](references/examples.md) — VRP, PDP, multi-depot
- [server_examples.md](references/server_examples.md) — REST client (curl, Python)
- **Reference models:** This skill's `assets/` — [vrp_basic](assets/vrp_basic/), [pdp_basic](assets/pdp_basic/). See [assets/README.md](assets/README.md).

## Escalate

For contribution or build-from-source, see the developer skill.

安裝 cuopt-routing-api-python

請下載並將技能檔案解壓縮至您的 .claude/skills/ 目錄中。

下載 ZIP

複製儲存庫並將技能檔案複製到您的專案中。

git clone https://github.com/NVIDIA/skills/tree/main/skills/cuopt-routing-api-python # Copy SKILL.md to your .claude/skills/ directory

複製 複製
快速設定: 將技能資料夾複製到 .claude/skills/ Claude 會自動偵測並使用該技能
儲存庫 NVIDIA/skills

相關技能

web-search
更新時間 2026-06-29
webapp-testing
更新時間 2026-06-29
lark-base
更新時間 2026-07-05
agentmail
更新時間 2026-06-29
OR