2#include "AlgRouletteWheel.h"
6RouletteWheel :: RouletteWheel()
8 name_ =
"RouletteWheel";
11 selRandomOp =
static_cast<SelRandomOpP
> (
new SelRandomOp);
12 selBestOp =
static_cast<SelBestOpP
> (
new SelBestOp);
18 registerParameter(state,
"crxprob", (voidP)
new double(0.5), ECF::DOUBLE,
"crossover rate");
20 "selection pressure: how much is the best individual 'better' than the worst");
26 selFitPropOp->initialize(state);
27 selRandomOp->initialize(state);
28 selBestOp->initialize(state);
31 crxRate_ = *((
double*) crRateP.get());
45 IndividualP best = selBestOp->select(*deme);
49 std::vector<IndividualP> wheel;
50 wheel = selFitPropOp->selectMany(*deme, (uint) deme->size());
53 for(uint i = 0; i < wheel.size(); ++i)
54 wheel[i] =
copy(wheel[i]);
57 for(uint i = 0; i < deme->size(); i++)
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()));
66 uint noCrx = (int)(deme->size() *
crxRate_ /2);
69 for(uint i = 0; i < noCrx; i++){
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()));
77 IndividualP child1 =
copy(parent1);
78 IndividualP child2 =
copy(parent2);
81 mate(parent1, parent2, child1);
82 mate(parent1, parent2, child2);
93 for(uint i = 0; i < deme->size(); i++)
94 if(!deme->at(i)->fitness->isValid()) {
99 IndividualP random = selRandomOp->select(*deme);
100 if(best->fitness->isBetterThan(random->fitness))
uint mutate(const std::vector< IndividualP > &pool)
Helper function: send a vector of individuals to mutation.
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.
bool mate(IndividualP p1, IndividualP p2, IndividualP child)
Helper function: crossover two individuals.
void replaceWith(IndividualP oldInd, IndividualP newInd)
Helper function: replace an individual in current deme.
void evaluate(IndividualP ind)
Helper function: evaluate an individual.
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.
Fitness proportional (and inverse proportional) individual selection operator.
Random individual selection operator.