2#include "SelFitnessProportionalOp.h"
32 IndividualP best = best_.
select(pool);
33 IndividualP worst = worst_.
select(pool);
34 double bestVal = best->fitness->getValue();
35 double worstVal = worst->fitness->getValue();
36 std::vector<double> howFit(pool.size(), 0.);
39 if(fabs(bestVal - worstVal) < 0.000001)
40 bestVal = worstVal + 1;
42 howFit[0] = 1 + (
selPressure_ - 1) * (pool[0]->fitness->getValue() - worstVal)/(bestVal - worstVal);
43 for(uint i = 1; i<pool.size(); i++) {
44 howFit[i] = 1 + (
selPressure_ - 1) * (pool[i]->fitness->getValue() - worstVal)/(bestVal - worstVal);
45 howFit[i] += howFit[i-1];
48 double randVal = state_->getRandomizer()->getRandomDouble() * howFit[howFit.size() - 1];
50 while(howFit[chosen] < randVal)
64 IndividualP best = best_.
select(pool);
65 IndividualP worst = worst_.
select(pool);
66 double bestVal = best->fitness->getValue();
67 double worstVal = worst->fitness->getValue();
68 std::vector<double> howFit(pool.size(), 0.);
69 std::vector<IndividualP> selected;
72 if(fabs(bestVal - worstVal) < 0.000001)
73 bestVal = worstVal + 1;
75 howFit[0] = 1 + (
selPressure_ - 1) * (pool[0]->fitness->getValue() - worstVal)/(bestVal - worstVal);
76 for(uint i = 1; i<pool.size(); i++) {
77 howFit[i] = 1 + (
selPressure_ - 1) * (pool[i]->fitness->getValue() - worstVal)/(bestVal - worstVal);
78 howFit[i] += howFit[i-1];
81 for(uint i = 0; i < repeats; i++) {
82 double randVal = state_->getRandomizer()->getRandomDouble() * howFit[howFit.size() - 1];
83 uint chosen = 0, begin = 0, end = (uint) howFit.size() - 1;
84 while((begin + 1) < end) {
85 chosen = (begin + end) / 2;
86 if(howFit[chosen] > randVal)
91 if(howFit[begin] >= randVal)
98 selected.push_back(pool[chosen]);
Best individual selection operator.
IndividualP select(const std::vector< IndividualP > &)
Select one individual from a set.
IndividualP select(const std::vector< IndividualP > &)
Select one individual from a set.
double selPressure_
the ratio of selection probability of the best and worst individual in the set
bool initialize(StateP)
Selection operator initialization. Must be called before individual selection.
std::vector< IndividualP > selectMany(const std::vector< IndividualP > &, uint)
Repeatedly select from the same pool (duplicates allowed)
bool setSelPressure(double)
Set selection pressure value.
Worst individual selection operator.
IndividualP select(const std::vector< IndividualP > &)
Select one individual from a set.