ECF 1.5
AlgSteadyStateTournament.cpp
1#include "ECF_base.h"
2#include "ECF_macro.h"
3#include "AlgSteadyStateTournament.h"
4#include "SelRandomOp.h"
5#include "SelWorstOp.h"
6
7
8SteadyStateTournament::SteadyStateTournament()
9{
10 // define algorithm name
11 name_ = "SteadyStateTournament";
12
13 // create selection operators needed
14 // in this case, SelRandomOp and SelWorstOp
15 selRandomOp = static_cast<SelectionOperatorP> (new SelRandomOp);
16 selWorstOp = static_cast<SelectionOperatorP> (new SelWorstOp);
17}
18
19
21{
22 registerParameter(state, "tsize", (voidP) new uint(3), ECF::UINT,
23 "tournament size (individuals selected randomly, worst one eliminated)");
24}
25
26
28{
29 // initialize all operators
30 selRandomOp->initialize(state);
31 selWorstOp->initialize(state);
32
33 // read parameter values
34 voidP tsizep = getParameterValue(state, "tsize");
35 nTournament_ = *((uint*) tsizep.get());
36
37 if(nTournament_ < 3) {
38 ECF_LOG(state, 1, "Error: SteadyStateTournament algorithm requires minimum tournament size of 3!");
39 throw "";
40 }
41
42 return true;
43}
44
45
46bool SteadyStateTournament::advanceGeneration(StateP state, DemeP deme)
47{
49 for(uint iIter = 0; iIter < deme->size(); iIter++) {
50
51 ECF_LOG(state, 5, "Individuals in tournament: ");
52
53 std::vector<IndividualP> tournament;
54 for (uint i = 0; i < nTournament_; ++i) {
55 // select a random individual for the tournament
56 tournament.push_back(selRandomOp->select(*deme));
57 ECF_LOG(state, 5, uint2str(i) + ": " + tournament[i]->toString());
58 }
59
60 // select the worst from the tournament
61 IndividualP worst = selWorstOp->select(tournament);
62 ECF_LOG(state, 5, "The worst from the tournament: " + worst->toString());
63
64 // remove pointer to 'worst' individual from vector 'tournament'
65 removeFrom(worst, tournament);
66
67 // crossover the first two (remaining) individuals in the tournament
68 mate(tournament[0], tournament[1], worst);
69
70 // perform mutation on new individual
71 mutate(worst);
72
73 // create new fitness
74 evaluate(worst);
75 ECF_LOG(state, 5, "New individual: " + worst->toString());
76 }
77
78 return true;
79}
uint mutate(const std::vector< IndividualP > &pool)
Helper function: send a vector of individuals to mutation.
Definition: Algorithm.h:169
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
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
Random individual selection operator.
Definition: SelRandomOp.h:12
Worst individual selection operator.
Definition: SelWorstOp.h:11
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.
uint nTournament_
tournament size