AI-Genetic
view release on metacpan or search on metacpan
Genetic/Individual.pm view on Meta::CPAN
package AI::Genetic::Individual;
use strict;
use vars qw/$VERSION/;
$VERSION = 0.02;
# this package is to serve as a base package to
# all other individuals. It doesn't do anything
# interesting.
1;
sub new { # hmm .. do I need this?
my ($class, $genes) = @_;
my $self;
if (ref $class) { # clone mode
$self = bless {} => ref $class;
$self->{$_} = $class->{$_} for keys %$class;
$self->{GENES} = $genes;
$self->{CALCED} = 0;
} else { # new mode. Genome is given
goto &newSpecific;
}
return $self;
}
sub new_old { # hmm .. do I need this?
my ($class, $genes) = @_;
my $self;
if (ref $class) { # clone mode
$self = bless {} => ref $class;
$self->{$_} = $class->{$_} for keys %$class;
$self->{GENES} = $genes;
$self->{CALCED} = 0;
} else { # new mode. Just call newRandom.
goto &newRandom;
}
return $self;
}
# should create default methods.
# those are the only three needed.
sub newRandom {}
sub newSpecific {}
sub genes {}
# the following methods shouldn't be overridden.
sub fitness {
my ($self, $sub) = @_;
$self->{FITFUNC} = $sub if $sub;
return $self->{FITFUNC};
}
sub score {
my $self = shift;
return $self->{SCORE} if $self->{CALCED};
$self->{SCORE} = $self->{FITFUNC}->(scalar $self->genes);
$self->{CALCED} = 1;
return $self->{SCORE};
}
sub resetScore { $_[0]{CALCED} = 0 }
# hmmm .. how do I reset {CALCED} in case of mutation?
__END__
=head1 NAME
AI::Genetic::Individual - Base class for AI::Genetic Individuals.
=head1 SYNOPSIS
See L<AI::Genetic>.
=head1 DESCRIPTION
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 I<IndBitVector>, I<IndListVector>,
and I<IndRangeVector>.
See L</CREATING YOUR OWN INDIVIDUAL CLASS> for more details.
=head1 CLASS METHODS
The following methods are accessible publicly. They are not meant to
be over-ridden:
=over
=item I<$individual> -E<gt>B<new(options)>
=item I<AI::Genetic::IndBitVector> -E<gt>B<new(options)>
=item I<AI::Genetic::IndListVector> -E<gt>B<new(options)>
=item I<AI::Genetic::IndRangeVector>-E<gt>B<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
Genetic/Individual.pm view on Meta::CPAN
AI::Genetic::IndBitVector->new([0, 1, 1, 0, 1, 0]);
=item 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 I<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/],
]);
=item 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 I<newRandom>.
AI::Genetic::IndListVector->new(
[3, 14, 4], # genes
[ # possible values
[1, 5],
[0, 20],
[4, 9],
]);
=back
=item I<$individual>-E<gt>B<genes()>
In list context, returns a list of genes. In scalar context returns an
anonymous list of the genes.
=back
Other useful non-generic methods:
=over
=item I<$listVectorInd>-E<gt>B<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 I<newRandom()>.
=item I<$rangeVectorInd>-E<gt>B<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 I<newRandom()>.
=back
=head1 CREATING YOUR OWN INDIVIDUAL CLASS
Creating your own individual class is easy. All you have to do is inherit from
AI::Genetic::Individual and override the I<newRandom()>, I<newSpecific>, and
I<genes()> methods to conform with the documentation above. Specifically, the
arguments to i<newRandom> and I<newSpecific> have to match what I<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.
=head1 AUTHOR
Written by Ala Qumsieh I<aqumsieh@cpan.org>.
=head1 COPYRIGHTS
(c) 2003,2004 Ala Qumsieh. All rights reserved.
This module is distributed under the same terms as Perl itself.
=cut
=cut
( run in 0.566 second using v1.01-cache-2.11-cpan-39bf76dae61 )