AIToolbox
A library that offers tools for AI problem solving.
|
Go to the documentation of this file. 1 #ifndef AI_TOOLBOX_MDP_SPARSE_MODEL_HEADER_FILE
2 #define AI_TOOLBOX_MDP_SPARSE_MODEL_HEADER_FILE
96 SparseModel(
size_t s,
size_t a,
double discount = 1.0);
139 template <IsNaive3DMatrix T, IsNaive3DMatrix R>
140 SparseModel(
size_t s,
size_t a,
const T & t,
const R & r,
double d = 1.0);
204 template <IsNaive3DMatrix T>
250 template <IsNaive3DMatrix R>
292 std::tuple<size_t, double>
sampleSR(
size_t s,
size_t a)
const;
379 template <IsNaive3DMatrix T, IsNaive3DMatrix R>
382 rewards_(S, A), rand_(
Seeder::getSeed())
391 S(model.getS()), A(model.getA()), transitions_(A,
SparseMatrix2D(S, S)),
392 rewards_(S, A), rand_(
Seeder::getSeed())
395 for (
size_t s = 0; s < S; ++s )
396 for (
size_t a = 0; a < A; ++a ) {
397 for (
size_t s1 = 0; s1 < S; ++s1 ) {
398 const double p = model.getTransitionProbability(s, a, s1);
399 if ( p < 0.0 || p > 1.0 )
400 throw std::invalid_argument(
"Input transition matrix contains an invalid value.");
403 const double r = model.getExpectedReward(s, a, s1);
407 throw std::invalid_argument(
"Input transition matrix contains an invalid row.");
410 for (
size_t a = 0; a < A; ++a )
411 transitions_[a].makeCompressed();
412 rewards_.makeCompressed();
415 template <IsNaive3DMatrix T>
418 throw std::invalid_argument(
"Input transition matrix does not contain valid probabilities.");
421 for (
size_t a = 0; a < A; ++a ) {
422 transitions_[a].setZero();
424 for (
size_t s = 0; s < S; ++s )
425 for (
size_t s1 = 0; s1 < S; ++s1 ) {
426 const double p = t[s][a][s1];
429 transitions_[a].makeCompressed();
433 template <IsNaive3DMatrix R>
436 for (
size_t a = 0; a < A; ++a ) {
437 for (
size_t s = 0; s < S; ++s ) {
439 for (
size_t s1 = 0; s1 < S; ++s1 )
440 newRew += r[s][a][s1] * transitions_[a].coeff(s, s1);
443 rewards_.coeffRef(s, a) = newRew;
446 rewards_.makeCompressed();