aGrUM  0.14.2
fmdpSimulator.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 // =====================================================================
28 #include <cstdlib>
29 #include <random>
30 //======================================================================
33 //======================================================================
34 
35 namespace gum {
36 
37  // ===========================================================================
38  // Constructors, Destructors.
39  // ===========================================================================
40 
41  /*
42  * Default constructor.
43  */
45  AbstractSimulator(), __fmdp(const_cast< FMDP< double >* >(fmdp)),
46  __loaded(false) {
47  GUM_CONSTRUCTOR(FMDPSimulator);
48  }
49 
50  FMDPSimulator::FMDPSimulator(const std::string& ressource) :
51  AbstractSimulator(), __loaded(true) {
52  GUM_CONSTRUCTOR(FMDPSimulator);
53 
54  __fmdp = new FMDP< double >(true);
55  FMDPDatReader< double > reader(__fmdp, ressource);
56  reader.trace(false);
57  reader.proceed();
58  }
59 
60  /*
61  * Default destructor.
62  */
64  GUM_DESTRUCTOR(FMDPSimulator);
65  if (__loaded) delete __fmdp;
66  }
67 
68 
69  // ===========================================================================
70  //
71  // ===========================================================================
72 
73  void FMDPSimulator::perform(Idx actionId) {
74  Instantiation newState;
75  for (auto varIter = this->beginVariables(); varIter != this->endVariables();
76  ++varIter) {
77  newState.add(**varIter);
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();
84  transit.incOut(_currentState)) {
85  cdd += this->_transitionProbability(*varIter, transit, actionId);
86  if (proba <= cdd) {
87  newState.chgVal(**varIter, transit.val(*(this->primeVar(*varIter))));
88  break;
89  }
90  }
91  }
92  _currentState = newState;
93  }
94 
95 
96 } // 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:82
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:45
gum is the global namespace for all aGrUM entities
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:91
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.
Class for simulating a markov decision process.
SequenceIteratorSafe< const DiscreteVariable *> endVariables()
Iteration over the variables of the simulated probleme.
Definition: fmdpSimulator.h:90
FMDPSimulator(const FMDP< double > *fmdp)
Default constructor.
SequenceIteratorSafe< const DiscreteVariable *> beginVariables()
Iteration over the variables of the simulated probleme.
Definition: fmdpSimulator.h:87
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:80
FMDP< double > * __fmdp
The Factored Markov Decision Process that describes how the system evolves.
Size Idx
Type for indexes.
Definition: types.h:50
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
Definition of templatized reader of dat files for Factored Markov Decision Process.
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.