line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
25
|
|
|
25
|
|
113891
|
use strict; #-*-cperl-*- |
|
25
|
|
|
|
|
41
|
|
|
25
|
|
|
|
|
828
|
|
2
|
25
|
|
|
25
|
|
97
|
use warnings; |
|
25
|
|
|
|
|
51
|
|
|
25
|
|
|
|
|
1105
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
=head1 NAME |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
Algorithm::Evolutionary::Individual::BitString - Classic bitstring individual for evolutionary computation; usually called I |
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 |
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
|
|
|
25
|
|
104
|
use Carp; |
|
25
|
|
|
|
|
28
|
|
|
25
|
|
|
|
|
1762
|
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
our $VERSION = '3.4'; |
57
|
|
|
|
|
|
|
|
58
|
25
|
|
|
25
|
|
120
|
use base 'Algorithm::Evolutionary::Individual::String'; |
|
25
|
|
|
|
|
46
|
|
|
25
|
|
|
|
|
7978
|
|
59
|
|
|
|
|
|
|
|
60
|
25
|
|
|
|
|
1351
|
use constant MY_OPERATORS => ( Algorithm::Evolutionary::Individual::String::MY_OPERATORS, |
61
|
25
|
|
|
25
|
|
137
|
qw(Algorithm::Evolutionary::Op::BitFlip Algorithm::Evolutionary::Op::Mutation )); |
|
25
|
|
|
|
|
30
|
|
62
|
|
|
|
|
|
|
|
63
|
25
|
|
|
25
|
|
10091
|
use Algorithm::Evolutionary::Utils qw(decode_string); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
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 |
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( 'Algorithm::Evolutionary::Individual::BitString', $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 |