Algorithm-Evolve

 view release on metacpan or  search on metacpan

examples/breeding_perls.pl  view on Meta::CPAN

    selection       => 'roulette',
    replacement     => 'rank',
    parents_per_gen => 2,
    size            => 200,
    callback        => \&callback,
)->start;

sub callback {
    my $p = shift;

    if ($p->best_fit->fitness == $TARGET) {
        $p->suspend;
        printf "Solution found after %d generations:\n%s\n",
                    $p->generations, $p->best_fit->as_perl_code;
    }
    
    if ($p->generations == 100_000) {
        $p->suspend;
        print "Timed out after 100,000 generations.. try a smaller target\n";
    }
}

examples/string_evolver.pl  view on Meta::CPAN


use lib '../lib';
use Algorithm::Evolve;
use StringEvolver;

sub callback {
    my $pop = shift;
    
    ## Print out statistics every 50 generations
    unless ($pop->generations % 50) {
        printf "generations:%d best_fit:%d avg_fitness:%f\n",
            $pop->generations, $pop->best_fit->fitness, $pop->avg_fitness;
    }
    
    ## We can even change some things part-way through if we felt like it!

#    if ($pop->avg_fitness > 19 and $pop->replacement ne 'absolute') {
#        $pop->replacement('absolute');
#        $pop->selection('absolute');
#        $pop->parents_children_per_gen(2,2);
#    }
    

lib/Algorithm/Evolve.pm  view on Meta::CPAN

    my $p = shift;
    $p->{is_suspended} = 1;
}

sub resume {
    my $p = shift;
    $p->{is_suspended} = 0;
    $p->start;
}

sub best_fit {
    my $p = shift;
    carp "It's hard to pick the most fit when fitness is relative!"
        unless ($p->use_fitness);
    $p->critters->[-1];
}

sub avg_fitness {
    my $p = shift;
    my $sum = 0;
    $sum += $_ for @{$p->fitnesses};

lib/Algorithm/Evolve.pm  view on Meta::CPAN

iterations and return from the C<run> method.

=item C<$p-E<gt>resume()>

Start up the algorithm again after being C<suspend>'ed.

=item C<$p-E<gt>generations()>

=item C<$p-E<gt>avg_fitness()>

=item C<$p-E<gt>best_fit()>

These return basic information about the current state of the population. You 
probably will use these methods from within the callback sub. The best_fit 
method returns the most-fit critter in the population.

=item C<$p-E<gt>critters()>

Returns a reference to an array containing all the critters in the population, 
sorted by increasing fitness. You can use this to iterate over the entire
population, but please don't modify the array.

=item C<$p-E<gt>fitnesses()>



( run in 1.150 second using v1.01-cache-2.11-cpan-4e96b696675 )