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