| 1 | 1 | ▶ | use strict; # -*- cperl -*- |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | 1 | ▶ | use lib qw( ../../../../lib ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 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 | " The ECC problem was presented in | ||
| 20 | [MW]. We will consider a three-tuple (n, M, d), where n is the length of each codeword | ||
| 21 | (number of bits), M is the number of codewords, and d is the minimum Hamming | ||
| 22 | distance between any pair of codewords. Our objective will be to find a code which | ||
| 23 | has a value for d as large as possible (reflecting greater tolerance to noise and errors), | ||
| 24 | given previously fixed values for n and M . The problem we have studied is a simplified | ||
| 25 | version of that in [MW]. In our case we search half of the codewords (M/2) that will | ||
| 26 | compose the code, and the other half is made up by the complement of the codewords | ||
| 27 | computed by the algorithm" | ||
| 28 | |||
| 29 | [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. | ||
| 30 | |||
| 31 | [MW] F. J. MacWilliams and N. J. A. Sloane. The Theory of Error-Correcting Codes. North- | ||
| 32 | Holland, Amsterdam, 1977. | ||
| 33 | |||
| 34 | |||
| 35 | =head1 METHODS | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | package Algorithm::Evolutionary::Fitness::ECC; | ||
| 40 | |||
| 41 | our ($VERSION) = ( '$Revision: 3.2 $ ' =~ / (\d+\.\d+)/ ) ; | ||
| 42 | |||
| 43 | 1 | ▶ | use Carp qw(croak); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 44 | |||
| 45 | 1 | ▶ | use lib qw(../../.. ../.. ..); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 46 | |||
| 47 | 1 | ▶ | use base qw(Algorithm::Evolutionary::Fitness::String); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 48 | 0 | ▶ | use Algorithm::Evolutionary::Utils qw(hamming); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 49 | |||
| 50 | =head2 new | ||
| 51 | |||
| 52 | Creates a new instance of the problem, with the said number of bits and peaks | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | sub new { | ||
| 57 | my $class = shift; | ||
| 58 | my ($number_of_codewords, $min_distance ) = @_; | ||
| 59 | croak "Too few codewords" if !$number_of_codewords; | ||
| 60 | croak "Distance too small" if !$min_distance; | ||
| 61 | my $self = $class->SUPER::new(); | ||
| 62 | bless $self, $class; | ||
| 63 | $self->initialize(); | ||
| 64 | $self->{'number_of_codewords'} = $number_of_codewords; | ||
| 65 | return $self; | ||
| 66 | } | ||
| 67 | |||
| 68 | =head2 _really_apply | ||
| 69 | |||
| 70 | Applies the instantiated problem to a chromosome | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | sub _really_apply { | ||
| 75 | my $self = shift; | ||
| 76 | return $self->ecc( @_ ); | ||
| 77 | } | ||
| 78 | |||
| 79 | =head2 ecc | ||
| 80 | |||
| 81 | Applies the instantiated problem to a string | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | sub ecc { | ||
| 86 | my $self = shift; | ||
| 87 | my $string = shift || croak "Can't work with a null string"; | ||
| 88 | my $cache = $self->{'_cache'}; | ||
| 89 | if ( $cache->{$string} ) { | ||
| 90 | return $cache->{$string}; | ||
| 91 | } | ||
| 92 | my $length = length($string)/$self->{'number_of_codewords'}; | ||
| 93 | my @codewords = ( $string =~ /.{$length}/gs ); | ||
| 94 | my $distance; | ||
| 95 | for ( my $i = 0; $i <= $#codewords; $i ++ ) { | ||
| 96 | for ( my $j = $i+1; $j <= $#codewords; $j ++ ) { | ||
| 97 | my $this_distance = hamming( $codewords[$i], $codewords[$j] ); | ||
| 98 | $distance += 1/(1+$this_distance*$this_distance); | ||
| 99 | } | ||
| 100 | } | ||
| 101 | $cache->{$string} = 1/$distance; | ||
| 102 | return $cache->{$string}; | ||
| 103 | |||
| 104 | } | ||
| 105 | |||
| 106 | =head1 Copyright | ||
| 107 | |||
| 108 | This file is released under the GPL. See the LICENSE file included in this distribution, | ||
| 109 | or go to http://www.fsf.org/licenses/gpl.txt | ||
| 110 | |||
| 111 | CVS Info: $Date: 2009/07/24 10:25:49 $ | ||
| 112 | $Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Fitness/ECC.pm,v 3.2 2009/07/24 10:25:49 jmerelo Exp $ | ||
| 113 | $Author: jmerelo $ | ||
| 114 | $Revision: 3.2 $ | ||
| 115 | $Name $ | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | "What???"; | ||