next up previous contents
Next: Attributes & Variables Up: Universal Base Class: PerlGPObject Previous: Universal Base Class: PerlGPObject   Contents

Subsections

Methods


new()

Arguments: none
Return value: void
Defined in: PerlGPObject.pm
Mainly called by:
Usually calls:
Relevant attributes:

This is actually where the constructor for all PerlGP classes is defined, but it has been explained in more detail above.


AUTOLOAD() (get and set attributes)

Arguments: Attribute name or value
Return value: Attribute value or $self
Defined in: PerlGPObject.pm
Mainly called by:
Usually calls:
Relevant attributes: all

Thanks to the magic of Perl's AUTOLOAD mechanism, we can use all valid attribute names as methods to get and set their values. So the usage is simply:

  # getting
  my $prob = $individual->NodeMutationProb();
  my $size = $population->PopulationSize();

  # setting
  $algorithm->TournamentsSinceBest(0);
  $individual->Terminals({ NUM=>[0 .. 9], CHAR=>['a' .. 'z'] });

However, sometimes it's not convenient to use this notation, particularly during string interpolation or with +=, so you will often see direct access to the attributes in the object hash.

  $algorithm->{Tournament}++;
  print "my file stem is $self->{DBFileStem}\n";

In order for this to mechanism to work, an object needs to know which attributes it has. It looks to see if the requested method name is a key in the object's hash table. The simplest way to define these is in the %defaults hash in the top level class definition file (e.g. Algorithm.pm). You can also ``reserve'' an attribute for later use with optionalParams(), which is equivalent to initialising the attribute with an undefined value.

Because the set routine returns $self, you can chain ``set'' calls like this:

  $object->NodeMutationProb(0.01)->NodeXoverProb(0.02);

optionalParams()

Arguments: ARRAY attribute_names
Return value:
Defined in: PerlGPObject.pm
Mainly called by: _init() methods
Usually calls:
Relevant attributes:

See the discussion for AUTOLOAD. Add a call to this method in the _init() routine (after the SUPER::_init() call - see GPPopulation.pm for an example) to reserve an attribute name so that the AUTOLOAD get/set routines will recognise it.

compulsoryParams()

Arguments: ARRAY attribute_names
Return value:
Defined in: PerlGPObject.pm
Mainly called by: _init() methods
Usually calls:
Relevant attributes:

If you want to make sure that the user provides values for certain attributes in the constructor or top level _init() routine, then you can use this routine. See GeneticProgram::_init() for an example.


next up previous contents
Next: Attributes & Variables Up: Universal Base Class: PerlGPObject Previous: Universal Base Class: PerlGPObject   Contents
Bob MacCallum 2003-02-03