aGrUM  0.16.0
fmdpSimulator.cpp
Go to the documentation of this file.
1 
30 // =====================================================================
31 #include <cstdlib>
32 #include <random>
33 //======================================================================
36 //======================================================================
37 
38 namespace gum {
39 
40  // ===========================================================================
41  // Constructors, Destructors.
42  // ===========================================================================
43 
44  /*
45  * Default constructor.
46  */
48  AbstractSimulator(), __fmdp(const_cast< FMDP< double >* >(fmdp)),
49  __loaded(false) {
50  GUM_CONSTRUCTOR(FMDPSimulator);
51  }
52 
53  FMDPSimulator::FMDPSimulator(const std::string& ressource) :
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  }
62 
63  /*
64  * Default destructor.
65  */
67  GUM_DESTRUCTOR(FMDPSimulator);
68  if (__loaded) delete __fmdp;
69  }
70 
71 
72  // ===========================================================================
73  //
74  // ===========================================================================
75 
76  void FMDPSimulator::perform(Idx actionId) {
77  Instantiation newState;
78  for (auto varIter = this->beginVariables(); varIter != this->endVariables();
79  ++varIter) {
80  newState.add(**varIter);
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  }
97 
98 
99 } // End of namespace gum
void perform(Idx)
Sets the intial statefrom which we begun the simulation.
<agrum/FMDP/simulation/abstractSimulator.h>
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 & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
<agrum/FMDP/simulation/fmdpSimulator.h>
Definition: fmdpSimulator.h:48
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
const bool __loaded
Just to know if it should be deleted in the end.
Definition of templatized reader of FMDPDat files for Factored Markov Decision Processes.
Definition: fmdpDatReader.h:94
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.
void incOut(const Instantiation &i)
Operator increment for the variables not in i.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
SequenceIteratorSafe< const DiscreteVariable *> endVariables()
Iteration over the variables of the simulated probleme.
Definition: fmdpSimulator.h:93
FMDPSimulator(const FMDP< double > *fmdp)
Default constructor.
SequenceIteratorSafe< const DiscreteVariable *> beginVariables()
Iteration over the variables of the simulated probleme.
Definition: fmdpSimulator.h:90
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
FMDP< double > * __fmdp
The Factored Markov Decision Process that describes how the system evolves.
Size Idx
Type for indexes.
Definition: types.h:53
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
bool end() const
Returns true if the Instantiation reached the end.
void setFirstOut(const Instantiation &i)
Assign the first values in the Instantiation for the variables not in i.
~FMDPSimulator()
Default destructor.