aGrUM  0.14.2
incrementalGraphLearner.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_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  ***************************************************************************/
28 // =========================================================================
29 #ifndef GUM_INCREMENTAL_GRAPH_LEARNER_H
30 #define GUM_INCREMENTAL_GRAPH_LEARNER_H
31 // =========================================================================
32 // =========================================================================
34 // =========================================================================
38 // =========================================================================
40 // =========================================================================
41 
42 namespace gum {
43 
62  template < TESTNAME AttributeSelection, bool isScalar = false >
65 
66  public:
67  // ###################################################################
69  // ###################################################################
71 
72  // ==========================================================================
83  // ==========================================================================
85  Set< const DiscreteVariable* > attributesSet,
86  const DiscreteVariable* learnVariable);
87 
88  // ==========================================================================
90  // ==========================================================================
91  virtual ~IncrementalGraphLearner();
92 
93  private:
94  // ==========================================================================
96  // ==========================================================================
98 
99  // ==========================================================================
102  // ==========================================================================
104 
105  // ==========================================================================
108  // ==========================================================================
110 
112 
113 
114  // ###################################################################
116  // ###################################################################
118  public:
119  // ==========================================================================
123  // ==========================================================================
124  virtual void addObservation(const Observation* obs);
125 
126  private:
127  // ==========================================================================
131  // ==========================================================================
132  void __assumeValue(const Observation* obs) {
134  }
136  if (!_valueAssumed.exists(obs->reward())) _valueAssumed << obs->reward();
137  }
139  if (!_valueAssumed.exists(obs->modality(_value)))
140  _valueAssumed << obs->modality(_value);
141  }
142 
143 
144  // ==========================================================================
148  // ==========================================================================
149  Idx __branchObs(const Observation* obs, const DiscreteVariable* var) {
150  return __branchObs(obs, var, Int2Type< isScalar >());
151  }
153  const DiscreteVariable* var,
155  return obs->rModality(var);
156  }
158  const DiscreteVariable* var,
160  return obs->modality(var);
161  }
162 
163  protected:
164  // ==========================================================================
171  // ==========================================================================
172  virtual void _updateNodeWithObservation(const Observation* newObs,
173  NodeId currentNodeId) {
174  _nodeId2Database[currentNodeId]->addObservation(newObs);
175  }
176 
178 
179  // ###################################################################
181  // ###################################################################
183 
184  public:
185  // ==========================================================================
190  // ==========================================================================
191  virtual void updateVar(const DiscreteVariable*);
192 
193  // ==========================================================================
195  // ==========================================================================
196  virtual void updateGraph() = 0;
197 
198  protected:
199  // ==========================================================================
208  // ==========================================================================
209  void _updateNode(NodeId nody, Set< const DiscreteVariable* >& bestVars);
210 
211  // ==========================================================================
213  // ==========================================================================
214  virtual void _convertNode2Leaf(NodeId);
215 
216  // ==========================================================================
219  // ==========================================================================
220  virtual void _transpose(NodeId, const DiscreteVariable*);
221 
222  // ==========================================================================
229  // ==========================================================================
231  const DiscreteVariable* boundVar);
232 
233  // ==========================================================================
241  // ==========================================================================
242  virtual NodeId
244  const DiscreteVariable* boundVar,
245  NodeId* sonsMap);
246 
247  // ==========================================================================
255  // ==========================================================================
256  virtual NodeId
258  const DiscreteVariable* boundVar,
259  Set< const Observation* >* obsSet);
260 
261  // ==========================================================================
267  // ==========================================================================
268  virtual void _chgNodeBoundVar(NodeId chgedNodeId,
269  const DiscreteVariable* desiredVar);
270 
271  // ==========================================================================
276  // ==========================================================================
277  virtual void _removeNode(NodeId removedNodeId);
278 
280 
281 
282  // ###################################################################
284  // ###################################################################
286  public:
287  // ==========================================================================
289  // ==========================================================================
290  virtual void updateFunctionGraph() = 0;
291 
293 
294 
295  public:
296  // ==========================================================================
298  // ==========================================================================
299  Size size() { return _nodeVarMap.size(); }
300 
301 
302  // ###################################################################
304  // ###################################################################
306  public:
307  // ==========================================================================
309  // ==========================================================================
310  NodeId root() const { return this->_root; }
311 
312  // ==========================================================================
314  // ==========================================================================
315  bool isTerminal(NodeId ni) const { return !this->_nodeSonsMap.exists(ni); }
316 
317  // ==========================================================================
319  // ==========================================================================
320  const DiscreteVariable* nodeVar(NodeId ni) const {
321  return this->_nodeVarMap[ni];
322  }
323 
324  // ==========================================================================
326  // ==========================================================================
327  NodeId nodeSon(NodeId ni, Idx modality) const {
328  return this->_nodeSonsMap[ni][modality];
329  }
330 
331  // ==========================================================================
333  // ==========================================================================
335  return this->_nodeId2Database[ni]->nbObservation();
336  }
337 
338  // ==========================================================================
340  // ==========================================================================
343  _setOfVars.beginSafe();
344  varIter != _setOfVars.endSafe();
345  ++varIter)
346  ret->add(**varIter);
347  }
349 
350  protected:
352 
353  // ###################################################################
355  // ###################################################################
357 
358  // ==========================================================================
360  // ==========================================================================
362 
363  // ==========================================================================
365  // ==========================================================================
367 
368  // ==========================================================================
370  // ==========================================================================
372 
373  // ==========================================================================
376  // ==========================================================================
378 
379  // ==========================================================================
382  // ==========================================================================
384 
385  // ==========================================================================
388  // ==========================================================================
391 
392  // ==========================================================================
395  // ==========================================================================
397 
399 
400 
403 
405 
408 
410  };
411 
412 
413 } /* namespace gum */
414 
416 
417 #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.
Template trick for efficient development.
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
Headers of the Learning Strategy interface.
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:808
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:1019
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.
gum is the global namespace for all aGrUM entities
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:676
Representation of a setA Set is a structure that contains arbitrary elements.
Definition: set.h:162
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:116
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.
Headers of the NodeDatabase class.
INLINE Idx rModality(const DiscreteVariable *var) const
Returns the modality assumed by the given variable in this observation.
Definition: observation.h:94
Class for node sets in graph.
Headers of MultiDimFunctionGraph.
INLINE Idx modality(const DiscreteVariable *var) const
Returns the modality assumed by the given variable in this observation.
Definition: observation.h:91
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:50
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:45
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:97
ValueSelect< isScalar, double, Idx >::type ValueType
<agrum/FMDP/learning/datastructure/nodeDatabase.h>
Definition: nodeDatabase.h:55