aGrUM  0.16.0
gum::FMDPSimulator Class Reference

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

#include <fmdpSimulator.h>

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

Public Attributes

Instantiation _currentState
 Tha state in which the system currently is. More...
 
Instantiation _endState
 

Public Member Functions

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

Protected Member Functions

virtual double _transitionProbability (const DiscreteVariable *var, const Instantiation &transit, Idx actionId)
 
INLINE void setInitialState (const Instantiation &initialState)
 Sets the intial statefrom which we begun the simulation. More...
 
void setInitialStateRandomly ()
 Sets the intial statefrom which we begun the simulation. More...
 
INLINE void setEndState (const Instantiation &endState)
 Sets the final states upon which a run is over. More...
 
virtual bool hasReachEnd ()
 Tests if end state has been reached. More...
 
INLINE const InstantiationcurrentState ()
 Sets the intial statefrom which we begun the simulation. More...
 
virtual Instantiation _randomState ()
 Choses a random state as the first test for a run. More...
 

Detailed Description

<agrum/FMDP/simulation/fmdpSimulator.h>

A class to simulate a Factored Markov Decision Process.

Definition at line 48 of file fmdpSimulator.h.

Constructor & Destructor Documentation

◆ FMDPSimulator() [1/2]

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

Default constructor.

Definition at line 47 of file fmdpSimulator.cpp.

47  :
48  AbstractSimulator(), __fmdp(const_cast< FMDP< double >* >(fmdp)),
49  __loaded(false) {
50  GUM_CONSTRUCTOR(FMDPSimulator);
51  }
const bool __loaded
Just to know if it should be deleted in the end.
FMDPSimulator(const FMDP< double > *fmdp)
Default constructor.
FMDP< double > * __fmdp
The Factored Markov Decision Process that describes how the system evolves.
AbstractSimulator()
Default constructor.

◆ FMDPSimulator() [2/2]

gum::FMDPSimulator::FMDPSimulator ( const std::string &  resource)

Default constructor.

Definition at line 53 of file fmdpSimulator.cpp.

References __fmdp.

53  :
54  AbstractSimulator(), __loaded(true) {
55  GUM_CONSTRUCTOR(FMDPSimulator);
56 
57  __fmdp = new FMDP< double >(true);
58  FMDPDatReader< double > reader(__fmdp, ressource);
59  reader.trace(false);
60  reader.proceed();
61  }
const bool __loaded
Just to know if it should be deleted in the end.
FMDPSimulator(const FMDP< double > *fmdp)
Default constructor.
FMDP< double > * __fmdp
The Factored Markov Decision Process that describes how the system evolves.
AbstractSimulator()
Default constructor.

◆ ~FMDPSimulator()

gum::FMDPSimulator::~FMDPSimulator ( )

Default destructor.

Definition at line 66 of file fmdpSimulator.cpp.

References __fmdp, and __loaded.

66  {
67  GUM_DESTRUCTOR(FMDPSimulator);
68  if (__loaded) delete __fmdp;
69  }
const bool __loaded
Just to know if it should be deleted in the end.
FMDPSimulator(const FMDP< double > *fmdp)
Default constructor.
FMDP< double > * __fmdp
The Factored Markov Decision Process that describes how the system evolves.

Member Function Documentation

◆ _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 66 of file abstractSimulator.cpp.

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

Referenced by gum::TaxiSimulator::_randomState(), gum::AbstractSimulator::setInitialState(), and gum::AbstractSimulator::setInitialStateRandomly().

66  {
67  Instantiation retState;
68  for (auto varIter = this->beginVariables(); varIter != this->endVariables();
69  ++varIter) {
70  retState.add(**varIter);
71  retState.chgVal(*varIter,
72  (Idx)(((double)std::rand() / (double)RAND_MAX)
73  * (double)(*varIter)->domainSize()));
74  }
75  return retState;
76  }
virtual SequenceIteratorSafe< const DiscreteVariable *> beginVariables()=0
Iteration over the variables of the simulated probleme.
virtual SequenceIteratorSafe< const DiscreteVariable *> endVariables()=0
Iteration over the variables of the simulated probleme.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _transitionProbability()

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

Definition at line 114 of file fmdpSimulator.h.

References __fmdp, and gum::FMDP< GUM_SCALAR >::transition().

Referenced by perform().

116  {
117  return reinterpret_cast< const MultiDimFunctionGraph< double >* >(
118  __fmdp->transition(actionId, var))
119  ->get(transit);
120  }
const MultiDimImplementation< GUM_SCALAR > * transition(Idx actionId, const DiscreteVariable *v) const
Returns transition associated to given in parameter variable and the given action.
Definition: fmdp_tpl.h:227
FMDP< double > * __fmdp
The Factored Markov Decision Process that describes how the system evolves.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ 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 104 of file fmdpSimulator.h.

References __fmdp, and gum::FMDP< GUM_SCALAR >::actionName().

104  {
105  return __fmdp->actionName(actionId);
106  }
const std::string & actionName(Idx actionId) const
Returns name of action given in parameter.
Definition: fmdp_tpl.h:347
FMDP< double > * __fmdp
The Factored Markov Decision Process that describes how the system evolves.
+ Here is the call graph for this function:

◆ beginActions()

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

Iteration over the variables of the simulated probleme.

Implements gum::AbstractSimulator.

Definition at line 109 of file fmdpSimulator.h.

References __fmdp, and gum::FMDP< GUM_SCALAR >::beginActions().

109 { return __fmdp->beginActions(); }
SequenceIteratorSafe< Idx > beginActions() const
Returns an iterator reference to he beginning of the list of actions.
Definition: fmdp.h:137
FMDP< double > * __fmdp
The Factored Markov Decision Process that describes how the system evolves.
+ Here is the call graph for this function:

◆ beginVariables()

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

Iteration over the variables of the simulated probleme.

Implements gum::AbstractSimulator.

Definition at line 90 of file fmdpSimulator.h.

References __fmdp, and gum::FMDP< GUM_SCALAR >::beginVariables().

Referenced by perform().

90  {
91  return __fmdp->beginVariables();
92  }
SequenceIteratorSafe< const DiscreteVariable *> beginVariables() const
Returns an iterator reference to he beginning of the list of variables.
Definition: fmdp.h:95
FMDP< double > * __fmdp
The Factored Markov Decision Process that describes how the system evolves.
+ Here is the call graph for this function:
+ 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 92 of file abstractSimulator.h.

References gum::AbstractSimulator::_currentState, gum::AbstractSimulator::actionName(), gum::AbstractSimulator::beginActions(), gum::AbstractSimulator::beginVariables(), gum::AbstractSimulator::endActions(), gum::AbstractSimulator::endVariables(), gum::AbstractSimulator::perform(), gum::AbstractSimulator::primeVar(), and gum::AbstractSimulator::reward().

92 { return _currentState; }
Instantiation _currentState
Tha state in which the system currently is.
+ Here is the call graph for this function:

◆ endActions()

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

Iteration over the variables of the simulated probleme.

Implements gum::AbstractSimulator.

Definition at line 110 of file fmdpSimulator.h.

References __fmdp, and gum::FMDP< GUM_SCALAR >::endActions().

110 { return __fmdp->endActions(); }
FMDP< double > * __fmdp
The Factored Markov Decision Process that describes how the system evolves.
SequenceIteratorSafe< Idx > endActions() const
Returns an iterator reference to the end of the list of actions.
Definition: fmdp.h:144
+ Here is the call graph for this function:

◆ endVariables()

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

Iteration over the variables of the simulated probleme.

Implements gum::AbstractSimulator.

Definition at line 93 of file fmdpSimulator.h.

References __fmdp, and gum::FMDP< GUM_SCALAR >::endVariables().

Referenced by perform().

93  {
94  return __fmdp->endVariables();
95  }
FMDP< double > * __fmdp
The Factored Markov Decision Process that describes how the system evolves.
SequenceIteratorSafe< const DiscreteVariable *> endVariables() const
Returns an iterator reference to the end of the list of variables.
Definition: fmdp.h:102
+ Here is the call graph for this function:
+ 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 79 of file abstractSimulator.cpp.

References gum::AbstractSimulator::_currentState, gum::AbstractSimulator::_endState, gum::Instantiation::empty(), gum::Instantiation::val(), and gum::Instantiation::variablesSequence().

Referenced by gum::AbstractSimulator::setEndState(), and gum::AbstractSimulator::setInitialStateRandomly().

79  {
80  if (_endState.empty()) return false;
81 
82  for (auto varIter = _endState.variablesSequence().beginSafe();
83  varIter != _endState.variablesSequence().endSafe();
84  ++varIter)
85  if (_endState.val(**varIter) != _currentState.val(**varIter)) return false;
86  return true;
87  }
const Sequence< const DiscreteVariable *> & variablesSequence() const final
Returns the sequence of DiscreteVariable of this instantiation.
Idx val(Idx i) const
Returns the current value of the variable at position i.
Instantiation _currentState
Tha state in which the system currently is.
virtual bool empty() const final
Returns true if the instantiation is empty.
+ Here is the call graph for this function:
+ 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 76 of file fmdpSimulator.cpp.

References gum::AbstractSimulator::_currentState, _transitionProbability(), gum::Instantiation::add(), beginVariables(), gum::Instantiation::chgVal(), gum::Instantiation::end(), endVariables(), gum::Instantiation::incOut(), primeVar(), gum::Instantiation::setFirstOut(), and gum::Instantiation::val().

Referenced by reward().

76  {
77  Instantiation newState;
78  for (auto varIter = this->beginVariables(); varIter != this->endVariables();
79  ++varIter) {
80  newState.add(**varIter);
81  Instantiation transit(_currentState);
82  transit.add(*(this->primeVar(*varIter)));
83 
84  double proba = (double)std::rand() / (double)RAND_MAX;
85  double cdd = 0.0;
86  for (transit.setFirstOut(_currentState); !transit.end();
87  transit.incOut(_currentState)) {
88  cdd += this->_transitionProbability(*varIter, transit, actionId);
89  if (proba <= cdd) {
90  newState.chgVal(**varIter, transit.val(*(this->primeVar(*varIter))));
91  break;
92  }
93  }
94  }
95  _currentState = newState;
96  }
const DiscreteVariable * primeVar(const DiscreteVariable *mainVar)
Iteration over the variables of the simulated probleme.
Definition: fmdpSimulator.h:85
virtual double _transitionProbability(const DiscreteVariable *var, const Instantiation &transit, Idx actionId)
Instantiation _currentState
Tha state in which the system currently is.
SequenceIteratorSafe< const DiscreteVariable *> endVariables()
Iteration over the variables of the simulated probleme.
Definition: fmdpSimulator.h:93
SequenceIteratorSafe< const DiscreteVariable *> beginVariables()
Iteration over the variables of the simulated probleme.
Definition: fmdpSimulator.h:90
+ Here is the call graph for this function:
+ Here is the caller 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 85 of file fmdpSimulator.h.

References __fmdp, and gum::FMDP< GUM_SCALAR >::main2prime().

Referenced by perform().

85  {
86  return __fmdp->main2prime(mainVar);
87  }
const DiscreteVariable * main2prime(const DiscreteVariable *mainVar) const
Returns the primed variable associate to the given main variable.
Definition: fmdp.h:109
FMDP< double > * __fmdp
The Factored Markov Decision Process that describes how the system evolves.
+ 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 74 of file fmdpSimulator.h.

References __fmdp, gum::AbstractSimulator::_currentState, perform(), and gum::FMDP< GUM_SCALAR >::reward().

74 { return __fmdp->reward()->get(this->_currentState); }
Instantiation _currentState
Tha state in which the system currently is.
const MultiDimImplementation< GUM_SCALAR > * reward(Idx actionId=0) const
Returns the reward table of mdp.
Definition: fmdp_tpl.h:324
FMDP< double > * __fmdp
The Factored Markov Decision Process that describes how the system evolves.
+ Here is the call graph for this function:

◆ setEndState()

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

Sets the final states upon which a run is over.

Definition at line 84 of file abstractSimulator.h.

References gum::AbstractSimulator::_endState, and gum::AbstractSimulator::hasReachEnd().

84  {
85  _endState = endState;
86  }
+ Here is the call graph for this function:

◆ setInitialState()

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

Sets the intial statefrom which we begun the simulation.

Definition at line 73 of file abstractSimulator.h.

References gum::AbstractSimulator::_currentState, gum::AbstractSimulator::_randomState(), and gum::AbstractSimulator::setInitialStateRandomly().

73  {
74  _currentState = initialState;
75  }
Instantiation _currentState
Tha state in which the system currently is.
+ Here is the call graph for this function:

◆ setInitialStateRandomly()

void gum::AbstractSimulator::setInitialStateRandomly ( )
inherited

Sets the intial statefrom which we begun the simulation.

Definition at line 58 of file abstractSimulator.cpp.

References gum::AbstractSimulator::_currentState, gum::AbstractSimulator::_randomState(), and gum::AbstractSimulator::hasReachEnd().

Referenced by gum::AbstractSimulator::setInitialState().

58  {
59  bool hre = true;
60  while (hre) {
62  hre = hasReachEnd();
63  }
64  }
virtual bool hasReachEnd()
Tests if end state has been reached.
Instantiation _currentState
Tha state in which the system currently is.
virtual Instantiation _randomState()
Choses a random state as the first test for a run.
+ 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 125 of file fmdpSimulator.h.

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

◆ __loaded

const bool gum::FMDPSimulator::__loaded
private

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

Definition at line 128 of file fmdpSimulator.h.

Referenced by ~FMDPSimulator().

◆ _currentState

◆ _endState

Instantiation gum::AbstractSimulator::_endState
inherited

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