Você está na página 1de 5

1

Solving the Travelling Sales Man Problem using a Genetic Algorithm


By Andy Thomas

Abstract
This essay discusses some issues which arise in solving the Travelling Salesman Problem (TSP) using a genetic algorithm (GA). Two approaches are described. I've personally implemented both !ailing with the !irst reali"ing my mista#es and succeeding with the second approach. A comparison o! the two approaches is intended to introduce the reader to the importance o! ta#ing the dynamics o! a given problem into account when designing a genetic algorithm to solve it. It is assumed that the reader has a basic #nowledge o! genetic algorithm concepts. The !ollowing is suggested as an e$cellent introduction and re!erence !or this sub%ect& Genetic Algorithms in Search 'ptimi"ation and (achine )earning. *avid +. Goldberg IS,- ./0.1/12343/2

Introduction
Background to Genetic Algorithms
+volution in the natural world displays a remar#able problem solving ability as demonstrated by the !act that a myriad species have evolved on +arth demonstrating a diverse range o! survival strategies. It would there!ore not be unreasonable to deduce that a problem solving strategy inspired by the mechanics o! natural selection and genetics may prove highly e!!ective in solving certain classes o! problems Genetic algorithms emulate the mechanics o! natural selection by a process o! randomi"ed data e$change. In this way they are able to solve o! range o! di!!icult problems which cannot be tac#led by other approaches. The !act that they are able to search in a randomi"ed yet directed manner allows them to reproduce some o! the innovative capabilities o! natural systems ,ecause genetic algorithms were inspired by the behavior o! natural systems the terminology used to describe them is a mi$ !rom both biological and computer !ields. A genetic algorithm manipulates strings o! in!ormation usually called chromosomes. These encode potential solutions to a given problem. 5hromosomes are evaluated and assigned a score (!itness value) in terms o! how well they solve the given problem according to criteria de!ined by the programmer. These !itness values are used as a probability o! survival during a round o! reproduction. -ew chromosomes are produced by combining two (or more) parent chromosomes. This process is designed to lead to a succession o! !itter o!!spring each encoding better solutions until an acceptably good solution is !ound.

The Travelling Salesman Problem


In this hypothetical problem a salesman must visit a number cities. The distances between the cities are #nown. 6ow does the salesman determine the order in which to visit each city %ust once in turn as to minimi"e the total distance travelled7 To gain an appreciation o! the problem consider 1. cities. The total number o! possible routes is 1. !actorial or 8 409 9... In this case it is per!ectly !easible to create a brute !orce algorithm to e$amine each route and yield the shortest one. :The number o! possible solutions to a problem is o!ten re!erred to as the problem 'search space'.; <or a less trivial case o! 1.. cities the search space rises to 1.. !actorial or around =.8e123. I! we assume that a computer can e$amine a million routes per second it will ta#e around 8e1>> years to e$amine them all. -eedless to say that to e$amine each route in turn is not !easible in any acceptable time scale. In a real/world route planning problem the determination o! the shortest possible route may not be re?uired. It may su!!ice to simply !ind an acceptably short route. A genetic algorithm is ideally suited such cases where the optimum solution is not re?uired provided a near/optimum solution can be delivered in a short time scale.

Genetic Algorithms Are Not a Panacea


Genetic algorithms cannot just be 'thrown' at a poorly understood problem in the hope that it will be solved in some magical way not at least i! you don't wish your e!!orts to be in vain. @hile GAs can o!ten be success!ully applied to analytically intractable tas#s it is usually necessary to understand some o! the dynamics o! the problem at hand in order to design the GA %udiciously. Perhaps the two most important design ?uestions o! any GA pro%ect are those o! the fitness evaluation function and reproduction scheme chosen as they must be mutually compatible. To illustrate this in the !ollowing section we consider what happens in a naive approach to solving a 1.. city Travelling Salesman Problem.

A Doomed Approach to TSP


@e need to !ind a short route between 1.. cities. The geographical location o! the cities are #nown hence we can easily calculate the distance o! a route between them using trigonometry. @e describe the route between the cities in a computer program with an array o! 9 bit integers where the array has 1.. elements. The array inde$ is our route order the contents o! each array element is a city number. 6ence i! we wrote the !ollowing pseudo code& a: ; A B2 4> 08 9 ... we would mean that we visit city 2 !irst !ollowed by 4> 08 and so on. This is going to be how we encode the 'chromosomes' or trial solutions in the GA.

Evaluation Function
@e write an evaluation !unction !or our problem which ta#es a trial solution and returns a !itness value. The shorter the route the higher the !itness value. 6owever we only want solutions which describe a route visiting all cities. @e reali"e this and write the !itness !unction to 'punish' with low !itness values those chromosomes which contain multiple visits to any city. I! we didn't do this the GA will !ind that the shortest route is the one which ta#es you nowhere (i.e. visits the same city 1.. times). In addition we punish chromosomes which e$press city numbers which don't e$ist//any allele value above == in our case.

Simple rossover !eproduction


Simple chromosome 'crossover' is chosen !or our reproduction scheme because it is the simplest. This is where two parent chromosomes are combined by choosing a random crossing site along the chromosome length. This can be best e$plained with the !ollowing 1. allele e$ample& A A 41>90=$.382 , A 30=841$2>9. where '$' mar#s the crossing site. Two o!!spring chromosomes are generated by crossing over A and , at '$' to yield& A' A 41>90=2>9. ,' A 30=841.382 <inally we generate an initial population o! 0.. random chromosomes and run the program. @e are astounded as the program produces nothing o! use.

Post"#ortem
Although the GA may yield a viable solution i.e. one in which all cities are visited %ust once no e!!ective attempt will be made to progress toward decreasingly shorter routes. The reason is ?uite simple. The crossover reproduction scheme we have used #ills the viability o! the o!!spring. 5onsider the 1. city chromosome e$amples A and ,. ,oth these are viable because city visits described in each are uni?ue. -ow loo# at the o!!spring A' and ,' and note how some cities are visited twice. The net result o! this approach is a random wal# through a massive problem space (appro$. 4.3e0>.) with little chance o! !inding many viable solutions never mind good ones.

>

A Success$ul Approach
In the previous section we identi!ied that the order in which data is encoded in the chromosome is important !or TSP and that a simple crossover reproduction mechanism is not suitable in these circumstances.

Partiall% #atched rossover


@ith this in mind we need a scheme analogous to simple crossover but one which preserves the solution viability while allowing the e$change o! ordering in!ormation. 'ne such scheme is Partially (atched 5rossover (P(C). In this scheme a crossing region is chosen by selecting two crossing sites. @e return to our initial 1. allele e$ample but now mar# two crossing sites& A A 41>$90=$.382 , A 30=$841$2>9. 5rossover is per!ormed in the centre region to yield the !ollowing transitionary o!!spring& AD A 66>$841$.362 ,D A 366$90=$2>6. The '6' positions are holes in the transitionary o!!spring le!t by the deliberate removal o! alleles which would otherwise be replicated in the crossing region. These holes are !illed by cross/re!erencing with the parent o! the alternate chromosome. This can best be e$plained with a step by step e$ample& 1. Ta#e the !irst hole in AD at inde$ . the missing allele value at this position is 4. 0. Search along , (the alternate parent o! AD) and match the !irst allele encountered with a value o! 4. This occurs in , at inde$ >. 8. <ill 6:.; in DA with the allele !ound at A:>; a value o! 0. Applying this process to the remaining holes we obtain the !ollowing P(C o!!spring& A' A 0=>841.392 ,' A 34190=2>8.

Inversion
(utation is o!ten used in GAs as a way o! adding random variation to the evolving population preventing a total loss o! diversity. In our case it's not so use!ul however as a mutation has a high probability o! resulting in a non/viable city order. @e may still wish to apply a small mutation rate provided o! course we account !or non/viable city orders in the evaluation !unction. 6owever an alternative to mutation is perhaps more appropriate. Inversion re!ers to the randomised e$change o! alleles in chromosomes. This can be made to occur with a given (usually small) probability in much the same way as mutation. As an e$ample let's consider inversion on the ,' chromosome&

2 ,' A 34190=2>8. Allele inde$ 0 is selected !or inversion. The value is simply e$changed with the allele at an additional randomly selected inde$ let's say inde$ 3 as !ollows& ,i A 34>90=218. 6ence allele positions 0 and 3 have been e$changed.

!esults
I've previously tried this approach to the Travelling Salesman Problem and ran it success!ully with city counts o! between 1.. and 1.... In the way o! illustration two outputs ta#en !rom a 1.. city problem are shown below (1... cities would be too cluttered to show). <igure 1a shows an initial randomly ordered route between 1.. randomly positioned points. <igure 1b a!ter around 1. ... generations.

Figure 1a : Initial Random Route Figure 1b : After 1 !! Generations Andy Thomas 0..1 andytEalpha/india2.com Submitted& .4F.3F0..1 Article content copyright G Andy Thomas 0..1.

Você também pode gostar