Algorithm-Evolutionary-Simple
view release on metacpan or search on metacpan
lib/Algorithm/Evolutionary/Simple.pm view on Meta::CPAN
my %fitness_of;
for (my $i = 0; $i < $number_of_strings; $i++) {
$population[$i] = random_chromosome( $length);
$fitness_of{$population[$i]} = max_ones( $population[$i] );
}
my @best;
my $generations=0;
do {
my @pool;
if ( $generations % 2 == 1 ) {
get_pool_roulette_wheel( \@population, \%fitness_of, $number_of_strings );
} else {
get_pool_binary_tournament( \@population, \%fitness_of, $number_of_strings );
}
my @new_pop = produce_offspring( \@pool, $number_of_strings/2 );
for my $p ( @new_pop ) {
if ( !$fitness_of{$p} ) {
$fitness_of{$p} = max_ones( $p );
}
}
@best = rnkeytop { $fitness_of{$_} } $number_of_strings/2 => @population;
@population = (@best, @new_pop);
print "Best so far $best[0] with fitness $fitness_of{$best[0]}\n";
} while ( ( $generations++ < $number_of_generations ) and ($fitness_of{$best[0]} != $length ));
=head1 DESCRIPTION
Assorted functions needed by an evolutionary algorithm, mainly for demos and simple clients.
=head1 INTERFACE
=head2 random_chromosome( $length )
Creates a binary chromosome, with uniform distribution of 0s and 1s,
and returns it as a string.
=head2 max_ones( $string )
Classical function that returns the number of ones in a binary string.
=head2 max_ones_fast( $string )
Faster implementation of max_ones.
=head2 spin($wheel, $slots )
Mainly for internal use, $wheel has the normalized probability, and
$slots the number of individuals to return.
=head2 single_generation( $population_arrayref, $fitness_of_hashref )
Applies all steps to arrive to a new generation, except
evaluation. Keeps the two best for the next generation.
=head2 get_pool_roulette_wheel( $population_arrayref, $fitness_of_hashref, $how_many_I_need )
Obtains a pool of new chromosomes using fitness_proportional selection
=head2 get_pool_binary_tournament( $population_arrayref, $fitness_of_hashref, $how_many_I_need )
Obtains a pool of new chromosomes using binary tournament, a greedier method.
=head2 produce_offspring( $population_hashref, $how_many_I_need )
Uses mutation first and then crossover to obtain a new population
=head2 mutate( $string )
Bitflips a a single point in the binary string
=head2 crossover( $one_string, $another_string )
Applies two-point crossover to both strings, returning them changed
=head1 DIAGNOSTICS
Will complain if some argument is missing.
Algorithm::Evolutionary::Simple requires no configuration files or environment variables.
=head1 DEPENDENCIES
L<Sort::Key::Top> for efficient sorting.
=head1 SEE ALSO
There are excellent evolutionary algorithm libraries out there; see
for instance L<AI::Genetic::Pro>
=head1 BUGS AND LIMITATIONS
It's intended for simplicity, not flexibility. If you want a
full-featured evolutionary algorithm library, check L<Algorithm::Evolutionary>
Please report any bugs or feature requests to
C<bug-algorithm-evolutionary-simple@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org>.
=head1 AUTHOR
JJ Merelo C<< <jj@merelo.net> >>
=head1 LICENCE AND COPYRIGHT
Copyright (c) 2011, JJ Merelo C<< <jj@merelo.net> >>. All rights reserved.
This module is free software; you can redistribute it and/or
modify it under the GPL v3 licence.
=head1 DISCLAIMER OF WARRANTY
( run in 1.354 second using v1.01-cache-2.11-cpan-ceb78f64989 )