AI-Genetic
view release on metacpan or search on metacpan
tournamentSinglePoint => \&AI::Genetic::Defaults::tournamentSinglePoint,
tournamentTwoPoint => \&AI::Genetic::Defaults::tournamentTwoPoint,
tournamentUniform => \&AI::Genetic::Defaults::tournamentUniform,
randomSinglePoint => \&AI::Genetic::Defaults::randomSinglePoint,
randomTwoPoint => \&AI::Genetic::Defaults::randomTwoPoint,
randomUniform => \&AI::Genetic::Defaults::randomUniform,
);
# this hash maps the genome types to the
# classes they're defined in.
my %_genome2class = (
bitvector => 'AI::Genetic::IndBitVector',
rangevector => 'AI::Genetic::IndRangeVector',
listvector => 'AI::Genetic::IndListVector',
);
##################
# sub new():
# This is the constructor. It creates a new AI::Genetic
# object. Options are:
# -population: set the population size
# -crossover: set the crossover probability
# -mutation: set the mutation probability
# -fitness: set the fitness function
# -type: set the genome type. See docs.
# -terminate: set termination sub.
sub new {
my ($class, %args) = @_;
my $self = bless {
ADDSEL => {}, # user-defined selections
ADDCRS => {}, # user-defined crossovers
ADDMUT => {}, # user-defined mutations
ADDSTR => {}, # user-defined strategies
} => $class;
$self->{FITFUNC} = $args{-fitness} || sub { 1 };
$self->{CROSSRATE} = $args{-crossover} || 0.95;
$self->{MUTPROB} = $args{-mutation} || 0.05;
$self->{POPSIZE} = $args{-population} || 100;
$self->{TYPE} = $args{-type} || 'bitvector';
$self->{TERM} = $args{-terminate} || sub { 0 };
$self->{PEOPLE} = []; # list of individuals
$self->{GENERATION} = 0; # current gen.
$self->{INIT} = 0; # whether pop is initialized or not.
$self->{SORTED} = 0; # whether the population is sorted by score or not.
$self->{INDIVIDUAL} = ''; # name of individual class to use().
return $self;
my @r = @{$self->{PEOPLE}}[0 .. $N-1];
return $r[0] if $N == 1 && not wantarray;
return @r;
}
# sub init():
# This method initializes the population to completely
# random individuals. It deletes all current individuals!!!
# It also examines the type of individuals we want, and
# require()s the proper class. Throws an error if it can't.
# Must pass to it an anon list that will be passed to the
# newRandom method of the individual.
# In case of bitvector, $newArgs is length of bitvector.
# In case of rangevector, $newArgs is anon list of anon lists.
# each sub-anon list has two elements, min number and max number.
# In case of listvector, $newArgs is anon list of anon lists.
# Each sub-anon list contains possible values of gene.
=head1 NAME
AI::Genetic - A pure Perl genetic algorithm implementation.
=head1 SYNOPSIS
use AI::Genetic;
my $ga = new AI::Genetic(
-fitness => \&fitnessFunc,
-type => 'bitvector',
-population => 500,
-crossover => 0.9,
-mutation => 0.01,
-terminate => \&terminateFunc,
);
$ga->init(10);
$ga->evolve('rouletteTwoPoint', 100);
print "Best score = ", $ga->getFittest->score, ".\n";
=item B<-mutation>
This defines the mutation rate. Defaults to 0.05.
=item I<-fitness>
This defines a fitness function. It expects a reference to a subroutine.
More details are given in L</"FITNESS FUNCTION">.
=item I<-type>
This defines the type of the genome. Currently, AI::Genetic
supports only three types:
=over
=item I<bitvector>
Individuals of this type have genes that are bits. Each gene
can be in one of two possible states, on or off.
=item I<listvector>
Each gene of a listvector individual can assume one string value from
a specified list of possible string values.
=item I<rangevector>
Each gene of a rangevector individual can assume one integer value
during evolution. It expects a unique strategy name, and a subroutine
reference as arguments. The subroutine will be called with one argument:
the AI::Genetic object. It is expected to alter the population at each
generation. See L</"STRATEGIES"> for more information.
=item I<$ga>-E<gt>B<init>(I<initArgs>)
This method initializes the population with random individuals. It B<MUST>
be called before any call to I<evolve()> or I<inject()>. As a side effect,
any already existing individuals in the population are deleted. It expects
one argument, which depends on the type of individuals:
=over
=item o
For bitvectors, the argument is simply the length of the bitvector.
$ga->init(10);
this initializes a population where each individual has 10 genes.
=item I<$ga>-E<gt>B<crossProb>(?I<newProb>?)
This method is used to query and set the crossover rate.
=item I<$ga>-E<gt>B<mutProb>(?I<newProb>?)
This method is used to query and set the mutation rate.
=item I<$ga>-E<gt>B<indType>()
This method returns the type of individual: I<bitvector>, I<listvector>,
or I<rangevector>.
=item I<$ga>-E<gt>B<generation>()
This method returns the current generation.
=back
=head1 FITNESS FUNCTION
Genetic/Individual.pm view on Meta::CPAN
=head1 SYNOPSIS
See L<AI::Genetic>.
=head1 DESCRIPTION
This package implements the base class for all AI::Genetic individuals.
It provides basic methods required by AI::Genetic for correct evolution.
Furthermore, it can be very easily used as a base class for additional
types of individuals. AI::Genetic comes with three individual types that
inherit from this class. These are I<IndBitVector>, I<IndListVector>,
and I<IndRangeVector>.
See L</CREATING YOUR OWN INDIVIDUAL CLASS> for more details.
=head1 CLASS METHODS
The following methods are accessible publicly. They are not meant to
be over-ridden:
Genetic/Individual.pm view on Meta::CPAN
=item I<$individual> -E<gt>B<new(options)>
=item I<AI::Genetic::IndBitVector> -E<gt>B<new(options)>
=item I<AI::Genetic::IndListVector> -E<gt>B<new(options)>
=item I<AI::Genetic::IndRangeVector>-E<gt>B<new(options)>
This is the default constructor. It can be called as an instance method or
as a class method. In both cases it returns a new individual of
the proper type.
If called as an instance method, it expects one argument
which defines the genes of the individual. All other attributes, like
fitness function, class, etc, will be copied from the calling
instance.
If called as a class method, then it calls I<newSpecific()>. See below
for details.
=item I<$individual>-E<gt>B<fitness(?anon_sub?)>
Genetic/Individual.pm view on Meta::CPAN
=item I<$individual>-E<gt>B<newRandom(options)>
This method returns an individual with random genes. It is called with the
arguments supplied to I<AI::Genetic::init()> as explained in
L<AI::Genetic/I<$ga>-E<gt>B<init>(I<initArgs>)>.
=item I<$individual>-E<gt>B<newSpecific(options)>
This method returns an individual with the given genetic makeup. The
options depend on the type of individual:
=over
=item o bitvector
One argument is expected which is an anonymous list of genes:
AI::Genetic::IndBitVector->new([0, 1, 1, 0, 1, 0]);
=item o listvector
Genetic/OpMutation.pm view on Meta::CPAN
This package implements a few mutation mechanisms that can be used in user-defined
strategies. The methods in this class are to be called as static class methods,
rather than instance methods, which means you must call them as such:
AI::Genetic::OpCrossover::MethodName(arguments)
=head1 CLASS METHODS
There is really one kind of mutation operator implemented in this class, but it
implemented for the three default individuals types. Each gene of an individual
is looked at separately to decide whether it will be mutated or not. Mutation is
decided based upon the mutation rate (or probability). If a mutation is to happen,
then the value of the gene is switched to some other possible value.
For the case of I<bitvectors>, an ON gene switches to an OFF gene.
For the case of I<listvectors>, a gene's value is replaced by another one from
the possible list of values.
For the case of I<rangevectors>, a gene's value is replaced by another one from
NAME
AI::Genetic - A pure Perl genetic algorithm implementation.
SYNOPSIS
use AI::Genetic;
my $ga = new AI::Genetic(
-fitness => \&fitnessFunc,
-type => 'bitvector',
-population => 500,
-crossover => 0.9,
-mutation => 0.01,
-terminate => \&terminateFunc,
);
$ga->init(10);
$ga->evolve('rouletteTwoPoint', 100);
print "Best score = ", $ga->getFittest->score, ".\n";
This defines the crossover rate. Defaults to 0.95.
-mutation
This defines the mutation rate. Defaults to 0.05.
*-fitness*
This defines a fitness function. It expects a reference to a
subroutine. More details are given in the section on
"FITNESS FUNCTION".
*-type* This defines the type of the genome. Currently, AI::Genetic
supports only three types:
*bitvector*
Individuals of this type have genes that are bits. Each
gene can be in one of two possible states, on or off.
*listvector*
Each gene of a listvector individual can assume one
string value from a specified list of possible string
values.
*rangevector*
Each gene of a rangevector individual can assume one
integer value from a range of possible integer values.
during evolution. It expects a unique strategy name, and a
subroutine reference as arguments. The subroutine will be called
with one argument: the AI::Genetic object. It is expected to alter
the population at each generation. See the section on "STRATEGIES"
for more information.
*$ga*->init(*initArgs*)
This method initializes the population with random individuals. It
MUST be called before any call to *evolve()* or *inject()*. As a
side effect, any already existing individuals in the population are
deleted. It expects one argument, which depends on the type of
individuals:
o For bitvectors, the argument is simply the length of the
bitvector.
$ga->init(10);
this initializes a population where each individual has 10
genes.
*$ga*->size(?*newSize*?)
This method is used to query and set the population size.
*$ga*->crossProb(?*newProb*?)
This method is used to query and set the crossover rate.
*$ga*->mutProb(?*newProb*?)
This method is used to query and set the mutation rate.
*$ga*->indType()
This method returns the type of individual: *bitvector*,
*listvector*, or *rangevector*.
*$ga*->generation()
This method returns the current generation.
FITNESS FUNCTION
Very quickly you will realize that properly defining the fitness
function is the most important aspect of a GA. Most of the time that a
genetic algorithm takes to run is spent in running the fitness function
for each separate individual to get its fitness. AI::Genetic tries to
( run in 1.128 second using v1.01-cache-2.11-cpan-df04353d9ac )