1use strict; #-*-cperl-*- 2use warnings; 3 4=head1 NAME 5 6Algorithm::Evolutionary::Op::GenerationalTerm - Checks for termination of an algorithm. 7 8=head1 SYNOPSIS 9 10 my $gt = new Algorithm::Evolutionary::Op::GenerationalTerm 100; #apply will return false after 100 generations 11 12=head1 Base Class 13 14L<Algorithm::Evolutionary::Op::Base|Algorithm::Evolutionary::Op::Base> 15 16=head1 DESCRIPTION 17 18Checks for termination after a number of generations 19 20=head1 METHODS 21 22=cut 23 24package Algorithm::Evolutionary::Op::GenerationalTerm; 25 26our ($VERSION) = ( '$Revision: 3.0 $ ' =~ / (\d+\.\d+)/ ) ; 27 28use base 'Algorithm::Evolutionary::Op::Base'; 29 30=head2 new( [$number_of_generations = 100] ) 31 32Creates a new generational terminator: 33 34 my $gt = new Algorithm::Evolutionary::Op::GenerationalTerm 100; #apply will return false after 100 generations 35 36will make the C<apply> method return false after 100 calls 37 38=cut 39 40sub new { 41 my $class = shift; 42 my $hash = { generations => shift || 100, 43 counter => 0 }; 44 my $self = Algorithm::Evolutionary::Op::Base::new( __PACKAGE__, 1, $hash ); 45 return $self; 46} 47 48=head2 reset 49 50Resets the number of generations to 0 51 52=cut 53 54sub reset { 55 my $self = shift; 56 $self->{'counter'} =0; 57} 58 59 60=head2 apply() 61 62Checks if the counter has arrived to the allotted number of generations, 63returns false when it does. 64 65 $gt->apply(); 66 67will return C<false> when it has been run for the number of times it has 68been initialized to 69 70=cut 71 72sub apply ($) { 73 my $self = shift; 74 $self->{_counter}++; 75 return $self->{_counter} <= $self->{_generations}; 76 77} 78 79=head1 See Also 80 81L<Algorithm::Evolutionary::Op::FullAlgorithm> needs an object of this class to check 82for the termination condition. It's normally used alongside "generation-type" 83objects such as L<Algorithm::Evolutionary::Op::Easy> 84 85There are other options for termination conditions: L<Algorithm::Evolutionary::Op::NoChangeTerm|Algorithm::Evolutionary::Op::NoChangeTerm>, L<Algorithm::Evolutionary::Op::Convergence_Terminator> and L<Algorithm::Evolutionary::Op::DeltaTerm>. 86 87 88=head1 Copyright 89 90 This file is released under the GPL. See the LICENSE file included in this distribution, 91 or go to http://www.fsf.org/licenses/gpl.txt 92 93 CVS Info: $Date: 2009/07/24 08:46:59 $ 94 $Header: /media/Backup/Repos/opeal/opeal/Algorithm-Evolutionary/lib/Algorithm/Evolutionary/Op/GenerationalTerm.pm,v 3.0 2009/07/24 08:46:59 jmerelo Exp $ 95 $Author: jmerelo $ 96 $Revision: 3.0 $ 97 $Name $ 98 99=cut 100 101"The truth is out there"; 102