cuopt-routing-api-python
NVIDIA/skills
Lösen Sie Fahrzeugrouting-Probleme (TSP, VRP, PDP) mithilfe der Python-API von NVIDIA cuOpt unter Berücksichtigung von Kostenmatrizen, Zeitfenstern, Kapazitätsbeschränkungen und Abhol-Liefer-Paaren.
...Alle erweiterncuOpt Routing – Python-API
Überprüfen Sie vor der Programmierung den Problemtyp (TSP, VRP, PDP) und die Daten (Standorte, Aufträge, Fuhrpark, Einschränkungen).
Diese Funktion ist ausschließlich in Python verfügbar. Für die Routing-Funktion gibt es in cuOpt keine C-API.
Einfaches VRP-Beispiel
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()
Hinzufügen von Nebenbedingungen
# Zeitfenster
dm.add_transit_time_matrix(transit_time_matrix)
dm.set_order_time_windows(earliest_series, latest_series)
# Kapazitäten
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)
# Abhol- und Lieferpaare
dm.set_pickup_delivery_pairs(pickup_indices, delivery_indices)
# Reihenfolge
dm.add_order_precedence(node_id=2, preceding_nodes=np.array([0, 1]))
Lösungsprüfung
status = solution.get_status() # 0=ERFOLG, 1=FEHLER, 2=ZEITÜBERSCHREITUNG, 3=LEER
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())
Datentypen (explizite Datentypen verwenden)
cost_matrix = cost_matrix.astype("float32")
order_locations = cudf.Series([...], dtype="int32")
demand = cudf.Series([...], dtype="int32")
Solver-Einstellungen
ss = routing.SolverSettings()
ss.set_time_limit(30)
ss.set_verbose_mode(True)
ss.set_error_logging_mode(True)
Häufige Probleme
| Problem | Behebung |
|---|---|
| Leere Lösung | Erweitern Sie die Zeitfenster oder überprüfen Sie die Fahrzeiten |
| Nicht realisierbare Aufträge | Flotte oder Kapazität erhöhen |
| Status != 0 bei Zeitfenstern | add_transit_time_matrix() hinzufügen |
| Falsche Kosten | Prüfen, ob cost_matrix symmetrisch ist |
compute_waypoint_sequence verändert route_df |
Es ersetzt die Spalte „location“ durch Wegpunkt-IDs – übergeben Sie route_df.copy(), wenn Sie weiterhin Indizes der Kostenmatrix benötigen (z. B. bei der Iteration pro LKW) |
Fehlerbehebung
Wenn `status != 0`: Führe `print(solution.get_error_message())` und `print(solution.get_infeasible_orders().to_list())` aus, um zu sehen, welche Aufträge nicht realisierbar sind.
Datentypen: Verwenden Sie explizite Datentypen (float32, int32) für Matrizen und Reihen, um stille Fehler zu vermeiden.
Beispiele
- examples.md – VRP, PDP, Multi-Depot
- server_examples.md — REST-Client (curl, Python)
- Referenzmodelle:
assets/dieses Skills — vrp_basic, pdp_basic. Siehe assets/README.md.
Eskalation
Informationen zu Beiträgen oder zum Kompilieren aus dem Quellcode finden Sie unter „Developer Skill“.
---
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.
Alle Dateien
12 Dateiencuopt-routing-api-python installieren
Laden Sie die Skill-Dateien herunter und entpacken Sie sie in Ihr Verzeichnis „.claude/skills/“.
ZIP herunterladenKlonen Sie das Repository und kopieren Sie die Skill-Dateien in Ihr Projekt.
git clone https://github.com/NVIDIA/skills/tree/main/skills/cuopt-routing-api-python # Copy SKILL.md to your .claude/skills/ directory
Kopieren





Heim
