# eoSteadyFitContinue.h

    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
    00034 template< class EOT>
    00035 class eoSteadyFitContinue: public eoContinue<EOT>
    00036 {
    00037 public:
    00038   typedef typename EOT::Fitness Fitness;
    00039
    00041   eoSteadyFitContinue( unsigned long _minGens, unsigned long _steadyGens)
    00042     : repMinGenerations( _minGens ), repSteadyGenerations( _steadyGens),
    00043       steadyState(false), thisGenerationPlaceHolder(0),
    00044       thisGeneration(thisGenerationPlaceHolder){};
    00045
    00047   eoSteadyFitContinue( unsigned long _minGens, unsigned long _steadyGen,
    00048                  unsigned long& _currentGen)
    00049     : repMinGenerations( _minGens ), repSteadyGenerations( _steadyGen),
    00050       steadyState(_currentGen>_minGens), thisGenerationPlaceHolder(0),
    00051       thisGeneration(_currentGen){};
    00052
    00055   virtual bool operator() ( const eoPop<EOT>& _vEO ) {
    00056     thisGeneration++;
    00057     //    std::cout << " [" << thisGeneration << "] ";
    00058     Fitness bestCurrentFitness = _vEO.nth_element_fitness(0);
    00059
    00060     if (steadyState) {     // already after MinGenenerations
    00061       if (bestCurrentFitness > bestSoFar) {
    00062         bestSoFar = bestCurrentFitness;
    00063         lastImprovement = thisGeneration;
    00064       } else {
    00065         if (thisGeneration - lastImprovement > repSteadyGenerations) {
    00066           std::cout << "STOP in eoSteadyFitContinue: Done " << repSteadyGenerations
    00067                << " generations without improvement\n";
    00068           return false;
    00069         }
    00070       }
    00071     } else {               // not yet in steady state
    00072       if (thisGeneration > repMinGenerations) { // go to steady state
    00073         steadyState = true;
    00074         bestSoFar = bestCurrentFitness;
    00075         lastImprovement = thisGeneration;
    00076         std::cout << "eoSteadyFitContinue: Done the minimum number of generations\n";
    00077       }
    00078     }
    00079     return true;
    00080   }
    00081
    00084   virtual void totalGenerations( unsigned long _mg, unsigned long _sg ) {
    00085     repMinGenerations = _mg;
    00086     repSteadyGenerations = _sg;
    00087     reset();
    00088   };
    00089
    00091   virtual void reset () {
    00092     steadyState=false;
    00093     thisGeneration = 0;
    00094   }
    00095
    00097   virtual unsigned long minGenerations( )
    00098   {  return repMinGenerations;  };
    00099   virtual unsigned long steadyGenerations( )
    00100   {  return repSteadyGenerations;       };
    00101
    00102   virtual std::string className(void) const { return "eoSteadyFitContinue"; }
    00103 private:
    00104   unsigned long repMinGenerations;
    00105   unsigned long  repSteadyGenerations;
    00106   bool steadyState;
    00107   unsigned long thisGenerationPlaceHolder;
    00108   unsigned long& thisGeneration;
    00109   unsigned int lastImprovement;
    00110   Fitness bestSoFar;
    00111 };
    00112
    00113 #endif
    00114