3#include "AlgEvolutionStrategy.h"
5#include "SelRandomOp.h"
9EvolutionStrategy::EvolutionStrategy()
12 name_ =
"EvolutionStrategy";
15 selBestOp_ =
static_cast<SelectionOperatorP
> (
new SelBestOp);
16 selRandomOp_ =
static_cast<SelectionOperatorP
> (
new SelRandomOp);
23 "number of offspring created in each iteration (default: 4)");
25 "number of parents used to create an offspring; may be 1 or 2 (default: 1)");
27 "the size of parent population (default: 1)");
28 registerParameter(state,
"selection", (voidP)
new std::string(
"plus"), ECF::STRING,
29 "selection scheme: \"plus\", uses both parents and offspring) or \"comma\", uses just offspring (default: plus)");
36 selBestOp_->initialize(state);
37 selRandomOp_->initialize(state);
41 lambda_ = *((uint*) sizep.get());
44 rho_ = *((uint*) rhop.get());
46 if(rho_ < 1 || rho_ > 2) {
47 ECF_LOG_ERROR(state,
"Error: number of parents to create an offspring in EvolutionStrategy can only be 1 or 2!");
52 mu_ = *((uint*) mup.get());
55 ECF_LOG_ERROR(state,
"Error: size of parent population in EvolutionStrategy must be greater than number of parents to create an offspring!");
60 voidP sptr = state->getRegistry()->getEntry(
"population.size");
61 uint demeSize = *((uint*) sptr.get());
62 if(demeSize %
mu_ != 0 ||
mu_ < 1) {
63 ECF_LOG_ERROR(state,
"Error: \"population.size\" parameter must be a multiple of size of parent population (mu) in EvolutionStrategy algorithm!");
68 std::string sels = *((std::string*) selp.get());
71 else if(sels ==
"comma")
74 ECF_LOG_ERROR(state,
"Error: selection type in EvolutionStrategy can only be \"plus\" or \"comma\"!");
79 ECF_LOG_ERROR(state,
"Error: offspring number (lambda) in comma EvolutionStrategy must be greater than the number of parents (mu)!");
92 for(uint subPopulation = 0; subPopulation <
subPopulations_; subPopulation++) {
95 uint firstInd = subPopulation *
mu_;
96 uint lastInd = (subPopulation + 1) *
mu_;
99 std::vector<IndividualP> parents;
100 for(uint ind = firstInd; ind < lastInd; ind++)
101 parents.push_back(deme->at(ind));
104 std::vector<IndividualP> offspring;
105 for(uint iChild = 0; iChild <
lambda_; iChild++) {
110 IndividualP parent = selRandomOp_->select(parents);
111 child =
copy(parent);
116 IndividualP p1 = selRandomOp_->select(parents);
119 p2 = selRandomOp_->select(parents);
123 offspring.push_back(child);
129 std::vector<IndividualP> selPool;
133 selPool.insert(selPool.end(), offspring.begin(), offspring.end());
140 std::sort(selPool.begin(), selPool.end(), &EvolutionStrategy::compare);
144 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.
MutationP mutation_
sptr to container of mutation operators (set by the system)
void evaluate(IndividualP ind)
Helper function: evaluate an individual.
uint rho_
number of parents (1 or 2)
uint mu_
the size of the parent population
void registerParameters(StateP state)
Register algorithm's parameters (if any).
uint lambda_
number of offspring
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.
uint subPopulations_
how many parent populations are in a deme
bool plusSelection_
type of selection (plus or comma)
Best individual selection operator.
Random individual selection operator.