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

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 87 CC 4 SCAR 14.8
CC
4
Cov
0%
new
12.0
p_peaks
6.0
random_string
2.0
CRAP 4.4
1 use strict; # -*- cperl -*-
2 use warnings;
3  
4 use lib qw( ../../../../lib );
5  
6 =head1 NAME
7  
8 Algorithm::Evolutionary::Fitness::P_Peaks - P Peaks problem generator
9  
10 =head1 SYNOPSIS
11  
12 my $number_of_peaks = 100;
13 my $number_of_bits = 32;
14 my $p_peaks = Algorithm::Evolutionary::Fitness::P_Peaks->new( $number_of_peaks, $number_of_bits );
15  
16 =head1 DESCRIPTION
17  
18 P_Peaks fitness function; optimizes the distance to the closest in a
19 series of peaks.
20 The P-Peaks problem was proposed by Kennedy and Spears in
21  
22 @conference{kennedy1998matching,
23 title={{Matching algorithms to problems: an experimental test of the particle swarm and some genetic algorithms on the multimodal problem generator}},
24 author={Kennedy, J. and Spears, W.M.},
25 booktitle={Evolutionary Computation Proceedings, 1998. IEEE World Congress on Computational Intelligence., The 1998 IEEE International Conference on},
26 pages={78--83},
27 isbn={0780348699},
28 year={1998},
29 organization={IEEE}
30 }
31  
32 And the optimum is 1.0. By default, result is cached, so be careful
33 when working with long strings and/or big populations
34  
35 =head1 METHODS
36  
37 =cut
38  
39 package Algorithm::Evolutionary::Fitness::P_Peaks;
40  
41 our $VERSION = '3.4';
42  
43 use String::Random;
44 use Carp;
45  
46 use lib qw(../../.. ../.. ..);
47  
48 use base qw(Algorithm::Evolutionary::Fitness::String);
49 use Algorithm::Evolutionary::Utils qw(hamming);
50  
51 =head2 new( $peaks, $bits )
52  
53 Creates a new instance of the problem, with the said number of bits
54 and peaks.
55  
56 =cut
57  
58 use constant VARS => qw( bits generator regex );
59  
60 sub new {
61 my $class = shift;
62 my ($peaks, $bits ) = @_;
63 croak "No peaks" if !$peaks;
64 croak "Too few bits" if !$bits;
65 my $self = $class->SUPER::new();
66 #Generate peaks
67 my $generator = new String::Random;
68 my @peaks;
69 my $regex = "\[01\]{$bits}";
70 for my $s ( VARS ) {
71 eval "\$self->{'$s'} = \$$s";
72 }
73 for my $p ( 1..$peaks ) {
74 push( @peaks, $generator->randregex($regex));
75 }
76 $self->{'peaks'} = \@peaks;
77 bless $self, $class;
78 $self->initialize();
79 return $self;
80 }
81  
82 =head2 random_string()
83  
84 Returns random string in the same style than the peaks. Useful for testing.
85  
86 =cut
87  
88 sub random_string {
89 my $self = shift;
90 return $self->{'generator'}->randregex($self->{'regex'});
91 }
92  
93 =head2 _really_apply( $string )
94  
95 Applies the instantiated problem to a chromosome. Intended for internal use.
96  
97 =cut
98  
99 sub _really_apply {
100 my $self = shift;
101 return $self->p_peaks( @_ )/$self->{'bits'} ;
102 }
103  
104 =head2 p_peaks( $string )
105  
106 Returns the distance to the closest bitstring
107  
108 =cut
109  
110 sub p_peaks {
111 my $self = shift;
112 my @peaks = @{$self->{'peaks'}};
113 my $string = shift || croak "No string!";
114 my $cache = $self->{'_cache'};
115 if ( $cache->{$string} ) {
116 return $cache->{$string};
117 }
118 my $bits = $self->{'bits'};
119 my @distances = sort {$b <=> $a} map($bits - hamming( $string, $_), @peaks);
120 $cache->{$string} = $distances[0];
121 return $cache->{$string};
122  
123 }
124  
125 =head1 Copyright
126
127 This file is released under the GPL. See the LICENSE file included in this distribution,
128 or go to http://www.fsf.org/licenses/gpl.txt
129  
130 =cut
131  
132 "What???";