• Main Page
  • Modules
  • Classes
  • Files
  • File List

D:/Projekt/ECF_trunk/ECF/AlgRouletteWheel.cpp

00001 #include "ECF_base.h"
00002 #include "AlgRouletteWheel.h"
00003 #include "ECF_macro.h"
00004 
00005 
00006 RouletteWheel :: RouletteWheel()
00007 {
00008     name_ = "RouletteWheel";
00009 
00010     selFitPropOp = static_cast<SelFitnessProportionalOpP> (new SelFitnessProportionalOp);
00011     selRandomOp = static_cast<SelRandomOpP> (new SelRandomOp);
00012     selBestOp = static_cast<SelBestOpP> (new SelBestOp);
00013 }
00014 
00015 
00016 void RouletteWheel :: registerParameters(StateP state)
00017 {
00018     registerParameter(state, "crxprob", (voidP) new double(0.5), ECF::DOUBLE);
00019     registerParameter(state, "selpressure", (voidP) new double(10), ECF::DOUBLE);
00020 }
00021 
00022 
00023 bool RouletteWheel :: initialize(StateP state)
00024 {
00025     selFitPropOp->initialize(state);
00026     selRandomOp->initialize(state);
00027     selBestOp->initialize(state);
00028 
00029     voidP crRateP = getParameterValue(state, "crxprob");
00030     crxRate_ = *((double*) crRateP.get());
00031 
00032     voidP selPressP = getParameterValue(state, "selpressure");
00033     selPressure_ = *((double*) selPressP.get());
00034 
00035     selFitPropOp->setSelPressure(selPressure_);
00036 
00037     return true;
00038 }
00039 
00040 
00041 bool RouletteWheel :: advanceGeneration(StateP state, DemeP deme)
00042 {
00043     // elitism: copy current best individual
00044     IndividualP best = selBestOp->select(*deme);
00045     best = copy(best);
00046 
00047     // select individuals
00048     std::vector<IndividualP> wheel;
00049     wheel = selFitPropOp->selectMany(*deme, (uint) deme->size());
00050 
00051     // copy selected to new population
00052     for(uint i = 0; i < wheel.size(); ++i) 
00053         wheel[i] = copy(wheel[i]);
00054 
00055     // replace old population
00056     for(uint i = 0; i < deme->size(); i++)
00057         replaceWith((*deme)[i], wheel[i]);
00058 
00059     ECF_LOG(state, 5, "Selected individuals:");
00060     for(uint i = 0; i < deme->size(); i++){
00061         ECF_LOG(state, 5, dbl2str(deme->at(i)->fitness->getValue()));
00062     }
00063 
00064     // determine the number of crx operations
00065     uint noCrx = (int)(deme->size() * crxRate_ /2);
00066 
00067     // perform crossover
00068     for(uint i = 0; i < noCrx; i++){
00069 
00070         // select parents
00071         IndividualP parent1 = selRandomOp->select(*deme);
00072         IndividualP parent2 = selRandomOp->select(*deme);
00073         ECF_LOG(state, 5, "Parents: " + dbl2str(parent1->fitness->getValue()) + ", " + dbl2str(parent2->fitness->getValue()));
00074 
00075         // create children
00076         IndividualP child1 = copy(parent1);
00077         IndividualP child2 = copy(parent2);
00078 
00079         // perform crx operations
00080         mate(parent1, parent2, child1);
00081         mate(parent1, parent2, child2);
00082 
00083         // replace parents with children
00084         replaceWith(parent1, child1);
00085         replaceWith(parent2, child2);
00086     }
00087 
00088     // perform mutation on whole population
00089     mutate(*deme);
00090 
00091     // evaluate new individuals
00092     for(uint i = 0; i < deme->size(); i++)
00093         if(!deme->at(i)->fitness->isValid()) {
00094             evaluate(deme->at(i));
00095         }
00096 
00097     // elitism: preserve best individual
00098     IndividualP random = selRandomOp->select(*deme);
00099     if(best->fitness->isBetterThan(random->fitness))
00100         replaceWith(random, best);
00101 
00102     return true;
00103 }

Generated on Fri Jul 5 2013 09:34:23 for ECF by  doxygen 1.7.1