aGrUM  0.14.2
statesCounter.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}@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  ***************************************************************************/
20 
22 
23 
24 namespace gum {
25 
26 
27  // =================================================================================================
28  //
29  // =================================================================================================
31  __counter(MultiDimFunctionGraph< Size >::getTreeInstance()) {
32  GUM_CONSTRUCTOR(StatesCounter);
33  }
34 
35 
36  // =================================================================================================
37  //
38  // =================================================================================================
40  delete __counter;
41  GUM_DESTRUCTOR(StatesCounter);
42  }
43 
44 
45  // =================================================================================================
46  //
47  // =================================================================================================
48  void StatesCounter::reset(const Instantiation& initialState) {
49  __counter->clear();
51  initialState.variablesSequence().beginSafe();
52  varIter != initialState.variablesSequence().endSafe();
53  ++varIter)
54  __counter->add(**varIter);
55 
57 
58  __incState(initialState, 0, 0, 0);
59  }
60 
61 
62  // =================================================================================================
63  //
64  // =================================================================================================
66  Idx nbVisits = __counter->get(state);
67 
68  NodeId parId = __counter->root();
69  Idx parModa = state.valFromPtr(__counter->node(parId)->nodeVar());
70  while (!__counter->isTerminalNode(__counter->node(parId)->son(parModa))) {
71  parId = __counter->node(parId)->son(parModa);
72  parModa = state.valFromPtr(__counter->node(parId)->nodeVar());
73  }
74  __incState(state, parId, parModa, nbVisits);
75  }
76 
77 
78  // =================================================================================================
79  //
80  // =================================================================================================
82  NodeId parentId,
83  Idx parentModa,
84  Size nbVisits) {
85  Idx varIter = 0;
86  if (parentId)
87  varIter =
88  state.variablesSequence().pos(__counter->node(parentId)->nodeVar()) + 1;
89 
90 
91  for (; varIter < state.variablesSequence().size(); ++varIter) {
92  const DiscreteVariable* curVar = state.variablesSequence().atPos(varIter);
93  NodeId varId = __counter->manager()->addInternalNode(curVar);
94  if (parentId)
95  __counter->manager()->setSon(parentId, parentModa, varId);
96  else
97  __counter->manager()->setRootNode(varId);
98  for (Idx moda = 0; moda < curVar->domainSize(); ++moda) {
99  if (moda == state.valFromPtr(curVar))
100  parentModa = moda;
101  else
103  varId, moda, __counter->terminalNodeId(nbVisits));
104  }
105  parentId = varId;
106  }
108  parentId, parentModa, __counter->manager()->addTerminalNode(nbVisits + 1));
109  }
110 } // End of namespace gum
Safe iterators for Sequence.
Definition: sequence.h:1203
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.
Headers of the States Counter class.
const InternalNode * node(NodeId n) const
Returns internalNode structure associated to that nodeId.
<agrum/FMDP/simulation/statesCounter.h>
Definition: statesCounter.h:48
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.
gum is the global namespace for all aGrUM entities
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:80
void incState(const Instantiation &)
NodeId addTerminalNode(const GUM_SCALAR &value)
Adds a value to the MultiDimFunctionGraph.
Size Idx
Type for indexes.
Definition: types.h:50
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:45
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:97
void clear()
Clears the function graph.