ECF 1.5
AlgElimination.cpp
1#include "ECF_base.h"
2#include "AlgElimination.h"
3#include "ECF_macro.h"
4
5
6Elimination :: Elimination()
7{
8 name_ = "Elimination";
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, "gengap", (voidP) new double(0.6), ECF::DOUBLE,
19 "generation gap (percentage of population to be eliminated)");
20 registerParameter(state, "selpressure", (voidP) new double(10), ECF::DOUBLE,
21 "selection pressure: how much is the worst individual 'worse' than the best");
22}
23
24
25bool Elimination :: initialize(StateP state)
26{
27 selFitPropOp->initialize(state);
28 selRandomOp->initialize(state);
29 selBestOp->initialize(state);
30
31 voidP genGapP = getParameterValue(state, "gengap");
32 genGap_ = *((double*) genGapP.get());
33
34 if(genGap_ <= 0 || genGap_ > 1) {
35 ECF_LOG_ERROR(state, "Error: generation gap parameter in Elimination algorithm must be in <0, 1]!");
36 throw "";
37 }
38
39 voidP selPressP = getParameterValue(state, "selpressure");
40 selPressure_ = *((double*) selPressP.get());
41
42 // selection chooses worse individuals
43 selFitPropOp->setSelPressure(1./selPressure_);
44
45 return true;
46}
47
48
49bool Elimination :: advanceGeneration(StateP state, DemeP deme)
50{
51 // elitism: copy current best individual
52 IndividualP best = selBestOp->select(*deme);
53 best = copy(best);
54
55 // copy pointers
56 std::vector<IndividualP> newGen;
57 for(uint i = 0; i < deme->size(); i++)
58 newGen.push_back(deme->at(i));
59
60 // eliminate genGap_ worse individuals
61 uint generationGap = (uint) (genGap_ * deme->size());
62 for(uint i = 0; i < generationGap; i++) {
63 IndividualP victim = selFitPropOp->select(newGen);
64 removeFrom(victim, newGen);
65 }
66
67 // make genGap_ new ones
68 for(uint i = 0; i < generationGap; i++) {
69 IndividualP parent1 = selRandomOp->select(*deme);
70 IndividualP parent2 = selRandomOp->select(*deme);
71 IndividualP child = copy(parent1);
72 mate(parent1, parent2, child);
73 newGen.push_back(child);
74 }
75
76 // replace
77 for(uint i = 0; i < deme->size(); i++)
78 replaceWith(deme->at(i), newGen[i]);
79
80 // mutate all
81 mutate(*deme);
82
83 // evaluate new individuals
84 for(uint i = 0; i < deme->size(); i++)
85 if(!deme->at(i)->fitness->isValid()) {
86 evaluate(deme->at(i));
87 }
88
89 // elitism: preserve best individual
90 IndividualP random = selFitPropOp->select(*deme);
91 if(best->fitness->isBetterThan(random->fitness))
92 replaceWith(random, best);
93
94 return true;
95}
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
bool removeFrom(IndividualP victim, std::vector< IndividualP > &pool)
Helper function: remove victim from pool of individual pointers.
Definition: Algorithm.h:297
void evaluate(IndividualP ind)
Helper function: evaluate an individual.
Definition: Algorithm.h:157
double selPressure_
selection pressure
double genGap_
generation gap
bool advanceGeneration(StateP state, DemeP deme)
Perform a single generation on a single deme.
void registerParameters(StateP state)
Register algorithm's parameters (if any).
bool initialize(StateP state)
Initialize the algorithm, read parameters from the system, do a sanity check.
Best individual selection operator.
Definition: SelBestOp.h:10
Fitness proportional (and inverse proportional) individual selection operator.
Random individual selection operator.
Definition: SelRandomOp.h:12