Algorithm-Genetic-Diploid
view release on metacpan or search on metacpan
lib/Algorithm/Genetic/Diploid/Experiment.pm view on Meta::CPAN
Getter and setter for the fraction of individuals in the population that
gets to reproduce
=cut
sub reproduction_rate {
my $self = shift;
$self->{'reproduction_rate'} = shift if @_;
return $self->{'reproduction_rate'};
}
=item mutation_rate
Amount of change to apply to the weight and/or function of a gene.
=cut
sub mutation_rate {
my $self = shift;
$self->{'mutation_rate'} = shift if @_;
return $self->{'mutation_rate'};
}
=item crossover_rate
Getter and setter for the proportion of genes that crossover
=cut
sub crossover_rate {
my $self = shift;
$self->{'crossover_rate'} = shift if @_;
return $self->{'crossover_rate'};
}
=item ngens
Getter and setter for the number of generations in the experiment
=cut
sub ngens {
my $self = shift;
if ( @_ ) {
$log->info("number of generations set to: @_");
$self->{'ngens'} = shift;
}
return $self->{'ngens'};
}
=item population
Getter and setter for the L<Algorithm::Genetic::Diploid::Population> object
=cut
sub population {
my $self = shift;
if ( @_ ) {
$log->debug("assigning new population: @_");
$self->{'population'} = shift;
}
return $self->{'population'};
}
=item run
Runs the experiment!
=cut
sub run {
my $self = shift;
my $log = $self->logger;
$log->info("going to run experiment");
my @results;
for my $i ( 1 .. $self->ngens ) {
my $optimum = $self->optimum($i);
$log->info("optimum at generation $i is $optimum");
my ( $fittest, $fitness ) = $self->population->turnover($i,$self->env,$optimum);
push @results, [ $fittest, $fitness ];
}
my ( $fittest, $fitness ) = map { @{ $_ } } sort { $a->[1] <=> $b->[1] } @results;
return $fittest, $fitness;
}
=item genecount
Returns the number of distinct genes that remained after an experiment.
=cut
sub genecount {
my $self = shift;
my %genes = map { $_->id => $_ }
map { $_->genes }
map { $_->chromosomes }
map { $_->individuals } $self->population;
return values %genes;
}
=back
=cut
1;
( run in 0.432 second using v1.01-cache-2.11-cpan-0bd6704ced7 )