lib/Algorithm/Evolutionary/Individual/Bit_Vector.pm

filter: Statementstmt 90.9% 90 / 99 Branchbran 75.0% 9 / 12 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 87.5% 21 / 24 Podpod 100% 6 / 6 totaltotal 87.0% 128 / 147 CC 9 SCAR 8.5
CC
9
Cov
88%
new
6.7
_create_bit_vector
2.0
Atom
2.0
CRAP 2.3
« lib/Algorithm/Evolutionary/Fitness/ONEMAX.pm ↑ Summary lib/Algorithm/Evolutionary/Individual/String.pm »
1 2 ▶use strict; #-*-cperl-*-
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use lib qw(../../../../lib);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Algorithm::Evolutionary::Individual::Bit_Vector - Classic bitstring individual for evolutionary computation;
9 usually called chromosome, and using a different implementation from Algorithm::Evolutionary::Individual::BitString
10  
11  
12 =head1 SYNOPSIS
13  
14 use Algorithm::Evolutionary::Individual::BitVector;
15  
16 my $indi = new Algorithm::Evolutionary::Individual::Bit_Vector 10 ; # Build random bitstring with length 10
17 # Each element in the range 0 .. 1
18  
19 my $indi3 = new Algorithm::Evolutionary::Individual::Bit_Vector;
20 $indi3->set( { length => 20 } ); #Sets values, but does not build the string
21
22 $indi3->randomize(); #Creates a random bitstring with length as above
23
24 print $indi3->Atom( 7 ); #Returns the value of the 7th character
25 $indi3->Atom( 3 ) = 1; #Sets the value
26  
27 $indi3->addAtom( 1 ); #Adds a new character to the bitstring at the end
28  
29 my $indi4 = Algorithm::Evolutionary::Individual::Bit_Vector->fromString( '10110101'); #Creates an individual from that string
30  
31 my $indi5 = $indi4->clone(); #Creates a copy of the individual
32  
33 my @array = qw( 0 1 0 1 0 0 1 ); #Create a tied array
34 tie my @vector, 'Algorithm::Evolutionary::Individual::Bit_Vector', @array;
35 print tied( @vector )->asXML();
36  
37 print $indi3->asString(); #Prints the individual
38 print $indi3->asXML() #Prints it as XML. See L<Algorithm::Evolutionary::XML>
39 print $indi3->as_yaml() #Change of convention, I know...
40  
41 =head1 Base Class
42  
43 L<Algorithm::Evolutionary::Individual::String|Algorithm::Evolutionary::Individual::String>
44  
45 =head1 DESCRIPTION
46  
47 Bitstring Individual for a Genetic Algorithm. Used, for instance, in a canonical GA
48  
49 =cut
50  
51 package Algorithm::Evolutionary::Individual::Bit_Vector;
52  
53 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 2 ▶use Bit::Vector;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 2 ▶use String::Random; # For initial string generation
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 2 our ($VERSION) = ( '$Revision: 3.1 $ ' =~ / (\d+\.\d+)/ );
58  
59 2 ▶use base 'Algorithm::Evolutionary::Individual::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 2 ▶use constant MY_OPERATORS => ( qw(Algorithm::Evolutionary::Op::BitFlip Algorithm::Evolutionary::Op::Mutation ));
Subroutine BEGIN: called CC=1 SCAR=0.0
62
63  
64 =head1 METHODS
65  
66 =head2 new( $arg )
67  
68 Creates a new bitstring individual. C<$arg> can be either { length =>
69 $length} or { string => [binary string] }. With no argument, a
70 length of 16 is given by default.
71  
72 =cut
73  
74 3 ▶sub new {
Subroutine new: called CC=6 SCAR=19.0
Pod documented
75 3 my $class = shift;
76 3 my $self = Algorithm::Evolutionary::Individual::Base::new( $class );
77 3 ▶ my $arg = shift || { length => 16};
Condition50%1/2
shift() || {'length', 16}
l!l
30
MC/DC0%0/1
shift() || {'length', 16}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
78 3 ▶ if ( $arg =~ /^\d+$/ ) { #It's a number
Branch: 3 branches
BranchTrueFalseTotal
if ($arg =~ /^\d+$/) { } 0 3 3
elsif ($arg->{'length'}) { } 2 1 3
elsif ($arg->{'string'}) { } 1 0 1
79 0 $self->{'_bit_vector'} = _create_bit_vector( $arg );
80 } elsif ( $arg->{'length'} ) {
81 2 $self->{'_bit_vector'} = _create_bit_vector( $arg->{'length'} );
82 } elsif ( $arg->{'string'} ) {
83 $self->{'_bit_vector'} =
84 1 Bit::Vector->new_Bin( length($arg->{'string'}), $arg->{'string'} );
85 }
86 3 ▶ croak "Incorrect creation options" if !$self->{'_bit_vector'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_bit_vector"} 0 3 3
87 3 return $self;
88 }
89  
90 2 ▶sub _create_bit_vector {
Subroutine _create_bit_vector: called CC=2 SCAR=7.0
91 2 ▶ my $length = shift || croak "No length!";
Condition50%1/2
shift() || croak('No length!')
l!l
20
MC/DC0%0/1
shift() || croak('No length!')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
92 2 my $rander = new String::Random;
93 2 my $hex_string = $rander->randregex("[0-9A-F]{".int($length/4)."}");
94 2 return Bit::Vector->new_Hex( $length, $hex_string );
95 }
96  
97 1 ▶sub TIEARRAY {
Subroutine TIEARRAY: called CC=1 SCAR=0.0
98 1 my $class = shift;
99 1 my $self = { _bit_vector => Bit::Vector->new_Bin(scalar( @_), join("",@_)) };
100 1 bless $self, $class;
101 1 return $self;
102 }
103  
104 =head2 Atom
105  
106 Sets or gets the value of the n-th character in the string. Counting
107 starts at 0, as usual in Perl arrays.
108  
109 =cut
110  
111 20 ▶sub Atom: lvalue {
Subroutine Atom: called CC=2 SCAR=6.9
Pod documented
112 20 my $self = shift;
113 20 my $index = shift;
114 20 my $last_index = $self->{'_bit_vector'}->Size()-1;
115 20 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 19 20
116 1 $self->{'_bit_vector'}->Bit_Copy($last_index-$index, shift );
117 } else {
118 19 $self->{'_bit_vector'}->bit_test($last_index - $index);
119 }
120 }
121  
122 =head2 size()
123  
124 Returns size in bits
125  
126 =cut
127  
128 19 ▶sub size {
Subroutine size: called CC=1 SCAR=0.0
Pod documented
129 19 my $self = shift;
130 19 return $self->{'_bit_vector'}->Size();
131 }
132  
133 =head2 clone()
134  
135 Clones using native methods. Does not work with general Clone::Fast, since it's implemented as an XS
136  
137 =cut
138  
139 0 ▶sub clone {
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
140 0 my $self = shift;
141 0 my $clone = Algorithm::Evolutionary::Individual::Base::new( ref $self );
142 0 $clone->{'_bit_vector'} = $self->{'_bit_vector'}->Clone();
143 0 return $clone;
144 }
145  
146 =head2 as_string()
147  
148 Overrides the default; prints the binary chromosome
149  
150 =cut
151  
152 11 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
153 11 my $self = shift;
154 11 return $self->{'_bit_vector'}->to_Bin();
155 }
156  
157 =head2 Chrom()
158  
159 Returns the internal bit_vector
160  
161 =cut
162  
163 0 ▶sub Chrom {
Subroutine Chrom: not called CC=1 SCAR=6.9
Pod documented
164 0 my $self = shift;
165 0 return $self->{'_bit_vector'};
166 }
167  
168 =head2 TIE methods
169  
170 String implements FETCH, STORE, PUSH and the rest, so an String
171 can be tied to an array and used as such.
172  
173 =cut
174  
175 2 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
176 2 my $self = shift;
177 2 my $bit_vector = $self->{'_bit_vector'};
178 2 return $bit_vector->bit_test( $bit_vector->Size() - 1 - shift );
179 }
180  
181 1 ▶sub STORE {
Subroutine STORE: called CC=1 SCAR=0.0
182 1 my $self = shift;
183 1 my $bit_vector = $self->{'_bit_vector'};
184 1 my $index = shift;
185 1 $self->{'_bit_vector'}->Bit_Copy($bit_vector->Size()- 1 -$index, shift );
186 }
187  
188 1 ▶sub PUSH {
Subroutine PUSH: called CC=1 SCAR=0.0
189 1 my $self = shift;
190 1 my $new_vector = Bit::Vector->new_Bin(scalar(@_), join("",@_));
191 1 $self->{'_bit_vector'} = $self->{'_bit_vector'}->Concat( $new_vector );
192 }
193  
194 1 ▶sub UNSHIFT {
Subroutine UNSHIFT: called CC=1 SCAR=0.0
195 1 my $self = shift;
196 1 my $new_vector = Bit::Vector->new_Bin(scalar(@_), join("",@_));
197 1 $self->{'_bit_vector'} = Bit::Vector->Concat_List( $new_vector, $self->{'_bit_vector'}) ;
198 }
199  
200 1 ▶sub POP {
Subroutine POP: called CC=1 SCAR=0.0
201 1 my $self = shift;
202 1 my $bit_vector = $self->{'_bit_vector'};
203 1 my $length = $bit_vector->Size();
204 1 my $pop = $bit_vector->lsb();
205 1 $self->{'_bit_vector'}->Delete(0,1);
206 1 $self->{'_bit_vector'}->Resize($length-1);
207 1 return $pop;
208 }
209  
210 1 ▶sub SHIFT {
Subroutine SHIFT: called CC=1 SCAR=0.0
211 1 my $self = shift;
212 1 my $length = $self->{'_bit_vector'}->Size();
213 1 my $bit = $self->{'_bit_vector'}->shift_left('0');
214 1 $self->{'_bit_vector'}->Reverse( $self->{'_bit_vector'});
215 1 $self->{'_bit_vector'}->Resize($length-1);
216 1 $self->{'_bit_vector'}->Reverse( $self->{'_bit_vector'});
217  
218 1 return $bit;
219 }
220  
221 3 ▶sub SPLICE {
Subroutine SPLICE: called CC=2 SCAR=6.9
222 3 my $self = shift;
223 3 my $offset = shift;
224 3 my $bits = shift;
225 3 my $new_vector;
226 3 my $slice = Bit::Vector->new($bits);
227 3 my $size = $self->{'_bit_vector'}->Size();
228 3 $slice->Interval_Copy( $self->{'_bit_vector'}, 0, $size-$offset-$bits, $bits );
229 3 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 2 3
230 1 $new_vector = Bit::Vector->new_Bin(scalar(@_), join("",@_));
231 1 $self->{'_bit_vector'}->Interval_Substitute( $new_vector,
232 $size-$offset-$bits, 0 , 0,
233 $new_vector->Size() );
234 } else {
235 2 $self->{'_bit_vector'}->Interval_Substitute( Bit::Vector->new(0), $size-$offset-$bits, $bits,
236 0, 0 );
237 }
238 3 return split(//,$slice->to_Bin());
239  
240 }
241  
242 0 ▶sub FETCHSIZE {
Subroutine FETCHSIZE: not called CC=1 SCAR=6.9
243 0 my $self = shift;
244 0 return length( $self->{'_bit_vector'}->Size() );
245 }
246  
247  
248 =head2 Copyright
249
250 This file is released under the GPL. See the LICENSE file included in this distribution,
251 or go to http://www.fsf.org/licenses/gpl.txt
252  
253 CVS Info: $Date: 2010/12/19 21:39:12 $
254 $Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Individual/Bit_Vector.pm,v 3.1 2010/12/19 21:39:12 jmerelo Exp $
255 $Author: jmerelo $
256 $Revision: 3.1 $
257 $Name $
258  
259 =cut
« lib/Algorithm/Evolutionary/Fitness/ONEMAX.pm ↑ Summary lib/Algorithm/Evolutionary/Individual/String.pm »