Algorithm-Evolutionary
view release on metacpan or search on metacpan
scripts/canonical-genetic-algorithm.pl view on Meta::CPAN
#!/usr/bin/perl
=head1 NAME
canonical-genetic-algorithm.pl - Canonical Genetic Algorithm on a simple fitness function
=head1 SYNOPSIS
prompt% ./canonical-genetic-algorithm.pl <bits> <block size> <population> <number of generations> <selection rate>
=head1 DESCRIPTION
A canonical GA uses mutation, crossover, binary representation, and
roulette wheel selection. Here mainly for reference, and so that
you can peruse to start your own programs.
In this case, we are optimizing the Royal Road function,
L<http://web.cecs.pdx.edu/~mm/handbook-of-ec-rr.pdf>. By default,
these values are used:
=over
=item *
I<number of bits>: 64 (this is the chromosome length)
=item *
I<size of block>: 4 (RR goes by blocks)
=item *
I<population size>: 256
=item *
I<number of generations>: 200 (could end before, if solution is
found)
=item *
I<selection rate>: 20% (will be replaced each generation); this means it's a steady state algorithm, which only changes a part of the population each generation.
=back
This program also demonstrates the use of caches in the fitness
evaluation, so be careful if you use too many bits or too many
generations, check the memory.
Output shows the number of generations, the winning chromosome, and
fitness. After finishing, it outputs time, cache ratio and some other
things.
=cut
use warnings;
use strict;
use Time::HiRes qw( gettimeofday tv_interval); #for benchmarking
use lib qw(lib ../lib);
#Importing all neded modules using the short POE-ish version
use Algorithm::Evolutionary qw( Individual::BitString Op::Creator
Op::CanonicalGA Op::Bitflip
Op::Crossover Fitness::Royal_Road);
use Algorithm::Evolutionary::Utils qw(entropy consensus);
#----------------------------------------------------------#
my $bits = shift || 64;
my $block_size = shift || 4;
my $pop_size = shift || 256; #Population size
my $numGens = shift || 200; #Max number of generations
my $selection_rate = shift || 0.2;
( run in 1.181 second using v1.01-cache-2.11-cpan-13bb782fe5a )