aGrUM  0.16.0
iti_tpl.h
Go to the documentation of this file.
1 
29 // =======================================================
30 #include <agrum/core/math/math.h>
32 #include <agrum/core/types.h>
33 // =======================================================
36 // =======================================================
38 // =======================================================
39 
40 
41 namespace gum {
42 
43  // ==========================================================================
45  // ==========================================================================
46 
47  // ###################################################################
59  // ###################################################################
60  template < TESTNAME AttributeSelection, bool isScalar >
63  double attributeSelectionThreshold,
64  Set< const DiscreteVariable* > attributeListe,
65  const DiscreteVariable* learnedValue) :
66  IncrementalGraphLearner< AttributeSelection, isScalar >(
67  target, attributeListe, learnedValue),
68  __nbTotalObservation(0),
69  __attributeSelectionThreshold(attributeSelectionThreshold) {
70  GUM_CONSTRUCTOR(ITI);
71  __staleTable.insert(this->_root, false);
72  }
73 
74  // ###################################################################
85  // ###################################################################
86  template < TESTNAME AttributeSelection, bool isScalar >
89  double attributeSelectionThreshold,
90  Set< const DiscreteVariable* > attributeListe) :
91  IncrementalGraphLearner< AttributeSelection, isScalar >(
92  target, attributeListe, new LabelizedVariable("Reward", "", 2)),
94  __attributeSelectionThreshold(attributeSelectionThreshold) {
95  GUM_CONSTRUCTOR(ITI);
96  __staleTable.insert(this->_root, false);
97  }
98 
99 
100  // ==========================================================================
102  // ==========================================================================
103 
104  // ############################################################################
109  // ############################################################################
110  template < TESTNAME AttributeSelection, bool isScalar >
111  void
115  }
116 
117  // ############################################################################
124  // ############################################################################
125  template < TESTNAME AttributeSelection, bool isScalar >
127  const Observation* newObs, NodeId currentNodeId) {
128  IncrementalGraphLearner< AttributeSelection,
129  isScalar >::_updateNodeWithObservation(newObs,
130  currentNodeId);
131  __staleTable[currentNodeId] = true;
132  }
133 
134 
135  // ============================================================================
137  // ============================================================================
138 
139  // ############################################################################
141  // ############################################################################
142  template < TESTNAME AttributeSelection, bool isScalar >
144  std::vector< NodeId > filo;
145  filo.push_back(this->_root);
147  potentialVars.insert(this->_root,
149 
150 
151  while (!filo.empty()) {
152  NodeId currentNodeId = filo.back();
153  filo.pop_back();
154 
155  // First we look for the best var to install on the node
156  double bestValue = __attributeSelectionThreshold;
158 
159  for (auto varIter = potentialVars[currentNodeId]->cbeginSafe();
160  varIter != potentialVars[currentNodeId]->cendSafe();
161  ++varIter)
162  if (this->_nodeId2Database[currentNodeId]->isTestRelevant(*varIter)) {
163  double varValue =
164  this->_nodeId2Database[currentNodeId]->testValue(*varIter);
165  if (varValue >= bestValue) {
166  if (varValue > bestValue) {
167  bestValue = varValue;
168  bestVars.clear();
169  }
170  bestVars.insert(*varIter);
171  }
172  }
173 
174  // Then We installed Variable a test on that node
175  this->_updateNode(currentNodeId, bestVars);
176 
177  // The we move on the children if needed
178  if (this->_nodeVarMap[currentNodeId] != this->_value) {
179  for (Idx moda = 0; moda < this->_nodeVarMap[currentNodeId]->domainSize();
180  moda++) {
181  Set< const DiscreteVariable* >* itsPotentialVars =
182  new Set< const DiscreteVariable* >(*potentialVars[currentNodeId]);
183  itsPotentialVars->erase(this->_nodeVarMap[currentNodeId]);
184  NodeId sonId = this->_nodeSonsMap[currentNodeId][moda];
185  if (__staleTable[sonId]) {
186  filo.push_back(sonId);
187  potentialVars.insert(sonId, itsPotentialVars);
188  }
189  }
190  }
191  }
192 
194  nodeIter = potentialVars.beginSafe();
195  nodeIter != potentialVars.endSafe();
196  ++nodeIter)
197  delete nodeIter.val();
198  }
199 
200 
201  // ############################################################################
208  // ############################################################################
209  template < TESTNAME AttributeSelection, bool isScalar >
212  const DiscreteVariable* boundVar) {
213  NodeId n =
215  nDB, boundVar);
216  __staleTable.insert(n, true);
217  return n;
218  }
219 
220 
221  // ############################################################################
227  // ############################################################################
228  template < TESTNAME AttributeSelection, bool isScalar >
230  NodeId currentNodeId, const DiscreteVariable* desiredVar) {
231  if (this->_nodeVarMap[currentNodeId] != desiredVar) {
232  __staleTable[currentNodeId] = true;
234  currentNodeId, desiredVar);
235  }
236  }
237 
238 
239  // ############################################################################
244  // ############################################################################
245  template < TESTNAME AttributeSelection, bool isScalar >
248  currentNodeId);
249  __staleTable.erase(currentNodeId);
250  }
251 
252 
253  // ============================================================================
255  // ============================================================================
256 
257  // ############################################################################
259  // ############################################################################
260  template < TESTNAME AttributeSelection, bool isScalar >
262  this->_target->clear();
263  this->_target->manager()->setRootNode(
264  this->__insertNodeInFunctionGraph(this->_root));
265  }
266 
267 
268  // ############################################################################
274  // ############################################################################
275  template < TESTNAME AttributeSelection, bool isScalar >
277  NodeId currentNodeId) {
278  if (this->_nodeVarMap[currentNodeId] == this->_value) {
279  NodeId nody = __insertTerminalNode(currentNodeId);
280  return nody;
281  }
282 
283  if (!this->_target->variablesSequence().exists(
284  this->_nodeVarMap[currentNodeId])) {
285  this->_target->add(*(this->_nodeVarMap[currentNodeId]));
286  }
287 
288  NodeId nody =
289  this->_target->manager()->addInternalNode(this->_nodeVarMap[currentNodeId]);
290  for (Idx moda = 0; moda < this->_nodeVarMap[currentNodeId]->domainSize();
291  ++moda) {
293  this->_nodeSonsMap[currentNodeId][moda]);
294  this->_target->manager()->setSon(nody, moda, son);
295  }
296 
297  return nody;
298  }
299 
300 
301  // ############################################################################
309  // ############################################################################
310  template < TESTNAME AttributeSelection, bool isScalar >
312  NodeId currentNodeId, Int2Type< false >) {
313  if (!this->_target->variablesSequence().exists(this->_value))
314  this->_target->add(*(this->_value));
315 
316  Size tot = this->_nodeId2Database[currentNodeId]->nbObservation();
317  if (tot == Size(0)) return this->_target->manager()->addTerminalNode(0.0);
318 
319  NodeId* sonsMap = static_cast< NodeId* >(
320  SOA_ALLOCATE(sizeof(NodeId) * this->_value->domainSize()));
321  for (Idx modality = 0; modality < this->_value->domainSize(); ++modality) {
322  double newVal = 0.0;
323  newVal = (double)this->_nodeId2Database[currentNodeId]->effectif(modality)
324  / (double)tot;
325  sonsMap[modality] = this->_target->manager()->addTerminalNode(newVal);
326  }
327  NodeId nody = this->_target->manager()->addInternalNode(this->_value, sonsMap);
328  return nody;
329  }
330 
331 
332  // ############################################################################
340  // ############################################################################
341  template < TESTNAME AttributeSelection, bool isScalar >
343  NodeId currentNodeId, Int2Type< true >) {
344  double value = 0.0;
345  for (auto valIter = this->_nodeId2Database[currentNodeId]->cbeginValues();
346  valIter != this->_nodeId2Database[currentNodeId]->cendValues();
347  ++valIter) {
348  value += (double)valIter.key() * valIter.val();
349  }
350  if (this->_nodeId2Database[currentNodeId]->nbObservation())
351  value /= (double)this->_nodeId2Database[currentNodeId]->nbObservation();
352  NodeId nody = this->_target->manager()->addTerminalNode(value);
353  return nody;
354  }
355 } // namespace gum
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
HashTable< NodeId, NodeId *> _nodeSonsMap
A table giving for any node a table mapping to its son idx is the modality of associated variable...
void updateFunctionGraph()
Updates target to currently learned graph structure.
Definition: iti_tpl.h:261
NodeId _insertNode(NodeDatabase< AttributeSelection, isScalar > *nDB, const DiscreteVariable *boundVar)
inserts a new node in internal graph
Definition: iti_tpl.h:210
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void setSon(const NodeId &node, const Idx &modality, const NodeId &sonNode)
Sets nodes son for given modality to designated son node.
Set< const DiscreteVariable *> _setOfVars
NodeId __insertNodeInFunctionGraph(NodeId src)
Inserts an internal node in the target.
Definition: iti_tpl.h:276
HashTable< NodeId, NodeDatabase< AttributeSelection, isScalar > *> _nodeId2Database
This hashtable binds every node to an associated NodeDatabase which handles every observation that co...
double __attributeSelectionThreshold
The threshold above which we consider variables to be dependant.
Definition: iti.h:262
Learn a graphical representation of a function as a decision tree.
Definition: iti.h:62
void setRootNode(const NodeId &root)
Sets root node of decision diagram.
void _updateNode(NodeId nody, Set< const DiscreteVariable * > &bestVars)
From the given sets of node, selects randomly one and installs it on given node.
class LabelizedVariable
const iterator_safe & endSafe() noexcept
Returns the safe iterator pointing to the end of the hashtable.
<agrum/FMDP/learning/datastructure/incrementalGraphLearner>
void erase(const Key &key)
Removes a given element from the hash table.
virtual NodeId _insertNode(NodeDatabase< AttributeSelection, isScalar > *nDB, const DiscreteVariable *boundVar)
inserts a new node in internal graph
HashTable< NodeId, bool > __staleTable
Hashtable indicating if given node has been modified (upon receiving new exemple or through a transpo...
Definition: iti.h:256
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void updateGraph()
Updates the internal graph after a new observation has been added.
Definition: iti_tpl.h:143
NodeId addInternalNode(const DiscreteVariable *var)
Inserts a new non terminal node in graph.
void erase(const Key &k)
Erases an element from the set.
Definition: set_tpl.h:656
Base class for discrete random variable.
Safe Iterators for hashtables.
Definition: hashTable.h:2220
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
MultiDimFunctionGraph< double > * _target
The final diagram we&#39;re building.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
The class for generic Hash Tables.
Definition: hashTable.h:679
Idx __nbTotalObservation
The total number of observation added to this tree.
Definition: iti.h:259
void _updateNodeWithObservation(const Observation *newObs, NodeId currentNodeId)
Will update internal graph&#39;s NodeDatabase of given node with the new observation. ...
Definition: iti_tpl.h:126
Representation of a setA Set is a structure that contains arbitrary elements.
Definition: set.h:165
ITI(MultiDimFunctionGraph< double > *target, double attributeSelectionThreshold, Set< const DiscreteVariable * > attributeListe, const DiscreteVariable *learnedValue)
ITI constructor for functions describing the behaviour of one variable according to a set of other va...
Definition: iti_tpl.h:61
virtual Size domainSize() const =0
virtual void add(const DiscreteVariable &v)
Adds a new var to the variables of the multidimensional matrix.
virtual void addObservation(const Observation *obs)
Inserts a new observation.
const const_iterator_safe & cendSafe() const noexcept
Returns the safe const_iterator pointing to the end of the hashtable.
void _removeNode(NodeId removedNodeId)
Removes a node from the internal graph.
Definition: iti_tpl.h:246
virtual void _removeNode(NodeId removedNodeId)
Removes a node from the internal graph.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const override
Returns a const ref to the sequence of DiscreteVariable*.
virtual void _chgNodeBoundVar(NodeId chgedNodeId, const DiscreteVariable *desiredVar)
Changes the associated variable of a node.
iterator_safe beginSafe()
Returns the safe iterator pointing to the beginning of the hashtable.
NodeId __insertTerminalNode(NodeId src)
Insert a terminal node in the target.
Definition: iti.h:208
NodeId _root
The root of the ordered tree.
void _chgNodeBoundVar(NodeId chgedNodeId, const DiscreteVariable *desiredVar)
Changes the associated variable of a node.
Definition: iti_tpl.h:229
NodeId addTerminalNode(const GUM_SCALAR &value)
Adds a value to the MultiDimFunctionGraph.
Size Idx
Type for indexes.
Definition: types.h:53
void clear()
Removes all the elements, if any, from the set.
Definition: set_tpl.h:375
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
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
HashTable< NodeId, const DiscreteVariable *> _nodeVarMap
Gives for any node its associated variable.
void addObservation(const Observation *obs)
Inserts a new observation.
Definition: iti_tpl.h:112
Size NodeId
Type for node ids.
Definition: graphElements.h:98
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
void clear()
Clears the function graph.
<agrum/FMDP/learning/datastructure/nodeDatabase.h>
Definition: nodeDatabase.h:58
#define SOA_ALLOCATE(x)