1 #ifndef AI_TOOLBOX_POMDP_SPARSE_MODEL_HEADER_FILE
2 #define AI_TOOLBOX_POMDP_SPARSE_MODEL_HEADER_FILE
15 template <MDP::IsModel M>
19 template <MDP::IsModel M>
77 template <MDP::IsModel M>
92 template <
typename... Args>
124 SparseModel(
size_t o, ObFun && of, Args&&... parameters);
141 template <
typename PM>
142 requires IsModel<PM> && std::constructible_from<M, PM>
160 template <
typename... Args>
180 template <IsNaive3DMatrix ObFun>
220 std::tuple<size_t,size_t, double>
sampleSOR(
size_t s,
size_t a)
const;
238 std::tuple<size_t, double>
sampleOR(
size_t s,
size_t a,
size_t s1)
const;
293 template <MDP::IsModel M>
294 template <
typename... Args>
296 M(std::forward<Args>(params)...), O(o), observations_(this->getA(),
299 for (
size_t a = 0; a < this->getA(); ++a ) {
300 for (
size_t s1 = 0; s1 < this->getS(); ++s1 )
301 observations_[a].insert(s1, 0) = 1.0;
302 observations_[a].makeCompressed();
306 template <MDP::IsModel M>
309 M(std::forward<Args>(params)...), O(o),
315 template <MDP::IsModel M>
316 template <
typename... Args>
318 M(std::forward<Args>(params)...), O(o),
319 observations_(std::move(ot))
322 template <MDP::IsModel M>
323 template <
typename PM>
324 requires IsModel<PM> && std::constructible_from<M, PM>
326 M(model), O(model.getO()), observations_(this->getA(),
SparseMatrix2D(this->getS(), O)),
329 for (
size_t a = 0; a < this->getA(); ++a ) {
330 for (
size_t s1 = 0; s1 < this->getS(); ++s1 ) {
331 for (
size_t o = 0; o < O; ++o ) {
332 const double p = model.getObservationProbability(s1, a, o);
333 if ( p < 0.0 || p > 1.0 )
334 throw std::invalid_argument(
"Input observation matrix contains an invalid value.");
339 throw std::invalid_argument(
"Input observation matrix contains an invalid row.");
341 observations_[a].makeCompressed();
345 template <MDP::IsModel M>
346 template <IsNaive3DMatrix ObFun>
348 for (
size_t s1 = 0; s1 < this->getS(); ++s1 )
349 for (
size_t a = 0; a < this->getA(); ++a )
351 throw std::invalid_argument(
"Input observation matrix does not contain valid probabilities.");
353 for (
size_t a = 0; a < this->getA(); ++a ) {
354 observations_[a].setZero();
355 for (
size_t s1 = 0; s1 < this->getS(); ++s1 )
356 for (
size_t o = 0; o < O; ++o ) {
357 const double p = of[s1][a][o];
359 observations_[a].insert(s1, o) = p;
363 for (
size_t a = 0; a < this->getA(); ++a )
364 observations_[a].makeCompressed();
367 template <MDP::IsModel M>
370 throw std::invalid_argument(
"Input observation matrix does not contain valid probabilities.");
375 template <MDP::IsModel M>
377 return observations_[a].coeff(s1, o);
380 template <MDP::IsModel M>
382 return observations_[a];
385 template <MDP::IsModel M>
390 template <MDP::IsModel M>
392 return observations_;
395 template <MDP::IsModel M>
397 const auto [s1, r] = this->sampleSR(s, a);
399 return std::make_tuple(s1, o, r);
402 template <MDP::IsModel M>
405 const double r = this->getExpectedReward(s, a, s1);
406 return std::make_tuple(o, r);