/mathjax/2.5/MathJax.js?config=Te

In this example we'll solve the Traveling Salesman Problem.

We'll construct a mathematical model of the problem, implement this model in Gurobi's Python interface, and compute and visualize an optimal solution.

Although your own business may not involve traveling salesmen, the same basic techniques used in this example can be used for many other applications like vehicle rounting, circuit design and DNA sequencing.

The Traveling Salesman Problem (TSP) is a classic problem in combinatorial optimization. It was first formulated as an integer program by Dantzig, Fulkerson and Johnson in 1954.

In this example, we consider a salesman traveling in the US. The salesman starts in New York and has to visit a set of cities on a business trip before returning home. The problem then consists of finding the shortest tour which visits every city on the itinerary.

We will formulate this problem as an integer program and implement
it in Gurobi. The implementation will also demonstrate the use of
*lazy constraints* in Gurobi.

We let the $n$ selected cities in the salesman's tour be the set of vertices $V$ of a graph. The set of edges $E$ of the graph corresponds to the different connections between each city. Since we can travel from any city to another, the graph is complete. That is, there is an edge between every pair of nodes. For each edge in the the graph we associate a binary variable \[ x_{ij} = \left\{\begin{array}{ll} 1 & \text{if edge $(i,j) \in E$ is in tour }\\ 0 & \mathrm{otherwise.} \end{array}\right. \] Since the edges are undirected, we have that $x_{ij} = x_{ji}$, and it suffices to only include edges with $i < j$ in the model.

We want to minimize the total distance travelled during the tour. Therefore, we calculate the distance $d_{ij}$ between each pair of nodes $i$ and $j$. The total distance travelled is then the sum of the distances of the edges included in the tour \[ \text{distance travelled} = \sum_{(i,j) \in E} d_{ij} x_{ij}. \]

The tour should only pass through each city once. Therefore, each node in the graph should have exactly one incoming edge and one outgoing edge. In other words, for every node $i$ exactly two of the $x_{ij}$ binary variables should be equal to 1. We write this constraint as \[ \sum_{j \in V} x_{ij} = 2 \quad \forall i \in V. \] This constraint means that the saleman should enter and leave each city exactly once.

With just this constraint on the number of edges in the tour entering and exit each node, we may produce solutions that are not connected tours. For example, the figure below shows a graph with 6 nodes and two disconnected subtours. The first subtour goes through nodes 1, 2, and 3, while the second subtour goes through nodes 4, 5, 6, and 7. Note that in this solution each node has exactly two edges in the tour incident to it. But there is no path for the salesman to travel between subtour. So we must add extra constraints to our model to eliminate these solutions.

To eliminate the subtours we add the following constraints: \[ \sum_{i,j \in S, \, i \neq j} x_{ij} \leq \left\vert{S}\right\vert - 1, \quad \forall S \subset V, S \ne \emptyset \] These constraints require that for each proper (nonempty) subset $S$ of the set of cities $V$, the number of edges between the nodes of $S$ must be at most $\left\vert{S}\right\vert - 1$.

Indeed if the number of edges were equal to $S$, it would be possible to form a subtour. For example, in the figure above the subset of nodes $S=\{1,2,3\}$ has 3 edges in the tour: $x_{13} = 1, x_{12} = 1, x_{23} = 1$. So \[ \sum_{i,j \in \{1,2,3\}, i \ne j} x_{ij} = 3 > 2 = |\{1, 2, 3\}| - 1 \] Thus, the subtour elimination constraint above is violated.

So finally the integer program formulation becomes \[ \begin{array}{ll} \text{minimize} & {\displaystyle \sum_{(i,j) \in E} d_{ij} x_{ij}} \\ \text{subject to} & {\displaystyle \sum_{j \in V} x_{ij}} = 2 \quad \forall i \in V \\ & {\displaystyle \sum_{i,j \in S, \, i \neq j} x_{ij}} \leq \left\vert{S}\right\vert - 1 \quad \forall S \subset V, S \ne \emptyset \\ & x_{ij} \in \{ 0, 1 \} \end{array} \]

If the set of cities $V$ is of size $n$, there are $2^n - 2$
subsets $S$ of $V$, excluding $S = V$ and $S = \emptyset$. Instead
of explicitally including a constraint
\[
\sum_{i,j \in S, \, i \neq j} x_{ij} \leq \left\vert{S}\right\vert - 1
\]
for each $S$. We include above constraints implicitly as *lazy constraints*.
That is, we generate and add these constraints to our model in a lazy fashion.

Initially we will have no subtour elimination constraints in our model. When Gurobi finds a feasible solution that satisifies the other constraints, we compute the shortest cycle in edges included in the tour so far. If this cycle is of length $n$, then the model is solved. Otherwise, a cycle of length $m < n$ defines a subtour, and a set $S$ with $|S| = m$. We then add the corresponding constraint to eliminate this subtour as a lazy constraint, and Gurobi continues solving this new modified model. This process continues until the shortest cycle is of length $n$, implying that all subtour elimination constraints have been satisfied.

Below is the full implementation of the model (and the associated data) in Gurobi's Python interface:

The cities which are part of the tour have been highlighted: New York, Houston, San Francisco, Seattle, Minneapolis and Denver. Click "Compute Tour" to find the optimal tour using Gurobi.

You can hover over cities to show their names and click to add and remove them from the tour.

We hope this example has taught you a bit about the traveling salesman problem and using Gurobi. We encourage you to try the example out for yourself with Gurobi. To enable this, we provide easy access to a full-featured evaluation version of Gurobi.

We are always happy to discuss your needs and answer your questions. Just contact us at your convenience.