GENEIAL  0.2=/
 All Classes Pages
SteadyStateAlgorithm.hpp
1 #pragma once
2 
3 #include <geneial/algorithm/SteadyStateAlgorithm.h>
4 
5 geneial_private_namespace(geneial)
6 {
7 geneial_private_namespace(algorithm)
8 {
9 using ::geneial::population::Population;
10 
11 geneial_export_namespace
12 {
13 
14 template<typename FITNESS_TYPE>
15 void SteadyStateAlgorithm<FITNESS_TYPE>::solve()
16 {
17 
18  using chromosome_container = typename Population<FITNESS_TYPE>::chromosome_container;
19 
20  this->_wasStarted = true;
21 
22  //Initialize the first population candidate, take whatever has been inserted and fill it up to max size.
23  this->_manager->replenishPopulation();
24 
25  long long int iterationCounter = 0;
26 
27  bool wasReached = this->wasCriteriaReached();
28 
29  const auto bookkeeper = this->_manager->getBookkeeper();
30  while (!wasReached)
31  {
32  {
33  //Notify algorithm observers about new generation change.
34  {
35  ScopedEvent trace_criteria("TIME_OBSERVERS",*bookkeeper);
36  this->notifyObservers(AlgorithmObserver<FITNESS_TYPE>::BEFORE_GENERATION);
37  }
38 
39  ScopedEvent trace_iteration("TIME_ITERATION",*bookkeeper);
40 
41  ++iterationCounter;
42 
43  //Let all the chromosomes in the population age...
44  {
45  ScopedEvent trace_aging("TIME_AGING",*bookkeeper);
46  this->_manager->getPopulation().doAge();
47  }
48 
49  //Select some chromosomes from the population using the specified selection operation
50  chromosome_container mating_pool;
51  {
52  ScopedEvent trace_selection("TIME_SELECTION",*bookkeeper);
53  //Perform a selection of mating candidates based on the given strategy.
54  mating_pool = this->_selectionOperation->doSelect(this->_manager->getPopulation(), *this->_manager);
55  }
56 
64  chromosome_container offspring;
65  {
66  ScopedEvent trace_coupling("TIME_OFFSPRING",*bookkeeper);
67  offspring = this->_couplingOperation->doCopulate(mating_pool, *this->_crossoverOperation, *this->_manager);
68  }
69 
70  //Mutate some of the offspring chromosomes using the specified mutation operation
71  //TODO (bewo): Should mutation operate on the whole population instead?
72  {
73  ScopedEvent trace_mutation("TIME_MUTATION",*bookkeeper);
74  offspring = this->_mutationOperation->doMutate(offspring, *this->_manager);
75  }
76 
77  //Given the new offspring, replace members of the iteration's starting population with the offspring
78  {
79  ScopedEvent trace_replacement("TIME_REPLACEMENT",*bookkeeper);
80  this->_replacementOperation->doReplace(this->_manager->getPopulation(), mating_pool, offspring, *this->_manager);
81  }
82 
83  //In the case that the replacement removed too much chromosomes from the population we will replenish the missing chromosomes
84  {
85  ScopedEvent trace_replenishment("TIME_REPLENISHMENT",*bookkeeper);
86  //If we had a deficit, fill up population with fresh chromosomes
87  this->_manager->replenishPopulation();
88  }
89 
90  //Check whether the stopping criteria were met this iteration
91  {
92  ScopedEvent trace_criteria("TIME_CRITERIA",*bookkeeper);
93  wasReached = this->wasCriteriaReached();
94  }
95 
96  //Notify algorithm observers about new generation change.
97  {
98  ScopedEvent trace_criteria("TIME_OBSERVERS",*bookkeeper);
99  this->notifyObservers(AlgorithmObserver<FITNESS_TYPE>::AFTER_GENERATION);
100  }
101  }
102 
103  }
104 
105  this->_wasSolved = true;
106 
107 }
108 
109 
110 } /* geneial_export_namespace */
111 } /* private namespace algorithm */
112 } /* private namespace geneial */