AI::Genetic::Individual - Base class for AI::Genetic
Individuals.
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 IndBitVector,
IndListVector, and IndRangeVector.
See "CREATING YOUR OWN INDIVIDUAL CLASS" for more
details.
The following methods are accessible publicly. They are not meant
to be over-ridden:
- $individual ->new(options)
- AI::Genetic::IndBitVector
->new(options)
- AI::Genetic::IndListVector
->new(options)
- AI::Genetic::IndRangeVector->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
newSpecific(). See below for details.
- $individual->fitness(?anon_sub?)
- This method is used to set/query the anonymous subroutine used to
calculate the individual's fitness. If an argument is given, it expects a
subroutine reference which will be set as the fitness subroutine. In
either case, it'll return the fitness sub ref.
- $individual->score()
- This method returns the fitness score of the individual. If the score has
never been calculated before, then the fitness function is executed and
the score saved. Subsequent calls to score() will return the cached
value.
- $individual->resetScore()
- This method resets the score of the individual such that a subsequent call
to score() will result in the execution of the
fitness sub.
The following methods are meant to be over-ridden by any class
that inherits from AI::Genetic::Individual:
- $individual->newRandom(options)
- This method returns an individual with random genes. It is called with the
arguments supplied to AI::Genetic::init() as
explained in
"$ga->init(initArgs)"
in AI::Genetic.
- $individual->newSpecific(options)
- This method returns an individual with the given genetic makeup. The
options depend on the type of individual:
- o bitvector
- One argument is expected which is an anonymous list of genes:
AI::Genetic::IndBitVector->new([0, 1, 1, 0, 1, 0]);
- o listvector
- Two arguments are expected. The first is an anonymous list of genes, the
second is an anonymous list of lists of possible gene values, similar to
the argument of newRandom.
AI::Genetic::IndListVector->new(
[qw/red medium fat/], # genes
[ # possible values
[qw/red blue green/],
[qw/big medium small/],
[qw/very_fat fat fit thin very_thin/],
]);
- o rangevector
- Two arguments are expected. The first is an anonymous list of genes, the
second is an anonymous list of lists of possible gene values, similar to
the argument of newRandom.
AI::Genetic::IndListVector->new(
[3, 14, 4], # genes
[ # possible values
[1, 5],
[0, 20],
[4, 9],
]);
- $individual->genes()
- In list context, returns a list of genes. In scalar context returns an
anonymous list of the genes.
Other useful non-generic methods:
- $listVectorInd->lists()
- This method returns an anonymous list of lists which describes the
possible value of each gene for the given AI::Genetic::IndListVector
individual. This is the same argument passed to
newRandom().
- $rangeVectorInd->ranges()
- This method returns an anonymous list of lists which describes the
possible range of each gene for the given AI::Genetic::IndRangeVector
individual. This is the same argument passed to
newRandom().
Creating your own individual class is easy. All you have to do is
inherit from AI::Genetic::Individual and override the
newRandom(), newSpecific, and
genes() methods to conform with the
documentation above. Specifically, the arguments to i<newRandom> and
newSpecific have to match what
AI::Genetic::init() expects as arguments. You
can also define any additional methods that you might require in your own
custom-made strategies.
Note that in order for your own individual class to be useful, you
have to define your own custom strategy that knows how to evolve such
individuals. Conceptually, this should be very simple.
Written by Ala Qumsieh aqumsieh@cpan.org.
(c) 2003,2004 Ala Qumsieh. All rights reserved. This module is
distributed under the same terms as Perl itself.
Hey! The above document had some coding errors, which are
explained below:
- Around line 248:
- =cut found outside a pod block. Skipping to next block.