ECF 1.5
AlgRouletteWheel.cpp
1#include "ECF_base.h"
2#include "AlgRouletteWheel.h"
3#include "ECF_macro.h"
4
5
6RouletteWheel :: RouletteWheel()
7{
8 name_ = "RouletteWheel";
9
10 selFitPropOp = static_cast<SelFitnessProportionalOpP> (new SelFitnessProportionalOp);
11 selRandomOp = static_cast<SelRandomOpP> (new SelRandomOp);
12 selBestOp = static_cast<SelBestOpP> (new SelBestOp);
13}
14
15
17{
18 registerParameter(state, "crxprob", (voidP) new double(0.5), ECF::DOUBLE, "crossover rate");
19 registerParameter(state, "selpressure", (voidP) new double(10), ECF::DOUBLE,
20 "selection pressure: how much is the best individual 'better' than the worst");
21}
22
23
25{
26 selFitPropOp->initialize(state);
27 selRandomOp->initialize(state);
28 selBestOp->initialize(state);
29
30 voidP crRateP = getParameterValue(state, "crxprob");
31 crxRate_ = *((double*) crRateP.get());
32
33 voidP selPressP = getParameterValue(state, "selpressure");
34 selPressure_ = *((double*) selPressP.get());
35
36 selFitPropOp->setSelPressure(selPressure_);
37
38 return true;
39}
40
41
42bool RouletteWheel :: advanceGeneration(StateP state, DemeP deme)
43{
44 // elitism: copy current best individual
45 IndividualP best = selBestOp->select(*deme);
46 best = copy(best);
47
48 // select individuals
49 std::vector<IndividualP> wheel;
50 wheel = selFitPropOp->selectMany(*deme, (uint) deme->size());
51
52 // copy selected to new population
53 for(uint i = 0; i < wheel.size(); ++i)
54 wheel[i] = copy(wheel[i]);
55
56 // replace old population
57 for(uint i = 0; i < deme->size(); i++)
58 replaceWith((*deme)[i], wheel[i]);
59
60 ECF_LOG(state, 5, "Selected individuals:");
61 for(uint i = 0; i < deme->size(); i++){
62 ECF_LOG(state, 5, dbl2str(deme->at(i)->fitness->getValue()));
63 }
64
65 // determine the number of crx operations
66 uint noCrx = (int)(deme->size() * crxRate_ /2);
67
68 // perform crossover
69 for(uint i = 0; i < noCrx; i++){
70
71 // select parents
72 IndividualP parent1 = selRandomOp->select(*deme);
73 IndividualP parent2 = selRandomOp->select(*deme);
74 ECF_LOG(state, 5, "Parents: " + dbl2str(parent1->fitness->getValue()) + ", " + dbl2str(parent2->fitness->getValue()));
75
76 // create children
77 IndividualP child1 = copy(parent1);
78 IndividualP child2 = copy(parent2);
79
80 // perform crx operations
81 mate(parent1, parent2, child1);
82 mate(parent1, parent2, child2);
83
84 // replace parents with children
85 replaceWith(parent1, child1);
86 replaceWith(parent2, child2);
87 }
88
89 // perform mutation on whole population
90 mutate(*deme);
91
92 // evaluate new individuals
93 for(uint i = 0; i < deme->size(); i++)
94 if(!deme->at(i)->fitness->isValid()) {
95 evaluate(deme->at(i));
96 }
97
98 // elitism: preserve best individual
99 IndividualP random = selRandomOp->select(*deme);
100 if(best->fitness->isBetterThan(random->fitness))
101 replaceWith(random, best);
102
103 return true;
104}
uint mutate(const std::vector< IndividualP > &pool)
Helper function: send a vector of individuals to mutation.
Definition: Algorithm.h:169
IndividualP copy(IndividualP source)
Helper function: make a copy of an individual.
Definition: Algorithm.h:291
std::string name_
algorithm name
Definition: Algorithm.h:23
bool registerParameter(StateP state, std::string name, voidP value, enum ECF::type T, std::string description="")
Helper function: register a single parameter with the system.
Definition: Algorithm.h:35
voidP getParameterValue(StateP state, std::string name)
Helper function: get parameter value from the system.
Definition: Algorithm.h:46
bool mate(IndividualP p1, IndividualP p2, IndividualP child)
Helper function: crossover two individuals.
Definition: Algorithm.h:285
void replaceWith(IndividualP oldInd, IndividualP newInd)
Helper function: replace an individual in current deme.
Definition: Algorithm.h:187
void evaluate(IndividualP ind)
Helper function: evaluate an individual.
Definition: Algorithm.h:157
bool initialize(StateP state)
Initialize the algorithm, read parameters from the system, do a sanity check.
void registerParameters(StateP state)
Register algorithm's parameters (if any).
bool advanceGeneration(StateP state, DemeP deme)
Perform a single generation on a single deme.
double crxRate_
crossover rate
double selPressure_
selection pressure
Best individual selection operator.
Definition: SelBestOp.h:10
Fitness proportional (and inverse proportional) individual selection operator.
Random individual selection operator.
Definition: SelRandomOp.h:12