Solving CVRP with ACO

Minimizing Travel Cost for Complex Delivery Problems

Start With The Story

This scenario involves the Capacitated Vehicle Routing Problem, solved using the meta-heuristics algorithm Ant Colony Optimization. Basically, VRP is a network consisting of a number of nodes (sometimes called cities) and arcs connecting one to all others along with the corresponding costs. Mostly, the aim is to minimize the cost in visiting each customer once and only once. The term "capacitated" is added due to some capacity constraints on the vehicles (vcap).

Enter the problem. Some company wants to deliver loads to a number of customers. In this case, we have 24 nodes based on the location of Germany's train stations (don't ask why). The delivery always starts from and ends at the depot, visiting a list of customers in other cities. And then a number of questions arise:

  • How do we minimize the travel cost in terms of distance?
  • How many trucks are required?
  • Which cities are visited by the truck #1, #2. etc.?
Such questions are addressed by employing the ants.

How to use this page?
Two parameters can be adjusted:
  • depot: [0..23], def = 0
  • vcap: [200..400], def = 400
Calling this page without parameter will get the defaults. Otherwise, just try something like this

There is a way to set all the demands, but I don't think you are ready for that. 😉
Map
DEPOT: Berlin Hbf
VCAP: 300 vol.

ACTIVE: 15 customers
  1. Düsseldorf Hbf (50 vol.)
  2. Frankfurt Hbf (25 vol.)
  3. Aachen Hbf (65 vol.)
  4. Stuttgart Hbf (50 vol.)
  5. Dresden Hbf (20 vol.)
  6. München Hbf (85 vol.)
  7. Bremen Hbf (40 vol.)
  8. Leipzig Hbf (100 vol.)
  9. Dortmund Hbf (90 vol.)
  10. Karlsruhe Hbf (65 vol.)
  11. Mannheim Hbf (75 vol.)
  12. Kiel Hbf (30 vol.)
  13. Würzburg Hbf (45 vol.)
  14. Saarbrücken Hbf (85 vol.)
  15. Freiburg Hbf (85 vol.)
Result
OVERALL | #TOURS: 4 | COST: 5933.953 km | LOAD: 910 vol. | VCAP: 300 vol.
Tour 1
COST: 1496.639 km
LOAD: 300 vol.

  1. Bremen Hbf | 40 vol.
  2. Dortmund Hbf | 90 vol.
  3. Düsseldorf Hbf | 50 vol.
  4. Leipzig Hbf | 100 vol.
  5. Dresden Hbf | 20 vol.

Tour 2
COST: 1611.912 km
LOAD: 295 vol.

  1. Frankfurt Hbf | 25 vol.
  2. Mannheim Hbf | 75 vol.
  3. Karlsruhe Hbf | 65 vol.
  4. Saarbrücken Hbf | 85 vol.
  5. Würzburg Hbf | 45 vol.

Tour 3
COST: 2123.459 km
LOAD: 285 vol.

  1. München Hbf | 85 vol.
  2. Stuttgart Hbf | 50 vol.
  3. Freiburg Hbf | 85 vol.
  4. Aachen Hbf | 65 vol.

Tour 4
COST: 701.943 km
LOAD: 30 vol.

  1. Kiel Hbf | 30 vol.

ANTS
#generations: 10 for global, 5 for local
#ants: 5 times #active_customers

ACO
Rel. importance of pheromones α = 1.0
Rel. importance of visibility β = 10.0
Trail persistance ρ = 0.5
Pheromone intensity Q = 10

See this wikipedia page to learn more.

What kind of cost?
Directed driving distance, obtained through Google API. The visualization does not display that since the idea is VRP. Adding such feature is very easy, but not a priority for this case.
Can we use any address?
Yes, absolutely. What we need is the geo-coordinates of the addresses, and the distance matrix, which is not a problem. See my oldie master thesis here, implemented using PHP/MySQL for a delivery case in Darmstadt city, Germany.
Travel time as the cost?
Just replace the distance matrix with a duration matrix, then it is done. Please keep in mind, this feature is not intended for realtime use. But regarding the idea, not an issue.
Up to how many nodes?
There is no definitive answer for that. However, if a large number of nodes involved, a good strategy is required. Actually, for this one, a suitable technique is already implemented instead of a "plain" ACO.

NETWORK
Depo: [1] Berlin Hbf | Number of cities: 24 | Total loads: 910 vol. | Vehicle capacity: 300 vol.
Loads: [0, 0, 50, 25, 0, 65, 50, 20, 0, 85, 40, 100, 90, 0, 65, 0, 0, 75, 30, 0, 45, 85, 0, 85]

ITERATION
Generation: #1
Best cost: 7004.752 | Path: [1, 2, 12, 5, 21, 1, 11, 7, 9, 6, 20, 1, 10, 18, 3, 17, 14, 1, 23, 1]
Best cost: 6752.814 | Path: [1, 3, 17, 14, 6, 20, 10, 1, 7, 11, 9, 23, 1, 18, 12, 2, 5, 1, 21, 1]
Best cost: 6424.190 | Path: [1, 6, 14, 17, 3, 20, 7, 1, 11, 10, 18, 5, 2, 1, 9, 23, 21, 1, 12, 1]
Best cost: 6226.940 | Path: [1, 18, 10, 12, 2, 5, 3, 1, 7, 11, 20, 6, 14, 1, 17, 21, 23, 1, 9, 1]
Best cost: 6221.677 | Path: [1, 3, 17, 14, 6, 20, 7, 1, 11, 2, 12, 10, 1, 18, 5, 21, 23, 1, 9, 1]
Best cost: 6152.125 | Path: [1, 9, 6, 14, 17, 3, 1, 11, 7, 20, 23, 2, 1, 12, 5, 21, 10, 1, 18, 1]
Best cost: 6039.823 | Path: [1, 7, 11, 3, 17, 14, 1, 18, 10, 12, 2, 5, 1, 20, 6, 23, 21, 1, 9, 1]
Generation: #10
Best cost: 5982.271 | Path: [1, 7, 11, 10, 12, 2, 1, 21, 14, 17, 3, 20, 1, 9, 6, 23, 5, 1, 18, 1]

OPTIMIZING each tour...
Current: [[1, 7, 11, 10, 12, 2, 1], [1, 21, 14, 17, 3, 20, 1], [1, 9, 6, 23, 5, 1], [1, 18, 1]]
[1] Cost: 1536.744 to 1496.639 | Optimized: [1, 10, 12, 2, 11, 7, 1]
[2] Cost: 1620.125 to 1611.912 | Optimized: [1, 3, 17, 14, 21, 20, 1]

ACO RESULTS
[1/300 vol./1496.639 km] Berlin Hbf -> Bremen Hbf -> Dortmund Hbf -> Düsseldorf Hbf -> Leipzig Hbf -> Dresden Hbf --> Berlin Hbf
[2/295 vol./1611.912 km] Berlin Hbf -> Frankfurt Hbf -> Mannheim Hbf -> Karlsruhe Hbf -> Saarbrücken Hbf -> Würzburg Hbf --> Berlin Hbf
[3/285 vol./2123.459 km] Berlin Hbf -> München Hbf -> Stuttgart Hbf -> Freiburg Hbf -> Aachen Hbf --> Berlin Hbf
[4/ 30 vol./ 701.943 km] Berlin Hbf -> Kiel Hbf --> Berlin Hbf
OPTIMIZATION RESULT: 4 tours | 5933.953 km.