aGrUM  0.16.0
incrementalGraphLearner.h
Go to the documentation of this file.
1 
31 // =========================================================================
32 #ifndef GUM_INCREMENTAL_GRAPH_LEARNER_H
33 #define GUM_INCREMENTAL_GRAPH_LEARNER_H
34 // =========================================================================
35 // =========================================================================
37 // =========================================================================
41 // =========================================================================
43 // =========================================================================
44 
45 namespace gum {
46 
65  template < TESTNAME AttributeSelection, bool isScalar = false >
68 
69  public:
70  // ###################################################################
72  // ###################################################################
74 
75  // ==========================================================================
86  // ==========================================================================
88  Set< const DiscreteVariable* > attributesSet,
89  const DiscreteVariable* learnVariable);
90 
91  // ==========================================================================
93  // ==========================================================================
94  virtual ~IncrementalGraphLearner();
95 
96  private:
97  // ==========================================================================
99  // ==========================================================================
101 
102  // ==========================================================================
105  // ==========================================================================
107 
108  // ==========================================================================
111  // ==========================================================================
113 
115 
116 
117  // ###################################################################
119  // ###################################################################
121  public:
122  // ==========================================================================
126  // ==========================================================================
127  virtual void addObservation(const Observation* obs);
128 
129  private:
130  // ==========================================================================
134  // ==========================================================================
135  void __assumeValue(const Observation* obs) {
137  }
139  if (!_valueAssumed.exists(obs->reward())) _valueAssumed << obs->reward();
140  }
142  if (!_valueAssumed.exists(obs->modality(_value)))
143  _valueAssumed << obs->modality(_value);
144  }
145 
146 
147  // ==========================================================================
151  // ==========================================================================
152  Idx __branchObs(const Observation* obs, const DiscreteVariable* var) {
153  return __branchObs(obs, var, Int2Type< isScalar >());
154  }
156  const DiscreteVariable* var,
158  return obs->rModality(var);
159  }
161  const DiscreteVariable* var,
163  return obs->modality(var);
164  }
165 
166  protected:
167  // ==========================================================================
174  // ==========================================================================
175  virtual void _updateNodeWithObservation(const Observation* newObs,
176  NodeId currentNodeId) {
177  _nodeId2Database[currentNodeId]->addObservation(newObs);
178  }
179 
181 
182  // ###################################################################
184  // ###################################################################
186 
187  public:
188  // ==========================================================================
193  // ==========================================================================
194  virtual void updateVar(const DiscreteVariable*);
195 
196  // ==========================================================================
198  // ==========================================================================
199  virtual void updateGraph() = 0;
200 
201  protected:
202  // ==========================================================================
211  // ==========================================================================
212  void _updateNode(NodeId nody, Set< const DiscreteVariable* >& bestVars);
213 
214  // ==========================================================================
216  // ==========================================================================
217  virtual void _convertNode2Leaf(NodeId);
218 
219  // ==========================================================================
222  // ==========================================================================
223  virtual void _transpose(NodeId, const DiscreteVariable*);
224 
225  // ==========================================================================
232  // ==========================================================================
234  const DiscreteVariable* boundVar);
235 
236  // ==========================================================================
244  // ==========================================================================
245  virtual NodeId
247  const DiscreteVariable* boundVar,
248  NodeId* sonsMap);
249 
250  // ==========================================================================
258  // ==========================================================================
259  virtual NodeId
261  const DiscreteVariable* boundVar,
262  Set< const Observation* >* obsSet);
263 
264  // ==========================================================================
270  // ==========================================================================
271  virtual void _chgNodeBoundVar(NodeId chgedNodeId,
272  const DiscreteVariable* desiredVar);
273 
274  // ==========================================================================
279  // ==========================================================================
280  virtual void _removeNode(NodeId removedNodeId);
281 
283 
284 
285  // ###################################################################
287  // ###################################################################
289  public:
290  // ==========================================================================
292  // ==========================================================================
293  virtual void updateFunctionGraph() = 0;
294 
296 
297 
298  public:
299  // ==========================================================================
301  // ==========================================================================
302  Size size() { return _nodeVarMap.size(); }
303 
304 
305  // ###################################################################
307  // ###################################################################
309  public:
310  // ==========================================================================
312  // ==========================================================================
313  NodeId root() const { return this->_root; }
314 
315  // ==========================================================================
317  // ==========================================================================
318  bool isTerminal(NodeId ni) const { return !this->_nodeSonsMap.exists(ni); }
319 
320  // ==========================================================================
322  // ==========================================================================
323  const DiscreteVariable* nodeVar(NodeId ni) const {
324  return this->_nodeVarMap[ni];
325  }
326 
327  // ==========================================================================
329  // ==========================================================================
330  NodeId nodeSon(NodeId ni, Idx modality) const {
331  return this->_nodeSonsMap[ni][modality];
332  }
333 
334  // ==========================================================================
336  // ==========================================================================
338  return this->_nodeId2Database[ni]->nbObservation();
339  }
340 
341  // ==========================================================================
343  // ==========================================================================
346  _setOfVars.beginSafe();
347  varIter != _setOfVars.endSafe();
348  ++varIter)
349  ret->add(**varIter);
350  }
352 
353  protected:
355 
356  // ###################################################################
358  // ###################################################################
360 
361  // ==========================================================================
363  // ==========================================================================
365 
366  // ==========================================================================
368  // ==========================================================================
370 
371  // ==========================================================================
373  // ==========================================================================
375 
376  // ==========================================================================
379  // ==========================================================================
381 
382  // ==========================================================================
385  // ==========================================================================
387 
388  // ==========================================================================
391  // ==========================================================================
394 
395  // ==========================================================================
398  // ==========================================================================
400 
402 
403 
406 
408 
411 
413  };
414 
415 
416 } /* namespace gum */
417 
419 
420 #endif // GUM_INCREMENTAL_GRAPH_LEARNER_H
void __assumeValue(const Observation *obs)
Get value assumed by studied variable for current observation.
virtual void updateFunctionGraph()=0
Updates target to currently learned graph structure.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
virtual void _updateNodeWithObservation(const Observation *newObs, NodeId currentNodeId)
Will update internal graph&#39;s NodeDatabase of given node with the new observation. ...
HashTable< NodeId, NodeId *> _nodeSonsMap
A table giving for any node a table mapping to its son idx is the modality of associated variable...
HashTable< const DiscreteVariable *, LinkedList< NodeId > *> _var2Node
Associates to any variable the list of all nodes associated to this variable.
IncrementalGraphLearner(MultiDimFunctionGraph< double > *target, Set< const DiscreteVariable * > attributesSet, const DiscreteVariable *learnVariable)
Default constructor.
Set< const DiscreteVariable *> _setOfVars
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
HashTable< NodeId, NodeDatabase< AttributeSelection, isScalar > *> _nodeId2Database
This hashtable binds every node to an associated NodeDatabase which handles every observation that co...
void _updateNode(NodeId nody, Set< const DiscreteVariable * > &bestVars)
From the given sets of node, selects randomly one and installs it on given node.
Safe iterators for the Set classDevelopers may consider using Set<x>::iterator_safe instead of SetIte...
Definition: set.h:811
virtual NodeId _insertInternalNode(NodeDatabase< AttributeSelection, isScalar > *nDB, const DiscreteVariable *boundVar, NodeId *sonsMap)
inserts a new internal node in internal graph
Idx __branchObs(const Observation *obs, const DiscreteVariable *var)
Seek modality assumed in obs for given var.
<agrum/FMDP/learning/datastructure/incrementalGraphLearner>
virtual ~IncrementalGraphLearner()
Default destructor.
virtual NodeId _insertNode(NodeDatabase< AttributeSelection, isScalar > *nDB, const DiscreteVariable *boundVar)
inserts a new node in internal graph
The generic class for storing (ordered) sequences of objects.
Definition: sequence.h:1022
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
virtual void _convertNode2Leaf(NodeId)
Turns the given node into a leaf if not already so.
<agrum/FMDP/SDyna/IVisitableGraphLearner.h>
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
Base class for discrete random variable.
void __clearValue()
Template function dispatcher.
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.
void __clearValue(Int2Type< false >)
In case where we&#39;re learning function of variable behaviour, this should do nothing.
Idx __branchObs(const Observation *obs, const DiscreteVariable *var, Int2Type< false >)
Inserts a new observation.
void __assumeValue(const Observation *obs, Int2Type< true >)
Inserts a new observation.
virtual NodeId _insertLeafNode(NodeDatabase< AttributeSelection, isScalar > *nDB, const DiscreteVariable *boundVar, Set< const Observation * > *obsSet)
inserts a new leaf node in internal graohs
The class for generic Hash Tables.
Definition: hashTable.h:679
Representation of a setA Set is a structure that contains arbitrary elements.
Definition: set.h:165
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.
HashTable< NodeId, Set< const Observation *> *> _leafDatabase
This hashtable binds to every leaf an associated set of all hte observations compatible with it...
Idx __branchObs(const Observation *obs, const DiscreteVariable *var, Int2Type< true >)
Inserts a new observation.
virtual void _transpose(NodeId, const DiscreteVariable *)
Installs given variable to the given node, ensuring that the variable is not present in its subtree...
double reward() const
Returns the modality assumed by the given variable in this observation.
Definition: observation.h:119
virtual void updateGraph()=0
Updates the tree after a new observation has been added.
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.
INLINE Idx rModality(const DiscreteVariable *var) const
Returns the modality assumed by the given variable in this observation.
Definition: observation.h:97
Class for node sets in graph.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
INLINE Idx modality(const DiscreteVariable *var) const
Returns the modality assumed by the given variable in this observation.
Definition: observation.h:94
void __clearValue(Int2Type< true >)
In the case where we&#39;re learning a function of real values this has to be wiped out upon destruction ...
const DiscreteVariable * nodeVar(NodeId ni) const
virtual void insertSetOfVars(MultiDimFunctionGraph< double > *ret) const
virtual void _chgNodeBoundVar(NodeId chgedNodeId, const DiscreteVariable *desiredVar)
Changes the associated variable of a node.
void __assumeValue(const Observation *obs, Int2Type< false >)
Inserts a new observation.
NodeId _root
The root of the ordered tree.
NodeId nodeSon(NodeId ni, Idx modality) const
Size Idx
Type for indexes.
Definition: types.h:53
virtual void updateVar(const DiscreteVariable *)
If a new modality appears to exists for given variable, call this method to turn every associated nod...
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
NodeGraphPart _model
The source of nodeId.
HashTable< NodeId, const DiscreteVariable *> _nodeVarMap
Gives for any node its associated variable.
Size NodeId
Type for node ids.
Definition: graphElements.h:98
ValueSelect< isScalar, double, Idx >::type ValueType
<agrum/FMDP/learning/datastructure/nodeDatabase.h>
Definition: nodeDatabase.h:58