GENEIAL  0.2=/
 All Classes Pages
BaseChromosome.h
1 #pragma once
2 
3 #include <geneial/namespaces.h>
4 #include <geneial/utility/mixins/Printable.h>
5 #include <geneial/core/fitness/Fitness.h>
6 #include <geneial/core/fitness/FitnessEvaluator.h>
7 
8 #include <iostream>
9 #include <memory>
10 #include <cassert>
11 
12 
13 geneial_private_namespace(geneial)
14 {
15 geneial_private_namespace(population)
16 {
17 geneial_private_namespace(chromosome)
18 {
19 using ::geneial::utility::Printable;
20 
21 geneial_export_namespace
22 {
23 
27 template<typename FITNESS_TYPE>
28 class BaseChromosome: public Printable, public std::enable_shared_from_this<BaseChromosome<FITNESS_TYPE> >
29 {
30 public:
31  static const int CHROMOSOME_AGE_UNITIALIZED = 0;
32 
33  using chromosome_age = unsigned int;
34  using chromsome_hash = std::size_t;
35 
36  using ptr = std::shared_ptr<BaseChromosome<FITNESS_TYPE>>;
37  using const_ptr =std::shared_ptr<const BaseChromosome<FITNESS_TYPE>>;
38 
39  ptr getPtr()
40  {
41  return this->shared_from_this();
42  }
43 
44  const_ptr getConstPtr()
45  {
46  return this->shared_from_this();
47  }
48 
52  explicit BaseChromosome(typename FitnessEvaluator<FITNESS_TYPE>::ptr fitnessEvaluator) :
53  _fitness(nullptr), _fitnessEvaluator(fitnessEvaluator), _age(CHROMOSOME_AGE_UNITIALIZED)
54  {
55  assert(_fitnessEvaluator);
56  }
57 
58  virtual ~BaseChromosome()
59  {
60  }
61 
62  virtual bool equals(const BaseChromosome<FITNESS_TYPE> &chromosome) const = 0;
63 
68  chromosome_age doAge();
69 
73  chromosome_age getAge() const;
74 
75  void setAge(const chromosome_age age);
76 
77  virtual void print(std::ostream& os) const = 0;
78 
79  bool inline hasFitness() const
80  {
81  //Note (bewo) cast to bool will yield to boost::optional evaluation
82  //return _fitness.is_initialized();
83  return _fitness != nullptr;
84  }
85 
90  const Fitness<FITNESS_TYPE>& getFitness() const;
91 
100  void invalidateFitness();
101 
102 
103  //"Resets" the whole chromosome in case where the chromosome is recycled from the holdoff set.
104  inline void invalidate()
105  {
106  invalidateFitness();
107  setAge(CHROMOSOME_AGE_UNITIALIZED);
108  //Allow for child classes to do their own magic.
109  doInvalidate();
110  }
111 
112  virtual void doInvalidate()
113  {
114  }
115 
119  void setFitness(typename std::unique_ptr<Fitness<FITNESS_TYPE>> fitness);
120 
121  const typename FitnessEvaluator<FITNESS_TYPE>::ptr getFitnessEvaluator() const;
122 
123  void setFitnessEvaluator(const typename FitnessEvaluator<FITNESS_TYPE>::ptr& fitnessEvaluator);
124 
125  virtual chromsome_hash getHash() const = 0;
126 
127 protected:
128 
129  virtual bool hashEquals(const BaseChromosome<FITNESS_TYPE> &chromosome) const;
130 
131  virtual void printHash(std::ostream& os) const;
132 
133 private:
134  mutable std::unique_ptr<Fitness<FITNESS_TYPE>> _fitness;
135 
136  typename FitnessEvaluator<FITNESS_TYPE>::ptr _fitnessEvaluator;
137 
138  chromosome_age _age;
139 };
140 
141 } /* geneial_export_namespace */
142 } /* private namespace chromosome */
143 } /* private namespace population */
144 } /* private namespace geneial */
145 
146 #include <geneial/core/population/chromosome/BaseChromosome.hpp>
147