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