GPLIB++
AnnealingGA.cpp
Go to the documentation of this file.
1 #include "AnnealingGA.h"
2 #include <cmath>
3 
4 #include "miscfunc.h"
5 
6 #ifdef _OPENMP
7 #include <omp.h>
8 #endif
9 
10 using namespace std;
11 using namespace gplib;
12 namespace gplib
13  {
14  AnnealingGA::AnnealingGA(GeneralPropagation* const LocalPropagation,
15  GeneralPopulation* const LocalPopulation,
16  GeneralTranscribe* const LocalTranscribe,
17  const tObjectiveVector &IndObjective, const int nthreads):
18  GeneralGA(LocalPropagation, LocalPopulation, LocalTranscribe,
19  IndObjective, nthreads), InitTemperature(1.0),
20  AnnealingGeneration(1), AnnealingRatio(1.0)
21  {
22  }
23 
25  {
26  }
27 
29  {
30  return distance(Population->GetProbabilities().begin(), max_element(
31  Population->GetProbabilities().begin(),
32  Population->GetProbabilities().end()));
33  }
34 
35  void AnnealingGA::SetParams(const double InitT, const int AnnealG,
36  const double AnnealR)
37  {
38  InitTemperature = InitT;
39  AnnealingGeneration = AnnealG;
40  AnnealingRatio = AnnealR;
41  }
42 
43  void AnnealingGA::CalcProbabilities(const int iterationnumber,
44  rmat &LocalMisFit, GeneralPopulation &LocalPopulation)
45  {
46  double sum = 0;
47  const int popsize = LocalPopulation.GetPopsize();
48  double T = 1.0;
49  gplib::rvec CurrentMisFit;
50  tprobabilityv Probabilities(popsize);
51  // The temperature depends on the iteration
52  if (iterationnumber < AnnealingGeneration)
53  T = InitTemperature;
54  else
55  T = InitTemperature / AnnealingRatio;
56  // for all members of the population
57  for (int i = 0; i < popsize; ++i)
58  {
59  CurrentMisFit = column(LocalMisFit, i); //Get the misfit vector of the current member
60  Probabilities(i) = exp(-ublas::sum(CurrentMisFit) / T); //apply the annealing scheme to the summed misfits
61  sum += Probabilities(i); // add all misfit values
62  }
63  const double factor = 1. / sum;
64  for (int i = 0; i < popsize; ++i)
65  Probabilities(i) *= factor;
66  LocalPopulation.SetProbabilities(Probabilities);
67  }
68 
69  void AnnealingGA::Elitism(const int iterationnumber)
70  {
71  }
72  }
The base class for the population of a genetic algorithm, implements storage and access functions...
ublas::vector< double > tprobabilityv
Definition: gentypes.h:17
GeneralPopulation *const Population
A pointer to an object holding the population.
Definition: GeneralGA.h:74
const float T[frequenzen]
void SetProbabilities(const tprobabilityv &LocalProb)
virtual void CalcProbabilities(const int iterationnumber, gplib::rmat &LocalMisFit, GeneralPopulation &LocalPopulation)
Calculate the selection probabilities given the iterationnumber, misfit and population to store the r...
Definition: AnnealingGA.cpp:43
The base class for genetic algorithm propagation methods.
General Transcribe base class for genetic algorithm parameter transcription.
virtual void Elitism(const int iterationnumber)
The implementation of Elitism for the AnnealingGA, in this case this function has no effect...
Definition: AnnealingGA.cpp:69
General genetic algorithm class.
Definition: GeneralGA.h:25
int GetBestIndex()
The index of the best population member.
Definition: AnnealingGA.cpp:28
const tprobabilityv & GetProbabilities() const
virtual ~AnnealingGA()
Definition: AnnealingGA.cpp:24
void SetParams(const double InitT, const int AnnealG, const double AnnealR)
Set the parameters for the annealing process.
Definition: AnnealingGA.cpp:35
std::vector< boost::shared_ptr< GeneralObjective > > tObjectiveVector
Definition: GeneralGA.h:31