« blib/lib/Algorithm/Evolutionary/Fitness/Any.pm
↑ Summary
blib/lib/Algorithm/Evolutionary/Fitness/P_Peaks.pm »
| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | use lib qw(../../..); | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Algorithm::Evolutionary::Op::ArithCrossover - Arithmetic crossover operator; performs the average of the n parents crossed | ||
| 9 | |||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | my $xmlStr6=<<EOC; #Create it from XML | ||
| 14 | <op name='ArithCrossover' type='binary' rate='1' /> | ||
| 15 | EOC | ||
| 16 | my $ref6 = XMLin($xmlStr6); | ||
| 17 | my $op6 = Algorithm::Evolutionary::Op::Base->fromXML( $ref6 ); | ||
| 18 | print $op6->asXML(), "\n"; | ||
| 19 | $op6->apply( $indi4, $indi5 ); | ||
| 20 | print $indi4->asString(), "\n" | ||
| 21 | |||
| 22 | my $op = new Algorithm::Evolutionary::Op::ArithCrossover; #Create from scratch | ||
| 23 | |||
| 24 | =head1 Base Class | ||
| 25 | |||
| 26 | L<Algorithm::Evolutionary::Op::Base|Algorithm::Evolutionary::Op::Base> | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | Crossover operator for a vector-rep individual | ||
| 31 | |||
| 32 | =head1 METHODS | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | package Algorithm::Evolutionary::Op::ArithCrossover; | ||
| 37 | |||
| 38 | our $VERSION = sprintf "%d.%03d", q$Revision: 3.1 $ =~ /(\d+)\.(\d+)/g; # Hack for avoiding version mismatch | ||
| 39 | |||
| 40 | use Carp; | ||
| 41 | use Clone qw(clone); | ||
| 42 | |||
| 43 | use base 'Algorithm::Evolutionary::Op::Base'; | ||
| 44 | |||
| 45 | #Class-wide constants | ||
| 46 | our $APPLIESTO = 'Algorithm::Evolutionary::Individual::Vector'; | ||
| 47 | our $ARITY = 2; | ||
| 48 | |||
| 49 | =head2 create() | ||
| 50 | |||
| 51 | Creates the operator, but is more or less empty. Does not have instance variables | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | sub create { | ||
| 56 | my $class = shift; | ||
| 57 | my $self; | ||
| 58 | bless $self, $class; | ||
| 59 | return $self; | ||
| 60 | } | ||
| 61 | |||
| 62 | =head2 apply( $chromosome_1, $chromosome_2 ) | ||
| 63 | |||
| 64 | Applies xover operator to a "Chromosome", a vector of stuff, | ||
| 65 | really. Can be applied only to I<victims> with the C<_array> instance | ||
| 66 | variable; but it checks before application that both operands are of | ||
| 67 | type L<Algorithm::Evolutionary::Individual::Vector|Algorithm::Evolutionary::Individual::Vector>. | ||
| 68 | Returns the resulting chromosome | ||
| 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 | my $victim2 = shift || croak "No victim here!"; | ||
| 77 | croak "Incorrect type ".(ref $victim) if !$self->check($victim); | ||
| 78 | croak "Incorrect type ".(ref $victim2) if !$self->check($victim2); | ||
| 79 | for ( my $i = 0; $i < scalar @{$victim->{_array}}; $i++ ) { | ||
| 80 | $victim->{_array}[$i] = | ||
| 81 | ( $victim2->{_array}[$i] + $victim->{_array}[$i] )/2; | ||
| 82 | } | ||
| 83 | $victim->Fitness( undef ); #It's been changed, so fitness is invalid | ||
| 84 | return $victim; | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | =head1 Copyright | ||
| 89 | |||
| 90 | This file is released under the GPL. See the LICENSE file included in this distribution, | ||
| 91 | or go to http://www.fsf.org/licenses/gpl.txt | ||
| 92 | |||
| 93 | CVS Info: $Date: 2011/02/14 06:55:36 $ | ||
| 94 | $Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Op/ArithCrossover.pm,v 3.1 2011/02/14 06:55:36 jmerelo Exp $ | ||
| 95 | $Author: jmerelo $ | ||
| 96 | $Revision: 3.1 $ | ||
| 97 | $Name $ | ||
| 98 | |||
| 99 | =cut |