Algorithm::Evolutionary::Op::Generation_Skeleton - Even more customizable single
generation for an evolutionary algorithm.
use Algorithm::Evolutionary qw( Individual::BitString
Op::Mutation Op::Crossover
Op::RouletteWheel
Fitness::ONEMAX Op::Generation_Skeleton
Op::Replace_Worst);
use Algorithm::Evolutionary::Utils qw(average);
my $onemax = new Algorithm::Evolutionary::Fitness::ONEMAX;
my @pop;
my $number_of_bits = 20;
my $population_size = 20;
my $replacement_rate = 0.5;
for ( 1..$population_size ) {
my $indi = new Algorithm::Evolutionary::Individual::BitString $number_of_bits ; #Creates random individual
$indi->evaluate( $onemax );
push( @pop, $indi );
}
my $m = new Algorithm::Evolutionary::Op::Mutation 0.5;
my $c = new Algorithm::Evolutionary::Op::Crossover; #Classical 2-point crossover
my $selector = new Algorithm::Evolutionary::Op::RouletteWheel $population_size; #One of the possible selectors
my $generation =
new Algorithm::Evolutionary::Op::Generation_Skeleton( $onemax, $selector, [$m, $c], $replacement_rate );
my @sortPop = sort { $b->Fitness() <=> $a->Fitness() } @pop;
my $bestIndi = $sortPop[0];
my $previous_average = average( \@sortPop );
$generation->apply( \@sortPop );
Algorithm::Evolutionary::Op::Base
Skeleton class for a general single-generation (or single step) in an
evolutionary algorithm; its instantiation requires a fitness function, a
Selector, a reference to an array of operators and a replacement operator
Creates an algorithm, with no defaults except for the default replacement
operator (defaults to Algorithm::Evolutionary::Op::ReplaceWorst)
Sets the instance variables. Takes a ref-to-hash as input. Not intended to be
used from outside the class
Applies the algorithm to the population, which should have been evaluated first;
checks that it receives a ref-to-array as input, croaks if it does not.
Returns a sorted, culled, evaluated population for next generation.
More or less in the same ballpark, alternatives to this one
- •
- Algorithm::Evolutionary::Op::GeneralGeneration
This file is released under the GPL. See the LICENSE file included in this
distribution, or go to http://www.fsf.org/licenses/gpl.txt