aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
gum::FMDPSimulator Class Reference

<agrum/FMDP/simulation/fmdpSimulator.h> More...

#include <fmdpSimulator.h>

Inheritance diagram for gum::FMDPSimulator:
Collaboration diagram for gum::FMDPSimulator:

Public Member Functions

Constructors, Destructors.
 FMDPSimulator (const FMDP< double > *fmdp)
 Default constructor.
 FMDPSimulator (std::string_view resource)
 Default constructor.
 ~FMDPSimulator () override
 Default destructor.
double reward () override
 Sets the intial statefrom which we begun the simulation.
void perform (Idx) override
 Sets the intial statefrom which we begun the simulation.
const DiscreteVariableprimeVar (const DiscreteVariable *mainVar) override
 Iteration over the variables of the simulated probleme.
SequenceIteratorSafe< const DiscreteVariable * > beginVariables () override
 Iteration over the variables of the simulated probleme.
SequenceIteratorSafe< const DiscreteVariable * > endVariables () override
 Iteration over the variables of the simulated probleme.
const std::string & actionName (Idx actionId) override
 Iteration over the variables of the simulated probleme.
SequenceIteratorSafe< IdxbeginActions () override
 Iteration over the variables of the simulated probleme.
SequenceIteratorSafe< IdxendActions () override
 Iteration over the variables of the simulated probleme.

Public Attributes

Instantiation currentState_
 Tha state in which the system currently is.
Instantiation endState_

Protected Member Functions

virtual double transitionProbability_ (const DiscreteVariable *var, const Instantiation &transit, Idx actionId)

Private Attributes

FMDP< double > * _fmdp_
 The Factored Markov Decision Process that describes how the system evolves.
const bool _loaded_
 Just to know if it should be deleted in the end.
void setInitialState (const Instantiation &initialState)
 Sets the intial statefrom which we begun the simulation.
void setInitialStateRandomly ()
 Sets the intial statefrom which we begun the simulation.
void setEndState (const Instantiation &endState)
 Sets the final states upon which a run is over.
virtual bool hasReachEnd ()
 Tests if end state has been reached.
const InstantiationcurrentState ()
 Sets the intial statefrom which we begun the simulation.
virtual Instantiation randomState_ ()
 Choses a random state as the first test for a run.

Detailed Description

<agrum/FMDP/simulation/fmdpSimulator.h>

A class to simulate a Factored Markov Decision Process.

Definition at line 67 of file fmdpSimulator.h.

Constructor & Destructor Documentation

◆ FMDPSimulator() [1/2]

gum::FMDPSimulator::FMDPSimulator ( const FMDP< double > * fmdp)

Default constructor.

Definition at line 73 of file fmdpSimulator.cpp.

73 :
74 AbstractSimulator(), _fmdp_(const_cast< FMDP< double >* >(fmdp)), _loaded_(false) {
75 GUM_CONSTRUCTOR(FMDPSimulator);
76 }
AbstractSimulator()
Default constructor.
FMDPSimulator(const FMDP< double > *fmdp)
Default constructor.
FMDP< double > * _fmdp_
The Factored Markov Decision Process that describes how the system evolves.
const bool _loaded_
Just to know if it should be deleted in the end.

References gum::AbstractSimulator::AbstractSimulator(), FMDPSimulator(), _fmdp_, and _loaded_.

Referenced by FMDPSimulator(), FMDPSimulator(), and ~FMDPSimulator().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ FMDPSimulator() [2/2]

gum::FMDPSimulator::FMDPSimulator ( std::string_view resource)

Default constructor.

Definition at line 78 of file fmdpSimulator.cpp.

78 : AbstractSimulator(), _loaded_(true) {
79 GUM_CONSTRUCTOR(FMDPSimulator);
80
81 _fmdp_ = new FMDP< double >(true);
82 FMDPDatReader< double > reader(_fmdp_, ressource);
83 reader.trace(false);
84 reader.proceed();
85 }

References gum::AbstractSimulator::AbstractSimulator(), FMDPSimulator(), _fmdp_, _loaded_, gum::FMDPDatReader< GUM_ELEMENT >::proceed(), and gum::FMDPDatReader< GUM_ELEMENT >::trace().

Here is the call graph for this function:

◆ ~FMDPSimulator()

gum::FMDPSimulator::~FMDPSimulator ( )
override

Default destructor.

Definition at line 90 of file fmdpSimulator.cpp.

90 {
91 GUM_DESTRUCTOR(FMDPSimulator);
92 if (_loaded_) delete _fmdp_;
93 }

References FMDPSimulator(), _fmdp_, and _loaded_.

Here is the call graph for this function:

Member Function Documentation

◆ actionName()

INLINE const std::string & gum::FMDPSimulator::actionName ( Idx actionId)
overridevirtual

Iteration over the variables of the simulated probleme.

Implements gum::AbstractSimulator.

Definition at line 62 of file fmdpSimulator_inl.h.

62 {
63 return _fmdp_->actionName(actionId);
64 }

References _fmdp_.

◆ beginActions()

INLINE SequenceIteratorSafe< Idx > gum::FMDPSimulator::beginActions ( )
overridevirtual

Iteration over the variables of the simulated probleme.

Implements gum::AbstractSimulator.

Definition at line 66 of file fmdpSimulator_inl.h.

66 {
67 return _fmdp_->beginActions();
68 }

References _fmdp_.

◆ beginVariables()

INLINE SequenceIteratorSafe< const DiscreteVariable * > gum::FMDPSimulator::beginVariables ( )
overridevirtual

Iteration over the variables of the simulated probleme.

Implements gum::AbstractSimulator.

Definition at line 54 of file fmdpSimulator_inl.h.

54 {
55 return _fmdp_->beginVariables();
56 }

References _fmdp_.

Referenced by perform().

Here is the caller graph for this function:

◆ currentState()

INLINE const Instantiation & gum::AbstractSimulator::currentState ( )
inherited

Sets the intial statefrom which we begun the simulation.

Definition at line 56 of file abstractSimulator_inl.h.

56{ return currentState_; }
Instantiation currentState_
Tha state in which the system currently is.

References currentState_.

◆ endActions()

INLINE SequenceIteratorSafe< Idx > gum::FMDPSimulator::endActions ( )
overridevirtual

Iteration over the variables of the simulated probleme.

Implements gum::AbstractSimulator.

Definition at line 70 of file fmdpSimulator_inl.h.

70{ return _fmdp_->endActions(); }

References _fmdp_.

◆ endVariables()

INLINE SequenceIteratorSafe< const DiscreteVariable * > gum::FMDPSimulator::endVariables ( )
overridevirtual

Iteration over the variables of the simulated probleme.

Implements gum::AbstractSimulator.

Definition at line 58 of file fmdpSimulator_inl.h.

58 {
59 return _fmdp_->endVariables();
60 }

References _fmdp_.

Referenced by perform().

Here is the caller graph for this function:

◆ hasReachEnd()

bool gum::AbstractSimulator::hasReachEnd ( )
virtualinherited

Tests if end state has been reached.

Reimplemented in gum::FactorySimulator, and gum::TaxiSimulator.

Definition at line 104 of file abstractSimulator.cpp.

104 {
105 if (endState_.empty()) return false;
106
107 for (auto varIter = endState_.variablesSequence().beginSafe();
108 varIter != endState_.variablesSequence().endSafe();
109 ++varIter)
110 if (endState_.val(**varIter) != currentState_.val(**varIter)) return false;
111 return true;
112 }

References currentState_, and endState_.

Referenced by setInitialStateRandomly().

Here is the caller graph for this function:

◆ perform()

void gum::FMDPSimulator::perform ( Idx )
overridevirtual

Sets the intial statefrom which we begun the simulation.

Implements gum::AbstractSimulator.

Definition at line 99 of file fmdpSimulator.cpp.

99 {
100 Instantiation newState;
101 for (auto varIter = this->beginVariables(); varIter != this->endVariables(); ++varIter) {
102 newState.add(**varIter);
103 Instantiation transit(currentState_);
104 transit.add(*(this->primeVar(*varIter)));
105
106 double proba = randomProba();
107 double cdd = 0.0;
108 for (transit.setFirstOut(currentState_); !transit.end(); transit.incOut(currentState_)) {
109 cdd += this->transitionProbability_(*varIter, transit, actionId);
110 if (proba <= cdd) {
111 newState.chgVal(**varIter, transit.val(*(this->primeVar(*varIter))));
112 break;
113 }
114 }
115 }
116 currentState_ = newState;
117 }
SequenceIteratorSafe< const DiscreteVariable * > endVariables() override
Iteration over the variables of the simulated probleme.
virtual double transitionProbability_(const DiscreteVariable *var, const Instantiation &transit, Idx actionId)
SequenceIteratorSafe< const DiscreteVariable * > beginVariables() override
Iteration over the variables of the simulated probleme.
const DiscreteVariable * primeVar(const DiscreteVariable *mainVar) override
Iteration over the variables of the simulated probleme.
double randomProba()
Returns a random double between 0 and 1 included (i.e.

References gum::Instantiation::add(), beginVariables(), gum::Instantiation::chgVal(), gum::AbstractSimulator::currentState_, gum::Instantiation::end(), endVariables(), gum::Instantiation::incOut(), primeVar(), gum::randomProba(), gum::Instantiation::setFirstOut(), transitionProbability_(), and gum::Instantiation::val().

Here is the call graph for this function:

◆ primeVar()

INLINE const DiscreteVariable * gum::FMDPSimulator::primeVar ( const DiscreteVariable * mainVar)
overridevirtual

Iteration over the variables of the simulated probleme.

Implements gum::AbstractSimulator.

Definition at line 50 of file fmdpSimulator_inl.h.

50 {
51 return _fmdp_->main2prime(mainVar);
52 }

References _fmdp_.

Referenced by perform().

Here is the caller graph for this function:

◆ randomState_()

Instantiation gum::AbstractSimulator::randomState_ ( )
protectedvirtualinherited

Choses a random state as the first test for a run.

Reimplemented in gum::FactorySimulator, and gum::TaxiSimulator.

Definition at line 94 of file abstractSimulator.cpp.

94 {
95 Instantiation retState;
96 for (auto varIter = this->beginVariables(); varIter != this->endVariables(); ++varIter) {
97 retState.add(**varIter);
98 retState.chgVal(**varIter, randomValue((*varIter)->domainSize()));
99 }
100 return retState;
101 }
virtual SequenceIteratorSafe< const DiscreteVariable * > endVariables()=0
Iteration over the variables of the simulated probleme.
virtual SequenceIteratorSafe< const DiscreteVariable * > beginVariables()=0
Iteration over the variables of the simulated probleme.
Idx randomValue(const Size max=2)
Returns a random Idx between 0 and max-1 included.

References gum::Instantiation::add(), beginVariables(), gum::Instantiation::chgVal(), endVariables(), and gum::randomValue().

Referenced by gum::TaxiSimulator::randomState_(), and setInitialStateRandomly().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reward()

INLINE double gum::FMDPSimulator::reward ( )
overridevirtual

Sets the intial statefrom which we begun the simulation.

Implements gum::AbstractSimulator.

Definition at line 48 of file fmdpSimulator_inl.h.

48{ return _fmdp_->reward()->get(this->currentState_); }

References _fmdp_, and gum::AbstractSimulator::currentState_.

◆ setEndState()

INLINE void gum::AbstractSimulator::setEndState ( const Instantiation & endState)
inherited

Sets the final states upon which a run is over.

Definition at line 52 of file abstractSimulator_inl.h.

52 {
53 endState_ = endState;
54 }

References endState_.

◆ setInitialState()

INLINE void gum::AbstractSimulator::setInitialState ( const Instantiation & initialState)
inherited

Sets the intial statefrom which we begun the simulation.

Definition at line 48 of file abstractSimulator_inl.h.

48 {
49 currentState_ = initialState;
50 }

References currentState_.

◆ setInitialStateRandomly()

void gum::AbstractSimulator::setInitialStateRandomly ( )
inherited

Sets the intial statefrom which we begun the simulation.

Definition at line 86 of file abstractSimulator.cpp.

86 {
87 bool hre = true;
88 while (hre) {
90 hre = hasReachEnd();
91 }
92 }
virtual Instantiation randomState_()
Choses a random state as the first test for a run.
virtual bool hasReachEnd()
Tests if end state has been reached.

References currentState_, hasReachEnd(), and randomState_().

Here is the call graph for this function:

◆ transitionProbability_()

INLINE double gum::FMDPSimulator::transitionProbability_ ( const DiscreteVariable * var,
const Instantiation & transit,
Idx actionId )
protectedvirtual

Definition at line 72 of file fmdpSimulator_inl.h.

74 {
75 return reinterpret_cast< const MultiDimFunctionGraph< double >* >(
76 _fmdp_->transition(actionId, var))
77 ->get(transit);
78 }

References _fmdp_, and gum::MultiDimFunctionGraph< GUM_ELEMENT, TerminalNodePolicy >::get().

Referenced by perform().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ _fmdp_

FMDP< double >* gum::FMDPSimulator::_fmdp_
private

The Factored Markov Decision Process that describes how the system evolves.

Definition at line 135 of file fmdpSimulator.h.

Referenced by FMDPSimulator(), FMDPSimulator(), ~FMDPSimulator(), actionName(), beginActions(), beginVariables(), endActions(), endVariables(), primeVar(), reward(), and transitionProbability_().

◆ _loaded_

const bool gum::FMDPSimulator::_loaded_
private

Just to know if it should be deleted in the end.

Definition at line 138 of file fmdpSimulator.h.

Referenced by FMDPSimulator(), FMDPSimulator(), and ~FMDPSimulator().

◆ currentState_

◆ endState_

Instantiation gum::AbstractSimulator::endState_
inherited

Definition at line 146 of file abstractSimulator.h.

Referenced by hasReachEnd(), and setEndState().


The documentation for this class was generated from the following files: