AIToolbox
A library that offers tools for AI problem solving.
Experience.hpp
Go to the documentation of this file.
1 #ifndef AI_TOOLBOX_MDP_EXPERIENCE_HEADER_FILE
2 #define AI_TOOLBOX_MDP_EXPERIENCE_HEADER_FILE
3 
4 #include <iosfwd>
5 
6 #include <AIToolbox/Types.hpp>
10 
11 namespace AIToolbox::MDP {
24  class Experience {
25  public:
32  Experience(size_t S, size_t A);
33 
52  template <IsNaive3DTable V>
53  void setVisitsTable(const V & v);
54 
72  void setVisitsTable(const Table3D & v);
73 
91  template <IsNaive2DMatrix R>
92  void setRewardMatrix(const R & r);
93 
108  void setRewardMatrix(const Matrix2D & r);
109 
127  template <IsNaive2DMatrix MM>
128  void setM2Matrix(const MM & mm);
129 
144  void setM2Matrix(const Matrix2D & mm);
145 
154  void record(size_t s, size_t a, size_t s1, double rew);
155 
159  void reset();
160 
166  unsigned long getTimesteps() const;
167 
175  unsigned long getVisits(size_t s, size_t a, size_t s1) const;
176 
183  unsigned long getVisitsSum(size_t s, size_t a) const;
184 
191  double getReward(size_t s, size_t a) const;
192 
199  double getM2(size_t s, size_t a) const;
200 
206  const Table3D & getVisitsTable() const;
207 
215  const Table2D & getVisitsTable(size_t a) const;
216 
224  const Table2D & getVisitsSumTable() const;
225 
233  const QFunction & getRewardMatrix() const;
234 
240  const Matrix2D & getM2Matrix() const;
241 
247  size_t getS() const;
248 
254  size_t getA() const;
255 
256  private:
257  size_t S, A;
258 
259  Table3D visits_;
260  Table2D visitsSum_;
261  Matrix2D rewards_;
262  Matrix2D M2s_;
263  unsigned long timesteps_;
264 
265  friend std::istream& operator>>(std::istream &is, Experience &);
266  };
267 
268  template <IsNaive3DTable V>
269  void Experience::setVisitsTable(const V & v) {
270  visitsSum_.setZero();
271  for ( size_t s = 0; s < S; ++s ) {
272  for ( size_t a = 0; a < A; ++a ) {
273  for ( size_t s1 = 0; s1 < S; ++s1 ) {
274  visits_[a](s, s1) = v[s][a][s1];
275  visitsSum_(s, a) += v[s][a][s1];
276  }
277  }
278  }
279  }
280 
281  template <IsNaive2DMatrix R>
282  void Experience::setRewardMatrix(const R & r) {
283  for ( size_t s = 0; s < S; ++s )
284  for ( size_t a = 0; a < A; ++a )
285  rewards_(s, a) = r[s][a];
286  }
287 
288  template <IsNaive2DMatrix MM>
289  void Experience::setM2Matrix(const MM & m) {
290  for ( size_t s = 0; s < S; ++s )
291  for ( size_t a = 0; a < A; ++a )
292  M2s_(s, a) = m[s][a];
293  }
294 }
295 
296 #endif
AIToolbox::MDP::Experience::getVisits
unsigned long getVisits(size_t s, size_t a, size_t s1) const
This function returns the current recorded visits for a transition.
Core.hpp
AIToolbox::MDP::QFunction
Matrix2D QFunction
Definition: Types.hpp:52
AIToolbox::MDP::Experience::getTimesteps
unsigned long getTimesteps() const
This function returns the number of times the record function has been called.
AIToolbox::MDP::Experience::getA
size_t getA() const
This function returns the number of available actions to the agent.
AIToolbox::Matrix2D
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor|Eigen::AutoAlign > Matrix2D
Definition: Types.hpp:18
AIToolbox::MDP::Experience::operator>>
friend std::istream & operator>>(std::istream &is, Experience &)
AIToolbox::MDP::Experience::setVisitsTable
void setVisitsTable(const V &v)
This function sets the internal visits table to the input.
Definition: Experience.hpp:269
AIToolbox::MDP::Experience::record
void record(size_t s, size_t a, size_t s1, double rew)
This function adds a new event to the recordings.
AIToolbox::MDP::Experience::getVisitsSumTable
const Table2D & getVisitsSumTable() const
This function returns the visits sum table for inspection.
AIToolbox::MDP::Experience::getReward
double getReward(size_t s, size_t a) const
This function returns the average reward for a state-action pair.
AIToolbox::MDP
Definition: DoubleQLearning.hpp:10
TypeTraits.hpp
AIToolbox::MDP::Experience::setM2Matrix
void setM2Matrix(const MM &mm)
This function sets the internal m2 matrix to the input.
Definition: Experience.hpp:289
Types.hpp
AIToolbox::MDP::Experience::getRewardMatrix
const QFunction & getRewardMatrix() const
This function returns the rewards matrix for inspection.
AIToolbox::Table3D
std::vector< Table2D > Table3D
Definition: Types.hpp:28
AIToolbox::MDP::Experience::getVisitsSum
unsigned long getVisitsSum(size_t s, size_t a) const
This function returns the current recorded visits for a state-action pair.
AIToolbox::MDP::Experience::getVisitsTable
const Table3D & getVisitsTable() const
This function returns the visits table for inspection.
Types.hpp
AIToolbox::MDP::Experience::getS
size_t getS() const
This function returns the number of states of the world.
AIToolbox::MDP::Experience::getM2
double getM2(size_t s, size_t a) const
This function returns the M2 statistic for a state-action pair.
AIToolbox::MDP::Experience::reset
void reset()
This function resets all experienced rewards, transitions and M2s.
AIToolbox::MDP::Experience
This class keeps track of registered events and rewards.
Definition: Experience.hpp:24
AIToolbox::MDP::Experience::setRewardMatrix
void setRewardMatrix(const R &r)
This function sets the internal reward matrix to the input.
Definition: Experience.hpp:282
AIToolbox::MDP::Experience::Experience
Experience(size_t S, size_t A)
Basic constructor.
AIToolbox::MDP::Experience::getM2Matrix
const Matrix2D & getM2Matrix() const
This function returns the rewards squared matrix for inspection.
AIToolbox::Table2D
Eigen::Matrix< unsigned long, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor|Eigen::AutoAlign > Table2D
Definition: Types.hpp:27