Home
Projects
  Software
  Downloads
  Links
  Authors
  Contact Us

 

 

 

Solving The Traveling Salesman Problem - Nature's Way by Mike

A traveling salesperson is getting ready for a very long trip to visit a number of her potential clients in different cities. She is rather tired of traveling and her boss tells her that there were large cut backs in this year's travel budget. So she wonders what is the best round-trip route from client to client that in the end will yield the shortest traveling distance overall, saving the company money and reducing her time away from home.

The traveling salesman problem, called TSP for short, is a famous optimization puzzle that researchers and hobbyist mathematicians have been solving for many years. This project was a short exploration into solving the TSP with nature-inspired optimization algorithms. I looked at three techniques to solve the TSP: simulated annealing, genetic algorithm, and ant colony optimization.

TSP Background

The origin of the traveling salesman problem can be traced back to mathematician and economist Karl Menger in Austria in the 1920's and has since gained notoriety as a difficult problem in combinatorial optimization. The puzzle stated more formally is: given a number of cities and the cost to travel between any two, find the least expensive tour for visiting each of the cities once and returning to the city of origin. For all but the smallest number of cities to visit, examining all possible tours one by one is out of the question because of their large number. For n cities, the number of possible tours is n factorial (n!). So for example, a small TSP problem with only 13 cities has over 6 billion possible tours. Mathematicians and computer scientists have developed many algorithms for efficiently searching the TSP solution space for the shortest path without having to perform an exhaustive search. In addition to its importance in mathematics, the TSP problem has application in many scheduling problems including network routing, urban transportation systems, and printed circuit manufacturing.

Simulated Annealing

Simulated annealing (SA) is a search algorithm for finding a good approximation to the global minimum or maximum of a function in a large search space. As applied to the TSP, the function is the cost of the round-trip tour, the search space is the set of all possible tours, and the optimization objective is to find a minimum cost tour.

SA is based on the way in which liquids crystallize in the process of annealing. In the annealing process a liquid, initially at high temperature, is slowly cooled. As cooling proceeds, the system becomes more ordered and approaches a frozen state with minimum energy. If the initial temperature of the system is too low or cooling is done too rapidly then the system may not reach a minimum energy state, creating poorly formed crystals. Slow cooling near thermodynamic equilibrium gives the molecules of the liquid more chances of finding optimum configurations with lower internal energy.

The TSP SA algorithm searches for the minimum tour by randomly perturbing the solution in a manner similar to the way the physical annealing process rearranges molecules during cooling. A tour perturbation is accepted with a probability that depends on the difference between the cost values of the old and new tours and on T (called temperature), that is gradually decreased during the process. When T is large, the solution changes almost randomly, accepting perturbations with both lower and higher costs. This allows for a broad sampling of the search space and prevents the search from getting "trapped" in a local cost minimum that could be much higher than the global cost minimum. But as T decreases the chances for accepting a new tour with greater costs decreases, until T reaches 0, the "frozen" state.

In my simulation, I used Bot objects to represent cities and a Group object to represent a tour. Cities are ordered within the tour according to the sequence in which they would be visited. The important controlling parameters are the annealing schedule (initial temperature and rate of reduction) and number of trials to perform for each temperature. The following pseudo code gives the overall logic of the simulation:

Create and locate a number of city (Bot) objects on the screen
Create a tour (Group) object to hold the cities
Create a best tour (Group) object to hold the shortest tour found
Randomly shuffle the order of the cities in the tour
For each temperature (T) do
    For number of trials to do at each temperature
        Perturb current tour by randomly interchanging two cities
        Calculate the new tour cost (distance)
        If cost of new tour is smaller than old tour then
            Accept the tour
            Copy the tour to best tour
        End if
        If cost of new tour is larger than old tour then
            Calculate cost difference (dE) between old and new tours
            Accept the new tour with the probability = exp(-dE/T)
        End if
    Next trial for this temperature
    If no improvement this temperature then stop
    If max number of temp trials exceeded then stop 
    Lower the temperature by a reduction factor
Next temperature

Genetic Algorithm

Like Simulated annealing, a genetic algorithm (GA) is a search technique used to find approximate solutions to optimization problems. Genetic algorithms are inspired by evolutionary biology and simulate the strategies of natural selection, inheritance, recombination (or crossover) and mutation for evolving optimal solutions.

Genetic algorithms maintain and evolve a pool of candidate solutions to an optimization problem. The evolution starts from a population of completely random solutions. In each generation, multiple solutions are selected from the population based on their fitness and then "mated" by recombination and mutation to form a new generation of solutions. The least fittest solutions from the previous generation are then discarded and replaced by the offspring solutions, forming a new (and improved) population to use in the next iteration of the algorithm. The algorithm continues to evolve the population until some stopping criteria is met.

In my TSP GA algorithm, the population of TSP solutions is maintained in an array, sorted from most fit to least fit. Fitness is assessed based on the inverse of tour distance (1/distance). During each iteration, a probabilistic rule called the "roulette wheel" method is used to decide which tours to select for mating - the higher a tour's fitness the higher probability that it will be selected. Mating pairs of solutions bear two offspring, each inheriting pieces of their parent's tours in a process called recombination. Recombination is performed by splicing a "piece" of one parent's tour into a copy of the other parent's tour, taking care to avoid duplicate cities. After recombination, the offspring tours are mutated, typically with a small probability (less than 20%). There are two options for mutation: the tour positions of two randomly chosen cities are swapped (called inversion), or the position of one randomly chosen city is moved to another randomly chosen position in the tour. After the new offspring are created, they replace the least fittest solutions from the previous iteration, and then the population array is resorted based on fitness. The simulation ends after a specified number of iterations.

Important controlling parameters are the population size, the number of mating pairs per iteration, crossover type, mutation type, and mutation rate. The following pseudo code gives the overall logic of the simulation:

Create and locate a number of city (bot) objects on the screen
Create a population of randomly shuffled tours (Groups)
Calculate fitness for each member of population (1/distance)
Sort population based on fitness
Repeat Until stopping criteria is met
    for desired number of mating pairs:
        Select tour pair to mate based on fitness
        For each of two offspring do:
            Inherit parent tours
            Perform cross-over (OX or PMX - see literature)
            Perform mutation (inversion or splice)
        Next offspring
        calculate fitness (1/distance)
    Next mating pair
    Replace least fittest of the population with new generation
    Sort population based on fitness
Loop

Ant Colony Optimization

The Ant Colony Optimization algorithm (ACO) has been used to finding optimal paths through graphs like the TSP. The technique uses many agents to traverse a solution space in order to search for optimal traversals. Real ants wander randomly and upon finding a food source, return to the colony while laying down a pheromone trail for other ants to find. Over time the pheromone evaporates so the trail must be continuously reinforced by the returning ants in order for it to survive. The longer the trail, the more time it takes for ants to follow the path and return, the more the pheromone trail weakens, and the less effective it becomes in attracting other ants. On the other hand, a shorter path gets reinforced by the ants more often as it evaporates, resulting in a stronger pheromone trail and a higher chance of attracting more ants. Repeated over and over again, this system behavior tends to reward shorter paths and penalize longer ones, resulting in a minimization of the path length to the food source. The idea of the ACO algorithm is to mimic this behavior with simulated ants searching for the shortest path through the graph representing the problem to solve.

In my ACO simulation, three large arrays (with dimensions n by n where n is the number of cities) are used. A distance array for precalculating all city to city distances, a pheromone array for keeping track of the amount of pheromone trail between all cities, and a delta pheromone array for temporarily storing the pheromone to add to the pheromone array at the end of a simulation iteration. The delta pheromone array is needed so that all ants sense the results of the previous iteration simultaneously.

Important controlling parameters are the number of ants, parameters for computing an ant's next move probability, the amount of pheromone to add per tour, and the pheromone evaporation rate. The following pseudo code gives the overall logic of the simulation:

Create and locate a number of city (bot) objects on the screen
Create a tour (Group) object to hold the cities
Create a best tour (Group) object to hold the shortest tour found
Randomly shuffle the order of the cities in the tour
Create number of ants equal to number of cities
Initialize distance, pheromone, and delta pheromone edge arrays
For each iteration do
    For each ant in colony do:
        Do until all cities visited once:
            Decide which city to go to next:
                Calculate visibility (1/distance) to each city
                Sense pheromone trail to each city
                Choose move with prob related
                    to (visibility^alpha)*(trail^beta)
            Move to next city
        Loop
        Calculate tour cost (distance)
        If cost of new tour is smaller than best tour then:
            Copy the tour to best tour
        End if
        Add pheromone to each edge of this ant's tour:
            Amount of pheromone to add based on 1/(tour cost)
            Add to delta pheromone array
    Next ant
    Evaporate pheromone array by some factor
    Add delta pheromone array to pheromone array
    Clear delta pheromone array
Next iteration

Summary of What I Learned

I tested the three algorithms on three geometric patterns of increasing number of cities. The three patterns had 16, 44, and 91 cities respectively. Here are some of my observations:

Performance

It is difficult to judge performance of the three techniques based on convergence time because I opted to code these for clarity of understanding rather than for efficiency. For example, there are improvements to be made in the way the cost function is calculated for SA, where I calculate the cost by summing distances over the entire tour, rather than considering only those segments that are affected by the perturbation. Also, the SA and ACO draw both intermediate tours (blue) as well as the best tour (red), and so must update the screen far more often than GA, which only draws the best tour. For a large number of cities, these inefficiencies are probably significant.

However, a couple of observations about performance seem obvious, the above not withstanding. For the 16 city problem, the GA seems to converge on an answer significantly faster than SA and ACO, with convergence resulting in an optimum solution most of the time. For the 44 and 91 city cases, it was no contest - the ACO solved these as fast as it did the 16 city problem. I think this is because ACO optimizes the traversal locally, based on a local cost measure (visibility and trail to the next city on the tour), whereas the SA and GA judge each potential solution using a global cost measure (length of the entire tour). For large number of cities, improving the solution via global cost measures can require a very large number of perturbation attempts.

Memory

There are large differences in the amount of memory utilized by each of the algorithms. SA needs the least as it does not require any arrays. GA requires several population arrays dimensioned to up to about 5 times the number of cities. However, ACO requires a substantial amount of memory above what is required by the other two techniques - about 2 or 3 times the number of cities squared in order to store pheromone trails and precalculated city to city distances. The distance array is actually not needed, as the distances between cities can be computed on the fly (but less efficiently).

Ease of Use

My favorite of the three techniques is SA because of its simplicity. This simplicity leads to more flexibility, making it very easy to adapt SA to a wide range of optimization problems, not just TSP. All you need for SA is a cost function and some reasonable way of making perturbations. On the other extreme is ACO, which because of its structure, is difficult to adapt to problems other than those which can be formulated in terms of a graph traversal problem. Both GA and ACO have more parameters than SA and a considerable amount of tweaking is needed to find optimal settings for each problem.

On a Philosophical Note...

I'd like to share a realization that occurred to me while I was working on this project. During my almost 25 years working as a scientist in a large corporation, there has been much discussion about the seemingly competing roles of process standardization versus the fostering of creativity and ingenuity - or freedom to act "outside the box". I believe there is something to learn from a common underpinning in these nature-inspired optimization algorithms. In all three, agents dutifully follow a common set of rules for decision making. The fact that they can rely on each other to abide by the same set of rules is in part why they are able to collectively reach a global optimum. If on the other hand, each selfishly sought their own local optimum at the expense of their teammates, failure in a global sense would surely be guaranteed. Yet, a critically important aspect of all of these algorithms is their heavy reliance on the occasional "errant" departure from the norm. In the case of GA, the departure takes place in the form of mutation, in SA, the acceptance of a larger cost solution at high temperatures, and in ACO, the occasional mis-wandering by the ant off the beaten path. These departures are critical for preventing the system from becoming trapped in local minimums - like ants following each other in an endless circle, or not unlike corporate drones faithfully reinforcing a sub-optimal behavior because "its the way we have always done it". What may seem like an errant departure from the norm at the time, may eventually lead to a better solution in the long run. So the moral of the story - both process standardization AND "ingenuity" are necessary to move towards a global optimum and in this sense, they are not competing mantras, but integral components of a nature-tested winning strategy.

The TSP simulations discussed on this page are included with the VisualBots software download.

References:

http://www.tsp.gatech.edu - The best site on the web for information on the TSP.

http://www.tsplib.com - A source for TSP problem sets.

Ant Colony Optimization, Genetic Algorithm, and Simulated Annealing on Wikipedia - the free encyclopedia.

Back to projects main page       <<prev project       next project>>

 

 
 
Home Projects Software Downloads Authors Links Contact Us
Copyright ©   This sited last updated Jan 05, 2008