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

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 76 CC 4 SCAR 15.9
CC
4
Cov
0%
new
12.0
ecc
6.0
_really_apply
2.0
CRAP 4.9
1 use strict; # -*- cperl -*-
2 use warnings;
3  
4 use lib qw( ../../../../lib );
5  
6 =head1 NAME
7  
8 Algorithm::Evolutionary::Fitness::ECC - Error Correcting codes problem generator
9  
10 =head1 SYNOPSIS
11  
12 my $number_of_codewords = 10;
13 my $min_distance = 1;
14 my $p_peaks = Algorithm::Evolutionary::Fitness::ECC->new( $number_of_codewords, $min_distance );
15  
16 =head1 DESCRIPTION
17  
18 Extracted from article "Effects of scale-free and small-world topologies on binary coded self-adaptive CEA", by Giacobini et al [Ga]. Quoting:
19  
20 " The ECC problem was presented in
21 [MW]. We will consider a three-tuple (n, M, d), where n is the length of each codeword
22 (number of bits), M is the number of codewords, and d is the minimum Hamming
23 distance between any pair of codewords. Our objective will be to find a code which
24 has a value for d as large as possible (reflecting greater tolerance to noise and errors),
25 given previously fixed values for n and M . The problem we have studied is a simplified
26 version of that in [MW]. In our case we search half of the codewords (M/2) that will
27 compose the code, and the other half is made up by the complement of the codewords
28 computed by the algorithm"
29  
30 [Ga] Mario Giacobini, Mike Preuss, Marco Tomassini: Effects of Scale-Free and Small-World Topologies on Binary Coded Self-adaptive CEA. EvoCOP 2006: 86-98.
31  
32 [MW] F. J. MacWilliams and N. J. A. Sloane. The Theory of Error-Correcting Codes. North-
33 Holland, Amsterdam, 1977.
34  
35  
36 =head1 METHODS
37  
38 =cut
39  
40 package Algorithm::Evolutionary::Fitness::ECC;
41  
42 our $VERSION = '3.2';
43  
44 use Carp qw(croak);
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
52  
53 Creates a new instance of the problem, with the said number of bits and peaks
54  
55 =cut
56  
57 sub new {
58 my $class = shift;
59 my ($number_of_codewords, $min_distance ) = @_;
60 croak "Too few codewords" if !$number_of_codewords;
61 croak "Distance too small" if !$min_distance;
62 my $self = $class->SUPER::new();
63 bless $self, $class;
64 $self->initialize();
65 $self->{'number_of_codewords'} = $number_of_codewords;
66 return $self;
67 }
68  
69 =head2 _really_apply
70  
71 Applies the instantiated problem to a chromosome
72  
73 =cut
74  
75 sub _really_apply {
76 my $self = shift;
77 return $self->ecc( @_ );
78 }
79  
80 =head2 ecc
81  
82 Applies the instantiated problem to a string
83  
84 =cut
85  
86 sub ecc {
87 my $self = shift;
88 my $string = shift || croak "Can't work with a null string";
89 my $cache = $self->{'_cache'};
90 if ( $cache->{$string} ) {
91 return $cache->{$string};
92 }
93 my $length = length($string)/$self->{'number_of_codewords'};
94 my @codewords = ( $string =~ /.{$length}/gs );
95 my $distance;
96 for ( my $i = 0; $i <= $#codewords; $i ++ ) {
97 for ( my $j = $i+1; $j <= $#codewords; $j ++ ) {
98 my $this_distance = hamming( $codewords[$i], $codewords[$j] );
99 $distance += 1/(1+$this_distance*$this_distance);
100 }
101 }
102 $cache->{$string} = 1/$distance;
103 return $cache->{$string};
104  
105 }
106  
107 =head1 Copyright
108
109 This file is released under the GPL. See the LICENSE file included in this distribution,
110 or go to http://www.fsf.org/licenses/gpl.txt
111  
112 =cut
113  
114 "What???";