lib/Algorithm/Evolutionary/Fitness/Base.pm

filter: Statementstmt 42.4% 14 / 33 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 36.3% 4 / 11 Podpod 100% 6 / 6 totaltotal 48.0% 24 / 50 CC 1 SCAR 4.9
CC
1
Cov
42%
new
2.0
initialize
2.0
apply
2.0
CRAP 1.6
« lib/Algorithm/Evolutionary/Fitness/MMDP.pm ↑ Summary lib/Algorithm/Evolutionary/Fitness/String.pm »
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::Base - Base class for fitness functions
9  
10 =head1 SYNOPSIS
11  
12 Shouldn't be used directly, it's an abstract class whose siblings are
13 used to implement fitness functions.
14  
15 =head1 DESCRIPTION
16  
17 This module includes functionality that should be common to all fitness. Or at least it
18 would 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  
24 package Algorithm::Evolutionary::Fitness::Base;
25  
26 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 15 our $VERSION = '3.1';
29  
30  
31 =head2 new()
32  
33 Initializes common variables, like the number of evaluations. Cache is not initialized.
34  
35 =cut
36  
37 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
38 0 my $class = shift;
39 0 my $self = {};
40 0 bless $self, $class;
41 0 $self->initialize();
42 0 return $self;
43 }
44  
45 =head2 initialize()
46  
47 Called from new, initializes the evaluations counter.
48  
49 =cut
50  
51 0 ▶sub initialize {
Subroutine initialize: not called CC=1 SCAR=6.9
Pod documented
52 0 my $self = shift;
53 0 $self->{'_counter'} = 0;
54 0 $self->{'_cache'} = {}; # This is optional; should be used from derived classes
55 }
56  
57  
58 =head2 apply( $individual )
59  
60 Applies the instantiated problem to a chromosome. Actually it is a
61 wrapper around C<_apply>
62  
63 =cut
64  
65 0 ▶sub apply {
Subroutine apply: not called CC=1 SCAR=6.9
Pod documented
66 0 my $self = shift;
67 0 my $individual = shift;
68 0 $self->{'_counter'}++;
69 0 return $self->_apply( $individual );
70 }
71  
72 =head2 _apply( $individual )
73  
74 This is the one that really does the stuff. Should be overloaded by
75 derived clases
76  
77 =cut
78  
79 0 ▶sub _apply {
Subroutine _apply: not called CC=1 SCAR=6.9
80 0 croak "You should have overloaded this\n";
81 }
82  
83  
84 =head2 evaluations()
85  
86 Returns the number of evaluations made with this object. Useful for
87 collecting stats
88  
89 =cut
90  
91 0 ▶sub evaluations {
Subroutine evaluations: not called CC=1 SCAR=6.9
Pod documented
92 0 my $self = shift;
93 0 return $self->{'_counter'};
94 }
95  
96 =head2 reset_evaluations()
97  
98 Sets to 0 the number of evaluations; useful for repeated use of the fitness object
99  
100 =cut
101  
102 0 ▶sub reset_evaluations {
Subroutine reset_evaluations: not called CC=1 SCAR=6.9
Pod documented
103 0 my $self = shift;
104 0 $self->{'_counter'} = 0;
105 }
106  
107 =head2 cache()
108  
109 Returns a reference to the internal evaluations cache. Not very encapsulated, but...
110  
111 =cut
112  
113 0 ▶sub cache {
Subroutine cache: not called CC=1 SCAR=6.9
Pod documented
114 0 my $self = shift;
115 0 return $self->{'cache'};
116 }
117  
118 =head1 Known subclasses
119  
120 =over 4
121  
122 =item *
123  
124 L<Algorithm::Evolutionary::Fitness::MMDP>
125  
126 =item *
127  
128 L<Algorithm::Evolutionary::Fitness::P_Peaks>
129  
130 =item *
131  
132 L<Algorithm::Evolutionary::Fitness::wP_Peaks>
133  
134 =item *
135  
136 L<Algorithm::Evolutionary::Fitness::Knapsack>
137  
138 =item *
139  
140 L<Algorithm::Evolutionary::Fitness::ECC>
141  
142 =item *
143  
144 L<Algorithm::Evolutionary::Fitness::Royal_Road>
145  
146 =item *
147  
148 L<Algorithm::Evolutionary::Fitness::String>
149  
150 =item *
151  
152 L<Algorithm::Evolutionary::Fitness::Trap>
153  
154 =item *
155  
156 L<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 15 "What???";
« lib/Algorithm/Evolutionary/Fitness/MMDP.pm ↑ Summary lib/Algorithm/Evolutionary/Fitness/String.pm »