| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
15
|
|
|
15
|
|
99
|
use strict; # -*- cperl -*- |
|
|
15
|
|
|
|
|
30
|
|
|
|
15
|
|
|
|
|
879
|
|
|
2
|
15
|
|
|
15
|
|
130
|
use warnings; |
|
|
15
|
|
|
|
|
28
|
|
|
|
15
|
|
|
|
|
445
|
|
|
3
|
|
|
|
|
|
|
|
|
4
|
15
|
|
|
15
|
|
71
|
use lib qw( ../../../../lib ); |
|
|
15
|
|
|
|
|
30
|
|
|
|
15
|
|
|
|
|
83
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
Algorithm::Evolutionary::Fitness::String - Base class for string-based fitness functors |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
package My::Own::Fitness; |
|
13
|
|
|
|
|
|
|
use base 'Algorithm::Evolutionary::Fitness::String'; #Mainly for deriving |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $evaluator = new My::Own::Fitness; |
|
16
|
|
|
|
|
|
|
my $cache_hits = $evaluator->cached_evals(); |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
Base class for fitness functions applied to string-based chromosomes; extracts common code, and saves time. Provides a hash called C<%cache> to be used as a, well, cache for evaluations. |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 METHODS |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=cut |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
package Algorithm::Evolutionary::Fitness::String; |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
our ($VERSION) = ( '$Revision: 3.0 $ ' =~ / (\d+\.\d+)/ ) ; |
|
29
|
|
|
|
|
|
|
|
|
30
|
15
|
|
|
15
|
|
3437
|
use Carp qw( croak ); |
|
|
15
|
|
|
|
|
25
|
|
|
|
15
|
|
|
|
|
892
|
|
|
31
|
15
|
|
|
15
|
|
82
|
use base qw(Algorithm::Evolutionary::Fitness::Base); |
|
|
15
|
|
|
|
|
30
|
|
|
|
15
|
|
|
|
|
9339
|
|
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head2 new() |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
Initializes the cache |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=cut |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub new { |
|
41
|
1
|
|
|
1
|
1
|
862
|
my $class = shift; |
|
42
|
1
|
|
|
|
|
3
|
my $self = { _cache => {} }; |
|
43
|
1
|
|
|
|
|
4
|
bless $self, $class; |
|
44
|
1
|
|
|
|
|
618
|
return $self; |
|
45
|
|
|
|
|
|
|
} |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head2 _apply( $individual ) |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Applies the instantiated problem to a chromosome, delegating to a specific function |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=cut |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub _apply { |
|
54
|
0
|
|
|
0
|
|
|
my $self = shift; |
|
55
|
0
|
|
|
|
|
|
my $individual = shift; |
|
56
|
0
|
|
|
|
|
|
return $self->_really_apply( $individual->{_str}); |
|
57
|
|
|
|
|
|
|
} |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head2 _really_apply( $string ) |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
This one applies the function to the string. Should be overloaded |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=cut |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub _really_apply { |
|
66
|
0
|
|
|
0
|
|
|
croak "This should be overriden"; |
|
67
|
|
|
|
|
|
|
} |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head2 cached_evals() |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
Returns the number of keys in the evaluation cache, which can be |
|
72
|
|
|
|
|
|
|
compared to the total number of evaluations to find the cache hit |
|
73
|
|
|
|
|
|
|
rate. |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=cut |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub cached_evals { |
|
78
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
79
|
0
|
|
|
|
|
|
return scalar keys %{$self->{'_cache'}}; |
|
|
0
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
} |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head1 Copyright |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
This file is released under the GPL. See the LICENSE file included in this distribution, |
|
85
|
|
|
|
|
|
|
or go to http://www.fsf.org/licenses/gpl.txt |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
CVS Info: $Date: 2009/07/24 08:46:59 $ |
|
88
|
|
|
|
|
|
|
$Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Fitness/String.pm,v 3.0 2009/07/24 08:46:59 jmerelo Exp $ |
|
89
|
|
|
|
|
|
|
$Author: jmerelo $ |
|
90
|
|
|
|
|
|
|
$Revision: 3.0 $ |
|
91
|
|
|
|
|
|
|
$Name $ |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=cut |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
"Where???"; |