TSP in R Part 2
Last time we created a distance matrix and a time matrix for use in TSP problems. We’re using a set of locations in the Ottawa, Ontario, Canada area, but any list of locations with addresses would work. Now we’ll work through getting optimized routes to visit each address once. We’ll optimize by distance, but we also generated a ‘time matrix’ and could run the TSP solver that way.
We’ll start this time by loading the data we saved last time:
Much of this post is informed by the vignette in the TSP package. There’s lots of great documentation in a more ‘tutorial’ type format than the official documentation in R vignettes.
We’ll start by making these matrices ‘TSP’ compatable objects.
Note that we use the ATSP function. The matrices we’re feeding in aren’t symmetrical. It may take longer or be farther to go from point A to point B than it is to go from B to A. This could be due to traffic, one way streets, the number of left hand turns, etc.
We can look at a few properties of this ATSP class:
We can also visualize a shaded matrix of the distances between objects:
Let’s solve the TSP by each of the internal methods and see what minimum distance for each we get:
Some of the solver algorithms do much better than others at finding the optimal route. Remember, distances were given by the Google Distance Matrix API in meters, so we’re looking at a 330 km trip on the ‘worst’ solution, and closer to 260km on the good end.
How much better is that than a random tour? We can simulate a tour a few thousand times to get an estimate of the improvements that even a simple solver can give us:
This gives us a mean of 669.84389km, significantly longer than our ‘worst’ optimized route. One route came out as long as 774.348km, and the best ‘random’ route was 489.175km, which wasn’t terrible.
Concorde, Chained Lin-Kernighan (linkern
) and other solvers can only do symmetric TSP. A function will convert our ATSP problem to a TSP for use with these advanced solvers. This function doubles each city, providing an ‘in’ and an ‘out’ city, with distance 0 between them (set as cheap = 0
).
We have to set the Concorde TSP path. It seems to work only with an abspath, no ~
involved.
We’ll try solve the TSP using concorde:
Similarly, we’ll reset the path to use the Chained Lin-Kernighan (linkern
) method:
By looking at the bottom of the dot plot, we can see that the Lin-Kernighan and Concorde solvers provide the most optimal routes, and assorted substitution algorithms provide less qualified answers.
Next post we’ll discuss mapping the routes visually.
Remember that Concorde is licenced for academic use only, all other uses must be requested on the Concorde website.