Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
As an addition test case that I'm using, I've taken the concepts in Genetic Programming or breeding Perls, and used them to develop the following code:
#!/usr/bin/perl -w use strict; use Algorithm::Genetic; use Data::Dumper; my @genes = qw{ $x+=1; $x=$y; $y=$x; $x|=$y; $x+=$y; }; my $target = 100; my $algo = new Algorithm::Genetic( { FITNESS => \&fitness, MUTATOR => \&mutate, REAP_CRITERIA => sub { $_[ 0 ]->{ FITNESS } }, MUTATE_CRITERIA => sub { (10000-$_[ 0 ]->{ FITNESS } )**2 } } ); my @initcode; foreach ( 0..10 ) { my @bits = map { int rand @genes } ( 0..10 ); $initcode[ $_ ] = \@bits; }; $algo->init_population( @initcode ); for (1..100) { print "GENERATION $_\n"; print "-------------\n"; print join "\n", map { eval_code( get_code( @$_ ) ).' : '.get_code +( @$_ ) } reverse $algo->get_population(); print "\n"; $algo->process_generation(); print "\n"; } sub mutate { my @clone = @{ $_[0]->{ DATA } }; if ( int( rand() + 0.5 ) ) { # mutate by switching a new op in my $pos = int rand @clone; my $newop = int rand @genes; while ( $newop == $clone[ $pos ] ) { $newop = int rand @genes; } $clone[ $pos ] = $newop; } else { # mutate by adding a new op in push @clone, $genes[ int rand @genes ]; } return \@clone; } sub fitness { my $code = $_[0]->{ DATA }; # Calculate the fitness; my $string = get_code( @$code ); my $calc = eval_code( $string ); return ( $calc - $target )**2; } sub get_code { my $string = 'my $x = 1; my $y = 1; '; $string = join '', $string, map { $genes[ $_ ] } @_; return $string; } sub eval_code { return eval( $_[0] ); }

While probably not as robust as the original entry, the solutions I'm getting are converging to the target value even after 100 generations, so something is working right...

Dr. Michael K. Neylon - || "You've left the lens cap of your mind on again, Pinky" - The Brain

In reply to Re: Algorithm::Genetic by Masem
in thread Algorithm::Genetic by Masem

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [marto]: in house as in Company network, not internet connected (power stations and the corporate HQs)
    [Discipulus]: new Monsignor party! free beverages and pizza for all you monks! and a big thanks for the patience you deserved me during these years
    [marto]: congrats Discipulus
    [Discipulus]: only 4k points to sundial's level ..;=)
    [Corion]: Yay Discipulus ;)
    [Corion]: marto: Ah, so this will be training videos etc., cool!
    [choroba]: Congratulations!
    [marto]: Corion worse, some plant inspection, cameras moving thru pipes
    [marto]: Discipulus yeah, just don't adopt his tactics :P
    [marto]: the content for the most part is fairly dull, but if they go for it, they can use it for all sorts of video, they have no solution for this at the moment

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (9)
    As of 2017-07-28 09:27 GMT
    Find Nodes?
      Voting Booth?
      I came, I saw, I ...

      Results (425 votes). Check out past polls.