1use strict; #-*-cperl-*- 2use warnings; 3 4use lib qw( ../../../../lib ); 5 6=head1 NAME 7 8Algorithm::Evolutionary::Fitness::Base - Base class for fitness functions 9 10=head1 SYNOPSIS 11 12Shouldn't be used directly, it's an abstract class whose siblings are 13used to implement fitness functions. 14 15=head1 DESCRIPTION 16 17This module includes functionality that should be common to all fitness. Or at least it 18would be nice to have it in common. It counts the number of evaluations and includes a common API for caching evaluations. 19 20=head1 METHODS 21 22=cut 23 24package Algorithm::Evolutionary::Fitness::Base; 25 26use Carp; 27 28our $VERSION = '3.1'; 29 30 31=head2 new() 32 33Initializes common variables, like the number of evaluations. Cache is not initialized. 34 35=cut 36 37sub new { 38 my $class = shift; 39 my $self = {}; 40 bless $self, $class; 41 $self->initialize(); 42 return $self; 43} 44 45=head2 initialize() 46 47Called from new, initializes the evaluations counter. 48 49=cut 50 51sub initialize { 52 my $self = shift; 53 $self->{'_counter'} = 0; 54 $self->{'_cache'} = {}; # This is optional; should be used from derived classes 55} 56 57 58=head2 apply( $individual ) 59 60Applies the instantiated problem to a chromosome. Actually it is a 61wrapper around C<_apply> 62 63=cut 64 65sub apply { 66 my $self = shift; 67 my $individual = shift; 68 $self->{'_counter'}++; 69 return $self->_apply( $individual ); 70} 71 72=head2 _apply( $individual ) 73 74This is the one that really does the stuff. Should be overloaded by 75derived clases 76 77=cut 78 79sub _apply { 80 croak "You should have overloaded this\n"; 81} 82 83 84=head2 evaluations() 85 86Returns the number of evaluations made with this object. Useful for 87collecting stats 88 89=cut 90 91sub evaluations { 92 my $self = shift; 93 return $self->{'_counter'}; 94} 95 96=head2 reset_evaluations() 97 98Sets to 0 the number of evaluations; useful for repeated use of the fitness object 99 100=cut 101 102sub reset_evaluations { 103 my $self = shift; 104 $self->{'_counter'} = 0; 105} 106 107=head2 cache() 108 109Returns a reference to the internal evaluations cache. Not very encapsulated, but... 110 111=cut 112 113sub cache { 114 my $self = shift; 115 return $self->{'cache'}; 116} 117 118=head1 Known subclasses 119 120=over 4 121 122=item * 123 124L<Algorithm::Evolutionary::Fitness::MMDP> 125 126=item * 127 128L<Algorithm::Evolutionary::Fitness::P_Peaks> 129 130=item * 131 132L<Algorithm::Evolutionary::Fitness::wP_Peaks> 133 134=item * 135 136L<Algorithm::Evolutionary::Fitness::Knapsack> 137 138=item * 139 140L<Algorithm::Evolutionary::Fitness::ECC> 141 142=item * 143 144L<Algorithm::Evolutionary::Fitness::Royal_Road> 145 146=item * 147 148L<Algorithm::Evolutionary::Fitness::String> 149 150=item * 151 152L<Algorithm::Evolutionary::Fitness::Trap> 153 154=item * 155 156L<Algorithm::Evolutionary::Fitness::Noisy> 157 158=back 159 160=head1 Copyright 161 162 This file is released under the GPL. See the LICENSE file included in this distribution, 163 or go to http://www.fsf.org/licenses/gpl.txt 164 165=cut 166 167"What???"; 168