AIToolbox
A library that offers tools for AI problem solving.
EpsilonPolicyInterface.hpp
Go to the documentation of this file.
1 #ifndef AI_TOOLBOX_EPSILON_POLICY_INTERFACE_HEADER_FILE
2 #define AI_TOOLBOX_EPSILON_POLICY_INTERFACE_HEADER_FILE
3 
6 
7 #include <stdexcept>
8 
9 namespace AIToolbox {
28  template <typename State, typename Sampling, typename Action>
29  class EpsilonPolicyInterface : public virtual PolicyInterface<State, Sampling, Action> {
30  public:
44  EpsilonPolicyInterface(const Base & p, double epsilon = 0.1);
45 
57  virtual Action sampleAction(const Sampling & s) const override;
58 
70  virtual double getActionProbability(const Sampling & s, const Action & a) const override;
71 
86  void setEpsilon(double e);
87 
93  double getEpsilon() const;
94 
95  protected:
101  virtual Action sampleRandomAction() const = 0;
102 
112  virtual double getRandomActionProbability() const = 0;
113 
114  const Base & policy_;
115  double epsilon_;
116  };
117 
118  template <typename State, typename Sampling, typename Action>
120  policy_(p)
121  {
122  setEpsilon(e);
123  }
124 
125  template <typename State, typename Sampling, typename Action>
127  if ( probabilityDistribution(this->rand_) <= epsilon_ )
128  return sampleRandomAction();
129 
130  return policy_.sampleAction(s);
131  }
132 
133  template <typename State, typename Sampling, typename Action>
135  // Probability of taking old decision Random action probability
136  return (1.0 - epsilon_) * policy_.getActionProbability(s,a) + epsilon_ * getRandomActionProbability();
137  }
138 
139  template <typename State, typename Sampling, typename Action>
141  if ( e < 0.0 || e > 1.0 ) throw std::invalid_argument("Epsilon must be >= 0 and <= 1");
142  epsilon_ = e;
143  }
144 
145  template <typename State, typename Sampling, typename Action>
147  return epsilon_;
148  }
149 
159  template <typename Action>
160  class EpsilonPolicyInterface<void, void, Action> : public virtual PolicyInterface<void, void, Action> {
161  public:
175  EpsilonPolicyInterface(const Base & p, double epsilon = 0.1);
176 
186  virtual Action sampleAction() const override;
187 
198  virtual double getActionProbability(const Action & a) const override;
199 
214  void setEpsilon(double e);
215 
221  double getEpsilon() const;
222 
223  protected:
229  virtual Action sampleRandomAction() const = 0;
230 
240  virtual double getRandomActionProbability() const = 0;
241 
242  const Base & policy_;
243  double epsilon_;
244  };
245 
246  template <typename Action>
248  policy_(p)
249  {
250  setEpsilon(e);
251  }
252 
253  template <typename Action>
255  if ( probabilityDistribution(this->rand_) <= epsilon_ )
256  return sampleRandomAction();
257 
258  return policy_.sampleAction();
259  }
260 
261  template <typename Action>
263  // Probability of taking old decision Random action probability
265  }
266 
267  template <typename Action>
269  if ( e < 0.0 || e > 1.0 ) throw std::invalid_argument("Epsilon must be >= 0 and <= 1");
270  epsilon_ = e;
271  }
272 
273  template <typename Action>
275  return epsilon_;
276  }
277 }
278 
279 #endif
AIToolbox::PolicyInterface< void, void, Action >
This class represents the base interface for policies in games and bandits.
Definition: PolicyInterface.hpp:110
AIToolbox::EpsilonPolicyInterface::EpsilonPolicyInterface
EpsilonPolicyInterface(const Base &p, double epsilon=0.1)
Basic constructor.
Definition: EpsilonPolicyInterface.hpp:119
AIToolbox::EpsilonPolicyInterface< void, void, Action >::policy_
const Base & policy_
Definition: EpsilonPolicyInterface.hpp:242
AIToolbox::EpsilonPolicyInterface::setEpsilon
void setEpsilon(double e)
This function sets the epsilon parameter.
Definition: EpsilonPolicyInterface.hpp:140
AIToolbox::EpsilonPolicyInterface::policy_
const Base & policy_
Definition: EpsilonPolicyInterface.hpp:114
AIToolbox::PolicyInterface
This class represents the base interface for policies.
Definition: PolicyInterface.hpp:31
AIToolbox::EpsilonPolicyInterface::getActionProbability
virtual double getActionProbability(const Sampling &s, const Action &a) const override
This function returns the probability of taking the specified action in the specified state.
Definition: EpsilonPolicyInterface.hpp:134
AIToolbox::EpsilonPolicyInterface::sampleRandomAction
virtual Action sampleRandomAction() const =0
This function returns a random action in the Action space.
AIToolbox::EpsilonPolicyInterface< void, void, Action >::epsilon_
double epsilon_
Definition: EpsilonPolicyInterface.hpp:243
AIToolbox
Definition: Experience.hpp:6
PolicyInterface.hpp
AIToolbox::EpsilonPolicyInterface::sampleAction
virtual Action sampleAction(const Sampling &s) const override
This function chooses an action for state s, following the policy distribution and epsilon.
Definition: EpsilonPolicyInterface.hpp:126
AIToolbox::PolicyInterface::sampleAction
virtual Action sampleAction(const Sampling &s) const =0
This function chooses a random action for state s, following the policy distribution.
AIToolbox::PolicyInterface::getActionProbability
virtual double getActionProbability(const Sampling &s, const Action &a) const =0
This function returns the probability of taking the specified action in the specified state.
AIToolbox::PolicyInterface::rand_
RandomEngine rand_
Definition: PolicyInterface.hpp:84
AIToolbox::POMDP::TigerProblemUtils::Action
Action
Definition: TigerProblem.hpp:9
AIToolbox::EpsilonPolicyInterface::getRandomActionProbability
virtual double getRandomActionProbability() const =0
This function returns the probability of picking a random action.
AIToolbox::EpsilonPolicyInterface
This class is a policy wrapper for epsilon action choice.
Definition: EpsilonPolicyInterface.hpp:29
AIToolbox::EpsilonPolicyInterface::epsilon_
double epsilon_
Definition: EpsilonPolicyInterface.hpp:115
AIToolbox::probabilityDistribution
static std::uniform_real_distribution< double > probabilityDistribution(0.0, 1.0)
AIToolbox::EpsilonPolicyInterface::getEpsilon
double getEpsilon() const
This function will return the currently set epsilon parameter.
Definition: EpsilonPolicyInterface.hpp:146
Probability.hpp