Algorithm-Evolutionary
view release on metacpan or search on metacpan
lib/Algorithm/Evolutionary/Op/Base.pm view on Meta::CPAN
use strict; #-*-cperl-*-
use warnings;
=head1 NAME
Algorithm::Evolutionary::Op::Base - Base class for Algorithm::Evolutionary operators,
=head1 SYNOPSIS
my $op = new Algorithm::Evolutionary::Op::Base; #Creates empty op, with rate
print $op->rate(); #application rate; relative number of times it must be applied
print "Yes" if $op->check( 'Algorithm::Evolutionary::Individual::Bit_Vector' ); #Prints Yes, it can be applied to Bit_Vector individual
print $op->arity(); #Prints 1, number of operands it can be applied to
=head1 DESCRIPTION
Base class for operators applied to Individuals and Populations and
all the rest. An operator is any object with the "apply" method,
which does things to individuals or populations. It is intendedly
quite general so that any genetic or population operator can fit in.
=head1 METHODS
=cut
package Algorithm::Evolutionary::Op::Base;
use lib qw( ../.. ../../.. );
use Memoize;
memoize('arity'); #To speed up this frequent computation
use B::Deparse; #For serializing code
use Algorithm::Evolutionary::Utils qw(parse_xml);
use Carp;
our ($VERSION) = ( '$Revision: 3.3 $ ' =~ / (\d+\.\d+)/ ) ;
our %parameters;
=head2 AUTOLOAD
Automatically define accesors for instance variables. You should
probably not worry about this unless you are going to subclass.
=cut
sub AUTOLOAD {
my $self = shift;
our $AUTOLOAD;
my ($method) = ($AUTOLOAD =~ /::(\w+)/);
my $instanceVar = "_".lcfirst($method);
if (defined ($self->{$instanceVar})) {
if ( @_ ) {
$self->{$instanceVar} = shift;
} else {
return $self->{$instanceVar};
}
}
}
=head2 new( [$priority] [,$options_hash] )
Takes a hash with specific parameters for each subclass, creates the
object, and leaves subclass-specific assignments to subclasses
=cut
sub new {
my $class = shift;
carp "Should be called from subclasses" if ( $class eq __PACKAGE__ );
my $rate = shift || 1;
my $hash = shift; #No carp here, some operators do not need specific stuff
my $self = { rate => $rate,
_arity => eval( "\$"."$class"."::ARITY" )}; # Create a reference
bless $self, $class; # And bless it
$self->set( $hash ) if $hash ;
return $self;
}
=head2 create( [@operator_parameters] )
Creates an operator via its default parameters. Probably obsolete
=cut
sub create {
my $class = shift;
( run in 0.483 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )