aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
gum::IncrementalGraphLearner< AttributeSelection, isScalar > Class Template Referenceabstract

<agrum/FMDP/learning/datastructure/incrementalGraphLearner> More...

#include <incrementalGraphLearner.h>

+ Inheritance diagram for gum::IncrementalGraphLearner< AttributeSelection, isScalar >:
+ Collaboration diagram for gum::IncrementalGraphLearner< AttributeSelection, isScalar >:

Public Member Functions

Size size ()
 
Function Graph Updating methods
virtual void updateFunctionGraph ()=0
 Updates target to currently learned graph structure. More...
 
Visit Methods
NodeId root () const
 
bool isTerminal (NodeId ni) const
 
const DiscreteVariablenodeVar (NodeId ni) const
 
NodeId nodeSon (NodeId ni, Idx modality) const
 
Idx nodeNbObservation (NodeId ni) const
 
virtual void insertSetOfVars (MultiDimFunctionGraph< double > *ret) const
 

Protected Attributes

MultiDimFunctionGraph< double > * target_
 The final diagram we're building. More...
 
Set< const DiscreteVariable *> setOfVars_
 
const DiscreteVariablevalue_
 
Sequence< ValueTypevalueAssumed_
 
bool needUpdate_
 
Model handling datastructures
NodeGraphPart model_
 The source of nodeId. More...
 
NodeId root_
 The root of the ordered tree. More...
 
HashTable< NodeId, const DiscreteVariable *> nodeVarMap_
 Gives for any node its associated variable. More...
 
HashTable< NodeId, NodeId *> nodeSonsMap_
 A table giving for any node a table mapping to its son idx is the modality of associated variable. More...
 
HashTable< const DiscreteVariable *, LinkedList< NodeId > *> var2Node_
 Associates to any variable the list of all nodes associated to this variable. More...
 
HashTable< NodeId, NodeDatabase< AttributeSelection, isScalar > *> nodeId2Database_
 This hashtable binds every node to an associated NodeDatabase which handles every observation that concerns that node. More...
 
HashTable< NodeId, Set< const Observation *> *> leafDatabase_
 This hashtable binds to every leaf an associated set of all hte observations compatible with it. More...
 

Constructor & destructor.

 IncrementalGraphLearner (MultiDimFunctionGraph< double > *target, Set< const DiscreteVariable * > attributesSet, const DiscreteVariable *learnVariable)
 Default constructor. More...
 
virtual ~IncrementalGraphLearner ()
 Default destructor. More...
 
void _clearValue_ ()
 Template function dispatcher. More...
 
void _clearValue_ (Int2Type< true >)
 In the case where we're learning a function of real values this has to be wiped out upon destruction (to be deprecated) More...
 
void _clearValue_ (Int2Type< false >)
 In case where we're learning function of variable behaviour, this should do nothing. More...
 

New Observation insertion methods

virtual void addObservation (const Observation *obs)
 Inserts a new observation. More...
 
void _assumeValue_ (const Observation *obs)
 Get value assumed by studied variable for current observation. More...
 
void _assumeValue_ (const Observation *obs, Int2Type< true >)
 Inserts a new observation. More...
 
void _assumeValue_ (const Observation *obs, Int2Type< false >)
 Inserts a new observation. More...
 
Idx _branchObs_ (const Observation *obs, const DiscreteVariable *var)
 Seek modality assumed in obs for given var. More...
 
Idx _branchObs_ (const Observation *obs, const DiscreteVariable *var, Int2Type< true >)
 Inserts a new observation. More...
 
Idx _branchObs_ (const Observation *obs, const DiscreteVariable *var, Int2Type< false >)
 Inserts a new observation. More...
 
virtual void updateNodeWithObservation_ (const Observation *newObs, NodeId currentNodeId)
 Will update internal graph's NodeDatabase of given node with the new observation. More...
 

Graph Structure update methods

virtual void updateVar (const DiscreteVariable *)
 If a new modality appears to exists for given variable, call this method to turn every associated node to this variable into leaf. Graph has then indeed to be revised. More...
 
virtual void updateGraph ()=0
 Updates the tree after a new observation has been added. More...
 
void updateNode_ (NodeId nody, Set< const DiscreteVariable * > &bestVars)
 From the given sets of node, selects randomly one and installs it on given node. More...
 
virtual void convertNode2Leaf_ (NodeId)
 Turns the given node into a leaf if not already so. More...
 
virtual void transpose_ (NodeId, const DiscreteVariable *)
 Installs given variable to the given node, ensuring that the variable is not present in its subtree. More...
 
virtual NodeId insertNode_ (NodeDatabase< AttributeSelection, isScalar > *nDB, const DiscreteVariable *boundVar)
 inserts a new node in internal graph More...
 
virtual NodeId insertInternalNode_ (NodeDatabase< AttributeSelection, isScalar > *nDB, const DiscreteVariable *boundVar, NodeId *sonsMap)
 inserts a new internal node in internal graph More...
 
virtual NodeId insertLeafNode_ (NodeDatabase< AttributeSelection, isScalar > *nDB, const DiscreteVariable *boundVar, Set< const Observation * > *obsSet)
 inserts a new leaf node in internal graohs More...
 
virtual void chgNodeBoundVar_ (NodeId chgedNodeId, const DiscreteVariable *desiredVar)
 Changes the associated variable of a node. More...
 
virtual void removeNode_ (NodeId removedNodeId)
 Removes a node from the internal graph. More...
 

Detailed Description

template<TESTNAME AttributeSelection, bool isScalar = false>
class gum::IncrementalGraphLearner< AttributeSelection, isScalar >

<agrum/FMDP/learning/datastructure/incrementalGraphLearner>

Abstract class for incrementaly learn a graphical representation of a function. Can handle both function of real values, and function explaining the behaviour of a variable given set of other variables (as typically in conditionnal probabilities)

Maintains two graph in memory, one which is incrementaly updated and the other one which is updated on demand and is usable by the outside.

Definition at line 65 of file incrementalGraphLearner.h.

Member Typedef Documentation

◆ ValueType

template<TESTNAME AttributeSelection, bool isScalar = false>
typedef ValueSelect< isScalar, double, Idx >::type gum::IncrementalGraphLearner< AttributeSelection, isScalar >::ValueType
private

Definition at line 66 of file incrementalGraphLearner.h.

Constructor & Destructor Documentation

◆ IncrementalGraphLearner()

template<TESTNAME AttributeSelection, bool isScalar>
gum::IncrementalGraphLearner< AttributeSelection, isScalar >::IncrementalGraphLearner ( MultiDimFunctionGraph< double > *  target,
Set< const DiscreteVariable * >  varList,
const DiscreteVariable value 
)

Default constructor.

Parameters
target: the output diagram usable by the outside
attributesSet: set of variables from which we try to describe the learned function
learnVariable: if we tried to learn a the behaviour of a variable given variable given another set of variables, this is the one. If we are learning a function of real value, this is just a computationnal trick (and is to be deprecated)

Definition at line 61 of file incrementalGraphLearner_tpl.h.

References gum::Set< Key, Alloc >::emplace().

64  :
65  target_(target),
66  setOfVars_(varList), value_(value) {
67  GUM_CONSTRUCTOR(IncrementalGraphLearner);
68 
69  for (auto varIter = setOfVars_.cbeginSafe(); varIter != setOfVars_.cendSafe(); ++varIter)
70  var2Node_.insert(*varIter, new LinkedList< NodeId >());
71  var2Node_.insert(value_, new LinkedList< NodeId >());
72 
73  model_.addNode();
74  this->root_
75  = insertLeafNode_(new NodeDatabase< AttributeSelection, isScalar >(&setOfVars_, value_),
76  value_,
77  new Set< const Observation* >());
78  }
IncrementalGraphLearner(MultiDimFunctionGraph< double > *target, Set< const DiscreteVariable * > attributesSet, const DiscreteVariable *learnVariable)
Default constructor.
MultiDimFunctionGraph< double > * target_
The final diagram we&#39;re building.
NodeId root_
The root of the ordered tree.
HashTable< const DiscreteVariable *, LinkedList< NodeId > *> var2Node_
Associates to any variable the list of all nodes associated to this variable.
virtual NodeId addNode()
insert a new node and return its id
Set< const DiscreteVariable *> setOfVars_
NodeGraphPart model_
The source of nodeId.
virtual NodeId insertLeafNode_(NodeDatabase< AttributeSelection, isScalar > *nDB, const DiscreteVariable *boundVar, Set< const Observation * > *obsSet)
inserts a new leaf node in internal graohs
+ Here is the call graph for this function:

◆ ~IncrementalGraphLearner()

template<TESTNAME AttributeSelection, bool isScalar>
gum::IncrementalGraphLearner< AttributeSelection, isScalar >::~IncrementalGraphLearner ( )
virtual

Default destructor.

Definition at line 85 of file incrementalGraphLearner_tpl.h.

References gum::Set< Key, Alloc >::emplace().

85  {
86  for (auto nodeIter = nodeId2Database_.beginSafe(); nodeIter != nodeId2Database_.endSafe();
87  ++nodeIter)
88  delete nodeIter.val();
89 
90  for (auto nodeIter = nodeSonsMap_.beginSafe(); nodeIter != nodeSonsMap_.endSafe(); ++nodeIter)
91  SOA_DEALLOCATE(nodeIter.val(), sizeof(NodeId) * nodeVarMap_[nodeIter.key()]->domainSize());
92 
93  for (auto varIter = var2Node_.beginSafe(); varIter != var2Node_.endSafe(); ++varIter)
94  delete varIter.val();
95 
96  for (auto nodeIter = leafDatabase_.beginSafe(); nodeIter != leafDatabase_.endSafe(); ++nodeIter)
97  delete nodeIter.val();
98 
99  _clearValue_();
100 
101  GUM_DESTRUCTOR(IncrementalGraphLearner);
102  }
IncrementalGraphLearner(MultiDimFunctionGraph< double > *target, Set< const DiscreteVariable * > attributesSet, const DiscreteVariable *learnVariable)
Default constructor.
const iterator_safe & endSafe() noexcept
Returns the safe iterator pointing to the end of the hashtable.
void _clearValue_()
Template function dispatcher.
#define SOA_DEALLOCATE(x, y)
HashTable< const DiscreteVariable *, LinkedList< NodeId > *> var2Node_
Associates to any variable the list of all nodes associated to this variable.
HashTable< NodeId, NodeDatabase< AttributeSelection, isScalar > *> nodeId2Database_
This hashtable binds every node to an associated NodeDatabase which handles every observation that co...
HashTable< NodeId, NodeId *> nodeSonsMap_
A table giving for any node a table mapping to its son idx is the modality of associated variable...
HashTable< NodeId, Set< const Observation *> *> leafDatabase_
This hashtable binds to every leaf an associated set of all hte observations compatible with it...
iterator_safe beginSafe()
Returns the safe iterator pointing to the beginning of the hashtable.
HashTable< NodeId, const DiscreteVariable *> nodeVarMap_
Gives for any node its associated variable.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:

Member Function Documentation

◆ _assumeValue_() [1/3]

template<TESTNAME AttributeSelection, bool isScalar = false>
void gum::IncrementalGraphLearner< AttributeSelection, isScalar >::_assumeValue_ ( const Observation obs)
inlineprivate

Get value assumed by studied variable for current observation.

Definition at line 134 of file incrementalGraphLearner.h.

134 { _assumeValue_(obs, Int2Type< isScalar >()); }
void _assumeValue_(const Observation *obs)
Get value assumed by studied variable for current observation.

◆ _assumeValue_() [2/3]

template<TESTNAME AttributeSelection, bool isScalar = false>
void gum::IncrementalGraphLearner< AttributeSelection, isScalar >::_assumeValue_ ( const Observation obs,
Int2Type< true >   
)
inlineprivate

Inserts a new observation.

Parameters
thenew observation to learn

Definition at line 135 of file incrementalGraphLearner.h.

135  {
136  if (!valueAssumed_.exists(obs->reward())) valueAssumed_ << obs->reward();
137  }

◆ _assumeValue_() [3/3]

template<TESTNAME AttributeSelection, bool isScalar = false>
void gum::IncrementalGraphLearner< AttributeSelection, isScalar >::_assumeValue_ ( const Observation obs,
Int2Type< false >   
)
inlineprivate

Inserts a new observation.

Parameters
thenew observation to learn

Definition at line 138 of file incrementalGraphLearner.h.

138  {
139  if (!valueAssumed_.exists(obs->modality(value_))) valueAssumed_ << obs->modality(value_);
140  }

◆ _branchObs_() [1/3]

template<TESTNAME AttributeSelection, bool isScalar = false>
Idx gum::IncrementalGraphLearner< AttributeSelection, isScalar >::_branchObs_ ( const Observation obs,
const DiscreteVariable var 
)
inlineprivate

Seek modality assumed in obs for given var.

Definition at line 148 of file incrementalGraphLearner.h.

148  {
149  return _branchObs_(obs, var, Int2Type< isScalar >());
150  }
Idx _branchObs_(const Observation *obs, const DiscreteVariable *var)
Seek modality assumed in obs for given var.

◆ _branchObs_() [2/3]

template<TESTNAME AttributeSelection, bool isScalar = false>
Idx gum::IncrementalGraphLearner< AttributeSelection, isScalar >::_branchObs_ ( const Observation obs,
const DiscreteVariable var,
Int2Type< true >   
)
inlineprivate

Inserts a new observation.

Parameters
thenew observation to learn

Definition at line 151 of file incrementalGraphLearner.h.

151  {
152  return obs->rModality(var);
153  }

◆ _branchObs_() [3/3]

template<TESTNAME AttributeSelection, bool isScalar = false>
Idx gum::IncrementalGraphLearner< AttributeSelection, isScalar >::_branchObs_ ( const Observation obs,
const DiscreteVariable var,
Int2Type< false >   
)
inlineprivate

Inserts a new observation.

Parameters
thenew observation to learn

Definition at line 154 of file incrementalGraphLearner.h.

154  {
155  return obs->modality(var);
156  }

◆ _clearValue_() [1/3]

template<TESTNAME AttributeSelection, bool isScalar = false>
void gum::IncrementalGraphLearner< AttributeSelection, isScalar >::_clearValue_ ( )
inlineprivate

Template function dispatcher.

Definition at line 99 of file incrementalGraphLearner.h.

99 { _clearValue_(Int2Type< isScalar >()); }
void _clearValue_()
Template function dispatcher.

◆ _clearValue_() [2/3]

template<TESTNAME AttributeSelection, bool isScalar = false>
void gum::IncrementalGraphLearner< AttributeSelection, isScalar >::_clearValue_ ( Int2Type< true >  )
inlineprivate

In the case where we're learning a function of real values this has to be wiped out upon destruction (to be deprecated)

Definition at line 105 of file incrementalGraphLearner.h.

105 { delete value_; }

◆ _clearValue_() [3/3]

template<TESTNAME AttributeSelection, bool isScalar = false>
void gum::IncrementalGraphLearner< AttributeSelection, isScalar >::_clearValue_ ( Int2Type< false >  )
inlineprivate

In case where we're learning function of variable behaviour, this should do nothing.

Definition at line 111 of file incrementalGraphLearner.h.

111 {}

◆ addObservation()

template<TESTNAME AttributeSelection, bool isScalar>
void gum::IncrementalGraphLearner< AttributeSelection, isScalar >::addObservation ( const Observation newObs)
virtual

Inserts a new observation.

Parameters
thenew observation to learn

Reimplemented in gum::ITI< AttributeSelection, isScalar >, and gum::IMDDI< AttributeSelection, isScalar >.

Definition at line 116 of file incrementalGraphLearner_tpl.h.

References gum::Set< Key, Alloc >::emplace().

117  {
118  _assumeValue_(newObs);
119 
120  // The we go across the tree
121  NodeId currentNodeId = root_;
122 
123  while (nodeSonsMap_.exists(currentNodeId)) {
124  // On each encountered node, we update the database
125  updateNodeWithObservation_(newObs, currentNodeId);
126 
127  // The we select the next to go throught
128  currentNodeId = nodeSonsMap_[currentNodeId][_branchObs_(newObs, nodeVarMap_[currentNodeId])];
129  }
130 
131  // On final insertion into the leave we reach
132  updateNodeWithObservation_(newObs, currentNodeId);
133  leafDatabase_[currentNodeId]->insert(newObs);
134  }
NodeId root_
The root of the ordered tree.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
Idx _branchObs_(const Observation *obs, const DiscreteVariable *var)
Seek modality assumed in obs for given var.
HashTable< NodeId, NodeId *> nodeSonsMap_
A table giving for any node a table mapping to its son idx is the modality of associated variable...
HashTable< NodeId, Set< const Observation *> *> leafDatabase_
This hashtable binds to every leaf an associated set of all hte observations compatible with it...
void _assumeValue_(const Observation *obs)
Get value assumed by studied variable for current observation.
virtual void updateNodeWithObservation_(const Observation *newObs, NodeId currentNodeId)
Will update internal graph&#39;s NodeDatabase of given node with the new observation. ...
HashTable< NodeId, const DiscreteVariable *> nodeVarMap_
Gives for any node its associated variable.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:

◆ chgNodeBoundVar_()

template<TESTNAME AttributeSelection, bool isScalar>
void gum::IncrementalGraphLearner< AttributeSelection, isScalar >::chgNodeBoundVar_ ( NodeId  currentNodeId,
const DiscreteVariable desiredVar 
)
protectedvirtual

Changes the associated variable of a node.

Parameters
chgedNodeId: the node to change
desiredVar: its new associated variable

Reimplemented in gum::ITI< AttributeSelection, isScalar >, and gum::IMDDI< AttributeSelection, isScalar >.

Definition at line 400 of file incrementalGraphLearner_tpl.h.

References gum::Set< Key, Alloc >::emplace().

402  {
403  if (nodeVarMap_[currentNodeId] == desiredVar) return;
404 
405  var2Node_[nodeVarMap_[currentNodeId]]->searchAndRemoveLink(currentNodeId);
406  var2Node_[desiredVar]->addLink(currentNodeId);
407  nodeVarMap_[currentNodeId] = desiredVar;
408 
409  if (nodeVarMap_[currentNodeId] != value_ && leafDatabase_.exists(currentNodeId)) {
410  delete leafDatabase_[currentNodeId];
411  leafDatabase_.erase(currentNodeId);
412  }
413 
414  if (nodeVarMap_[currentNodeId] == value_ && !leafDatabase_.exists(currentNodeId)) {
415  leafDatabase_.insert(currentNodeId, new Set< const Observation* >());
416  }
417 
418  needUpdate_ = true;
419  }
HashTable< const DiscreteVariable *, LinkedList< NodeId > *> var2Node_
Associates to any variable the list of all nodes associated to this variable.
HashTable< NodeId, Set< const Observation *> *> leafDatabase_
This hashtable binds to every leaf an associated set of all hte observations compatible with it...
HashTable< NodeId, const DiscreteVariable *> nodeVarMap_
Gives for any node its associated variable.
+ Here is the call graph for this function:

◆ convertNode2Leaf_()

template<TESTNAME AttributeSelection, bool isScalar>
void gum::IncrementalGraphLearner< AttributeSelection, isScalar >::convertNode2Leaf_ ( NodeId  currentNodeId)
protectedvirtual

Turns the given node into a leaf if not already so.

Definition at line 200 of file incrementalGraphLearner_tpl.h.

References gum::Set< Key, Alloc >::emplace().

201  {
202  if (nodeVarMap_[currentNodeId] != value_) {
203  leafDatabase_.insert(currentNodeId, new Set< const Observation* >());
204 
205  // Resolving potential sons issue
206  for (Idx modality = 0; modality < nodeVarMap_[currentNodeId]->domainSize(); ++modality) {
207  NodeId sonId = nodeSonsMap_[currentNodeId][modality];
208  convertNode2Leaf_(sonId);
209  (*leafDatabase_[currentNodeId]) = (*leafDatabase_[currentNodeId]) + *(leafDatabase_[sonId]);
210  removeNode_(sonId);
211  }
212 
213  SOA_DEALLOCATE(nodeSonsMap_[currentNodeId],
214  sizeof(NodeId) * nodeVarMap_[currentNodeId]->domainSize());
215  nodeSonsMap_.erase(currentNodeId);
216 
217  chgNodeBoundVar_(currentNodeId, value_);
218  }
219  }
virtual void convertNode2Leaf_(NodeId)
Turns the given node into a leaf if not already so.
void erase(const Key &key)
Removes a given element from the hash table.
#define SOA_DEALLOCATE(x, y)
virtual void removeNode_(NodeId removedNodeId)
Removes a node from the internal graph.
virtual void chgNodeBoundVar_(NodeId chgedNodeId, const DiscreteVariable *desiredVar)
Changes the associated variable of a node.
HashTable< NodeId, NodeId *> nodeSonsMap_
A table giving for any node a table mapping to its son idx is the modality of associated variable...
HashTable< NodeId, Set< const Observation *> *> leafDatabase_
This hashtable binds to every leaf an associated set of all hte observations compatible with it...
HashTable< NodeId, const DiscreteVariable *> nodeVarMap_
Gives for any node its associated variable.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:

◆ insertInternalNode_()

template<TESTNAME AttributeSelection, bool isScalar>
NodeId gum::IncrementalGraphLearner< AttributeSelection, isScalar >::insertInternalNode_ ( NodeDatabase< AttributeSelection, isScalar > *  nDB,
const DiscreteVariable boundVar,
NodeId sonsMap 
)
protectedvirtual

inserts a new internal node in internal graph

Parameters
nDB: the associated database
boundVar: the associated variable
sonsMap: a table giving node's sons node
Returns
the newly created node's id

Definition at line 362 of file incrementalGraphLearner_tpl.h.

References gum::Set< Key, Alloc >::emplace().

365  {
366  NodeId newNodeId = this->insertNode_(nDB, boundVar);
367  nodeSonsMap_.insert(newNodeId, sonsMap);
368  return newNodeId;
369  }
virtual NodeId insertNode_(NodeDatabase< AttributeSelection, isScalar > *nDB, const DiscreteVariable *boundVar)
inserts a new node in internal graph
HashTable< NodeId, NodeId *> nodeSonsMap_
A table giving for any node a table mapping to its son idx is the modality of associated variable...
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:

◆ insertLeafNode_()

template<TESTNAME AttributeSelection, bool isScalar>
NodeId gum::IncrementalGraphLearner< AttributeSelection, isScalar >::insertLeafNode_ ( NodeDatabase< AttributeSelection, isScalar > *  nDB,
const DiscreteVariable boundVar,
Set< const Observation * > *  obsSet 
)
protectedvirtual

inserts a new leaf node in internal graohs

Parameters
nDB: the associated database
boundVar: the associated variable
obsSet: the set of observation this leaf retains
Returns
the newly created node's id

Reimplemented in gum::IMDDI< AttributeSelection, isScalar >.

Definition at line 382 of file incrementalGraphLearner_tpl.h.

References gum::Set< Key, Alloc >::emplace().

385  {
386  NodeId newNodeId = this->insertNode_(nDB, boundVar);
387  leafDatabase_.insert(newNodeId, obsSet);
388  return newNodeId;
389  }
virtual NodeId insertNode_(NodeDatabase< AttributeSelection, isScalar > *nDB, const DiscreteVariable *boundVar)
inserts a new node in internal graph
HashTable< NodeId, Set< const Observation *> *> leafDatabase_
This hashtable binds to every leaf an associated set of all hte observations compatible with it...
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:

◆ insertNode_()

template<TESTNAME AttributeSelection, bool isScalar>
NodeId gum::IncrementalGraphLearner< AttributeSelection, isScalar >::insertNode_ ( NodeDatabase< AttributeSelection, isScalar > *  nDB,
const DiscreteVariable boundVar 
)
protectedvirtual

inserts a new node in internal graph

Parameters
nDB: the associated database
boundVar: the associated variable
Returns
the newly created node's id

Reimplemented in gum::ITI< AttributeSelection, isScalar >.

Definition at line 338 of file incrementalGraphLearner_tpl.h.

References gum::Set< Key, Alloc >::emplace().

340  {
341  NodeId newNodeId = model_.addNode();
342  nodeVarMap_.insert(newNodeId, boundVar);
343  nodeId2Database_.insert(newNodeId, nDB);
344  var2Node_[boundVar]->addLink(newNodeId);
345 
346  needUpdate_ = true;
347 
348  return newNodeId;
349  }
HashTable< const DiscreteVariable *, LinkedList< NodeId > *> var2Node_
Associates to any variable the list of all nodes associated to this variable.
virtual NodeId addNode()
insert a new node and return its id
HashTable< NodeId, NodeDatabase< AttributeSelection, isScalar > *> nodeId2Database_
This hashtable binds every node to an associated NodeDatabase which handles every observation that co...
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
+ Here is the call graph for this function:

◆ insertSetOfVars()

template<TESTNAME AttributeSelection, bool isScalar = false>
virtual void gum::IncrementalGraphLearner< AttributeSelection, isScalar >::insertSetOfVars ( MultiDimFunctionGraph< double > *  ret) const
inlinevirtual

Implements gum::IVisitableGraphLearner.

Reimplemented in gum::IMDDI< AttributeSelection, isScalar >.

Definition at line 326 of file incrementalGraphLearner.h.

326  {
327  for (SetIteratorSafe< const DiscreteVariable* > varIter = setOfVars_.beginSafe();
328  varIter != setOfVars_.endSafe();
329  ++varIter)
330  ret->add(**varIter);
331  }
virtual void add(const DiscreteVariable &v)
Adds a new var to the variables of the multidimensional matrix.
Set< const DiscreteVariable *> setOfVars_

◆ isTerminal()

template<TESTNAME AttributeSelection, bool isScalar = false>
bool gum::IncrementalGraphLearner< AttributeSelection, isScalar >::isTerminal ( NodeId  ni) const
inlinevirtual

Implements gum::IVisitableGraphLearner.

Definition at line 306 of file incrementalGraphLearner.h.

306 { return !this->nodeSonsMap_.exists(ni); }
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
HashTable< NodeId, NodeId *> nodeSonsMap_
A table giving for any node a table mapping to its son idx is the modality of associated variable...

◆ nodeNbObservation()

template<TESTNAME AttributeSelection, bool isScalar = false>
Idx gum::IncrementalGraphLearner< AttributeSelection, isScalar >::nodeNbObservation ( NodeId  ni) const
inlinevirtual

Implements gum::IVisitableGraphLearner.

Definition at line 321 of file incrementalGraphLearner.h.

321 { return this->nodeId2Database_[ni]->nbObservation(); }
HashTable< NodeId, NodeDatabase< AttributeSelection, isScalar > *> nodeId2Database_
This hashtable binds every node to an associated NodeDatabase which handles every observation that co...

◆ nodeSon()

template<TESTNAME AttributeSelection, bool isScalar = false>
NodeId gum::IncrementalGraphLearner< AttributeSelection, isScalar >::nodeSon ( NodeId  ni,
Idx  modality 
) const
inlinevirtual

Implements gum::IVisitableGraphLearner.

Definition at line 316 of file incrementalGraphLearner.h.

316 { return this->nodeSonsMap_[ni][modality]; }
HashTable< NodeId, NodeId *> nodeSonsMap_
A table giving for any node a table mapping to its son idx is the modality of associated variable...

◆ nodeVar()

template<TESTNAME AttributeSelection, bool isScalar = false>
const DiscreteVariable* gum::IncrementalGraphLearner< AttributeSelection, isScalar >::nodeVar ( NodeId  ni) const
inlinevirtual

Implements gum::IVisitableGraphLearner.

Definition at line 311 of file incrementalGraphLearner.h.

311 { return this->nodeVarMap_[ni]; }
HashTable< NodeId, const DiscreteVariable *> nodeVarMap_
Gives for any node its associated variable.

◆ removeNode_()

template<TESTNAME AttributeSelection, bool isScalar>
void gum::IncrementalGraphLearner< AttributeSelection, isScalar >::removeNode_ ( NodeId  currentNodeId)
protectedvirtual

Removes a node from the internal graph.

Parameters
removedNodeId: the node to remove

Reimplemented in gum::ITI< AttributeSelection, isScalar >, and gum::IMDDI< AttributeSelection, isScalar >.

Definition at line 429 of file incrementalGraphLearner_tpl.h.

References gum::Set< Key, Alloc >::emplace().

429  {
430  // Retriat de l'id
431  model_.eraseNode(currentNodeId);
432 
433  // Retrait du vecteur fils
434  if (nodeSonsMap_.exists(currentNodeId)) {
435  SOA_DEALLOCATE(nodeSonsMap_[currentNodeId],
436  sizeof(NodeId) * nodeVarMap_[currentNodeId]->domainSize());
437  nodeSonsMap_.erase(currentNodeId);
438  }
439 
440  if (leafDatabase_.exists(currentNodeId)) {
441  delete leafDatabase_[currentNodeId];
442  leafDatabase_.erase(currentNodeId);
443  }
444 
445  // Retrait de la variable
446  var2Node_[nodeVarMap_[currentNodeId]]->searchAndRemoveLink(currentNodeId);
447  nodeVarMap_.erase(currentNodeId);
448 
449  // Retrait du NodeDatabase
450  delete nodeId2Database_[currentNodeId];
451  nodeId2Database_.erase(currentNodeId);
452 
453  needUpdate_ = true;
454  }
void erase(const Key &key)
Removes a given element from the hash table.
#define SOA_DEALLOCATE(x, y)
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
HashTable< const DiscreteVariable *, LinkedList< NodeId > *> var2Node_
Associates to any variable the list of all nodes associated to this variable.
HashTable< NodeId, NodeDatabase< AttributeSelection, isScalar > *> nodeId2Database_
This hashtable binds every node to an associated NodeDatabase which handles every observation that co...
HashTable< NodeId, NodeId *> nodeSonsMap_
A table giving for any node a table mapping to its son idx is the modality of associated variable...
NodeGraphPart model_
The source of nodeId.
HashTable< NodeId, Set< const Observation *> *> leafDatabase_
This hashtable binds to every leaf an associated set of all hte observations compatible with it...
HashTable< NodeId, const DiscreteVariable *> nodeVarMap_
Gives for any node its associated variable.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
virtual void eraseNode(const NodeId id)
erase the node with the given id
+ Here is the call graph for this function:

◆ root()

template<TESTNAME AttributeSelection, bool isScalar = false>
NodeId gum::IncrementalGraphLearner< AttributeSelection, isScalar >::root ( ) const
inlinevirtual

Implements gum::IVisitableGraphLearner.

Definition at line 301 of file incrementalGraphLearner.h.

301 { return this->root_; }
NodeId root_
The root of the ordered tree.

◆ size()

template<TESTNAME AttributeSelection, bool isScalar = false>
Size gum::IncrementalGraphLearner< AttributeSelection, isScalar >::size ( )
inline

Definition at line 290 of file incrementalGraphLearner.h.

290 { return nodeVarMap_.size(); }
HashTable< NodeId, const DiscreteVariable *> nodeVarMap_
Gives for any node its associated variable.

◆ transpose_()

template<TESTNAME AttributeSelection, bool isScalar>
void gum::IncrementalGraphLearner< AttributeSelection, isScalar >::transpose_ ( NodeId  currentNodeId,
const DiscreteVariable desiredVar 
)
protectedvirtual

Installs given variable to the given node, ensuring that the variable is not present in its subtree.

Definition at line 227 of file incrementalGraphLearner_tpl.h.

References gum::Set< Key, Alloc >::emplace().

229  {
230  // **************************************************************************************
231  // Si le noeud courant contient déjà la variable qu'on souhaite lui amener
232  // Il n'y a rien à faire
233  if (nodeVarMap_[currentNodeId] == desiredVar) { return; }
234 
235  // **************************************************************************************
236  // Si le noeud courant est terminal,
237  // Il faut artificiellement insérer un noeud liant à la variable
238  if (nodeVarMap_[currentNodeId] == value_) {
239  // We turned this leaf into an internal node.
240  // This mean that we'll need to install children leaves for each value of
241  // desiredVar
242 
243  // First We must prepare these new leaves NodeDatabases and Sets<const
244  // Observation*>
245  NodeDatabase< AttributeSelection, isScalar >** dbMap
246  = static_cast< NodeDatabase< AttributeSelection, isScalar >** >(SOA_ALLOCATE(
247  sizeof(NodeDatabase< AttributeSelection, isScalar >*) * desiredVar->domainSize()));
248  Set< const Observation* >** obsetMap = static_cast< Set< const Observation* >** >(
249  SOA_ALLOCATE(sizeof(Set< const Observation* >*) * desiredVar->domainSize()));
250  for (Idx modality = 0; modality < desiredVar->domainSize(); ++modality) {
251  dbMap[modality] = new NodeDatabase< AttributeSelection, isScalar >(&setOfVars_, value_);
252  obsetMap[modality] = new Set< const Observation* >();
253  }
254  for (SetIteratorSafe< const Observation* > obsIter
255  = leafDatabase_[currentNodeId]->beginSafe();
256  leafDatabase_[currentNodeId]->endSafe() != obsIter;
257  ++obsIter) {
258  dbMap[_branchObs_(*obsIter, desiredVar)]->addObservation(*obsIter);
259  obsetMap[_branchObs_(*obsIter, desiredVar)]->insert(*obsIter);
260  }
261 
262  // Then we can install each new leaves (and put in place the sonsMap)
263  NodeId* sonsMap
264  = static_cast< NodeId* >(SOA_ALLOCATE(sizeof(NodeId) * desiredVar->domainSize()));
265  for (Idx modality = 0; modality < desiredVar->domainSize(); ++modality)
266  sonsMap[modality] = insertLeafNode_(dbMap[modality], value_, obsetMap[modality]);
267 
268  // Some necessary clean up
269  SOA_DEALLOCATE(dbMap,
270  sizeof(NodeDatabase< AttributeSelection, isScalar >*)
271  * desiredVar->domainSize());
272  SOA_DEALLOCATE(obsetMap, sizeof(Set< const Observation* >*) * desiredVar->domainSize());
273 
274  // And finally we can turn the node into an internal node associated to
275  // desiredVar
276  chgNodeBoundVar_(currentNodeId, desiredVar);
277  nodeSonsMap_.insert(currentNodeId, sonsMap);
278 
279  return;
280  }
281 
282  // *************************************************************************************
283  // Remains the general case where currentNodeId is an internal node.
284 
285  // First we ensure that children node use desiredVar as variable
286  for (Idx modality = 0; modality < nodeVarMap_[currentNodeId]->domainSize(); ++modality)
287  transpose_(nodeSonsMap_[currentNodeId][modality], desiredVar);
288 
289  // Sequence<NodeDatabase<AttributeSelection, isScalar>*>
290  // sonsNodeDatabase =
291  // nodeId2Database_[currentNodeId]->splitOnVar(desiredVar);
292  NodeId* sonsMap
293  = static_cast< NodeId* >(SOA_ALLOCATE(sizeof(NodeId) * desiredVar->domainSize()));
294 
295  // Then we create the new mapping
296  for (Idx desiredVarModality = 0; desiredVarModality < desiredVar->domainSize();
297  ++desiredVarModality) {
298  NodeId* grandSonsMap = static_cast< NodeId* >(
299  SOA_ALLOCATE(sizeof(NodeId) * nodeVarMap_[currentNodeId]->domainSize()));
300  NodeDatabase< AttributeSelection, isScalar >* sonDB
301  = new NodeDatabase< AttributeSelection, isScalar >(&setOfVars_, value_);
302  for (Idx currentVarModality = 0;
303  currentVarModality < nodeVarMap_[currentNodeId]->domainSize();
304  ++currentVarModality) {
305  grandSonsMap[currentVarModality]
306  = nodeSonsMap_[nodeSonsMap_[currentNodeId][currentVarModality]][desiredVarModality];
307  sonDB->operator+=((*nodeId2Database_[grandSonsMap[currentVarModality]]));
308  }
309 
310  sonsMap[desiredVarModality]
311  = insertInternalNode_(sonDB, nodeVarMap_[currentNodeId], grandSonsMap);
312  }
313 
314  // Finally we clean the old remaining nodes
315  for (Idx currentVarModality = 0; currentVarModality < nodeVarMap_[currentNodeId]->domainSize();
316  ++currentVarModality) {
317  removeNode_(nodeSonsMap_[currentNodeId][currentVarModality]);
318  }
319 
320  // We suppress the old sons map and remap to the new one
321  SOA_DEALLOCATE(nodeSonsMap_[currentNodeId],
322  sizeof(NodeId) * nodeVarMap_[currentNodeId]->domainSize());
323  nodeSonsMap_[currentNodeId] = sonsMap;
324 
325  chgNodeBoundVar_(currentNodeId, desiredVar);
326  }
virtual void removeNode_(NodeId removedNodeId)
Removes a node from the internal graph.
#define SOA_DEALLOCATE(x, y)
virtual NodeId insertInternalNode_(NodeDatabase< AttributeSelection, isScalar > *nDB, const DiscreteVariable *boundVar, NodeId *sonsMap)
inserts a new internal node in internal graph
virtual void transpose_(NodeId, const DiscreteVariable *)
Installs given variable to the given node, ensuring that the variable is not present in its subtree...
Idx _branchObs_(const Observation *obs, const DiscreteVariable *var)
Seek modality assumed in obs for given var.
virtual void chgNodeBoundVar_(NodeId chgedNodeId, const DiscreteVariable *desiredVar)
Changes the associated variable of a node.
HashTable< NodeId, NodeDatabase< AttributeSelection, isScalar > *> nodeId2Database_
This hashtable binds every node to an associated NodeDatabase which handles every observation that co...
Set< const DiscreteVariable *> setOfVars_
HashTable< NodeId, NodeId *> nodeSonsMap_
A table giving for any node a table mapping to its son idx is the modality of associated variable...
HashTable< NodeId, Set< const Observation *> *> leafDatabase_
This hashtable binds to every leaf an associated set of all hte observations compatible with it...
HashTable< NodeId, const DiscreteVariable *> nodeVarMap_
Gives for any node its associated variable.
virtual NodeId insertLeafNode_(NodeDatabase< AttributeSelection, isScalar > *nDB, const DiscreteVariable *boundVar, Set< const Observation * > *obsSet)
inserts a new leaf node in internal graohs
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
#define SOA_ALLOCATE(x)
+ Here is the call graph for this function:

◆ updateFunctionGraph()

template<TESTNAME AttributeSelection, bool isScalar = false>
virtual void gum::IncrementalGraphLearner< AttributeSelection, isScalar >::updateFunctionGraph ( )
pure virtual

Updates target to currently learned graph structure.

Implemented in gum::ITI< AttributeSelection, isScalar >, and gum::IMDDI< AttributeSelection, isScalar >.

◆ updateGraph()

template<TESTNAME AttributeSelection, bool isScalar = false>
virtual void gum::IncrementalGraphLearner< AttributeSelection, isScalar >::updateGraph ( )
pure virtual

Updates the tree after a new observation has been added.

Implemented in gum::ITI< AttributeSelection, isScalar >, and gum::IMDDI< AttributeSelection, isScalar >.

◆ updateNode_()

template<TESTNAME AttributeSelection, bool isScalar>
void gum::IncrementalGraphLearner< AttributeSelection, isScalar >::updateNode_ ( NodeId  updatedNode,
Set< const DiscreteVariable * > &  varsOfInterest 
)
protected

From the given sets of node, selects randomly one and installs it on given node.

Chechks of course if node's current variable is not in that set first.

Parameters
nody: the node we update
bestVars: the set of interessting vars to be installed here

Chechks of course if node's current variable is not in that set first.

Parameters
nody: the node we update
bestVar: the set of interessting vars to be installed here

Definition at line 169 of file incrementalGraphLearner_tpl.h.

References gum::Set< Key, Alloc >::emplace().

171  {
172  // If this node has no interesting variable, we turn it into a leaf
173  if (varsOfInterest.empty()) {
174  convertNode2Leaf_(updatedNode);
175  return;
176  }
177 
178  // If this node has already one of the best variable intalled as test, we
179  // move on
180  if (nodeVarMap_.exists(updatedNode) && varsOfInterest.exists(nodeVarMap_[updatedNode])) {
181  return;
182  }
183 
184  // In any other case we have to install variable as best test
185  Idx randy = (Idx)(std::rand() / RAND_MAX) * varsOfInterest.size(), basc = 0;
186  SetConstIteratorSafe< const DiscreteVariable* > varIter;
187  for (varIter = varsOfInterest.cbeginSafe(), basc = 0;
188  varIter != varsOfInterest.cendSafe() && basc < randy;
189  ++varIter, basc++)
190  ;
191 
192  transpose_(updatedNode, *varIter);
193  }
virtual void convertNode2Leaf_(NodeId)
Turns the given node into a leaf if not already so.
virtual void transpose_(NodeId, const DiscreteVariable *)
Installs given variable to the given node, ensuring that the variable is not present in its subtree...
HashTable< NodeId, const DiscreteVariable *> nodeVarMap_
Gives for any node its associated variable.
Size Idx
Type for indexes.
Definition: types.h:52
+ Here is the call graph for this function:

◆ updateNodeWithObservation_()

template<TESTNAME AttributeSelection, bool isScalar = false>
virtual void gum::IncrementalGraphLearner< AttributeSelection, isScalar >::updateNodeWithObservation_ ( const Observation newObs,
NodeId  currentNodeId 
)
inlineprotectedvirtual

Will update internal graph's NodeDatabase of given node with the new observation.

Parameters
newObs
currentNodeId

Reimplemented in gum::ITI< AttributeSelection, isScalar >, and gum::IMDDI< AttributeSelection, isScalar >.

Definition at line 167 of file incrementalGraphLearner.h.

167  {
168  nodeId2Database_[currentNodeId]->addObservation(newObs);
169  }
HashTable< NodeId, NodeDatabase< AttributeSelection, isScalar > *> nodeId2Database_
This hashtable binds every node to an associated NodeDatabase which handles every observation that co...

◆ updateVar()

template<TESTNAME AttributeSelection, bool isScalar>
void gum::IncrementalGraphLearner< AttributeSelection, isScalar >::updateVar ( const DiscreteVariable var)
virtual

If a new modality appears to exists for given variable, call this method to turn every associated node to this variable into leaf. Graph has then indeed to be revised.

Definition at line 147 of file incrementalGraphLearner_tpl.h.

References gum::Set< Key, Alloc >::emplace().

148  {
149  Link< NodeId >* nodIter = var2Node_[var]->list();
150  Link< NodeId >* nni = nullptr;
151  while (nodIter) {
152  nni = nodIter->nextLink();
153  convertNode2Leaf_(nodIter->element());
154  nodIter = nni;
155  }
156  }
virtual void convertNode2Leaf_(NodeId)
Turns the given node into a leaf if not already so.
HashTable< const DiscreteVariable *, LinkedList< NodeId > *> var2Node_
Associates to any variable the list of all nodes associated to this variable.
+ Here is the call graph for this function:

Member Data Documentation

◆ leafDatabase_

template<TESTNAME AttributeSelection, bool isScalar = false>
HashTable< NodeId, Set< const Observation* >* > gum::IncrementalGraphLearner< AttributeSelection, isScalar >::leafDatabase_
protected

This hashtable binds to every leaf an associated set of all hte observations compatible with it.

Definition at line 379 of file incrementalGraphLearner.h.

◆ model_

template<TESTNAME AttributeSelection, bool isScalar = false>
NodeGraphPart gum::IncrementalGraphLearner< AttributeSelection, isScalar >::model_
protected

The source of nodeId.

Definition at line 345 of file incrementalGraphLearner.h.

◆ needUpdate_

template<TESTNAME AttributeSelection, bool isScalar = false>
bool gum::IncrementalGraphLearner< AttributeSelection, isScalar >::needUpdate_
protected

Definition at line 392 of file incrementalGraphLearner.h.

◆ nodeId2Database_

template<TESTNAME AttributeSelection, bool isScalar = false>
HashTable< NodeId, NodeDatabase< AttributeSelection, isScalar >* > gum::IncrementalGraphLearner< AttributeSelection, isScalar >::nodeId2Database_
protected

This hashtable binds every node to an associated NodeDatabase which handles every observation that concerns that node.

Definition at line 373 of file incrementalGraphLearner.h.

◆ nodeSonsMap_

template<TESTNAME AttributeSelection, bool isScalar = false>
HashTable< NodeId, NodeId* > gum::IncrementalGraphLearner< AttributeSelection, isScalar >::nodeSonsMap_
protected

A table giving for any node a table mapping to its son idx is the modality of associated variable.

Definition at line 361 of file incrementalGraphLearner.h.

◆ nodeVarMap_

template<TESTNAME AttributeSelection, bool isScalar = false>
HashTable< NodeId, const DiscreteVariable* > gum::IncrementalGraphLearner< AttributeSelection, isScalar >::nodeVarMap_
protected

Gives for any node its associated variable.

Definition at line 355 of file incrementalGraphLearner.h.

◆ root_

template<TESTNAME AttributeSelection, bool isScalar = false>
NodeId gum::IncrementalGraphLearner< AttributeSelection, isScalar >::root_
protected

The root of the ordered tree.

Definition at line 350 of file incrementalGraphLearner.h.

◆ setOfVars_

template<TESTNAME AttributeSelection, bool isScalar = false>
Set< const DiscreteVariable* > gum::IncrementalGraphLearner< AttributeSelection, isScalar >::setOfVars_
protected

Definition at line 387 of file incrementalGraphLearner.h.

◆ target_

template<TESTNAME AttributeSelection, bool isScalar = false>
MultiDimFunctionGraph< double >* gum::IncrementalGraphLearner< AttributeSelection, isScalar >::target_
protected

The final diagram we're building.

Definition at line 385 of file incrementalGraphLearner.h.

◆ value_

template<TESTNAME AttributeSelection, bool isScalar = false>
const DiscreteVariable* gum::IncrementalGraphLearner< AttributeSelection, isScalar >::value_
protected

Definition at line 389 of file incrementalGraphLearner.h.

◆ valueAssumed_

template<TESTNAME AttributeSelection, bool isScalar = false>
Sequence< ValueType > gum::IncrementalGraphLearner< AttributeSelection, isScalar >::valueAssumed_
protected

Definition at line 390 of file incrementalGraphLearner.h.

◆ var2Node_

template<TESTNAME AttributeSelection, bool isScalar = false>
HashTable< const DiscreteVariable*, LinkedList< NodeId >* > gum::IncrementalGraphLearner< AttributeSelection, isScalar >::var2Node_
protected

Associates to any variable the list of all nodes associated to this variable.

Definition at line 367 of file incrementalGraphLearner.h.


The documentation for this class was generated from the following files: