A library for solving instances of the travelling sales agent problem
In this tutorial we will see how to use tsp
to solve instances of the
Travelling Salesman Problem
Assuming we have the following distance matrix:
>>> import numpy as np
>>> distance_matrix = np.array(((0, 5, 2, 9), (5, 0, 3, 1), (2, 3, 0, 7), (9, 1, 7, 0)))
We can obtain a tour using the following:
>>> import tsp
>>> tour = tsp.run_2_opt_algorithm(
... number_of_stops=4,
... distance_matrix=distance_matrix,
... iterations=1000,
... seed=0,
... )
We can see the tour here:
>>> tour
[0, 3, 1, 2, 0]
The tsp
library includes further functionality which you can read in the
How To guides.
To obtain a basic tour, we use the tsp.get_tour
function:
>>> import tsp
>>> tsp.get_tour(number_of_stops=5)
[0, 1, 2, 3, 4, 0]
If we pass a random seed this will also shuffle the interior stops (in a reproducible manner):
>>> tsp.get_tour(number_of_stops=5, seed=0)
[0, 3, 4, 2, 1, 0]
To swap two stops for a given tour, we use the tsp.swap_stops
function:
>>> tour = [0, 1, 2, 3, 4, 5, 0]
>>> tsp.swap_stops(tour=tour, steps=(2, 4))
[0, 1, 4, 3, 2, 5, 0]
To calculate the cost of a given tour, we use the tsp.get_cost
function:
distance_matrix = np.array(((0, 5, 2, 9), (5, 0, 3, 1), (2, 3, 0, 7), (9, 1, 7, 0)))
tour = [0, 1, 2, 3, 0]
tsp.get_cost(tour=tour, distance_matrix=distance_matrix)
which gives:
24
To plot a tour we use the tsp.plot_tour
function:
xs = (0, 1, 1, 2.5)
ys = (0, 5, 1, 3)
tour = [0, 1, 3, 2, 0]
tsp.plot_tour(x=xs, y=ys, tour=tour)
This gives the following image:
To run the full algorithm, we use the
tsp.run_2_opt_algorithm
function:
distance_matrix = np.array(((0, 5, 2, 9), (5, 0, 3, 1), (2, 3, 0, 7), (9, 1, 7, 0)))
tour = tsp.run_2_opt_algorithm(number_of_stops=4, distance_matrix=distance_matrix, iterations=1000, seed=0)
tour
This gives:
[0, 3, 1, 2, 0]
This software implements the 2-opt algorithm for the travelling sales agent problem.
As an example, if we consider three cities with the following matrix defining their distances between them:
Note that the distance matrix is not symmetric, it is a lot further to go from the 3rd to the 1st city (9) than to go from the 1st to the 3rd (1)
If a tour starts and finishes at the first city there are in fact 2 possibilities:
The cost
- For
$t=(0, 1, 2, 0)$ we have$c(t)=4 + 2 + 9=15$ - For
$t=(0, 2, 1, 0)$ we have$c(t)=1 + 2 + 2=5$
As the size of our problem grows the complexity of finding the optimal tour grows in complexity. In fact this problem is NP-hard, which puts it in a class of problems for which a general solution cannot be obtained efficiently for any given sized problem.
One solution approach of this is the 2-opt algorithm which is what is implemented in this software.
The 2-opt algorithm is an example of a neighbourhood search algorithm which means that it iteratively improves a given solution by looking in at other candidates near it.
The 2-opt algorithm does this by randomly choosing two stops in a tour, and
swapping the order between them. Essentially picking stop
Once this candidate tour is obtained the cost is evaluated and if it is good it is accepted as the new solution.
This has the effect of essentially untangling a given tour.
There are various other algorithms for solving the TSP. They includ:
- Trying all possible permutations.
This software implements 5 functions:
plot_tour
get_tour
swap_cities
get_cost
run_2_opt_algorithm
The wikipedia page on the TSP offers good background reading: https://en.wikipedia.org/wiki/Travelling_salesman_problem
The following text is a recommended text on neighbourhood search algorithms:
Aarts, Emile, Emile HL Aarts, and Jan Karel Lenstra, eds. Local search in combinatorial optimization. Princeton University Press, 2003.