| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 2 |  |  | 2 |  | 16023 | use strict; #-*-cperl-*- | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 43 |  | 
| 2 | 2 |  |  | 2 |  | 6 | use warnings; | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 49 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 2 |  |  | 2 |  | 6 | use lib qw( ../../../../lib ); | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 6 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | =head1 NAME | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | Algorithm::Evolutionary::Fitness - Base class for fitness functions | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | Shouldn't be used directly, it's an abstract class whose siblings are | 
| 13 |  |  |  |  |  |  | used to implement fitness functions. | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | This module includes functionality that should be common to all fitness functions. Or at least it | 
| 18 |  |  |  |  |  |  | would be nice to have it in common. It counts the number of evaluations and includes a common API for caching evaluations. | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | =head1 METHODS | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | =cut | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | package Algorithm::Evolutionary::Fitness; | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 2 |  |  | 2 |  | 205 | use Carp; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 109 |  | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 2 |  |  | 2 |  | 777 | use version; our $VERSION = qv("v3.102"); | 
|  | 2 |  |  |  |  | 2681 |  | 
|  | 2 |  |  |  |  | 9 |  | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | =head2 new() | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | Initializes common variables, like the number of evaluations. Cache is not initialized. | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | =cut | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | sub new { | 
| 38 | 0 |  |  | 0 | 1 |  | my $class = shift; | 
| 39 | 0 |  |  |  |  |  | my $self = {}; | 
| 40 | 0 |  |  |  |  |  | bless $self, $class; | 
| 41 | 0 |  |  |  |  |  | $self->initialize(); | 
| 42 | 0 |  |  |  |  |  | return $self; | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | =head2 initialize() | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | Called from new, initializes the evaluations counter. | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | =cut | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | sub initialize { | 
| 52 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 53 | 0 |  |  |  |  |  | $self->{'_counter'} = 0; | 
| 54 | 0 |  |  |  |  |  | $self->{'_cache'} = {}; # This is optional; should be used from derived classes | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | =head2 apply( $individual ) | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | Applies the instantiated problem to a chromosome. Actually it is a | 
| 61 |  |  |  |  |  |  | wrapper around C<_apply> | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | =cut | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | sub apply { | 
| 66 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 67 | 0 |  |  |  |  |  | my $individual = shift; | 
| 68 | 0 |  |  |  |  |  | $self->{'_counter'}++; | 
| 69 | 0 |  |  |  |  |  | return $self->_apply( $individual ); | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | =head2 _apply( $individual ) | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | This is the one that really does the stuff. Should be overloaded by | 
| 75 |  |  |  |  |  |  | derived clases | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | =cut | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | sub _apply { | 
| 80 | 0 |  |  | 0 |  |  | croak "You should have overloaded this\n"; | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | =head2 evaluations() | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | Returns the number of evaluations made with this object. Useful for | 
| 87 |  |  |  |  |  |  | collecting stats, but it's up to the offspring to update it by using apply (which could make it slower anyway). | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | =cut | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | sub evaluations { | 
| 92 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 93 | 0 |  |  |  |  |  | return $self->{'_counter'}; | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | =head2 reset_evaluations() | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | Sets to 0 the number of evaluations; useful for repeated use of the fitness object | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | =cut | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | sub reset_evaluations { | 
| 103 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 104 | 0 |  |  |  |  |  | $self->{'_counter'} = 0; | 
| 105 |  |  |  |  |  |  | } | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | =head2 cache() | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | Returns a reference to the internal evaluations cache. Not very encapsulated, but... | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | =cut | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | sub cache { | 
| 114 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 115 | 0 |  |  |  |  |  | return $self->{'_cache'}; | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | =head1 Known subclasses | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | =over 4 | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | =item * | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | L | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | =item * | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | L | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | =item * | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | L | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | =item * | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | L | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | =item * | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | L | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | =item * | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | L | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | =item * | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | L | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | =item * | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | L | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | =item * | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | L | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | =back | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | =head1 Copyright | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | This file is released under the GPL. See the LICENSE file included in this distribution, | 
| 163 |  |  |  |  |  |  | or go to http://www.fsf.org/licenses/gpl.txt | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | =cut | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | "What???"; |