AI-Genetic
view release on metacpan or search on metacpan
}
# 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.
sub init {
my ($self, $newArgs) = @_;
$self->{INIT} = 0;
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 o
For listvectors, the argument is an anonymous list of lists. The
number of sub-lists is equal to the number of genes of each individual.
Each sub-list defines the possible string values that the corresponding gene
Genetic/IndBitVector.pm view on Meta::CPAN
package AI::Genetic::IndBitVector;
use strict;
use base qw/AI::Genetic::Individual/;
1;
sub newRandom {
my ($class, $length) = @_;
my $self = bless {
GENES => [],
SCORE => 0,
FITFUNC => sub {},
CALCED => 0,
} => $class;
push @{$self->{GENES}} => rand > 0.5 ? 1 : 0
for 1 .. $length;
return $self;
}
sub newSpecific {
my ($class, $genes) = @_;
my $self = bless {
GENES => $genes,
CALCED => 0,
Genetic/OpCrossover.pm view on Meta::CPAN
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],
Genetic/OpCrossover.pm view on Meta::CPAN
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
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];
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.
o For listvectors, the argument is an anonymous list of lists. The
number of sub-lists is equal to the number of genes of each
individual. Each sub-list defines the possible string values
( run in 0.837 second using v1.01-cache-2.11-cpan-65fba6d93b7 )