Math, programming, ecology, and more

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 inMathematica’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:

1

2

3

4

earthRad=6378.7;

positionVec[{u_,v_}]:={Cos[v °]Cos[u°],Sin[v°]Cos[u°],

Sin[u°]};

position[{u_,v_}]:=earthRad*positionVec[{u,v}]

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.

1

2

distance[{u1_,v1_},{u2_,v2_}]:=earthRad*VectorAngle[

positionVec[{u1,v1}],positionVec[{u2,v2}]];

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.

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.

1

2

3

4

5

6

makearc[{u1_,v1_},{u2_,v2_}]:=

Module[{city1=position[{u1,v1}],city2=position[{u2,v2}],dist},

dist=VectorAngle[city1,city2];

NestList[(Evaluate[

RotationTransform[dist/Ceiling[100*dist],{city1,city2}][#]])&amp;,

city1,Ceiling[100dist]]]

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.

1

2

routePic=Apply[makearc,

Partition[citylocations[[tour[[2]]]],2,1],{1}];

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:

1

countries=CountryData["Countries"];

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: