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 15     15   70 use strict; # -*- cperl -*-
  15         17  
  15         1137  
2 15     15   53 use warnings;
  15         19  
  15         341  
3              
4 15     15   47 use lib qw( ../../../../lib );
  15         19  
  15         63  
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 15     15   1784 use Carp qw( croak );
  15         17  
  15         638  
34 15     15   55 use base qw(Algorithm::Evolutionary::Fitness::Base);
  15         16  
  15         4791  
35              
36              
37             =head2 new()
38              
39             Initializes the cache
40              
41             =cut
42              
43             sub new {
44 1     1 1 370 my $class = shift;
45 1         3 my $self = { _cache => {} };
46 1         1 bless $self, $class;
47 1         2 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             sub _apply {
57 0     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             sub _really_apply {
69 0     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             sub cached_evals {
81 0     0 1   my $self = shift;
82 0           return scalar keys %{$self->{'_cache'}};
  0            
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             "Theory";