aGrUM  0.16.0
statesCounter.cpp
Go to the documentation of this file.
1 
24 
25 
26 namespace gum {
27 
28 
29  // =================================================================================================
30  //
31  // =================================================================================================
33  __counter(MultiDimFunctionGraph< Size >::getTreeInstance()) {
34  GUM_CONSTRUCTOR(StatesCounter);
35  }
36 
37 
38  // =================================================================================================
39  //
40  // =================================================================================================
42  delete __counter;
43  GUM_DESTRUCTOR(StatesCounter);
44  }
45 
46 
47  // =================================================================================================
48  //
49  // =================================================================================================
50  void StatesCounter::reset(const Instantiation& initialState) {
51  __counter->clear();
53  initialState.variablesSequence().beginSafe();
54  varIter != initialState.variablesSequence().endSafe();
55  ++varIter)
56  __counter->add(**varIter);
57 
59 
60  __incState(initialState, 0, 0, 0);
61  }
62 
63 
64  // =================================================================================================
65  //
66  // =================================================================================================
68  Idx nbVisits = __counter->get(state);
69 
70  NodeId parId = __counter->root();
71  Idx parModa = state.valFromPtr(__counter->node(parId)->nodeVar());
72  while (!__counter->isTerminalNode(__counter->node(parId)->son(parModa))) {
73  parId = __counter->node(parId)->son(parModa);
74  parModa = state.valFromPtr(__counter->node(parId)->nodeVar());
75  }
76  __incState(state, parId, parModa, nbVisits);
77  }
78 
79 
80  // =================================================================================================
81  //
82  // =================================================================================================
84  NodeId parentId,
85  Idx parentModa,
86  Size nbVisits) {
87  Idx varIter = 0;
88  if (parentId)
89  varIter =
90  state.variablesSequence().pos(__counter->node(parentId)->nodeVar()) + 1;
91 
92 
93  for (; varIter < state.variablesSequence().size(); ++varIter) {
94  const DiscreteVariable* curVar = state.variablesSequence().atPos(varIter);
95  NodeId varId = __counter->manager()->addInternalNode(curVar);
96  if (parentId)
97  __counter->manager()->setSon(parentId, parentModa, varId);
98  else
99  __counter->manager()->setRootNode(varId);
100  for (Idx moda = 0; moda < curVar->domainSize(); ++moda) {
101  if (moda == state.valFromPtr(curVar))
102  parentModa = moda;
103  else
105  varId, moda, __counter->terminalNodeId(nbVisits));
106  }
107  parentId = varId;
108  }
110  parentId, parentModa, __counter->manager()->addTerminalNode(nbVisits + 1));
111  }
112 } // End of namespace gum
Safe iterators for Sequence.
Definition: sequence.h:1206
void setSon(const NodeId &node, const Idx &modality, const NodeId &sonNode)
Sets nodes son for given modality to designated son node.
bool isTerminalNode(const NodeId &node) const
Indicates if given node is terminal or not.
Idx valFromPtr(const DiscreteVariable *pvar) const
Returns the current value of a given variable.
void setRootNode(const NodeId &root)
Sets root node of decision diagram.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
const InternalNode * node(NodeId n) const
Returns internalNode structure associated to that nodeId.
<agrum/FMDP/simulation/statesCounter.h>
Definition: statesCounter.h:51
const DiscreteVariable * nodeVar() const
Returns the node variable.
const NodeId & terminalNodeId(const GUM_SCALAR &v) const
Returns the id of the terminal node that has the given value.
NodeId son(Idx modality) const
Returns the son at a given index.
NodeId addInternalNode(const DiscreteVariable *var)
Inserts a new non terminal node in graph.
Base class for discrete random variable.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
const Sequence< const DiscreteVariable *> & variablesSequence() const final
Returns the sequence of DiscreteVariable of this instantiation.
virtual Size domainSize() const =0
virtual void add(const DiscreteVariable &v)
Adds a new var to the variables of the multidimensional matrix.
MultiDimFunctionGraph< Size > * __counter
const NodeId & root() const
Returns the id of the root node from the diagram.
Class implementingting a function graph.
~StatesCounter()
Default destructor.
void reset(const Instantiation &)
void __incState(const Instantiation &, NodeId, Idx, Size)
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
void incState(const Instantiation &)
NodeId addTerminalNode(const GUM_SCALAR &value)
Adds a value to the MultiDimFunctionGraph.
Size Idx
Type for indexes.
Definition: types.h:53
MultiDimFunctionGraphManager< GUM_SCALAR, TerminalNodePolicy > * manager()
Returns a const reference to the manager of this diagram.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
StatesCounter()
Default constructor.
virtual GUM_SCALAR get(const Instantiation &i) const
Returns the value pointed by i.
Size NodeId
Type for node ids.
Definition: graphElements.h:98
void clear()
Clears the function graph.