File Coverage

lib/Algorithm/Evolutionary/Op/String_Mutation.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 20 20 100.0


line stmt bran cond sub pod time code
1 2     2   32170 use strict; #-*-cperl-*-
  2         6  
  2         95  
2 2     2   13 use warnings;
  2         4  
  2         85  
3              
4 2     2   12 use lib qw( ../../lib ../../../lib ../../../../lib);
  2         4  
  2         19  
5              
6             =head1 NAME
7              
8             Algorithm::Evolutionary::Op::String_Mutation - Single character string mutation
9              
10             =head1 SYNOPSIS
11              
12             #Create from scratch with priority = 2
13             my $op = new Algorithm::Evolutionary::Op::String_Mutation 2;
14              
15             =head1 Base Class
16              
17             L
18              
19             =head1 DESCRIPTION
20              
21             Mutation operator for a GA; changes a single bit in the string;
22             does not need a rate
23              
24             =head1 METHODS
25              
26             =cut
27              
28             package Algorithm::Evolutionary::Op::String_Mutation;
29              
30             our $VERSION = sprintf "%d.%03d", q$Revision: 3.5 $ =~ /(\d+)\.(\d+)/g;
31              
32 2     2   1073 use Carp;
  2         4  
  2         282  
33              
34 2     2   12 use base 'Algorithm::Evolutionary::Op::Base';
  2         3  
  2         1430  
35              
36             #Class-wide constants
37             our $ARITY = 1;
38              
39             =head2 new( [$how_many] [,$priority] )
40              
41             Creates a new mutation operator with a bitflip application rate, which defaults to 0.5,
42             and an operator application rate (general for all ops), which defaults to 1.
43              
44             =cut
45              
46             sub new {
47             my $class = shift;
48             my $howMany = shift || 1;
49             my $rate = shift || 1;
50              
51             my $hash = { howMany => $howMany || 1};
52             my $self = Algorithm::Evolutionary::Op::Base::new( 'Algorithm::Evolutionary::Op::String_Mutation',
53             $rate, $hash );
54             return $self;
55             }
56              
57             =head2 create()
58              
59             Creates a new mutation operator.
60              
61             =cut
62              
63             sub create {
64             my $class = shift;
65             my $self = {};
66             bless $self, $class;
67             return $self;
68             }
69              
70             =head2 apply( $chromosome )
71              
72             Applies mutation operator to a "Chromosome", a string, really.
73              
74             =cut
75              
76             sub apply ($;$){
77             my $self = shift;
78             my $arg = shift || croak "No victim here!";
79             my $victim = $arg->clone();
80             my $size = length($victim->{'_str'});
81              
82             croak "Too many changes" if $self->{'_howMany'} >= $size;
83             my @char_array = 0..($size-1); # Avoids double mutation in a single place
84             for ( my $i = 0; $i < $self->{'_howMany'}; $i++ ) {
85             my $rnd = int (rand( @char_array ));
86             my $who = splice(@char_array, $rnd, 1 );
87             my $what = $victim->Atom( $who );
88             my @these_chars = @{ $victim->{'_chars'}};
89             for ( my $c = 0; $c < @{ $victim->{'_chars'}}; $c++ ) { #Exclude this character
90             if ( $victim->{'_chars'}[$c] eq $what ) {
91             splice( @these_chars, $c, 1 );
92             last;
93             }
94             }
95             $victim->Atom( $who, $these_chars[rand(@these_chars)] );
96             }
97             $victim->{'_fitness'} = undef ;
98             return $victim;
99             }
100              
101             =head1 Copyright
102            
103             This file is released under the GPL. See the LICENSE file included in this distribution,
104             or go to http://www.fsf.org/licenses/gpl.txt
105              
106             CVS Info: $Date: 2011/02/14 06:55:36 $
107             $Header: /cvsroot/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Op/String_Mutation.pm,v 3.5 2011/02/14 06:55:36 jmerelo Exp $
108             $Author: jmerelo $
109             $Revision: 3.5 $
110             $Name $
111              
112             =cut
113