EvolvingObjects
|
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 00055 template <class Indi> 00056 eoCombinedContinue<Indi> * make_combinedContinue(eoCombinedContinue<Indi> *_combined, eoContinue<Indi> *_cont) 00057 { 00058 if (_combined) // already exists 00059 _combined->add(*_cont); 00060 else 00061 _combined = new eoCombinedContinue<Indi>(*_cont); 00062 return _combined; 00063 } 00064 00069 template <class Indi> 00070 eoContinue<Indi> & do_make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter<Indi> & _eval) 00071 { 00073 // the combined continue - to be filled 00074 eoCombinedContinue<Indi> *continuator = NULL; 00075 00076 // for each possible criterion, check if wanted, otherwise do nothing 00077 00078 // First the eoGenContinue - need a default value so you can run blind 00079 // but we also need to be able to avoid it <--> 0 00080 eoValueParam<unsigned>& maxGenParam = _parser.getORcreateParam(unsigned(100), "maxGen", "Maximum number of generations () = none)",'G',"Stopping criterion"); 00081 00082 if (maxGenParam.value()) // positive: -> define and store 00083 { 00084 eoGenContinue<Indi> *genCont = new eoGenContinue<Indi>(maxGenParam.value()); 00085 _state.storeFunctor(genCont); 00086 // and "add" to combined 00087 continuator = make_combinedContinue<Indi>(continuator, genCont); 00088 } 00089 00090 // the steadyGen continue - only if user imput 00091 eoValueParam<unsigned>& steadyGenParam = _parser.createParam(unsigned(100), "steadyGen", "Number of generations with no improvement",'s', "Stopping criterion"); 00092 eoValueParam<unsigned>& minGenParam = _parser.createParam(unsigned(0), "minGen", "Minimum number of generations",'g', "Stopping criterion"); 00093 if (_parser.isItThere(steadyGenParam)) 00094 { 00095 eoSteadyFitContinue<Indi> *steadyCont = new eoSteadyFitContinue<Indi> 00096 (minGenParam.value(), steadyGenParam.value()); 00097 // store 00098 _state.storeFunctor(steadyCont); 00099 // add to combinedContinue 00100 continuator = make_combinedContinue<Indi>(continuator, steadyCont); 00101 } 00102 00103 // Same thing with Eval - but here default value is 0 00104 eoValueParam<unsigned long>& maxEvalParam 00105 = _parser.getORcreateParam((unsigned long)0, "maxEval", 00106 "Maximum number of evaluations (0 = none)", 00107 'E', "Stopping criterion"); 00108 00109 if (maxEvalParam.value()) // positive: -> define and store 00110 { 00111 eoEvalContinue<Indi> *evalCont = new eoEvalContinue<Indi>(_eval, maxEvalParam.value()); 00112 _state.storeFunctor(evalCont); 00113 // and "add" to combined 00114 continuator = make_combinedContinue<Indi>(continuator, evalCont); 00115 } 00116 /* 00117 // the steadyEval continue - only if user imput 00118 eoValueParam<unsigned>& steadyGenParam = _parser.createParam(unsigned(100), "steadyGen", "Number of generations with no improvement",'s', "Stopping criterion"); 00119 eoValueParam<unsigned>& minGenParam = _parser.createParam(unsigned(0), "minGen", "Minimum number of generations",'g', "Stopping criterion"); 00120 if (_parser.isItThere(steadyGenParam)) 00121 { 00122 eoSteadyGenContinue<Indi> *steadyCont = new eoSteadyFitContinue<Indi> 00123 (minGenParam.value(), steadyGenParam.value()); 00124 // store 00125 _state.storeFunctor(steadyCont); 00126 // add to combinedContinue 00127 continuator = make_combinedContinue<Indi>(continuator, steadyCont); 00128 } 00129 */ 00130 // the target fitness 00131 eoFitContinue<Indi> *fitCont; 00132 eoValueParam<double>& targetFitnessParam = _parser.createParam(double(0.0), "targetFitness", "Stop when fitness reaches",'T', "Stopping criterion"); 00133 if (_parser.isItThere(targetFitnessParam)) 00134 { 00135 fitCont = new eoFitContinue<Indi> 00136 (targetFitnessParam.value()); 00137 // store 00138 _state.storeFunctor(fitCont); 00139 // add to combinedContinue 00140 continuator = make_combinedContinue<Indi>(continuator, fitCont); 00141 } 00142 00143 #ifndef _MSC_VER 00144 // the CtrlC interception (Linux only I'm afraid) 00145 eoCtrlCContinue<Indi> *ctrlCCont; 00146 eoValueParam<bool>& ctrlCParam = _parser.createParam(false, "CtrlC", "Terminate current generation upon Ctrl C",'C', "Stopping criterion"); 00147 if (ctrlCParam.value()) 00148 { 00149 ctrlCCont = new eoCtrlCContinue<Indi>; 00150 // store 00151 _state.storeFunctor(ctrlCCont); 00152 // add to combinedContinue 00153 continuator = make_combinedContinue<Indi>(continuator, ctrlCCont); 00154 } 00155 #endif 00156 00157 // now check that there is at least one! 00158 if (!continuator) 00159 throw std::runtime_error("You MUST provide a stopping criterion"); 00160 // OK, it's there: store in the eoState 00161 _state.storeFunctor(continuator); 00162 00163 // and return 00164 return *continuator; 00165 } 00166 00167 #endif