13.07.2015 Views

A Fast TSP Solver Using GA on JAVA - Gcd.org

A Fast TSP Solver Using GA on JAVA - Gcd.org

A Fast TSP Solver Using GA on JAVA - Gcd.org

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

A<str<strong>on</strong>g>Fast</str<strong>on</strong>g> <str<strong>on</strong>g>TSP</str<strong>on</strong>g> <str<strong>on</strong>g>Solver</str<strong>on</strong>g> <str<strong>on</strong>g>Using</str<strong>on</strong>g> <str<strong>on</strong>g>GA</str<strong>on</strong>g> <strong>on</strong> <strong>JAVA</strong>Hiroaki SENGOKUSystems Development Laboratory,Hitachi, Ltd.Kawasaki, 215 JAPANIkuo YOSHIHARAGraduate School of Engineering,Tohoku UniversitySendai, 980-77 JAPANAbstractAhybrid algorithm using <str<strong>on</strong>g>GA</str<strong>on</strong>g> and heuristics forrapid soluti<strong>on</strong> of Travelling Salespers<strong>on</strong> Problems(<str<strong>on</strong>g>TSP</str<strong>on</strong>g>) is presented. We developed a <strong>JAVA</strong> programbased <strong>on</strong> this algorithm. Visiting our web pages, every<strong>on</strong>ecan easily try our <str<strong>on</strong>g>TSP</str<strong>on</strong>g> solver. Since <strong>JAVA</strong>programs are executable <strong>on</strong> many platforms, any <strong>on</strong>ewho design a new <str<strong>on</strong>g>TSP</str<strong>on</strong>g> algorithm can compare his ownsolver and ours <strong>on</strong> the same machine. <str<strong>on</strong>g>Using</str<strong>on</strong>g> our programas the criteri<strong>on</strong>, <str<strong>on</strong>g>TSP</str<strong>on</strong>g> researchers can evaluatetheir algorithms objectively.1 Introducti<strong>on</strong>The Genetic Algorithm (<str<strong>on</strong>g>GA</str<strong>on</strong>g>)[1] is an optimizing algorithmthat is modeled after the evoluti<strong>on</strong> of <strong>org</strong>anisms.Some of the <str<strong>on</strong>g>GA</str<strong>on</strong>g>'s merits are that it can be easilydeveloped because it does not require detailed knowledgeabout the problem, it can search globally, and itcan adapt to the changing c<strong>on</strong>diti<strong>on</strong>s in the problem.Despite of these merits, <str<strong>on</strong>g>GA</str<strong>on</strong>g> is often slower thanc<strong>on</strong>venti<strong>on</strong>al methods such as heuristic searches. Thisis because <str<strong>on</strong>g>GA</str<strong>on</strong>g> does not utilize explicitly the knowledgeof how to search for the soluti<strong>on</strong>s. Therefore, hybridmethods that combine <str<strong>on</strong>g>GA</str<strong>on</strong>g> with other techniques havebeen attempted.The <str<strong>on</strong>g>TSP</str<strong>on</strong>g> solver we presented[2] is <strong>on</strong>e of the hybridmethods. It uses <str<strong>on</strong>g>GA</str<strong>on</strong>g> and the 2opt method (secti<strong>on</strong>2.1). Owing to the 2opt, it is much faster thanother <str<strong>on</strong>g>TSP</str<strong>on</strong>g> solvers based <strong>on</strong> <str<strong>on</strong>g>GA</str<strong>on</strong>g> al<strong>on</strong>e.Generally, it is dicult to compare two approximatealgorithms objectively. Some algorithms have the advantageof high speed, but have the disadvantage of alow success rate in nding the optimum soluti<strong>on</strong>.The best way to compare two methods is by runningthem both <strong>on</strong> the same machine. But there are fewsuch programs in public domain[4], and we have notfound a program that can solve the problems listed in<str<strong>on</strong>g>TSP</str<strong>on</strong>g>LIB[5].C<strong>on</strong>sequently, we wrote a <strong>JAVA</strong> 1 program based <strong>on</strong>1<strong>JAVA</strong> is a trademark of Sun Microsystems, Inc.our proposed method, and made it public <strong>on</strong> our page<strong>on</strong> the World Wide Web (WWW). <strong>JAVA</strong> programs areexecutable <strong>on</strong> many platforms, so people who have developeda <str<strong>on</strong>g>TSP</str<strong>on</strong>g> solver can compare their programs andours by running both programs <strong>on</strong> the same machineunder equal c<strong>on</strong>diti<strong>on</strong>s.We also developed a graphical user interface. Peoplecan freely situate towns by using a mouse, andsolve the problem they c<strong>on</strong>structed. It's easy and fun.It's helpful for students studying the basics of computeralgorithms.In secti<strong>on</strong> 2, we explain our method. In secti<strong>on</strong> 3,we show the WWW page in which we present our <str<strong>on</strong>g>TSP</str<strong>on</strong>g>program.2 A <str<strong>on</strong>g>TSP</str<strong>on</strong>g> solver: 2opt<str<strong>on</strong>g>GA</str<strong>on</strong>g>We present a hybrid method[2] that uses <str<strong>on</strong>g>GA</str<strong>on</strong>g> andthe 2opt method. In our <str<strong>on</strong>g>GA</str<strong>on</strong>g>, the 2opt method providesmutati<strong>on</strong>, while the crossover operator providesthe capability of jumping out from the local minima,where the soluti<strong>on</strong> often falls where <strong>on</strong>ly 2opt is used.The algorithm c<strong>on</strong>sists of the following steps.individuals ran-Initializati<strong>on</strong>: Generati<strong>on</strong> of Mdomly.Natural Selecti<strong>on</strong>: Eliminate p e % individuals. Thepopulati<strong>on</strong> decreases by M 2 p e =100.Multiplicati<strong>on</strong>: Choose M 2p e =100 pairs of individualsrandomly and produce an ospring from eachpair of individuals. The populati<strong>on</strong> reverts to theinitial populati<strong>on</strong> M.Mutati<strong>on</strong> by 2opt: Choose p i % of individuals randomlyand improve them by the 2opt method.The elite individual, or the individual that hasthe best tness value in the populati<strong>on</strong>, is alwayschosen. If the individual is already improved, d<strong>on</strong>othing, because it cannot be further improvedby 2opt.We now describe the detail of each step.


2.1 Mutati<strong>on</strong> by 2optThe 2opt method is <strong>on</strong>e of the most well-known localsearch algorithms am<strong>on</strong>g <str<strong>on</strong>g>TSP</str<strong>on</strong>g> solving algorithms.It improves the tour edge by edge and reverses the orderof the subtour. For example, imagine a tour asshown in the upper part of Fig. 1. Remove the twoedges ab and cd, and reverse the order of the subtour(from b to c), and add the two edges ac and bd. Thisgives us a tour as shown in the lower part of Fig. 1.The lower tour is shorter than the upper <strong>on</strong>e becauseab + cd > ac + bd.tour lengthcrossoverchildtourFigure 2: Pop-up from local minima.adcbg =(D; H; B; A; C; F; G; E) means that the salespers<strong>on</strong>visits towns D, H, B, A, ..., E, successively, andreturns to town D.adFigure 1: The 2opt methodWe check every pair of edges, for example, ab andcd. Ifab + cd > ac + bd holds, we improve them in thesame way as shown in Fig. 1. Actually, ifbothac > aband bd > cd hold, then it is not necessary to check theedges. Therefore we can skip the pairs whose edgesare far away from each other.We repeat the procedures described above until nofurther improvement can be made.2.2 Crossover in Multiplicati<strong>on</strong>When we apply the 2opt method to a soluti<strong>on</strong>, thesoluti<strong>on</strong> often falls into a local minimum. Then it cannotbe improved further by the 2opt method. C<strong>on</strong>sidertwo soluti<strong>on</strong>s that have fallen into dierent localminima. Potentially, each soluti<strong>on</strong> may have the bestsubtour for a dierent part of the tour. Then, we canmake a better soluti<strong>on</strong> if we combine those best subtoursappropriately. We cannot say, of course, whichof the subtours are good, but after many trials, we canexpect ospring soluti<strong>on</strong>s to be located in the valleyof the global minimum as shown in Fig. 2.We propose a new crossover operator that acquiresthe l<strong>on</strong>gest possible sequence of parents' subtours.We named it `Greedy Subtour Crossover (GSX)'. Weshowed[3] by experiments that using the GSX, the soluti<strong>on</strong>can pop up from local minima more eectivelythan by using simulated annealing (SA) methods.In the GSX, we use the path representati<strong>on</strong> fora genetic coding. For example, the chromosomecbParentsPick up alphabets from the parents alternately.D H B A C F G EB C D G H F E AH B A C D GChildFigure 3: Greedy Subtour CrossoverAdd the rest of alphabetsin the random order.F EAlgorithm: Greedy Subtour CrossoverInputs: Chromosomes g a = (a 0 ;a 1 ;:::;a n01) andg b =(b 0 ;b 1 ;:::;b n01).Outputs: The ospring chromosome g.procedure crossover(g a ,g b ) ff a truef b truechoose town t randomlychoose x, where a x = tchoose y, where b y = tg tdo fx x 0 1 (mod n),y y +1 (mod n).if f a = true then fif a x 62 g then fg a x 1 g,g else ff a false.ggif f b = true then fif b y 62 g then fg g 1 b y ,g else f


gf b false.ggg while f a = true or f b = trueif jgj < jg a j then fadd the rest of townsto g in the random ordergreturn gNote that \1" in\g a x 1 g" is the c<strong>on</strong>catenati<strong>on</strong>operator, and that sentence means to add a x beforethe chromosome g.An example is shown in Fig. 3. Suppose that chromosomesof parents are g a =(D;H; B; A; C; F; G; E)and g b =(B;C; D; G; H; F; E; A). First, choose <strong>on</strong>etown at random. In this example, town C is chosen.Then, x = 4 and y = 1 because a 4 = C and b 1 = Crespectively. Now the child g is (C).Next, pick up towns from the parents alternately.Begin with a 3 (town A) because x 4 0 1=3,andnext is b 2 (town D) because y 1 + 1 = 2. The childbecomes g =(A; C; D).In the same way, add a 2 (town B), b 3 (townG), a 1 (town H), and the child becomes g =(H; B; A; C; D; G). Now the next town is b 4 = H andtown H has already appeared in the child (rememberthe salespers<strong>on</strong> may not visit the same town twice), sowe can't add any more towns from parent g b .Therefore we add towns from parent g a . The nexttown is a 0 = D, but D is already used. Thus we can'tadd towns from parent g a , either.Then, we add the rest of the towns, i.e., E and F,to the child in the random order. Finally the child isg =(H; B; A;C; D; G; F; E).3 <strong>JAVA</strong> applet3.1 Applet <strong>on</strong> WWWWe developed a <strong>JAVA</strong> applet based <strong>on</strong> the proposedmethod, and put it <strong>on</strong> our WWW page:http://www.hitachi.co.jp/Div/sdl/e-naiyo/e-seika22/<str<strong>on</strong>g>TSP</str<strong>on</strong>g>.htmlIt is easy to use. First, using a mouse, put \towns"in the rectangular eld. Then, push the \start" butt<strong>on</strong>.The applet solves the problem you've just madeand displays the tour and its length (Fig. 4). Theparameters of the 2opt<str<strong>on</strong>g>GA</str<strong>on</strong>g> method, i.e., \Populati<strong>on</strong>"(M), \Selecti<strong>on</strong>" (p e ), \2opt" (p i ), are adjustable bylling out each eld <strong>on</strong> the applet.We announced the web page <strong>on</strong> the Net News inJapanese (fj.* newsgroups) <strong>on</strong>ly <strong>on</strong>ce in July 1996.Since then, the page has been accessed more than 200times a m<strong>on</strong>th.One pers<strong>on</strong> who sent us comments had mistakenlythought that <str<strong>on</strong>g>GA</str<strong>on</strong>g> was a tool <strong>on</strong>ly for experiments, andthat <str<strong>on</strong>g>GA</str<strong>on</strong>g> would be so slow that it could not be appliedto practical problems. But after accessing our webpages, he could understand why <str<strong>on</strong>g>GA</str<strong>on</strong>g> is usable.2.3 Natural Selecti<strong>on</strong>Eliminate R = M 2 p e =100 individuals. In the socalledsimple <str<strong>on</strong>g>GA</str<strong>on</strong>g>, the possibility of survival is proporti<strong>on</strong>alto the tness value, but we pay more attenti<strong>on</strong>to the diversity of the populati<strong>on</strong>. We eliminatesimilar individuals to maintain the diversity in orderto avoid the immature c<strong>on</strong>vergence that is <strong>on</strong>e of thewell-known problems in <str<strong>on</strong>g>GA</str<strong>on</strong>g>.First, sort the individuals in tness-value order.Compare the tness value of adjoining individuals. Ifthe dierence is less than " (a small positive real number),eliminate preceding individual while the numberof eliminated individuals is less than R. Let r be thenumber of eliminated individuals.Next, if r


Netscape is a trademark of Netscape Communicati<strong>on</strong>s Corporati<strong>on</strong>.Figure 4: applet <strong>on</strong> WWWing new problems <strong>on</strong>e after another and executing theapplet. Some of those problems are shown in Fig. 5.Some students added and removed a few towns intheir problems and watched the transformati<strong>on</strong> of thesoluti<strong>on</strong>. Some tried to alter the parameters of the2opt<str<strong>on</strong>g>GA</str<strong>on</strong>g> and observe what would happen. Throughmany trials, they got the feeling of the diculty ofthe <str<strong>on</strong>g>TSP</str<strong>on</strong>g>, and gained a c<strong>on</strong>crete understanding of theoptimizati<strong>on</strong> problems.A summary of comments from the students follows: Good Points{ It's easy and fun.{ I can see the progress of solving the problemsimultaneously.{ I had thought that web pages were <strong>on</strong>ly forreading, so I was surprised to see your pagefor solving the <str<strong>on</strong>g>TSP</str<strong>on</strong>g>.


Bad Points{ No grid. The coordinates of the mouse arenot displayed, so I cannot put towns accuratelyinto the places I want.{ I can't try pre-dened problems. I can't saveproblems. (Note: the current versi<strong>on</strong> of theapplet can read the <str<strong>on</strong>g>TSP</str<strong>on</strong>g> le that c<strong>on</strong>tainsthe coordinates of towns.){ I want to see the c<strong>on</strong>vergence graph. I wishto check not <strong>on</strong>ly the elite but also the otherindividuals in the populati<strong>on</strong>. It would bec<strong>on</strong>venient if the CPU time c<strong>on</strong>sumed tosolve the problem was displayed.We also present the following sample problems inour WWW pages:and sample problems listed in Table 2 from the URL:http://www.hitachi.co.jp/Div/sdl/e-naiyo/e-seika22/Table 1: Class les to download.File nameCities.classCity.class<str<strong>on</strong>g>GA</str<strong>on</strong>g>.classGene.classSort.classSortable.class<str<strong>on</strong>g>TSP</str<strong>on</strong>g>.classTspRoute.classSize3k1k3k3k1k1k8k4k randomly located 100 towns double circle 192 towns (\C"-type) double circle 192 towns (\O"-type)Double circle problems were used as benchmarksby Yamamura et al.[6] The minimum soluti<strong>on</strong>s of theproblems are known and they are either \C"-type or\O"-type.Table 2: Sample problems.File name Problemgr96.data Africa-Subproblem of 666-city <str<strong>on</strong>g>TSP</str<strong>on</strong>g>gr202.data Europe-Subproblem of 666-city <str<strong>on</strong>g>TSP</str<strong>on</strong>g>test100.dat Randomly located 100 townsc192-4682.dat Double circle 192 towns (\C"-type)c192-4684.dat Double circle 192 towns (\O"-type)‘C’-type‘O’-typeinner / outer = 3/5 inner / outer = 4/5Figure 6: the minimum soluti<strong>on</strong>s of the double circleproblemsAs shown in Fig. 6, the type of soluti<strong>on</strong> that willyield the minimum, depends <strong>on</strong> the number of townsand the ratio of the radii of the inner and outer circles.3.2 Stand-al<strong>on</strong>e Applicati<strong>on</strong>The applet can also be used as a stand-al<strong>on</strong>e applicati<strong>on</strong>.First, download class les listed in Table 1\Africa-Subproblem of 666-city <str<strong>on</strong>g>TSP</str<strong>on</strong>g>" and \Europe-Subproblem of 666-city <str<strong>on</strong>g>TSP</str<strong>on</strong>g>" are c<strong>on</strong>tained in<str<strong>on</strong>g>TSP</str<strong>on</strong>g>LIB[5].\<str<strong>on</strong>g>TSP</str<strong>on</strong>g>.class" c<strong>on</strong>tains the main method, so executethat le as a <strong>JAVA</strong> applicati<strong>on</strong> with arguments `lename'and `generati<strong>on</strong>'. The rst argument, `lename',is the name of the <str<strong>on</strong>g>TSP</str<strong>on</strong>g> le that c<strong>on</strong>tains thelocati<strong>on</strong> of towns of the <str<strong>on</strong>g>TSP</str<strong>on</strong>g> to be solved. The sec<strong>on</strong>darguments, `generati<strong>on</strong>', is the maximum generati<strong>on</strong>to be evolved. Opti<strong>on</strong>al ags listed in Table 3 can beused.Each line in the <str<strong>on</strong>g>TSP</str<strong>on</strong>g> le represents the coordinatesof a town and c<strong>on</strong>sists of two elds separated by acomma. The rst eld means the X coordinate of thetown and the sec<strong>on</strong>d eld means the Y coordinate.The cost between two towns is measured as the Euclideandistance between the coordinates of each town.If the <str<strong>on</strong>g>TSP</str<strong>on</strong>g> le c<strong>on</strong>tains the lineEDGE_WEIGHT_TYPE: GEOthe cost is dened by the geographical distance oftwo towns, i.e., the distance <strong>on</strong> the earth, instead of


Table 3: Opti<strong>on</strong>al ags of <str<strong>on</strong>g>TSP</str<strong>on</strong>g>.classOpti<strong>on</strong> Meaning Default-p M The populati<strong>on</strong> 100-e p e Eliminate p e % 30individuals-R p i Improve p i % individuals 20by 2opt-v Increment verbositylevelthe Euclidean distance. The X and Y coordinates describedabove mean the latitude and the l<strong>on</strong>gitude respectively.If the <str<strong>on</strong>g>TSP</str<strong>on</strong>g> le c<strong>on</strong>tains the lineMin:nwhere n is the positive real number, the programwill stop when the elite, whose tour length is n, isfound.On Windows 2 95 with the Java Development Kit(JDK)[7], for example, type the following commandin the \MS-DOS 2 Prompt" window.java <str<strong>on</strong>g>TSP</str<strong>on</strong>g> -v -p 200 gr96.data 300That command means to solve the <str<strong>on</strong>g>TSP</str<strong>on</strong>g> storedin the le `gr96.data' (Africa-Subproblem of 666-city<str<strong>on</strong>g>TSP</str<strong>on</strong>g> in <str<strong>on</strong>g>TSP</str<strong>on</strong>g>LIB[5]), where the populati<strong>on</strong> is 200, andthe maximum generati<strong>on</strong> is 300.The output from the command follows (partiallyomitted):C:\>java <str<strong>on</strong>g>TSP</str<strong>on</strong>g> -v -p 200 gr96.data 300File: gr96.dataPopulati<strong>on</strong>: 200Selecti<strong>on</strong>: 30 %2 opt: 20 %1:3[93 94 92 91 76 11111111111111111111 27 64 95]572842:253[94 93 95 64 65 1111111111111111 76 91 92]558403:119[37 34 33 32 10 1111111111111111 31 35 36]5548119:1251[49 51 52 54 50 111111111111 47 46 45]5533244:2646[93 94 92 91 76 111111111111 65 64 95]55322120:7132[41 40 39 38 78 1111111111 49 48 42]55291165:9780[49 45 46 47 53 1111111111 54 52 51]55259180:10692[52 51 49 45 46 11111111 48 50 54]55209The program outputs the elite, when a new elite is2MS-DOS and Windows are registered trademarks of MicrosoftCorporati<strong>on</strong>found, in the following format:g : i [ tour ] `where g, i, tour, and ` mean the generati<strong>on</strong>, theID of the individual, the tour (a list of towns in thevisiting order) that corresp<strong>on</strong>ds to the individual, andthe length of the tour.The last line, for example, says that a new elite isfound at 180th generati<strong>on</strong>, it is the 10692nd individual,and the length of the tour is 55209. According to<str<strong>on</strong>g>TSP</str<strong>on</strong>g>LIB, it is the minimum soluti<strong>on</strong>.4 C<strong>on</strong>clusi<strong>on</strong>We presented an algorithm for rapid soluti<strong>on</strong> of the<str<strong>on</strong>g>TSP</str<strong>on</strong>g> that combines the <str<strong>on</strong>g>GA</str<strong>on</strong>g> and the 2opt method. Wepublished a program based <strong>on</strong> the algorithm <strong>on</strong> ourweb pages. Because the program is written in <strong>JAVA</strong>language and can be executed <strong>on</strong> many platforms, any<strong>on</strong>ecan verify the eciency of our algorithm. Any<strong>on</strong>ewho designed a new algorithm can compare his own<str<strong>on</strong>g>TSP</str<strong>on</strong>g> solver with ours by running both programs <strong>on</strong> thesame machine. Our <str<strong>on</strong>g>TSP</str<strong>on</strong>g> solver is useful as a criteri<strong>on</strong>for evaluating the performance of <str<strong>on</strong>g>TSP</str<strong>on</strong>g> solvers.References[1] Holland, J.H.: Adaptati<strong>on</strong> in Natural and ArticialSystems, Univ. of Michigan Press (1975)[2] Sengoku, H., Yoshihara, I.: A<str<strong>on</strong>g>Fast</str<strong>on</strong>g> <str<strong>on</strong>g>TSP</str<strong>on</strong>g> Soluti<strong>on</strong>using Genetic Algorithm (Japanese), Informati<strong>on</strong>Processing Society of Japan 46th Nat'lC<strong>on</strong>v. (1993)[3] Sengoku, H., Yoshihara, I.: An Evaluati<strong>on</strong> of OptimizingCapability of Genetic Algorithm | <str<strong>on</strong>g>GA</str<strong>on</strong>g>vs SA | (Japanese), Informati<strong>on</strong> Processing Societyof Japan 47th Nat'l C<strong>on</strong>v. (1993)[4] Moscato, P.: <str<strong>on</strong>g>TSP</str<strong>on</strong>g>BIB, http://www.ing.unlp.edu.ar/cetad/mos/<str<strong>on</strong>g>TSP</str<strong>on</strong>g>BIB_home.html[5] Reinelt, G.: <str<strong>on</strong>g>TSP</str<strong>on</strong>g>LIB, ftp://softlib.rice.edu/pub/tsplib/tsplib.tar[6] Yamamura, M., Ono, T., Kobayashi, S.:Character-Preserving Genetic Algorithms forTraveling Salesman Problem (Japanese), Journalof Japanese Society for Articial IntelligenceVol.7, No.6 (1992)[7] Cornell, G., Horstmann, C.S.: core <strong>JAVA</strong>, Sun-Soft Press (1996)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!