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

filter: Statementstmt 0.0% 0 / 37 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 61 CC 3 SCAR 16.1
CC
3
Cov
0%
_apply
12.0
new
2.0
apply
2.0
CRAP 5.0
1 use strict; #-*-cperl-*-
2 use warnings;
3  
4 use lib qw( ../../../../lib );
5  
6 =encoding utf8
7  
8 =head1 NAME
9  
10 Algorithm::Evolutionary::Fitness::Any - Façade for any function so that it can be used as fitness
11  
12 =head1 SYNOPSIS
13  
14 use Algorithm::Evolutionary::Utils qw( string_decode )
15  
16 sub squares {
17 my $chrom = shift;
18 my @values = string_decode( $chrom, 10, -1, 1 );
19 return $values[0] * $values[1];
20 }
21  
22 my $any_eval = new Algorithm::Evolutionary::Fitness::Any \&squares;
23  
24  
25 =head1 DESCRIPTION
26  
27 Turns any subroutine or closure into a fitness function. Useful mainly
28 if you want results cached; it's not really needed otherwise.
29  
30 =head1 METHODS
31  
32 =cut
33  
34 package Algorithm::Evolutionary::Fitness::Any;
35  
36 use Carp;
37  
38 use base 'Algorithm::Evolutionary::Fitness::Base';
39  
40 our $VERSION = '3.2';
41  
42 =head2 new( $function )
43  
44 Assigns default variables
45  
46 =cut
47  
48 sub new {
49 my $class = shift;
50 my $self = { _function => shift || croak "No functiona rray" };
51 bless $self, $class;
52 $self->initialize();
53 return $self;
54 }
55  
56 =head2 apply( $individual )
57  
58 Applies the instantiated problem to a chromosome. It is actually a
59 wrapper around C<_apply>.
60  
61 =cut
62  
63 sub apply {
64 my $self = shift;
65 my $individual = shift || croak "Nobody here!!!";
66 $self->{'_counter'}++;
67 return $self->_apply( $individual );
68 }
69  
70 =head2 _apply( $individual )
71  
72 This is the one that really does the stuff. It applies the defined
73 function to each individual. Itis cached for efficiency.
74  
75 =cut
76  
77 sub _apply {
78 my $self = shift;
79 my $individual = shift || croak "Nobody here!";
80 my $chrom = $individual->Chrom();
81 my $cache = $self->{'_cache'};
82 if ( $cache->{$chrom} ) {
83 return $cache->{$chrom};
84 }
85 my $result = $self->{'_function'}->($chrom);
86 if ( (scalar $chrom ) eq $chrom ) {
87 $cache->{$chrom} = $result;
88 }
89 return $result;
90 }
91  
92  
93 =head1 Copyright
94
95 This file is released under the GPL. See the LICENSE file included in this distribution,
96 or go to http://www.fsf.org/licenses/gpl.txt
97  
98 =cut
99  
100 "What???";