« blib/lib/Algorithm/Evolutionary/Op/StringRand.pm
↑ Summary
lib/Algorithm/Evolutionary/Individual/Tree.pm »
| 1 | package Algorithm::Evolutionary::Fitness::Trap; # -*- cperl -*- | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp qw(croak); | ||
| 6 | |||
| 7 | our $VERSION = '3.2'; | ||
| 8 | |||
| 9 | use lib qw(../../.. ../.. .. ../../../../lib); | ||
| 10 | |||
| 11 | use base qw(Algorithm::Evolutionary::Fitness::String); | ||
| 12 | |||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | Algorithm::Evolutionary::Fitness::Trap - 'Trap' fitness function for evolutionary algorithms | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | my $number_of_bits = 5; | ||
| 21 | my $a = $number_of_bits -1; # Usual default values follow | ||
| 22 | my $b = $number_of_bits; | ||
| 23 | my $z = $number_of_bits -1; | ||
| 24 | my $trap = Algorithm::Evolutionary::Fitness::Trap->new( $number_of_bits, $a, $b, $z ); | ||
| 25 | |||
| 26 | # Equivalent to | ||
| 27 | |||
| 28 | $trap = Algorithm::Evolutionary::Fitness::Trap->new( $number_of_bits ); | ||
| 29 | |||
| 30 | my $chromosome = "10101111110000"; | ||
| 31 | my $fitness = $trap->apply( $chromosome ); | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | Trap functions act as "yucky" or deceptive for evolutionary algorithms; | ||
| 36 | they "trap" population into going to easier, but local, optima. | ||
| 37 | |||
| 38 | =head1 METHODS | ||
| 39 | |||
| 40 | |||
| 41 | =head2 new( $number_of_bits, [$a = $number_of_bits -1, $b = $number_of_bits, $z=$number_of_bits-1]) | ||
| 42 | |||
| 43 | Creates a new instance of the problem, with the said number of bits | ||
| 44 | and traps. Uses default values from C<$number_of_bits> if needed | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | sub new { | ||
| 49 | my $class = shift; | ||
| 50 | my $number_of_bits = shift || croak "Need non-null number of bits\n"; | ||
| 51 | my $a = shift || $number_of_bits - 1; | ||
| 52 | my $b = shift || $number_of_bits; | ||
| 53 | my $z = shift || $number_of_bits - 1; | ||
| 54 | |||
| 55 | croak "Z too big" if $z >= $number_of_bits; | ||
| 56 | croak "Z too small" if $z < 1; | ||
| 57 | croak "A must be less than B" if $a > $b; | ||
| 58 | my $self = $class->SUPER::new(); | ||
| 59 | bless $self, $class; | ||
| 60 | $self->initialize(); | ||
| 61 | $self->{'l'} = $number_of_bits; | ||
| 62 | $self->{'a'} = $a; | ||
| 63 | $self->{'b'} = $b; | ||
| 64 | $self->{'z'} = $z; | ||
| 65 | return $self; | ||
| 66 | } | ||
| 67 | |||
| 68 | =head2 _really_apply | ||
| 69 | |||
| 70 | Applies the instantiated problem to a chromosome | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | sub _really_apply { | ||
| 75 | my $self = shift; | ||
| 76 | return $self->trap( @_ ); | ||
| 77 | } | ||
| 78 | |||
| 79 | =head2 trap( $string ) | ||
| 80 | |||
| 81 | Computes the value of the trap function on the C<$string>. Optimum is | ||
| 82 | number_of_blocs * $b (by default, $b = $l or number of ones) | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | sub trap { | ||
| 87 | my $self = shift; | ||
| 88 | my $string = shift; | ||
| 89 | my $cache = $self->{'_cache'}; | ||
| 90 | if ( $cache->{$string} ) { | ||
| 91 | return $cache->{$string}; | ||
| 92 | } | ||
| 93 | my $l = $self->{'l'}; | ||
| 94 | my $z = $self->{'z'}; | ||
| 95 | my $total = 0; | ||
| 96 | for ( my $i = 0; $i < length( $string); $i+= $l ) { | ||
| 97 | my $substr = substr( $string, $i, $l ); | ||
| 98 | my $key = $substr; | ||
| 99 | if ( !$cache->{$substr} ) { | ||
| 100 | my $num_ones = 0; | ||
| 101 | while ( $substr ) { | ||
| 102 | $num_ones += chop( $substr ); | ||
| 103 | } | ||
| 104 | if ( $num_ones <= $z ) { | ||
| 105 | $cache->{$key} = $self->{'a'}*($z-$num_ones)/$z; | ||
| 106 | } else { | ||
| 107 | $cache->{$key} = $self->{'b'}*($num_ones -$z)/($l-$z); | ||
| 108 | } | ||
| 109 | } | ||
| 110 | $total += $cache->{$key}; | ||
| 111 | } | ||
| 112 | $cache->{$string} = $total; | ||
| 113 | return $cache->{$string}; | ||
| 114 | |||
| 115 | } | ||
| 116 | |||
| 117 | =head1 Copyright | ||
| 118 | |||
| 119 | This file is released under the GPL. See the LICENSE file included in this distribution, | ||
| 120 | or go to http://www.fsf.org/licenses/gpl.txt | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | "Gotcha trapped!"; | ||
| 125 |