« blib/lib/Algorithm/Evolutionary/Op/Storing.pm
↑ Summary
blib/lib/Algorithm/Evolutionary/Op/Easy_MO.pm »
| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | Algorithm::Evolutionary::Op::Eval::General - General and simple population evaluator | ||
| 7 | |||
| 8 | =head1 SYNOPSIS | ||
| 9 | |||
| 10 | =head1 Base Class | ||
| 11 | |||
| 12 | L<Algorithm::Evolutionary::Op::Base> | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | A general evaluator: applies an evaluation function to each member of | ||
| 17 | the population | ||
| 18 | |||
| 19 | =head1 METHODS | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | package Algorithm::Evolutionary::Op::Eval::General; | ||
| 24 | |||
| 25 | use lib qw(../../..); | ||
| 26 | |||
| 27 | our $VERSION = sprintf "%d.%03d", q$Revision: 3.0 $ =~ /(\d+)\.(\d+)/g; | ||
| 28 | |||
| 29 | use Carp; | ||
| 30 | |||
| 31 | use base 'Algorithm::Evolutionary::Op::Base'; | ||
| 32 | |||
| 33 | # Class-wide constants | ||
| 34 | our $APPLIESTO = 'ARRAY'; | ||
| 35 | our $ARITY = 1; | ||
| 36 | |||
| 37 | =head2 new( $evaluation_function ) | ||
| 38 | |||
| 39 | Creates an algorithm, with no defaults except for the default | ||
| 40 | replacement operator (defaults to L<Algorithm::Evolutionary::Op::ReplaceWorst>) | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | sub new { | ||
| 45 | my $class = shift; | ||
| 46 | my $self = {}; | ||
| 47 | $self->{_eval} = shift || croak "No eval function found"; | ||
| 48 | bless $self, $class; | ||
| 49 | return $self; | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | =head2 set( $ref_to_params_hash, $ref_to_code_hash, $ref_to_operators_hash ) | ||
| 54 | |||
| 55 | Sets the instance variables. Takes a ref-to-hash as | ||
| 56 | input. Not intended to be used from outside the class | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub set { | ||
| 61 | my $self = shift; | ||
| 62 | my $hashref = shift || croak "No params here"; | ||
| 63 | my $codehash = shift || croak "No code here"; | ||
| 64 | my $opshash = shift || croak "No ops here"; | ||
| 65 | |||
| 66 | for ( keys %$codehash ) { | ||
| 67 | $self->{"_$_"} = eval "sub { $codehash->{$_} } "; | ||
| 68 | } | ||
| 69 | |||
| 70 | $self->{_ops} =(); | ||
| 71 | for ( keys %$opshash ) { | ||
| 72 | push @{$self->{_ops}}, | ||
| 73 | Algorithm::Evolutionary::Op::Base::fromXML( $_, $opshash->{$_}->[1], $opshash->{$_}->[0] ) ; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | =head2 apply( $population ) | ||
| 78 | |||
| 79 | Evaluates the population, setting its fitness value | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | sub apply ($) { | ||
| 84 | my $self = shift; | ||
| 85 | my $pop = shift || croak "No population here"; | ||
| 86 | croak "Incorrect type ".(ref $pop) if ref( $pop ) ne $APPLIESTO; | ||
| 87 | |||
| 88 | #Evaluate only the new ones | ||
| 89 | my $eval = $self->{_eval}; | ||
| 90 | #Eliminate and substitute | ||
| 91 | map( defined $_->Fitness() || $_->evaluate( $eval), @$pop ); | ||
| 92 | |||
| 93 | } | ||
| 94 | |||
| 95 | =head1 SEE ALSO | ||
| 96 | |||
| 97 | =over 4 | ||
| 98 | |||
| 99 | =item * | ||
| 100 | |||
| 101 | L<Algorithm::Evolutionary::Fitness::Base> | ||
| 102 | |||
| 103 | =back | ||
| 104 | |||
| 105 | =head1 Copyright | ||
| 106 | |||
| 107 | This file is released under the GPL. See the LICENSE file included in this distribution, | ||
| 108 | or go to http://www.fsf.org/licenses/gpl.txt | ||
| 109 | |||
| 110 | CVS Info: $Date: 2009/07/24 08:46:59 $ | ||
| 111 | $Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Op/Eval/General.pm,v 3.0 2009/07/24 08:46:59 jmerelo Exp $ | ||
| 112 | $Author: jmerelo $ | ||
| 113 | $Revision: 3.0 $ | ||
| 114 | |||
| 115 | =cut | ||
| 116 | |||
| 117 | "The truth is out there"; |