3#include "AlgGeneticAnnealing.h"
6GeneticAnnealing::GeneticAnnealing()
9 name_ =
"GeneticAnnealing";
11 selBestOp_ =
static_cast<SelBestOpP
> (
new SelBestOp);
18 "total starting energy bank (fitness dependant)");
20 "simulated annealing cooling factor");
22 "is the best individual preserved");
30 this->energyBank_ = *((
double*) p.get());
32 if(energyBank_ <= 0) {
33 ECF_LOG(state, 1,
"Error: GeneticAnnealing algorithm parameter 'energybank' must be greater than zero!");
38 this->coolingF_ = *((
double*) p.get());
40 if ((coolingF_ < 0)||(coolingF_>=1)){
41 ECF_LOG(state, 1,
"Error: GeneticAnnealing algorithm parameter 'coolingfactor' must be in <0, 1]!");
46 elitism_ = *((uint*) p.get()) ?
true :
false;
48 isFirstGeneration_ =
true;
50 selBestOp_->initialize(state);
58 uint N = (uint) deme->size();
60 IndividualP mutant, ind;
62 IndividualP best = selBestOp_->select(*deme);
64 if (isFirstGeneration_){
65 Th = (
double *) malloc (N*
sizeof(
double));
66 for (i = 0; i < N; i++){
68 Th[i] = ind->fitness->getValue() + energyBank_/N;
72 isFirstGeneration_ =
false;
75 for (i = 0; i < N; i++){
78 if(elitism_ && ind == best)
81 mutant = (IndividualP) ind->copy();
85 double eMutant = mutant->fitness->getValue();
87 if (eMutant <= Th[i]){
88 energyBank_ = energyBank_ + Th[i] - eMutant;
94 dE_ = energyBank_ * coolingF_ / N;
95 ECF_LOG(state, 4,
"GeneticAnnealing: energy bank=" + dbl2str(energyBank_) +
", dE_=" + dbl2str(dE_));
98 for (i = 0; i < N; i++)
uint mutate(const std::vector< IndividualP > &pool)
Helper function: send a vector of individuals to mutation.
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.
void evaluate(IndividualP ind)
Helper function: evaluate an individual.
bool advanceGeneration(StateP state, DemeP deme)
Perform a single generation on a single deme.
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.
Best individual selection operator.