File Coverage

lib/Algorithm/Evolutionary/Fitness/Royal_Road.pm
Criterion Covered Total %
statement 12 36 33.3
branch 0 6 0.0
condition n/a
subroutine 4 7 57.1
pod 2 2 100.0
total 18 51 35.2


line stmt bran cond sub pod time code
1 1     1   5 use strict;
  1         3  
  1         34  
2 1     1   5 use warnings;
  1         2  
  1         27  
3              
4 1     1   4 use lib qw( ../../../../lib );
  1         1  
  1         5  
5              
6             =head1 NAME
7              
8             Algorithm::Evolutionary::Fitness::Royal_Road - Mitchell's Royal Road function
9              
10             =head1 SYNOPSIS
11              
12             my $block_size = 4;
13             my $rr = Algorithm::Evolutionary::Fitness::Royal_Road->new( $block_size );
14              
15             =head1 DESCRIPTION
16              
17             Royal Road function, adds block_size to fitness only when the block is complete
18              
19             =head1 METHODS
20              
21             =cut
22              
23             package Algorithm::Evolutionary::Fitness::Royal_Road;
24              
25             our ($VERSION) = ( '$Revision: 3.1 $ ' =~ / (\d+\.\d+)/ ) ;
26              
27 1     1   182 use base qw(Algorithm::Evolutionary::Fitness::String);
  1         1  
  1         457  
28              
29             =head2 new( $block_size )
30              
31             Creates a new instance of the problem, with the said block size.
32              
33             =cut
34              
35             sub new {
36 0     0 1   my $class = shift;
37 0           my ( $block_size ) = @_;
38 0           my $self = $class->SUPER::new();
39 0           $self->{'_block_size'} = $block_size;
40 0           $self->initialize();
41 0           return $self;
42             }
43              
44             sub _really_apply {
45 0     0     my $self = shift;
46 0           return $self->royal_road( @_ );
47             }
48              
49             =head2 royal_road( $string )
50              
51             Computes the royal road function with given block size. Results are
52             cached by default.
53              
54             =cut
55              
56             sub royal_road {
57 0     0 1   my $self = shift;
58 0           my $string = shift;
59 0           my $cache = $self->{'_cache'};
60            
61 0 0         if ( $cache->{$string} ) {
62 0           return $cache->{$string};
63             }
64              
65 0           my $fitness = 0;
66 0           my $block_size = $self->{'_block_size'};
67 0           for ( my $i = 0; $i < length( $string ) / $block_size; $i++ ) {
68 0           my $block = 0;
69 0 0         if ( length( substr( $string, $i*$block_size, $block_size )) == $block_size ) {
70 0           $block=1;
71 0           for ( my $j = 0; $j < $block_size; $j++ ) {
72 0           $block &= substr( $string, $i*$block_size+$j, 1 );
73             }
74             }
75 0 0         ( $fitness += $block_size ) if $block;
76             }
77 0           $cache->{$string} = $fitness;
78 0           return $cache->{$string};
79              
80             }
81              
82              
83             =head1 Copyright
84            
85             This file is released under the GPL. See the LICENSE file included in this distribution,
86             or go to http://www.fsf.org/licenses/gpl.txt
87              
88             CVS Info: $Date: 2009/07/28 11:30:56 $
89             $Header: /cvsroot/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Fitness/Royal_Road.pm,v 3.1 2009/07/28 11:30:56 jmerelo Exp $
90             $Author: jmerelo $
91             $Revision: 3.1 $
92             $Name $
93              
94             =cut
95              
96             "What???";