line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
4
|
|
|
4
|
|
27
|
use strict; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
130
|
|
2
|
4
|
|
|
4
|
|
15
|
use warnings; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
286
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
=head1 NAME |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
Algorithm::Evolutionary::Op::Mutation - BitFlip mutation, changes several bits in a bitstring, depending on the probability |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 SYNOPSIS |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use Algorithm::Evolutionary::Op::Mutation; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
my $xmlStr=<
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
EOC |
17
|
|
|
|
|
|
|
my $ref = XMLin($xmlStr); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my $op = Algorithm::Evolutionary::Op::->fromXML( $ref ); |
20
|
|
|
|
|
|
|
print $op->asXML(), "\n*Arity ->", $op->arity(), "\n"; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
#Create from scratch |
23
|
|
|
|
|
|
|
my $op = new Algorithm::Evolutionary::Op::Mutation (0.5 ); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
#All options |
26
|
|
|
|
|
|
|
my $priority = 1; |
27
|
|
|
|
|
|
|
my $mutation = new Algorithm::Evolutionary::Op::Mutation 1/$length, $priority; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 Base Class |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
L |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head1 DESCRIPTION |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
Mutation operator for a GA |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=cut |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
package Algorithm::Evolutionary::Op::Mutation; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
our ($VERSION) = ( '$Revision: 3.1 $ ' =~ /(\d+\.\d+)/ ); |
42
|
|
|
|
|
|
|
|
43
|
4
|
|
|
4
|
|
21
|
use Carp; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
210
|
|
44
|
|
|
|
|
|
|
|
45
|
4
|
|
|
4
|
|
14
|
use base 'Algorithm::Evolutionary::Op::Base'; |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
1301
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
#Class-wide constants |
48
|
|
|
|
|
|
|
our $APPLIESTO = 'Algorithm::Evolutionary::Individual::BitString'; |
49
|
|
|
|
|
|
|
our $ARITY = 1; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head1 METHODS |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head2 new( [$mutation_rate] [, $operator_probability] ) |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Creates a new mutation operator with a bitflip application rate, which |
56
|
|
|
|
|
|
|
defaults to 0.5, and an operator application rate (general for all |
57
|
|
|
|
|
|
|
ops), which defaults to 1. Application rate will be converted in |
58
|
|
|
|
|
|
|
runtime to application probability, which will eventually depend on |
59
|
|
|
|
|
|
|
the rates of all the other operators. For instance, if this operator's |
60
|
|
|
|
|
|
|
rate is one and there's another with rate=4, probability will be 20% |
61
|
|
|
|
|
|
|
for this one and 80% for the other; 1 in 5 new individuals will be |
62
|
|
|
|
|
|
|
generated using this and the rest using the other one. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=cut |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub new { |
67
|
|
|
|
|
|
|
my $class = shift; |
68
|
|
|
|
|
|
|
my $mutRate = shift || 0.5; |
69
|
|
|
|
|
|
|
my $rate = shift || 1; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
my $hash = { mutRate => $mutRate }; |
72
|
|
|
|
|
|
|
my $self = Algorithm::Evolutionary::Op::Base::new( 'Algorithm::Evolutionary::Op::Mutation', $rate, $hash ); |
73
|
|
|
|
|
|
|
return $self; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head2 create( [$operator_probability] ) |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Creates a new mutation operator with an application rate. Rate |
80
|
|
|
|
|
|
|
defaults to 0.5 (which is rather high, you should not rely on it). |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Called C to distinguish from the classwide ctor, new. It just |
83
|
|
|
|
|
|
|
makes simpler to create a Mutation Operator |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=cut |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub create { |
88
|
|
|
|
|
|
|
my $class = shift; |
89
|
|
|
|
|
|
|
my $rate = shift || 0.5; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
my $self = {_mutRate => $rate }; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
bless $self, $class; |
94
|
|
|
|
|
|
|
return $self; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head2 apply( $chromosome ) |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Applies mutation operator to a "Chromosome", a bitstring, really. Can be |
100
|
|
|
|
|
|
|
applied only to I with the C<_str> instance variable; |
101
|
|
|
|
|
|
|
it checks before application that the operand is of type |
102
|
|
|
|
|
|
|
L. |
103
|
|
|
|
|
|
|
It returns the victim. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=cut |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub apply ($;$) { |
108
|
|
|
|
|
|
|
my $self = shift; |
109
|
|
|
|
|
|
|
my $arg = shift || croak "No victim here!"; |
110
|
|
|
|
|
|
|
my $victim = $arg->clone(); |
111
|
|
|
|
|
|
|
croak "Incorrect type ".(ref $victim) if ! $self->check( $victim ); |
112
|
|
|
|
|
|
|
for ( my $i = 0; $i < length( $victim->{_str} ); $i ++ ) { |
113
|
|
|
|
|
|
|
if ( rand() < $self->{_mutRate} ) { |
114
|
|
|
|
|
|
|
my $bit = $victim->Atom($i); |
115
|
|
|
|
|
|
|
$victim->Atom($i, $bit?0:1 ); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
$victim->{'_fitness'} = undef ; |
119
|
|
|
|
|
|
|
return $victim; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 Copyright |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
This file is released under the GPL. See the LICENSE file included in this distribution, |
125
|
|
|
|
|
|
|
or go to http://www.fsf.org/licenses/gpl.txt |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
CVS Info: $Date: 2009/09/13 12:49:04 $ |
128
|
|
|
|
|
|
|
$Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Op/Mutation.pm,v 3.1 2009/09/13 12:49:04 jmerelo Exp $ |
129
|
|
|
|
|
|
|
$Author: jmerelo $ |
130
|
|
|
|
|
|
|
$Revision: 3.1 $ |
131
|
|
|
|
|
|
|
$Name $ |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=cut |
134
|
|
|
|
|
|
|
|