Computación evolutiva en Perl

1,916 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,916
On SlideShare
0
From Embeds
0
Number of Embeds
51
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • Computación evolutiva en Perl

    1. 1. Algoritmos evolutivos en Perl J. J. Merelo http://geneura.ugr.es/~jmerelo [email_address] GeNeura Team University of Granada Spain
    2. 3. Los pájaros <ul><li>Darwin se preguntó sobre la variación de especies de pinzón existentes en las Galápagos. </li></ul><ul><li>Lo explicó mediante dos mecanismos básicos: variaciones aleatorias y supervivencia del más adaptado. </li></ul><ul><li>Wallace también, más o menos al mismo tiempo. </li></ul>
    3. 4. Los repollos <ul><li>Mendel describió la herencia como algo atómico . </li></ul><ul><li>Von Neumann fue el primero en especular sobre el código genético. </li></ul><ul><li>Watson y Crick describieron en los años 50, la estructura y función del código ADN (ácido desoxirribonucleico) </li></ul>
    4. 5. O séase <ul><li>La evolución procede a través de cambios en el ADN, que dan lugar a diferencias somáticas. </li></ul><ul><li>Los cambios se conservan si son viables y el propietario se reproduce ( fitness > 0). </li></ul><ul><li>Cambio ❐ Adaptación ❐ Supervivencia. </li></ul><ul><li>La Naturaleza optimiza </li></ul><ul><ul><li>Localmente, espacial y temporalmente. </li></ul></ul>
    5. 6. El algoritmo <ul><li>Los algoritmos evolutivos imitan a la naturaleza trabajando sobre una población de posibles soluciones a un problema, cambiándolas aleatoriamente, combinándolas las unas con las otras, seleccionándolas de acuerdo con lo bien que resuelven el problema. </li></ul><ul><ul><li>Repítelo varias generaciones. </li></ul></ul><ul><ul><li>Yastá! </li></ul></ul>
    6. 7. Tribus genéticas
    7. 8. Glot-bot <ul><li>http://www.ocf.berkeley.edu/~jkunken/glot-bot/version2.cgi </li></ul>
    8. 9. Otros sistemas <ul><li>AI::Gene::Simple </li></ul><ul><li>AI::GP (antes myBeasties). </li></ul><ul><li>AI::Genetic </li></ul><ul><li>Diversos módulos no publicados en CPAN. </li></ul>
    9. 10. The X-Men <ul><li>Mutation is random variation of a single individual. </li></ul><ul><li>Generate a population of n individuals </li></ul><ul><ul><li>Mutate one until it improves. </li></ul></ul><ul><ul><li>Put the sonny back into the population </li></ul></ul><ul><ul><li>Go back. </li></ul></ul><ul><li>Problem to solve: find the string yetanother . </li></ul>
    10. 11. X-Men: Data Structures <ul><li>my $chromosome = { _str => '', </li></ul><ul><li>_fitness => 0 }; </li></ul><ul><li>Any data structure can be used </li></ul><ul><ul><li>Historically: bitstrings, floating point vectors, cellular automata, trees </li></ul></ul><ul><li>Fitness is usually a floating point number </li></ul><ul><ul><li>It should be comparable </li></ul></ul>
    11. 12. X-Men: Fitness Evaluation <ul><li>sub fitness ($;$) { </li></ul><ul><li>my $string = shift ; </li></ul><ul><li>my $distance = 0; </li></ul><ul><li>for ( 0..( $strLength -1)) { </li></ul><ul><li>$distance += </li></ul><ul><ul><ul><li>abs ( ord ( substr ( $string , $_ , 1)) - </li></ul></ul></ul><ul><ul><ul><ul><li>ord ( substr ( $targetString , $_ , 1))); </li></ul></ul></ul></ul><ul><li>} </li></ul><ul><li>return $distance ; </li></ul><ul><li>} </li></ul>
    12. 13. X-Men: Mutation <ul><li>my @alphabet = ('a'..'z'); </li></ul><ul><li>sub mutate { </li></ul><ul><li>my $chromosome = shift ; </li></ul><ul><li>my $mutationPoint = rand ( length ( $chromosome ->{_str})); </li></ul><ul><li>substr ( $chromosome ->{_str}, $mutationPoint , 1 ) = $alphabet [( rand ( @alphabet ))]; </li></ul><ul><li>} </li></ul><ul><li>my @thisAlphabet = @alphabet ; </li></ul><ul><li>splice ( @thisAlphabet , </li></ul><ul><li>ord ( substr ( $chromosome ->{_str}, $mutationPoint ,1))- ord ('a'),1 ); </li></ul><ul><li>substr ( $chromosome ->{_str}, $mutationPoint , 1 ) = </li></ul><ul><li>$thisAlphabet [( rand ( @thisAlphabet ))]; </li></ul>
    13. 14. X-Men: Mutation <ul><li>Example </li></ul>
    14. 15. X-Men: Mutation <ul><li>Results: </li></ul><ul><ul><li>Finds the solution... eventually (sometimes). </li></ul></ul><ul><li>We need something more: </li></ul>SEX
    15. 16. Sex: Crossover <ul><li>Crossover == recombination </li></ul><ul><ul><li>2 or more “parents” interchange material, hopefully for the best. </li></ul></ul><ul><li>Example </li></ul>
    16. 17. Sex: Crossover <ul><li>sub crossover { </li></ul><ul><li>my ( $chr1 , $chr2 ) = @_ ; </li></ul><ul><li>my $crossoverPoint = int ( rand ( length ( $chr1 ->{_str}) - 1)); </li></ul><ul><li>my $range = int ( rand ( length ( $chr1 ->{_str}) - $crossoverPoint + 1)); </li></ul><ul><li>my $str = $chr1 ->{_str}; </li></ul><ul><li>substr ( $chr1 ->{_str}, $crossoverPoint , $range , </li></ul><ul><li>substr ( $chr2 ->{_str}, $crossoverPoint , $range )); </li></ul><ul><li>substr ( $chr2 ->{_str}, $crossoverPoint , $range , </li></ul><ul><li>substr ( $str , $crossoverPoint , $range )); </li></ul><ul><li>} </li></ul><ul><li>my $range = 1 + </li></ul><ul><li>int ( rand ( length ( $chr1 ->{_str}) - $crossoverPoint )); </li></ul>
    17. 18. Sex: Crossover <ul><li>Do { </li></ul><ul><li>$clone1 = { _str => $chr1 ->{_str}, _fitness => 0 }; </li></ul><ul><li>$clone2 = { _str => $chr2 ->{_str}, _fitness => 0 }; </li></ul><ul><li>mutate( $clone1 , @alphabet ); mutate( $clone2 , @alphabet ); </li></ul><ul><li>crossover( $clone1 , $clone2 ); </li></ul><ul><li>$clone1 ->{_fitness} = fitness( $clone1 ->{_str} ); </li></ul><ul><li>$clone2 ->{_fitness} = fitness( $clone2 ->{_str} ); </li></ul><ul><li>} until ( </li></ul><ul><li>( $clone1 ->{_fitness} < $population [$#population]->{_fitness}) || </li></ul><ul><li>( $clone2 ->{_fitness} < $population [$#population]->{_fitness})); </li></ul><ul><li>if ( $clone1 ->{_fitness} > $population [$#population]->{_fitness}) { </li></ul><ul><li>$population [$#population]= $clone1 ; </li></ul><ul><li>} else { </li></ul><ul><li>$population [$#population]= $clone1 ; </li></ul><ul><li>} </li></ul>
    18. 19. Sex: Crossover
    19. 20. Fish Market <ul><li>Ydtvslqflr -> 42 </li></ul><ul><li>ygrslvihcr -> 44 </li></ul><ul><li>vfodoqaegj -> 47 </li></ul><ul><li>rjyaknrqts -> 48 </li></ul><ul><li>tgiaakugps -> 49 </li></ul><ul><li>hecdimrhbr -> 49 </li></ul><ul><li>pexesvqgac -> 52 </li></ul><ul><li>zxpiipwmhn -> 53 </li></ul><ul><li>ygrslgiqcr -> 54 </li></ul><ul><li>cglinjtihl -> 55 </li></ul><ul><li>jhkdkphbiq -> 57 </li></ul><ul><li>Ydtvslqflr -> 42 </li></ul><ul><li>ygrslvihcr -> 44 </li></ul><ul><li>vfodoqaegj -> 47 </li></ul><ul><li>rjyaknrqts -> 48 </li></ul><ul><li>tgiaakugps -> 49 </li></ul><ul><li>hecdimrhbr -> 49 </li></ul><ul><li>pexesvqgac -> 52 </li></ul><ul><li>zxpiipwmhn -> 53 </li></ul><ul><li>ygrslgiqcr -> 54 </li></ul><ul><li>Ydt aak qflr </li></ul><ul><li>tgi vsl ugps </li></ul>
    20. 21. Fish Market <ul><li>Ydtvslqflr -> 42 </li></ul><ul><li>ygrslvihcr -> 44 </li></ul><ul><li>vfodoqaegj -> 47 </li></ul><ul><li>rjyaknrqts -> 48 </li></ul><ul><li>tgiaakugps -> 49 </li></ul><ul><li>hecdimrhbr -> 49 </li></ul><ul><li>pexesvqgac -> 52 </li></ul><ul><li>zxpiipwmhn -> 53 </li></ul><ul><li>ygrslgiqcr -> 54 </li></ul><ul><li>cglinjtihl -> 55 </li></ul><ul><li>jhkdkphbiq -> 57 </li></ul><ul><li>Ydtvslqflr -> 42 </li></ul><ul><li>ygrslvihcr -> 44 </li></ul><ul><li>vfodoqaegj -> 47 </li></ul><ul><li>rjyaknrqts -> 48 </li></ul><ul><li>tgiaakugps -> 49 </li></ul><ul><li>hecdimrhbr -> 49 </li></ul><ul><li>pexesvqgac -> 52 </li></ul><ul><li>ygrslv ae cr </li></ul><ul><li>vfodoq ih gj </li></ul><ul><li>Ydt doqa flr </li></ul><ul><li>vfo vslq egj </li></ul>
    21. 22. Fish Market
    22. 23. Fish Market <ul><li>Exploration vs. Exploitation </li></ul><ul><ul><li>Exploration : creation of new possibilities: could degenerate to random search. </li></ul></ul><ul><ul><li>Exploitation : use what's already there: could degenerate to hillclimbing, inbreeding and stagnation </li></ul></ul><ul><li>Evolutionary algorithming is the art of balancing exploration and exploitation. </li></ul>
    23. 24. Fish Market <ul><li>Relax greeding sustitution strategy, and use variable rates for genetic pool/suckers' pool. </li></ul>
    24. 25. Canonical Evolutionary Algorithm <ul><li>Goldberg proposed it in his book Genetic Algorithms in Search, Optimization and Machine Learning. </li></ul><ul><ul><li>Mutation, Crossover, roulette-wheel selection. </li></ul></ul><ul><li>Example </li></ul>
    25. 26. Canonical Evolutionary Algorithm <ul><li>for ( 1.. $generations ) { </li></ul><ul><li>my @newPop, @rates ; </li></ul><ul><li>for ( @population ) { push @rates , 1/ $_ ->{_fitness}; } </li></ul><ul><li>my $popWheel =new Algorithm::Evolutionary::Wheel @rates ; </li></ul><ul><li>for ( my $i = 0; $i < $popSize /2; $i ++ ) { </li></ul><ul><li>my $chr1 = $population [ $popWheel ->spin()]; </li></ul><ul><li>my $chr2 = $population [ $popWheel ->spin()]; </li></ul><ul><li>my $clone1 = { _str => $chr1 ->{_str}, _fitness => 0 }; </li></ul><ul><li>my $clone2 = { _str => $chr2 ->{_str}, _fitness => 0 }; </li></ul><ul><li>mutate( $clone1 , @alphabet ); mutate( $clone2 , @alphabet ); </li></ul><ul><li>crossover( $clone1 , $clone2 ); </li></ul><ul><li>$clone1 ->{_fitness} = fitness( $clone1 ->{_str} ); </li></ul><ul><li>$clone2 ->{_fitness} = fitness( $clone2 ->{_str} ); </li></ul><ul><li>push @newPop , $clone1 , $clone2 ; </li></ul><ul><li>} </li></ul><ul><li>@population = sort { $a ->{_fitness} <=> $b ->{_fitness} } @newPop ; </li></ul>
    26. 27. Canonical Evolutionary Algorithm
    27. 28. Canonical Evolutionary Algorithm <ul><li>Elitism : keep the best for the next generation, deterministically. </li></ul>
    28. 29. Classical Canonical Evolutionary Algorithm <ul><li>Classical genetic algorithms used only binary strings. </li></ul><ul><li>A classical problem was the ONEMAX, c-ones or count-ones problem: find the string where all bits were set to 1. </li></ul><ul><li>sub fitness ($;$) { </li></ul><ul><li>my $indi = shift ; </li></ul><ul><li>return scalar ( grep ( $_ == 1, split (//, $indi ))); </li></ul><ul><li>} </li></ul>
    29. 30. Classical Canonical Evolutionary Algorithm <ul><li>Mutation is bitflip mutation </li></ul><ul><li>sub mutateBinary { </li></ul><ul><li>my $chromosome = shift ; </li></ul><ul><li>my $mutationPoint = rand ( length ( $chromosome ->{_str})); </li></ul><ul><li>my $bit = substr ( $chromosome ->{_str}, $mutationPoint , 1 ); </li></ul><ul><li>} </li></ul><ul><li>Finds the solution: eventually. </li></ul>
    30. 31. Doing evolutionary algorithms with Algorithm::Evolutionary <ul><li>Introduction: EA and Perl modules </li></ul><ul><li>Canonical GA with Algorithm::Evolutionary </li></ul><ul><li>Full GA with Algorithm::Evolutionary </li></ul><ul><li>Extending Algorithm::Evolutionary </li></ul>
    31. 32. Intro: EA and Perl <ul><li>Perl and evolutionary computation have met several times. </li></ul><ul><li>Mentioned for the first time in a paper in a Chemistry journal. </li></ul><ul><li>Used in GlotBot, an evolutionary algorithm that generates fake english words (available online). </li></ul><ul><li>First CPAN module: AI::Gene </li></ul><ul><ul><li>Created as part of a system for fighting spam </li></ul></ul>
    32. 33. Intro: EA and Perl: AI::Gene: mutation <ul><li>sub mutate_minor { </li></ul><ul><li>my $self = shift ; </li></ul><ul><li>my $num = + $_ [0] || 1; </li></ul><ul><li>my $rt = 0; </li></ul><ul><li>for (1.. $num ) { </li></ul><ul><li>my $glen = scalar @{ $self ->[0]}; </li></ul><ul><li>my $ pos = defined $_ [1] ? $_ [1] : int rand $glen ; </li></ul><ul><li>next if $ pos >= $glen ; # pos lies outside of gene </li></ul><ul><li>my $token = $self ->generate_token(); </li></ul><ul><li>$self ->[0][$ pos ] = $token ; </li></ul><ul><li>$rt ++; </li></ul><ul><li>} </li></ul><ul><li>return $rt ; </li></ul><ul><li>} </li></ul>
    33. 34. Intro: EA and Perl: AI::Gene: crossover <ul><li>sub crossover { </li></ul><ul><li>my ( $chr1 , $chr2 ) = @_ ; </li></ul><ul><li>my $ length = scalar( @{ $chr1 ->[0]}); </li></ul><ul><li>my $crossoverPoint = int ( rand ( $ length )); </li></ul><ul><li>my $range = int ( rand ( $ length - $crossoverPoint )); </li></ul><ul><li>my @tmpAry = @{ $chr1 ->[0]}; </li></ul><ul><li>@{ $chr1 ->[0]}[ $crossoverPoint ..( $crossoverPoint + $range )] </li></ul><ul><li>=@{ $chr2 ->[0]}[ $crossoverPoint ..( $crossoverPoint + $range )]; </li></ul><ul><li>@{ $chr2 ->[0]}[ $crossoverPoint ..( $crossoverPoint + $range )] </li></ul><ul><li>= @tmpAry [ $crossoverPoint ..( $crossoverPoint + $range )]; </li></ul><ul><li>} </li></ul>
    34. 35. Intro: EA and Perl: AI::Gene <ul><li>Problems: </li></ul><ul><ul><li>Operators are not part of the chromosomes : mutation, for instance, should be a separate class. </li></ul></ul><ul><ul><li>No provision for crossover or other n-ary operators. </li></ul></ul><ul><ul><li>No higher-level algorithms. </li></ul></ul><ul><li>Still some way to go </li></ul>
    35. 36. Intro: EA and Perl: Others <ul><li>AI::GA , Algorithm::Genetic (not in CPAN) </li></ul><ul><ul><li>Simple and straighforward implementation of a genetic algorithm </li></ul></ul><ul><li>Article by Zlatanov in DeveloperWorks: similar to glotbot. </li></ul><ul><li>MyBeasties: SourceForge project </li></ul><ul><ul><li>Complete implementation of many different kinds of genotypes and operators. </li></ul></ul><ul><ul><li>Language for describing genotypes and implementations. </li></ul></ul><ul><ul><li>Steep learning curve. </li></ul></ul><ul><ul><li>Does not support (but might) several kind of of higher-level algorithms. </li></ul></ul>
    36. 37. Algorithm::Evolutionary : Canonical GA <ul><li>Design principles: </li></ul><ul><ul><li>Complete </li></ul></ul><ul><ul><li>Extensible </li></ul></ul><ul><ul><li>Integrated with XML. </li></ul></ul><ul><li>How to obtain it: </li></ul><ul><ul><li>Download from http://opeal.sourceforge.net </li></ul></ul>
    37. 38. Algorithm::Evolutionary : Canonical GA <ul><li>use Algorithm::Evolutionary::Experiment; </li></ul><ul><li>use Algorithm::Evolutionary::Op::Easy; </li></ul><ul><li>my $fitness = sub { </li></ul><ul><li>my $indi = shift ; </li></ul><ul><li>my $total = grep ( $_ ==1, split (//, $indi ->Chrom() )); </li></ul><ul><li>return $total ; </li></ul><ul><li>}; </li></ul>
    38. 39. Algorithm::Evolutionary : Canonical GA <ul><li>my $ez =new Algorithm::Evolutionary::Op::Easy $fitness ; </li></ul><ul><li>my $popSize = 100; </li></ul><ul><li>my $indiType = 'BitString'; </li></ul><ul><li>my $indiSize = 32; </li></ul><ul><li>my $e = new Algorithm::Evolutionary::Experiment $popSize , $indiType , $indiSize , $ez ; </li></ul><ul><li>print $e ->asXML(); </li></ul>
    39. 40. Algorithm::Evolutionary : Canonical GA <ul><li>my $populationRef ; </li></ul><ul><li>do { </li></ul><ul><li>$populationRef = $e ->go(); </li></ul><ul><li>print &quot;Best so far: &quot;, $populationRef ->[0]->asString(), &quot; &quot;; </li></ul><ul><li>} until ( $populationRef ->[0]->Fitness() == $indiSize ); </li></ul><ul><li>print &quot;Final &quot;, $e ->asXML(); </li></ul>
    40. 41. Algorithm::Evolutionary : Canonical GA: XML <ul><li><ea> </li></ul><ul><li><initial> </li></ul><ul><li><op name='Easy' > </li></ul><ul><li><op name='Bitflip' rate='1' > </li></ul><ul><li><param name='howMany' value='1' /> </li></ul><ul><li></op> </li></ul><ul><li><op name='Crossover' rate='1' > </li></ul><ul><li><param name='numPoints' value='2' /> </li></ul><ul><li></op> <!-- and so on... --> </li></ul>
    41. 42. Algorithm::Evolutionary : Canonical GA: XML <ul><li>#!perl </li></ul><ul><li>use strict; </li></ul><ul><li>use warnings; </li></ul><ul><li>use Algorithm::Evolutionary::Experiment; </li></ul><ul><li>my $xmlDoc = join (&quot;&quot;,<>); </li></ul><ul><li>my $e = </li></ul><ul><li>Algorithm::Evolutionary::Experiment->fromXML( $xmlDoc ); </li></ul><ul><li>my $populationRef = $e ->go(); </li></ul><ul><li>print &quot;Final &quot;, $e ->asXML(); </li></ul>
    42. 43. Algorithm::Evolutionary : Full GA <ul><li>my $creator = </li></ul><ul><ul><li>new Algorithm::Evolutionary::Op::Creator( </li></ul></ul><ul><ul><ul><li>$popSize , 'BitString', { length => $numberOfBits }); </li></ul></ul></ul><ul><li>my $selector = </li></ul><ul><ul><li>new Algorithm::Evolutionary::Op::RouletteWheel $popSize ; </li></ul></ul><ul><li>my $mutation = new Algorithm::Evolutionary::Op::Bitflip; </li></ul><ul><li>my $crossover = new Algorithm::Evolutionary::Op::Crossover; </li></ul><ul><li>my $replacementRate = 0.4; </li></ul><ul><li>my $generation = new </li></ul><ul><ul><li>Algorithm::Evolutionary::Op::GeneralGeneration( </li></ul></ul><ul><ul><li>$fitness , $selector ,[ $mutation , $crossover ], $replacementRate ); </li></ul></ul>
    43. 44. Algorithm::Evolutionary : Full GA <ul><li>my $terminator = new Algorithm::Evolutionary::Op::DeltaTerm $numberOfBits , 0; </li></ul><ul><li>my $algorithm = new Algorithm::Evolutionary::Op::FullAlgorithm $generation , $terminator ; </li></ul><ul><li>my $experiment = new Algorithm::Evolutionary::Experiment $creator , $algorithm ; </li></ul><ul><li>$experiment ->go(); </li></ul>
    44. 45. Algorithm::Evolutionary : Full GA <ul><li>XML output can be processed using XSLT stylesheets to a web page. </li></ul><ul><li>Literate evolutionary computation could be imagined: XML interspersed with comments could be mixed, using POD2XML, description of the classes and the XML doc, to produce a document that would describe the algorithm. </li></ul>
    45. 46. Extending Algorithm::Evolutionary <ul><li>Many CPAN modules can be used to extend A::E with new operators and individuals. </li></ul><ul><li>Algorithm::Permute can be used to create a permutation operator </li></ul><ul><li>Subclass Algorithm::Evolutionary::Op::Base and override at least 2 methods: new and apply </li></ul>
    46. 47. Extending Algorithm::Evolutionary <ul><li>This new class can be used, along with String::Approx , to optimize DNA strings. </li></ul><ul><li>This is a combinatorial optimization problem which does not always good results </li></ul><ul><ul><li>Evolution proceeds by big steps, and the fitness landscape is rough. </li></ul></ul>
    47. 48. (ns)FAQ <ul><li>How many species of camels are there? </li></ul><ul><li>Wouldn't Perl make EAs slower? </li></ul><ul><ul><li>Isn't Java more efficient than Perl at regexps? </li></ul></ul><ul><ul><li>Who do you love more, your mother or your father? </li></ul></ul><ul><ul><li>So what? </li></ul></ul>
    48. 49. Apps <ul><li>Perl EA (PEArls?) apps </li></ul><ul><ul><li>Fake word evolution </li></ul></ul><ul><ul><li>Distributed EAs using SOAP::Lite </li></ul></ul><ul><ul><ul><li>Differential neural net evolution . </li></ul></ul></ul><ul><ul><li>Fantasy soccer team optimization. </li></ul></ul><ul><ul><li>Conference paper assignment </li></ul></ul><ul><ul><ul><li>Assign papers to reviewers, maximizing match of expertises. </li></ul></ul></ul><ul><ul><ul><li>Used during PPSN 2002 conference. </li></ul></ul></ul>
    49. 50. The end

    ×