Click here to Skip to main content
Email Password   helpLost your password?

Introduction

I have recently become very interested in the area of genetic algorithms and Ant Colony Optimization techniques. I was determined to write a complete program demonstrating these two techniques. In particular I wanted to compare the efficiency of these two approaches in the area of finding solutions to the Traveling Salesman Problem (TSP).

Traveling Salesman Problem

Buckland (2002, pp. 118) succinctly summarizes the problem as, �Given a collection of cities, the traveling salesman must determine the shortest route that will enable him to visit each city precisely once and then return back to his starting point.�

He goes on to explain that this is an example of what mathematicians call NP-Complete problems. As more cities are added, the computational power required to solve the problem increases exponentially. An algorithm implemented on a computer that solves the TSP for fifty cities would require an increase in computer power of a thousand-fold just to add an additional ten cities. The following table demonstrated the problem:

Clearly, a "brute force" way becomes impossible for a large number of cities and alternate algorithms need to be employed if a solution is to be found for this problem.

Background

The Algorithms Explained

Genetic Algorithm

The algorithm consists of the following fundamental steps:

  1. Initialization: chromosomes are randomly created. At this point, it is very important that the population is diverse. Otherwise, the algorithm may not produce good solutions.
  2. Evaluation: each chromosome is rated on how well the chromosome solves the problem at hand. A fitness value is assigned to each chromosome.
  3. Selection: the fittest chromosomes are selected for propagation into the future generation based on how fit they are.
  4. Recombination: individual chromosomes and pairs of chromosomes are recombined, modified and then put back into the population.

I am not going to discuss the details of genetic algorithms, as these are better explained elsewhere, apart from discussing the mechanisms used to produce valid crossover operators and Roulette Wheel Selection. Each chromosome is encoded as a possible tour. For example, a tour of five cities might be encoded as 3,4,0,1,2. A difficulty with the TSP is that a simple crossover will not work. Consider the following situation where crossover occurs at position 3.

Parent 1 1 2 3 4 5
Parent 2 3 5 2 1 4
Child 1 1 2 3 1 4
Child 2 3 5 2 4 5

A problem that has occurred here is that Child 1 has visited city 1 two times, which is not allowed, and Child 2 has not visited city 1 at all. A defacement encoding must be used in order to make sure that only valid tours are produced. A well-known and perhaps the simplest to understand crossover operator is called the Partially Matched Crossover. Buckland (2002, pp. 130-132) explains this technique as follows:

A crossing region is picked.

Parent 1: 12x34x5

Parent 2: 35x21x4

The following mapping is established.

3 -> 2

4 -> 1

Now we cycle through each parent gene and swap the genes wherever a gene is found that occurs in the above mapping.

First iteration (mapping 3 with 2):

Child 1: 13245

Child 2: 25314

Second iteration (mapping 4 with 1):

Child 1: 43215

Child 2: 25341

The final crossed-over genes are valid permutations with no duplicates.

Roulette Wheel Selection is a technique of choosing members from the population of chromosomes in a way that is proportional to their fitness. Buckland illustrates, �Imagine that the population�s total fitness score is represented by a pie chart or roulette wheel. Now you assign a slice of the wheel to each member of the population. The size of the slice is proportional to that chromosome�s fitness score: the fitter a member is, the bigger the slice of pie it gets. Now, to choose a chromosome, all you have to do is spin the wheel, toss in the ball and grab the chromosome that the ball stops on.� Buckland (2002, pp. 100)

Ant Algorithm

This algorithm is inspired by observation of real ants. Individually, each ant is blind, frail and almost insignificant. Yet, by being able to co-operate with each other, the colony of ants demonstrates complex behavior. One of these is the ability to find the closest route to a food source or some other interesting landmark. This is done by laying down special chemicals called "pheromones." As more ants use a particular trail, the pheromone concentration on it increases, hence attracting more ants. In our example, an artificial ant is placed randomly in each city and, during each iteration, chooses the next city to go to. This choice is governed by the following formula, as described by Dorigo (1997):

Each ant located at city i hops to a city j selected among the cities that have not yet been visited according to the probability:

where:

  • is the probability that ant k in city i will go to city j.
  • is the set of cities that have not yet been visited by ant k in city i.
  • is the relative importance of the pheromone trail.
  • is the relative importance of the distance between cities.

Therefore the probability that a city is chosen is a function of how close the city is and how much pheromone already exists on that trail. It is further possible to determine which of these has a larger weight by tweaking with the and parameters. Once a tour has been completed (i.e. each city has been visited exactly once by the ant), pheromone evaporation the edges is calculated. Then each ant deposits pheromone on the complete tour by a quantity which is calculated from the following formula (Dorigo 1991):

if , where:

Quick User Manual

At the heart of the application lies the two algorithms which are used to solve the traveling salesman problem. Each algorithm is brought into a view by selecting it from a toolbar of the main frame. A view is a window which is presented to the user. The genetic algorithm view is shown below:

Each view is divided into two horizontal sections.

  1. The top half shows a graphical view of the current performance of each algorithm.
  2. The bottom half shows a graph of the current solution charted against the best know solution. In most demonstrations of the TSP, cities are placed at random. I chose to distribute the cities in a circular fashion. This allows an easy way to calculate the shortest path among the cities, as well as visually assess how far away the algorithm is from the optimal solution.

A Simulation is Controlled by the Control Buttons

Each button is self-explanatory. The Start button is used to start the application and the Stop button is used to stop the simulation. The Reset button resets the simulation while the Step button is used to step through the simulation. The text information shows the following information:

Graph of the above information:

The blue line is a plot of Epoch vs. the shortest distance found by the algorithm to date. The green line represents the shortest possible tour (optimal solution). When the two lines converge, a solution has been found.

Design and Implementation

I choose C++ as the language to program this application in. A compiled program will always be faster than an interpreted program. Both of the algorithms are computationally quite expensive and so is the process of visualization. Potentially C would be faster still, but by choosing this language the benefits of object orientation are lost. Other possible languages were Delphi, Visual Basic and C#, but I don�t have enough experience in any of these languages to attempt a project of this size. I chose to use the Microsoft Foundation Classes (MFC) application framework. The benefits of using this are:

One of the main disadvantages is that it takes a long time to understand the complexity of the underlying model. Also, once the developer tries to move away from wizard-generated code, it is very easy to get lost in the code with the debugger. The project was developed and compiled using Visual Studio .NET Academic Version 7.1.3088. The code used to implement the genetic algorithm is based on the work of Mat Buckland (2002) in his book "AI Techniques for Game Programming." The code for the ACO algorithm is partially based on the work of M Jones (2003) in his book "AI Application Programming." Several other code sources were also used:

Initially I thought that the document would hold information common to both algorithms, including the number of cities, city position and shortest possible tour. However, as the project progressed, I realized that each algorithm is sufficiently different to warrant its own data structure. What was needed was a way to switch between different views (derived from a common ancestor).

�Sometimes the way a document is being visualized needs to be significantly changed. For example, in PowerPoint, you can see the slides in a WYSIWYG form or view only the text in a text edit window. In the MFC world, one can find an amazingly large quantity of programs that implement this behavior, defining one CView descendant and making it responsible for all visualization changes. This path has several disadvantages:

  1. Big, difficult to manage class definition file.
  2. Diminished reusability: you could reuse one big CView descendant or nothing.
  3. Hard to maintain (what if you want to modify or add a new "look" to the document?).
  4. Wasted memory: some variables (objects) will exist in memory and won�t be used.� (Lodos,1999)

The author then goes on to explain how this is possible and gives an example of code that defines different Views and how to switch between them.

The following sequence diagram shows the basic interaction between the CACOView and CAntSystem classes.

The following sequence diagram shows the basic interaction between the CGAView and CGASystem classes.

Performance

I next turned my attention to the ACO and looked at how certain parameters in the algorithm affect performance.

Parameters used here are City No: 40, alpha: 1, beta: 1 and rho: 0.6. These findings suggest that it might be possible to write a genetic algorithm that would optimize the parameters. This might make an interesting project somewhere down the track.

Conclusion

This has been both a rewarding and difficult project. I previously had little knowledge of MFC programming and thought that it would be relatively easy to master. It turns out that I was wrong and it took me a very long time to get the program up and running. Despite the steep learning curve, I was thrilled to actually produce a working program and learned a lot along the way about genetic algorithms and ant colony optimization algorithms. I am very grateful for all the people who have contributed to this fantastic site. I have found many of the articles very useful in this project. Keep up all the good work, guys.

References

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generalproject
bhuvibharathi
21:06 16 Feb '10  
hello sir,

i am doing project in multicast routing using aco in matlab. i found shortest path between seven nodes using improved ant colony algorithm.now i want to do future work. whether it can combine with neural network algorithm
GeneralThanks for your contribution
wukong-325
3:35 15 Jan '10  
It's very useful for me, not only in studying the algorithms, but also in studying the programing.
GeneralExcellent effort
AMR NADA
18:06 8 Dec '09  
Dear Mr. Peter ,
Your article is an excellant one
It is clear that you have exerted a great effort
I am an electrical engineer in a petroluem company
I am interested in researches about SVC allocation in a power system
Please send me Ant Matlab source code

Best regards,
GeneralRe: Excellent effort
AMR NADA
18:20 8 Dec '09  
My email is
eng_a.ahmed@yahoo.com

Please send me Ant colony matlab source code
Generalrequest
shafikhani
0:03 25 Dec '09  
dear ahmed
if you have any matlab source code for tsp , ...
please send me
abbas_shafikhani22@yahoo.com
GeneralMatlab ACO code
Olympia Roeva
3:09 7 Dec '09  
Dear Mr Kohout,

I'm working on parameter optimization of mathematical models of fermentation processes. I use various optimization techniques - classical optimization methods, as well as some metaheuristics as genetic algorithms. I want to try to apply Ant Optimization Algorithms for considered parameter optimization problem.

Can you help me with ACO codes for Matlab.

With my best regards,
Olympia Roeva

email: olympia@clbme.bas.bg
Generalrequest
shafikhani
0:10 25 Dec '09  
Dear Olympia Roeva
i'm currently Having a final project about Allocation.
i'm Wondering If You Send to Me ACO & genetic Base Code in Matlab or c.
My Email:abbas_shafikhani22@yahoo.com
your sincerely
Generalshortest genetices colony tsp (travling selse man problem)in the c# language
baghbani
9:10 14 Nov '09  
hi
Dear sir;
I need a code for genetices colony tsp (travling selse man problem)in the c# language. Please mail me if you have any. I would be glad to have the code for genetices colony algorithm tsp (travling selse man problem) solution,
Best Regards,
this program proje final university : help help help f1 f1 f1
thanks
GeneralRe: shortest genetices colony tsp (travling selse man problem)in the c# language
shafikhani
0:05 25 Dec '09  
dear baghbani
if you have any matlab or c# language source code for tsp , ...
please send me.
best regards
abbas_shafikhani22@yahoo.com
Generalshortest path routing using evolutionary algorithm
rehvathi
9:24 4 Nov '09  
Iam doing II year M.Tech.....My project is to find the shortest path with minimum cost and delay in a standard network using Non Dominated Sorting Genetic Algorithm....pls send me the matlab codings....thankful to you
Generalrequest shortest path routing using evolutionary algorithm
shafikhani
0:11 25 Dec '09  
Dear rehvathi
i'm currently Having a final project about Allocation.
i'm Wondering If You Send to Me ACO & genetic Base Code in Matlab or c.
My Email:abbas_shafikhani22@yahoo.com
your sincerely
GeneralACO Code
levbar
7:58 30 Oct '09  
Hello Petter,

I'm working on Frequency Assignment Problem as a research for my thesis. I try to discover that ANTS and Ant System

algorithms can solve FAP.

Can you help me with ACO codes. I prefere it in Matlab or C++.

My email: levbar@bgu.ac.il

Thanks.
GeneralSource Code for ACO
Meethi1982
1:35 27 Oct '09  
Dear Sir,
Currently I'm working on ACO. It will be kind enough if you send me the source code for the same in any of the following languages. C, FORTRAN, MATLAB, JAVA.
With regards
Meethi
Generalpso matlab code
mehra vishal
22:16 9 Sep '09  
sir
i am doig my thisis on pid controller in which for parameter tuning i want to used pso algorithm.there are three variable kp,ki,kd i have to find from pso. sir i request u if u have proper matlab code for pso then please give me with example .my thisis is stop at this point.
i request u ,please help me
Generalmatlab code for ant colony optimization
sedghy
0:38 9 Sep '09  
Dear sir;
I need a code for ant colony optimization algorithm in the Matlab language. Please mail me if you have any. I would be glad to have the code for travelling sales man problem solution,
Best Regards,
Roghi
Generalcode java of ant miner classifier
hamlich
9:47 30 Aug '09  
Hi
please i nead a source code java of ant-miner data classifier.
e-mail: moha.hamlich@gmail.com
Thanks
GeneralRe: code java of ant miner classifier
Learner0903
22:41 14 Dec '09  
Hi,

I urgently need Ant-Miner's source code as well, cause I'm doing a small research project which has to be finished by Feb. 2010.
Any kind of help will be much appreciated.
Please email to learner0903@gmail.com

Thanks.
Generalsource code for aco in matlab
sedghy
22:26 15 Aug '09  
Dear sir;
I need source code of ant colony algoritm in matlab.Would you please help me?
Thanks a lot;
roghi_nodet@yahoo.com
QuestionACO in VB
Hoda R
1:29 11 Aug '09  
Is there anybody to help me by sending a general ACO code in VB langulage to "roosta.hoda@gmail.com"?
Generalcode for PSO
kavithag_mit
9:50 14 Jun '09  
can u pls provide the PSO source code for simple edge detection
Kavitha
GeneralGood evening Dr
yinka26
14:49 10 May '09  
Pls I need A matlab source code for ant colony optimization (antnet algorithm) for network traffic routing.i also would like to know how to run the simulation using matlab.my email address is oluyinkaakinyemi@yahoo.com.Thanks and i hope to hear from you soon.
GeneralRe: Good evening Dr
mohannachar
17:37 31 Oct '09  
plz send matlab code for aco
GeneralMatlab Code
Masoud.1060
21:15 18 Apr '09  
Dear Mr Kohout
i'm currently Having a final project about Allocation.
i'm Wondering If You Send to Me ACO Base Code in Matlab.
My Email:masod.1060@yahoo.com
your sincerely
GeneralRe: Matlab Code
shafikhani
0:07 25 Dec '09  
Dear Masoud
i'm currently Having a final project about Allocation.
i'm Wondering If You Send to Me ACO Base Code in Matlab.
My Email:abbas_shafikhani22@yahoo.com
your sincerely
Generalaco matlab
sonia_arb
14:00 12 Apr '09  
Dear Mr Kohout
Do you have ACO base code in Matlab form
i'm currently doing a final project for clustring problem. I'm student. Would appreciate it if you could help me out.
can you send it to sonia34dz@hotmail.com
yours


Last Updated 26 Sep 2006 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010