1#include "ECF/ECF_base.h"
2#include "SolverEvolutionStrategy.h"
6SolverEvolutionStrategy::SolverEvolutionStrategy()
9 name_ =
"SolverEvolutionStrategy";
12 selBestOp_ =
static_cast<SelectionOperatorP
> (
new SelBestOp);
13 selRandomOp_ =
static_cast<SelectionOperatorP
> (
new SelRandomOp);
20 "number of offspring created in each iteration (default: 4)");
22 "number of parents used to create an offspring; may be 1 or 2 (default: 1)");
24 "the size of parent population (default: 1)");
25 registerParameter(state,
"selection", (voidP)
new std::string(
"plus"), ECF::STRING,
26 "selection scheme: \"plus\", uses both parents and offspring) or \"comma\", uses just offspring (default: plus)");
33 selBestOp_->initialize(state);
34 selRandomOp_->initialize(state);
38 lambda_ = *((uint*) sizep.get());
41 rho_ = *((uint*) rhop.get());
43 if(rho_ < 1 || rho_ > 2) {
44 ECF_LOG_ERROR(state,
"Error: number of parents to create an offspring in EvolutionStrategy can only be 1 or 2!");
49 mu_ = *((uint*) mup.get());
52 ECF_LOG_ERROR(state,
"Error: size of parent population in EvolutionStrategy must be greater than number of parents to create an offspring!");
57 voidP sptr = state->getRegistry()->getEntry(
"population.size");
58 uint demeSize = *((uint*) sptr.get());
59 if(demeSize %
mu_ != 0 ||
mu_ < 1) {
60 ECF_LOG_ERROR(state,
"Error: \"population.size\" parameter must be a multiple of size of parent population (mu) in EvolutionStrategy algorithm!");
65 std::string sels = *((std::string*) selp.get());
68 else if(sels ==
"comma")
71 ECF_LOG_ERROR(state,
"Error: selection type in EvolutionStrategy can only be \"plus\" or \"comma\"!");
76 ECF_LOG_ERROR(state,
"Error: offspring number (lambda) in comma EvolutionStrategy must be greater than the number of parents (mu)!");
89 for(uint subPopulation = 0; subPopulation <
subPopulations_; subPopulation++) {
92 uint firstInd = subPopulation *
mu_;
93 uint lastInd = (subPopulation + 1) *
mu_;
96 std::vector<IndividualP> parents;
97 for(uint ind = firstInd; ind < lastInd; ind++)
98 parents.push_back(deme->at(ind));
101 std::vector<IndividualP> offspring;
102 for(uint iChild = 0; iChild <
lambda_; iChild++) {
107 IndividualP parent = selRandomOp_->select(parents);
108 child =
copy(parent);
109 MoveP
move = getProblem()->randomMove(child);
110 getProblem()->applyMove(child,
move);
114 IndividualP p1 = selRandomOp_->select(parents);
117 p2 = selRandomOp_->select(parents);
121 offspring.push_back(child);
127 std::vector<IndividualP> selPool;
131 selPool.insert(selPool.end(), offspring.begin(), offspring.end());
138 std::sort(selPool.begin(), selPool.end(), &SolverEvolutionStrategy::compare);
142 for(uint ind = firstInd; ind < lastInd; ind++, selected++)
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.
Best individual selection operator.
Random individual selection operator.
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).
uint rho_
number of parents (1 or 2)
bool advanceGeneration(StateP state, DemeP deme)
Perform a single generation on a single deme.
uint lambda_
number of offspring
uint mu_
the size of the parent population
uint subPopulations_
how many parent populations are in a deme
bool plusSelection_
type of selection (plus or comma)