aGrUM  0.20.2
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();
70  ++varIter)
71  var2Node_.insert(*varIter, new LinkedList< NodeId >());
72  var2Node_.insert(value_, new LinkedList< NodeId >());
73 
74  model_.addNode();
75  this->root_ = insertLeafNode_(
76  new NodeDatabase< AttributeSelection, isScalar >(&setOfVars_, value_),
77  value_,
78  new Set< const Observation* >());
79  }
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 87 of file incrementalGraphLearner_tpl.h.

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

87  {
88  for (auto nodeIter = nodeId2Database_.beginSafe();
89  nodeIter != nodeId2Database_.endSafe();
90  ++nodeIter)
91  delete nodeIter.val();
92 
93  for (auto nodeIter = nodeSonsMap_.beginSafe();
94  nodeIter != nodeSonsMap_.endSafe();
95  ++nodeIter)
96  SOA_DEALLOCATE(nodeIter.val(),
97  sizeof(NodeId) * nodeVarMap_[nodeIter.key()]->domainSize());
98 
99  for (auto varIter = var2Node_.beginSafe(); varIter != var2Node_.endSafe();
100  ++varIter)
101  delete varIter.val();
102 
103  for (auto nodeIter = leafDatabase_.beginSafe();
104  nodeIter != leafDatabase_.endSafe();
105  ++nodeIter)
106  delete nodeIter.val();
107 
108  clearValue__();
109 
110  GUM_DESTRUCTOR(IncrementalGraphLearner);
111  }
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.
#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...
void clearValue__()
Template function dispatcher.
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

◆ 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 125 of file incrementalGraphLearner_tpl.h.

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

126  {
127  assumeValue__(newObs);
128 
129  // The we go across the tree
130  NodeId currentNodeId = root_;
131 
132  while (nodeSonsMap_.exists(currentNodeId)) {
133  // On each encountered node, we update the database
134  updateNodeWithObservation_(newObs, currentNodeId);
135 
136  // The we select the next to go throught
137  currentNodeId
138  = nodeSonsMap_[currentNodeId]
139  [branchObs__(newObs, nodeVarMap_[currentNodeId])];
140  }
141 
142  // On final insertion into the leave we reach
143  updateNodeWithObservation_(newObs, currentNodeId);
144  leafDatabase_[currentNodeId]->insert(newObs);
145  }
void assumeValue__(const Observation *obs)
Get value assumed by studied variable for current observation.
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.
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...
virtual void updateNodeWithObservation_(const Observation *newObs, NodeId currentNodeId)
Will update internal graph&#39;s NodeDatabase of given node with the new observation. ...
Idx branchObs__(const Observation *obs, const DiscreteVariable *var)
Seek modality assumed in obs for given var.
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:

◆ 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  {
135  assumeValue__(obs, Int2Type< isScalar >());
136  }
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 137 of file incrementalGraphLearner.h.

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

◆ 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 140 of file incrementalGraphLearner.h.

140  {
141  if (!valueAssumed_.exists(obs->modality(value_)))
142  valueAssumed_ << obs->modality(value_);
143  }

◆ 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 151 of file incrementalGraphLearner.h.

151  {
152  return branchObs__(obs, var, Int2Type< isScalar >());
153  }
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 154 of file incrementalGraphLearner.h.

156  {
157  return obs->rModality(var);
158  }

◆ 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 159 of file incrementalGraphLearner.h.

161  {
162  return obs->modality(var);
163  }

◆ 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 424 of file incrementalGraphLearner_tpl.h.

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

426  {
427  if (nodeVarMap_[currentNodeId] == desiredVar) return;
428 
429  var2Node_[nodeVarMap_[currentNodeId]]->searchAndRemoveLink(currentNodeId);
430  var2Node_[desiredVar]->addLink(currentNodeId);
431  nodeVarMap_[currentNodeId] = desiredVar;
432 
433  if (nodeVarMap_[currentNodeId] != value_
434  && leafDatabase_.exists(currentNodeId)) {
435  delete leafDatabase_[currentNodeId];
436  leafDatabase_.erase(currentNodeId);
437  }
438 
439  if (nodeVarMap_[currentNodeId] == value_
440  && !leafDatabase_.exists(currentNodeId)) {
441  leafDatabase_.insert(currentNodeId, new Set< const Observation* >());
442  }
443 
444  needUpdate_ = true;
445  }
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:

◆ 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 {}

◆ 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 212 of file incrementalGraphLearner_tpl.h.

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

213  {
214  if (nodeVarMap_[currentNodeId] != value_) {
215  leafDatabase_.insert(currentNodeId, new Set< const Observation* >());
216 
217  // Resolving potential sons issue
218  for (Idx modality = 0; modality < nodeVarMap_[currentNodeId]->domainSize();
219  ++modality) {
220  NodeId sonId = nodeSonsMap_[currentNodeId][modality];
221  convertNode2Leaf_(sonId);
222  (*leafDatabase_[currentNodeId])
223  = (*leafDatabase_[currentNodeId]) + *(leafDatabase_[sonId]);
224  removeNode_(sonId);
225  }
226 
227  SOA_DEALLOCATE(nodeSonsMap_[currentNodeId],
228  sizeof(NodeId) * nodeVarMap_[currentNodeId]->domainSize());
229  nodeSonsMap_.erase(currentNodeId);
230 
231  chgNodeBoundVar_(currentNodeId, value_);
232  }
233  }
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 386 of file incrementalGraphLearner_tpl.h.

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

389  {
390  NodeId newNodeId = this->insertNode_(nDB, boundVar);
391  nodeSonsMap_.insert(newNodeId, sonsMap);
392  return newNodeId;
393  }
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 406 of file incrementalGraphLearner_tpl.h.

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

409  {
410  NodeId newNodeId = this->insertNode_(nDB, boundVar);
411  leafDatabase_.insert(newNodeId, obsSet);
412  return newNodeId;
413  }
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 361 of file incrementalGraphLearner_tpl.h.

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

363  {
364  NodeId newNodeId = model_.addNode();
365  nodeVarMap_.insert(newNodeId, boundVar);
366  nodeId2Database_.insert(newNodeId, nDB);
367  var2Node_[boundVar]->addLink(newNodeId);
368 
369  needUpdate_ = true;
370 
371  return newNodeId;
372  }
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 343 of file incrementalGraphLearner.h.

343  {
344  for (SetIteratorSafe< const DiscreteVariable* > varIter
345  = setOfVars_.beginSafe();
346  varIter != setOfVars_.endSafe();
347  ++varIter)
348  ret->add(**varIter);
349  }
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 317 of file incrementalGraphLearner.h.

317 { 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 336 of file incrementalGraphLearner.h.

336  {
337  return this->nodeId2Database_[ni]->nbObservation();
338  }
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 329 of file incrementalGraphLearner.h.

329  {
330  return this->nodeSonsMap_[ni][modality];
331  }
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 322 of file incrementalGraphLearner.h.

322  {
323  return this->nodeVarMap_[ni];
324  }
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 455 of file incrementalGraphLearner_tpl.h.

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

456  {
457  // Retriat de l'id
458  model_.eraseNode(currentNodeId);
459 
460  // Retrait du vecteur fils
461  if (nodeSonsMap_.exists(currentNodeId)) {
462  SOA_DEALLOCATE(nodeSonsMap_[currentNodeId],
463  sizeof(NodeId) * nodeVarMap_[currentNodeId]->domainSize());
464  nodeSonsMap_.erase(currentNodeId);
465  }
466 
467  if (leafDatabase_.exists(currentNodeId)) {
468  delete leafDatabase_[currentNodeId];
469  leafDatabase_.erase(currentNodeId);
470  }
471 
472  // Retrait de la variable
473  var2Node_[nodeVarMap_[currentNodeId]]->searchAndRemoveLink(currentNodeId);
474  nodeVarMap_.erase(currentNodeId);
475 
476  // Retrait du NodeDatabase
477  delete nodeId2Database_[currentNodeId];
478  nodeId2Database_.erase(currentNodeId);
479 
480  needUpdate_ = true;
481  }
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 312 of file incrementalGraphLearner.h.

312 { 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 301 of file incrementalGraphLearner.h.

301 { 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 241 of file incrementalGraphLearner_tpl.h.

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

243  {
244  // **************************************************************************************
245  // Si le noeud courant contient déjà la variable qu'on souhaite lui amener
246  // Il n'y a rien à faire
247  if (nodeVarMap_[currentNodeId] == desiredVar) { return; }
248 
249  // **************************************************************************************
250  // Si le noeud courant est terminal,
251  // Il faut artificiellement insérer un noeud liant à la variable
252  if (nodeVarMap_[currentNodeId] == value_) {
253  // We turned this leaf into an internal node.
254  // This mean that we'll need to install children leaves for each value of
255  // desiredVar
256 
257  // First We must prepare these new leaves NodeDatabases and Sets<const
258  // Observation*>
259  NodeDatabase< AttributeSelection, isScalar >** dbMap
260  = static_cast< NodeDatabase< AttributeSelection, isScalar >** >(
261  SOA_ALLOCATE(sizeof(NodeDatabase< AttributeSelection, isScalar >*)
262  * desiredVar->domainSize()));
263  Set< const Observation* >** obsetMap
264  = static_cast< Set< const Observation* >** >(SOA_ALLOCATE(
265  sizeof(Set< const Observation* >*) * desiredVar->domainSize()));
266  for (Idx modality = 0; modality < desiredVar->domainSize(); ++modality) {
267  dbMap[modality]
268  = new NodeDatabase< AttributeSelection, isScalar >(&setOfVars_, value_);
269  obsetMap[modality] = new Set< const Observation* >();
270  }
271  for (SetIteratorSafe< const Observation* > obsIter
272  = leafDatabase_[currentNodeId]->beginSafe();
273  leafDatabase_[currentNodeId]->endSafe() != obsIter;
274  ++obsIter) {
275  dbMap[branchObs__(*obsIter, desiredVar)]->addObservation(*obsIter);
276  obsetMap[branchObs__(*obsIter, desiredVar)]->insert(*obsIter);
277  }
278 
279  // Then we can install each new leaves (and put in place the sonsMap)
280  NodeId* sonsMap = static_cast< NodeId* >(
281  SOA_ALLOCATE(sizeof(NodeId) * desiredVar->domainSize()));
282  for (Idx modality = 0; modality < desiredVar->domainSize(); ++modality)
283  sonsMap[modality]
284  = insertLeafNode_(dbMap[modality], value_, obsetMap[modality]);
285 
286  // Some necessary clean up
287  SOA_DEALLOCATE(dbMap,
288  sizeof(NodeDatabase< AttributeSelection, isScalar >*)
289  * desiredVar->domainSize());
290  SOA_DEALLOCATE(obsetMap,
291  sizeof(Set< const Observation* >*)
292  * desiredVar->domainSize());
293 
294  // And finally we can turn the node into an internal node associated to
295  // desiredVar
296  chgNodeBoundVar_(currentNodeId, desiredVar);
297  nodeSonsMap_.insert(currentNodeId, sonsMap);
298 
299  return;
300  }
301 
302  // *************************************************************************************
303  // Remains the general case where currentNodeId is an internal node.
304 
305  // First we ensure that children node use desiredVar as variable
306  for (Idx modality = 0; modality < nodeVarMap_[currentNodeId]->domainSize();
307  ++modality)
308  transpose_(nodeSonsMap_[currentNodeId][modality], desiredVar);
309 
310  // Sequence<NodeDatabase<AttributeSelection, isScalar>*>
311  // sonsNodeDatabase =
312  // nodeId2Database_[currentNodeId]->splitOnVar(desiredVar);
313  NodeId* sonsMap = static_cast< NodeId* >(
314  SOA_ALLOCATE(sizeof(NodeId) * desiredVar->domainSize()));
315 
316  // Then we create the new mapping
317  for (Idx desiredVarModality = 0; desiredVarModality < desiredVar->domainSize();
318  ++desiredVarModality) {
319  NodeId* grandSonsMap = static_cast< NodeId* >(
320  SOA_ALLOCATE(sizeof(NodeId) * nodeVarMap_[currentNodeId]->domainSize()));
321  NodeDatabase< AttributeSelection, isScalar >* sonDB
322  = new NodeDatabase< AttributeSelection, isScalar >(&setOfVars_, value_);
323  for (Idx currentVarModality = 0;
324  currentVarModality < nodeVarMap_[currentNodeId]->domainSize();
325  ++currentVarModality) {
326  grandSonsMap[currentVarModality]
327  = nodeSonsMap_[nodeSonsMap_[currentNodeId][currentVarModality]]
328  [desiredVarModality];
329  sonDB->operator+=((*nodeId2Database_[grandSonsMap[currentVarModality]]));
330  }
331 
332  sonsMap[desiredVarModality]
333  = insertInternalNode_(sonDB, nodeVarMap_[currentNodeId], grandSonsMap);
334  }
335 
336  // Finally we clean the old remaining nodes
337  for (Idx currentVarModality = 0;
338  currentVarModality < nodeVarMap_[currentNodeId]->domainSize();
339  ++currentVarModality) {
340  removeNode_(nodeSonsMap_[currentNodeId][currentVarModality]);
341  }
342 
343  // We suppress the old sons map and remap to the new one
344  SOA_DEALLOCATE(nodeSonsMap_[currentNodeId],
345  sizeof(NodeId) * nodeVarMap_[currentNodeId]->domainSize());
346  nodeSonsMap_[currentNodeId] = sonsMap;
347 
348  chgNodeBoundVar_(currentNodeId, desiredVar);
349  }
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...
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...
Idx branchObs__(const Observation *obs, const DiscreteVariable *var)
Seek modality assumed in obs for given var.
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 180 of file incrementalGraphLearner_tpl.h.

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

182  {
183  // If this node has no interesting variable, we turn it into a leaf
184  if (varsOfInterest.empty()) {
185  convertNode2Leaf_(updatedNode);
186  return;
187  }
188 
189  // If this node has already one of the best variable intalled as test, we
190  // move on
191  if (nodeVarMap_.exists(updatedNode)
192  && varsOfInterest.exists(nodeVarMap_[updatedNode])) {
193  return;
194  }
195 
196  // In any other case we have to install variable as best test
197  Idx randy = (Idx)(std::rand() / RAND_MAX) * varsOfInterest.size(), basc = 0;
198  SetConstIteratorSafe< const DiscreteVariable* > varIter;
199  for (varIter = varsOfInterest.cbeginSafe(), basc = 0;
200  varIter != varsOfInterest.cendSafe() && basc < randy;
201  ++varIter, basc++)
202  ;
203 
204  transpose_(updatedNode, *varIter);
205  }
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 174 of file incrementalGraphLearner.h.

175  {
176  nodeId2Database_[currentNodeId]->addObservation(newObs);
177  }
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 158 of file incrementalGraphLearner_tpl.h.

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

159  {
160  Link< NodeId >* nodIter = var2Node_[var]->list();
161  Link< NodeId >* nni = nullptr;
162  while (nodIter) {
163  nni = nodIter->nextLink();
164  convertNode2Leaf_(nodIter->element());
165  nodIter = nni;
166  }
167  }
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 398 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 363 of file incrementalGraphLearner.h.

◆ needUpdate_

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

Definition at line 411 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 392 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 379 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 373 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 368 of file incrementalGraphLearner.h.

◆ setOfVars_

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

Definition at line 406 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 404 of file incrementalGraphLearner.h.

◆ value_

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

Definition at line 408 of file incrementalGraphLearner.h.

◆ valueAssumed_

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

Definition at line 409 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 385 of file incrementalGraphLearner.h.


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