line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
24
|
|
|
24
|
|
252483
|
use strict; #-*-cperl-*- |
|
24
|
|
|
|
|
55
|
|
|
24
|
|
|
|
|
1245
|
|
2
|
24
|
|
|
24
|
|
216
|
use warnings; |
|
24
|
|
|
|
|
44
|
|
|
24
|
|
|
|
|
1546
|
|
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
|
|
|
|
|
|
|
print tied( @vector )->asXML(); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
print $indi3->asString(); #Prints the individual |
36
|
|
|
|
|
|
|
print $indi3->asXML() #Prints it as XML. See |
37
|
|
|
|
|
|
|
print $indi3->as_yaml() #Change of convention, I know... |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
my $gene_size = 5; |
40
|
|
|
|
|
|
|
my $min = -1; |
41
|
|
|
|
|
|
|
my $range = 2; |
42
|
|
|
|
|
|
|
my @decoded_vector = $indi3->decode( $gene_size, $min, $range); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head1 Base Class |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
L |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=head1 DESCRIPTION |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
Bitstring Individual for a Genetic Algorithm. Used, for instance, in a canonical GA |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=cut |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
package Algorithm::Evolutionary::Individual::BitString; |
55
|
|
|
|
|
|
|
|
56
|
24
|
|
|
24
|
|
217
|
use Carp; |
|
24
|
|
|
|
|
372
|
|
|
24
|
|
|
|
|
8832
|
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
our ($VERSION) = ( '$Revision: 3.4 $ ' =~ /(\d+\.\d+)/ ); |
59
|
|
|
|
|
|
|
|
60
|
24
|
|
|
24
|
|
176
|
use base 'Algorithm::Evolutionary::Individual::String'; |
|
24
|
|
|
|
|
47
|
|
|
24
|
|
|
|
|
20001
|
|
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
use constant MY_OPERATORS => ( Algorithm::Evolutionary::Individual::String::MY_OPERATORS, |
63
|
|
|
|
|
|
|
qw(Algorithm::Evolutionary::Op::BitFlip Algorithm::Evolutionary::Op::Mutation )); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
use Algorithm::Evolutionary::Utils qw(decode_string); |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head1 METHODS |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head2 new( $length ) |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
Creates a new random bitstring individual, with fixed initial length, and |
72
|
|
|
|
|
|
|
uniform distribution of bits. Options as in L |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=cut |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub new { |
77
|
|
|
|
|
|
|
my $class = shift; |
78
|
|
|
|
|
|
|
my $chars = [ '0', '1' ]; |
79
|
|
|
|
|
|
|
my $self = |
80
|
|
|
|
|
|
|
Algorithm::Evolutionary::Individual::String::new( 'Algorithm::Evolutionary::Individual::BitString', $chars, @_ ); |
81
|
|
|
|
|
|
|
return $self; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head2 from_string |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Similar to a copy ctor; creates a bitstring individual from a string. Will be deprecated soon |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=cut |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub from_string { |
91
|
|
|
|
|
|
|
my $class = shift; |
92
|
|
|
|
|
|
|
my $chars = [ '0', '1' ]; |
93
|
|
|
|
|
|
|
my $self = |
94
|
|
|
|
|
|
|
Algorithm::Evolutionary::Individual::String::from_string( 'Algorithm::Evolutionary::Individual::BitString', $chars, @_ ); |
95
|
|
|
|
|
|
|
return $self; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head2 set( $hash ) |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Sets values of an individual; takes a hash as input. Keys are prepended an |
101
|
|
|
|
|
|
|
underscore and turn into instance variables |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=cut |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub set { |
106
|
|
|
|
|
|
|
my $self = shift; |
107
|
|
|
|
|
|
|
my $hash = shift || croak "No params here"; |
108
|
|
|
|
|
|
|
$self->{_chars} = [ '0', '1' ]; |
109
|
|
|
|
|
|
|
$self->SUPER::set( $hash ); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head2 decode( $gene_size, $min, $range ) |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Decodes to a vector, each one of whose components ranges between $min |
115
|
|
|
|
|
|
|
and $max. Returns that vector |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=cut |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub decode { |
120
|
|
|
|
|
|
|
my $self = shift; |
121
|
|
|
|
|
|
|
my ( $gene_size, $min, $range ) = @_; |
122
|
|
|
|
|
|
|
my $chromosome = $self->{'_str'}; |
123
|
|
|
|
|
|
|
return decode_string( $chromosome, $gene_size, $min, $range ); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head2 Copyright |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
This file is released under the GPL. See the LICENSE file included in this distribution, |
129
|
|
|
|
|
|
|
or go to http://www.fsf.org/licenses/gpl.txt |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
CVS Info: $Date: 2010/09/24 08:39:07 $ |
132
|
|
|
|
|
|
|
$Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Individual/BitString.pm,v 3.4 2010/09/24 08:39:07 jmerelo Exp $ |
133
|
|
|
|
|
|
|
$Author: jmerelo $ |
134
|
|
|
|
|
|
|
$Revision: 3.4 $ |
135
|
|
|
|
|
|
|
$Name $ |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=cut |