cuopt-routing-api-python

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

cuOpt 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
undefined
python
undefined

Time 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]))
undefined
dm.add_order_precedence(node_id=2, preceding_nodes=np.array([0, 1]))
undefined

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())
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

常见问题

ProblemFix
Empty solutionWiden time windows or check travel times
Infeasible ordersIncrease fleet or capacity
Status != 0 with time windowsAdd
add_transit_time_matrix()
Wrong costCheck 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)
问题解决方法
空解决方案放宽时间窗口或检查行程时间
不可行订单增加车队规模或容量
带时间窗口时状态≠0添加
add_transit_time_matrix()
成本计算错误检查cost_matrix是否对称
compute_waypoint_sequence
修改route_df
它会原地将
location
列替换为途经点ID——如果仍需要成本矩阵索引(例如按卡车迭代时),请传入
route_df.copy()

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.
当状态≠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
    assets/
    vrp_basic, pdp_basic. See assets/README.md.
  • examples.md — VRP、PDP、多仓库场景
  • server_examples.md — REST客户端(curl、Python)
  • 参考模型: 本技能的
    assets/
    目录下——vrp_basicpdp_basic。详见assets/README.md

Escalate

升级处理

For contribution or build-from-source, see the developer skill.
如需贡献代码或从源码构建,请参考开发者技能。