AI-Genetic
view release on metacpan or search on metacpan
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
that the corresponding gene can assume.
$ga->init([
[qw/red blue green/],
[qw/big medium small/],
[qw/very_fat fat fit thin very_thin/],
]);
this initializes a population where each individual has 3 genes,
and each gene can assume one of the given values.
o For rangevectors, 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 minimum and maximum
integer values that the corresponding gene can assume.
$ga->init([
[1, 5],
[0, 20],
[4, 9],
]);
this initializes a population where each individual has 3 genes,
and each gene can assume an integer within the corresponding
range.
*$ga*->inject(*N*, ?*args*?)
This method can be used to add more individuals to the population.
New individuals can be randomly generated, or be explicitly
specified. The first argument specifies the number, *N*, of new
individuals to add. This can be followed by at most *N* arguments,
each of which is an anonymous list that specifies the genome of a
single individual to add. If the number of genomes given, *n*, is
less than *N*, then *N* - *n* random individuals are added for a
total of *N* new individuals. Random individuals are generated using
the same arguments passed to the *init()* method. For example:
$ga->inject(5,
[qw/red big thin/],
[qw/blue small fat/],
);
this adds 5 new individuals, 2 with the specified genetic coding,
and 3 randomly generated.
*$ga*->evolve(*strategy*, ?*num_generations*?)
This method causes the GA to evolve the population using the
specified strategy. A strategy name has to be specified as the first
argument. The second argument is optional and specifies the number
of generations to evolve. It defaults to 1. See the section on
"STRATEGIES" for more information on the default strategies.
Each generation consists of the following steps:
o The population is sorted according to the individuals'
fitnesses.
o The subroutine corresponding to the named strategy is called
with one argument, the AI::Genetic object. This subroutine is
expected to alter the object itself.
o If a termination subroutine is given, it is executed and the
return value is checked. Evolution terminates if this sub
returns a true value.
*$ga*->getFittest(?*N*?)
This returns the *N* fittest individuals. If not specified, *N*
defaults to 1. As a side effect, it sorts the population by fitness
score. The actual AI::Genetic::Individual objects are returned. You
can use the "genes()" and "score()" methods to get the genes and the
scores of the individuals. Please check the AI::Genetic::Individual
manpage for details.
*$ga*->sortPopulation
This method sorts the population according to fitness function. The
results are cached for speed.
*$ga*->sortIndividuals(?[*ListOfIndividuals*]?)
Given an anonymous list of individuals, this method sorts them
according to fitness, returning an anonymous list of the sorted
individuals.
*$ga*->people()
Returns an anonymous list of individuals of the current population.
IMPORTANT: the actual array reference used by the AI::Genetic object
is returned, so any changes to it will be reflected in *$ga*.
*$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
minimize this time by caching the fitness result for each individual.
But, you should spend a lot of time optimizing your fitness function to
achieve decent run times.
The fitness function should expect only one argument, an anonymous list
of genes, corresponding to the individual being analyzed. It is expected
to return a number which defines the fitness score of the said
individual. The higher the score, the more fit the individual, the more
the chance it has to be chosen for crossover.
STRATEGIES
AI::Genetic comes with 9 predefined strategies. These are:
rouletteSinglePoint
This strategy implements roulette-wheel selection and single-point
crossover.
rouletteTwoPoint
This strategy implements roulette-wheel selection and two-point
crossover.
( run in 3.106 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )