« lib/Algorithm/Evolutionary/Individual/Vector.pm
↑ Summary
blib/lib/Algorithm/Evolutionary/Op/Eval/MO_Rank.pm »
| 1 | use strict; #-*-cperl-*- | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | use lib qw ( ../../../../lib); | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Algorithm::Evolutionary::Op::FullAlgorithm - Skeleton class for a fully-featured evolutionary algorithm | ||
| 9 | |||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | use Algorithm::Evolutionary qw( Op::Base Op::Bitflip Op::Crossover | ||
| 14 | Op::RouletteWheel Op::GeneralGeneration | ||
| 15 | Op::GenerationalTerm Op::FullAlgorithm ); | ||
| 16 | |||
| 17 | # Using the base class as factory | ||
| 18 | my $easyEA = Algorithm::Evolutionary::Op::Base->fromXML( $ref->{$xml} ); | ||
| 19 | $easyEA->apply(\@pop ); | ||
| 20 | |||
| 21 | #Or using the constructor | ||
| 22 | my $m = new Algorithm::Evolutionary::Op::Bitflip; #Changes a single bit | ||
| 23 | my $c = new Algorithm::Evolutionary::Op::Crossover; #Classical 2-point crossover | ||
| 24 | my $replacementRate = 0.3; #Replacement rate | ||
| 25 | my $popSize = 20; | ||
| 26 | my $selector = new Algorithm::Evolutionary::Op::RouletteWheel $popSize; #One of the possible selectors | ||
| 27 | my $onemax = sub { | ||
| 28 | my $indi = shift; | ||
| 29 | my $total = 0; | ||
| 30 | my $len = $indi->length(); | ||
| 31 | my $i = 0; | ||
| 32 | while ($i < $len ) { | ||
| 33 | $total += substr($indi->{'_str'}, $i, 1); | ||
| 34 | $i++; | ||
| 35 | } | ||
| 36 | return $total; | ||
| 37 | }; | ||
| 38 | my $generation = | ||
| 39 | new Algorithm::Evolutionary::Op::GeneralGeneration( $onemax, $selector, [$m, $c], $replacementRate ); | ||
| 40 | my $g100 = new Algorithm::Evolutionary::Op::GenerationalTerm 10; | ||
| 41 | my $f = new Algorithm::Evolutionary::Op::FullAlgorithm $generation, $g100; | ||
| 42 | print $f->asXML(); | ||
| 43 | |||
| 44 | $f->apply( $pop ); # Pop should be defined else where | ||
| 45 | |||
| 46 | =head1 Base Class | ||
| 47 | |||
| 48 | L<Algorithm::Evolutionary::Op::Base> | ||
| 49 | |||
| 50 | =head1 DESCRIPTION | ||
| 51 | |||
| 52 | Class for a configurable evolutionary algoritm. It takes a | ||
| 53 | single-generarion object, and mixes it with a termination condition | ||
| 54 | to create a full algorithm. Includes a sensible default | ||
| 55 | (100-generation generational algorithm) if it is issued only an object | ||
| 56 | of class L<Algorithm::Evolutionary::Op::GeneralGeneration>. | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | package Algorithm::Evolutionary::Op::FullAlgorithm; | ||
| 61 | |||
| 62 | our ($VERSION) = ( '$Revision: 3.0 $ ' =~ / (\d+\.\d+)/ ) ; | ||
| 63 | |||
| 64 | use Carp; | ||
| 65 | |||
| 66 | use base 'Algorithm::Evolutionary::Op::Base'; | ||
| 67 | |||
| 68 | # Class-wide constants | ||
| 69 | our $APPLIESTO = 'ARRAY'; | ||
| 70 | our $ARITY = 1; | ||
| 71 | |||
| 72 | =head2 new( $single_generation[, $termination_test] [, $verboseness] ) | ||
| 73 | |||
| 74 | Takes an already created algorithm and a terminator, and creates an object | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub new { | ||
| 79 | my $class = shift; | ||
| 80 | my $algo = shift|| croak "No single generation algorithm found"; | ||
| 81 | my $term = shift || new Algorithm::Evolutionary::Op::GenerationalTerm 100; | ||
| 82 | my $verbose = shift || 0; | ||
| 83 | my $hash = { algo => $algo, | ||
| 84 | terminator => $term, | ||
| 85 | verbose => $verbose }; | ||
| 86 | my $self = Algorithm::Evolutionary::Op::Base::new( __PACKAGE__, 1, $hash ); | ||
| 87 | return $self; | ||
| 88 | } | ||
| 89 | |||
| 90 | =head2 set( $hashref, $codehash, $opshash ) | ||
| 91 | |||
| 92 | Sets the instance variables. Takes hashes to the different options of | ||
| 93 | the algorithm: parameters, fitness functions and operators | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | sub set { | ||
| 98 | my $self = shift; | ||
| 99 | my $hashref = shift || croak "No params here"; | ||
| 100 | my $codehash = shift; | ||
| 101 | my $opshash = shift; | ||
| 102 | |||
| 103 | $self->SUPER::set( $hashref ); # Base class only aware of options | ||
| 104 | #Now reconstruct operators | ||
| 105 | for my $o ( keys %$opshash ) { #ops are keyed by type | ||
| 106 | $self->{$opshash->{$o}->[1]->{'-id'}} = | ||
| 107 | Algorithm::Evolutionary::Op::Base::fromXML( $o, $opshash->{$o}->[1], $opshash->{$o}->[0] ); | ||
| 108 | } | ||
| 109 | |||
| 110 | } | ||
| 111 | |||
| 112 | =head2 apply( $reference_to_population_array ) | ||
| 113 | |||
| 114 | Applies the algorithm to the population; checks that it receives a | ||
| 115 | ref-to-array as input, croaks if it does not. Returns a sorted, | ||
| 116 | culled, evaluated population for next generation. | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | sub apply ($) { | ||
| 121 | my $self = shift; | ||
| 122 | my $pop = shift || croak "No population here"; | ||
| 123 | croak "Incorrect type ".(ref $pop) if ref( $pop ) ne $APPLIESTO; | ||
| 124 | |||
| 125 | my $term = $self->{_terminator}; | ||
| 126 | my $algo = $self->{_algo}; | ||
| 127 | #Evaluate population, just in case | ||
| 128 | my $eval = $algo->{_eval}; | ||
| 129 | for ( @$pop ) { | ||
| 130 | if ( !defined $_->Fitness() ) { | ||
| 131 | $_->evaluate( $eval ); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | #Run the algorithm | ||
| 135 | do { | ||
| 136 | $algo->apply( $pop ); | ||
| 137 | if ($self->{_verbose}) { | ||
| 138 | print "Best ", $pop->[0]->asString(), "\n" ; | ||
| 139 | print "Median ", $pop->[@$pop/2]->asString(), "\n"; | ||
| 140 | print "Worst ", $pop->[@$pop-1]->asString(), "\n\n"; | ||
| 141 | } | ||
| 142 | } while( $term->apply( $pop ) ); | ||
| 143 | |||
| 144 | } | ||
| 145 | |||
| 146 | =head1 SEE ALSO | ||
| 147 | |||
| 148 | More or less in the same ballpark, alternatives to this one | ||
| 149 | |||
| 150 | =over 4 | ||
| 151 | |||
| 152 | =item * | ||
| 153 | |||
| 154 | L<Algorithm::Evolutionary::Op::CanonicalGA>. | ||
| 155 | |||
| 156 | =item * | ||
| 157 | |||
| 158 | L<Algorithm::Evolutionary::Op::Easy>. | ||
| 159 | |||
| 160 | =back | ||
| 161 | |||
| 162 | Classes you can use within FullAlgorithm: | ||
| 163 | |||
| 164 | =over 4 | ||
| 165 | |||
| 166 | =item * | ||
| 167 | |||
| 168 | L<Algorithm::Evolutionary::Op::Convergence_Terminator> | ||
| 169 | |||
| 170 | =item * | ||
| 171 | |||
| 172 | L<Algorithm::Evolutionary::Op::GenerationalTerm> | ||
| 173 | |||
| 174 | =item * | ||
| 175 | |||
| 176 | L<Algorithm::Evolutionary::Op::Generation_Skeleton> | ||
| 177 | |||
| 178 | =back | ||
| 179 | |||
| 180 | =head1 Copyright | ||
| 181 | |||
| 182 | This file is released under the GPL. See the LICENSE file included in this distribution, | ||
| 183 | or go to http://www.fsf.org/licenses/gpl.txt | ||
| 184 | |||
| 185 | CVS Info: $Date: 2009/07/24 08:46:59 $ | ||
| 186 | $Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Op/FullAlgorithm.pm,v 3.0 2009/07/24 08:46:59 jmerelo Exp $ | ||
| 187 | $Author: jmerelo $ | ||
| 188 | |||
| 189 | =cut | ||
| 190 | |||
| 191 | "Who wants to rock the party"; |