« blib/lib/Algorithm/Evolutionary/Op/Canonical_GA_NN.pm
↑ Summary
blib/lib/Algorithm/Evolutionary/Fitness/wP_Peaks.pm »
| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | Algorithm::Evolutionary::Op::TreeMutation - GP-like mutation operator for trees | ||
| 7 | |||
| 8 | =head1 SYNOPSIS | ||
| 9 | |||
| 10 | my $op = new Algorithm::Evolutionary::Op::TreeMutation (0.5 ); #Create from scratch | ||
| 11 | |||
| 12 | =head1 Base Class | ||
| 13 | |||
| 14 | L<Algorithm::Evolutionary::Op::Base|Algorithm::Evolutionary::Op::Base> | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | Mutation operator for a genetic programming, mutates tree nodes with | ||
| 19 | a certain probability | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | package Algorithm::Evolutionary::Op::TreeMutation; | ||
| 24 | |||
| 25 | our ($VERSION) = ( '$Revision: 3.1 $ ' =~ /(\d+\.\d+)/ ); | ||
| 26 | |||
| 27 | use Carp; | ||
| 28 | |||
| 29 | use Algorithm::Evolutionary::Op::Base; | ||
| 30 | our @ISA = qw (Algorithm::Evolutionary::Op::Base); | ||
| 31 | |||
| 32 | #Class-wide constants | ||
| 33 | our $APPLIESTO = 'Algorithm::Evolutionary::Individual::Tree'; | ||
| 34 | our $ARITY = 1; | ||
| 35 | |||
| 36 | =head1 METHODS | ||
| 37 | |||
| 38 | =head2 new | ||
| 39 | |||
| 40 | Creates a new mutation operator with an application rate. Rate defaults to 0.1. | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | sub new { | ||
| 45 | my $class = shift; | ||
| 46 | my $mutRate = shift || 0.5; | ||
| 47 | my $rate = shift || 1; | ||
| 48 | |||
| 49 | my $hash = { mutRate => $mutRate }; | ||
| 50 | my $self = Algorithm::Evolutionary::Op::Base::new( 'Algorithm::Evolutionary::Op::TreeMutation', $rate, $hash ); | ||
| 51 | return $self; | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | =head2 create | ||
| 56 | |||
| 57 | Creates a new mutation operator with an application rate. Rate defaults to 0.5. | ||
| 58 | |||
| 59 | Called create to distinguish from the classwide ctor, new. It just | ||
| 60 | makes simpler to create a Mutation Operator | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | sub create { | ||
| 65 | my $class = shift; | ||
| 66 | my $rate = shift || 0.5; | ||
| 67 | |||
| 68 | my $self = {_mutRate => $rate }; | ||
| 69 | |||
| 70 | bless $self, $class; | ||
| 71 | return $self; | ||
| 72 | } | ||
| 73 | |||
| 74 | =head2 apply | ||
| 75 | |||
| 76 | Applies mutation operator to a "Chromosome", but | ||
| 77 | it checks before application that both operands are of type | ||
| 78 | L<Algorithm::Evolutionary::Individual::Tree|Algorithm::Evolutionary::Individual::Tree>. | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | sub apply ($;$) { | ||
| 83 | my $self = shift; | ||
| 84 | my $arg = shift || croak "No victim here!"; | ||
| 85 | my $victim = $arg->clone(); | ||
| 86 | croak "Incorrect type ".(ref $victim) if ! $self->check( $victim ); | ||
| 87 | my $node = $victim->{_tree}; | ||
| 88 | #Build the list of primitives | ||
| 89 | my %primitives = %{$victim->{_primitives}}; | ||
| 90 | my @arities; | ||
| 91 | for ( keys %primitives ) { | ||
| 92 | push @{$arities[ $primitives{$_}[0] ]}, $_; | ||
| 93 | } | ||
| 94 | $node->walk_down( { callback => \&mutate, | ||
| 95 | mutrate => $self->{_mutRate}, | ||
| 96 | arities => \@arities, | ||
| 97 | primitives => $victim->{_primitives} }); | ||
| 98 | |||
| 99 | return $victim; | ||
| 100 | } | ||
| 101 | |||
| 102 | =head2 mutate | ||
| 103 | |||
| 104 | Callback routine called from apply; decides on mutation application, and | ||
| 105 | applies it. If appliable, substitutes a node by other with the same arity. | ||
| 106 | Builds a lists of nodes before, to speed up operation | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | sub mutate { | ||
| 111 | my $node = shift; | ||
| 112 | my $hashref = shift; | ||
| 113 | |||
| 114 | my $mutrate = $hashref->{mutrate}; | ||
| 115 | my @arities = @{$hashref->{arities}}; | ||
| 116 | my %primitives = %{$hashref->{primitives}}; | ||
| 117 | if ( rand > $mutrate ) { #Mutate | ||
| 118 | my $primitive = $node->name(); | ||
| 119 | my $arity = $primitives{$primitive}[0]; | ||
| 120 | my $newName; | ||
| 121 | do { | ||
| 122 | $newName = $arities[$arity][ rand( @{$arities[$arity]} )]; | ||
| 123 | } until ($newName ne $primitive); | ||
| 124 | $node->name( $newName ); | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | =head1 Copyright | ||
| 129 | |||
| 130 | This file is released under the GPL. See the LICENSE file included in this distribution, | ||
| 131 | or go to http://www.fsf.org/licenses/gpl.txt | ||
| 132 | |||
| 133 | CVS Info: $Date: 2009/07/28 11:30:56 $ | ||
| 134 | $Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Op/TreeMutation.pm,v 3.1 2009/07/28 11:30:56 jmerelo Exp $ | ||
| 135 | $Author: jmerelo $ | ||
| 136 | $Revision: 3.1 $ | ||
| 137 | |||
| 138 | =cut | ||
| 139 |