Vehicle routing with time windows¶
A fleet of vehicles leaves a depot, serves every customer once and comes back. Each vehicle carries a limited load, and each customer must be served inside a time window: a vehicle that comes early waits, and one that would come late cannot serve that customer. The goal is the shortest total distance. Delivery, waste collection and school bus routes all look like this.
This example solves Solomon's instance C101 (1987), the classic benchmark of this problem: customers in clusters, vehicles of capacity 200, a service time of 90 at each customer, and travel time equal to the Euclidean distance. The 25 and 50 customer instances are the first 25 and 50 customers of the full instance.
| Customers | Best known distance | Vehicles | Proven optimum, arcs truncated to 0.1 |
|---|---|---|---|
| 25 | 191.81 | 3 | 191.3 |
| 50 | 363.25 | 5 | 362.4 |
| 100 | 828.94 | 10 | 827.3 |
The distances here use full precision, as published heuristic results do. 828.94 is the best known value for 100 customers (Rochat and Taillard 1995, with the fewest vehicles first, then the distance). Exact methods (Kohl et al. 1999) proved the optima in the last column, but with every arc truncated to one decimal, which makes them a little shorter. 191.81 and 363.25 are the full precision lengths of routes that reach those proven optima. No published table lists them, so they are best known values, not proven ones.
The model¶
A solution is a giant tour: one Permutation of all customers, with no depot visits. The objective cuts the giant tour into routes in the best possible way (the split of Prins, 2004). A route serves a run of consecutive customers of the tour, and it is allowed only if:
- its load fits the vehicle capacity,
- each customer is served by its due date (a vehicle that comes early waits until the ready time),
- the vehicle is back at the depot before the depot closes.
Among all allowed cuts, the split takes the one with the shortest total distance, a shortest path over the positions of the tour. So the capacity and the time windows are hard constraints, met by construction: every permutation gives a feasible plan, and the algorithm only searches the order. The fleet size is not fixed. Each route uses one vehicle, and on C101 the shortest plans use exactly the best known number of vehicles.
Time windows make the order within a route matter a lot: a small change can make a customer late and force a new route. Ant colony methods suit this well, because they build tours step by step and can prefer close customers. The heuristic 1 / distance tells the ants how close the next customer is.
import numpy as np
import ypea
# Solomon C101, the depot (row 0) and the first 25 customers:
# number, x, y, demand, ready time, due date, service time
C101 = """
0 40 50 0 0 1236 0
1 45 68 10 912 967 90
2 45 70 30 825 870 90
3 42 66 10 65 146 90
4 42 68 10 727 782 90
5 42 65 10 15 67 90
6 40 69 20 621 702 90
7 40 66 20 170 225 90
8 38 68 20 255 324 90
9 38 70 10 534 605 90
10 35 66 10 357 410 90
11 35 69 10 448 505 90
12 25 85 20 652 721 90
13 22 75 30 30 92 90
14 22 85 10 567 620 90
15 20 80 40 384 429 90
16 20 85 40 475 528 90
17 18 75 20 99 148 90
18 15 75 20 179 254 90
19 15 80 10 278 345 90
20 30 50 10 10 73 90
21 30 52 20 914 965 90
22 28 52 20 812 883 90
23 28 55 10 732 777 90
24 25 50 10 65 144 90
25 25 52 40 169 224 90
"""
capacity = 200
rows = []
for line in C101.strip().splitlines():
rows.append(line.split())
rows = np.array(rows, dtype=float)
xy = rows[:, 1:3]
distance = np.linalg.norm(xy[:, None, :] - xy[None, :, :], axis=2).tolist()
demand = rows[:, 3].tolist()
ready = rows[:, 4].tolist()
due = rows[:, 5].tolist()
service = rows[:, 6].tolist()
def split(order):
"""Cut the giant tour into routes with the shortest total distance."""
n = len(order)
best = [0.0] + [np.inf] * n # best[j]: the shortest distance that serves the first j customers
cut = [0] * (n + 1) # cut[j]: where the last route of best[j] starts
for i in range(n): # a route that starts with the i-th customer of the tour
load = 0.0
time = 0.0
length = 0.0
previous = 0 # the depot
for j in range(i, n):
customer = order[j] + 1
load += demand[customer]
start = max(time + distance[previous][customer], ready[customer])
if load > capacity or start > due[customer]:
break # more customers only make the route later and fuller
time = start + service[customer]
length += distance[previous][customer]
previous = customer
if time + distance[customer][0] > due[0]:
break # back at the depot too late
total = best[i] + length + distance[customer][0]
if total < best[j + 1]:
best[j + 1] = total
cut[j + 1] = i
routes = []
j = n
while j > 0:
i = cut[j]
route = []
for item in order[i:j]:
route.append(int(item) + 1)
routes.insert(0, route)
j = i
return best[n], routes
def total_distance(sol):
length, routes = split(sol.order)
return length
# the ants prefer a next customer that is close: 1 / distance
between = np.array(distance)[1:, 1:]
np.fill_diagonal(between, np.inf) # a customer never follows itself
closeness = 1 / between
problem = ypea.Problem(
ypea.Permutation("order", n=25),
total_distance,
name="C101-25",
)
alg = ypea.MMAS(heuristic={"order": closeness}, max_nfe=20_000)
result = alg.solve(problem, seed=1)
print(result.summary())
length, routes = split(result.best.solution.order)
for route in routes:
print(route)
MMAS on C101-25
best objective value: 191.814
feasible: True (violation 0)
evaluations: 20000, iterations: 800, time: 1.53 s, stopped by: max_nfe
[13, 17, 18, 19, 15, 16, 14, 12]
[5, 3, 7, 8, 10, 11, 9, 6, 4, 2, 1]
[20, 24, 25, 23, 22, 21]
Differs from Python: MATLAB draws its own random numbers, so the results differ from Python's. Customers are numbered from 1, and the objective returns only the distance (outputs=1).
% Solomon C101, the depot (row 0) and the first 25 customers:
% number, x, y, demand, ready time, due date, service time
c101 = [
0 40 50 0 0 1236 0
1 45 68 10 912 967 90
2 45 70 30 825 870 90
3 42 66 10 65 146 90
4 42 68 10 727 782 90
5 42 65 10 15 67 90
6 40 69 20 621 702 90
7 40 66 20 170 225 90
8 38 68 20 255 324 90
9 38 70 10 534 605 90
10 35 66 10 357 410 90
11 35 69 10 448 505 90
12 25 85 20 652 721 90
13 22 75 30 30 92 90
14 22 85 10 567 620 90
15 20 80 40 384 429 90
16 20 85 40 475 528 90
17 18 75 20 99 148 90
18 15 75 20 179 254 90
19 15 80 10 278 345 90
20 30 50 10 10 73 90
21 30 52 20 914 965 90
22 28 52 20 812 883 90
23 28 55 10 732 777 90
24 25 50 10 65 144 90
25 25 52 40 169 224 90
];
% row k + 1 holds customer k, and row 1 the depot
customers = struct();
customers.capacity = 200;
xy = c101(:, 2:3);
customers.distance = sqrt((xy(:, 1) - xy(:, 1)').^2 + (xy(:, 2) - xy(:, 2)').^2);
customers.demand = c101(:, 4);
customers.ready = c101(:, 5);
customers.due = c101(:, 6);
customers.service = c101(:, 7);
% the ants prefer a next customer that is close: 1 / distance
between = customers.distance(2:end, 2:end);
between(logical(eye(25))) = Inf; % a customer never follows itself
closeness = 1 ./ between;
problem = ypea.Problem( ...
vars=ypea.Permutation("order", n=25), ...
objective=@(sol) split_tour(sol.order, customers), ...
outputs=1, ... % split_tour also returns the routes
name="C101-25");
alg = ypea.MMAS(heuristic=struct("order", closeness), max_nfe=20000);
result = alg.solve(problem, seed=1);
disp(result.summary())
[total, routes] = split_tour(result.best.solution.order, customers);
for k = 1:numel(routes)
disp(mat2str(routes{k}))
end
function [total, routes] = split_tour(order, customers)
% Cut the giant tour into routes with the shortest total distance.
n = numel(order);
best = [0, Inf(1, n)]; % best(j + 1): the shortest distance that serves the first j customers
cut = ones(1, n + 1); % cut(j + 1): where the last route of best(j + 1) starts
for i = 1:n % a route that starts with the i-th customer of the tour
load = 0;
time = 0;
route_length = 0;
previous = 1; % the depot
for j = i:n
customer = order(j) + 1; % the row of this customer
load = load + customers.demand(customer);
start = max(time + customers.distance(previous, customer), customers.ready(customer));
if load > customers.capacity || start > customers.due(customer)
break % more customers only make the route later and fuller
end
time = start + customers.service(customer);
route_length = route_length + customers.distance(previous, customer);
previous = customer;
if time + customers.distance(customer, 1) > customers.due(1)
break % back at the depot too late
end
candidate = best(i) + route_length + customers.distance(customer, 1);
if candidate < best(j + 1)
best(j + 1) = candidate;
cut(j + 1) = i;
end
end
end
routes = {};
j = n;
while j > 0
i = cut(j + 1);
routes = [{order(i:j)}, routes];
j = i - 1;
end
total = best(n + 1);
end
MMAS on C101-25
best objective value: 191.814
feasible: true (violation 0)
evaluations: 20000, iterations: 800, time: 5.89 s, stopped by: max_nfe
[5 3 7 8 10 11 9 6 4 2 1]
[13 17 18 19 15 16 14 12]
[20 24 25 23 22 21]
MAX-MIN Ant System finds the best known plan, 191.81 with 3 vehicles, in under two seconds. These are the routes behind the best known value. The full script, examples/applications/vrptw.py, holds all 100 customers and solves the 25, 50 and 100 customer instances.
Results¶
Five algorithms, each with 10 seeded runs: 20 000 evaluations for 25 customers and 50 000 for 50. On the full instance only MMAS, with 100 000 evaluations (about 40 seconds a run). GA and SA use the native permutation operators (space="native"). The ant algorithms get the heuristic 1 / distance, the others only the objective. To show what the heuristic adds, MMAS also runs without it. The vehicles column belongs to the best run.
| Customers | Algorithm | Best | Mean | Worst | Vehicles | Runs at best known | Gap of best |
|---|---|---|---|---|---|---|---|
| 25 | MMAS | 191.81 | 191.81 | 191.81 | 3 | 10 of 10 | 0.0% |
| 25 | MMAS, no heuristic | 198.60 | 236.59 | 253.56 | 3 | 0 of 10 | 3.5% |
| 25 | ACS | 191.81 | 199.44 | 231.88 | 3 | 8 of 10 | 0.0% |
| 25 | ILS | 191.81 | 231.77 | 262.09 | 3 | 2 of 10 | 0.0% |
| 25 | SA | 214.64 | 278.17 | 352.11 | 4 | 0 of 10 | 11.9% |
| 25 | GA | 219.95 | 261.37 | 291.91 | 4 | 0 of 10 | 14.7% |
| 50 | MMAS | 363.25 | 363.25 | 363.25 | 5 | 10 of 10 | 0.0% |
| 50 | MMAS, no heuristic | 519.56 | 574.86 | 637.71 | 9 | 0 of 10 | 43.0% |
| 50 | ACS | 363.25 | 404.38 | 471.48 | 5 | 3 of 10 | 0.0% |
| 50 | ILS | 466.99 | 522.39 | 628.21 | 8 | 0 of 10 | 28.6% |
| 50 | SA | 620.61 | 692.70 | 746.40 | 9 | 0 of 10 | 70.8% |
| 50 | GA | 508.31 | 586.48 | 639.56 | 8 | 0 of 10 | 39.9% |
| 100 | MMAS | 828.94 | 842.00 | 866.38 | 10 | 6 of 10 | 0.0% |
MAX-MIN Ant System finds the best known plan in every run on 25 and 50 customers. ACS gets there too, but less often. The methods without the distance heuristic do much worse, and the gap grows with the size: ILS finds the best known plan for 25 customers in 2 runs of 10, but its best plan for 50 customers is 29 percent longer and needs 8 vehicles. A random order is almost always a bad plan here, because a customer out of time order forces a new route, so a search that starts from random orders and changes them a little at a time is slow to find the clusters. The ants build each tour one close customer at a time, so their tours follow the clusters from the start. Without the heuristic, MMAS does no better than ILS: the knowledge of distances makes the difference.
Simple rules are not enough either. The best nearest neighbor tour (over all start customers), cut by the same split, has length 326.49 for 25 customers and 865.98 for 50, 70 and 138 percent above the best known.
On the full instance of 100 customers, MMAS reaches the best known 828.94 with 10 vehicles in 6 runs of 10, and its worst run is 4.5 percent above it.
Try it on your own data¶
Replace the rows of the data with your depot and customers: position, demand, ready time, due date and service time, the depot first. For road distances or travel times, replace the distance matrix. Other rules fit into the split, because it checks each route as it grows: a maximum route length or a driver's shift. To pay for each vehicle as well, add a fixed cost per route to total. If a customer cannot be reached on time even by a vehicle of its own, the split finds no plan and returns infinity, so check the data first.