« blib/lib/Algorithm/Evolutionary/Op/Eval/General.pm
↑ Summary
blib/lib/Algorithm/Evolutionary/Op/GeneralGeneration.pm »
| 1 | use strict; #-*-cperl-*- | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | use lib qw( ../../.. ); | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Algorithm::Evolutionary::Op::Easy_MO - Multiobjecttive evolutionary algorithm, single generation, with | ||
| 9 | variable operators | ||
| 10 | |||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | #Mutation and crossover. Default selection rate is 0.4 | ||
| 15 | my $algo = new Algorithm::Evolutionary::Op::Easy_MO( $eval ); | ||
| 16 | |||
| 17 | #Define an easy single-generation algorithm with predefined mutation and crossover | ||
| 18 | my $m = new Algorithm::Evolutionary::Op::Bitflip; #Changes a single bit | ||
| 19 | my $c = new Algorithm::Evolutionary::Op::Crossover; #Classical 2-point crossover | ||
| 20 | my $generation = new Algorithm::Evolutionary::Op::Easy_MO( $rr, 0.2, [$m, $c] ); | ||
| 21 | |||
| 22 | =head1 Base Class | ||
| 23 | |||
| 24 | L<Algorithm::Evolutionary::Op::Base> | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | "Easy" to use, single generation of an evolutionary algorithm. Takes | ||
| 31 | an arrayref of operators as input, or defines bitflip-mutation and | ||
| 32 | 2-point crossover as default. The C<apply> method applies a single | ||
| 33 | iteration of the algorithm to the population it takes as input | ||
| 34 | |||
| 35 | =head1 METHODS | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | package Algorithm::Evolutionary::Op::Easy_MO; | ||
| 40 | |||
| 41 | our ($VERSION) = ( '$Revision: 3.6 $ ' =~ / (\d+\.\d+)/ ) ; | ||
| 42 | |||
| 43 | use Carp; | ||
| 44 | |||
| 45 | use Algorithm::Evolutionary qw( Wheel Op::Bitflip | ||
| 46 | Op::Crossover | ||
| 47 | Op::Eval::MO_Rank ); | ||
| 48 | |||
| 49 | use base 'Algorithm::Evolutionary::Op::Base'; | ||
| 50 | |||
| 51 | # Class-wide constants | ||
| 52 | our $APPLIESTO = 'ARRAY'; | ||
| 53 | |||
| 54 | =head2 new( $eval_func, [$selection_rate,] [$operators_arrayref] ) | ||
| 55 | |||
| 56 | Creates an algorithm that optimizes the handled fitness function and | ||
| 57 | reference to an array of operators. If this reference is null, an | ||
| 58 | array consisting of bitflip mutation and 2 point crossover is | ||
| 59 | generated. Which, of course, might not what you need in case you don't | ||
| 60 | have a binary chromosome. Take into account that in this case the | ||
| 61 | fitness function should return a reference to array. | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | sub new { | ||
| 66 | my $class = shift; | ||
| 67 | my $self = {}; | ||
| 68 | $self->{_eval} = shift || croak "No eval function found"; | ||
| 69 | $self->{_rank} = new Algorithm::Evolutionary::Op::Eval::MO_Rank $self->{'_eval'}; | ||
| 70 | $self->{_selrate} = shift || 0.4; | ||
| 71 | if ( @_ ) { | ||
| 72 | $self->{_ops} = shift; | ||
| 73 | } else { | ||
| 74 | #Create mutation and crossover | ||
| 75 | my $mutation = new Algorithm::Evolutionary::Op::Bitflip; | ||
| 76 | push( @{$self->{_ops}}, $mutation ); | ||
| 77 | my $xover = new Algorithm::Evolutionary::Op::Crossover; | ||
| 78 | push( @{$self->{_ops}}, $xover ); | ||
| 79 | } | ||
| 80 | bless $self, $class; | ||
| 81 | return $self; | ||
| 82 | |||
| 83 | } | ||
| 84 | |||
| 85 | =head2 set( $hashref, codehash, opshash ) | ||
| 86 | |||
| 87 | Sets the instance variables. Takes a ref-to-hash (for options), codehash (for fitness) and opshash (for operators) | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | sub set { | ||
| 92 | my $self = shift; | ||
| 93 | my $hashref = shift || croak "No params here"; | ||
| 94 | my $codehash = shift || croak "No code here"; | ||
| 95 | my $opshash = shift || croak "No ops here"; | ||
| 96 | $self->{_selrate} = $hashref->{selrate}; | ||
| 97 | |||
| 98 | for ( keys %$codehash ) { | ||
| 99 | $self->{"_$_"} = eval "sub { $codehash->{$_} } " || carp "Error compiling fitness function: $! => $@"; | ||
| 100 | } | ||
| 101 | |||
| 102 | $self->{_ops} =(); | ||
| 103 | for ( keys %$opshash ) { | ||
| 104 | #First element of the array contains the content, second the rate. | ||
| 105 | push @{$self->{_ops}}, | ||
| 106 | Algorithm::Evolutionary::Op::Base::fromXML( $_, $opshash->{$_}->[1], $opshash->{$_}->[0] ); | ||
| 107 | } | ||
| 108 | |||
| 109 | } | ||
| 110 | |||
| 111 | =head2 apply( $population ) | ||
| 112 | |||
| 113 | Applies the algorithm to the population; checks that it receives a | ||
| 114 | ref-to-array as input, croaks if it does not. Returns a sorted, | ||
| 115 | culled, evaluated population for next generation. | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | sub apply ($) { | ||
| 120 | my $self = shift; | ||
| 121 | my $pop = shift || croak "No population here"; | ||
| 122 | |||
| 123 | #Evaluate | ||
| 124 | my $eval = $self->{_eval}; | ||
| 125 | my @ops = @{$self->{_ops}}; | ||
| 126 | $self->{'_rank'}->apply( $pop ); | ||
| 127 | |||
| 128 | #Sort | ||
| 129 | my @popsort = sort { $b->{_fitness} <=> $a->{_fitness}; } @$pop; | ||
| 130 | |||
| 131 | #Cull | ||
| 132 | my $pringaos = int(($#popsort+1)*$self->{_selrate}); #+1 gives you size | ||
| 133 | # print "Pringaos $pringaos\n"; | ||
| 134 | splice @popsort, - $pringaos; | ||
| 135 | # print "Población ", scalar @popsort, "\n"; | ||
| 136 | |||
| 137 | #Reproduce | ||
| 138 | my @rates = map( $_->{'rate'}, @ops ); | ||
| 139 | my $opWheel = new Algorithm::Evolutionary::Wheel @rates; | ||
| 140 | |||
| 141 | #Generate offpring; | ||
| 142 | my $originalSize = $#popsort; # Just for random choice | ||
| 143 | for ( my $i = 0; $i < $pringaos; $i ++ ) { | ||
| 144 | my @offspring; | ||
| 145 | my $selectedOp = $ops[ $opWheel->spin()]; | ||
| 146 | for ( my $j = 0; $j < $selectedOp->arity(); $j ++ ) { | ||
| 147 | my $chosen = $popsort[ int ( rand( $originalSize ) )]; | ||
| 148 | push( @offspring, $chosen ); #No need to clone, it's not changed in ops | ||
| 149 | } | ||
| 150 | my $mutante = $selectedOp->apply( @offspring ); | ||
| 151 | push( @popsort, $mutante ); | ||
| 152 | } | ||
| 153 | |||
| 154 | #Return | ||
| 155 | for ( my $i = 0; $i <= $#popsort; $i++ ) { | ||
| 156 | # print $i, "->", $popsort[$i]->asString, "\n"; | ||
| 157 | $pop->[$i] = $popsort[$i]; | ||
| 158 | } | ||
| 159 | |||
| 160 | |||
| 161 | } | ||
| 162 | |||
| 163 | =head1 SEE ALSO | ||
| 164 | |||
| 165 | L<Algorithm::Evolutionary::Op::CanonicalGA>. | ||
| 166 | L<Algorithm::Evolutionary::Op::FullAlgorithm>. | ||
| 167 | L<Algorithm::Evolutionary::Op::Easy> for the scalar version of this code. | ||
| 168 | |||
| 169 | =head1 Copyright | ||
| 170 | |||
| 171 | This file is released under the GPL. See the LICENSE file included in this distribution, | ||
| 172 | or go to http://www.fsf.org/licenses/gpl.txt | ||
| 173 | |||
| 174 | CVS Info: $Date: 2011/02/14 06:55:36 $ | ||
| 175 | $Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Op/Easy_MO.pm,v 3.6 2011/02/14 06:55:36 jmerelo Exp $ | ||
| 176 | $Author: jmerelo $ | ||
| 177 | $Revision: 3.6 $ | ||
| 178 | $Name $ | ||
| 179 | |||
| 180 | =cut | ||
| 181 | |||
| 182 | "The truth is out there"; |