aGrUM  0.20.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 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)),
49  loaded__(false) {
50  GUM_CONSTRUCTOR(FMDPSimulator);
51  }
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 53 of file fmdpSimulator.cpp.

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

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  }
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 66 of file fmdpSimulator.cpp.

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

66  {
67  GUM_DESTRUCTOR(FMDPSimulator);
68  if (loaded__) delete fmdp__;
69  }
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  {
105  return fmdp__->actionName(actionId);
106  }
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:349

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

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.

◆ 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:95

◆ currentState()

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

Sets the intial statefrom which we begun the simulation.

Definition at line 91 of file abstractSimulator.h.

References gum::AbstractSimulator::reward().

91 { 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.

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

◆ 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:102

◆ 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::Set< Key, Alloc >::emplace().

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  }
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 76 of file fmdpSimulator.cpp.

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

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

◆ 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::Set< Key, Alloc >::emplace().

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:

◆ 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:326

◆ setEndState()

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

Sets the final states upon which a run is over.

Definition at line 83 of file abstractSimulator.h.

References gum::AbstractSimulator::reward().

83  {
84  endState_ = endState;
85  }
+ 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  {
73  currentState_ = initialState;
74  }
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::Set< Key, Alloc >::emplace().

58  {
59  bool hre = true;
60  while (hre) {
62  hre = hasReachEnd();
63  }
64  }
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 114 of file fmdpSimulator.h.

116  {
117  return reinterpret_cast< const MultiDimFunctionGraph< double >* >(
118  fmdp__->transition(actionId, var))
119  ->get(transit);
120  }
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:227

Member Data Documentation

◆ currentState_

Instantiation gum::AbstractSimulator::currentState_
inherited

Tha state in which the system currently is.

Definition at line 129 of file abstractSimulator.h.

◆ endState_

Instantiation gum::AbstractSimulator::endState_
inherited

Definition at line 129 of file abstractSimulator.h.

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

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


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