GENEIAL  0.2=/
 All Classes Pages
PopulationUnchangedCriterion.h
1 #pragma once
2 
3 #include <geneial/algorithm/criteria/StatefulStoppingCriterion.h>
4 
5 geneial_private_namespace(geneial)
6 {
7 geneial_private_namespace(algorithm)
8 {
9 geneial_private_namespace(stopping_criteria)
10 {
11 using ::geneial::utility::Printable;
12 using ::geneial::population::management::BaseManager;
13 
14 geneial_export_namespace
15 {
16 using namespace geneial::algorithm::stopping_criteria;
17 
18 //TODO (bewo) Think about this class semantics...
19 template<typename FITNESS_TYPE>
20 class PopulationUnchangedCriterion: StatefulStoppingCriterion<FITNESS_TYPE>
21 {
22 public:
23  PopulationUnchangedCriterion() :
24  _wasEverInvoked(false), _lastGenerationOldestAge(0), _lastGenerationYoungestAge(0)
25  {
26  }
27 
28  virtual ~PopulationUnchangedCriterion()
29  {
30  }
31 
32  virtual bool wasStatefullyReached(BaseManager<FITNESS_TYPE> &manager)
33  {
34  bool result = false;
35 
36  if (!_wasEverInvoked)
37  {
38  _wasEverInvoked = true;
39  }
40  else
41  {
42  //TODO(bewo) is this correct?
43  //Check the oldest age of the youngest chromosome against the previous run and if it has not changed abort
44  result = (_lastGenerationOldestAge == manager.getPopulation().getYoungestChromosome()->getAge() - 1);
45  }
46 
47  _lastGenerationOldestAge = manager.getPopulation().getOldestChromosome()->getAge();
48  _lastGenerationYoungestAge = manager.getPopulation().getYoungestChromosome()->getAge();
49 
50  return result;
51  }
52 
53  virtual void print(std::ostream& os) const
54  {
55  os << "Population Unchanged ()";
56  }
57 
58 private:
59  //What was the last generation that was checked?
60  unsigned int _wasEverInvoked;
61 
62  //Last generation age?
63  unsigned int _lastGenerationOldestAge;
64  unsigned int _lastGenerationYoungestAge;
65 };
66 
67 } /* geneial_export_namespace */
68 } /* private namespace stopping_criteria */
69 } /* private namespace algorithm */
70 } /* private namespace geneial */