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

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 79 CC 3 SCAR 12.0
CC
3
Cov
0%
new
6.0
p_peaks
6.0
random_string
2.0
CRAP 3.3
1 use strict;
2 use warnings;
3  
4 use lib qw( ../../../../lib );
5  
6 =head1 NAME
7  
8 Algorithm::Evolutionary::Fitness::wP_Peaks - wP Peaks problem generator - weighted version of P_Peaks
9  
10 =head1 SYNOPSIS
11  
12 my $number_of_bits = 32;
13 my @weights = (1);
14 for (my $i = 0; $i < 99; $i ++ ) {
15 push @weights, 0.99;
16 }
17 my $wp_peaks = Algorithm::Evolutionary::Fitness::wP_Peaks->new( $number_of_bits, @weights ); #Number of peaks = scalar @weights
18  
19 # Or use an alternative ctor
20 my $descriptor = { number_of_peaks => 100,
21 weight => 0.99 };
22 my $wp_peaks = Algorithm::Evolutionary::Fitness::wP_Peaks->new( $number_of_bits, $descriptor)
23  
24 =head1 DESCRIPTION
25  
26 wP-Peaks fitness function, weighted version of the P-Peaks fitness function, which has now a single peak
27  
28 =head1 METHODS
29  
30 =cut
31  
32 package Algorithm::Evolutionary::Fitness::wP_Peaks;
33  
34 our $VERSION = sprintf "%d.%03d", q$Revision: 3.2 $ =~ /(\d+)\.(\d+)/g;
35  
36 use String::Random;
37 use Algorithm::Evolutionary::Utils qw(hamming);
38  
39 use base qw(Algorithm::Evolutionary::Fitness::String);
40  
41 =head2 new( $number_of_bits, @weights_array )
42  
43 or new( $number_of_bits, $hash_with_number_of_peaks_and_weight )
44  
45 Creates a new instance of the problem, with the said number of bits and peaks
46  
47 =cut
48  
49 sub new {
50 my $class = shift;
51 my ( $bits ) = shift;
52  
53 my @weights;
54 if ( ref $_[0] eq 'HASH' ) {
55 push @weights, 1;
56 for (my $i = 0; $i < $_[0]->{'number_of_peaks'}-1; $i ++ ) {
57 push @weights, $_[0]->{'weight'};
58 }
59 } else {
60 @weights = @_;
61 }
62 my $self = $class->SUPER::new();
63  
64 #Generate peaks
65 my $generator = new String::Random;
66 my @peaks;
67 my $regex = "\[01\]{$bits}";
68 for my $s ( qw( bits generator regex ) ) {
69 eval "\$self->{'$s'} = \$$s";
70 }
71 while ( @weights ) {
72 my $this_w = shift @weights;
73 push( @peaks, [$this_w, $generator->randregex($regex)]);
74 }
75 $self->{'peaks'} = \@peaks;
76 bless $self, $class;
77 $self->initialize();
78 return $self;
79 }
80  
81 =head2 random_string
82  
83 Returns random string in the same style than the peaks. Useful for testing
84  
85 =cut
86  
87 sub random_string {
88 my $self = shift;
89 return $self->{'generator'}->randregex($self->{'regex'});
90 }
91  
92 sub _really_apply {
93 my $self = shift;
94 return $self->p_peaks( @_ )/$self->{'bits'};
95 }
96  
97 =head2 p_peaks
98  
99 Applies the instantiated problem to a string
100  
101 =cut
102  
103 sub p_peaks {
104 my $self = shift;
105 my @peaks = @{$self->{'peaks'}};
106 my $string = shift;
107 my $cache = $self->{'_cache'};
108
109 if ( $cache->{$string} ) {
110 return $cache->{$string};
111 }
112 my $bits = $self->{'bits'};
113 my @distances = sort {$b <=> $a} map($bits*$_->[0]- hamming( $string, $_->[1]), @peaks);
114 $cache->{$string} = $distances[0];
115 return $cache->{$string};
116  
117 }
118  
119  
120 =head1 Copyright
121
122 This file is released under the GPL. See the LICENSE file included in this distribution,
123 or go to http://www.fsf.org/licenses/gpl.txt
124  
125 CVS Info: $Date: 2012/07/08 16:37:25 $
126 $Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Fitness/wP_Peaks.pm,v 3.2 2012/07/08 16:37:25 jmerelo Exp $
127 $Author: jmerelo $
128 $Revision: 3.2 $
129 $Name $
130  
131 =cut
132  
133 "What???";