lib/Algorithm/Evolutionary/Individual/BitString.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/Algorithm/Evolutionary/Experiment.pm ↑ Summary lib/Algorithm/Evolutionary/Op/Base.pm »
1 25 ▶use strict; #-*-cperl-*-
Subroutine BEGIN: called CC=1 SCAR=0.0
2 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 =head1 NAME
5  
6 Algorithm::Evolutionary::Individual::BitString - Classic bitstring individual for evolutionary computation; usually called I<chromosome>
7  
8  
9 =head1 SYNOPSIS
10  
11 use Algorithm::Evolutionary::Individual::BitString;
12  
13 my $indi = new Algorithm::Evolutionary::Individual::BitString 10 ; # Build random bitstring with length 10
14 # Each element in the range 0 .. 1
15  
16 my $indi3 = new Algorithm::Evolutionary::Individual::BitString;
17 $indi3->set( { length => 20 } ); #Sets values, but does not build the string
18  
19 $indi3->randomize(); #Creates a random bitstring with length as above
20  
21 print $indi3->Atom( 7 ); #Returns the value of the 7th character
22 $indi3->Atom( 3 ) = 1; #Sets the value
23  
24 $indi3->addAtom( 1 ); #Adds a new character to the bitstring at the end
25 my $size = $indi3->size(); #Common interface to all individuals, should return 21
26  
27 my $indi4 = Algorithm::Evolutionary::Individual::BitString->fromString( '10110101'); #Creates an individual from that string
28  
29 my $indi5 = $indi4->clone(); #Creates a copy of the individual
30  
31 my @array = qw( 0 1 0 1 0 0 1 ); #Create a tied array
32 tie my @vector, 'Algorithm::Evolutionary::Individual::BitString', @array;
33  
34 print $indi3->asString(); #Prints the individual
35 print $indi3->as_yaml() #Change of convention, I know...
36  
37 my $gene_size = 5;
38 my $min = -1;
39 my $range = 2;
40 my @decoded_vector = $indi3->decode( $gene_size, $min, $range);
41  
42 =head1 Base Class
43  
44 L<Algorithm::Evolutionary::Individual::String>
45  
46 =head1 DESCRIPTION
47  
48 Bitstring Individual for a Genetic Algorithm. Used, for instance, in a canonical GA
49  
50 =cut
51  
52 package Algorithm::Evolutionary::Individual::BitString;
53  
54 25 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 our $VERSION = '3.4';
57  
58 25 ▶use base 'Algorithm::Evolutionary::Individual::String';
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 25 ▶use constant MY_OPERATORS => ( Algorithm::Evolutionary::Individual::String::MY_OPERATORS,
Subroutine BEGIN: called CC=1 SCAR=0.0
61 25 qw(Algorithm::Evolutionary::Op::BitFlip Algorithm::Evolutionary::Op::Mutation ));
62  
63 0 ▶use Algorithm::Evolutionary::Utils qw(decode_string);
Subroutine BEGIN: called CC=1 SCAR=2.6
64  
65 =head1 METHODS
66  
67 =head2 new( $length )
68  
69 Creates a new random bitstring individual, with fixed initial length, and
70 uniform distribution of bits. Options as in L<Algorithm::Evolutionary::Individual::String>
71  
72 =cut
73  
74 sub new {
75 my $class = shift;
76 my $chars = [ '0', '1' ];
77 my $self =
78 Algorithm::Evolutionary::Individual::String::new( $class, $chars, @_ );
79 return $self;
80 }
81  
82 =head2 from_string
83  
84 Similar to a copy ctor; creates a bitstring individual from a string. Will be deprecated soon
85  
86 =cut
87  
88 sub from_string {
89 my $class = shift;
90 my $chars = [ '0', '1' ];
91 my $self =
92 Algorithm::Evolutionary::Individual::String::from_string( 'Algorithm::Evolutionary::Individual::BitString', $chars, @_ );
93 return $self;
94 }
95  
96 =head2 set( $hash )
97  
98 Sets values of an individual; takes a hash as input. Keys are prepended an
99 underscore and turn into instance variables
100  
101 =cut
102  
103 sub set {
104 my $self = shift;
105 my $hash = shift || croak "No params here";
106 $self->{_chars} = [ '0', '1' ];
107 $self->SUPER::set( $hash );
108 }
109  
110 =head2 decode( $gene_size, $min, $range )
111  
112 Decodes to a vector, each one of whose components ranges between $min
113 and $max. Returns that vector
114  
115 =cut
116  
117 sub decode {
118 my $self = shift;
119 my ( $gene_size, $min, $range ) = @_;
120 my $chromosome = $self->{'_str'};
121 return decode_string( $chromosome, $gene_size, $min, $range );
122 }
123  
124 =head2 Copyright
125
126 This file is released under the GPL. See the LICENSE file included in this distribution,
127 or go to http://www.fsf.org/licenses/gpl.txt
128  
129 =cut
« lib/Algorithm/Evolutionary/Experiment.pm ↑ Summary lib/Algorithm/Evolutionary/Op/Base.pm »