lib/Algorithm/Evolutionary/Individual/String.pm

filter: Statementstmt 83.5% 86 / 103 Branchbran 50.0% 4 / 8 Conditioncond 66.6% 6 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 88.4% 23 / 26 Podpod 100% 11 / 11 totaltotal 81.4% 132 / 162 CC 10 SCAR 7.5
CC
10
Cov
80%
asString
6.0
Chrom
6.0
clone
3.0
CRAP 2.1
« lib/Algorithm/Evolutionary/Individual/Bit_Vector.pm ↑ Summary blib/lib/Algorithm/Evolutionary/Fitness/Rastrigin.pm »
1 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 32 ▶use lib qw(../../../lib);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =encoding utf8
7  
8 =head1 NAME
9  
10 Algorithm::Evolutionary::Individual::String - A character string to be evolved. Useful mainly in word games
11  
12 =head1 SYNOPSIS
13  
14 use Algorithm::Evolutionary::Individual::String;
15  
16 my $indi = new Algorithm::Evolutionary::Individual::String ['a'..'z'], 10;
17 # Build random bitstring with length 10
18  
19 my $indi3 = new Algorithm::Evolutionary::Individual::String;
20 $indi3->set( { length => 20,
21 chars => ['A'..'Z'] } ); #Sets values, but does not build the string
22 $indi3->randomize(); #Creates a random bitstring with length as above
23 print $indi3->Atom( 7 ); #Returns the value of the 7th character
24 $indi3->Atom( 3, 'Q' ); #Sets the value
25  
26 $indi3->addAtom( 'K' ); #Adds a new character to the bitstring at the end
27  
28 my $indi4 = Algorithm::Evolutionary::Individual::String->fromString( 'esto es un string'); #Creates an individual from that string
29  
30 my $indi5 = $indi4->clone(); #Creates a copy of the individual
31  
32 my @array = qw( a x q W z ñ); #Tie a String individual
33 tie my @vector, 'Algorithm::Evolutionary::Individual::String', @array;
34  
35 print $indi3->as_string(); #Prints the individual
36  
37 =head1 Base Class
38  
39 L<Algorithm::Evolutionary::Individual::Base>
40  
41 =head1 DESCRIPTION
42  
43 String Individual for a evolutionary algorithm. Contains methods to handle strings
44 easily. It is also TIEd so that strings can be handled as arrays.
45  
46 =head1 METHODS
47  
48 =cut
49  
50 package Algorithm::Evolutionary::Individual::String;
51  
52 32 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 32 our $VERSION = '3.7';
55  
56 32 ▶use base 'Algorithm::Evolutionary::Individual::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 =head2 MY_OPERATORS
59  
60 Known operators that act on this subroutine. Probably will be deprecated, so don't rely on its presence
61  
62 =cut
63  
64 32 ▶use constant MY_OPERATORS => qw(Algorithm::Evolutionary::Op::Crossover
Subroutine BEGIN: called CC=1 SCAR=0.0
65 Algorithm::Evolutionary::Op::QuadXOver
66 Algorithm::Evolutionary::Op::StringRand
67 Algorithm::Evolutionary::Op::Permutation
68 Algorithm::Evolutionary::Op::IncMutation
69 32 Algorithm::Evolutionary::Op::ChangeLengthMutation );
70  
71 =head2 new
72  
73 Creates a new random string, with fixed initial length, and uniform
74 distribution of characters along the character class that is
75 defined. However, this character class is just used to generate new
76 individuals and in mutation operators, and the validity is not
77 enforced unless the client class does it
78  
79 =cut
80  
81 5 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
82 5 my $class = shift;
83 5 my $self = Algorithm::Evolutionary::Individual::Base::new( $class );
84 4 ▶ $self->{'_chars'} = shift || ['a'..'z'];
Condition100%2/2
shift() || [('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')]
l!l
31
MC/DC100%1/1
shift() || [('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')]
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
85 4 ▶ $self->{'_length'} = shift || 10;
Condition100%2/2
shift() || 10
l!l
31
MC/DC100%1/1
shift() || 10
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
86 4 $self->randomize();
87 4 return $self;
88 }
89  
90 1 ▶sub TIEARRAY {
Subroutine TIEARRAY: called CC=1 SCAR=0.0
91 1 my $class = shift;
92 1 my $self = { _str => join("",@_),
93 _length => scalar( @_ ),
94 _fitness => undef };
95 1 bless $self, $class;
96 1 return $self;
97 }
98  
99 =head2 randomize
100  
101 Assigns random values to the elements
102  
103 =cut
104  
105 5 ▶sub randomize {
Subroutine randomize: called CC=2 SCAR=6.9
Pod documented
106 5 my $self = shift;
107 5 $self->{'_str'} = ''; # Reset string
108 5 ▶ for ( my $i = 0; $i < $self->{'_length'}; $i ++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"_length"}) 88 5 93
109 88 $self->{'_str'} .= $self->{'_chars'}[ rand( @{$self->{'_chars'}} ) ];
110 }
111 }
112  
113 =head2 addAtom
114  
115 Adds an atom at the end
116  
117 =cut
118  
119 1 ▶sub addAtom{
Subroutine addAtom: called CC=1 SCAR=0.0
Pod documented
120 1 my $self = shift;
121 1 my $atom = shift;
122 1 $self->{_str}.= $atom;
123 }
124  
125 =head2 fromString
126  
127 Similar to a copy ctor; creates a bitstring individual from a string. Will be deprecated soon
128  
129 =cut
130  
131 1 ▶sub fromString {
Subroutine fromString: called CC=1 SCAR=0.0
Pod documented
132 1 my $class = shift;
133 1 my $str = shift;
134 1 my $self = Algorithm::Evolutionary::Individual::Base::new( $class );
135 1 $self->{_str} = $str;
136 1 my %chars;
137 1 map ( $chars{$_} = 1, split(//,$str) );
138 1 my @chars = keys %chars;
139 1 $self->{_length} = length( $str );
140 1 $self->{'_chars'} = \@chars;
141 1 return $self;
142 }
143  
144 =head2 from_string
145  
146 Similar to a copy ctor; creates a bitstring individual from a string.
147  
148 =cut
149  
150 0 ▶sub from_string {
Subroutine from_string: not called CC=1 SCAR=6.9
Pod documented
151 0 my $class = shift;
152 0 my $chars = shift;
153 0 my $str = shift;
154 0 my $self = Algorithm::Evolutionary::Individual::Base::new( $class );
155 0 $self->{'_chars'} = $chars;
156 0 $self->{'_str'} = $str;
157 0 $self->{'_length'} = length( $str );
158 0 return $self;
159 }
160  
161 =head2 clone
162  
163 Similar to a copy ctor: creates a new individual from another one
164  
165 =cut
166  
167 1 ▶sub clone {
Subroutine clone: called CC=3 SCAR=11.0
Pod documented
168 1 ▶ my $indi = shift || croak "Indi to clone missing ";
Condition50%1/2
shift() || croak('Indi to clone missing ')
l!l
10
MC/DC0%0/1
shift() || croak('Indi to clone missing ')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
169 1 my $self = { '_fitness' => undef };
170 1 bless $self, ref $indi;
171 1 for ( qw( _chars _str _length) ) {
172 3 $self->{ $_ } = $indi->{$_};
173 }
174 1 return $self;
175 }
176  
177  
178 =head2 asString
179  
180 Returns the individual as a string with the fitness as a suffix.
181  
182 =cut
183  
184 0 ▶sub asString {
Subroutine asString: not called CC=2 SCAR=17.9
Pod documented
185 0 my $self = shift;
186 0 my $str = $self->{'_str'} . " -> ";
187 0 ▶ if ( defined $self->{'_fitness'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_fitness"}) 0 0 0
188 0 $str .=$self->{'_fitness'};
189 }
190 0 return $str;
191 }
192  
193 =head2 Atom
194  
195 Sets or gets the value of the n-th character in the string. Counting
196 starts at 0, as usual in Perl arrays.
197  
198 =cut
199  
200 51 ▶sub Atom {
Subroutine Atom: called CC=2 SCAR=6.9
Pod documented
201 51 my $self = shift;
202 51 my $index = shift;
203 51 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 3 48 51
204 3 substr( $self->{_str}, $index, 1 ) = substr(shift,0,1);
205 } else {
206 48 substr( $self->{_str}, $index, 1 );
207 }
208 }
209  
210 =head2 TIE methods
211  
212 String implements FETCH, STORE, PUSH and the rest, so an String
213 can be tied to an array and used as such.
214  
215 =cut
216  
217 26 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
218 26 my $self = shift;
219 26 return $self->Atom( @_ );
220 }
221  
222 2 ▶sub STORE {
Subroutine STORE: called CC=1 SCAR=0.0
223 2 my $self = shift;
224 2 $self->Atom( @_ );
225 }
226  
227 2 ▶sub PUSH {
Subroutine PUSH: called CC=1 SCAR=0.0
228 2 my $self = shift;
229 2 $self->{_str}.= join("", @_ );
230 }
231  
232 1 ▶sub UNSHIFT {
Subroutine UNSHIFT: called CC=1 SCAR=0.0
233 1 my $self = shift;
234 1 $self->{_str} = join("", @_ ).$self->{_str} ;
235 }
236  
237 2 ▶sub POP {
Subroutine POP: called CC=1 SCAR=0.0
238 2 my $self = shift;
239 2 my $pop = substr( $self->{_str}, length( $self->{_str} )-1, 1 );
240 2 substr( $self->{_str}, length( $self->{_str} ) -1, 1 ) = '';
241 2 return $pop;
242 }
243  
244 2 ▶sub SHIFT {
Subroutine SHIFT: called CC=1 SCAR=0.0
245 2 my $self = shift;
246 2 my $shift = substr( $self->{_str}, 0, 1 );
247 2 substr( $self->{_str}, 0, 1 ) = '';
248 2 return $shift;
249 }
250  
251 2 ▶sub SPLICE {
Subroutine SPLICE: called CC=2 SCAR=7.1
252 2 my $self = shift;
253 2 my $offset = shift;
254 2 ▶ my $length = shift || length( $self->{'_str'} - $offset );
Condition33%1/3
shift() || length $self->{'_str'} - $offset
l!l&&r!l&&!r
200
MC/DC0%0/2
shift() || length $self->{'_str'} - $offset
shift() length $self->{'_str'} - $offset
Truth table33%1/3
A || B
A= shift()B= length $self->{'_str'} - $offset
ABresult
1X1
011
000
255 2 my $sub_string = substr( $self->{_str}, $offset, $length );
256 # if ( @_ ) {
257 2 substr( $self->{_str}, $offset, $length ) = join("", @_ );
258 # }
259 2 return split(//,$sub_string);
260 }
261  
262 8 ▶sub FETCHSIZE {
Subroutine FETCHSIZE: called CC=1 SCAR=0.0
263 8 my $self = shift;
264 8 return length( $self->{_str} );
265 }
266  
267 =head2 size()
268  
269 Returns length of the string that stores the info; overloads abstract base method.
270  
271 =cut
272  
273 18 ▶sub size {
Subroutine size: called CC=1 SCAR=0.0
Pod documented
274 18 my $self = shift;
275 18 return length($self->{_str}); #Solves ambiguity
276 }
277  
278 =head2 as_string()
279
280 Returns the string used as internal representation
281  
282 =cut
283  
284 1 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
285 1 my $self = shift;
286 1 return $self->{_str};
287 }
288  
289 =head2 Chrom
290  
291 Sets or gets the variable that holds the chromosome. Not very nice, and
292 I would never ever do this in C++
293  
294 =cut
295  
296 0 ▶sub Chrom {
Subroutine Chrom: not called CC=2 SCAR=17.9
Pod documented
297 0 my $self = shift;
298 0 ▶ if ( defined $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 0 0
299 0 $self->{_str} = shift;
300 }
301 return $self->{_str}
302 0 }
303  
304 =head1 Known subclasses
305  
306 =over 4
307  
308 =item *
309  
310 L<Algorithm::Evolutionary::Individual::BitString|Algorithm::Evolutionary::Individual::BitString>
311  
312 =back
313  
314 =head2 Copyright
315  
316 This file is released under the GPL. See the LICENSE file included in this distribution,
317 or go to http://www.fsf.org/licenses/gpl.txt
318  
319 =cut
« lib/Algorithm/Evolutionary/Individual/Bit_Vector.pm ↑ Summary blib/lib/Algorithm/Evolutionary/Fitness/Rastrigin.pm »