lib/Algorithm/Evolutionary/Individual/Base.pm

filter: Statementstmt 65.7% 46 / 70 Branchbran 44.4% 8 / 18 Conditioncond 12.5% 1 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 63.1% 12 / 19 Podpod 100% 12 / 12 totaltotal 60.3% 79 / 131 CC 15 SCAR 16.6
CC
15
Cov
57%
evaluate
20.0
create
6.0
as_string_with_fitness
6.0
CRAP 5.3
« blib/lib/Algorithm/Evolutionary/Op/ChangeLengthMutation.pm ↑ Summary blib/lib/Algorithm/Evolutionary/Fitness/Any.pm »
1 36 ▶use strict; #-*-cperl,hi-lock,auto-fill-*-
Subroutine BEGIN: called CC=1 SCAR=0.0
2 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 36 ▶use lib qw( ../../../../lib );
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Algorithm::Evolutionary::Individual::Base - Base class for chromosomes that knows how to build them, and has some helper methods.
9
10 =head1 SYNOPSIS
11  
12 use Algorithm::Evolutionary::Individual::Base;
13  
14 my $indi = Algorithm::Evolutionary::Individual::Base->fromParam( $param_hashref ); #From parametric description
15  
16 $binIndi2->Fitness( 3.5 ); #Sets or gets fitness
17 print $binIndi2->Fitness();
18  
19 my $emptyIndi = new Algorithm::Evolutionary::Individual::Base;
20  
21 =head1 DESCRIPTION
22  
23 Base class for individuals, that is, "chromosomes" in evolutionary
24 computation algorithms. However, chromosomes needn't be bitstrings, so
25 the name is a bit misleading. This is, however, an "empty" base class,
26 that acts as a boilerplate for deriving others.
27  
28 =cut
29  
30 package Algorithm::Evolutionary::Individual::Base;
31  
32 36 ▶use YAML qw(Dump Load LoadFile);
Subroutine BEGIN: called CC=1 SCAR=0.0
33 36 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 36 our $VERSION = '3.3';
36  
37 36 ▶use constant MY_OPERATORS => qw(None);
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 =head1 METHODS
40  
41  
42 =head2 AUTOLOAD
43  
44 Creates methods for instance variables automatically
45  
46 =cut
47  
48 20 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=15.0
49 20 my $self = shift;
50 20 my $attr = our $AUTOLOAD;
51 20 $attr =~ s/.*:://;
52 20 ▶ return unless $attr =~ /[^A-Z]/; # skip DESTROY and all-cap methods
Branch: 1 branch
BranchTrueFalseTotal
unless $attr =~ /[^A-Z]/ 20 0 20
53 0 my $instance_variable = "_$attr";
54 0 ▶ $self->{$instance_variable} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
55 0 return $self->{$instance_variable};
56 }
57  
58 =head2 new( $options )
59  
60 Creates a new Base individual of the required class, with a fitness, and sets fitnes to undef.
61 Takes as params a hash to the options of the individual, that will be passed
62 on to the object of the class when it iss initialized.
63  
64 =cut
65  
66 10 ▶sub new {
Subroutine new: called CC=5 SCAR=17.0
Pod documented
67 10 my $class = shift;
68 10 ▶ if ( $class !~ /Algorithm::Evolutionary/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class =~ /Algorithm::Evolutionary/) 0 10 10
69 0 $class = "Algorithm::Evolutionary::Individual::$class";
70 }
71 10 my $options = shift;
72 10 my $self = { _fitness => undef }; # Avoid error
73 10 bless $self, $class; # And bless it
74  
75 #If the class is not loaded, we load it.
76 10 ▶ if ( !$INC{"$class\.pm"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($INC{"$class.pm"}) 10 0 10
77 10 ▶ eval "require $class" || croak "Can't find $class Module";
Branch: 1 branch
BranchTrueFalseTotal
eval "require $class" or croak("Can't find $class Module") 9 1 10
78 }
79 9 ▶ if ( $options ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options) 0 9 9
80 0 $self->set( $options );
81 }
82  
83 9 return $self;
84 }
85  
86 =head2 create( $ref_to_hash )
87  
88 Creates a new individual, but uses a different interface: takes a
89 ref-to-hash, with named parameters, which gives it a common interface
90 to all the hierarchy. The main difference with respect to new is that
91 after creation, it is initialized with random values.
92  
93 =cut
94  
95 0 ▶sub create {
Subroutine create: not called CC=2 SCAR=17.9
Pod documented
96 0 my $class = shift;
97 0 ▶ my $ref = shift || croak "Can't find the parameters hash";
Condition0%0/2
shift() || croak(q[Can't find the parameters hash])
l!l
00
MC/DC0%0/1
shift() || croak(q[Can't find the parameters hash])
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
98 0 my $self = Algorithm::Evolutionary::Individual::Base::new( $class, $ref );
99 0 $self->randomize();
100 0 return $self;
101 }
102  
103 =head2 set( $ref_to_hash )
104  
105 Sets values of an individual; takes a hash as input. Keys are prepended an
106 underscore and turn into instance variables
107  
108 =cut
109  
110 1 ▶sub set {
Subroutine set: called CC=3 SCAR=11.1
Pod documented
111 1 my $self = shift;
112 1 ▶ my $hash = shift || croak "No params here";
Condition50%1/2
shift() || croak('No params here')
l!l
10
MC/DC0%0/1
shift() || croak('No params here')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
113 1 for ( keys %{$hash} ) {
114 2 $self->{"_$_"} = $hash->{$_};
115 }
116 }
117  
118 =head2 as_yaml()
119  
120 Prints it as YAML.
121  
122 =cut
123  
124 8 ▶sub as_yaml {
Subroutine as_yaml: called CC=1 SCAR=0.0
Pod documented
125 8 my $self = shift;
126 8 return Dump($self);
127 }
128  
129 =head2 as_string()
130  
131 Prints it as a string in the most meaningful representation possible
132  
133 =cut
134  
135 0 ▶sub as_string {
Subroutine as_string: not called CC=1 SCAR=6.9
Pod documented
136 0 croak "This function is not defined at this level, you should override it in a subclass\n";
137 }
138  
139 =head2 as_string_with_fitness( [$separator] )
140  
141 Prints it as a string followed by fitness. Separator by default is C<;>
142  
143 =cut
144  
145 0 ▶sub as_string_with_fitness {
Subroutine as_string_with_fitness: not called CC=2 SCAR=17.9
Pod documented
146 0 my $self = shift;
147 0 ▶ my $separator = shift || "; ";
Condition0%0/2
shift() || '; '
l!l
00
MC/DC0%0/1
shift() || '; '
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
148 0 return $self->as_string().$separator.$self->Fitness();
149 }
150  
151 =head2 Atom( $index [, $value )
152  
153 Sets or gets the value of an atom; each individual is divided in atoms, which
154 can be accessed sequentially. If that does not apply, Atom can simply return the
155 whole individual
156  
157 =cut
158  
159 0 ▶sub Atom {
Subroutine Atom: not called CC=1 SCAR=6.9
Pod documented
160 0 croak "This function is not defined at this level, you should override it in a subclass\n";
161 }
162  
163 =head2 Fitness( [$value] )
164  
165 Sets or gets fitness
166  
167 =cut
168  
169 2 ▶sub Fitness {
Subroutine Fitness: called CC=2 SCAR=6.9
Pod documented
170 2 my $self = shift;
171 2 ▶ if ( defined $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 1 1 2
172 1 $self->{_fitness} = shift;
173 }
174 2 return $self->{_fitness};
175 }
176  
177 =head2 my_operators()
178  
179 Operators that can act on this data structure. Returns an array with the names of the known operators
180  
181 =cut
182  
183 2 ▶sub my_operators {
Subroutine my_operators: called CC=1 SCAR=0.0
Pod documented
184 2 my $self = shift;
185 2 return $self->MY_OPERATORS;
186 }
187  
188 =head2 evaluate( $fitness )
189  
190 Evaluates using the $fitness thingy given. Can be a L<Algorithm::Evolutionary::Fitness::Base|Fitness> object or a ref-to-sub
191  
192 =cut
193  
194 0 ▶sub evaluate {
Subroutine evaluate: not called CC=4 SCAR=30.0
Pod documented
195 0 my $self = shift;
196 0 ▶ my $fitness_func = shift || croak "Need a fitness function";
Condition0%0/2
shift() || croak('Need a fitness function')
l!l
00
MC/DC0%0/1
shift() || croak('Need a fitness function')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
197 0 ▶ if ( ref $fitness_func eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $fitness_func eq 'CODE') { } 0 0 0
elsif (ref($fitness_func) =~ /Fitness/) { } 0 0 0
198 0 return $self->Fitness( $fitness_func->($self) );
199 } elsif ( ( ref $fitness_func ) =~ 'Fitness' ) {
200 0 return $self->Fitness( $fitness_func->apply($self) );
201 } else {
202 0 croak "$fitness_func can't be used to evaluate";
203 }
204  
205 }
206  
207 =head2 Chrom()
208  
209 Sets or gets the chromosome itself, that is, the data
210 structure evolved. Since each derived class has its own
211 data structure, and its own name, it is left to them to return
212 it
213  
214 =cut
215  
216 0 ▶sub Chrom {
Subroutine Chrom: not called CC=1 SCAR=6.9
Pod documented
217 0 my $self = shift;
218 0 croak "To be implemented in derived classes!";
219 }
220  
221 =head2 size()
222  
223 OK, OK, this is utter inconsistence, but I'll re-consistence it
224 eventually. Returns a meaningful size; but should be reimplemented
225 by siblings
226  
227 =cut
228  
229 0 ▶sub size() {
Subroutine size: not called CC=1 SCAR=6.9
Pod documented
230 0 croak "To be implemented in derived classes!";
231 }
232  
233 =head1 Known subclasses
234  
235 There are others, but I'm not so sure they work.
236  
237 =over 4
238  
239 =item *
240  
241 L<Algorithm::Evolutionary::Individual::Vector>
242  
243 =item *
244  
245 L<Algorithm::Evolutionary::Individual::String>
246  
247 =item *
248  
249 L<Algorithm::Evolutionary::Individual::Tree>
250  
251 =item *
252  
253 L<Algorithm::Evolutionary::Individual::Bit_Vector>
254  
255 =back
256  
257 =head1 Copyright
258
259 This file is released under the GPL. See the LICENSE file included in this distribution,
260 or go to http://www.fsf.org/licenses/gpl.txt
261  
262  
263 =cut
264  
265 36 "The plain truth";
266  
« blib/lib/Algorithm/Evolutionary/Op/ChangeLengthMutation.pm ↑ Summary blib/lib/Algorithm/Evolutionary/Fitness/Any.pm »