EvolvingObjects
|
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- 00002 00003 //----------------------------------------------------------------------------- 00004 // eoSteadyFitContinue.h 00005 // (c) GeNeura Team, 1999, Marc Schoenauer, 2000 00006 /* 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Lesser General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public 00018 License along with this library; if not, write to the Free Software 00019 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 00021 Contact: todos@geneura.ugr.es, http://geneura.ugr.es 00022 */ 00023 //----------------------------------------------------------------------------- 00024 00025 #ifndef _eoSteadyFitContinue_h 00026 #define _eoSteadyFitContinue_h 00027 00028 #include <eoContinue.h> 00029 #include <utils/eoLogger.h> 00030 00037 template< class EOT> 00038 class eoSteadyFitContinue: public eoContinue<EOT> 00039 { 00040 public: 00041 typedef typename EOT::Fitness Fitness; 00042 00044 eoSteadyFitContinue( unsigned long _minGens, unsigned long _steadyGens) 00045 : repMinGenerations( _minGens ), repSteadyGenerations( _steadyGens), 00046 steadyState(false), thisGenerationPlaceHolder(0), 00047 thisGeneration(thisGenerationPlaceHolder){}; 00048 00050 eoSteadyFitContinue( unsigned long _minGens, unsigned long _steadyGen, 00051 unsigned long& _currentGen) 00052 : repMinGenerations( _minGens ), repSteadyGenerations( _steadyGen), 00053 steadyState(_currentGen>_minGens), thisGenerationPlaceHolder(0), 00054 thisGeneration(_currentGen){}; 00055 00058 virtual bool operator() ( const eoPop<EOT>& _vEO ) { 00059 thisGeneration++; 00060 00061 Fitness bestCurrentFitness = _vEO.nth_element_fitness(0); 00062 00063 if (steadyState) { // already after MinGenenerations 00064 if (bestCurrentFitness > bestSoFar) { 00065 bestSoFar = bestCurrentFitness; 00066 lastImprovement = thisGeneration; 00067 } else { 00068 if (thisGeneration - lastImprovement > repSteadyGenerations) { 00069 eo::log << eo::progress << "STOP in eoSteadyFitContinue: Done " << repSteadyGenerations 00070 << " generations without improvement\n"; 00071 return false; 00072 } 00073 } 00074 } else { // not yet in steady state 00075 if (thisGeneration > repMinGenerations) { // go to steady state 00076 steadyState = true; 00077 bestSoFar = bestCurrentFitness; 00078 lastImprovement = thisGeneration; 00079 eo::log << eo::progress << "eoSteadyFitContinue: Done the minimum number of generations\n"; 00080 } 00081 } 00082 return true; 00083 } 00084 00090 virtual void totalGenerations( unsigned long _mg, unsigned long _sg ) { 00091 repMinGenerations = _mg; 00092 repSteadyGenerations = _sg; 00093 reset(); 00094 }; 00095 00097 virtual void reset () { 00098 steadyState=false; 00099 thisGeneration = 0; 00100 } 00101 00103 virtual unsigned long minGenerations( ) 00104 { return repMinGenerations; }; 00105 virtual unsigned long steadyGenerations( ) 00106 { return repSteadyGenerations; }; 00107 00108 virtual std::string className(void) const { return "eoSteadyFitContinue"; } 00109 private: 00110 unsigned long repMinGenerations; 00111 unsigned long repSteadyGenerations; 00112 bool steadyState; 00113 unsigned long thisGenerationPlaceHolder; 00114 unsigned long& thisGeneration; 00115 unsigned int lastImprovement; 00116 Fitness bestSoFar; 00117 }; 00118 00119 #endif