Algorithm-Evolutionary
view release on metacpan or search on metacpan
lib/Algorithm/Evolutionary/Fitness/Trap.pm view on Meta::CPAN
package Algorithm::Evolutionary::Fitness::Trap; # -*- cperl -*-
use strict;
use warnings;
use Carp qw(croak);
our $VERSION = '3.2';
use lib qw(../../.. ../.. .. ../../../../lib);
use base qw(Algorithm::Evolutionary::Fitness::String);
=head1 NAME
Algorithm::Evolutionary::Fitness::Trap - 'Trap' fitness function for evolutionary algorithms
=head1 SYNOPSIS
my $number_of_bits = 5;
my $a = $number_of_bits -1; # Usual default values follow
my $b = $number_of_bits;
my $z = $number_of_bits -1;
my $trap = Algorithm::Evolutionary::Fitness::Trap->new( $number_of_bits, $a, $b, $z );
# Equivalent to
$trap = Algorithm::Evolutionary::Fitness::Trap->new( $number_of_bits );
my $chromosome = "10101111110000";
my $fitness = $trap->apply( $chromosome );
=head1 DESCRIPTION
Trap functions act as "yucky" or deceptive for evolutionary algorithms;
they "trap" population into going to easier, but local, optima.
=head1 METHODS
=head2 new( $number_of_bits, [$a = $number_of_bits -1, $b = $number_of_bits, $z=$number_of_bits-1])
Creates a new instance of the problem, with the said number of bits
and traps. Uses default values from C<$number_of_bits> if needed
=cut
sub new {
my $class = shift;
my $number_of_bits = shift || croak "Need non-null number of bits\n";
my $a = shift || $number_of_bits - 1;
my $b = shift || $number_of_bits;
my $z = shift || $number_of_bits - 1;
croak "Z too big" if $z >= $number_of_bits;
croak "Z too small" if $z < 1;
croak "A must be less than B" if $a > $b;
my $self = $class->SUPER::new();
bless $self, $class;
$self->initialize();
$self->{'l'} = $number_of_bits;
$self->{'a'} = $a;
$self->{'b'} = $b;
$self->{'z'} = $z;
return $self;
}
=head2 _really_apply
( run in 0.631 second using v1.01-cache-2.11-cpan-5735350b133 )