lib/Algorithm/Evolutionary/Op/DeltaTerm.pm

filter: Statementstmt 100% 12 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 100% 16 / 16 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/Algorithm/Evolutionary/Op/Crossover.pm ↑ Summary lib/Algorithm/Evolutionary/Op/EDA_step.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4  
5 =head1 NAME
6  
7 Algorithm::Evolutionary::Op::DeltaTerm - Termination condition for an algorithm; checks that
8 the difference of the best to a target is less than a delta
9
10  
11 =head1 SYNOPSIS
12  
13 my $target = 1;
14 my $epsilon = 0.01;
15 my $dt = new Algorithm::Evolutionary::Op::DeltaTerm $target, $epsilon;
16 #$dt->apply( \@pop ) when the best fitness is 1 plus/minus 0.1
17  
18 =head1 Base Class
19  
20 L<Algorithm::Evolutionary::Op::Base|Algorithm::Evolutionary::Op::Base>
21  
22 =head1 DESCRIPTION
23  
24 Termination condition for evolutionary algorithm loops; the C<apply>
25 method returns false when the first element in the array is as close
26 to the target as the differente indicated.
27  
28 =head1 METHODS
29  
30 =cut
31  
32 package Algorithm::Evolutionary::Op::DeltaTerm;
33 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 our ($VERSION) = ( '$Revision: 3.0 $ ' =~ / (\d+\.\d+)/ ) ;
36  
37 1 ▶use base 'Algorithm::Evolutionary::Op::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 =head2 new( $target[, $delta] )
40  
41 Creates a new terminator. Takes as parameters the target and the
42 epsilon (or delta, whatever you want to call it):
43  
44 my $target = 1;
45 my $epsilon = 0.01;
46 my $dt = new Algorithm::Evolutionary::Op::DeltaTerm $target, $epsilon;
47  
48 Delta can be 0, which means that application of this operator will
49 return true only when the first element fitness is the same as the
50 target. Use this judiciously when your fitness is a floating
51 point number.
52  
53 =cut
54  
55 sub new {
56 my $class = shift;
57 my $target = shift || croak "No target here!";
58 my $delta = shift ; # Could be 0
59 my $hash = { target => $target,
60 delta => $delta };
61 my $self = Algorithm::Evolutionary::Op::Base::new( $class, 1, $hash );
62 return $self;
63 }
64  
65  
66 =head2 apply( $population )
67  
68 Will return true while the difference between the fitness of the first element
69 in the population and the target is less than C<$delta>, true otherwise
70  
71 $dt->apply( \@pop ) == 1
72  
73 if the target has not been reached. Population must be sorted before this.
74  
75 =cut
76  
77 sub apply ($) {
78 my $self = shift;
79 my $pop = shift;
80  
81 return abs( $pop->[0]->Fitness() - $self->{_target} ) > $self->{_delta};
82
83 }
84
85 =head1 See Also
86  
87 L<Algorithm::Evolutionary::Op::FullAlgorithm> needs an object of this class to check
88 for the termination condition. It's normally used alongside "generation-type"
89 objects such as L<Algorithm::Evolutionary::Op::Easy>.
90  
91 There are other options for termination conditions: L<Algorithm::Evolutionary::Op::NoChangeTerm|Algorithm::Evolutionary::Op::NoChangeTerm> and
92 L<Algorithm::Evolutionary::Op::GenerationalTerm>.
93  
94  
95 =head1 Copyright
96
97 This file is released under the GPL. See the LICENSE file included in this distribution,
98 or go to http://www.fsf.org/licenses/gpl.txt
99  
100 CVS Info: $Date: 2009/07/24 08:46:59 $
101 $Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Op/DeltaTerm.pm,v 3.0 2009/07/24 08:46:59 jmerelo Exp $
102 $Author: jmerelo $
103  
104 =cut
105  
106 "The truth is out there";
« lib/Algorithm/Evolutionary/Op/Crossover.pm ↑ Summary lib/Algorithm/Evolutionary/Op/EDA_step.pm »