« blib/lib/Algorithm/Evolutionary/Op/FullAlgorithm.pm
↑ Summary
blib/lib/Algorithm/Evolutionary/Op/ChangeLengthMutation.pm »
| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | Algorithm::Evolutionary::Op::Eval::MO_Rank - Multiobjective evaluator | ||
| 7 | based on Pareto rank | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | =head1 Base Class | ||
| 12 | |||
| 13 | L<Algorithm::Evolutionary::Op::Base> | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | After evaluating the population, it's ranked according to the Pareto | ||
| 18 | fron they're in | ||
| 19 | |||
| 20 | =head1 METHODS | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | package Algorithm::Evolutionary::Op::Eval::MO_Rank; | ||
| 25 | |||
| 26 | use lib qw(../../../..); | ||
| 27 | |||
| 28 | our $VERSION = sprintf "%d.%03d", q$Revision: 3.2 $ =~ /(\d+)\.(\d+)/g; | ||
| 29 | |||
| 30 | use Carp; | ||
| 31 | |||
| 32 | use base 'Algorithm::Evolutionary::Op::Base'; | ||
| 33 | |||
| 34 | use Algorithm::Evolutionary::Utils qw(vector_compare); | ||
| 35 | |||
| 36 | # Class-wide constants | ||
| 37 | our $APPLIESTO = 'ARRAY'; | ||
| 38 | our $ARITY = 1; | ||
| 39 | |||
| 40 | =head2 new( $evaluation_function ) | ||
| 41 | |||
| 42 | Creates an evaluator object | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | sub new { | ||
| 47 | my $class = shift; | ||
| 48 | my $self = {}; | ||
| 49 | $self->{'_eval'} = shift || croak "No eval function found"; | ||
| 50 | bless $self, $class; | ||
| 51 | return $self; | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | =head2 set( $ref_to_params_hash, $ref_to_code_hash, $ref_to_operators_hash ) | ||
| 56 | |||
| 57 | Sets the instance variables. Takes a ref-to-hash as input. Not | ||
| 58 | intended to be used from outside the class. This should go to base, | ||
| 59 | probably. | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | sub set { | ||
| 64 | my $self = shift; | ||
| 65 | my $hashref = shift || croak "No params here"; | ||
| 66 | my $codehash = shift || croak "No code here"; | ||
| 67 | my $opshash = shift || croak "No ops here"; | ||
| 68 | |||
| 69 | for ( keys %$codehash ) { | ||
| 70 | $self->{"_$_"} = eval "sub { $codehash->{$_} } "; | ||
| 71 | } | ||
| 72 | |||
| 73 | $self->{_ops} =(); | ||
| 74 | for ( keys %$opshash ) { | ||
| 75 | push @{$self->{_ops}}, | ||
| 76 | Algorithm::Evolutionary::Op::Base::fromXML( $_, $opshash->{$_}->[1], $opshash->{$_}->[0] ) ; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | =head2 apply( $population ) | ||
| 81 | |||
| 82 | Evaluates the population, setting its fitness value. Fitness should be | ||
| 83 | univocal, that is, have a single value per string. | ||
| 84 | |||
| 85 | =cut | ||
| 86 | |||
| 87 | sub apply ($) { | ||
| 88 | my $self = shift; | ||
| 89 | my $pop = shift || croak "No population here"; | ||
| 90 | croak "Incorrect type ".(ref $pop) if ref( $pop ) ne $APPLIESTO; | ||
| 91 | |||
| 92 | my $eval = $self->{_eval}; | ||
| 93 | #Compute vector fitness | ||
| 94 | my %fitness_vector_of; | ||
| 95 | for my $p (@$pop ) { | ||
| 96 | $p->evaluate( $eval ); | ||
| 97 | $fitness_vector_of{$p->as_string} = $p->Fitness(); #Provisional fitness | ||
| 98 | } | ||
| 99 | |||
| 100 | #Compute rank | ||
| 101 | my @dominated_by; | ||
| 102 | my $i; | ||
| 103 | for ( $i = 0; $i < @$pop; $i++ ) { | ||
| 104 | for ( my $j = $i+1; $j < @$pop; $j++ ) { | ||
| 105 | my $result = | ||
| 106 | vector_compare( $fitness_vector_of{ $pop->[$i]->as_string }, | ||
| 107 | $fitness_vector_of{ $pop->[$j]->as_string } ); | ||
| 108 | $dominated_by[$j]++ if $result == -1; | ||
| 109 | $dominated_by[$i]++ if $result == 1; | ||
| 110 | } | ||
| 111 | |||
| 112 | } | ||
| 113 | |||
| 114 | for ( $i = 0; $i < @$pop; $i++ ) { | ||
| 115 | $dominated_by[$i]++; # To avoid uninitialized | ||
| 116 | $pop->[$i]->Fitness( 1/$dominated_by[$i] ); | ||
| 117 | } | ||
| 118 | |||
| 119 | } | ||
| 120 | |||
| 121 | =head1 SEE ALSO | ||
| 122 | |||
| 123 | =over 4 | ||
| 124 | |||
| 125 | =item * | ||
| 126 | |||
| 127 | L<Algorithm::Evolutionary::Fitness::Base> | ||
| 128 | |||
| 129 | =back | ||
| 130 | |||
| 131 | =head1 Copyright | ||
| 132 | |||
| 133 | This file is released under the GPL. See the LICENSE file included in this distribution, | ||
| 134 | or go to http://www.fsf.org/licenses/gpl.txt | ||
| 135 | |||
| 136 | CVS Info: $Date: 2009/11/23 12:31:16 $ | ||
| 137 | $Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Op/Eval/MO_Rank.pm,v 3.2 2009/11/23 12:31:16 jmerelo Exp $ | ||
| 138 | $Author: jmerelo $ | ||
| 139 | $Revision: 3.2 $ | ||
| 140 | |||
| 141 | =cut | ||
| 142 | |||
| 143 | "The truth is out there"; |