1#include "ECF/ECF_base.h"
2#include "ECF/SelRandomOp.h"
3#include "ECF/SelWorstOp.h"
4#include "SolverTournamentEA.h"
7SolverTournamentEA::SolverTournamentEA()
9 name_ =
"SolverTournamentEA";
12 selRandomOp = (SelectionOperatorP) (
new SelRandomOp);
13 selWorstOp = (SelectionOperatorP) (
new SelWorstOp);
20 "tournament size (individuals selected randomly, worst one eliminated)");
26 selRandomOp->initialize(state);
27 selWorstOp->initialize(state);
34 ECF_LOG(state, 1,
"Error: SolverTournamentEA requires minimum tournament size of 3!");
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!");
56 for(uint iIter = 0; iIter < deme->size(); iIter++) {
58 ECF_LOG(state, 5,
"Individuals in tournament: ");
60 std::vector<IndividualP> tournament;
63 tournament.push_back(selRandomOp->select(*deme));
64 ECF_LOG(state, 5, uint2str(i) +
": " + tournament[i]->toString());
68 IndividualP worst = selWorstOp->select(tournament);
69 ECF_LOG(state, 5,
"The worst from the tournament: " + worst->toString());
75 IndividualP child (tournament[0]->
copy());
79 PathP path = getProblem()->initPathTo(tournament[0], tournament[1]);
80 int l = getProblem()->getPathLength(path);
82 l -= state->getRandomizer()->getRandomInteger(l/2) + 1;
86 while (getProblem()->getPathLength(path) > l) {
87 MoveP
move = getProblem()->nextRandomMove(path);
88 getProblem()->applyMove(child,
move);
95 if(state->getRandomizer()->getRandomDouble() <=
mutation_->getIndMutProb()) {
96 MoveP
move = getProblem()->randomMove(child);
97 getProblem()->applyMove(child,
move);
104 ECF_LOG(state, 5,
"New individual: " + child->toString());
IndividualP copy(IndividualP source)
Helper function: make a copy of an individual.
std::string name_
algorithm name
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.
voidP getParameterValue(StateP state, std::string name)
Helper function: get parameter value from the system.
void replaceWith(IndividualP oldInd, IndividualP newInd)
Helper function: replace an individual in current deme.
bool removeFrom(IndividualP victim, std::vector< IndividualP > &pool)
Helper function: remove victim from pool of individual pointers.
MutationP mutation_
sptr to container of mutation operators (set by the system)
void evaluate(IndividualP ind)
Helper function: evaluate an individual.
virtual void registerParameters(StateP)
Register evaluator parameters. Called before EvaluateOp::initialize method.
virtual bool initialize(StateP)
Initialize the evaluator. Called before first evaluation occurs.
Individual class - inherits a vector of Genotype objects.
Random individual selection operator.
Worst individual selection operator.
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.