(C++)  1.0.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
EvoParam.hh
1 #ifndef GRAIL_EVO_PARAM_H
2 #define GRAIL_EVO_PARAM_H
3 
4 #include "IEvoParam.hh"
5 
6 #include <vector>
7 #include <iostream>
8 #include <string>
9 
10 namespace grail
11 {
12  namespace evolution
13  {
14  /*
15  A class for defining evolutionary-optimizable parameters.
16  They can be registered in @EvoScripts for the evolutionary optimization functionality.
17  The template type @T defines type of possible values the parameter may store.
18  As @T you have to either use a trivially destructible object or a smart pointer that will delete itself when it goes out of scope.
19  */
20  template<class T>
21  class EvoParam final : public IEvoParam
22  {
23  private:
24  std::vector<T> domain{};
25 
26  public:
27  EvoParam(std::initializer_list<T> argsDomain)
28  : IEvoParam(argsDomain.size())
29  {
30  domain.reserve(argsDomain.size());
31  std::copy(argsDomain.begin(), argsDomain.end(), std::back_inserter(domain));
32  }
33 
34  EvoParam(std::vector<T>& argsDomain)
35  : IEvoParam(argsDomain.size())
36  {
37  domain.reserve(argsDomain.size());
38  std::copy(argsDomain.begin(), argsDomain.end(), std::back_inserter(domain));
39  }
40 
41  int IndexOfValue(T value)
42  {
43  const auto it = std::find(domain.begin(), domain.end(), value);
44  if (it == domain.end())
45  return -1;
46  return std::distance(domain.begin(), it);
47  }
48 
49  const T Value() const
50  {
51  return domain[positionIndex];
52  }
53 
54  /*Expicit cast so you can use constructions such as:
55  EvoParam<int> speed({10,15,20})
56  if(speed > 2) .... */
57  operator T() const
58  {
59  return Value();
60  }
61 
62  std::string ToString() const
63  {
64  return std::to_string(Value());
65  }
66  };
67  }
68 }
69 #endif //GRAIL_EVO_PARAM_H
Definition: EvoParam.hh:22
Definition: IEvoParam.hh:12
size_t positionIndex
Definition: IEvoParam.hh:16