liveBook · Manning

Traveling Salesman problem

As explained in Optimization Algorithms: AI techniques for design, planning, and control problems

The traveling salesman problem is the optimization problem of finding a minimum-time tour that visits every specified city, potentially with asymmetric travel times between cities.

The traveling salesman problem (TSP) asks for a tour that visits every specified city and returns to its starting point while minimizing a chosen cost, such as travel time, distance, or price. The decision is the order of visits: a permutation of the cities. A fast route need not be the shortest route if travel times differ from distances. section 2.1.1 section 1.4.3

Direction matters. In symmetric TSP, travel costs are the same both ways, so the cities and connections form an undirected graph. In asymmetric TSP, a return trip may cost differently or a connection may exist in only one direction. Traffic patterns and one-way streets make the asymmetric case practical, not merely theoretical. section 2.1.1 section 1.4.3

Why not check every tour?

For 13 cities, fixing the starting city still leaves 12! = 479,001,600 possible visit orders in the asymmetric case. Exhaustively checking them is a poor real-time plan. Dynamic programming can reduce the work; approximate search methods offer another practical route when the search space is too large. section 2.1.1 section 1.4.3

To turn a routing task into a TSP instance:

  • Choose the stops, the starting location, and the cost to minimize. For a delivery truck, that might be total road distance over a round trip. section 5.6
  • Build inter-stop costs. One truck example uses GPS coordinates, an OSMnx road graph, and NetworkX shortest paths; a 20-city example instead builds a distance matrix from coordinates. section 5.6 section 8.6
  • Represent a candidate as a city order, sum its consecutive leg costs, and include the return leg. Keep the search feasible: a permutation visits each city once, while a repair operator can rotate a tour to a designated start. section 8.6 section 9.6

The solver can change without changing the underlying routing question. Simulated annealing improved an initial 593.88 km route among 18 Greater Toronto Area stores to a 227.17 km route. A separate 20-US-city implementation uses a genetic algorithm with permutation sampling, order crossover, inversion mutation, and start-city repair. These are found routes, not demonstrations that no shorter tour exists. section 5.6 section 8.6

problem = TravelingSalesman(cities,distance_matrix)                          #1
 
algorithm = GA(
    pop_size=20,
    sampling=PermutationRandomSampling(),
    mutation=InversionMutation(),
    crossover=OrderCrossover(),
    repair=StartFromZeroRepair(),
    eliminate_duplicates=True
)                                                                             #2
 
termination = DefaultSingleObjectiveTermination(period=300, n_max_gen=np.inf) #3
 
res = minimize(
    problem,
    algorithm,
    termination,
    seed=1,
    verbose=False
)                                                                              #4

Inspect how permutation sampling, crossover, mutation, and repair are configured together in a genetic-algorithm TSP solver. section 8.6

Other approaches need different representations. A particle-swarm example sorts a particle’s values to obtain a city order, then scores the tour. A graph-learning pipeline predicts promising nodes or edges and uses graph search to assemble a feasible candidate; its worked application uses a pretrained model rather than training one from scratch. Compare routes using the same stops and cost definition before drawing conclusions about solvers. section 9.6 section 11.7

How Optimization Algorithms: AI techniques for design, planning, and control problems covers it

How deep it goes
Defines tour structure and search-space growth, then works through solver implementations; does not establish that their found tours are optimal.
Examples
Uses a 13-city counting example, an 18-store delivery route, and 20-city US tours.
What it assumes
Introduces tours and permutations in plain terms; assumes Python familiarity for the worked library implementations.
What you can do after
Apply it via optalgotools simulated annealing or pymoo genetic algorithms to seek and score round-trip routes.
How it connects
Connects permutations and graph distances to dynamic programming, simulated annealing, genetic algorithms, particle swarm optimization, and graph neural networks.

Also explained in

Compare how 6 books explain Traveling Salesman problem →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Optimization Algorithms: AI techniques for design, planning, and control problems. The section links point to the parts of the book it draws on.