(C++)  1.0.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
Evaluator.hh
1 #ifndef GRAIL_EVALUATOR_H
2 #define GRAIL_EVALUATOR_H
3 
4 #include "Consideration.hh"
5 #include "Curves/Curve.hh"
6 #include "IEvaluator.hh"
7 
8 #include <vector>
9 #include <cassert>
10 #include <limits>
11 #include <memory>
12 
13 namespace grail
14 {
15  namespace utility
16  {
17  template <typename ContextType>
21  class Evaluator final : public IEvaluator<ContextType>
22  {
23  public:
24  using CurveDataPair = std::pair<std::shared_ptr<Consideration<ContextType>>, std::shared_ptr<curves::Curve>>;
30  Evaluator(WeightEvaluationMethod evaluationMethod, const std::vector<CurveDataPair>& curveData)
31  : curveData{ curveData }, method{ evaluationMethod }
32  {
33  }
34 
35  Evaluator(const Evaluator<ContextType>&) = default;
36  Evaluator(Evaluator<ContextType>&&) = default;
37 
38  virtual ~Evaluator() = default;
39 
40  Evaluator& operator = (const Evaluator<ContextType>&) = default;
41  Evaluator& operator = (Evaluator<ContextType>&&) = default;
42 
48  float EvaluateWeight(const ContextType& object) const override
49  {
50  switch (method)
51  {
52  case WeightEvaluationMethod::MULTIPLY:
53  return EvaluateByMultiplication(object);
54 
55  case WeightEvaluationMethod::AVERAGE:
56  return EvaluateByAverage(object);
57 
58  case WeightEvaluationMethod::MAX:
59  return EvaluateByMax(object);
60 
61  case WeightEvaluationMethod::MIN:
62  return EvaluateByMin(object);
63 
64  case WeightEvaluationMethod::ADD:
65  return EvaluateByAddition(object);
66  default:
67  return 0;
68  }
69  }
70 
75  int EvaluateRank() const override
76  {
77  int rank = std::numeric_limits<int>::min();
78  for (const CurveDataPair& data : curveData)
79  {
80  if (data.first->GetRank() > rank)
81  rank = data.first->GetRank();
82  }
83  return rank;
84  }
85 
86  const std::vector<CurveDataPair>& GetCurveData() const
87  {
88  return curveData;
89  }
90 
91  WeightEvaluationMethod GetEvaluationMethod() const override
92  {
93  return method;
94  }
95 
96  protected:
97  float EvaluateConsiderations(const std::shared_ptr<Consideration<ContextType>>& consideration, const ContextType& object) const
98  {
99  return consideration->Evaluate(object);
100  }
101 
102  private:
103  std::vector<CurveDataPair> curveData = {};
104  WeightEvaluationMethod method{};
105 
106  float EvaluateByMultiplication(const ContextType& object) const
107  {
108  float weight = 1;
109  for (const CurveDataPair& data : curveData)
110  {
111  weight *= data.second->Sample(EvaluateConsiderations(data.first, object));
112  }
113  return weight;
114  }
115 
116  float EvaluateByAverage(const ContextType& object) const
117  {
118  float weight = 0;
119  for (const CurveDataPair& data : curveData)
120  {
121  weight += data.second->Sample(EvaluateConsiderations(data.first, object));
122  }
123  return curveData.size() > 0 ? weight / curveData.size() : 0;
124  }
125 
126  float EvaluateByAddition(const ContextType& object) const
127  {
128  float weight = 0;
129  for (const CurveDataPair& data : curveData)
130  {
131  weight += data.second->Sample(EvaluateConsiderations(data.first, object));
132  }
133  return weight;
134  }
135 
136  float EvaluateByMax(const ContextType& object) const
137  {
138  float weight = std::numeric_limits<float>::min();
139  for (const CurveDataPair& data : curveData)
140  {
141  float current = data.second->Sample(EvaluateConsiderations(data.first, object));
142  if (current > weight)
143  weight = current;
144  }
145  return weight;
146  }
147 
148  float EvaluateByMin(const ContextType& object) const
149  {
150  float weight = std::numeric_limits<float>::max();
151  for (const CurveDataPair& data : curveData)
152  {
153  float current = data.second->Sample(EvaluateConsiderations(data.first, object));
154  if (current < weight)
155  weight = current;
156  }
157  return weight;
158  }
159  };
160  }
161 }
162 #endif //GRAIL_EVALUATOR_H
The UtilityEvaluator class - Standard evaluator calculating scores based on considerations and curves...
Definition: Evaluator.hh:22
Evaluator(WeightEvaluationMethod evaluationMethod, const std::vector< CurveDataPair > &curveData)
UtilityEvaluator.
Definition: Evaluator.hh:30
int EvaluateRank() const override
EvaluateRank - Evaluates rank by choosing highest rank from all of it's owned considerations.
Definition: Evaluator.hh:75
float EvaluateWeight(const ContextType &object) const override
EvaluateWeight - Evaluates weight using provided evaluation method.
Definition: Evaluator.hh:48
The Evaluator class - Evaluates object in terms of provided Context.
Definition: IEvaluator.hh:15