File Coverage

lib/Algorithm/Evolutionary/Op/GaussianMutation.pm
Criterion Covered Total %
statement 21 21 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 28 28 100.0


line stmt bran cond sub pod time code
1 1     1   31994 use strict;
  1         2  
  1         55  
2 1     1   5 use warnings;
  1         2  
  1         32  
3              
4 1     1   5 use lib qw(../../..);
  1         9  
  1         8  
5              
6             =head1 NAME
7              
8             Algorithm::Evolutionary::Op::GaussianMutation - Changes numeric chromosome components following the gaussian distribution
9              
10             =cut
11              
12             =head1 SYNOPSIS
13              
14             my $xmlStr4=<
15            
16            
17            
18            
19             EOC
20             my $ref4 = XMLin($xmlStr4);
21             my $op4 = Algorithm::Evolutionary::Op::Base->fromXML( $ref4 );
22             print $op4->asXML(), "\n";
23              
24             my $op = new Algorithm::Evolutionary::Op::GaussianMutation( 0, 0.05) # With average 0, and 0.05 standard deviation
25              
26             =cut
27              
28             =head1 Base Class
29              
30             L
31              
32             =cut
33              
34             =head1 DESCRIPTION
35              
36             Mutation operator for a GA: applies gaussian mutation to a number
37              
38             =cut
39              
40             package Algorithm::Evolutionary::Op::GaussianMutation;
41              
42             our $VERSION = sprintf "%d.1%02d", q$Revision: 3.3 $ =~ /(\d+)\.(\d+)/g; # Hack for avoiding version mismatch
43              
44 1     1   216 use Carp;
  1         3  
  1         103  
45 1     1   1050 use Math::Random;
  1         8321  
  1         123  
46 1     1   725 use Clone qw(clone);
  1         665  
  1         71  
47              
48 1     1   7 use base 'Algorithm::Evolutionary::Op::Base';
  1         2  
  1         611  
49              
50             #Class-wide constants
51             our $APPLIESTO = 'Algorithm::Evolutionary::Individual::Vector';
52             our $ARITY = 1;
53              
54             =head2 new( [$average = 0] [, $standard deviation = 1] [, $rate = 1 ]
55              
56             Creates a new mutation operator with an application rate. Rate defaults to 1.
57              
58             =cut
59              
60             sub new {
61             my $class = shift;
62             my $avg = shift || 0;
63             my $stddev = shift || 1;
64             my $rate = shift || 1;
65              
66             my $hash = {avg => $avg,
67             stddev => $stddev };
68             my $self = Algorithm::Evolutionary::Op::Base::new( 'Algorithm::Evolutionary::Op::GaussianMutation', $rate, $hash );
69             return $self;
70             }
71              
72             =head2 create
73              
74             Creates a new mutation operator with an application rate. Rate defaults to 0.1.
75              
76             Called create to distinguish from the classwide ctor, new. It just
77             makes simpler to create a Mutation Operator
78              
79             =cut
80              
81             sub create {
82             my $class = shift;
83             my $avg = shift || 0;
84             my $stddev = shift || 1;
85            
86             my $self = {_avg => $avg,
87             _stddev => $stddev };
88              
89             bless $self, $class;
90             return $self;
91             }
92              
93             =head2 apply( $chromosome )
94              
95             Applies mutation operator to a "Chromosome", a vector of stuff,
96             really. Can be applied only to I with the C<_array> instance
97             variable; but it checks before application (roughly) that both operands are of
98             type L.
99              
100             =cut
101              
102             sub apply ($$) {
103             my $self = shift;
104             my $arg = shift || croak "No victim here!";
105             croak "Incorrect type".(ref $arg) if !$arg->{_array};
106             my $victim = clone($arg);
107             my @deltas = random_normal( @{$victim->{_array}} + 1, #+1 is needed, returns empty if not
108             $self->{_avg}, $self->{_stddev} );
109             for ( @{$victim->{_array}} ) {
110             my $adjust = pop @deltas;
111             $_ += $adjust;
112             # makes sure that the new value stays within its confines
113             if($_ < $victim->{_rangestart}) {
114             $_ = $victim->{_rangestart};
115             } elsif($_ > $victim->{_rangeend}) {
116             $_ = $victim->{_rangeend};
117             }
118             }
119             $victim->{_fitness} = undef;
120             return $victim;
121             }
122              
123             =head1 THANKS
124              
125             This file has been improved with input from Christoph Meißner.
126              
127             =head1 Copyright
128            
129             This file is released under the GPL. See the LICENSE file included in this distribution,
130             or go to http://www.fsf.org/licenses/gpl.txt
131              
132             CVS Info: $Date: 2011/11/23 11:10:10 $
133             $Header: /cvsroot/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Op/GaussianMutation.pm,v 3.3 2011/11/23 11:10:10 jmerelo Exp $
134             $Author: jmerelo $
135             $Revision: 3.3 $
136             $Name $
137              
138             =cut