00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef _make_op_h
00028 #define _make_op_h
00029
00030
00031 #include <eoOp.h>
00032 #include <eoGenOp.h>
00033 #include <eoCloneOps.h>
00034 #include <eoOpContainer.h>
00035
00036 #include <eoProportionalCombinedOp.h>
00037
00038
00039 #include <es/eoReal.h>
00040 #include <es/eoRealInitBounded.h>
00041 #include <es/eoRealOp.h>
00042 #include <es/eoNormalMutation.h>
00043
00044 #include <utils/eoParser.h>
00045 #include <utils/eoState.h>
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 template <class EOT>
00067 eoGenOp<EOT> & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit<EOT>& _init)
00068 {
00069
00070 eoValueParam<eoParamParamType>& boundsParam
00071 = _parser.getORcreateParam(eoParamParamType("(0,1)"), "objectBounds",
00072 "Bounds for variables (unbounded if absent)",
00073 'B', "Genetic Operators");
00074
00075
00076
00077
00078
00079
00080 EOT eoTmp;
00081 _init(eoTmp);
00082 unsigned vecSize = eoTmp.size();
00083
00084
00085 eoRealVectorBounds * ptBounds;
00086 if (_parser.isItThere(boundsParam))
00087 {
00091 eoParamParamType & ppBounds = boundsParam.value();
00092
00093 std::vector<double> v;
00094 std::vector<std::string>::iterator it;
00095 for (it=ppBounds.second.begin(); it<ppBounds.second.end(); it++)
00096 {
00097 istrstream is(it->c_str());
00098 double r;
00099 is >> r;
00100 v.push_back(r);
00101 }
00102
00103 if (v.size() == 2)
00104 ptBounds = new eoRealVectorBounds(vecSize, v[0], v[1]);
00105 else
00106 throw std::runtime_error("Sorry, only unique bounds for all variables implemented at the moment. Come back later");
00107
00109 }
00110 else
00111 ptBounds = new eoRealVectorNoBounds(vecSize);
00112
00113
00114
00115
00116
00117
00118 eoValueParam<std::string>& operatorParam
00119 = _parser.getORcreateParam(std::string("SGA"), "operator",
00120 "Description of the operator (SGA only now)",
00121 'o', "Genetic Operators");
00122
00123 if (operatorParam.value() != std::string("SGA"))
00124 throw std::runtime_error("Sorry, only SGA-like operator available right now\n");
00125
00126
00127
00128
00129
00130
00131
00132 eoValueParam<double>& pCrossParam
00133 = _parser.getORcreateParam(0.6, "pCross", "Probability of Crossover",
00134 'C', "Genetic Operators" );
00135
00136 if ( (pCrossParam.value() < 0) || (pCrossParam.value() > 1) )
00137 throw std::runtime_error("Invalid pCross");
00138
00139 eoValueParam<double>& pMutParam
00140 = _parser.getORcreateParam(0.1, "pMut", "Probability of Mutation",
00141 'M', "Genetic Operators" );
00142
00143 if ( (pMutParam.value() < 0) || (pMutParam.value() > 1) )
00144 throw std::runtime_error("Invalid pMut");
00145
00146
00148
00149 eoValueParam<double>& segmentRateParam
00150 = _parser.getORcreateParam(double(1.0), "segmentRate",
00151 "Relative rate for segment crossover",
00152 's', "Genetic Operators" );
00153
00154 if ( (segmentRateParam.value() < 0) )
00155 throw std::runtime_error("Invalid segmentRate");
00156
00157 eoValueParam<double>& arithmeticRateParam
00158 = _parser.getORcreateParam(double(2.0), "arithmeticRate",
00159 "Relative rate for arithmetic crossover",
00160 'A', "Genetic Operators" );
00161
00162 if ( (arithmeticRateParam.value() < 0) )
00163 throw std::runtime_error("Invalid arithmeticRate");
00164
00165
00166 bool bCross = true;
00167 if (segmentRateParam.value()+arithmeticRateParam.value()==0)
00168 {
00169 std::cerr << "Warning: no crossover" << std::endl;
00170 bCross = false;
00171 }
00172
00173
00174 eoPropCombinedQuadOp<EOT> *ptCombinedQuadOp = NULL;
00175 eoQuadOp<EOT> *ptQuad = NULL;
00176
00177 if (bCross)
00178 {
00179
00180 ptQuad = new eoSegmentCrossover<EOT>(*ptBounds);
00181 _state.storeFunctor(ptQuad);
00182 ptCombinedQuadOp = new eoPropCombinedQuadOp<EOT>(*ptQuad, segmentRateParam.value());
00183
00184
00185 ptQuad = new eoArithmeticCrossover<EOT>(*ptBounds);
00186 _state.storeFunctor(ptQuad);
00187 ptCombinedQuadOp->add(*ptQuad, arithmeticRateParam.value());
00188
00189
00190 _state.storeFunctor(ptCombinedQuadOp);
00191 }
00192
00193
00195
00196 eoValueParam<double> & epsilonParam
00197 = _parser.getORcreateParam(0.01, "epsilon", "Half-size of interval for Uniform Mutation",
00198 'e', "Genetic Operators" );
00199
00200 if ( (epsilonParam.value() < 0) )
00201 throw std::runtime_error("Invalid epsilon");
00202
00203 eoValueParam<double> & uniformMutRateParam
00204 = _parser.getORcreateParam(1.0, "uniformMutRate",
00205 "Relative rate for uniform mutation", 'u', "Genetic Operators" );
00206
00207 if ( (uniformMutRateParam.value() < 0) )
00208 throw std::runtime_error("Invalid uniformMutRate");
00209
00210 eoValueParam<double> & detMutRateParam
00211 = _parser.getORcreateParam(1.0, "detMutRate",
00212 "Relative rate for deterministic uniform mutation",
00213 'd', "Genetic Operators" );
00214
00215 if ( (detMutRateParam.value() < 0) )
00216 throw std::runtime_error("Invalid detMutRate");
00217
00218 eoValueParam<double> & normalMutRateParam
00219 = _parser.getORcreateParam(1.0, "normalMutRate",
00220 "Relative rate for Gaussian mutation",
00221 'd', "Genetic Operators" );
00222
00223 if ( (normalMutRateParam.value() < 0) )
00224 throw std::runtime_error("Invalid normalMutRate");
00225
00226 eoValueParam<double> & sigmaParam
00227 = _parser.getORcreateParam(1.0, "sigma",
00228 "Sigma (fixed) for Gaussian mutation",
00229 'S', "Genetic Operators" );
00230
00231 if ( (sigmaParam.value() < 0) )
00232 throw std::runtime_error("Invalid sigma");
00233
00234
00235 bool bMut = true;
00236 if (uniformMutRateParam.value()+detMutRateParam.value()+normalMutRateParam.value()==0)
00237 {
00238 std::cerr << "Warning: no mutation" << std::endl;
00239 bMut = false;
00240 }
00241 if (!bCross && !bMut)
00242 throw std::runtime_error("No operator called in SGA operator definition!!!");
00243
00244
00245 eoPropCombinedMonOp<EOT> *ptCombinedMonOp = NULL;
00246 eoMonOp<EOT> *ptMon = NULL;
00247
00248 if (bMut)
00249 {
00250
00251
00252 ptMon = new eoUniformMutation<EOT>(*ptBounds, epsilonParam.value());
00253 _state.storeFunctor(ptMon);
00254
00255 ptCombinedMonOp = new eoPropCombinedMonOp<EOT>(*ptMon, uniformMutRateParam.value());
00256
00257
00258 ptMon = new eoDetUniformMutation<EOT>(*ptBounds, epsilonParam.value());
00259 _state.storeFunctor(ptMon);
00260 ptCombinedMonOp->add(*ptMon, detMutRateParam.value());
00261
00262
00263 ptMon = new eoNormalMutation<EOT>(*ptBounds, sigmaParam.value());
00264 _state.storeFunctor(ptMon);
00265 ptCombinedMonOp->add(*ptMon, normalMutRateParam.value());
00266 _state.storeFunctor(ptCombinedMonOp);
00267 }
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278 eoProportionalOp<EOT> * cross = new eoProportionalOp<EOT> ;
00279 _state.storeFunctor(cross);
00280 ptQuad = new eoQuadCloneOp<EOT>;
00281 _state.storeFunctor(ptQuad);
00282 cross->add(*ptCombinedQuadOp, pCrossParam.value());
00283 cross->add(*ptQuad, 1-pCrossParam.value());
00284
00285
00286 eoSequentialOp<EOT> *op = new eoSequentialOp<EOT>;
00287 _state.storeFunctor(op);
00288 op->add(*cross, 1.0);
00289 op->add(*ptCombinedMonOp, pMutParam.value());
00290
00291
00292 return *op;
00293 }
00294 #endif