ECF 1.5
SolverTournamentEA.cpp
1#include "ECF/ECF_base.h"
2#include "ECF/SelRandomOp.h"
3#include "ECF/SelWorstOp.h"
4#include "SolverTournamentEA.h"
5
6
7SolverTournamentEA::SolverTournamentEA()
8{
9 name_ = "SolverTournamentEA";
10
11 // create selection operators needed
12 selRandomOp = (SelectionOperatorP) (new SelRandomOp);
13 selWorstOp = (SelectionOperatorP) (new SelWorstOp);
14}
15
16
18{
19 registerParameter(state, "tsize", (voidP) new uint(3), ECF::UINT,
20 "tournament size (individuals selected randomly, worst one eliminated)");
21}
22
23
25{
26 selRandomOp->initialize(state);
27 selWorstOp->initialize(state);
28
29 // read parameter values
30 voidP tsizep = getParameterValue(state, "tsize");
31 nTournament_ = *((uint*) tsizep.get());
32
33 if(nTournament_ < 3) {
34 ECF_LOG(state, 1, "Error: SolverTournamentEA requires minimum tournament size of 3!");
35 throw "";
36 }
37
38 // check if the problem provides API methods
39 getProblem()->registerParameters(state);
40 getProblem()->initialize(state);
41 IndividualP ind (new Individual(state));
42 PathP path = getProblem()->initPathTo(ind, ind);
43 uint l = getProblem()->getPathLength(path);
44 MoveP move = getProblem()->nextRandomMove(path);
45 if(path == 0 || l == -1 || move == 0) {
46 ECF_LOG(state, 1, "Error: SolverTournamentEA requires initPathTo, getPathLength, nextRandomMove!");
47 throw "";
48 }
49 return true;
50}
51
52
53bool SolverTournamentEA::advanceGeneration(StateP state, DemeP deme)
54{
56 for(uint iIter = 0; iIter < deme->size(); iIter++) {
57
58 ECF_LOG(state, 5, "Individuals in tournament: ");
59
60 std::vector<IndividualP> tournament;
61 for (uint i = 0; i < nTournament_; ++i) {
62 // select a random individual for the tournament
63 tournament.push_back(selRandomOp->select(*deme));
64 ECF_LOG(state, 5, uint2str(i) + ": " + tournament[i]->toString());
65 }
66
67 // select the worst from the tournament
68 IndividualP worst = selWorstOp->select(tournament);
69 ECF_LOG(state, 5, "The worst from the tournament: " + worst->toString());
70
71 // remove pointer to 'worst' individual from vector 'tournament'
72 removeFrom(worst, tournament);
73
74 // generate new individual, replace worst
75 IndividualP child (tournament[0]->copy());
76 replaceWith(worst, child);
77
78 // geometric recombination of the first two (remaining) individuals in the tournament
79 PathP path = getProblem()->initPathTo(tournament[0], tournament[1]);
80 int l = getProblem()->getPathLength(path);
81 if (l > 1) {
82 l -= state->getRandomizer()->getRandomInteger(l/2) + 1;
83 /* This depends on the current path length being updated by
84 * nextRandomMove() and assumes that calling getPathLength() is
85 * cheap. */
86 while (getProblem()->getPathLength(path) > l) {
87 MoveP move = getProblem()->nextRandomMove(path);
88 getProblem()->applyMove(child, move);
89 }
90 }
91 // (original code form ECF: crossover)
92 //mate(tournament[0], tournament[1], worst);
93
94 // single-step mutation of new individual
95 if(state->getRandomizer()->getRandomDouble() <= mutation_->getIndMutProb()) {
96 MoveP move = getProblem()->randomMove(child);
97 getProblem()->applyMove(child, move);
98 }
99 // (original code from ECF: perform mutation on new individual)
100 //mutate(worst);
101
102 // create new fitness
103 evaluate(child);
104 ECF_LOG(state, 5, "New individual: " + child->toString());
105 }
106
107 return true;
108}
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
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
MutationP mutation_
sptr to container of mutation operators (set by the system)
Definition: Algorithm.h:81
void evaluate(IndividualP ind)
Helper function: evaluate an individual.
Definition: Algorithm.h:157
virtual void registerParameters(StateP)
Register evaluator parameters. Called before EvaluateOp::initialize method.
Definition: EvaluateOp.h:27
virtual bool initialize(StateP)
Initialize the evaluator. Called before first evaluation occurs.
Definition: EvaluateOp.h:32
Individual class - inherits a vector of Genotype objects.
Definition: Individual.h:12
Random individual selection operator.
Definition: SelRandomOp.h:12
Worst individual selection operator.
Definition: SelWorstOp.h:11
uint nTournament_
tournament size
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.
bool advanceGeneration(StateP state, DemeP deme)
Perform a single generation on a single deme.
Definition: flowshop.h:78