Algorithm-Evolve
view release on metacpan or search on metacpan
lib/Algorithm/Evolve.pm view on Meta::CPAN
=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()>
Returns a reference to an array containing all the fitnesses of the
population (in increasing order), if appropriate. The order of this array
corresponds to the order of the critters array. You might use this if you
write your own seleciton and replacement methods. Please don't modify the
array.
=item C<$p-E<gt>random_seed()>
Returns the random seed that was used for this execution.
=item C<$p-E<gt>selection( [ $new_method ] )>
=item C<$p-E<gt>replacement( [ $new_method ] )>
Fetch or change the selection/replacement method while the algorithm is
running.
=item C<$p-E<gt>parents_children_per_gen($parents, $children)>
Changes the parents_per_gen and children_per_gen attributes of the population
while the algorithm is running. Both are changed at once because the latter
must always be a multiple of the former.
=back
=head2 Co-Evolution
When there is no absolute measure of fitness for a problem, and a critter's
fitness depends on the other memebers of the population, this is called
B<co-evolution>. A good example of such a problem is rock-paper-scissors. If
we were to evolve strategies for this game, any strategy's success would be
dependent on what the rest of the population is doing.
To perform such an evolutionary algorithm, implement the C<compare> method
in your critter class and choose gladitorial selection and replacement.
Gladitorial selection/replacement chooses random pairs of critters and
C<compare>s them. If the result is not a tie, the winner receives reproduction
rights, and the loser is chosen for replacement. This happens until the
desired number of parents have been selected, or until a timeout occurs.
=head2 Adding Selection/Replacement Methods
To add your own selection and replacement methods, simply declare them in
the C<Algorithm::Evolve::selection> or C<Algorithm::Evolve::replacement>
namespaces, respectively. The first argument will be the population object,
and the second will be the number of critters to choose for
selection/replacement. You should return a list of the I<indices> you chose.
use Algorithm::Evolve;
sub Algorithm::Evolve::selection::reverse_absolute {
my ($p, $num) = @_;
## Select the indices of the $num lowest-fitness critters.
## Remember that they are sorted by increasing fitness.
return (0 .. $num-1);
}
sub Algorithm::Evolve::replacement::reverse_absolute {
my ($p, $num) = @_;
## Select indices of the $num highest-fitness critters.
return ($p->size - $num .. $p->size - 1);
}
## These are like absolute selection/replacement, but reversed, so that
## the evolutionary algorithm *minimizes* the fitness function.
my $p = Algorithm::Evolve->new(
selection => reverse_absolute,
replacement => reverse_absolute,
...
);
See the source of this module to see how the various selection/replacement
methods are implemented.
The mechanism for adding additional selection/replacement methods may change
in future versions.
=head1 SEE ALSO
L<Algorithm::Evolve::Util|Algorithm::Evolve::Util>, the F<examples/>
directory.
=head1 AUTHOR
Algorithm::Evolve is written by Mike Rosulek E<lt>mike@mikero.comE<gt>. Feel
free to contact me with comments, questions, patches, or whatever.
=head1 COPYRIGHT
Copyright (c) 2003 Mike Rosulek. All rights reserved. This module is free
software; you can redistribute it and/or modify it under the same terms as Perl
itself.
( run in 0.675 second using v1.01-cache-2.11-cpan-39bf76dae61 )