GENEIAL  0.2=/
 All Classes Pages
BaseManager.h
1 #pragma once
2 
3 #include <geneial/namespaces.h>
4 #include <geneial/core/population/chromosome/BaseChromosome.h>
5 #include <geneial/core/population/Population.h>
6 #include <geneial/core/population/PopulationSettings.h>
7 #include <geneial/core/population/builder/BaseChromosomeFactory.h>
8 #include <geneial/core/population/management/Bookkeeper.h>
9 #include <geneial/utility/ExecutionManager.h>
10 #include <geneial/utility/mixins/EnableMakeShared.h>
11 
12 #include <cassert>
13 
14 geneial_private_namespace(geneial)
15 {
16 geneial_private_namespace(population)
17 {
18 geneial_private_namespace(management)
19 {
20 using ::geneial::population::chromosome::BaseChromosomeFactory;
21 using ::geneial::utility::BaseExecutionManager;
22 using ::geneial::utility::SequentialExecutionManager;
23 using ::geneial::utility::EnableMakeShared;
24 
25 geneial_export_namespace
26 {
27 
31 template<typename FITNESS_TYPE>
32 class BaseManager : public std::enable_shared_from_this<BaseManager<FITNESS_TYPE> >,
33  public EnableMakeShared<BaseManager<FITNESS_TYPE>>
34 {
35 protected:
36  explicit BaseManager<FITNESS_TYPE>(const std::shared_ptr<BaseChromosomeFactory<FITNESS_TYPE>> &chromosomeFactory) :
37  _population(),
38  _populationSettings(),
39  _chromosomeFactory(chromosomeFactory),
40  _executionManager(new SequentialExecutionManager),
41  _bookkeeper(std::make_shared<DefaultBookkeeper>())
42  {
43  }
44  BaseManager(const BaseManager& other) = delete;
45  BaseManager(const BaseManager&& other) = delete;
46 public:
47 
48  static std::shared_ptr<BaseManager<FITNESS_TYPE>> create(const std::shared_ptr<BaseChromosomeFactory<FITNESS_TYPE>> chromosomeFactory)
49  {
50  auto prototype = BaseManager<FITNESS_TYPE>::makeShared(chromosomeFactory);
51 
52  prototype->_population._manager = prototype;
53 
54  return std::move(prototype);
55  }
56 
57 
58  virtual ~BaseManager<FITNESS_TYPE>()
59  {
60  }
61 
62  void replacePopulation(typename Population<FITNESS_TYPE>::chromosome_container replacementPopulation);
63 
67  void replenishPopulation();
68 
69  typename BaseChromosome<FITNESS_TYPE>::ptr getHighestFitnessChromosome() const;
70 
71  FITNESS_TYPE getHighestFitness() const;
72 
73  typename BaseChromosome<FITNESS_TYPE>::ptr getLowestFitnessChromosome() const;
74 
75  FITNESS_TYPE getLowestFitness() const;
76 
77  Population<FITNESS_TYPE>& getPopulation()
78  {
79  return _population;
80  }
81 
82  Population<FITNESS_TYPE>& getPopulation() const
83  {
84  return const_cast<Population<FITNESS_TYPE>&>(_population);
85  }
86 
87  BaseChromosomeFactory<FITNESS_TYPE>& getChromosomeFactory() const
88  {
89  return _chromosomeFactory;
90  }
91 
92  void setChromosomeFactory(std::shared_ptr<BaseChromosomeFactory<FITNESS_TYPE>> chromosomeFactory)
93  {
94  _chromosomeFactory = chromosomeFactory;
95  }
96 
97  PopulationSettings& getPopulationSettings()
98  {
99  return _populationSettings;
100  }
101 
102  void setPopulationSettings(PopulationSettings& populationSettings)
103  {
104  _populationSettings = populationSettings;
105  }
106 
107  BaseExecutionManager& getExecutionManager() const
108  {
109  return *_executionManager;
110  }
111 
112  void setExecutionManager(std::unique_ptr<BaseExecutionManager>&& executionManager)
113  {
114  _executionManager = std::move(executionManager);
115  }
116 
117  std::shared_ptr<BaseBookkeeper> getBookkeeper() const
118  {
119  return _bookkeeper;
120  }
121 
122  void setBookkeeper(const std::shared_ptr<BaseBookkeeper>& bookkeeper)
123  {
124  _bookkeeper = bookkeeper;
125  }
126 
127 private:
128  Population<FITNESS_TYPE> _population;
129 
130  PopulationSettings _populationSettings;
131 
132  std::shared_ptr<BaseChromosomeFactory<FITNESS_TYPE>> _chromosomeFactory;
133 
134  std::unique_ptr<BaseExecutionManager> _executionManager;
135 
136  std::shared_ptr<BaseBookkeeper> _bookkeeper;
137 
138 };
139 
140 } /* geneial_export_namespace */
141 } /* private namespace management */
142 } /* private namespace population */
143 } /* private namespace geneial */
144 
145 
146 #include <geneial/core/population/management/BaseManager.hpp>