关于 cuopt-routing-api-python
面向 Python 的技能,用于构建和求解 cuOpt 路由问题,如 TSP、VRP 和 PDP。内容包括模型数据、约束、求解器设置、结果检查及常见问题排查。
主要功能
- 01TSP、VRP 和 PDP 建模
- 02时间窗与容量约束
- 03获取路线和目标值
- 04检查求解状态与不可行订单
- 05明确指定数据类型
适用场景
- 01构建带时间窗的车辆路径模型
- 02按车辆容量安排配送订单
- 03求解失败后检查不可行订单
- 04检查路线成本和求解状态
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ promptingcompany/ nv-skills --skill cuopt-routing-api-python安装后在 Claude Code 中输入/cuopt-routing-api-python确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
BENCHMARK.mdskill-card.mdskill.oms.sigassets/README.mdassets/pdp_basic/model.pyREADME.mdassets/vrp_basic/model.pyREADME.mdevals/evals.jsonreferences/examples.mdserver_examples.md详细介绍
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()
添加约束
# 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]))
解的检查
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())
数据类型(使用显式数据类型)
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 | 它会就地将 location 列替换为路径点 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。
升级处理
有关贡献或从源代码构建,请参阅开发者技能。
评论
支持 Markdown