# make_continue.h

    00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
    00002
    00003 //-----------------------------------------------------------------------------
    00004 // make_continue.h
    00005 // (c) Maarten Keijzer, Marc Schoenauer and GeNeura Team, 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              Marc.Schoenauer@polytechnique.fr
    00023              mkeijzer@dhi.dk
    00024  */
    00025 //-----------------------------------------------------------------------------
    00026
    00027 #ifndef _make_continue_h
    00028 #define _make_continue_h
    00029
    00030 /*
    00031 Contains the templatized version of parser-based choice of stopping criterion
    00032 It can then be instantiated, and compiled on its own for a given EOType
    00033 (see e.g. in dir ga, ga.cpp)
    00034 */
    00035
    00036 // Continuators - all include eoContinue.h
    00037 #include <eoCombinedContinue.h>
    00038 #include <eoGenContinue.h>
    00039 #include <eoSteadyFitContinue.h>
    00040 #include <eoEvalContinue.h>
    00041 #include <eoFitContinue.h>
    00042 #ifndef _MSC_VER
    00043 #include <eoCtrlCContinue.h>  // CtrlC handling (using 2 global variables!)
    00044 #endif
    00045
    00046   // also need the parser and param includes
    00047 #include <utils/eoParser.h>
    00048 #include <utils/eoState.h>
    00049
    00050
    00052 template <class Indi>
    00053 eoCombinedContinue<Indi> * make_combinedContinue(eoCombinedContinue<Indi> *_combined, eoContinue<Indi> *_cont)
    00054 {
    00055   if (_combined)                   // already exists
    00056     _combined->add(*_cont);
    00057   else
    00058     _combined = new eoCombinedContinue<Indi>(*_cont);
    00059   return _combined;
    00060 }
    00061
    00062 template <class Indi>
    00063 eoContinue<Indi> & do_make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter<Indi> & _eval)
    00064 {
    00066   // the combined continue - to be filled
    00067   eoCombinedContinue<Indi> *continuator = NULL;
    00068
    00069   // for each possible criterion, check if wanted, otherwise do nothing
    00070
    00071   // First the eoGenContinue - need a default value so you can run blind
    00072   // but we also need to be able to avoid it <--> 0
    00073   eoValueParam<unsigned>& maxGenParam = _parser.getORcreateParam(unsigned(100), "maxGen", "Maximum number of generations () = none)",'G',"Stopping criterion");
    00074
    00075     if (maxGenParam.value()) // positive: -> define and store
    00076       {
    00077         eoGenContinue<Indi> *genCont = new eoGenContinue<Indi>(maxGenParam.value());
    00078         _state.storeFunctor(genCont);
    00079         // and "add" to combined
    00080         continuator = make_combinedContinue<Indi>(continuator, genCont);
    00081       }
    00082
    00083   // the steadyGen continue - only if user imput
    00084   eoValueParam<unsigned>& steadyGenParam = _parser.createParam(unsigned(100), "steadyGen", "Number of generations with no improvement",'s', "Stopping criterion");
    00085   eoValueParam<unsigned>& minGenParam = _parser.createParam(unsigned(0), "minGen", "Minimum number of generations",'g', "Stopping criterion");
    00086     if (_parser.isItThere(steadyGenParam))
    00087       {
    00088         eoSteadyFitContinue<Indi> *steadyCont = new eoSteadyFitContinue<Indi>
    00089           (minGenParam.value(), steadyGenParam.value());
    00090         // store
    00091         _state.storeFunctor(steadyCont);
    00092         // add to combinedContinue
    00093         continuator = make_combinedContinue<Indi>(continuator, steadyCont);
    00094       }
    00095
    00096   // Same thing with Eval - but here default value is 0
    00097   eoValueParam<unsigned long>& maxEvalParam
    00098       = _parser.getORcreateParam((unsigned long)0, "maxEval",
    00099                                  "Maximum number of evaluations (0 = none)",
    00100                                  'E', "Stopping criterion");
    00101
    00102     if (maxEvalParam.value()) // positive: -> define and store
    00103       {
    00104         eoEvalContinue<Indi> *evalCont = new eoEvalContinue<Indi>(_eval, maxEvalParam.value());
    00105         _state.storeFunctor(evalCont);
    00106         // and "add" to combined
    00107         continuator = make_combinedContinue<Indi>(continuator, evalCont);
    00108       }
    00109     /*
    00110   // the steadyEval continue - only if user imput
    00111   eoValueParam<unsigned>& steadyGenParam = _parser.createParam(unsigned(100), "steadyGen", "Number of generations with no improvement",'s', "Stopping criterion");
    00112   eoValueParam<unsigned>& minGenParam = _parser.createParam(unsigned(0), "minGen", "Minimum number of generations",'g', "Stopping criterion");
    00113     if (_parser.isItThere(steadyGenParam))
    00114       {
    00115         eoSteadyGenContinue<Indi> *steadyCont = new eoSteadyFitContinue<Indi>
    00116           (minGenParam.value(), steadyGenParam.value());
    00117         // store
    00118         _state.storeFunctor(steadyCont);
    00119         // add to combinedContinue
    00120         continuator = make_combinedContinue<Indi>(continuator, steadyCont);
    00121       }
    00122     */
    00123     // the target fitness
    00124     eoFitContinue<Indi> *fitCont;
    00125     eoValueParam<double>& targetFitnessParam = _parser.createParam(double(0.0), "targetFitness", "Stop when fitness reaches",'T', "Stopping criterion");
    00126     if (_parser.isItThere(targetFitnessParam))
    00127       {
    00128         fitCont = new eoFitContinue<Indi>
    00129           (targetFitnessParam.value());
    00130         // store
    00131         _state.storeFunctor(fitCont);
    00132         // add to combinedContinue
    00133         continuator = make_combinedContinue<Indi>(continuator, fitCont);
    00134       }
    00135
    00136 #ifndef _MSC_VER
    00137     // the CtrlC interception (Linux only I'm afraid)
    00138     eoCtrlCContinue<Indi> *ctrlCCont;
    00139     eoValueParam<bool>& ctrlCParam = _parser.createParam(false, "CtrlC", "Terminate current generation upon Ctrl C",'C', "Stopping criterion");
    00140     if (ctrlCParam.value())
    00141       {
    00142         ctrlCCont = new eoCtrlCContinue<Indi>;
    00143         // store
    00144         _state.storeFunctor(ctrlCCont);
    00145         // add to combinedContinue
    00146         continuator = make_combinedContinue<Indi>(continuator, ctrlCCont);
    00147       }
    00148 #endif
    00149
    00150     // now check that there is at least one!
    00151     if (!continuator)
    00152       throw std::runtime_error("You MUST provide a stopping criterion");
    00153   // OK, it's there: store in the eoState
    00154   _state.storeFunctor(continuator);
    00155
    00156   // and return
    00157     return *continuator;
    00158 }
    00159
    00160 #endif