Quickstart
Installation
GRID is available on PyPI:
pip install pygridopt
A first model
The Capacitated Vehicle Routing Problem (CVRP) asks for a set of routes, each performed by one vehicle starting and ending at a common depot, that together visit every customer exactly once while respecting a per-vehicle capacity, minimising the total travel distance. Each customer has a demand, and the sum of demands along any route cannot exceed the vehicle capacity.
The following is a complete CVRP instance with a depot, three customers, two vehicles of capacity 10, and a fully connected graph with symmetric distances.
import grid
demands = {1: 3, 2: 5, 3: 2}
distances = {
(0, 1): 4, (0, 2): 6, (0, 3): 5,
(1, 2): 3, (1, 3): 7, (2, 3): 4,
}
distances.update({(j, i): d for (i, j), d in distances.items()})
model = grid.RoutingModel()
model.add_vehicle_type(id=0, count=2, capacity=10)
model.add_node(id=0, depot=True)
for customer, demand in demands.items():
model.add_node(id=customer, demand=demand)
for (i, j), d in distances.items():
model.add_edge(
node_from=model.get_node(i),
node_to=model.get_node(j),
distance=d,
)
model.set_objective(metric="distance")
result = model.solve(solver="CABS", time_limit=10)
print(f"Optimal: {result['Optimal']}")
print(f"Cost: {result['Cost']}")
print(f"Routes: {result['Solution']}")
Step by step
RoutingModelis the top-level container of the model.add_vehicle_type()registers a homogeneous fleet with the given count and capacity.add_node()adds a vertex of the routing graph, marked as a depot or carrying a customer demand.add_edge()adds a directed arc with a travel attribute (here,distance).set_objective()selects a built-in objective metric.solve()compiles the model to DIDP and runs the chosen solver, returning a dictionary with the optimality status, the cost, the best dual bound, and the routes.
Next steps
Modelling Elements introduces the four core classes and their relationships.
Native Features describes the predefined modelling primitives that cover common VRP variants (CVRP, VRPTW, PDPTW, …) and walks through a full PDPTW example.
Custom Features shows how to declare user-defined variables and expressions for variants that go beyond the native primitives, with a full Electric Capacitated VRP (ECVRP) example.
API Reference provides the complete Python API reference.