From a14526e01af4f349293e1e7ea44159477216646d Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Thu, 26 Jul 2012 15:12:54 +0200 Subject: [PATCH] eoGenContinue and eoSteadyFitContinue now inherits from a common class eoCountContinue, which contains a overridable method reset. --- eo/src/eoContinue.h | 39 ++++++++++++++++++++++++++++++++++++ eo/src/eoGenContinue.h | 23 +++++++++------------ eo/src/eoSteadyFitContinue.h | 21 ++++++++++--------- 3 files changed, 60 insertions(+), 23 deletions(-) diff --git a/eo/src/eoContinue.h b/eo/src/eoContinue.h index dbbf24805..82544115b 100644 --- a/eo/src/eoContinue.h +++ b/eo/src/eoContinue.h @@ -67,4 +67,43 @@ public: } }; +/** + * Termination condition with a count condition (totalGenerations). This continuator contains + * a count of cycles, which can be retrieved or set. + * + * @ingroup Continuators + * @ingroup Core + */ +template< class EOT > +class eoCountContinue : public eoContinue< EOT > +{ + public: + + eoCountContinue( ) : + thisGenerationPlaceholder( 0 ), + thisGeneration( thisGenerationPlaceholder ) + { + // empty + } + + eoCountContinue( unsigned long& currentGen ) : + thisGenerationPlaceholder( 0 ), + thisGeneration( currentGen ) + { + // empty + } + + virtual std::string className( void ) const { return "eoCountContinue"; } + + virtual void reset( ) + { + thisGeneration = 0; + } + + protected: + + unsigned long thisGenerationPlaceholder; + unsigned long& thisGeneration; +}; + #endif diff --git a/eo/src/eoGenContinue.h b/eo/src/eoGenContinue.h index 0d01bb0a1..dc756795d 100644 --- a/eo/src/eoGenContinue.h +++ b/eo/src/eoGenContinue.h @@ -35,24 +35,24 @@ @ingroup Continuators */ template< class EOT> -class eoGenContinue: public eoContinue, public eoValueParam +class eoGenContinue: public eoCountContinue, public eoValueParam { public: + using eoCountContinue::thisGeneration; + using eoCountContinue::thisGenerationPlaceholder; + /// Ctor for setting a eoGenContinue( unsigned long _totalGens) - : eoValueParam(0, "Generations", "Generations"), - repTotalGenerations( _totalGens ), - thisGenerationPlaceHolder(0), - thisGeneration(thisGenerationPlaceHolder) + : eoCountContinue( ), + eoValueParam(0, "Generations", "Generations"), + repTotalGenerations( _totalGens ) {}; /// Ctor for enabling the save/load the no. of generations counted eoGenContinue( unsigned long _totalGens, unsigned long& _currentGen) - : eoValueParam(0, "Generations", "Generations"), - repTotalGenerations( _totalGens ), - thisGenerationPlaceHolder(0), - thisGeneration(_currentGen) + : eoCountContinue( _currentGen ), eoValueParam(0, "Generations", "Generations"), + repTotalGenerations( _totalGens ) {}; /** Returns false when a certain number of generations is @@ -77,7 +77,7 @@ public: */ virtual void totalGenerations( unsigned long _tg ) { repTotalGenerations = _tg; - thisGeneration = 0; + eoCountContinue::reset(); }; /** Returns the number of generations to reach*/ @@ -86,7 +86,6 @@ public: return repTotalGenerations; }; - virtual std::string className(void) const { return "eoGenContinue"; } /** Read from a stream @@ -107,8 +106,6 @@ public: private: unsigned long repTotalGenerations; - unsigned long thisGenerationPlaceHolder; - unsigned long& thisGeneration; }; #endif diff --git a/eo/src/eoSteadyFitContinue.h b/eo/src/eoSteadyFitContinue.h index 229d93bc1..f05697b50 100644 --- a/eo/src/eoSteadyFitContinue.h +++ b/eo/src/eoSteadyFitContinue.h @@ -35,23 +35,26 @@ @ingroup Continuators */ template< class EOT> -class eoSteadyFitContinue: public eoContinue +class eoSteadyFitContinue: public eoCountContinue { public: typedef typename EOT::Fitness Fitness; + using eoCountContinue::thisGenerationPlaceholder; + using eoCountContinue::thisGeneration; + /// Ctor for setting a eoSteadyFitContinue( unsigned long _minGens, unsigned long _steadyGens) - : repMinGenerations( _minGens ), repSteadyGenerations( _steadyGens), - steadyState(false), thisGenerationPlaceHolder(0), - thisGeneration(thisGenerationPlaceHolder){}; + : eoCountContinue( ), repMinGenerations( _minGens ), repSteadyGenerations( _steadyGens), + steadyState(false) + {}; /// 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){}; + : eoCountContinue( _currentGen ), repMinGenerations( _minGens ), repSteadyGenerations( _steadyGen), + steadyState(_currentGen>_minGens) + {}; /** Returns false when a certain number of generations is * reached withtout improvement */ @@ -96,7 +99,7 @@ public: /// Resets the state after it's been reached virtual void reset () { steadyState=false; - thisGeneration = 0; + eoCountContinue::reset(); } /** accessors*/ @@ -110,8 +113,6 @@ private: unsigned long repMinGenerations; unsigned long repSteadyGenerations; bool steadyState; - unsigned long thisGenerationPlaceHolder; - unsigned long& thisGeneration; unsigned int lastImprovement; Fitness bestSoFar; };