blib/lib/Algorithm/Evolutionary/Op/IncMutation.pm untested

filter: Statementstmt 0.0% 0 / 46 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 74 CC 6 SCAR 30.2
CC
6
Cov
0%
apply
42.0
create
2.0
BEGIN
2.0
CRAP 20.5
1 use strict;
2 use warnings;
3  
4 =head1 NAME
5  
6 Algorithm::Evolutionary::Op::IncMutation - Increments/decrements by one the value of one of the components
7 of the string, takes into account the char class
8  
9 =cut
10  
11 =head1 SYNOPSIS
12  
13 my $op = new Algorithm::Evolutionary::Op::IncMutation; #Create from scratch
14  
15 =head1 Base Class
16  
17 L<Algorithm::Evolutionary::Op::Base|Algorithm::Evolutionary::Op::Base>
18  
19  
20 =head1 DESCRIPTION
21  
22 Mutation operator for a GA; changes a single element in a string by
23 changing it to the next in the sequence deducted from the chromosome
24 itself.
25  
26 =cut
27  
28 package Algorithm::Evolutionary::Op::IncMutation;
29  
30 use lib qw(../../..);
31  
32 our $VERSION = sprintf "%d.%03d", q$Revision: 3.1 $ =~ /(\d+)\.(\d+)/g; # Hack for avoiding version mismatch
33  
34 use Carp;
35 use Clone qw(clone);
36  
37 use base 'Algorithm::Evolutionary::Op::Base';
38  
39 #Class-wide constants
40 our $APPLIESTO = 'Algorithm::Evolutionary::Individual::String';
41 our $ARITY = 1;
42  
43 =head2 create()
44  
45 Creates a new mutation operator.
46  
47 =cut
48  
49 sub create {
50 my $class = shift;
51 my $self = {};
52 bless $self, $class;
53 return $self;
54 }
55  
56 =head2 apply( $indiv )
57  
58 Applies mutation operator to a "Chromosome", a string, really. Can be
59 applied only to I<victims> with the C<_str> instance variable; but
60 it checks before application that both operands are of the required
61 type. The chosen character is changed to the next or previous in
62 the array of chars used for coding the the string
63 my $strChrom = new Algorithm::Evolutionary::Individual::String ['a','c','g','t'] 10;
64 my $xmen = new Algorithm::Evolutionary::Op::IncMutation;
65 $xmen->apply( $strChrom ) # will change 'acgt' into 'aagt' or
66 # 'aggt', for instance
67  
68 Issues an error if there is no C<_chars> array, which is needed for computing the next.
69  
70 =cut
71  
72 sub apply ($;$){
73 my $self = shift;
74 my $arg = shift || croak "No victim here!";
75 my $victim = clone( $arg );
76 croak "Incorrect type ".(ref $victim) if ! $self->check( $victim );
77 my $rnd = int (rand( length( $victim->{_str} ) ));
78 my $char = $victim->Atom( $rnd );
79 #Compute its place in the array
80 my $i = 0;
81 #Compute order in the array
82 croak "Can't do nuthin'; there's no alphabet in the victim" if @{$victim->{_chars}}< 0;
83 while ( ($victim->{_chars}[$i] ne $char )
84 && ($i < @{$victim->{_chars}}) ) { $i++;};
85 #Generate next or previous
86 my $newpos = ( rand() > 0.5)?$i-1:$i+1;
87 $newpos = @{$victim->{_chars}}-1 if !$newpos;
88 $newpos = 0 if $newpos >= @{$victim->{_chars}};
89 substr( $victim->{_str}, $rnd, 1 ) = $victim->{_chars}[$newpos];
90 $victim->Fitness(undef);
91 return $victim;
92 }
93  
94 =head1 Copyright
95
96 This file is released under the GPL. See the LICENSE file included in this distribution,
97 or go to http://www.fsf.org/licenses/gpl.txt
98  
99  
100 =cut
101