• Like
  • Save
Computación evolutiva en Perl
Upcoming SlideShare
Loading in...5
×

Computación evolutiva en Perl

  • 1,576 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,576
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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