EvolvingObjects
make_general_replacement.h
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // make_general_replacement.h
00005 // (c) Marc Schoenauer and Pierre Collet, 2002
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_general_replacement_h
00028 #define _make_general_replacement_h
00029 
00030 #include <utils/eoData.h>     // for eo_is_a_rate
00031 
00032 // Replacement
00033 #include <eoReduceMergeReduce.h>
00034 
00035 // also need the parser and param includes
00036 #include <utils/eoParser.h>
00037 #include <utils/eoState.h>
00038 
00039 
00045 template <class EOT>
00046 eoReduce<EOT> & decode_reduce(eoParamParamType & _ppReduce, eoState & _state)
00047 {
00048   unsigned int detSize;
00049   eoReduce<EOT> * ptReduce;
00050 
00051   // ---------- Deterministic
00052   if ( (_ppReduce.first == std::string("Deterministic")) ||
00053        (_ppReduce.first == std::string("Sequential"))
00054        )
00055   {
00056     ptReduce = new eoTruncate<EOT>;
00057   }
00058   // ---------- EP
00059   else if (_ppReduce.first == std::string("EP"))
00060   {
00061     if (!_ppReduce.second.size())   // no parameter added
00062     {
00063       std::cerr << "WARNING, no parameter passed to EP, using 6" << std::endl;
00064       detSize = 6;
00065       // put back 6 in parameter for consistency (and status file)
00066       _ppReduce.second.push_back(std::string("6"));
00067     }
00068     else          // parameter passed by user as EP(T)
00069       detSize = atoi(_ppReduce.second[0].c_str());
00070     ptReduce = new eoEPReduce<EOT>(detSize);
00071   }
00072   // ---------- DetTour
00073   else if (_ppReduce.first == std::string("DetTour"))
00074   {
00075     if (!_ppReduce.second.size())   // no parameter added
00076       {
00077         std::cerr << "WARNING, no parameter passed to DetTour, using 2" << std::endl;
00078         detSize = 2;
00079         // put back 2 in parameter for consistency (and status file)
00080         _ppReduce.second.push_back(std::string("2"));
00081       }
00082     else          // parameter passed by user as DetTour(T)
00083       detSize = atoi(_ppReduce.second[0].c_str());
00084     ptReduce = new eoDetTournamentTruncate<EOT>(detSize);
00085   }
00086   else if (_ppReduce.first == std::string("StochTour"))
00087     {
00088       double p;
00089       if (!_ppReduce.second.size())   // no parameter added
00090         {
00091           std::cerr << "WARNING, no parameter passed to StochTour, using 1" << std::endl;
00092           p = 1;
00093           // put back p in parameter for consistency (and status file)
00094           _ppReduce.second.push_back(std::string("1"));
00095         }
00096       else        // parameter passed by user as DetTour(T)
00097         {
00098           p = atof(_ppReduce.second[0].c_str());
00099           if ( (p<=0.5) || (p>1) )
00100             throw std::runtime_error("Stochastic tournament size should be in [0.5,1]");
00101         }
00102 
00103       ptReduce = new eoStochTournamentTruncate<EOT>(p);
00104     }
00105   else if ( (_ppReduce.first == std::string("Uniform")) ||
00106             (_ppReduce.first == std::string("Random"))
00107             )
00108     {
00109       ptReduce = new eoRandomReduce<EOT>;
00110     }
00111   else // no known reduction entered
00112     {
00113       throw std::runtime_error("Unknown reducer: " + _ppReduce.first);
00114     }
00115   // all done, stores and return a reference
00116   _state.storeFunctor(ptReduce);
00117   return (*ptReduce);
00118 }
00119 
00137 template <class EOT>
00138 eoReplacement<EOT> & make_general_replacement(
00139     eoParser& _parser, eoState& _state,
00140     eoHowMany _elite = eoHowMany(0),
00141     bool _strongElitism = false,
00142     eoHowMany _surviveParents = eoHowMany(0.0),
00143     eoParamParamType & _reduceParentType = eoParamParamType("Deterministic"),
00144     eoHowMany _surviveOffspring = eoHowMany(1.0),
00145     eoParamParamType & _reduceOffspringType = eoParamParamType("Deterministic"),
00146     eoParamParamType & _reduceFinalType  = eoParamParamType("Deterministic")
00147         )
00148 {
00150   // the replacement
00152 
00153   // Elitism
00154     eoHowMany elite =  _parser.createParam(_elite, "elite", "Nb of elite parents (percentage or absolute)", '\0', "Evolution Engine / Replacement").value();
00155 
00156     bool strongElitism = _parser.createParam(_strongElitism,"eliteType", "Strong (true) or weak (false) elitism (set elite to 0 for none)", '\0', "Evolution Engine / Replacement").value();
00157 
00158   // reduce the parents
00159     eoHowMany surviveParents =  _parser.createParam(_surviveParents, "surviveParents", "Nb of surviving parents (percentage or absolute)", '\0', "Evolution Engine / Replacement").value();
00160 
00161   eoParamParamType & reduceParentType = _parser.createParam(_reduceParentType, "reduceParents", "Parents reducer: Deterministic, EP(T), DetTour(T), StochTour(t), Uniform", '\0', "Evolution Engine / Replacement").value();
00162 
00163   eoReduce<EOT> & reduceParent = decode_reduce<EOT>(reduceParentType, _state);
00164 
00165   // reduce the offspring
00166     eoHowMany surviveOffspring =  _parser.createParam(_surviveOffspring, "surviveOffspring", "Nb of surviving offspring (percentage or absolute)", '\0', "Evolution Engine / Replacement").value();
00167 
00168   eoParamParamType & reduceOffspringType = _parser.createParam(_reduceOffspringType, "reduceOffspring", "Offspring reducer: Deterministic, EP(T), DetTour(T), StochTour(t), Uniform", '\0', "Evolution Engine / Replacement").value();
00169 
00170   eoReduce<EOT> & reduceOffspring = decode_reduce<EOT>(reduceOffspringType, _state);
00171 
00172   eoParamParamType & reduceFinalType = _parser.createParam(_reduceFinalType, "reduceFinal", "Final reducer: Deterministic, EP(T), DetTour(T), StochTour(t), Uniform", '\0', "Evolution Engine / Replacement").value();
00173 
00174   eoReduce<EOT> & reduceFinal = decode_reduce<EOT>(reduceFinalType, _state);
00175 
00176   // now the replacement itself
00177   eoReduceMergeReduce<EOT> *ptReplace = new eoReduceMergeReduce<EOT>(elite, strongElitism, surviveParents, reduceParent, surviveOffspring, reduceOffspring, reduceFinal);
00178   _state.storeFunctor(ptReplace);
00179 
00180   // that's it!
00181   return *ptReplace;
00182 }
00183 
00184 #endif
 All Classes Namespaces Files Functions Variables Typedefs Friends