← Back to the lab

Simulation

Medicine supply digital twin (5/14): routes with Clarke-Wright savings and 2-opt

What a delivery route costs, how the savings formula decides which units travel together, and how 2-opt undoes crossings. With the cost of every kilometre, every hour and every trip.

By ·

The fictional system’s regional warehouse delivers to its units by van. Each unit has its delivery day, and each day someone decides which van goes to which units and in what order. It is the vehicle routing problem, one of the most studied in operations research, and it has no practical exact solution with many stops: the number of possible routes grows faster than any power.

The twin uses two classic heuristics that, together, give very good routes in milliseconds.

First, what a route costs

Without cost there is no optimization. The twin charges for three things:

C=cf+ckm K+ch(Kv+n td60)C = c_f + c_{km}\,K + c_h\left(\frac{K}{v} + n\,\frac{t_d}{60}\right)
ParameterModel value
cfc_f, cost per trip (vehicle, insurance, loading)$350
ckmc_{km}, cost per km (fuel and maintenance)$9.50
chc_h, driver cost per hour$130
vv, average speed55 km/h
tdt_d, unloading per stop20 min
Maximum shift / capacity10 h / 8,000 L

KK is the route’s kilometres and nn the number of stops. Distances are straight lines multiplied by 1.35, a typical road factor; with real data they are replaced by a road distance matrix.

Clarke-Wright: the saving from joining two stops

The 1964 idea is simple. Start with the worst possible plan: one van per unit, out and back. Then ask how much is saved if two units ii and jj are visited on the same trip:

sij=d0i+d0j−dijs_{ij} = d_{0i} + d_{0j} - d_{ij}

Instead of returning to the warehouse from ii and setting out again towards jj (di0+d0jd_{i0} + d_{0j}), you go straight from ii to jj (dijd_{ij}). Compute every saving, sort from largest to smallest, and merge routes in that order, as long as:

  • ii and jj are on different routes and at an end of their route (a stop can’t be inserted in the middle);
  • the merged route fits in the van and within the shift.

2-opt: undoing crossings

Clarke-Wright decides which units travel together, but the order within each route can end up poor. 2-opt tries removing two legs of the route and reconnecting it the other way round, that is, reversing a segment:

⋯→a→b→⋯→c⏟reversed→e→⋯⟹⋯→a→c→⋯→b→e→⋯\cdots \to a \to \underbrace{b \to \cdots \to c}_{\text{reversed}} \to e \to \cdots \quad\Longrightarrow\quad \cdots \to a \to c \to \cdots \to b \to e \to \cdots

The change only touches two legs, so it is quick to evaluate:

Δ=dac+dbe−dab−dce\Delta = d_{ac} + d_{be} - d_{ab} - d_{ce}

If Δ<0\Delta < 0 it is accepted and the search starts over; when no change improves the route, it is 2-optimal.

In the aid, the baseline is fixed zone routes: units are grouped by direction, up to three per van, and visited in the order they were registered. It’s a common way to organize deliveries when routes were defined years ago. On the starting map:

OptionTripsKmCost
Fixed zone routes61,153$16,473
Clarke-Wright3696$9,997
Clarke-Wright + 2-opt3693$9,965

Almost all the saving comes from joining (Clarke-Wright); 2-opt adds little because routes are short and Clarke-Wright already builds them in good order. With long routes of 10 or more stops, 2-opt matters more. Try other maps and lower the capacity: with small vans, each route carries fewer units and the saving from joining drops.

Routes and costs tab (“Rutas y costos”): the vans run the delivery routes to the 60 units. (Interface in Spanish.) · Open full screen

In the full twin

With the same visit days and the same loads, regrouping one week’s routes cut trips from 15 to 10, cost by 20 % and distance by 709 km. The aid overstates the saving because its baseline is clumsier than a real operation; 20 % is the number that matters.

The twin uses the same idea for urgent orders: it compares the cost of inserting the stop into that day’s route, wherever it fits most cheaply, against sending a dedicated trip. The cheapest insertion between ii and jj costs diu+duj−dijd_{iu} + d_{uj} - d_{ij}, the same logic as the saving but in reverse.

What comes next

Vans leave the warehouse and suppliers’ trucks arrive at it. In the next post, the yard and docks as a queue: how many docks are needed so trucks don’t wait for hours.