« lib/Algorithm/Evolutionary/Op/GenerationalTerm.pm
↑ Summary
lib/Algorithm/Evolutionary/Op/NoChangeTerm.pm »
| 1 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 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 | |||
| 13 | #Create from scratch | ||
| 14 | my $op = new Algorithm::Evolutionary::Op::Mutation (0.5 ); | ||
| 15 | |||
| 16 | #All options | ||
| 17 | my $priority = 1; | ||
| 18 | my $mutation = new Algorithm::Evolutionary::Op::Mutation 1/$length, $priority; | ||
| 19 | |||
| 20 | =head1 Base Class | ||
| 21 | |||
| 22 | L<Algorithm::Evolutionary::Op::Base|Algorithm::Evolutionary::Op::Base> | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | Mutation operator for a GA | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | package Algorithm::Evolutionary::Op::Mutation; | ||
| 31 | |||
| 32 | our ($VERSION) = ( '$Revision: 3.2 $ ' =~ /(\d+\.\d+)/ ); | ||
| 33 | |||
| 34 | 4 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 35 | |||
| 36 | 4 | ▶ | use base 'Algorithm::Evolutionary::Op::Base'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 37 | |||
| 38 | #Class-wide constants | ||
| 39 | our $APPLIESTO = 'Algorithm::Evolutionary::Individual::BitString'; | ||
| 40 | our $ARITY = 1; | ||
| 41 | |||
| 42 | =head1 METHODS | ||
| 43 | |||
| 44 | =head2 new( [$mutation_rate] [, $operator_probability] ) | ||
| 45 | |||
| 46 | Creates a new mutation operator with a bitflip application rate, which | ||
| 47 | defaults to 0.5, and an operator application rate (general for all | ||
| 48 | ops), which defaults to 1. Application rate will be converted in | ||
| 49 | runtime to application probability, which will eventually depend on | ||
| 50 | the rates of all the other operators. For instance, if this operator's | ||
| 51 | rate is one and there's another with rate=4, probability will be 20% | ||
| 52 | for this one and 80% for the other; 1 in 5 new individuals will be | ||
| 53 | generated using this and the rest using the other one. | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | sub new { | ||
| 58 | my $class = shift; | ||
| 59 | my $mutRate = shift || 0.5; | ||
| 60 | my $rate = shift || 1; | ||
| 61 | |||
| 62 | my $hash = { mutRate => $mutRate }; | ||
| 63 | my $self = Algorithm::Evolutionary::Op::Base::new( 'Algorithm::Evolutionary::Op::Mutation', $rate, $hash ); | ||
| 64 | return $self; | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | =head2 create( [$operator_probability] ) | ||
| 69 | |||
| 70 | Creates a new mutation operator with an application rate. Rate | ||
| 71 | defaults to 0.5 (which is rather high, you should not rely on it). | ||
| 72 | |||
| 73 | Called C<create> to distinguish from the classwide ctor, new. It just | ||
| 74 | makes simpler to create a Mutation Operator | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub create { | ||
| 79 | my $class = shift; | ||
| 80 | my $rate = shift || 0.5; | ||
| 81 | |||
| 82 | my $self = {_mutRate => $rate }; | ||
| 83 | |||
| 84 | bless $self, $class; | ||
| 85 | return $self; | ||
| 86 | } | ||
| 87 | |||
| 88 | =head2 apply( $chromosome ) | ||
| 89 | |||
| 90 | Applies mutation operator to a "Chromosome", a bitstring, really. Can be | ||
| 91 | applied only to I<victims> with the C<_str> instance variable; | ||
| 92 | it checks before application that the operand is of type | ||
| 93 | L<Algorithm::Evolutionary::Individual::BitString|Algorithm::Evolutionary::Individual::BitString>. | ||
| 94 | It returns the victim. | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | sub apply ($;$) { | ||
| 99 | my $self = shift; | ||
| 100 | my $arg = shift || croak "No victim here!"; | ||
| 101 | my $victim = $arg->clone(); | ||
| 102 | croak "Incorrect type ".(ref $victim) if ! $self->check( $victim ); | ||
| 103 | for ( my $i = 0; $i < length( $victim->{_str} ); $i ++ ) { | ||
| 104 | if ( rand() < $self->{_mutRate} ) { | ||
| 105 | my $bit = $victim->Atom($i); | ||
| 106 | $victim->Atom($i, $bit?0:1 ); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | $victim->{'_fitness'} = undef ; | ||
| 110 | return $victim; | ||
| 111 | } | ||
| 112 | |||
| 113 | =head1 Copyright | ||
| 114 | |||
| 115 | This file is released under the GPL. See the LICENSE file included in this distribution, | ||
| 116 | or go to http://www.fsf.org/licenses/gpl.txt | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||