AI-Genetic
view release on metacpan or search on metacpan
I will not go into the details of GAs here, but here are the
bare basics. Plenty of information can be found on the web.
In a GA, a population of individuals compete for survival. Each
individual is designated by a set of genes that define its
behaviour. Individuals that perform better (as defined by the
fitness function) have a higher chance of mating with other
individuals. When two individuals mate, they swap some of
their genes, resulting in an individual that has properties
from both of its "parents". Every now and then, a mutation
occurs where some gene randomly changes value, resulting in
a different individual. If all is well defined, after a few
generations, the population should converge on a "good-enough"
solution to the problem being tackled.
A GA implementation runs for a discrete number of time steps
called I<generations>. What happens during each generation can
vary greatly depending on the strategy being used (See
L</"STRATEGIES"> for more info).
Typically, a variation of the following happens at
fitness value. The higher the value, the bigger the chance
of an individual passing its genes on in future generations
through mating (crossover).
=item B<2. Crossover>
Here, individuals selected are randomly paired up for
crossover (aka I<sexual reproduction>). This is further
controlled by the crossover rate specified and may result in
a new offspring individual that contains genes common to
both parents. New individuals are injected into the current
population.
=item B<3. Mutation>
In this step, each individual is given the chance to mutate
based on the mutation probability specified. If an individual
is to mutate, each of its genes is given the chance to randomly
switch its value to some other state.
=back
Genetic/Defaults.pm view on Meta::CPAN
my ($ssub, $xsub, $msub);
{
no strict 'refs';
$ssub = \&{"AI::Genetic::OpSelection::$selOp"};
$xsub = \&{"AI::Genetic::OpCrossover::$Xop"};
$msub = \&{"AI::Genetic::OpMutation::$mutOp"};
}
for my $i (1 .. $size/2) {
my @parents = $ssub->(@$selArgs);
@parents < 2 and push @parents => $ssub->(@$selArgs);
my @cgenes = $xsub->($crossProb, map scalar $_->genes, @parents);
# check if two didn't mate.
unless (ref $cgenes[0]) {
@cgenes = map scalar $_->genes, @parents;
}
# mutate them.
$_ = $msub->(@mutArgs, $_) for @cgenes;
# push them into pop.
push @newPop => map $pop->[0]->new($_), @cgenes;
}
# assign the fitness function. This is UGLY.
Genetic/OpCrossover.pm view on Meta::CPAN
package AI::Genetic::OpCrossover;
use strict;
1;
# sub vectorSinglePoint():
# Single point crossover.
# arguments are crossover prob, two
# anon lists of genes (parents).
# If crossover occurs, returns two anon lists
# of children genes. If no crossover, returns 0.
# both parents have to be of same length.
sub vectorSinglePoint {
my ($prob, $mom, $dad) = @_;
return 0 if rand > $prob;
# get single index from 1 to $#{$dad}
my $ind = 1 + int rand $#{$dad};
my @c1 = (@$mom[0 .. $ind - 1],
@$dad[$ind .. $#{$dad}]);
my @c2 = (@$dad[0 .. $ind - 1],
@$mom[$ind .. $#{$dad}]);
return (\@c1, \@c2);
}
# sub vectorTwoPoint():
# Two point crossover.
# arguments are crossover prob, two
# anon lists of genes (parents).
# If crossover occurs, returns two anon lists
# of children genes. If no crossover, returns 0.
# both parents have to be of same length.
sub vectorTwoPoint {
my ($prob, $mom, $dad) = @_;
return 0 if rand > $prob;
# get first index from 1 to $#{$dad}-1
my $ind1 = 1 + int rand($#{$dad} - 1);
# get second index from $ind1 to $#{$dad}
Genetic/OpCrossover.pm view on Meta::CPAN
my @c2 = (@$dad[0 .. $ind1 - 1],
@$mom[$ind1 .. $ind2 - 1],
@$dad[$ind2 .. $#{$dad}]);
return (\@c1, \@c2);
}
# sub vectorUniform():
# Uniform crossover.
# arguments are crossover prob, two
# anon lists of genes (parents).
# If crossover occurs, returns two anon lists
# of children genes. If no crossover, returns 0.
# both parents have to be of same length.
sub vectorUniform {
my ($prob, $mom, $dad) = @_;
return 0 if rand > $prob;
my (@c1, @c2);
for my $i (0 .. $#{$dad}) {
if (rand > 0.5) {
push @c1 => $mom->[$i];
Genetic/OpCrossover.pm view on Meta::CPAN
AI::Genetic::OpCrossover::MethodName(arguments)
=head1 CROSSOVER OPERATORS AND THEIR METHODS
The following crossover operators are defined:
=over
=item Single Point
In single point crossover, a point is selected along the choromosomes of both parents.
The chromosomes are then split at that point, and the head of one parent chromosome is
joined with the tail of the other and vice versa, creating two child chromosomes.
The following method is defined:
=over
=item B<vectorSinglePoint>(I<Xprob, parent1, parent2>)
The first argument is the crossover rate. The second and third arguments are anonymous
lists that define the B<genes>
of the parents (not AI::Genetic::Individual objects, but the return value of the I<genes()>
method in scalar context).
If mating occurs, two anonymous lists of genes are returned corresponding to the two
new children. If no mating occurs, 0 is returned.
=back
=item Two Point
In two point crossover, two points are selected along the choromosomes of both parents.
The chromosomes are then cut at those points, and the middle parts are swapped,
creating two child chromosomes. The following method is defined:
=over
=item B<vectorTwoPoint>(I<Xprob, parent1, parent2>)
The first argument is the crossover rate. The second and third arguments are anonymous
lists that define the B<genes>
of the parents (not AI::Genetic::Individual objects, but the return value of the I<genes()>
method in scalar context).
If mating occurs, two anonymous lists of genes are returned corresponding to the two
new children. If no mating occurs, 0 is returned.
=back
=item Uniform
In uniform crossover, two child chromosomes are created by looking at each gene in both
parents, and randomly selecting which one to go with each child.
The following method is defined:
=over
=item B<vectorUniform>(I<Xprob, parent1, parent2>)
The first argument is the crossover rate. The second and third arguments are anonymous
lists that define the B<genes>
of the parents (not AI::Genetic::Individual objects, but the return value of the I<genes()>
method in scalar context).
If mating occurs, two anonymous lists of genes are returned corresponding to the two
new children. If no mating occurs, 0 is returned.
=back
=back
=head1 AUTHOR
plan on supporting v0.01.
I will not go into the details of GAs here, but here are the bare
basics. Plenty of information can be found on the web.
In a GA, a population of individuals compete for survival. Each
individual is designated by a set of genes that define its behaviour.
Individuals that perform better (as defined by the fitness function)
have a higher chance of mating with other individuals. When two
individuals mate, they swap some of their genes, resulting in an
individual that has properties from both of its "parents". Every now and
then, a mutation occurs where some gene randomly changes value,
resulting in a different individual. If all is well defined, after a few
generations, the population should converge on a "good-enough" solution
to the problem being tackled.
A GA implementation runs for a discrete number of time steps called
*generations*. What happens during each generation can vary greatly
depending on the strategy being used (See the section on "STRATEGIES"
for more info). Typically, a variation of the following happens at each
generation:
1. Selection
Here the performance of all the individuals is evaluated based on
the fitness function, and each is given a specific fitness value.
The higher the value, the bigger the chance of an individual passing
its genes on in future generations through mating (crossover).
2. Crossover
Here, individuals selected are randomly paired up for crossover (aka
*sexual reproduction*). This is further controlled by the crossover
rate specified and may result in a new offspring individual that
contains genes common to both parents. New individuals are injected
into the current population.
3. Mutation
In this step, each individual is given the chance to mutate based on
the mutation probability specified. If an individual is to mutate,
each of its genes is given the chance to randomly switch its value
to some other state.
CLASS METHODS
Here are the public methods.
( run in 0.271 second using v1.01-cache-2.11-cpan-a5abf4f5562 )