3 #include <geneial/namespaces.h>
4 #include <geneial/core/population/Population.h>
5 #include <geneial/core/fitness/Fitness.h>
15 geneial_private_namespace(geneial)
17 geneial_private_namespace(population)
19 using ::geneial::population::chromosome::BaseChromosome;
20 using ::geneial::population::management::BaseManager;
21 using ::geneial::Fitness;
23 geneial_export_namespace
26 template<
typename FITNESS_TYPE>
27 Population<FITNESS_TYPE>::Population() :
35 template<
typename FITNESS_TYPE>
36 Population<FITNESS_TYPE>::~Population()
40 template<
typename FITNESS_TYPE>
41 void Population<FITNESS_TYPE>::print(std::ostream& os)
const
43 os <<
"Population: Age (" << _age <<
"), #Chromosomes (" << _fitnessMap.size() <<
"):" << std::endl;
44 os <<
" Chromosomes" << std::endl;
46 std::for_each (_fitnessMap.cbegin(), _fitnessMap.cend(), [&os](
const typename Population<FITNESS_TYPE>::fitnessmap_value_type &c){
54 template<
typename FITNESS_TYPE>
55 typename Population<FITNESS_TYPE>::population_size Population<FITNESS_TYPE>::getSize()
const
57 return _fitnessMap.size();
60 template<
typename FITNESS_TYPE>
61 unsigned int Population<FITNESS_TYPE>::getAge()
const
66 template<
typename FITNESS_TYPE>
67 void Population<FITNESS_TYPE>::setAge(
unsigned int age)
75 template<
typename FITNESS_TYPE>
76 void Population<FITNESS_TYPE>::doAge()
79 std::for_each (_hashMap.cbegin(), _hashMap.cend(), [](
const typename Population<FITNESS_TYPE>::hashmap_value_type &c){
95 template<
typename FITNESS_TYPE>
96 inline unsigned int Population<FITNESS_TYPE>::removeDuplicates(chromosome_container &toCheck)
98 unsigned int removed = 0;
99 std::set<typename BaseChromosome<FITNESS_TYPE>::chromsome_hash> tmpHashSet;
101 for (
auto it = toCheck.begin(); it != toCheck.end();)
103 const typename BaseChromosome<FITNESS_TYPE>::chromsome_hash hashValue = (*it)->getHash();
105 if (tmpHashSet.find(hashValue) != tmpHashSet.end() || hashExists(hashValue))
107 it = toCheck.erase(it);
112 tmpHashSet.insert(hashValue);
120 template<
typename FITNESS_TYPE>
121 typename BaseChromosome<FITNESS_TYPE>::ptr Population<FITNESS_TYPE>::getOldestChromosome()
123 typename BaseChromosome<FITNESS_TYPE>::ptr oldest;
124 for (
const auto& fmv : _fitnessMap)
132 if (oldest->getAge() < fmv.second->getAge())
141 template<
typename FITNESS_TYPE>
142 typename BaseChromosome<FITNESS_TYPE>::ptr Population<FITNESS_TYPE>::getYoungestChromosome()
144 typename BaseChromosome<FITNESS_TYPE>::ptr youngest;
145 for (
const auto& fmv : _fitnessMap)
149 youngest = fmv.second;
153 if (youngest->getAge() > fmv.second->getAge())
155 youngest = fmv.second;
162 template<
typename FITNESS_TYPE>
163 inline bool Population<FITNESS_TYPE>::insertChromosome(
const typename BaseChromosome<FITNESS_TYPE>::ptr& chromosome)
166 typename BaseChromosome<FITNESS_TYPE>::chromsome_hash hashValue = chromosome->getHash();
167 if (!hashExists(hashValue))
169 _insertChromosome(chromosome, hashValue);
178 template<
typename FITNESS_TYPE>
179 inline void Population<FITNESS_TYPE>::_insertChromosome(
const typename BaseChromosome<FITNESS_TYPE>::ptr& chromosome,
180 typename BaseChromosome<FITNESS_TYPE>::chromsome_hash hashValue)
195 fitnessmap_value_type fitness_map_value(chromosome->getFitness().get(), chromosome);
196 _fitnessMap.insert(fitness_map_value);
198 hashmap_value_type hash_map_value(hashValue, chromosome);
199 _hashMap.insert(hash_map_value);
202 template<
typename FITNESS_TYPE>
203 inline unsigned int Population<FITNESS_TYPE>::insertChromosomeContainer(chromosome_container &container)
205 std::vector<typename BaseChromosome<FITNESS_TYPE>::chromsome_hash> hashCache;
206 hashCache.reserve(container.size());
207 for (
typename chromosome_container::iterator it = container.begin(); it != container.end();)
209 const typename BaseChromosome<FITNESS_TYPE>::chromsome_hash hashValue = (*it)->getHash();
210 const bool inHashCache = std::find(hashCache.begin(), hashCache.end(), hashValue) != hashCache.end();
211 if (!hashExists(hashValue) && !inHashCache)
213 hashCache.emplace_back(hashValue);
219 it = container.erase(it);
223 for (
size_t i = 0; i < container.size(); i++)
225 if (!container[i]->hasFitness())
227 getManager().getExecutionManager().addTask([i,&container]
232 container[i]->getFitness().get();
244 getManager().getExecutionManager().waitForTasks();
247 std::for_each(container.cbegin(),container.cend(),
249 (
const typename chromosome_container::value_type &it)
251 this->_insertChromosome(it, hashCache[i]);
255 return hashCache.size();
258 template<
typename FITNESS_TYPE>
259 inline void Population<FITNESS_TYPE>::removeChromosome(
const typename BaseChromosome<FITNESS_TYPE>::ptr &chromosome)
261 const FITNESS_TYPE fitness = chromosome->getFitness().get();
263 const auto hash = chromosome->getHash();
264 const auto range = _fitnessMap.equal_range(fitness);
266 bool candidateFound =
false;
268 auto it = range.first;
269 assert(range.first != _fitnessMap.end());
272 while (!candidateFound && it != range.second)
274 if (it->second == chromosome)
276 candidateFound =
true;
284 assert(candidateFound);
285 const auto hit = _hashMap.find(hash);
286 assert(hit != _hashMap.end());
288 _fitnessMap.erase(it);
296 template<
typename FITNESS_TYPE>
297 inline bool Population<FITNESS_TYPE>::hashExists(
const typename BaseChromosome<FITNESS_TYPE>::chromsome_hash hashValue)
299 return (_hashMap.find(hashValue) != _hashMap.end());
302 template<
typename FITNESS_TYPE>
303 inline typename BaseChromosome<FITNESS_TYPE>::ptr Population<FITNESS_TYPE>::getChromosomeByHash(
304 const typename BaseChromosome<FITNESS_TYPE>::chromsome_hash hashValue)
306 const auto it = _hashMap.find - (hashValue);
307 if (it == _hashMap.end())
309 typename BaseChromosome<FITNESS_TYPE>::ptr null_ptr(
nullptr);
318 template<
typename FITNESS_TYPE>
319 inline void Population<FITNESS_TYPE>::replacePopulation(chromosome_container &replacementPopulation)
322 insertChromosomeContainer(replacementPopulation);
325 template<
typename FITNESS_TYPE>
326 inline void Population<FITNESS_TYPE>::removeChromosomeContainer(
const chromosome_container &container)
328 for(
const auto& chromosomomeToRemove: container)
330 removeChromosome(chromosomomeToRemove);
334 template<
typename FITNESS_TYPE>
335 inline void Population<FITNESS_TYPE>::clearChromosomes()