Click here to Skip to main content
Licence CPOL
First Posted 7 Jun 2008
Views 57,359
Downloads 1,988
Bookmarked 47 times

Simulated Annealing - Solving the Travelling Salesman Problem (TSP)

By | 7 Jun 2008 | Article
This articles solves the Travelling Salesman Problem (TSP) using the Simulated Annealing Metaheuristic algorithm.

Introduction

Combinatorial optimization is the process of finding an optimal solution for problems with a large discrete set of possible solutions. Such optimizations can be used to solve problems in resources management, operations management, and quality control, such as routing, scheduling, packing, production management, and resources assignment. Meta-heuristic algorithms have proved to be good solvers for combinatorial optimization problems, in a way that they provide good optimal solutions in a bounded (usually short) time.

Examples of meta-heuristics are: simulated annealing, tabu search, harmony search, scatter search, genetic algorithms, ant colony optimization, and many others. In this article, we will be discussing Simulated Annealing and its implementation in solving the Travelling Salesman Problem (TSP).

Background

Simulated Annealing was given this name in analogy to the “Annealing Process” in thermodynamics, specifically with the way metal is heated and then is gradually cooled so that its particles will attain the minimum energy state (annealing). Then, the aim for a Simulated Annealing algorithm is to randomly search for an objective function (that mainly characterizes the combinatorial optimization problem).

Simulated Annealing's advantage over other methods is the ability to obviate being trapped in local minima. In here, we mean that the algorithm does not always reject changes that decrease the objective function but also changes that increase the objective function according to its probability function:

P = exp (-∆f/T)

Where T is the control parameter (analogy to temperature) and ∆f is the variation in the objective function.

The probability function is definitely a derivative of the Boltzmann probability distribution function.

Travelling Salesman Problem

A salesman wants to travel t o N cities (he should pass by each city). How can we order the cities so that the salesman’s journey will be the shortest? The objective function to minimize here is the length of the journey (the sum of the distances between all the cities in a specified order).

To start solving this problem; we need:

  1. Configuration setting: This is the permutation of the cities from 1 to N, given in all orders. Selecting an optimal one between these permutations is our aim.
  2. Rearrangement strategy: The strategy that we will follow here is replacing sections of the path, and replacing them with random ones to retest if this modified one is optimal or not.
  3. The objective function (which is the aim of the minimization): This is the sum of the distances between all the cities for a specific order.

Using the code

The class TravellingSalesmanProblem.cs does the job. Just instantiate a new object, and assign to it your adjacency matrix (which is a text file), then call the Anneal() method. The Anneal() method will return the shortest path (order of the cities).

TravellingSalesmanProblem problem = new TravellingSalesmanProblem();
problem.FilePath = "Cities.txt";
problem.Anneal();

Below is the code for the Simulated Annealing algorithm:

/// <summary>
/// Annealing Process
/// </summary>
public void Anneal()
{
    int iteration = -1;

    double temperature = 10000.0;
    double deltaDistance = 0;
    double coolingRate = 0.9999;
    double absoluteTemperature = 0.00001;

    LoadCities();

    double distance = GetTotalDistance(currentOrder);

    while (temperature > absoluteTemperature)
    {
        nextOrder = GetNextArrangement(currentOrder);

        deltaDistance = GetTotalDistance(nextOrder) - distance;

        //if the new order has a smaller distance
        //or if the new order has a larger distance but 
        //satisfies Boltzman condition then accept the arrangement
        if ((deltaDistance < 0) || (distance > 0 && 
             Math.Exp(-deltaDistance / temperature) > random.NextDouble()))
        {
            for (int i = 0; i < nextOrder.Count; i++)
                currentOrder[i] = nextOrder[i];

            distance = deltaDistance + distance;
        }

        //cool down the temperature
        temperature *= coolingRate;

        iteration++;
    }

    shortestDistance = distance;
}

References

  • Optimization by Simulated Annealing – S. Kirkpatrick
  • Simulated Annealing Overview - Franco Busetti
  • Metaheuristics Progress as Real Problem Solvers – Springer
  • Numerical Recipes in C: The Art of Scientific Computing

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ali Hamdar

Software Developer (Senior)
Integrated Digital Systems - IDS
Lebanon Lebanon

Member

Been programming since 2001 interested in finance, security, workflows, SharePoint and algorithms. He is an MCSD, MCDBA, MCAD, MCSD, (again), MCTS, MCPD and MCT.
My Blog: www.alihamdar.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionneed a job scheduling algoriothm Pinmember523sahi23:36 7 Jan '12  
Questionwhat are used value for SA parameters? Pinmemberaliebrahimi9841:19 5 Nov '11  
GeneralA few basic questions on the algorithm PinmemberMikeChristensen21:17 1 Aug '10  
QuestionWhat is "int iteration" used for? Pinmemberimaudi@comcast.net20:34 1 Aug '10  
Generalshortes path routing using non dominated sorting genetic algorithm Pinmemberrehvathi8:38 4 Nov '09  
GeneralLet me give you a challenge PinmemberGUI Developer2:52 18 Sep '08  
GeneralInteresting PinmemberSaurabh.Garg15:43 11 Jun '08  
GeneralRe: Interesting PinmemberAli Hamdar12:27 17 Jun '08  
GeneralRe: Interesting PinmemberSaurabh.Garg19:00 17 Jun '08  
GeneralRe: Interesting PinmemberAli Hamdar1:52 18 Jun '08  
GeneralRe: Interesting PinmemberSaurabh.Garg2:17 18 Jun '08  
All solutions will be correct only if graph of cities is a complete graph. That is think is general notion but somehow I was thinking that graph of cities is may not be a complete graph.
 
I do have objection that you call three solutions to be optimal. According to me there can only be one optimal solution. Is there any specific reason that you call 25, 20, and 19 to be optimal? I guess you may be refering to local and global mininum in that case it is fine. But there can be only one optimal solution.
 
It will be really nice if you can explain more about GetNextArrangement. How exactly does it work? What are the effects of different strategies for getting next arrangement. Also since absoluteTemperature is a hard coded number, does algorithm always terminate? What if it is trapped in local minima with a temperature higher than absoluteTemperature?
 
Anyway it is a good work and nice discussing with you.
 
-Saurabh
GeneralInput string was not in a correct format. Pinmembermashiharu11:58 10 Jun '08  
GeneralRe: Input string was not in a correct format. PinmemberAli Hamdar12:25 17 Jun '08  
GeneralInteresting Pinmembermerlin9814:14 9 Jun '08  
GeneralHave to note PinmemberUser of Users Group12:31 7 Jun '08  
GeneralRe: Have to note PinmemberAli Hamdar12:31 17 Jun '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120604.1 | Last Updated 7 Jun 2008
Article Copyright 2008 by Ali Hamdar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid