File Coverage

lib/Algorithm/Evolutionary/Fitness/String.pm
Criterion Covered Total %
statement 19 26 73.0
branch n/a
condition n/a
subroutine 6 9 66.6
pod 2 2 100.0
total 27 37 72.9


line stmt bran cond sub pod time code
1 1     1   6 use strict; # -*- cperl -*-
  1         2  
  1         29  
2 1     1   6 use warnings;
  1         2  
  1         34  
3              
4 1     1   6 use lib qw( ../../../../lib );
  1         2  
  1         11  
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             our $VERSION = '3.0' ;
32              
33 1     1   127 use Carp qw( croak );
  1         1  
  1         60  
34 1     1   3 use base qw(Algorithm::Evolutionary::Fitness);
  1         2  
  1         275  
35              
36             =head2 new()
37              
38             Initializes the cache
39              
40             =cut
41              
42             sub new {
43 1     1 1 413 my $class = shift;
44 1         3 my $self = { _cache => {} };
45 1         2 bless $self, $class;
46 1         3 return $self;
47             }
48              
49             =head2 _apply( $individual )
50              
51             Applies the instantiated problem to a chromosome, delegating to a specific function
52              
53             =cut
54              
55             sub _apply {
56 0     0     my $self = shift;
57 0           my $individual = shift;
58 0           return $self->_really_apply( $individual->{'_str'});
59             }
60              
61             =head2 _really_apply( $string )
62              
63             This one applies the function to the string. Should be overloaded
64              
65             =cut
66              
67             sub _really_apply {
68 0     0     croak "This should be overriden";
69             }
70              
71             =head2 cached_evals()
72              
73             Returns the number of keys in the evaluation cache, which can be
74             compared to the total number of evaluations to find the cache hit
75             rate.
76              
77             =cut
78              
79             sub cached_evals {
80 0     0 1   my $self = shift;
81 0           return scalar keys %{$self->{'_cache'}};
  0            
82             }
83              
84             =head1 Copyright
85            
86             This file is released under the GPL. See the LICENSE file included in this distribution,
87             or go to http://www.fsf.org/licenses/gpl.txt
88              
89             =cut
90              
91             "Theory";