AI-Genetic
view release on metacpan or search on metacpan
$self->{INIT} = 0;
my $ind;
if (exists $_genome2class{$self->{TYPE}}) {
$ind = $_genome2class{$self->{TYPE}};
} else {
$ind = $self->{TYPE};
}
eval "use $ind"; # does this work if package is in same file?
if ($@) {
carp "ERROR: Init failed. Can't require '$ind': $@,";
return undef;
}
$self->{INDIVIDUAL} = $ind;
$self->{PEOPLE} = [];
$self->{SORTED} = 0;
$self->{GENERATION} = 0;
$self->{INITARGS} = $newArgs;
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
each generation:
=over 4
=item B<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).
=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
Genetic algorithms are inherently slow.
Perl can be pretty fast, but will never reach the speed of optimized
C code (at least my Perl coding will not). I wrote AI::Genetic mainly
for my own learning experience, but still tried to optimize it as
much as I can while trying to keep it as flexible as possible.
To do that, I resorted to some well-known tricks like passing a
reference of a long list instead of the list itself (for example,
when calling the fitness function, a reference of the gene list
is passed), and caching fitness scores (if you try to evaluate
the fitness of the same individual more than once, then the fitness
function will not be called, and the cached result is returned).
To help speed up your run times, you should pay special attention
to the design of your fitness function since this will be called once
for each unique individual in each generation. If you can shave off a
few clock cycles here and there, then it will be greatly magnified in
the total run time.
=head1 BUGS
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.
A NOTE ON SPEED/EFFICIENCY
Genetic algorithms are inherently slow. Perl can be pretty fast, but
will never reach the speed of optimized C code (at least my Perl coding
will not). I wrote AI::Genetic mainly for my own learning experience,
but still tried to optimize it as much as I can while trying to keep it
as flexible as possible.
To do that, I resorted to some well-known tricks like passing a
reference of a long list instead of the list itself (for example, when
calling the fitness function, a reference of the gene list is passed),
and caching fitness scores (if you try to evaluate the fitness of the
same individual more than once, then the fitness function will not be
called, and the cached result is returned).
To help speed up your run times, you should pay special attention to the
design of your fitness function since this will be called once for each
unique individual in each generation. If you can shave off a few clock
cycles here and there, then it will be greatly magnified in the total
run time.
BUGS
( run in 0.591 second using v1.01-cache-2.11-cpan-acf6aa7dc9e )