| 名称 | cuopt-routing-api-python |
| 版本 | ‘26.10.00’ |
| 描述 | 使用 cuOpt 进行车辆路径规划(VRP、TSP、PDP)—— 仅 Python API。当用户正在用 Python 构建或求解路由问题时使用。 |
| 开源协议 | Apache-2.0 metadata: |
| 作者 | NVIDIA cuOpt Team tags: - cuopt - 路由 - vrp - tsp - python |
cuOpt 路由 — Python API
本技能仅适用于 Python。cuOpt 中没有用于路由的 C API。
必需的提问
如果尚未明确,请先询问:
- 问题类型 — TSP、VRP 还是 PDP?
- 地点 — 有多少个?有车场吗?地点之间的成本或距离(距离矩阵或推算)?
- 订单/任务 — 必须访问哪些地点?每个停靠点的需求或服务时间?
- 车队 — 车辆数目、每辆车的容量(如果是多维度则每个维度的容量),起点/终点位置?
- 约束 — 时间窗(最早/最晚到达)、服务时长、先后顺序(订单 A 必须在订单 B 之前)?
最小 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 |
它会就地更换 location 列为航点 ID — 如仍需成本矩阵索引(如按卡车迭代时),请传入 route_df.copy() |
调试
当状态 != 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。
升级
如需贡献代码或从源码构建,请参阅开发者技能。