| 1 | 15 | ▶ | use strict; # -*- cperl -*- |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 15 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | 15 | ▶ | use lib qw( ../../../../lib ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Algorithm::Evolutionary::Fitness::String - Base class for string-based fitness functors | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | package My::Own::Fitness; | ||
| 13 | use base 'Algorithm::Evolutionary::Fitness::String'; #Mainly for deriving | ||
| 14 | |||
| 15 | my $evaluator = new My::Own::Fitness; | ||
| 16 | my $cache_hits = $evaluator->cached_evals(); | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | Base class for fitness functions applied to string-based chromosomes; | ||
| 21 | extracts common code, and saves time. Provides a hash called C<%cache> | ||
| 22 | to be used as a, well, cache for evaluations. Assumes it's using the | ||
| 23 | C<_str> instance variable of the chromosome | ||
| 24 | |||
| 25 | =head1 METHODS | ||
| 26 | |||
| 27 | =cut | ||
| 28 | |||
| 29 | package Algorithm::Evolutionary::Fitness::String; | ||
| 30 | |||
| 31 | 15 | our $VERSION = '3.0' ; | |
| 32 | |||
| 33 | 15 | ▶ | use Carp qw( croak ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 34 | 15 | ▶ | use base qw(Algorithm::Evolutionary::Fitness::Base); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 35 | |||
| 36 | |||
| 37 | =head2 new() | ||
| 38 | |||
| 39 | Initializes the cache | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | 1 | ▶ | sub new { |
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||
| 44 | 1 | my $class = shift; | |
| 45 | 1 | my $self = { _cache => {} }; | |
| 46 | 1 | bless $self, $class; | |
| 47 | 1 | return $self; | |
| 48 | } | ||
| 49 | |||
| 50 | =head2 _apply( $individual ) | ||
| 51 | |||
| 52 | Applies the instantiated problem to a chromosome, delegating to a specific function | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | 0 | ▶ | sub _apply { |
|
Subroutine
_apply: not called CC=1 SCAR=6.9
| |||
| 57 | 0 | my $self = shift; | |
| 58 | 0 | my $individual = shift; | |
| 59 | 0 | return $self->_really_apply( $individual->{'_str'}); | |
| 60 | } | ||
| 61 | |||
| 62 | =head2 _really_apply( $string ) | ||
| 63 | |||
| 64 | This one applies the function to the string. Should be overloaded | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | 0 | ▶ | sub _really_apply { |
|
Subroutine
_really_apply: not called CC=1 SCAR=6.9
| |||
| 69 | 0 | croak "This should be overriden"; | |
| 70 | } | ||
| 71 | |||
| 72 | =head2 cached_evals() | ||
| 73 | |||
| 74 | Returns the number of keys in the evaluation cache, which can be | ||
| 75 | compared to the total number of evaluations to find the cache hit | ||
| 76 | rate. | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | 0 | ▶ | sub cached_evals { |
|
Subroutine
cached_evals: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 81 | 0 | my $self = shift; | |
| 82 | 0 | return scalar keys %{$self->{'_cache'}}; | |
| 83 | } | ||
| 84 | |||
| 85 | =head1 Copyright | ||
| 86 | |||
| 87 | This file is released under the GPL. See the LICENSE file included in this distribution, | ||
| 88 | or go to http://www.fsf.org/licenses/gpl.txt | ||
| 89 | |||
| 90 | =cut | ||
| 91 | |||
| 92 | 15 | "Theory"; | |