File Coverage

lib/Algorithm/Evolutionary/Op/DeltaTerm.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1 1     1   8499 use strict;
  1         3  
  1         36  
2 1     1   6 use warnings;
  1         2  
  1         56  
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
21              
22             =head1 DESCRIPTION
23              
24             Termination condition for evolutionary algorithm loops; the C
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     1   7 use Carp;
  1         2  
  1         121  
34              
35             our ($VERSION) = ( '$Revision: 3.0 $ ' =~ / (\d+\.\d+)/ ) ;
36              
37 1     1   5 use base 'Algorithm::Evolutionary::Op::Base';
  1         3  
  1         567  
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 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.
90              
91             There are other options for termination conditions: L and
92             L.
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";