aGrUM 2.3.2
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 (const std::string &resource)
 Default constructor.
 ~FMDPSimulator ()
 Default destructor.
double reward ()
 Sets the intial statefrom which we begun the simulation.
void perform (Idx)
 Sets the intial statefrom which we begun the simulation.
const DiscreteVariableprimeVar (const DiscreteVariable *mainVar)
 Iteration over the variables of the simulated probleme.
SequenceIteratorSafe< const DiscreteVariable * > beginVariables ()
 Iteration over the variables of the simulated probleme.
SequenceIteratorSafe< const DiscreteVariable * > endVariables ()
 Iteration over the variables of the simulated probleme.
virtual const std::string & actionName (Idx actionId)
 Iteration over the variables of the simulated probleme.
SequenceIteratorSafe< IdxbeginActions ()
 Iteration over the variables of the simulated probleme.
SequenceIteratorSafe< IdxendActions ()
 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.
INLINE 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.
INLINE 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.
INLINE 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 69 of file fmdpSimulator.cpp.

69 :
70 AbstractSimulator(), _fmdp_(const_cast< FMDP< double >* >(fmdp)), _loaded_(false) {
71 GUM_CONSTRUCTOR(FMDPSimulator);
72 }
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 ( const std::string & resource)

Default constructor.

Definition at line 74 of file fmdpSimulator.cpp.

74 : AbstractSimulator(), _loaded_(true) {
75 GUM_CONSTRUCTOR(FMDPSimulator);
76
77 _fmdp_ = new FMDP< double >(true);
78 FMDPDatReader< double > reader(_fmdp_, ressource);
79 reader.trace(false);
80 reader.proceed();
81 }

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

Here is the call graph for this function:

◆ ~FMDPSimulator()

gum::FMDPSimulator::~FMDPSimulator ( )

Default destructor.

Definition at line 86 of file fmdpSimulator.cpp.

86 {
87 GUM_DESTRUCTOR(FMDPSimulator);
88 if (_loaded_) delete _fmdp_;
89 }

References FMDPSimulator(), _fmdp_, and _loaded_.

Here is the call graph for this function:

Member Function Documentation

◆ actionName()

virtual const std::string & gum::FMDPSimulator::actionName ( Idx actionId)
inlinevirtual

Iteration over the variables of the simulated probleme.

Implements gum::AbstractSimulator.

Definition at line 124 of file fmdpSimulator.h.

124{ return _fmdp_->actionName(actionId); }

References _fmdp_.

◆ beginActions()

SequenceIteratorSafe< Idx > gum::FMDPSimulator::beginActions ( )
inlinevirtual

Iteration over the variables of the simulated probleme.

Implements gum::AbstractSimulator.

Definition at line 127 of file fmdpSimulator.h.

127{ return _fmdp_->beginActions(); }

References _fmdp_.

◆ beginVariables()

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

Iteration over the variables of the simulated probleme.

Implements gum::AbstractSimulator.

Definition at line 109 of file fmdpSimulator.h.

109 {
110 return _fmdp_->beginVariables();
111 }

References _fmdp_.

Referenced by perform().

Here is the caller graph for this function:

◆ currentState()

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

Sets the intial statefrom which we begun the simulation.

Definition at line 108 of file abstractSimulator.h.

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

References currentState_.

◆ endActions()

SequenceIteratorSafe< Idx > gum::FMDPSimulator::endActions ( )
inlinevirtual

Iteration over the variables of the simulated probleme.

Implements gum::AbstractSimulator.

Definition at line 129 of file fmdpSimulator.h.

129{ return _fmdp_->endActions(); }

References _fmdp_.

◆ endVariables()

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

Iteration over the variables of the simulated probleme.

Implements gum::AbstractSimulator.

Definition at line 113 of file fmdpSimulator.h.

113 {
114 return _fmdp_->endVariables();
115 }

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 100 of file abstractSimulator.cpp.

100 {
101 if (endState_.empty()) return false;
102
103 for (auto varIter = endState_.variablesSequence().beginSafe();
104 varIter != endState_.variablesSequence().endSafe();
105 ++varIter)
106 if (endState_.val(**varIter) != currentState_.val(**varIter)) return false;
107 return true;
108 }

References currentState_, and endState_.

Referenced by setInitialStateRandomly().

Here is the caller graph for this function:

◆ perform()

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

Sets the intial statefrom which we begun the simulation.

Implements gum::AbstractSimulator.

Definition at line 95 of file fmdpSimulator.cpp.

95 {
96 Instantiation newState;
97 for (auto varIter = this->beginVariables(); varIter != this->endVariables(); ++varIter) {
98 newState.add(**varIter);
99 Instantiation transit(currentState_);
100 transit.add(*(this->primeVar(*varIter)));
101
102 double proba = randomProba();
103 double cdd = 0.0;
104 for (transit.setFirstOut(currentState_); !transit.end(); transit.incOut(currentState_)) {
105 cdd += this->transitionProbability_(*varIter, transit, actionId);
106 if (proba <= cdd) {
107 newState.chgVal(**varIter, transit.val(*(this->primeVar(*varIter))));
108 break;
109 }
110 }
111 }
112 currentState_ = newState;
113 }
SequenceIteratorSafe< const DiscreteVariable * > endVariables()
Iteration over the variables of the simulated probleme.
SequenceIteratorSafe< const DiscreteVariable * > beginVariables()
Iteration over the variables of the simulated probleme.
virtual double transitionProbability_(const DiscreteVariable *var, const Instantiation &transit, Idx actionId)
const DiscreteVariable * primeVar(const DiscreteVariable *mainVar)
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()

const DiscreteVariable * gum::FMDPSimulator::primeVar ( const DiscreteVariable * mainVar)
inlinevirtual

Iteration over the variables of the simulated probleme.

Implements gum::AbstractSimulator.

Definition at line 104 of file fmdpSimulator.h.

104 {
105 return _fmdp_->main2prime(mainVar);
106 }

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 90 of file abstractSimulator.cpp.

90 {
91 Instantiation retState;
92 for (auto varIter = this->beginVariables(); varIter != this->endVariables(); ++varIter) {
93 retState.add(**varIter);
94 retState.chgVal(**varIter, randomValue((*varIter)->domainSize()));
95 }
96 return retState;
97 }
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()

double gum::FMDPSimulator::reward ( )
inlinevirtual

Sets the intial statefrom which we begun the simulation.

Implements gum::AbstractSimulator.

Definition at line 93 of file fmdpSimulator.h.

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

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

◆ setEndState()

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

Sets the final states upon which a run is over.

Definition at line 102 of file abstractSimulator.h.

102{ endState_ = endState; }

References endState_.

◆ setInitialState()

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

Sets the intial statefrom which we begun the simulation.

Definition at line 92 of file abstractSimulator.h.

92{ currentState_ = initialState; }

References currentState_.

◆ setInitialStateRandomly()

void gum::AbstractSimulator::setInitialStateRandomly ( )
inherited

Sets the intial statefrom which we begun the simulation.

Definition at line 82 of file abstractSimulator.cpp.

82 {
83 bool hre = true;
84 while (hre) {
86 hre = hasReachEnd();
87 }
88 }
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_()

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

Definition at line 134 of file fmdpSimulator.h.

136 {
137 return reinterpret_cast< const MultiDimFunctionGraph< double >* >(
138 _fmdp_->transition(actionId, var))
139 ->get(transit);
140 }

References _fmdp_, and gum::MultiDimFunctionGraph< GUM_SCALAR, 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 145 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 148 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: