Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | Related Pages | Search for

# ga/make_op.h

    00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
    00002
    00003 //-----------------------------------------------------------------------------
    00004 // make_op.h
    00005 // (c) Maarten Keijzer, Marc Schoenauer and GeNeura Team, 2001
    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_op_h
    00028 #define _make_op_h
    00029
    00030 // the operators
    00031 #include <eoOp.h>
    00032 #include <eoGenOp.h>
    00033 #include <eoCloneOps.h>
    00034 #include <eoOpContainer.h>
    00035 // combinations of simple eoOps (eoMonOp and eoQuadOp)
    00036 #include <eoProportionalCombinedOp.h>
    00037
    00038 // the specialized GA stuff
    00039 #include <ga/eoBit.h>
    00040 #include <ga/eoBitOp.h>
    00041   // also need the parser and param includes
    00042 #include <utils/eoParser.h>
    00043 #include <utils/eoState.h>
    00044
    00045
    00047 // canonical (crossover + mutation) only at the moment //
    00048
    00049 /*
    00050  * This function builds the operators that will be applied to the bitstrings
    00051  *
    00052  * It uses a parser (to get user parameters) and a state (to store the memory)
    00053  * the last argument is an individual, needed for 2 reasons
    00054  *     it disambiguates the call after instanciations
    00055  *     some operator might need some private information about the indis
    00056  *
    00057  * This is why the template is the complete EOT even though only the fitness
    00058  * is actually templatized here: the following only applies to bitstrings
    00059  *
    00060  * Note : the last parameter is an eoInit: if some operator needs some info
    00061  *        about the gneotypes, the init has it all (e.g. bounds, ...)
    00062  *        Simply do
    00063  *        EOT myEO;
    00064  *        _init(myEO);
    00065  *        and myEO is then an ACTUAL object
    00066 */
    00067
    00068 template <class EOT>
    00069 eoGenOp<EOT> & do_make_op(eoParser& _parser, eoState& _state, eoInit<EOT>& _init)
    00070 {
    00071   // this is a temporary version, while Maarten codes the full tree-structured
    00072   // general operator input
    00073   // BTW we must leave that simple version available somehow, as it is the one
    00074   // that 90% people use!
    00075     eoValueParam<std::string>& operatorParam =  _parser.createParam(std::string("SGA"), "operator", "Description of the operator (SGA only now)", 'o', "Variation Operators");
    00076
    00077     if (operatorParam.value() != std::string("SGA"))
    00078         throw std::runtime_error("Only SGA-like operator available right now\n");
    00079
    00080     // now we read Pcross and Pmut,
    00081     // the relative weights for all crossovers -> proportional choice
    00082     // the relative weights for all mutations -> proportional choice
    00083     // and create the eoGenOp that is exactly
    00084     // crossover with pcross + mutation with pmut
    00085
    00086     eoValueParam<double>& pCrossParam = _parser.createParam(0.6, "pCross", "Probability of Crossover", 'C', "Variation Operators" );
    00087     // minimum check
    00088     if ( (pCrossParam.value() < 0) || (pCrossParam.value() > 1) )
    00089       throw std::runtime_error("Invalid pCross");
    00090
    00091     eoValueParam<double>& pMutParam = _parser.createParam(0.1, "pMut", "Probability of Mutation", 'M', "Variation Operators" );
    00092     // minimum check
    00093     if ( (pMutParam.value() < 0) || (pMutParam.value() > 1) )
    00094       throw std::runtime_error("Invalid pMut");
    00095
    00096     // the crossovers
    00098     // the parameters
    00099     eoValueParam<double>& onePointRateParam = _parser.createParam(double(1.0), "onePointRate", "Relative rate for one point crossover", '1', "Variation Operators" );
    00100     // minimum check
    00101     if ( (onePointRateParam.value() < 0) )
    00102       throw std::runtime_error("Invalid onePointRate");
    00103
    00104     eoValueParam<double>& twoPointsRateParam = _parser.createParam(double(1.0), "twoPointRate", "Relative rate for two point crossover", '2', "Variation Operators" );
    00105     // minimum check
    00106     if ( (twoPointsRateParam.value() < 0) )
    00107       throw std::runtime_error("Invalid twoPointsRate");
    00108
    00109     eoValueParam<double>& uRateParam = _parser.createParam(double(2.0), "uRate", "Relative rate for uniform crossover", 'U', "Variation Operators" );
    00110     // minimum check
    00111     if ( (uRateParam.value() < 0) )
    00112       throw std::runtime_error("Invalid uRate");
    00113
    00114     // minimum check
    00115     bool bCross = true;
    00116     if (onePointRateParam.value()+twoPointsRateParam.value()+uRateParam.value()==0)
    00117       {
    00118         std::cerr << "Warning: no crossover" << std::endl;
    00119         bCross = false;
    00120       }
    00121
    00122     // Create the CombinedQuadOp
    00123     eoPropCombinedQuadOp<EOT> *ptCombinedQuadOp = NULL;
    00124     eoQuadOp<EOT> *ptQuad = NULL;
    00125     // 1-point crossover for bitstring
    00126     ptQuad = new eo1PtBitXover<EOT>;
    00127     _state.storeFunctor(ptQuad);
    00128     ptCombinedQuadOp = new eoPropCombinedQuadOp<EOT>(*ptQuad, onePointRateParam.value());
    00129
    00130     // uniform crossover for bitstring
    00131     ptQuad = new eoUBitXover<EOT>;
    00132     _state.storeFunctor(ptQuad);
    00133     ptCombinedQuadOp->add(*ptQuad, uRateParam.value());
    00134
    00135     // 2-points xover
    00136     ptQuad = new eoNPtsBitXover<EOT>;
    00137     _state.storeFunctor(ptQuad);
    00138     ptCombinedQuadOp->add(*ptQuad, twoPointsRateParam.value());
    00139
    00140     // don't forget to store the CombinedQuadOp
    00141     _state.storeFunctor(ptCombinedQuadOp);
    00142
    00143     // the mutations
    00145     // the parameters
    00146     eoValueParam<double> & pMutPerBitParam = _parser.createParam(0.01, "pMutPerBit", "Probability of flipping 1 bit in bit-flip mutation", 'b', "Variation Operators" );
    00147     // minimum check
    00148     if ( (pMutPerBitParam.value() < 0) || (pMutPerBitParam.value() > 0.5) )
    00149       throw std::runtime_error("Invalid pMutPerBit");
    00150
    00151     eoValueParam<double> & bitFlipRateParam = _parser.createParam(0.01, "bitFlipRate", "Relative rate for bit-flip mutation", 's', "Variation Operators" );
    00152     // minimum check
    00153     if ( (bitFlipRateParam.value() < 0) )
    00154       throw std::runtime_error("Invalid bitFlipRate");
    00155
    00156     eoValueParam<double> & oneBitRateParam = _parser.createParam(0.01, "oneBitRate", "Relative rate for deterministic bit-flip mutation", 'd', "Variation Operators" );
    00157     // minimum check
    00158     if ( (oneBitRateParam.value() < 0) )
    00159       throw std::runtime_error("Invalid oneBitRate");
    00160
    00161     // minimum check
    00162     bool bMut = true;
    00163     if (bitFlipRateParam.value()+oneBitRateParam.value()==0)
    00164       {
    00165         std::cerr << "Warning: no mutation" << std::endl;
    00166         bMut = false;
    00167       }
    00168
    00169     // Create the CombinedMonOp
    00170     eoPropCombinedMonOp<EOT> *ptCombinedMonOp = NULL;
    00171     eoMonOp<EOT> *ptMon = NULL;
    00172
    00173   // standard bit-flip mutation for bitstring
    00174   ptMon = new eoBitMutation<EOT>(pMutPerBitParam.value());
    00175   _state.storeFunctor(ptMon);
    00176   // create the CombinedMonOp
    00177   ptCombinedMonOp = new eoPropCombinedMonOp<EOT>(*ptMon, bitFlipRateParam.value());
    00178
    00179   // mutate exactly 1 bit per individual
    00180   ptMon = new eoDetBitFlip<EOT>;
    00181   _state.storeFunctor(ptMon);
    00182   ptCombinedMonOp->add(*ptMon, oneBitRateParam.value());
    00183
    00184   _state.storeFunctor(ptCombinedMonOp);
    00185
    00186   // now build the eoGenOp:
    00187   // to simulate SGA (crossover with proba pCross + mutation with proba pMut
    00188   // we must construct
    00189   //     a sequential combination of
    00190   //          with proba 1, a proportional combination of
    00191   //                        a QuadCopy and our crossover
    00192   //          with proba pMut, our mutation
    00193
    00194   // the crossover - with probability pCross
    00195   eoProportionalOp<EOT> * cross = new eoProportionalOp<EOT> ;
    00196   _state.storeFunctor(cross);
    00197   ptQuad = new eoQuadCloneOp<EOT>;
    00198   _state.storeFunctor(ptQuad);
    00199   cross->add(*ptCombinedQuadOp, pCrossParam.value()); // user crossover
    00200   cross->add(*ptQuad, 1-pCrossParam.value()); // clone operator
    00201
    00202   // now the sequential
    00203   eoSequentialOp<EOT> *op = new eoSequentialOp<EOT>;
    00204   _state.storeFunctor(op);
    00205   op->add(*cross, 1.0);  // always crossover (but clone with prob 1-pCross
    00206   op->add(*ptCombinedMonOp, pMutParam.value());
    00207
    00208   // that's it!
    00209   return *op;
    00210 }
    00211 #endif