You are on page 1of 31



Ankit Nayan (2009EE50474) Ashok Meghwal (2009EE50478) Iftikar Ansari (2009EE50482) Priyanshu Jain (2009EE50485)

This project deals with a performance evaluation of particle swarm optimization (PSO) and genetic algorithms (GA) for travelling salesman problem (TSP). This problem is known to be NP-hard, and consists of the solution containing N! permutations. The objective of the project is to compare the ability to solve the large-scale and other benchmark problems for both algorithms. The travelling salesman problem (TSP), which is known to be NP-hard in the field of discrete or combinatorial optimization, is concerned with finding an optimum path of travel in pre-located cities which are characterized using the coordinates on a map while minimizing the total cost associated with the path Also, it has been expressed as a non-deterministic polynomial (NP-hard) problem. In this project, a large-scale travelling salesman problem is solved by PSO and GA over multiple runs. In addition, these algorithms are compared to determine which one produced better results. The results show that PSO has a good convergence property (i.e., average generation) compared to GA, but in terms of the effectiveness of solutions, it is not as good as GA. Both algorithms for addressing problems can obtain solutions in similar execution time.

Formally, TSP can be defined as follows A travelling salesman who is required to visit N cities exactly one time returns to the starting node with the shortest distance. This can be admitted as a permutation problem that aims at finding the shortest distance (the minimum cost) on cities or the node to be visited. The Given N cities or nodes, a cost matrix C=[Cij] is searched for a permutation :

where Cij represents the cost of the travel from city i to j, which minimizes the distance, f( ,C).

Particle swarm optimization, which is a population-based algorithm like GAs, is an optimization technique which is based on social intelligence which exists in biological population. Social intelligence exhibits adaptive capabilities of people and animals by implementing an ‘‘information sharing’’ approach, furthermore also contributes to the creation, facilitation, and maintenance of critical behaviours.

n . i =1. A normal solution sequence of TSP with n nodes is assumed as follows: S=(bi). Here. we have briefly explained the discrete PSO algorithm.…. (2003) introduced the concepts of ‘Swap operator’ and ‘Swap sequence’ redefining PSO operators for TSP problems.2.The PSO Algorithm for TSP Wang et al.


e. and mutation) in each generation. crossover. and mutation in nature.. crossing. real. including the TSP. They have been used successfully for solving a wide variety of different problems. . is changed by probabilistic application of the genetic operators (i.GENETIC ALGORITHMS Genetic Algorithms are stochastic search techniques that are based on the ideas of selection of the fittest. selection. which is represented with different codes such as binary. and permutation. A population of chromosomes.

One of the GAs most attractive features is that GA has the ability to explore the search space by considering the entire population of the chromosomes.e. In TSP. GAs are different from random search methods. a tour). random methods consisting of a combination of iterative search methods and simple random search methods can find a solution for a given problem. While GAs carry out a multidirectional search by maintaining a population of potential solution. Initial Population and Encoding The initial population is randomly generated and each chromosome in the population represents a solution to the problem (i. The basic steps of the genetic algorithm are given in the following: (1) generate the initial population (2) evaluate the fitness of each individual (3) apply selection for individuals (4) apply crossover and mutation selected individuals (5) evaluate and introduce the reproduced individuals. The fitness of the chromosome means the tour length that is coded into it and depends on the ordering of the cities. . The GA searches the space of solutions to obtain a best approximate tour. GA is implemented in the following way. the chromosome shown in the following represents the ‘‘path’’ representation of a tour with eight cities. As a class of stochastic methods.The fitness of chromosomes is measured by the objective function. For instance..

H in that order and returning back to A. In the CX crossover. That is. The CX crossover operator is as shown. In this study.Evaluation of Fitness The evaluation function is an important indicator and is used to decide how ‘‘good’’ a chromosome is under environmental conditions. Selection The selection is applied to the population by using alternative technique such as a roulette wheel tournament. In general. D. cycle crossover (CX) and swap mutation (EM) were used. E. (ACEBFDGH) corresponds to a tour starting from city A. F. which is based on a tournament among a few individuals chosen at random from the population. rather than diversity it is used to rectify infeasible chromosomes generated by a crossover operator. The two parents are randomly recombined to create a new offspring which will be inserted into a new population in the next generation. In this study. is used. B. G. tournament selection. the selection is dependent upon the fitness level of the individuals actually existing in the population. assuming an 8-city TSP. visiting C. . an individual is chosen for transferring into the next generation if he has better fitness value than other individuals of the population. Crossover and Mutation Crossover and mutation are highly efficient evolutionary operators to successful applications of GAs. The fitness of each individual is calculated using Equation (1). A schematic illustration of how these genetic operators work is given in Figure 1. Crossover can maintain good common genes in the parents. the recombination is performed under the constraint that each gene comes from one parent or the other. and also search new possibilities of recombining non common genes to converge to an optimal solution. Although it is said that mutation increases the diversity of the population by exploring the entire solution space. For instance.

a fixed generation number is used as the termination criterion. Symbols in the cycle ofParent1 are copied into the same positions ofChild1. In this article. The residual symbols of the parent are copied into the Child1. Termination Criterion The termination criterion can be selected in different ways: reaching the predefined fitness value. The EM is shown in Figure 1(b). the same procedure is applied to generate the second child.The cycle that is defined in a manner similar to an algebraic permutation group always starts from the first position. . the two genes selected randomly along the chromosome are swapped. and these symbols are removed from the Parent2. the number of generations. By changing the roles of parents. or a non-existing difference in the fitness value of each generation. So the genes coded into phenotype are to rearrange in a way. In the EM.

Therefore. the computational effort of PSO and the GA was especially investigated for the same parameter settings. Op is the best known solution so far. In this test. ‘‘the known solution’’ is the found best solution. Also the effectiveness in terms of the solutions found by the algorithms was evaluated with respect to . is considered in the performance measurement and calculated by: Where Av is the average fitness value for the total number of runs under each problem. and relative error for the problems with synthetic data. RESULTS AND DISCUSSIONS The relative error. After the program is run 30 times. which indicates how close the solution is to the known global solution. ‘‘average length’’ is the average length of 30 running. Figure 3 shows the relative errors according to average length for the problems with synthetic data. different results may be obtained at the end of every run. In Table 1. average length.EXPERIMENTAL RESULTS Because GA and PSO are accepted as the random search algorithm. Table 1 shows the known solution. multiple independent runs having different random seeds were performed to achieve a good solution. the results are noted.

Regarding the average generation. PSO is better than the GA. it can be easily argued that the GA is more successful than the PSO. However. . As can be seen in Table 1. in general.known solutions for the test problems. PSO converges to a solution in less generation than that of the GA. the relative error. in terms of the average length. and computational time. The optimum paths found by both algorithms is close to each other in this case.

2006. M. O. M. [3] Özsaˇ glam.Proceedings of the Second International Conference on Genetic Algorithms and their Applications. Turkey. Selcuk University.. and Erenturk.. the GA has a better performance compared to PSO. Smith. 2004. . It should also be noted that today the performance improvement studies for the PSO-based approach are conducted by scientists so that a global solution or a near-global optimal solution are obtained in TSPs. C. C.CONCLUSIONS Particle Swarm Optimization is a relatively recent heuristic search method based on social behaviour of bird flocking or fish schooling in searching for food. A brief review of memetic algorithms for solving Euclidean 2D travelling salesrep problem. A discrete particle swarm optimization algorithm for travelling salesman problem. I.. of the 13th Turkish Symposium on Artificial Intelligence and Neural Networks99 –108. 1987. and Liang. D. 2008. Konya. which is sometimes referred to as an evolutionary algorithm because of similarities. X. and provide the information-sharing among their population members by moving in a search space using a combination of deterministic and probabilistic rules. Particle swarm optimization and Gas are population-based search methods. Application of particle swarm optimization algorithm to travelling salesman problem and its performance investigation. The obtained results showed that PSO has rapidly converged to a solution but could get stuck on local minimum. Computational Methods1063–1068. Y.. Y.. M. R. Wang. M. Considering the optimum path and the relative error in terms of quality of solution. A study of permutation crossover operators on the travelling salesman problem. Zhou. J. H. X. and Holland.. Wang. J. L. REFERENCES [1] Ozcan. Hillsdale.Proc. 224–230. NJ. Master thesis. [2] Shi. [4] Oliver. E. L.

and Ali. F. A. 2008. H. Cybernetics and Systems40: 1–24. Enhanced travelling salesman problem solving by genetic algorithm technique. B. M. Proceedings of World Academy of Science.[5] Al-Dulaimi. . Comparative study of some solution methods for travelling salesman problem using genetic algorithms. A. K. [6] Bhattacharyya. 2009. Engineering and Technology28: 296 –302. and Bandyopadhyay.


private static void initializeMap() { for(int i = 0. (29.10. private static final int MAX_EPOCHS = 1000. 25).20. // Number for algorithm to find. boolean done = false. //(30. } private static void PSOAlgorithm() { Particle aParticle = null. private static ArrayList<cCity> map = new ArrayList<cCity>().add(city).Random. city. (40.9. i < PARTICLE_COUNT.9.25. private static int YLocs[] = new int[] {5.63. i++) . (20.40.19. 19). (40. 25).y(YLocs[i]).25. private static final double TARGET = 86.5}. } return. 10). import java. import java. int epoch = 0.package tspusepso.9.29. while(!done) { // Two conditions can end this loop: // if the maximum number of epochs allowed has been reached. 5). i < CITY_COUNT. private static final int V_MAX = 4. 5) private static int XLocs[] = new int[] {30. i++) { cCity city = new cCity(). private static ArrayList<Particle> particles = new ArrayList<Particle>(). map.40. 9). private static final int CITY_COUNT = 8. 20). city. (9.20}. public class Main { public static class Swarm_Ex2b { private static final int PARTICLE_COUNT = 8.19.ArrayList. initialize(). (9.util. // Maximum velocity change allowed.x(XLocs[i]). // if the Target value has been found. or. if(epoch < MAX_EPOCHS){ for(int i = 0.util. (19.

if(aParticle.out.out.out.print(aParticle. } private static void initialize() { for(int i = 0. j < CITY_COUNT. i++) { Particle newParticle = new Particle().indexOf(newParticle)). j < 10. } // j getTotalDistance(i). for(int j = + ".pBest() + "\n"). j < CITY_COUNT. System.get(i). epoch++. for(int j = 0. j++) { System. getVelocity().println("epoch number: " + epoch).pBest() <= TARGET){ done = true. i < PARTICLE_COUNT. best to j++) { randomlyArrange(particles. System. } } return.out.print("Distance: " + aParticle.indexOf(newParticle)). } getTotalDistance(particles. . j).{ aParticle = particles.add(newParticle). System. } private static void randomlyArrange(final int index) { int cityA = new Random(). for(int j = 0. j++) { newParticle. int cityB = 0.print("Route: "). }else{ done = true.nextInt(CITY_COUNT). } // i return. // sort particles by their pBest scores. "). updateparticles(). } // j particles. } } // i bubbleSort().

return.velocity(V_MAX).get(PARTICLE_COUNT . the more changes it will need.get(i).nextInt(CITY_COUNT).1).get(i). i < PARTICLE_COUNT. i). i++) { // The higher the velocity score.nextBoolean()){ randomlyArrange(i).velocity(0.out. } } int temp = particles. so start from the second best. worst will be last in list.get(index). particles.get(index).data(cityB. for(int j = 0.0. } // Push it closer to it's best neighbor.floor(Math. } private static void getVelocity() { double worstResults = 0. particles. if(cityB != cityA){ done = / worstResults. } // j // Update pBest value.println("Changes for particle " + i + ": " + changes).velocity(vValue).boolean done = false.abs(particles. while(!done) { cityB = new Random().data(cityA).0). if(vValue > V_MAX){ particles.get(index). copyFromParticle(i . double vValue = 0. // after sorting. for(int i = 1. } private static void updateparticles() { // Best is at index 0.pBest(). j++){ if(new Random().velocity())). }else{ particles.get(index). i++) { vValue = (V_MAX * particles. worstResults = particles.get(i). j < temp). i < PARTICLE_COUNT. for(int i = 0. particles. } } return. .1. }else if(vValue < 0.get(i). System. int changes = (int)Math.0){ particles.

// targetB will be source's neighbor immediately succeeding targetA (circular).print("Distance: " + particles. } private static void copyFromParticle(final int source. }else{ targetB = best.get(0). int targetA = new Random(). take from beginning.pBest() + "\n").get(0). final int destination) { // push destination's data points closer to source's data points.println("Target reached. // source's city to target.getTotalDistance(i). i++) { if(best.nextInt(CITY_COUNT). j++) { System. int i = 0. } // i return. } break. int indexB = 0.print("Shortest Route: ").pBest() <= TARGET){ // Print it.get(0).out. j < == targetA){ if(i == CITY_COUNT .data(j) == targetA){ indexA = + ". for(int j = 0. } // j System. for(int j = 0.println("Target not reached"). j < CITY_COUNT.print(particles. int tempIndex = 0. System. int indexA = 0. for(. int targetB = 0. Particle best = particles.get(source). } System.get(destination).").out.out. "). }else{ System.out. j++) { if(particles. } private static void printBestSolution() { if(particles. // if end of array.1){ targetB = best. i < CITY_COUNT. return. } . } } // Move targetB next to targetA by switching + 1).out.

get(index).1). 2).data(indexB.get(secondCity).pow(Math. i++) { if(i == CITY_COUNT . thisParticle. thisParticle.pBest(thisParticle.abs(cityA. particles. for(int i = 0. particles.1){ thisParticle. } private static void getTotalDistance(final int index) { Particle thisParticle = cityA = map. a2 = Math. cCity cityB = null. i < CITY_COUNT.get(destination).get(destination). // Complete trip. double b2 = 2). double a2 = 0. }else{ thisParticle.get(firstCity). == targetB){ indexB = j. if(indexA == CITY_COUNT . final int secondCity) { cCity cityA = null. return Math. particles. } } return. return.if(particles.y()). int temp = particles. } // Switch indexB value with tempIndex value.pow(Math.pBest() + getDistance( .0).get(destination).1){ tempIndex = } } // get temp index succeeding indexA. cityB = map. temp).y() .get(destination). } private static double getDistance(final int firstCity. thisParticle = particles. } private static void bubbleSort() { .pBest(thisParticle.sqrt(a2 + b2).abs(cityA. b2 = + 1))).data(tempIndex). }else{ tempIndex = indexA + 1.pBest() + getDistance(thisParticle.x() .

private double mVelocity = 0. this. private double mpBest = 0.pBest()){ return -1. particles.1. }else if(this.pBest() < that.size().0. for(int i = 0. int listSize = particles. particles. return. } public void data(final int index. } } if(changes == 0){ done = true.pBest() > that. changes++.mpBest = 0.mData[index]. } public double pBest() .set(i.get(i). public Particle() { this.pBest()){ return 1. temp). } } return.mVelocity = 0.compareTo(particles. }else{ return 0.mData[index] = value.boolean done = false. while(!done) { int changes = 0. } private static class Particle implements Comparable<Particle> { private int mData[] = new int[CITY_COUNT].get(i). i < listSize . } public int compareTo(Particle that) { if(this.get(i + 1)) == 1){ Particle temp = particles. } } public int data(final int index) { return this.set(i + 1.get(i + 1)). particles. final int value) { this.0. i++) { if(particles.

return. public int x() { return mX.mpBest = value.mVelocity. return. } public void velocity(final double velocityScore) { this. return. } } // Particle private static class cCity { private int mX = 0. return. } public void pBest(final double value) { this. } public void x(final int xCoordinate) { mX = xCoordinate.{ return this. } public void y(final int yCoordinate) { mY = yCoordinate. } public int y() { return mY.mVelocity = velocityScore. } } // cCity public static void main(String[] args) { initializeMap(). return. PSOAlgorithm(). printBestSolution().mpBest. } public double velocity() { return this. private int mY = 0. } .

7. 1. 3. 5.62998956150375 BUILD SUCCESSFUL (total time: 2 seconds) . Distance: 86.} } RESULTS: Changes for particle 1: 2 Changes for particle 2: 2 Changes for particle 3: 3 Changes for particle 4: 3 Changes for particle 5: 3 Changes for particle 6: 3 Changes for particle 7: 4 epoch number: 1 Target reached. 2. Shortest Route: 0. 6. 4.