Académique Documents
Professionnel Documents
Culture Documents
Introduction
The aim of this tutorial is to explain genetic algorithms sufficiently for you to be able to use them in your own projects. This is a stripped-down to-the-bare-essentials type of tutorial. I'm not going to go into a great deal of depth and I'm not going to scare those of you with math anxiety by throwing evil equations at you every few sentences. In fact, I'm not going to throw any nasty equations at you at all !ot in this particular tutorial anyway... "smile# This tutorial is designed to be read through twice... so don't worry if little of it ma$es sense the first time you study it. (A reader, Daniel, has kindly translated this tutorial into German. You can find it here.) (Another reader, David Lewin, has translated the tutorial into French. You can find it here.)
tem ered #ooter). An underground river flowed through the cave system and water continuously dri ed down through the water ta!le !ringing with it the fresh nutrients the algae thrived on so there was always lenty to eat and drink. #owever, although #ooters could feel and hear well they never had any need for eyes in the itch !lackness of the caves and as a result were totally !lind. $his never seemed to concern any of the #ooters though and they all had a whale of a time munching away and hooting in the darkness. $hen one day an earth)uake caused art of the cave system to colla se and for the first time in many millennia the #ooters felt the warmth of sunlight u on their skin and the soft s ringiness of moss !eneath their feet. A few daring #ooters tasted the moss and found that it was even !etter eating than the cave algae. *"oooooooooh+* they hooted !etween mouthfuls of moss and rom tly got go!!led u !y the marauding eagles who had flown in to see what all the commotion was a!out. For a while it looked as though the #ooters may !e hunted to e(tinction, for although they liked to eat the moss they could never tell if an eagle was flying a!ove. ,ot only that, they couldn%t even tell if they were concealed !eneath a rock or not unless it was low enough to reach for with their feelers. -very day many #ooters would stum!le out from the caves with the sweet smell of moss in their nostrils only to !e swiftly carried away and eaten !y an eagle. $heir situation seemed grim indeed. Fortunately, over the years, the o ulation of #ooters had grown to !e enormous in the safety of the caves and enough of them were surviving to mate . after all, an eagle can only eat so much. "ne day, a !rood of #ooters was !orn that shared a mutated skin cell gene. $his articular gene was res onsi!le for the develo ment of the skin cells on their foreheads. During the develo ment of the !a!y #ooters, when their skin cells grew from the mutated gene instructions they were slightly light sensitive. -ach new !a!y #ooter could sense if something was !locking the light to its forehead or not. /hen these little !a!y #ooters grew u into !igger #ooters and ventured into the light to eat the moss they could tell if something was swoo ing overhead or not. 0o these #ooters grew u to have a slightly !etter chance of survival than their totally !lind cousins. And !ecause they had a !etter chance of survival, they re roduced much more, therefore assing the new light sensitive skin cell gene to their offs ring. After a very short while the o ulation !ecame dominated !y the #ooters with this slight advantage. ,ow let%s &i a few thousand generations into the future. 1f you e(tra olate this rocess over very many years and involving lots of tiny mutations occurring in the skin cell genes it%s easy to imagine a rocess where one light sensitive cell may !ecome a clum of light sensitive cells, and then how the interior cells of the clum may mutate to harden into a tiny lens sha ed area, which would hel to gather the light and focus it into one lace. 1t%s not too difficult to envision a mutation that gives rise to two of these light gathering areas there!y !estowing !inocular vision u on the #ooters. $his would !e a huge advantage over their 2yclo sian cousins as the #ooters would now !e a!le to 'udge distances accurately and have a greater field of view.
*s you can see the processes of natural selection - survival of the fittest - and gene mutation have very powerful roles to play in the evolution of an organism. +ut how does recombination fit into the scheme of things, 'ell to show you that I need to tell about some other -ooters... At around the same time the #ooters with the light sensitive cells were frolicking around in the moss and teasing the eagles, another !rood of #ooters had !een !orn who shared a mutated gene that affected their hooter. $his mutation gave rise to a slightly !igger hooter than their cousins, and !ecause it was !igger they could now hoot over longer distances. $his turned out to !e useful in the ra idly diminishing o ulation !ecause the #ooters with the !igger hooters could call out to otential mates situated far away. ,ot only that !ut the female #ooters !egan to show a slight reference to males with larger hooters . $he u shot of this of course was that the !etter endowed #ooters stood a much !etter chance of mating than any not so well off #ooters. "ver a eriod of time, large hooters !ecame revalent in the o ulation. $hen one fine day a female #ooter with the gene for light sensitive skin cells met a male #ooter with the gene for roducing huge hooters. $hey fell in love, and shortly afterwards roduced a !rood of lovely !a!y #ooters. ,ow, !ecause the !a!ies chromosomes were a recom!ination of !oth arents chromosomes, some of the !a!ies shared !oth the s ecial genes and grew u not only to have light sensitive skin cells, !ut huge hooters too+ $hese new offs ring were e(tremely good at avoiding the eagles and re roducing so the rocess of evolution !egan to favour them and once again this new im roved ty e of #ooter !ecame dominant in the o ulation. And so on. And so on... Genetic Algorithms are a way of solving problems by mimic$ing the same processes mother nature uses. They use the same combination of selection, recombination and mutation to evolve a solution to a problem. !eat huh, Turn the page to find out exactly how it's done.
/00/0/0///0/0/00/0/00///0//0///0///////0/
(Don%t worry if non of this is making sense to you at the moment, it will all start to !ecome clear shortly. For now, 'ust rela( and go with the flow.) *t the beginning of a run of a genetic algorithm a large population of random chromosomes is created. %ach one, when decoded will represent a different solution to the problem at hand. )et's say there are , chromosomes in the initial population. Then, the following steps are repeated until a solution is found
Test each chromosome to see how good it is at solving the problem at hand and assign a fitness score accordingly. The fitness score is a measure of how good that chromosome is at solving the problem to hand.
1elect two members from the current population. The chance of being selected is proportional to the chromosomes fitness. 3oulette wheel selection is a commonly used method.
2ependent on the crossover rate crossover the bits from each chosen chromosome at a randomly chosen point.
1tep through the chosen chromosomes bits and flip dependent on the mutation rate. 3epeat step 4, 5, 6 until a new population of , members has been created.
!"""!""!!!""!""!" "!"!"""!""!""""!!
:hoose a random bit along the length, say at position <, and swap all the bits after that point so the above become.
Stage 1: Encoding
&irst we need to encode a possible solution as a string of bitsE a chromosome. 1o how do we do this, 'ell, first we need to represent all the different characters available to the solution... that is 0 through < and @, -, B and C. This will represent a gene. %ach chromosome will be made up of several genes. &our bits are required to represent the range of characters used. ". !. #. $. %. &. '. (. ). *. +. -. ,. -. """" """! ""!" ""!! "!"" "!"! "!!" "!!! !""" !""! !"!" !"!! !!"" !!"!
The above show all the different genes required to encode the problem as described. The possible genes !!!" F !!!! will remain unused and will be ignored by the algorithm if encountered. 1o now you can see that the solution mentioned above for 45, ' ?@AB6C4@/' would be represented by nine genes li$e so.
"!!" !"!" "!"! !!"" "!"" !!"! ""!" !"!" """! ' + & , % # + !
These genes are all strung together to form the chromosome. "!!"!"!""!"!!!"""!""!!"!""!"!"!""""! A .uic/ 0ord about 1ecoding +ecause the algorithm deals with random arrangements of bits it is often going to come across a string of bits li$e this. ""!"""!"!"!"!!!"!"!!"!!!""!" 2ecoded, these bits represent. ""!" ""!" !"!" !!!" !"!! "!!! ""!" # # + n-a ( #
'hich is meaningless in the context of this problem Therefore, when decoding, the algorithm will just ignore any genes which don7t conform to the expected pattern of. number -# operator -# number -# operator Eand so on. 'ith this in mind the above Gnonsense7 chromosome is read =and tested> as. # + (
/C=64-64>. This is not a problem however as we have found what we were loo$ing for... a solution. Therefore a test can be made for this occurrence and the algorithm halted accordingly.
Last 0ords
I hope this tutorial has helped you get to grips with the basics of genetic algorithms. Dlease note that I have only covered the very basics here. If you have found genetic algorithms interesting then there is much more for you to learn. There are different selection techniques to use, different crossover and mutation operators to try and more esoteric stuff li$e fitness sharing and speciation to fool around with. *ll or some of these techniques will improve the performance of your genetic algorithms considerably.
2tuff to Try
If you have succeeded in coding a genetic algorithm to solve the problem given in the tutorial, try having a go at the following more difficult problem. ;iven an area that has a number of non overlapping dis$s scattered about its surface as shown in 1creenshot /, Screenshot 1 Ise a genetic algorithm to find the dis$ of largest radius which may be placed amongst these dis$s without overlapping any of them. 1ee 1creenshot 4.
Screenshot 2 *s you may have already gathered, I've already written some code that solves this problem so if you get stuc$ you can find it here. =but you will have a go yourself first eh, J0>>. &or those of you without compilers, you can get the executable file here.