1use strict; 2use warnings; 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 20L<Algorithm::Evolutionary::Op::Base|Algorithm::Evolutionary::Op::Base> 21 22=head1 DESCRIPTION 23 24Termination condition for evolutionary algorithm loops; the C<apply> 25method returns false when the first element in the array is as close 26to the target as the differente indicated. 27 28=head1 METHODS 29 30=cut 31 32package Algorithm::Evolutionary::Op::DeltaTerm; 33use Carp; 34 35our ($VERSION) = ( '$Revision: 3.0 $ ' =~ / (\d+\.\d+)/ ) ; 36 37use base 'Algorithm::Evolutionary::Op::Base'; 38 39=head2 new( $target[, $delta] ) 40 41Creates a new terminator. Takes as parameters the target and the 42epsilon (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 48Delta can be 0, which means that application of this operator will 49return true only when the first element fitness is the same as the 50target. Use this judiciously when your fitness is a floating 51point number. 52 53=cut 54 55sub 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 68Will return true while the difference between the fitness of the first element 69in the population and the target is less than C<$delta>, true otherwise 70 71 $dt->apply( \@pop ) == 1 72 73if the target has not been reached. Population must be sorted before this. 74 75=cut 76 77sub 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 87L<Algorithm::Evolutionary::Op::FullAlgorithm> needs an object of this class to check 88for the termination condition. It's normally used alongside "generation-type" 89objects such as L<Algorithm::Evolutionary::Op::Easy>. 90 91There are other options for termination conditions: L<Algorithm::Evolutionary::Op::NoChangeTerm|Algorithm::Evolutionary::Op::NoChangeTerm> and 92L<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"; 107