File Coverage

lib/Algorithm/Evolutionary/Op/Bitflip.pm
Criterion Covered Total %
statement 18 18 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 24 24 100.0


line stmt bran cond sub pod time code
1 3     3   72274 use strict; #-*-cperl-*-
  3         7  
  3         127  
2 3     3   15 use warnings;
  3         7  
  3         116  
3              
4 3     3   19 use lib qw( ../../lib ../../../lib ../../../../lib);
  3         5  
  3         22  
5              
6             =head1 NAME
7              
8             Algorithm::Evolutionary::Op::Bitflip - Bit-flip mutation
9              
10             =head1 SYNOPSIS
11              
12             my $xmlStr2=<
13            
14            
15            
16             EOC
17             my $ref2 = XMLin($xmlStr2);
18              
19             my $op2 = Algorithm::Evolutionary::Op::Base->fromXML( $ref2 );
20             print $op2->asXML(), "\n*Arity ", $op->arity(), "\n";
21              
22             my $op = new Algorithm::Evolutionary::Op::Bitflip 2; #Create from scratch with default rate
23              
24             =head1 Base Class
25              
26             L
27              
28             =head1 DESCRIPTION
29              
30             Mutation operator for a GA; changes a single bit in the bitstring;
31             does not need a rate
32              
33             =head1 METHODS
34              
35             =cut
36              
37             package Algorithm::Evolutionary::Op::Bitflip;
38              
39             our ($VERSION) = ( '$Revision: 3.3 $ ' =~ /(\d+\.\d+)/ );
40              
41 3     3   2359 use Carp;
  3         6  
  3         339  
42 3     3   3209 use Clone qw(clone);
  3         12488  
  3         332  
43              
44 3     3   30 use base 'Algorithm::Evolutionary::Op::Base';
  3         6  
  3         2497  
45              
46             #Class-wide constants
47             our $ARITY = 1;
48              
49             =head2 new( [$how_many] [,$priority] )
50              
51             Creates a new mutation operator with a bitflip application rate, which defaults to 0.5,
52             and an operator application rate (general for all ops), which defaults to 1.
53              
54             =cut
55              
56             sub new {
57             my $class = shift;
58             my $howMany = shift || 1;
59             my $rate = shift || 1;
60              
61             my $hash = { howMany => $howMany || 1};
62             my $self = Algorithm::Evolutionary::Op::Base::new( 'Algorithm::Evolutionary::Op::Bitflip', $rate, $hash );
63             return $self;
64             }
65              
66             =head2 create()
67              
68             Creates a new mutation operator.
69              
70             =cut
71              
72             sub create {
73             my $class = shift;
74             my $self = {};
75             bless $self, $class;
76             return $self;
77             }
78              
79             =head2 apply( $chromosome )
80              
81             Applies mutation operator to a "Chromosome", a bitstring, really. Can be
82             applied only to I composed of [0,1] atoms, independently of representation; but
83             it checks before application that the operand is of type
84             L.
85              
86             =cut
87              
88             sub apply ($;$){
89             my $self = shift;
90             my $arg = shift || croak "No victim here!";
91             # my $victim = $arg->clone();
92             my $victim;
93             if ( (ref $arg ) =~ /BitString/ ) {
94             $victim = clone( $arg );
95             } else {
96             $victim = $arg->clone();
97             }
98             my $size = $victim->size();
99             # croak "Incorrect type ".(ref $victim) if ! $self->check( $victim );
100             croak "Too many changes" if $self->{_howMany} >= $size;
101             my @bits = 0..($size-1); # Hash with all bits
102             for ( my $i = 0; $i < $self->{_howMany}; $i++ ) {
103             my $rnd = int (rand( @bits ));
104             my $who = splice(@bits, $rnd, 1 );
105             $victim->Atom( $who, $victim->Atom( $who )?0:1 );
106             }
107             $victim->{'_fitness'} = undef ;
108             return $victim;
109             }
110              
111             =head1 Copyright
112            
113             This file is released under the GPL. See the LICENSE file included in this distribution,
114             or go to http://www.fsf.org/licenses/gpl.txt
115              
116             CVS Info: $Date: 2011/02/13 17:45:53 $
117             $Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Op/Bitflip.pm,v 3.3 2011/02/13 17:45:53 jmerelo Exp $
118             $Author: jmerelo $
119             $Revision: 3.3 $
120             $Name $
121              
122             =cut
123