aGrUM  0.20.3
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 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.

References gum::Set< Key, Alloc >::emplace().

47  :
48  AbstractSimulator(), _fmdp_(const_cast< FMDP< double >* >(fmdp)), _loaded_(false) {
49  GUM_CONSTRUCTOR(FMDPSimulator);
50  }
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.
FMDPSimulator(const FMDP< double > *fmdp)
Default constructor.
AbstractSimulator()
Default constructor.
+ Here is the call graph for this function:

◆ FMDPSimulator() [2/2]

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

Default constructor.

Definition at line 52 of file fmdpSimulator.cpp.

References gum::Set< Key, Alloc >::emplace().

52  : AbstractSimulator(), _loaded_(true) {
53  GUM_CONSTRUCTOR(FMDPSimulator);
54 
55  _fmdp_ = new FMDP< double >(true);
56  FMDPDatReader< double > reader(_fmdp_, ressource);
57  reader.trace(false);
58  reader.proceed();
59  }
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.
FMDPSimulator(const FMDP< double > *fmdp)
Default constructor.
AbstractSimulator()
Default constructor.
+ Here is the call graph for this function:

◆ ~FMDPSimulator()

gum::FMDPSimulator::~FMDPSimulator ( )

Default destructor.

Definition at line 64 of file fmdpSimulator.cpp.

References gum::Set< Key, Alloc >::emplace().

64  {
65  GUM_DESTRUCTOR(FMDPSimulator);
66  if (_loaded_) delete _fmdp_;
67  }
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.
FMDPSimulator(const FMDP< double > *fmdp)
Default constructor.
+ 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 104 of file fmdpSimulator.h.

104 { return _fmdp_->actionName(actionId); }
FMDP< double > * _fmdp_
The Factored Markov Decision Process that describes how the system evolves.
const std::string & actionName(Idx actionId) const
Returns name of action given in parameter.
Definition: fmdp_tpl.h:314

◆ beginActions()

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

Iteration over the variables of the simulated probleme.

Implements gum::AbstractSimulator.

Definition at line 107 of file fmdpSimulator.h.

107 { return _fmdp_->beginActions(); }
FMDP< double > * _fmdp_
The Factored Markov Decision Process that describes how the system evolves.
SequenceIteratorSafe< Idx > beginActions() const
Returns an iterator reference to he beginning of the list of actions.
Definition: fmdp.h:136

◆ 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.

90  {
91  return _fmdp_->beginVariables();
92  }
FMDP< double > * _fmdp_
The Factored Markov Decision Process that describes how the system evolves.
SequenceIteratorSafe< const DiscreteVariable *> beginVariables() const
Returns an iterator reference to he beginning of the list of variables.
Definition: fmdp.h:94

◆ currentState()

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

Sets the intial statefrom which we begun the simulation.

Definition at line 87 of file abstractSimulator.h.

References gum::AbstractSimulator::reward().

87 { 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 108 of file fmdpSimulator.h.

108 { 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:141

◆ 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.

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:101

◆ hasReachEnd()

bool gum::AbstractSimulator::hasReachEnd ( )
virtualinherited

Tests if end state has been reached.

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

Definition at line 84 of file abstractSimulator.cpp.

References gum::Set< Key, Alloc >::emplace().

84  {
85  if (endState_.empty()) return false;
86 
87  for (auto varIter = endState_.variablesSequence().beginSafe();
88  varIter != endState_.variablesSequence().endSafe();
89  ++varIter)
90  if (endState_.val(**varIter) != currentState_.val(**varIter)) return false;
91  return true;
92  }
Instantiation currentState_
Tha state in which the system currently is.
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.
virtual bool empty() const final
Returns true if the instantiation is empty.
+ Here is the call 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 74 of file fmdpSimulator.cpp.

References gum::Set< Key, Alloc >::emplace().

74  {
75  Instantiation newState;
76  for (auto varIter = this->beginVariables(); varIter != this->endVariables(); ++varIter) {
77  newState.add(**varIter);
78  Instantiation transit(currentState_);
79  transit.add(*(this->primeVar(*varIter)));
80 
81  double proba = (double)std::rand() / (double)RAND_MAX;
82  double cdd = 0.0;
83  for (transit.setFirstOut(currentState_); !transit.end(); transit.incOut(currentState_)) {
84  cdd += this->transitionProbability_(*varIter, transit, actionId);
85  if (proba <= cdd) {
86  newState.chgVal(**varIter, transit.val(*(this->primeVar(*varIter))));
87  break;
88  }
89  }
90  }
91  currentState_ = newState;
92  }
const DiscreteVariable * primeVar(const DiscreteVariable *mainVar)
Iteration over the variables of the simulated probleme.
Definition: fmdpSimulator.h:85
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
virtual double transitionProbability_(const DiscreteVariable *var, const Instantiation &transit, Idx actionId)
+ 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 85 of file fmdpSimulator.h.

85  {
86  return _fmdp_->main2prime(mainVar);
87  }
FMDP< double > * _fmdp_
The Factored Markov Decision Process that describes how the system evolves.
const DiscreteVariable * main2prime(const DiscreteVariable *mainVar) const
Returns the primed variable associate to the given main variable.
Definition: fmdp.h:108

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

References gum::Set< Key, Alloc >::emplace().

72  {
73  Instantiation retState;
74  for (auto varIter = this->beginVariables(); varIter != this->endVariables(); ++varIter) {
75  retState.add(**varIter);
76  retState.chgVal(
77  *varIter,
78  (Idx)(((double)std::rand() / (double)RAND_MAX) * (double)(*varIter)->domainSize()));
79  }
80  return retState;
81  }
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:

◆ 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.

74 { return _fmdp_->reward()->get(this->currentState_); }
FMDP< double > * _fmdp_
The Factored Markov Decision Process that describes how the system evolves.
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:293

◆ setEndState()

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

Sets the final states upon which a run is over.

Definition at line 81 of file abstractSimulator.h.

References gum::AbstractSimulator::reward().

81 { endState_ = endState; }
+ 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 72 of file abstractSimulator.h.

References gum::AbstractSimulator::reward().

72 { currentState_ = initialState; }
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 64 of file abstractSimulator.cpp.

References gum::Set< Key, Alloc >::emplace().

64  {
65  bool hre = true;
66  while (hre) {
68  hre = hasReachEnd();
69  }
70  }
virtual bool hasReachEnd()
Tests if end state has been reached.
virtual Instantiation randomState_()
Choses a random state as the first test for a run.
Instantiation currentState_
Tha state in which the system currently is.
+ 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 112 of file fmdpSimulator.h.

114  {
115  return reinterpret_cast< const MultiDimFunctionGraph< double >* >(
116  _fmdp_->transition(actionId, var))
117  ->get(transit);
118  }
FMDP< double > * _fmdp_
The Factored Markov Decision Process that describes how the system evolves.
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:208

Member Data Documentation

◆ _fmdp_

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

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

Definition at line 123 of file fmdpSimulator.h.

◆ _loaded_

const bool gum::FMDPSimulator::_loaded_
private

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

Definition at line 126 of file fmdpSimulator.h.

◆ currentState_

Instantiation gum::AbstractSimulator::currentState_
inherited

Tha state in which the system currently is.

Definition at line 125 of file abstractSimulator.h.

◆ endState_

Instantiation gum::AbstractSimulator::endState_
inherited

Definition at line 125 of file abstractSimulator.h.


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