AIToolbox
A library that offers tools for AI problem solving.
SparseExperience.hpp
Go to the documentation of this file.
1 #ifndef AI_TOOLBOX_MDP_SPARSE_EXPERIENCE_HEADER_FILE
2 #define AI_TOOLBOX_MDP_SPARSE_EXPERIENCE_HEADER_FILE
3 
4 #include <iosfwd>
5 
6 #include <AIToolbox/Types.hpp>
9 
10 namespace AIToolbox::MDP {
31  public:
38  SparseExperience(size_t S, size_t A);
39 
58  template <IsNaive3DTable V>
59  void setVisitsTable(const V & v);
60 
78  void setVisitsTable(const SparseTable3D & v);
79 
97  template <IsNaive2DMatrix R>
98  void setRewardMatrix(const R & r);
99 
114  void setRewardMatrix(const SparseMatrix2D & r);
115 
133  template <IsNaive2DMatrix MM>
134  void setM2Matrix(const MM & mm);
135 
150  void setM2Matrix(const SparseMatrix2D & mm);
151 
163  void record(size_t s, size_t a, size_t s1, double rew);
164 
168  void reset();
169 
175  unsigned long getTimesteps() const;
176 
184  unsigned long getVisits(size_t s, size_t a, size_t s1) const;
185 
192  unsigned long getVisitsSum(size_t s, size_t a) const;
193 
200  double getReward(size_t s, size_t a) const;
201 
208  double getM2(size_t s, size_t a) const;
209 
215  const SparseTable3D & getVisitsTable() const;
216 
224  const SparseTable2D & getVisitsTable(size_t a) const;
225 
233  const SparseTable2D & getVisitsSumTable() const;
234 
240  const SparseMatrix2D & getRewardMatrix() const;
241 
247  const SparseMatrix2D & getM2Matrix() const;
248 
254  size_t getS() const;
255 
261  size_t getA() const;
262 
263  private:
264  size_t S, A;
265 
266  SparseTable3D visits_;
267  SparseTable2D visitsSum_;
268  SparseMatrix2D rewards_;
269  SparseMatrix2D M2s_;
270  unsigned long timesteps_;
271 
272  friend std::istream& operator>>(std::istream &is, SparseExperience &);
273  };
274 
275  template <IsNaive3DTable V>
277  for ( size_t a = 0; a < A; ++a )
278  visits_[a].setZero();
279  visitsSum_.setZero();
280 
281  for ( size_t s = 0; s < S; ++s )
282  for ( size_t a = 0; a < A; ++a )
283  for ( size_t s1 = 0; s1 < S; ++s1 )
284  if ( v[s][a][s1] > 0 ) {
285  visits_[a].insert(s, s1) = v[s][a][s1];
286  visitsSum_.coeffRef(s, a) += v[s][a][s1];
287  }
288 
289  for ( size_t a = 0; a < A; ++a )
290  visits_[a].makeCompressed();
291  visitsSum_.makeCompressed();
292  }
293 
294  template <IsNaive2DMatrix R>
296  rewards_.setZero();
297 
298  for ( size_t s = 0; s < S; ++s )
299  for ( size_t a = 0; a < A; ++a )
300  if ( checkDifferentSmall(0.0, r[s][a]) )
301  rewards_.insert(s, a) = r[s][a];
302 
303  rewards_.makeCompressed();
304  }
305 
306  template <IsNaive2DMatrix MM>
307  void SparseExperience::setM2Matrix(const MM & mm) {
308  M2s_.setZero();
309 
310  for ( size_t s = 0; s < S; ++s )
311  for ( size_t a = 0; a < A; ++a )
312  if ( checkDifferentSmall(0.0, mm[s][a]) )
313  M2s_.insert(s, a) = mm[s][a];
314 
315  M2s_.makeCompressed();
316  }
317 }
318 
319 #endif
AIToolbox::SparseTable2D
Eigen::SparseMatrix< unsigned long, Eigen::RowMajor > SparseTable2D
Definition: Types.hpp:30
AIToolbox::checkDifferentSmall
bool checkDifferentSmall(const double a, const double b)
This function checks if two doubles near [0,1] are reasonably different.
Definition: Core.hpp:60
AIToolbox::MDP::SparseExperience::getVisitsSum
unsigned long getVisitsSum(size_t s, size_t a) const
This function returns the current recorded visits for a state-action pair.
Core.hpp
AIToolbox::MDP::SparseExperience::getReward
double getReward(size_t s, size_t a) const
This function returns the average reward for a state-action pair.
AIToolbox::MDP::SparseExperience::getVisitsSumTable
const SparseTable2D & getVisitsSumTable() const
This function returns the visits sum table for inspection.
AIToolbox::MDP::SparseExperience::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::SparseExperience::SparseExperience
SparseExperience(size_t S, size_t A)
Basic constructor.
AIToolbox::SparseTable3D
std::vector< SparseTable2D > SparseTable3D
Definition: Types.hpp:31
AIToolbox::MDP::SparseExperience::setM2Matrix
void setM2Matrix(const MM &mm)
This function sets the internal m2 matrix to the input.
Definition: SparseExperience.hpp:307
AIToolbox::MDP::SparseExperience::reset
void reset()
This function resets all experienced rewards, transitions and M2s.
AIToolbox::MDP::SparseExperience::operator>>
friend std::istream & operator>>(std::istream &is, SparseExperience &)
AIToolbox::MDP::SparseExperience
This class keeps track of registered events and rewards.
Definition: SparseExperience.hpp:30
AIToolbox::MDP
Definition: DoubleQLearning.hpp:10
AIToolbox::MDP::SparseExperience::getS
size_t getS() const
This function returns the number of states of the world.
TypeTraits.hpp
AIToolbox::MDP::SparseExperience::getA
size_t getA() const
This function returns the number of available actions to the agent.
AIToolbox::MDP::SparseExperience::getTimesteps
unsigned long getTimesteps() const
This function returns the number of times the record function has been called.
AIToolbox::MDP::SparseExperience::getM2
double getM2(size_t s, size_t a) const
This function returns the M2 statistic for a state-action pair.
AIToolbox::MDP::SparseExperience::setRewardMatrix
void setRewardMatrix(const R &r)
This function sets the internal reward matrix to the input.
Definition: SparseExperience.hpp:295
Types.hpp
AIToolbox::MDP::SparseExperience::getM2Matrix
const SparseMatrix2D & getM2Matrix() const
This function returns the rewards squared matrix for inspection.
AIToolbox::MDP::SparseExperience::getVisitsTable
const SparseTable3D & getVisitsTable() const
This function returns the visits table for inspection.
AIToolbox::MDP::SparseExperience::getRewardMatrix
const SparseMatrix2D & getRewardMatrix() const
This function returns the rewards matrix for inspection.
AIToolbox::MDP::SparseExperience::getVisits
unsigned long getVisits(size_t s, size_t a, size_t s1) const
This function returns the current recorded visits for a transition.
AIToolbox::MDP::SparseExperience::setVisitsTable
void setVisitsTable(const V &v)
This function sets the internal visits table to the input.
Definition: SparseExperience.hpp:276
AIToolbox::SparseMatrix2D
Eigen::SparseMatrix< double, Eigen::RowMajor > SparseMatrix2D
Definition: Types.hpp:19