Grail (C++)  1.1.1
A multi-platform, modular, universal engine for embedding advanced AI in games.
SigmoidFunction.hh
1 #ifndef GRAIL_SIGMOID_FUNCTION_H
2 #define GRAIL_SIGMOID_FUNCTION_H
3 
4 #include <cmath>
5 #include "Curve.hh"
6 
7 namespace grail
8 {
9  namespace curves
10  {
11  template <typename ContextType>
16  class SigmoidFunction final : public Curve<ContextType>
17  {
18  public:
27  SigmoidFunction(std::shared_ptr<utility::Evaluator<ContextType>> childEvaluator,
28  float range,
29  float slope,
30  float threshold,
31  float displacement)
32  : Curve<ContextType>{childEvaluator},
33  range(range),
34  slope(slope),
35  threshold(threshold),
36  displacement(displacement)
37  {
38  }
39 
40  virtual float Sample(float argument) const override final
41  {
42  float x = slope * (argument - threshold);
43  if(x < 0)
44  {
45  float exp_x = std::exp(x);
46  return ((range * exp_x) / (1 + exp_x)) + displacement;
47  }
48  else
49  {
50  return (range / (1 + std::exp(-x))) + displacement;
51  }
52  }
53 
58  float GetRange() const { return range; }
63  float GetSlope() const { return slope; }
68  float GetThreshold() const { return threshold; }
73  float GetDisplacement() const { return displacement; }
74 
75  virtual EvaluatorType GetEvaluatorType() const override final { return EvaluatorType::CURVE_SIGMOID; }
76 
77  private:
78  float range = 0.0f;
79  float slope = 0.0f;
80  float threshold = 0.0f;
81  float displacement = 0.0f;
82  };
83  }
84 }
85 
86 #endif // GRAIL_SIGMOID_FUNCTION_H
grail::curves::Curve
The Curve class - Defines objects transforming one value into the other.
Definition: Curve.hh:19
grail::curves::SigmoidFunction::GetThreshold
float GetThreshold() const
GetThreshold.
Definition: SigmoidFunction.hh:68
grail::curves::SigmoidFunction::Sample
virtual float Sample(float argument) const override final
Sample - Transforms argument into output value depending on the type of Curve.
Definition: SigmoidFunction.hh:40
grail::curves::SigmoidFunction::GetSlope
float GetSlope() const
GetSlope.
Definition: SigmoidFunction.hh:63
grail::curves::SigmoidFunction::SigmoidFunction
SigmoidFunction(std::shared_ptr< utility::Evaluator< ContextType >> childEvaluator, float range, float slope, float threshold, float displacement)
SigmoidFunction - Constructor.
Definition: SigmoidFunction.hh:27
grail::curves::SigmoidFunction::GetDisplacement
float GetDisplacement() const
GetDisplacement.
Definition: SigmoidFunction.hh:73
grail::curves::SigmoidFunction
The SigmoidFunction class - Sigmoid function.
Definition: SigmoidFunction.hh:16
grail::utility::Evaluator
The Evaluator class - base class being able to evaluate given context and output the result.
Definition: Evaluator.hh:20
grail::curves::SigmoidFunction::GetRange
float GetRange() const
GetRange.
Definition: SigmoidFunction.hh:58