An eoContinue class that allows a minimum number of generations,
then stops whenever a number of generations happens without fitness improvement.
This commit is contained in:
parent
08e6be4b66
commit
8f9f255223
1 changed files with 108 additions and 0 deletions
108
eo/src/eoSteadyFitContinue.h
Executable file
108
eo/src/eoSteadyFitContinue.h
Executable file
|
|
@ -0,0 +1,108 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoSteadyFitContinue.h
|
||||
// (c) GeNeura Team, 1999, Marc Schoenauer, 2000
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _eoSteadyFitContinue_h
|
||||
#define _eoSteadyFitContinue_h
|
||||
|
||||
#include <eoContinue.h>
|
||||
|
||||
/**
|
||||
A continuator: does a minimum number of generations, then
|
||||
stops whenever a given number of generations takes place without improvement
|
||||
*/
|
||||
template< class EOT>
|
||||
class eoSteadyFitContinue: public eoContinue<EOT>
|
||||
{
|
||||
public:
|
||||
typedef typename EOT::Fitness Fitness;
|
||||
|
||||
/// Ctor for setting a
|
||||
eoSteadyFitContinue( unsigned long _minGens, unsigned long _steadyGens)
|
||||
: repMinGenerations( _minGens ), repSteadyGenerations( _steadyGens),
|
||||
steadyState(false), thisGenerationPlaceHolder(0),
|
||||
thisGeneration(thisGenerationPlaceHolder){};
|
||||
|
||||
/// Ctor for enabling the save/load the no. of generations counted
|
||||
eoSteadyFitContinue( unsigned long _minGens, unsigned long _steadyGen,
|
||||
unsigned long& _currentGen)
|
||||
: repMinGenerations( _minGens ), repSteadyGenerations( _steadyGen),
|
||||
steadyState(_currentGen>_minGens), thisGenerationPlaceHolder(0),
|
||||
thisGeneration(_currentGen){};
|
||||
|
||||
/** Returns false when a certain number of generations is
|
||||
* reached withtout improvement */
|
||||
virtual bool operator() ( const eoPop<EOT>& _vEO ) {
|
||||
thisGeneration++;
|
||||
// cout << " [" << thisGeneration << "] ";
|
||||
Fitness bestCurrentFitness = _vEO.nth_element_fitness(0);
|
||||
|
||||
if (steadyState) { // already after MinGenenerations
|
||||
if (bestCurrentFitness > bestSoFar) {
|
||||
bestSoFar = bestCurrentFitness;
|
||||
lastImprovement = thisGeneration;
|
||||
} else {
|
||||
if (thisGeneration - lastImprovement > repSteadyGenerations) {
|
||||
cout << "STOP in eoSteadyFitContnue: Done " << repSteadyGenerations
|
||||
<< " generations without improvement\n";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else { // not yet in steady state
|
||||
if (thisGeneration > repMinGenerations) { // go to steady state
|
||||
steadyState = true;
|
||||
bestSoFar = bestCurrentFitness;
|
||||
lastImprovement = thisGeneration;
|
||||
cout << "eoSteadyFitContnue: Done the minimum number of generations\n";
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Sets the parameters (minimum nb of gen. + steady nb of gen.)
|
||||
and sets the current generation to 0 (the begin)*/
|
||||
virtual void totalGenerations( unsigned long _mg, unsigned long _sg ) {
|
||||
repMinGenerations = _mg;
|
||||
repSteadyGenerations = _sg;
|
||||
thisGeneration = 0;
|
||||
steadyState = false;
|
||||
};
|
||||
|
||||
/** accessors*/
|
||||
virtual unsigned long minGenerations( )
|
||||
{ return repMinGenerations; };
|
||||
virtual unsigned long steadyGenerations( )
|
||||
{ return repSteadyGenerations; };
|
||||
|
||||
private:
|
||||
unsigned long repMinGenerations;
|
||||
unsigned long repSteadyGenerations;
|
||||
bool steadyState;
|
||||
unsigned long thisGenerationPlaceHolder;
|
||||
unsigned long& thisGeneration;
|
||||
unsigned int lastImprovement;
|
||||
Fitness bestSoFar;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in a new issue