• Main Page
  • Classes
  • Files
  • File List

D:/Radagast_D/Projekt/ECF_trunk/ECF/SelFitnessProportionalOp.cpp

00001 #include "ECF_base.h"
00002 #include "ECF_derived.h"
00003 
00004 
00005 bool SelFitnessProportionalOp::initialize(StateP state) {
00006     state_ = state;
00007     selPressure_ = 2;
00008     bInverseSelect_ = false;
00009     return true;
00010 }
00011 
00012 
00013 bool SelFitnessProportionalOp::setSelPressure(double selPressure)
00014 {
00015     if(selPressure > 1){
00016         selPressure_ = selPressure;
00017         return true;
00018     }
00019     return false;
00020 }
00021 
00022 
00023 IndividualP SelFitnessProportionalOp::select(const std::vector<IndividualP>& pool)
00024 {
00025     SelBestOp best_;
00026     SelWorstOp worst_;
00027     IndividualP best = best_.select(pool);
00028     IndividualP worst = worst_.select(pool);
00029     double bestVal = best->fitness->getValue();
00030     double worstVal = worst->fitness->getValue();
00031     std::vector<double> howFit(pool.size(), 0.); 
00032 
00033     if(bInverseSelect_) {
00034         howFit[0] = 1 + (selPressure_ - 1) * (bestVal - pool[0]->fitness->getValue())/(bestVal - worstVal);
00035         for(uint i = 1; i<pool.size(); i++) {
00036             howFit[i] = 1 + (selPressure_ - 1) * (bestVal - pool[i]->fitness->getValue())/(bestVal - worstVal);
00037             howFit[i] += howFit[i-1];
00038         }
00039     }
00040     else {
00041         howFit[0] = 1 + (selPressure_ - 1) * (pool[0]->fitness->getValue() - worstVal)/(bestVal - worstVal);
00042         for(uint i = 1; i<pool.size(); i++) {
00043             howFit[i] = 1 + (selPressure_ - 1) * (pool[i]->fitness->getValue() - worstVal)/(bestVal - worstVal);
00044             howFit[i] += howFit[i-1];
00045         }
00046     }
00047 
00048     double randVal = state_->getRandomizer()->getRandomDouble() * howFit[howFit.size() - 1];
00049     uint chosen = 0;
00050     while(howFit[chosen] < randVal)
00051         chosen++;
00052 
00053     return pool[chosen];
00054 }
00055 
00056 
00057 std::vector<IndividualP> SelFitnessProportionalOp::selectMany(const std::vector<IndividualP>& pool, uint repeats)
00058 {
00059     SelBestOp best_;
00060     SelWorstOp worst_;
00061     IndividualP best = best_.select(pool);
00062     IndividualP worst = worst_.select(pool);
00063     double bestVal = best->fitness->getValue();
00064     double worstVal = worst->fitness->getValue();
00065     std::vector<double> howFit(pool.size(), 0.);
00066     std::vector<IndividualP> selected;
00067 
00068     if(bInverseSelect_) {
00069         howFit[0] = 1 + (selPressure_ - 1) * (bestVal - pool[0]->fitness->getValue())/(bestVal - worstVal);
00070         for(uint i = 1; i<pool.size(); i++) {
00071             howFit[i] = 1 + (selPressure_ - 1) * (bestVal - pool[i]->fitness->getValue())/(bestVal - worstVal);
00072             howFit[i] += howFit[i-1];
00073         }
00074     }
00075     else {
00076         howFit[0] = 1 + (selPressure_ - 1) * (pool[0]->fitness->getValue() - worstVal)/(bestVal - worstVal);
00077         for(uint i = 1; i<pool.size(); i++) {
00078             howFit[i] = 1 + (selPressure_ - 1) * (pool[i]->fitness->getValue() - worstVal)/(bestVal - worstVal);
00079             howFit[i] += howFit[i-1];
00080         }
00081     }
00082 
00083     for(uint i = 0; i < repeats; i++) {
00084         double randVal = state_->getRandomizer()->getRandomDouble() * howFit[howFit.size() - 1];
00085         uint chosen = 0;
00086         while(howFit[chosen] < randVal)
00087             chosen++;
00088         selected.push_back(pool[chosen]);
00089     }
00090 
00091     return selected;
00092 }

Generated on Wed Sep 1 2010 14:31:21 for ECF by  doxygen 1.7.1