Skip to main content
in France
Français

TSP Resolver

CESI project · Python, Jupyter

A delivery route with blocked roads and a required visiting order, and eight algorithms compared on graphs from 5 to 3,000 cities.

The gap to the lower bound by graph size. The lower the curve, the better the route.

The brief started from a scenario. France’s environment agency, ADEME, brings in a consultancy to help a butcher’s shop swamped with Christmas orders, which has to deliver to Rennes, Rouen, Bordeaux, Toulouse and Lyon starting from Paris. We had to find the cheapest route, counting tolls and fuel. Two constraints made it more realistic than the classic travelling salesman problem. Some roads are closed, and some cities have to be delivered before others.

There were five of us. I wrote the graph generator and set up the shared experiment plan, the one that compares every algorithm against the others.

The problem

The cost of a road is its toll, if it has one, plus the distance times the price of fuel per kilometre. We’re looking for the cheapest route that visits each city once and comes back to the depot, without taking a closed road and while keeping the required order between some cities.

Before writing any code, we had to show how hard the problem is. The classic travelling salesman problem is a special case of ours, where no road is blocked and no order is imposed. So our problem is at least as hard, which means NP-hard. With six cities you can try everything, but the number of possible routes blows up as soon as you add more. With thousands of cities, you need methods that find a good route without promising the best one.

The graph generator

Six cities aren’t enough to compare algorithms, so I wrote a generator that builds networks of any size following the same rules as the brief. Every city is connected to every other one. A road is between 1 and 99 kilometres long, fuel costs €0.15 per kilometre, and one road in five has a toll between €5 and €30. One road in twenty is closed, and about 15% of cities have to be delivered after another one.

The generator takes a random seed. So a 200-city graph with seed 3 is identical for every algorithm, and the whole comparison rests on that.

The project is split into three parts. The data side holds the generator and the instances, then come the algorithms, and last the evaluation, which computes a route’s cost and checks that it follows the constraints. A route that takes a closed road or breaks the order gets a huge penalty cost, which pushes it out of the ranking.

Eight algorithms

We tried eight, from the simplest to the most involved.

  • Nearest neighbour. It starts at the depot and always goes to the cheapest city it hasn’t visited yet. It’s fast but never goes back on a choice, so it’s mostly a baseline. We also ran it from every possible starting city and kept the best route.
  • Multi-start hill climbing. It starts from a random route and improves it with small changes as long as the cost goes down, then starts again from another route.
  • Ant colony optimisation. Virtual ants build routes and leave pheromones on the roads they take. Good roads draw more and more ants.
  • Genetic algorithm. A population of routes evolves generation after generation, through selection, crossover and mutation.
  • Simulated annealing. It sometimes accepts a worse route to escape a local minimum, and does so less and less as the “temperature” drops. The multi-start version runs it several times.
  • Tabu search. It moves to the best neighbouring route, while banning recent moves so it doesn’t go round in circles. The 2-opt variant swaps two roads at once, which uncrosses routes.
instancesresultsGraph generatorblocked roads, precedence, tollsEight algorithmsgreedy, ant colony, genetic, annealing, tabu…25 sizes × 5 seedsGap and runtimePandas, Matplotlib
The same graphs go through all eight algorithms, then we compare the gap to the bound and the runtime.

A fair comparison

Each algorithm first had its own experiment plan. We look at how it converges on a few graphs, then try a grid of parameters and keep the best ones. Tuning changes the results a lot. For simulated annealing, cooling too fast (a factor of 0.99 instead of 0.999) makes it settle too early, and the route is clearly worse however long it runs.

The shared plan then takes those parameters and runs everyone on the same instances, 25 sizes from 5 to 3,000 cities, with five seeds per size. For each result, we record the runtime and the gap to a lower bound, meaning a value we know no route can go below. Our bound takes the cheapest road out of each city and halves the sum. That lets us compare a 10-city graph and a 3,000-city one on the same scale.

What the results show

Up to about fifty cities, all the algorithms are level, and the spread between seeds is bigger than the spread between methods. Past that, differences show up fast.

The gaps in the table look huge, from 150% to over 2,000%, but that doesn’t mean our routes were four times too long. Our bound stays far below the real best route, so the gaps rank the algorithms against each other, but they don’t tell you how far each one is from the optimum.

Algorithm Largest graph handled Gap at that size
Nearest neighbour, multi-start 750 cities about 225%
Multi-start hill climbing 50 cities about 690%
Ant colony 50 cities about 460%
Genetic algorithm 500 cities over 2,000% from 200 cities
Simulated annealing 3,000 cities about 150%
Multi-start simulated annealing 3,000 cities about 150%
Tabu search 75 cities about 500%
Tabu search with 2-opt 1,000 cities about 190%

The genetic algorithm falls apart from 50 cities. Ant colony, hill climbing and plain tabu search get too slow and stop between 50 and 75 cities. For the ant colony, memory is a problem too, since it keeps a pheromone value for every pair of cities.

Runtime per instance, on a log scale. Curves stop where the algorithm gets too slow.

Tabu search with 2-opt finds the best routes up to roughly 1,000 cities, but each iteration costs more and more, and it ends up too slow. Simulated annealing, with a fixed number of iterations, is the only one that reaches 3,000 cities in reasonable time, and it gets the best gap there. Nearest neighbour with several starting points does surprisingly well. It gets close to tabu search while doing far less work.

So our final recommendation was tabu search with 2-opt up to 300 cities, simulated annealing beyond that, and nearest neighbour when you need an answer right away.

What I took from it

The right algorithm depends mostly on the size of the problem. On twenty or so cities, nearest neighbour is plenty, and bringing out a heavier method barely helps.

Multi-start only works if each restart gets enough time. For nearest neighbour, trying every starting point is cheap and clearly improves the result. For simulated annealing, with the same budget, splitting the time into several restarts leaves each one too few iterations, and the multi-start version does no better than the plain one.

The genetic algorithm probably needed a different crossover. The ordered crossover we used often breaks up the good stretches of route inherited from the parents. I’d try PMX instead.

Looking back

If I did the project again, I’d compute the exact solution on small graphs, with branch and bound for instance, to know where we really stand, and I’d use a better bound for the large ones. I’d also run the ant colony and the genetic algorithm on several cores, since their ants and individuals can be computed in parallel and everything ran on a single one. I’d try a hybrid approach, starting from the nearest neighbour route and refining it with tabu search and 2-opt. And our graphs don’t change while being solved, whereas a real delivery round has to cope with a road closing halfway through the day.

at a glance

  • ContextCESI operations research project
  • RoleGraph generator and shared experiment plan
  • TeamFive people, three weeks
  • StackPython, Jupyter, NumPy, Pandas, NetworkX, Matplotlib
  • Method25 graph sizes, 5 seeds per size
  • LinksView code