tsp

A Traveling Salesman on a Sphere: Pitbull’s Arctic Adventure

You’ve probably heard of the traveling salesman problem: given a set of cities, what is the shortest possible route that visits each city exactly once and returns to the origin city? This problem pops up in a huge number of applications.

For the purposes of this post, let’s focus on a concrete example: a musician is planning a tour and wants to travel as efficiently as possible. Suppose that concerts are to be held in the following cities (with an encore concert in the origin city): Anchorage, Athens, Barrow, Berlin, Brussels, Cairo, Copenhagen, Denver, Detroit, Dublin, Helsinki, Istanbul, Jerusalem, Kabul, Lagos, Lisbon, London, Los Angeles, Miami, Moscow, Murmansk, New York, Prague, Reykjavik, Rome, Seattle, Stockholm, and Toronto.

Perhaps you’ll object that this is an unlikely list, because it includes several high latitude cities with limited concert venues. Musicians can get sent to some pretty obscure locations, though. Take the rapper Pitbull’s recent trip to Kodiak, for example. Bear with me.

Fortunately, Mathematica has a built-in function, FindShortestTour to help compute the solution to our problem. Before we get too excited, though, we should remember that the world is not flat, and hence we won’t be able to use our usual Euclidean distance metric.

I’ll walk you through some code that computes and displays the solution to our hypothetical problem. Most of this code is adapted from an example in Mathematica’s documentation. For those who want the end picture with no code, here you go:

Now, let’s turn to the code. First, we can make use of Wolfram’s extensive databases, and find the latitudinal and longitudinal coordinates of all of our cities.

Next, we want to be able to convert things from latitude and longitude to Cartesian coordinates 3-space. We will work in units of kilometers. The position function below accomplishes this:

The length of an arc between two points with specified latitudinal and longitudinal coordinates can be computed using the distance function defined below. Note the use of Mathematica’s built-in function VectorAngle.

The built-in FindShortestTour command can accommodate our distance metric. We define “tour” to be the list Mathematica returns. The first element of this list is the total distance of the journey; the second is the list of indices corresponding to the cities, and it specifies the order in which they are to be visited.

Now we get to the interesting part–displaying our results.

First, we’d like a function to connect a given pair of cities with a nice arc. The most important feature of this chunk of code is the use of RotationTransform. This built-in function takes three arguments. The first is an angle, the second is a pair of vectors. The pair of vectors in the second argument specify the plane in which the rotation will occur. RotationTransform outputs a function (a rotation matrix) that can be applied to a point.

I use NestList (as you will see, this is one of the Mathematica functions I use the most) to iteratively perform rotation transforms, outputting a sequence of positions on the surface of the sphere between the two cities.

Next, we’d like to make nice arcs between each pair of consecutively cities. To get pairs of consecutively visited cities, we use the Partition function. Then we apply our makearc function (defined above) onto the result.

When we display everything, we want a sphere, a map of the countries of the world, dots for cities, and the path taken by our tour. The latter two things we have all but done, and the first is trivial. Now we just need a map of the countries. To do this, we again use Mathematica’s databases:

Each country has an associated schematic polygon, with vertices specified by longitude and latitude. We need to translate that into Cartesian 3-space, and then draw lines connecting these vertices. This can be accomplished with the following command:

Finally, putting everything together into a single graphics display:

In Mathematica, it is east to grab and spin the globe for whatever perspective you want:

The following two tabs change content below.

Ben Nolting

Ph.D. Candidate at University of Nebraska

Leave a Reply