GENEIAL  0.2=/
 All Classes Pages
MultiValueChromosome.h
1 #pragma once
2 
3 #include <geneial/utility/Debug.h>
4 
5 #include <geneial/namespaces.h>
6 #include <geneial/core/population/chromosome/BaseChromosome.h>
7 
8 #include <cassert>
9 #include <vector>
10 #include <algorithm>
11 #include <numeric>
12 #include <memory>
13 
14 geneial_private_namespace(geneial)
15 {
16 geneial_private_namespace(population)
17 {
18 geneial_private_namespace(chromosome)
19 {
20 
21 geneial_export_namespace
22 {
23 
24 template<typename VALUE_TYPE, typename FITNESS_TYPE>
25 class MultiValueChromosome: public BaseChromosome<FITNESS_TYPE>
26 {
27 public:
28 
29  typedef typename BaseChromosome<FITNESS_TYPE>::chromsome_hash chromsome_hash;
30 
31  typedef typename std::vector<VALUE_TYPE> value_container;
32  typedef typename value_container::const_iterator const_it;
33  typedef typename value_container::iterator it;
34 
35  typedef typename std::shared_ptr<MultiValueChromosome<VALUE_TYPE, FITNESS_TYPE> > ptr;
36  typedef typename std::shared_ptr<const MultiValueChromosome<VALUE_TYPE, FITNESS_TYPE> > const_ptr;
37 
38  explicit MultiValueChromosome(typename FitnessEvaluator<FITNESS_TYPE>::ptr fitnessEvaluator) :
39  BaseChromosome<FITNESS_TYPE>(fitnessEvaluator), _container()
40  {
41  static_assert(std::is_arithmetic<VALUE_TYPE>::value,"VALUE_TYPE is expected to be an arithmetic data type!");
42  static_assert(std::is_arithmetic<FITNESS_TYPE>::value,"FITNESS_TYPE is expected to be an arithmetic data type!");
43  }
44 
45 
50  MultiValueChromosome(const MultiValueChromosome& other) :
51  BaseChromosome<FITNESS_TYPE>(other._fitnessEvaluator), _container(other._container),
52  _cacheValid(other._cacheValid), _hashCache(other._hashCache)
53  {
54  }
55 
60  MultiValueChromosome(MultiValueChromosome&& other) :
61  BaseChromosome<FITNESS_TYPE>(other._fitnessEvaluator), _container(),
62  _cacheValid(other._cacheValid), _hashCache(other._hashCache)
63  {
64  swap(other);
65  }
66 
71  void swap(MultiValueChromosome& other)
72  {
73  using std::swap;
74  swap(other._container,_container);
75  swap(other._fitnessEvaluator,this->_fitnessEvaluator);
76  swap(other._cacheValid,_cacheValid);
77  swap(other._hashCache,_hashCache);
78  }
79 
80  virtual ~MultiValueChromosome()
81  {
82  }
83 
84  bool equals(const BaseChromosome<FITNESS_TYPE> &chromosome) const override;
85 
86  const_it getConstIt() const;
87  it getIt() const;
88 
89  unsigned int getSize() const;
90 
91  it getMax() const;
92  it getMin() const;
93 
94  VALUE_TYPE getAverage() const;
95  VALUE_TYPE getRange() const;
96  VALUE_TYPE getSum() const;
97 
98  //TODO (bewo) : provide further convenience methods at this point.
99 
100  value_container& getContainer();
101 
102  const value_container& getContainer() const;
103 
104  void setValueContainer(const value_container& container);
105 
106  void setValueContainer(value_container&& container);
107 
108 
109 
110  void print(std::ostream& os) const override;
111 
112  chromsome_hash getHash() const override;
113 
114  bool inline hasCache() const;
115 
116  virtual void doInvalidate() override
117  {
118  invalidateHashCache();
119  _hashCache = 0;
120  //_container.clear();
121  }
122 
123  void inline invalidateHashCache() const;
124 
125 private:
126  value_container _container;
127 
128  mutable bool _cacheValid;
129 
130  mutable chromsome_hash _hashCache;
131 
132 };
133 
134 } /* geneial_export_namespace */
135 } /* private namespace chromosome */
136 } /* private namespace population */
137 } /* private namespace geneial */
138 
139 #include <geneial/core/population/chromosome/MultiValueChromosome.hpp>