cuopt-routing-api-python
NVIDIA/skills
コスト行列、時間枠、容量制約、および集荷・配送ペアを用いて、NVIDIA cuOptのPython APIを使用して車両ルート計画問題(TSP、VRP、PDP)を解く。
...すべて拡張します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(pickup_indices, delivery_indices)
# 順序関係
dm.add_order_precedence(node_id=2, preceding_nodes=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())
データ型(明示的なデータ型指定を使用)
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())を実行して、どの注文が実行不可能かを確認してください。
データ型:サイレントエラーを回避するため、行列や系列には明示的なデータ型(float32、int32)を使用してください。
例
- examples.md — VRP、PDP、マルチデポ
- server_examples.md — REST クライアント (curl、Python)
- 参照モデル:このスキルの
assets/— vrp_basic、pdp_basic。assets/README.md を参照してください。
エスカレーション
貢献やソースからのビルドについては、開発者向けスキルを参照してください。
---
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.
すべてのファイル
12件のファイル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
コピー





家
