• 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, "crossover rate");
00019     registerParameter(state, "selpressure", (voidP) new double(10), ECF::DOUBLE, 
00020         "selection pressure: how much is the best individual 'better' than the worst");
00021 }
00022 
00023 
00024 bool RouletteWheel :: initialize(StateP state)
00025 {
00026     selFitPropOp->initialize(state);
00027     selRandomOp->initialize(state);
00028     selBestOp->initialize(state);
00029 
00030     voidP crRateP = getParameterValue(state, "crxprob");
00031     crxRate_ = *((double*) crRateP.get());
00032 
00033     voidP selPressP = getParameterValue(state, "selpressure");
00034     selPressure_ = *((double*) selPressP.get());
00035 
00036     selFitPropOp->setSelPressure(selPressure_);
00037 
00038     return true;
00039 }
00040 
00041 
00042 bool RouletteWheel :: advanceGeneration(StateP state, DemeP deme)
00043 {
00044     // elitism: copy current best individual
00045     IndividualP best = selBestOp->select(*deme);
00046     best = copy(best);
00047 
00048     // select individuals
00049     std::vector<IndividualP> wheel;
00050     wheel = selFitPropOp->selectMany(*deme, (uint) deme->size());
00051 
00052     // copy selected to new population
00053     for(uint i = 0; i < wheel.size(); ++i) 
00054         wheel[i] = copy(wheel[i]);
00055 
00056     // replace old population
00057     for(uint i = 0; i < deme->size(); i++)
00058         replaceWith((*deme)[i], wheel[i]);
00059 
00060     ECF_LOG(state, 5, "Selected individuals:");
00061     for(uint i = 0; i < deme->size(); i++){
00062         ECF_LOG(state, 5, dbl2str(deme->at(i)->fitness->getValue()));
00063     }
00064 
00065     // determine the number of crx operations
00066     uint noCrx = (int)(deme->size() * crxRate_ /2);
00067 
00068     // perform crossover
00069     for(uint i = 0; i < noCrx; i++){
00070 
00071         // select parents
00072         IndividualP parent1 = selRandomOp->select(*deme);
00073         IndividualP parent2 = selRandomOp->select(*deme);
00074         ECF_LOG(state, 5, "Parents: " + dbl2str(parent1->fitness->getValue()) + ", " + dbl2str(parent2->fitness->getValue()));
00075 
00076         // create children
00077         IndividualP child1 = copy(parent1);
00078         IndividualP child2 = copy(parent2);
00079 
00080         // perform crx operations
00081         mate(parent1, parent2, child1);
00082         mate(parent1, parent2, child2);
00083 
00084         // replace parents with children
00085         replaceWith(parent1, child1);
00086         replaceWith(parent2, child2);
00087     }
00088 
00089     // perform mutation on whole population
00090     mutate(*deme);
00091 
00092     // evaluate new individuals
00093     for(uint i = 0; i < deme->size(); i++)
00094         if(!deme->at(i)->fitness->isValid()) {
00095             evaluate(deme->at(i));
00096         }
00097 
00098     // elitism: preserve best individual
00099     IndividualP random = selRandomOp->select(*deme);
00100     if(best->fitness->isBetterThan(random->fitness))
00101         replaceWith(random, best);
00102 
00103     return true;
00104 }

Generated on Tue Nov 4 2014 13:04:30 for ECF by  doxygen 1.7.1