Grail (C++)  1.4.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
EvoParam.hh
1 // Copyright QED Software 2023.
2 
3 #ifndef GRAIL_EVO_PARAM_H
4 #define GRAIL_EVO_PARAM_H
5 
6 #include <random>
7 #include <string>
8 #include <algorithm>
9 
10 namespace grail
11 {
12 namespace evolution
13 {
21  {
22  public:
27  BaseEvoParam(size_t domainLength);
28 
31  void Randomize(std::mt19937_64& rand_gen);
32 
35  const BaseEvoParam* SetNext();
36 
39  const BaseEvoParam* SetPrev();
40 
44 
48 
51  float GetNormalizedPositionWeight() const;
52 
55  size_t GetPositionIndex() const;
56 
59  void SetPositionIndex(size_t position);
60 
62  virtual std::string ToString() const;
63 
65  size_t GetDomainLength() const;
66 
67  protected:
71  const size_t domainLength = 1;
72 
73  private:
77  size_t positionIndex;
78  };
79 
86  template <class T>
87  class EvoParam final : public BaseEvoParam
88  {
89  public:
94  EvoParam(std::initializer_list<T> argsDomain)
95  : BaseEvoParam(argsDomain.size())
96  {
97  domain.reserve(argsDomain.size());
98  std::copy(argsDomain.begin(), argsDomain.end(), std::back_inserter(domain));
99  }
100 
105  EvoParam(std::vector<T>& argsDomain)
106  : BaseEvoParam(argsDomain.size())
107  {
108  domain.reserve(argsDomain.size());
109  std::copy(argsDomain.begin(), argsDomain.end(), std::back_inserter(domain));
110  }
111 
116  const T Value() const
117  {
118  return domain[GetPositionIndex()];
119  }
120 
124  operator T() const
125  {
126  return Value();
127  }
128 
129  private:
130  std::vector<T> domain{};
131  };
132 }
133 }
134 #endif //GRAIL_EVO_PARAM_H
grail::evolution::BaseEvoParam
BaseEvoParam - A class that represents a parameter that is modifiable (optimizable) by evolutionary a...
Definition: EvoParam.hh:20
grail::evolution::BaseEvoParam::GetNormalizedPositionWeight
float GetNormalizedPositionWeight() const
The normalized position, between 0.0 and 1.0, is the ratio of the current position index to the domai...
Definition: EvoParam.cpp:55
grail::evolution::EvoParam::EvoParam
EvoParam(std::initializer_list< T > argsDomain)
Constructs a new EvoParam instance with the given possible values.
Definition: EvoParam.hh:94
grail::evolution::EvoParam
EvoParam A class for defining evolutionary-optimizable parameters. They can be registered in Individu...
Definition: EvoParam.hh:87
grail::evolution::BaseEvoParam::SetPrevClamped
const BaseEvoParam * SetPrevClamped()
Sets to the previous possible value in the domain (clamped, i.e. first.prev -> first).
Definition: EvoParam.cpp:41
grail::evolution::BaseEvoParam::GetPositionIndex
size_t GetPositionIndex() const
Returns the position of the parameter. BaseEvoParam uses PositionIndex to point to an element from it...
Definition: EvoParam.cpp:60
grail::evolution::BaseEvoParam::domainLength
const size_t domainLength
domainLength - the number of distinct values BaseEvoParam may take.
Definition: EvoParam.hh:71
grail::evolution::BaseEvoParam::SetPrev
const BaseEvoParam * SetPrev()
Sets to the previous possible value in the domain (wrapped, i.e. first.prev -> last).
Definition: EvoParam.cpp:21
grail::evolution::EvoParam::EvoParam
EvoParam(std::vector< T > &argsDomain)
Constructs a new EvoParam instance with the given possible values.
Definition: EvoParam.hh:105
grail::evolution::BaseEvoParam::BaseEvoParam
BaseEvoParam(size_t domainLength)
BaseEvoParam - Constructor.
Definition: EvoParam.cpp:80
grail::evolution::BaseEvoParam::SetPositionIndex
void SetPositionIndex(size_t position)
Sets the position of the parameter. Setting this index makes the parameter hold the PositionIndex-ith...
Definition: EvoParam.cpp:65
grail::evolution::BaseEvoParam::Randomize
void Randomize(std::mt19937_64 &rand_gen)
Sets to a random possible value from a domain.
Definition: EvoParam.cpp:10
grail::evolution::EvoParam::Value
const T Value() const
Value - gets the current value that the parameter is holding.
Definition: EvoParam.hh:116
grail::evolution::BaseEvoParam::GetDomainLength
size_t GetDomainLength() const
Gets the number of distinct values BaseEvoParam may take.
Definition: EvoParam.cpp:75
grail::evolution::BaseEvoParam::SetNext
const BaseEvoParam * SetNext()
Sets to the next possible value in the domain (wrapped, i.e. last.next -> first).
Definition: EvoParam.cpp:15
grail::evolution::BaseEvoParam::SetNextClamped
const BaseEvoParam * SetNextClamped()
Sets to the next possible value in the domain (clamped, i.e. last.next -> last).
Definition: EvoParam.cpp:35
grail::evolution::BaseEvoParam::ToString
virtual std::string ToString() const
ToString - returns a string representation of the current value hold by the parameter.
Definition: EvoParam.cpp:70