cuopt-routing-api-python
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesecuOpt Routing — Python API
cuOpt路径规划——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.
编码前请确认问题类型(TSP、VRP、PDP)和数据(位置、订单、车队、约束条件)。
本技能仅支持Python。cuOpt的路径规划功能没有C API。
Minimal VRP Example
最小化VRP示例
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()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
undefinedpython
undefinedTime windows
Time windows
dm.add_transit_time_matrix(transit_time_matrix)
dm.set_order_time_windows(earliest_series, latest_series)
dm.add_transit_time_matrix(transit_time_matrix)
dm.set_order_time_windows(earliest_series, latest_series)
Capacities
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)
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
Pickup-delivery pairs
dm.set_pickup_delivery_pairs(pickup_indices, delivery_indices)
dm.set_pickup_delivery_pairs(pickup_indices, delivery_indices)
Precedence
Precedence
dm.add_order_precedence(node_id=2, preceding_nodes=np.array([0, 1]))
undefineddm.add_order_precedence(node_id=2, preceding_nodes=np.array([0, 1]))
undefinedSolution 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())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")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)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 |
| Wrong cost | Check cost_matrix is symmetric |
| It replaces the |
| 问题 | 解决方法 |
|---|---|
| 空解决方案 | 放宽时间窗口或检查行程时间 |
| 不可行订单 | 增加车队规模或容量 |
| 带时间窗口时状态≠0 | 添加 |
| 成本计算错误 | 检查cost_matrix是否对称 |
| 它会原地将 |
Debugging
调试
When status != 0: and to see which orders are infeasible.
print(solution.get_error_message())print(solution.get_infeasible_orders().to_list())Data types: Use explicit dtypes (float32, int32) for matrices and series to avoid silent errors.
当状态≠0时: 使用和查看哪些订单不可行。
print(solution.get_error_message())print(solution.get_infeasible_orders().to_list())数据类型: 为矩阵和序列使用显式数据类型(float32、int32),避免静默错误。
Examples
示例
- examples.md — VRP, PDP, multi-depot
- server_examples.md — REST client (curl, Python)
- Reference models: This skill's — vrp_basic, pdp_basic. See assets/README.md.
assets/
- examples.md — VRP、PDP、多仓库场景
- server_examples.md — REST客户端(curl、Python)
- 参考模型: 本技能的目录下——vrp_basic、pdp_basic。详见assets/README.md。
assets/
Escalate
升级处理
For contribution or build-from-source, see the developer skill.
如需贡献代码或从源码构建,请参考开发者技能。