GENEIAL  0.2=/
 All Classes Pages
Population.h
1 #pragma once
2 
3 #include <geneial/namespaces.h>
4 #include <geneial/core/population/chromosome/BaseChromosome.h>
5 #include <geneial/core/population/ContainerTypes.h>
6 #include <geneial/utility/mixins/Printable.h>
7 
8 #include <iostream>
9 #include <map>
10 #include <vector>
11 #include <unordered_map>
12 #include <memory>
13 
14 
15 geneial_private_namespace(geneial)
16 {
17 geneial_private_namespace(population)
18 {
19 geneial_private_namespace(management)
20 {
21 geneial_export_namespace
22 {
23 template<typename FITNESS_TYPE>
24  class BaseManager;
25 }
26 }
27 }
28 }
29 
30 geneial_private_namespace(geneial)
31 {
32 geneial_private_namespace(population)
33 {
34  using ::geneial::utility::Printable;
35  using ::geneial::population::management::BaseManager;
36 geneial_export_namespace
37 {
38 
39 template<typename FITNESS_TYPE>
40 class Population: public Printable
41 {
42 private:
43  Population();
44 public:
45 
46  friend class BaseManager<FITNESS_TYPE>;
47 
48  const static int POPULATION_AGE_INITIAL = 0;
49 
50  //TODO(bewo): cleanup this typedef mess
51  typedef unsigned int population_age;
52  typedef unsigned int population_size;
53 
54  //Alias chromomsome container
55  typedef typename ContainerTypes<FITNESS_TYPE>::chromosome_container chromosome_container;
56 
57  //In our case we do not need to rehash so we redirect
58  struct IDHash
59  {
60  inline size_t operator() (const typename BaseChromosome<FITNESS_TYPE>::chromsome_hash &hash) const
61  {
62  return hash;
63  }
64  };
65 
66  //A map containing all the chromosomes hash values.
67  typedef typename std::unordered_map<typename BaseChromosome<FITNESS_TYPE>::chromsome_hash,
68  typename BaseChromosome<FITNESS_TYPE>::ptr> hash_map;
69 
70 
71  typedef typename hash_map::value_type hashmap_value_type;
72  typedef typename hash_map::key_type hashmap_key_type;
73 
74  typedef typename hash_map::const_iterator hashmap_const_it;
75  typedef typename hash_map::iterator hashmap_it;
76 
77  //A fitness <-> chromsome map holding the actual population, multiple chromosomes might have same fitness
78  typedef typename std::multimap<FITNESS_TYPE, typename BaseChromosome<FITNESS_TYPE>::ptr> fitness_map;
79 
80  typedef typename fitness_map::value_type fitnessmap_value_type;
81  typedef typename fitness_map::key_type fitnessmap_key_type;
82 
83  typedef typename fitness_map::const_iterator fitnessmap_const_it;
84  typedef typename fitness_map::iterator fitnessmap_it;
85 
86 // typedef typename std::unordered_map<typename BaseChromosome<FITNESS_TYPE>::chromsome_hash,FITNESS_TYPE> fitness_cache;
87 
88  population_size getSize() const;
89 
90  //TODO(bewo) Population(Population &other);
91 
92  //TODO(bewo) Population(Population &&other);
93 
94  virtual ~Population();
95 
96  virtual void print(std::ostream& os) const;
97 
98  population_age getAge() const;
99 
100  void setAge(population_age age);
101 
102  void doAge();
103 
104  typename BaseChromosome<FITNESS_TYPE>::ptr getOldestChromosome();
105 
106  typename BaseChromosome<FITNESS_TYPE>::ptr getYoungestChromosome();
107 
108  const inline fitness_map& getFitnessMap() const
109  {
110  return _fitnessMap;
111  }
112 
113  const inline hash_map& getHashMap() const
114  {
115  return _hashMap;
116  }
117 
118  bool hashExists(const typename BaseChromosome<FITNESS_TYPE>::chromsome_hash);
119 
120  unsigned int removeDuplicates(chromosome_container &toCheck);
121 
122  typename BaseChromosome<FITNESS_TYPE>::ptr getChromosomeByHash(
123  const typename BaseChromosome<FITNESS_TYPE>::chromsome_hash);
124 
125  void replacePopulation(chromosome_container &replacementPopulation);
126 
127  unsigned int insertChromosomeContainer(chromosome_container &container);
128 
129  bool insertChromosome(const typename BaseChromosome<FITNESS_TYPE>::ptr &chromosome);
130 
131  void removeChromosomeContainer(const chromosome_container &container);
132 
133  void removeChromosome(const typename BaseChromosome<FITNESS_TYPE>::ptr &chromosome);
134 
135  void clearChromosomes();
136 
137  BaseManager<FITNESS_TYPE>& getManager() const
138  {
139  assert(!_manager.expired() && "Manager is already expired. Something was not properly bootstrapped");
140  const auto manager = _manager.lock();
141  return *manager;
142  }
143 
144 private:
145 
146  void _insertChromosome(const typename BaseChromosome<FITNESS_TYPE>::ptr &chromosome,
147  typename BaseChromosome<FITNESS_TYPE>::chromsome_hash hashValue);
148 
149  fitness_map _fitnessMap;
150 
151  hash_map _hashMap;
152 
153 // fitness_cache _fitnessCache;
154 
155  population_age _age;
156 
157  std::weak_ptr<BaseManager<FITNESS_TYPE>> _manager;
158 
159 };
160 
161 } /* geneial_export_namespace */
162 } /* private namespace population */
163 } /* private namespace geneial */
164 #include <geneial/core/population/Population.hpp>
165