00001
00002
00003
00004
00005
00006
00007
00008
00009 struct my_type
00010 {
00011 double v;
00012 bool b;
00013 };
00014
00015
00016 class MyTerminal : public Primitives::Primitive
00017 {
00018 public:
00019 my_type value_;
00020
00021 MyTerminal()
00022 {
00023 nArguments_ = 0;
00024 }
00025 void execute(void* result, Tree& tree)
00026 {
00027 my_type& res = *(my_type*)result;
00028 res = value_;
00029 }
00030 void setValue(void* value)
00031 {
00032 value_ = *(my_type*)value;
00033 }
00034 ~MyTerminal()
00035 { }
00036 };
00037
00038
00039 class MyFunc : public Primitives::Primitive
00040 {
00041 public:
00042 MyFunc()
00043 {
00044 nArguments_ = 2;
00045 name_ = "func";
00046 }
00047 void execute(void* result, Tree& tree)
00048 {
00049 my_type first, second;
00050 my_type& func = *(my_type*)result;
00051
00052 getNextArgument(&first, tree);
00053 getNextArgument(&second, tree);
00054
00055 func.b = first.b && second.b;
00056 func.v = first.v + second.v;
00057 }
00058 ~MyFunc()
00059 { }
00060 };
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 class MyGenotypeCrxOp : public CrossoverOp
00071 {
00072 public:
00073 bool mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
00074 {
00075
00076 return true;
00077 }
00078 };
00079 typedef boost::shared_ptr<MyGenotypeCrxOp> MyGenotypeCrxOpP;
00080
00081
00082 class MyGenotypeMutOp : public MutationOp
00083 {
00084 public:
00085 bool mutate(GenotypeP gene)
00086 {
00087
00088 return true;
00089 }
00090 };
00091 typedef boost::shared_ptr<MyGenotypeMutOp> MyGenotypeMutOpP;
00092
00093
00094 class MyGenotype : public Genotype
00095 {
00096 public:
00097 std::vector<double> realValues_;
00098
00099
00100 MyGenotype()
00101 {
00102 name_ = "MyGenotype";
00103 }
00104
00105
00106 MyGenotype* copy()
00107 {
00108 MyGenotype *newObject = new MyGenotype(*this);
00109 return newObject;
00110 }
00111
00113
00114
00115
00116
00117
00118
00119
00121
00122
00123
00124
00125
00126
00127
00128
00129 void registerParameters(StateP state)
00130 {
00131 registerParameter(state, "size", (voidP) (new uint(1)), UINT);
00132 }
00133
00134
00135 bool initialize(StateP state)
00136 {
00137 if(!isParameterDefined(state, "size")) {
00138 state->getLogger()->log(1, "Error: MyGenotype size not defined");
00139 throw("");
00140 }
00141
00142 voidP sizep = getParameterValue(state, "size");
00143 uint size = *((uint*) sizep.get());
00144
00145 realValues_.resize(size);
00146
00147 for(uint i = 0; i < size; i++)
00148 realValues_[i] = state->getRandomizer()->getRandomDouble();
00149
00150 return true;
00151 }
00152
00153
00154 void write(XMLNode &xMyGenotype)
00155 {
00156 xMyGenotype = XMLNode::createXMLTopNode("MyGenotype");
00157 std::stringstream sValue;
00158 sValue << realValues_.size();
00159 xMyGenotype.addAttribute("size", sValue.str().c_str());
00160
00161 sValue.str("");
00162 for(uint iVar = 0; iVar < realValues_.size(); iVar++)
00163 sValue << "\t" << realValues_[iVar];
00164 xMyGenotype.addText(sValue.str().c_str());
00165 }
00166
00167
00168 void read(XMLNode& xMyGenotype)
00169 {
00170 XMLCSTR values = xMyGenotype.getText();
00171 std::stringstream sValues;
00172 sValues << values;
00173
00174 for(uint iVar = 0; iVar < realValues_.size(); iVar++)
00175 sValues >> realValues_[iVar];
00176 }
00177 };
00178 typedef boost::shared_ptr<MyGenotype> MyGenotypeP;
00179
00180
00181
00182
00183
00184
00185
00186
00187 class MyOp : public Operator
00188 {
00189 public:
00190 uint myStopGen_;
00191
00192
00193 void registerParameters(StateP state)
00194 {
00195 state->getRegistry()->registerEntry("my.stopgen", (voidP) (new uint(0)), UINT);
00196 }
00197
00198
00199 bool initialize(StateP state)
00200 {
00201
00202 if(!state->getRegistry()->isModified("my.stopgen"))
00203 return false;
00204
00205 voidP sptr = state->getRegistry()->getEntry("my.stopgen");
00206 myStopGen_ = *((uint*) sptr.get());
00207 return true;
00208 }
00209
00210
00211 bool operate(StateP state)
00212 {
00213
00214 if(state->getGenerationNo() == myStopGen_)
00215 state->setTerminateCond();
00216 return true;
00217 }
00218 };
00219 typedef boost::shared_ptr<MyOp> MyOpP;
00220
00221
00222
00223
00224
00225
00226
00227 class MyAlg : public Algorithm
00228 {
00229 protected:
00230
00231 SelFitnessProportionalOpP selFitOp_;
00232 SelRandomOpP selRandomOp_;
00233 SelBestOpP selBestOp_;
00234 SelWorstOpP selWorstOp_;
00235
00236 bool replaceWorst_;
00237 public:
00238
00239 MyAlg()
00240 {
00241 name_ = "MyAlg";
00242 selFitOp_ = (SelFitnessProportionalOpP) (new SelFitnessProportionalOp);
00243 selRandomOp_ = (SelRandomOpP) (new SelRandomOp);
00244 selBestOp_ = (SelBestOpP) (new SelBestOp);
00245 selWorstOp_ = (SelWorstOpP) (new SelWorstOp);
00246 }
00247
00248
00249 void registerParameters(StateP state)
00250 {
00251
00252 registerParameter(state, "replace", (voidP) (new std::string("random")), STRING);
00253 }
00254
00255
00256 bool initialize(StateP state)
00257 {
00258
00259 selFitOp_->initialize(state);
00260
00261 selFitOp_->setSelPressure(10);
00262
00263
00264 selRandomOp_->initialize(state);
00265 selBestOp_->initialize(state);
00266 selWorstOp_->initialize(state);
00267
00268
00269 voidP sptr = getParameterValue(state, "replace");
00270 std::string replace = *((std::string*) sptr.get());
00271 replaceWorst_ = false;
00272 if(replace == "worst")
00273 replaceWorst_ = true;
00274
00275 return true;
00276 }
00277
00278
00279 bool advanceGeneration(StateP state, DemeP deme)
00280 {
00281
00282 IndividualP first = selFitOp_->select(*deme);
00283 IndividualP second = selBestOp_->select(*deme);
00284
00285 IndividualP child;
00286 if(replaceWorst_)
00287 child = selWorstOp_->select(*deme);
00288 else
00289 child = selRandomOp_->select(*deme);
00290
00291 mate(first, second, child);
00292 mutate(child);
00293
00294
00295 evaluate(child);
00296
00297
00298
00299
00300 return true;
00301 }
00302 };
00303 typedef boost::shared_ptr<MyAlg> MyAlgP;