Grail (C++)  1.4.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
Curve.hh
1 // Copyright QED Software 2023.
2 
3 #ifndef GRAIL_CURVE_H
4 #define GRAIL_CURVE_H
5 
6 #include "../Evaluator.hh"
7 #include "../../libs/vector2/vector2.h"
8 
9 #include <utility>
10 #include <vector>
11 
12 namespace grail
13 {
14 namespace evaluator
15 {
20  template <typename ContextType>
21  class Curve : public Evaluator<ContextType>
22  {
23  public:
28  explicit Curve(std::shared_ptr<Evaluator<ContextType>> childEvaluator)
29  : childEvaluator{childEvaluator}
30  {
31  }
32 
33  Curve(const Curve<ContextType>& other) = default;
34  Curve(Curve<ContextType>&& other) = default;
35  virtual ~Curve() = default;
36 
42  virtual float Sample(float argument) const = 0;
43 
44  virtual float
45  Evaluate(const ContextType& context, data::UtilityEvaluatorSnapshot* const snapshot) const override final
46  {
47  return Sample(childEvaluator->EvaluateContext(context, snapshot));
48  }
49 
50  virtual void GetDependentConsiderations(std::vector<Consideration<ContextType>*>& considerations) override
51  {
52  childEvaluator->GetDependentConsiderations(considerations);
53  }
54 
55  protected:
56  virtual void DebugDump(const std::map<const void*, unsigned int>& nodeMapping,
57  data::EvaluationDebugData& debugData) const override final
58  {
59  debugData.AddChildNode(nodeMapping.at(childEvaluator.get()));
60  }
61 
62  private:
63  std::shared_ptr<Evaluator<ContextType>> childEvaluator{};
64  };
65 
66  template <typename ContextType>
71  class LowerBound final : public Curve<ContextType>
72  {
73  public:
79  LowerBound(std::shared_ptr<Evaluator<ContextType>> childEvaluator, float lowerBound)
80  : Curve<ContextType>(childEvaluator), lowerBound{lowerBound}
81  {
82  }
83 
84  virtual float Sample(float argument) const override final
85  {
86  return std::max(lowerBound, argument);
87  }
88 
89  virtual data::EvaluatorType GetEvaluatorType() const override final { return data::EvaluatorType::CURVE_LOWER_BOUND; }
90 
91  private:
92  float lowerBound = 0.0f;
93  };
94 
95  template <typename ContextType>
100  class UpperBound final : public Curve<ContextType>
101  {
102  public:
108  UpperBound(std::shared_ptr<Evaluator<ContextType>> childEvaluator, float upperBound)
109  : Curve<ContextType>(childEvaluator), upperBound{upperBound}
110  {
111  }
112 
113  virtual float Sample(float argument) const override final
114  {
115  return std::min(upperBound, argument);
116  }
117 
118  virtual data::EvaluatorType GetEvaluatorType() const override final { return data::EvaluatorType::CURVE_UPPER_BOUND; }
119 
120  private:
121  float upperBound = 0.0f;
122  };
123 
124  template <typename ContextType>
129  class DoubleSidedBound final : public Curve<ContextType>
130  {
131  public:
138  DoubleSidedBound(std::shared_ptr<Evaluator<ContextType>> childEvaluator,
139  float lowerBound,
140  float upperBound)
141  : Curve<ContextType>{childEvaluator}, lowerBound{lowerBound}, upperBound{upperBound}
142  {
143  }
144 
145  virtual float Sample(float argument) const override final
146  {
147  return std::min(upperBound, std::max(lowerBound, argument));
148  }
149 
150  virtual data::EvaluatorType GetEvaluatorType() const override final
151  {
152  return data::EvaluatorType::CURVE_DOUBLE_SIDED_BOUND;
153  }
154 
155  private:
156  float lowerBound = 0.0f;
157  float upperBound = 0.0f;
158  };
159 
160  template <typename ContextType>
167  std::vector<Vector2> DiscretizeCurve(const Curve<ContextType>& curve, std::size_t samplesQuantity)
168  {
169  std::vector<Vector2> controlPoints;
170  float increment = 1.0f / (samplesQuantity - 1);
171  for(float i = 0; i <= 1; i += increment)
172  {
173  controlPoints.push_back({i, curve.Sample(i)});
174  }
175  return controlPoints;
176  }
177 }
178 }
179 #endif //GRAIL_CURVE_H
grail::evaluator::Curve::Evaluate
virtual float Evaluate(const ContextType &context, data::UtilityEvaluatorSnapshot *const snapshot) const override final
Evaluate - Called from EvaluateContext which also evaluates context, but without automatically fillin...
Definition: Curve.hh:45
grail::data::EvaluationDebugData
The EvaluationDebugData class - debug data describing singular evaluator.
Definition: EvaluationDebugData.h:24
grail::evaluator::UpperBound
The UpperBound class - Upper-bounds output of provided Evaluator.
Definition: Curve.hh:100
grail::evaluator::UpperBound::UpperBound
UpperBound(std::shared_ptr< Evaluator< ContextType >> childEvaluator, float upperBound)
UpperBound - Constructor.
Definition: Curve.hh:108
grail::evaluator::Curve::Curve
Curve(std::shared_ptr< Evaluator< ContextType >> childEvaluator)
Curve - Constructor.
Definition: Curve.hh:28
grail::evaluator::DoubleSidedBound::Sample
virtual float Sample(float argument) const override final
Sample - Transforms argument into output value depending on the type of Curve.
Definition: Curve.hh:145
grail::evaluator::Curve::DebugDump
virtual void DebugDump(const std::map< const void *, unsigned int > &nodeMapping, data::EvaluationDebugData &debugData) const override final
DebugDump - Called from EvaluateContext, which generates additional debug data for each evaluator....
Definition: Curve.hh:56
grail::evaluator::Curve::Sample
virtual float Sample(float argument) const =0
Sample - Transforms argument into output value depending on the type of Curve.
grail::evaluator::LowerBound
The LowerBound class - Lower-bounds output of provided Evaluator.
Definition: Curve.hh:71
grail::evaluator::Evaluator
The Evaluator class - base class being able to evaluate given context and output the result.
Definition: Evaluator.hh:25
grail::evaluator::UpperBound::Sample
virtual float Sample(float argument) const override final
Sample - Transforms argument into output value depending on the type of Curve.
Definition: Curve.hh:113
grail::evaluator::LowerBound::Sample
virtual float Sample(float argument) const override final
Sample - Transforms argument into output value depending on the type of Curve.
Definition: Curve.hh:84
grail::evaluator::Consideration
The Consideration class - Abstract type of Evaluator representing a subset of game state.
Definition: Consideration.hh:20
grail::data::UtilityEvaluatorSnapshot
The UtilityEvaluatorSnapshot class - debug snapshot of whole evaluator tree assigned to evaluated obj...
Definition: UtilityEvaluatorSnapshot.h:26
grail::evaluator::DoubleSidedBound::DoubleSidedBound
DoubleSidedBound(std::shared_ptr< Evaluator< ContextType >> childEvaluator, float lowerBound, float upperBound)
DoubleSidedBound - Constructor.
Definition: Curve.hh:138
grail::evaluator::LowerBound::LowerBound
LowerBound(std::shared_ptr< Evaluator< ContextType >> childEvaluator, float lowerBound)
LowerBound - Constructor.
Definition: Curve.hh:79
grail::evaluator::DoubleSidedBound
The DoubleSidedBound class - Bounds output of provided Evaluator from both sides.
Definition: Curve.hh:129
grail::evaluator::Curve
The Curve class - Defines objects transforming one value into the other.
Definition: Curve.hh:21