ECF 1.5
AlgGeneticAnnealing.cpp
1#include "ECF_base.h"
2#include "ECF_macro.h"
3#include "AlgGeneticAnnealing.h"
4
5
6GeneticAnnealing::GeneticAnnealing()
7{
8 // define algorithm name
9 name_ = "GeneticAnnealing";
10
11 selBestOp_ = static_cast<SelBestOpP> (new SelBestOp);
12}
13
14
16{
17 registerParameter(state, "energybank", (voidP) new double(200), ECF::DOUBLE,
18 "total starting energy bank (fitness dependant)");
19 registerParameter(state, "coolingfactor", (voidP) new double (0.7), ECF::DOUBLE,
20 "simulated annealing cooling factor");
21 registerParameter(state, "elitism", (voidP) new uint (0), ECF::UINT,
22 "is the best individual preserved");
23}
24
25
27{
28 // read parameter values
29 voidP p = getParameterValue(state, "energybank");
30 this->energyBank_ = *((double*) p.get());
31
32 if(energyBank_ <= 0) {
33 ECF_LOG(state, 1, "Error: GeneticAnnealing algorithm parameter 'energybank' must be greater than zero!");
34 throw "";
35 }
36
37 p = getParameterValue(state, "coolingfactor");
38 this->coolingF_ = *((double*) p.get());
39
40 if ((coolingF_ < 0)||(coolingF_>=1)){
41 ECF_LOG(state, 1, "Error: GeneticAnnealing algorithm parameter 'coolingfactor' must be in <0, 1]!");
42 throw "";
43 }
44
45 p = getParameterValue(state, "elitism");
46 elitism_ = *((uint*) p.get()) ? true : false;
47
48 isFirstGeneration_ = true;
49
50 selBestOp_->initialize(state);
51
52 return true;
53}
54
55
56bool GeneticAnnealing::advanceGeneration(StateP state, DemeP deme)
57{
58 uint N = (uint) deme->size();
59 uint i;
60 IndividualP mutant, ind;
61
62 IndividualP best = selBestOp_->select(*deme);
63
64 if (isFirstGeneration_){
65 Th = (double *) malloc (N*sizeof(double));
66 for (i = 0; i < N; i++){
67 ind = deme->at(i);
68 Th[i] = ind->fitness->getValue() + energyBank_/N;
69 } // init of threshold
70
71 energyBank_ = 0;
72 isFirstGeneration_ = false;
73 }
74
75 for (i = 0; i < N; i++){
76 ind = deme->at(i);
77
78 if(elitism_ && ind == best)
79 continue;
80
81 mutant = (IndividualP) ind->copy();
82 mutate (mutant); // create mutant
83 evaluate(mutant);
84
85 double eMutant = mutant->fitness->getValue();
86
87 if (eMutant <= Th[i]){
88 energyBank_ = energyBank_ + Th[i] - eMutant;
89 Th[i] = eMutant;
90 replaceWith(ind, mutant); // replace individual with mutant
91 }
92 }
93
94 dE_ = energyBank_ * coolingF_ / N;
95 ECF_LOG(state, 4, "GeneticAnnealing: energy bank=" + dbl2str(energyBank_) + ", dE_=" + dbl2str(dE_));
96
97 energyBank_ = 0;
98 for (i = 0; i < N; i++)
99 Th[i] += dE_;
100
101 return true;
102}
103
104
105
uint mutate(const std::vector< IndividualP > &pool)
Helper function: send a vector of individuals to mutation.
Definition: Algorithm.h:169
std::string name_
algorithm name
Definition: Algorithm.h:23
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.
Definition: Algorithm.h:35
voidP getParameterValue(StateP state, std::string name)
Helper function: get parameter value from the system.
Definition: Algorithm.h:46
void replaceWith(IndividualP oldInd, IndividualP newInd)
Helper function: replace an individual in current deme.
Definition: Algorithm.h:187
void evaluate(IndividualP ind)
Helper function: evaluate an individual.
Definition: Algorithm.h:157
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.
Definition: SelBestOp.h:10