ECF 1.5
SelFitnessProportionalOp.cpp
1#include "ECF_base.h"
2#include "SelFitnessProportionalOp.h"
3#include "SelBestOp.h"
4#include "SelWorstOp.h"
5#include <cmath>
6
7
9 state_ = state;
10 selPressure_ = 10;
11 return true;
12}
13
14
16{
17 if(selPressure > 0){
18 selPressure_ = selPressure;
19 return true;
20 }
21 return false;
22}
23
24
25IndividualP SelFitnessProportionalOp::select(const std::vector<IndividualP>& pool)
26{
27 if(pool.empty())
28 return IndividualP();
29
30 SelBestOp best_;
31 SelWorstOp worst_;
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.);
37
38 // check for uniform population
39 if(fabs(bestVal - worstVal) < 0.000001)
40 bestVal = worstVal + 1;
41
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];
46 }
47
48 double randVal = state_->getRandomizer()->getRandomDouble() * howFit[howFit.size() - 1];
49 uint chosen = 0;
50 while(howFit[chosen] < randVal)
51 chosen++;
52
53 return pool[chosen];
54}
55
56
57std::vector<IndividualP> SelFitnessProportionalOp::selectMany(const std::vector<IndividualP>& pool, uint repeats)
58{
59 if(pool.empty())
60 return pool;
61
62 SelBestOp best_;
63 SelWorstOp worst_;
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;
70
71 // check for uniform population
72 if(fabs(bestVal - worstVal) < 0.000001)
73 bestVal = worstVal + 1;
74
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];
79 }
80
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)
87 end = chosen;
88 else
89 begin = chosen;
90 }
91 if(howFit[begin] >= randVal)
92 chosen = begin;
93 else
94 chosen = end;
95
96// while(howFit[chosen] < randVal)
97// chosen++;
98 selected.push_back(pool[chosen]);
99 }
100
101 return selected;
102}
Best individual selection operator.
Definition: SelBestOp.h:10
IndividualP select(const std::vector< IndividualP > &)
Select one individual from a set.
Definition: SelBestOp.cpp:11
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.
Definition: SelWorstOp.h:11
IndividualP select(const std::vector< IndividualP > &)
Select one individual from a set.
Definition: SelWorstOp.cpp:12