EvolvingObjects
|
00001 //----------------------------------------------------------------------------- 00024 #ifndef EO_make_genotype_h 00025 #define EO_make_genotype_h 00026 00027 #include <iostream> 00028 #include <sstream> 00029 #include <vector> 00030 00031 #include "es/eoReal.h" 00032 #include "es/eoEsChromInit.h" 00033 #include "utils/eoParser.h" 00034 #include "utils/eoRealVectorBounds.h" 00035 #include "utils/eoState.h" 00036 00037 00064 template <class EOT> 00065 eoEsChromInit<EOT> & do_make_genotype(eoParser& _parser, eoState& _state, EOT) 00066 { 00067 // the fitness type 00068 typedef typename EOT::Fitness FitT; 00069 eoEsChromInit<EOT> *init; 00070 00071 // for eoReal, only thing needed is the size - but might have been created elswhere ... 00072 eoValueParam<unsigned>& vecSize 00073 = _parser.getORcreateParam(unsigned(10), "vecSize", 00074 "The number of variables ", 00075 'n', "Genotype Initialization"); 00076 // to build an eoReal Initializer, we need bounds: [-1,1] by default 00077 eoValueParam<eoRealVectorBounds>& boundsParam 00078 = _parser.getORcreateParam(eoRealVectorBounds(vecSize.value(), -1, 1), 00079 "initBounds", 00080 "Bounds for initialization (MUST be bounded)", 00081 'B', "Genotype Initialization"); 00082 // now some initial value for sigmas - even if useless? 00083 // should be used in Normal mutation 00084 eoValueParam<std::string>& sigmaParam 00085 = _parser.getORcreateParam(std::string("0.3"), "sigmaInit", 00086 "Initial value for Sigmas (with a '%' -> scaled by the range of each variable)", 00087 's', "Genotype Initialization"); 00088 // check for % 00089 bool to_scale = false; 00090 size_t pos = sigmaParam.value().find('%'); 00091 if(pos < sigmaParam.value().size()) { 00092 // found a % - use scaling and get rid of '%' 00093 to_scale = true; 00094 sigmaParam.value().resize(pos); 00095 } 00096 std::istringstream is(sigmaParam.value()); 00097 double sigma; 00098 is >> sigma; 00099 // minimum check 00100 if(sigma < 0) 00101 throw std::runtime_error("Negative sigma in make_genotype"); 00102 if(to_scale) 00103 init = new eoEsChromInit<EOT>(boundsParam.value(), sigma, to_scale); 00104 else { 00105 // define parameter 00106 eoValueParam<std::vector<double> >& vecSigmaParam 00107 = _parser.getORcreateParam(std::vector<double>(vecSize.value(), sigma), "vecSigmaInit", 00108 "Initial value for Sigmas (only used when initSigma is not scaled)", 00109 'S', "Genotype Initialization"); 00110 init = new eoEsChromInit<EOT>(boundsParam.value(), vecSigmaParam.value()); 00111 } 00112 // store in state 00113 _state.storeFunctor(init); 00114 return *init; 00115 } 00116 00118 #endif // EO_make_genotype_h 00119 00120 00121 00122 // Local Variables: 00123 // coding: iso-8859-1 00124 // mode:C++ 00125 // c-file-style: "Stroustrup" 00126 // comment-column: 35 00127 // fill-column: 80 00128 // End: