GENEIAL  0.2=/
 All Classes Pages
BaseChromosome.hpp
1 #pragma once
2 
3 #include <geneial/namespaces.h>
4 #include <geneial/core/population/chromosome/BaseChromosome.h>
5 #include <geneial/core/fitness/Fitness.h>
6 
7 #include <boost/version.hpp>
8 
9 #include <cstring> //memcmp
10 
11 geneial_private_namespace(geneial)
12 {
13 geneial_private_namespace(population)
14 {
15 geneial_private_namespace(chromosome)
16 {
17 
18 geneial_export_namespace
19 {
20 template<typename FITNESS_TYPE>
21 const Fitness<FITNESS_TYPE>& BaseChromosome<FITNESS_TYPE>::getFitness() const
22 {
23 
24  //Note that _fitness is mutable and should be only computed if needed.
25 
26  //Note that _fitness is const wrt. the chromosome contents, only the actual computation is held off until firstly needed.
27 
28  if (_fitnessEvaluator && !hasFitness())
29  {
30  //if we have a fitness evaluator assigned to the chromosome, but no fitness was calculated yet, do so.
31  assert(_fitnessEvaluator);
32  assert(this);
33  _fitness = (std::move(_fitnessEvaluator->evaluate(*this)));
34  }
35 
36  //No fitness evaluator -> undefined, probably null but no guarantee.
37  return *_fitness;
38 
39 }
40 
41 template<typename FITNESS_TYPE>
42 bool BaseChromosome<FITNESS_TYPE>::hashEquals(const BaseChromosome<FITNESS_TYPE> &chromosome) const
43 {
44  const typename BaseChromosome<FITNESS_TYPE>::chromsome_hash hashA = chromosome.getHash();
45  const typename BaseChromosome<FITNESS_TYPE>::chromsome_hash hashB = this->getHash();
46 
47  if (memcmp((void*) &hashA, (void*) &hashB, sizeof(hashA)) == 0)
48  {
49  return true;
50  }
51  else
52  {
53  return false;
54  }
55 }
56 
57 template<typename FITNESS_TYPE>
58 void BaseChromosome<FITNESS_TYPE>::printHash(std::ostream& os) const
59 {
60  os << this->getHash();
61 }
62 
63 template<typename FITNESS_TYPE>
64 void BaseChromosome<FITNESS_TYPE>::setFitness(typename std::unique_ptr<Fitness<FITNESS_TYPE>> fit)
65 {
66  _fitness = std::move(fit);
67 }
68 
69 template<typename FITNESS_TYPE>
70 const typename FitnessEvaluator<FITNESS_TYPE>::ptr BaseChromosome<FITNESS_TYPE>::getFitnessEvaluator() const
71 {
72  return _fitnessEvaluator;
73 }
74 
75 template<typename FITNESS_TYPE>
76 void BaseChromosome<FITNESS_TYPE>::invalidateFitness()
77 {
78  _fitness = nullptr;
79 }
80 
81 template<typename FITNESS_TYPE>
82 void BaseChromosome<FITNESS_TYPE>::setFitnessEvaluator(
83  const typename FitnessEvaluator<FITNESS_TYPE>::ptr& fitnessEvaluator)
84 {
85  _fitnessEvaluator = fitnessEvaluator;
86 }
87 
88 template<typename FITNESS_TYPE>
89 unsigned int BaseChromosome<FITNESS_TYPE>::doAge()
90 {
91  return ++_age;
92 }
93 
94 template<typename FITNESS_TYPE>
95 void BaseChromosome<FITNESS_TYPE>::setAge(unsigned int age)
96 {
97  _age = age;
98 }
99 
100 template<typename FITNESS_TYPE>
101 unsigned int BaseChromosome<FITNESS_TYPE>::getAge() const
102 {
103  return _age;
104 }
105 
106 } /* geneial_export_namespace */
107 } /* private namespace chromosome */
108 } /* private namespace population */
109 } /* private namespace geneial */
110