blib/lib/Algorithm/Evolutionary/Fitness/Trap.pm untested

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