aGrUM  0.14.2
gum::LeafAggregator Class Reference

<agrum/FMDP/learning/FunctionGraph/leafAggregator.h> More...

#include <leafAggregator.h>

+ Collaboration diagram for gum::LeafAggregator:

Public Member Functions

void update ()
 
bool needsUpdate ()
 
HashTable< NodeId, AbstractLeaf *> leavesMap ()
 
std::string toString ()
 
Constructor & destructor.
 LeafAggregator (NodeGraphPart *idSource, double similarityThreshold)
 Default constructor. More...
 
 ~LeafAggregator ()
 Default destructor. More...
 
Leaf Handling methods
void addLeaf (AbstractLeaf *)
 
bool updateLeaf (AbstractLeaf *)
 
void removeLeaf (AbstractLeaf *)
 
void begin ()
 
bool hasNext ()
 
double next ()
 

Detailed Description

<agrum/FMDP/learning/FunctionGraph/leafAggregator.h>

Gather together leaves with similar distributions

Definition at line 57 of file leafAggregator.h.

Constructor & Destructor Documentation

◆ LeafAggregator()

gum::LeafAggregator::LeafAggregator ( NodeGraphPart idSource,
double  similarityThreshold 
)

Default constructor.

Definition at line 40 of file leafAggregator.cpp.

References __initialContext, and __needsUpdate.

41  :
42  __leavesCpt(idSource),
43  __similarityThreshold(similarityThreshold) {
44  GUM_CONSTRUCTOR(LeafAggregator);
46  __needsUpdate = false;
47  }
NodeGraphPart * __leavesCpt
LeafAggregator(NodeGraphPart *idSource, double similarityThreshold)
Default constructor.
FusionContext< true > * __initialContext

◆ ~LeafAggregator()

gum::LeafAggregator::~LeafAggregator ( )

Default destructor.

Definition at line 52 of file leafAggregator.cpp.

References __initialContext, __leaf2Pair, __removeContext(), and gum::LeafPair::otherLeaf().

52  {
53  __removeContext(0);
54 
55  delete __initialContext;
56 
57  for (HashTableIteratorSafe< AbstractLeaf*, Set< LeafPair* >* > leafIter =
58  __leaf2Pair.beginSafe();
59  leafIter != __leaf2Pair.endSafe();
60  ++leafIter) {
61  for (SetIteratorSafe< LeafPair* > pairIter = leafIter.val()->beginSafe();
62  pairIter != leafIter.val()->endSafe();
63  ++pairIter) {
64  LeafPair* curPair = *pairIter;
65  __leaf2Pair[curPair->otherLeaf(leafIter.key())]->erase(*pairIter);
66  leafIter.val()->erase(curPair);
67  delete curPair;
68  }
69  delete leafIter.val();
70  }
71 
72 
73  GUM_DESTRUCTOR(LeafAggregator);
74  }
LeafAggregator(NodeGraphPart *idSource, double similarityThreshold)
Default constructor.
FusionContext< true > * __initialContext
HashTable< AbstractLeaf *, Set< LeafPair *> *> __leaf2Pair
+ Here is the call graph for this function:

Member Function Documentation

◆ __addInitialPair()

void gum::LeafAggregator::__addInitialPair ( LeafPair p)
private

Definition at line 359 of file leafAggregator.cpp.

References __initialContext, __removeContext(), and gum::FusionContext< isInitial >::addPair().

Referenced by addLeaf(), and needsUpdate().

359  {
360  bool res = __initialContext->addPair(p);
361  if (res) __removeContext(0);
362  }
bool addPair(LeafPair *p)
FusionContext< true > * __initialContext
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __removeContext()

void gum::LeafAggregator::__removeContext ( Idx  startingPos)
private

Definition at line 344 of file leafAggregator.cpp.

References __fusionSeq, __leavesCpt, __needsUpdate, and gum::NodeGraphPart::eraseNode().

Referenced by __addInitialPair(), __removeInitialPair(), __updateInitialPair(), addLeaf(), needsUpdate(), removeLeaf(), updateLeaf(), and ~LeafAggregator().

344  {
345  for (Idx i = __fusionSeq.size() - 1; !__fusionSeq.empty() && i >= startingPos;
346  --i) {
347  __leavesCpt->eraseNode(__fusionSeq.atPos(i)->leaf()->id());
348  delete __fusionSeq.atPos(i);
349  __fusionSeq.erase(__fusionSeq.atPos(i));
350  }
351 
352  __needsUpdate = true;
353  }
NodeGraphPart * __leavesCpt
Sequence< FusionContext< false > *> __fusionSeq
virtual void eraseNode(const NodeId id)
erase the node with the given id
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __removeInitialPair()

void gum::LeafAggregator::__removeInitialPair ( LeafPair p)
private

Definition at line 376 of file leafAggregator.cpp.

References __initialContext, __removeContext(), and gum::FusionContext< isInitial >::removePair().

Referenced by needsUpdate(), and removeLeaf().

376  {
377  bool res = __initialContext->removePair(p);
378  if (res) __removeContext(0);
379  }
bool removePair(LeafPair *p)
FusionContext< true > * __initialContext
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __updateInitialPair()

void gum::LeafAggregator::__updateInitialPair ( LeafPair p)
private

Definition at line 368 of file leafAggregator.cpp.

References __initialContext, __removeContext(), and gum::FusionContext< isInitial >::updatePair().

Referenced by needsUpdate(), and updateLeaf().

368  {
369  bool res = __initialContext->updatePair(p);
370  if (res) __removeContext(0);
371  }
FusionContext< true > * __initialContext
bool updatePair(LeafPair *p)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ addLeaf()

void gum::LeafAggregator::addLeaf ( AbstractLeaf l)

Definition at line 83 of file leafAggregator.cpp.

References __addInitialPair(), __fusionSeq, __leaf2Pair, __needsUpdate, __removeContext(), gum::Set< Key, Alloc >::beginSafe(), gum::Set< Key, Alloc >::endSafe(), gum::Set< Key, Alloc >::insert(), and gum::LeafPair::updateLikelyhood().

Referenced by gum::IMDDI< AttributeSelection, isScalar >::__addLeaf().

83  {
84  Set< LeafPair* >* leafPairSet = new Set< LeafPair* >();
85  Set< LeafPair* > bag;
86 
87  // ****************************************************************************************
88  // Création et ajout des pairs de base (Feuille de base + nouvelle Feuille)
89  for (HashTableConstIteratorSafe< AbstractLeaf*, Set< LeafPair* >* > leafIter =
90  __leaf2Pair.cbeginSafe();
91  leafIter != __leaf2Pair.cendSafe();
92  ++leafIter) {
93  // Création de la pair et ajout dans les listes de pair des feuilles de
94  // base
95  LeafPair* p = new LeafPair(l, leafIter.key());
96  p->updateLikelyhood();
97  leafPairSet->insert(p);
98  (leafIter.val())->insert(p);
99 
100  // Ajout de la nouvelle pair au tas initial
101  __addInitialPair(p);
102 
103  bag.insert(p);
104  }
105 
106  // ****************************************************************************************
107  // Enregistrement de la nouvelle Feuille en tant que feuille de base
108  __leaf2Pair.insert(l, leafPairSet);
109 
110  // ****************************************************************************************
111  // Ajout de la feuille aux FusionContext
112 
113  for (SequenceIteratorSafe< FusionContext< false >* > fusIter =
114  __fusionSeq.beginSafe();
115  fusIter != __fusionSeq.endSafe();
116  ++fusIter) {
117  // Ajout de la nouvelle pair composée de la feuille de FusIter et de la
118  // nouvelle feuille aux FusionContext suivant
119  for (SetIteratorSafe< LeafPair* > pairIter = bag.beginSafe();
120  pairIter != bag.endSafe();
121  ++pairIter) {
122  if ((*fusIter)->leaf()->contains((*pairIter)->secondLeaf()->id())) {
123  bag >> *pairIter;
124  continue;
125  }
126 
127  if ((*fusIter)->addPair(*pairIter)) __removeContext(fusIter.pos() + 1);
128  }
129 
130  if ((*fusIter)->associateLeaf(l)) __removeContext(fusIter.pos() + 1);
131 
132  bag << (*fusIter)->leafAssociatedPair(l);
133  }
134 
135  __needsUpdate = true;
136  }
void __addInitialPair(LeafPair *)
Sequence< FusionContext< false > *> __fusionSeq
HashTable< AbstractLeaf *, Set< LeafPair *> *> __leaf2Pair
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ begin()

void gum::LeafAggregator::begin ( )

◆ hasNext()

bool gum::LeafAggregator::hasNext ( )

◆ leavesMap()

HashTable< NodeId, AbstractLeaf *> gum::LeafAggregator::leavesMap ( )

Definition at line 285 of file leafAggregator.cpp.

References __fusionSeq, __leaf2Pair, gum::HashTable< Key, Val, Alloc >::beginSafe(), gum::HashTable< Key, Val, Alloc >::endSafe(), gum::HashTable< Key, Val, Alloc >::exists(), and gum::HashTable< Key, Val, Alloc >::insert().

Referenced by gum::IMDDI< AttributeSelection, isScalar >::__rebuildFunctionGraph(), and needsUpdate().

285  {
286  HashTable< NodeId, AbstractLeaf* > retMap;
287  for (SequenceIteratorSafe< FusionContext< false >* > fusIter =
288  __fusionSeq.rbeginSafe();
289  fusIter != __fusionSeq.rendSafe();
290  --fusIter) {
291  bool alreadyIn = false;
292  for (HashTableIteratorSafe< NodeId, AbstractLeaf* > mapIter =
293  retMap.beginSafe();
294  mapIter != retMap.endSafe();
295  ++mapIter)
296  if (mapIter.val()->contains((*fusIter)->leaf()->id())) {
297  alreadyIn = true;
298  break;
299  }
300  if (!alreadyIn) retMap.insert((*fusIter)->leaf()->id(), (*fusIter)->leaf());
301  }
302 
303  for (HashTableIteratorSafe< AbstractLeaf*, Set< LeafPair* >* > leafIter =
304  __leaf2Pair.beginSafe();
305  leafIter != __leaf2Pair.endSafe();
306  ++leafIter) {
307  for (HashTableIteratorSafe< NodeId, AbstractLeaf* > mapIter =
308  retMap.beginSafe();
309  mapIter != retMap.endSafe();
310  ++mapIter)
311  if (mapIter.val()->contains(leafIter.key()->id())) {
312  retMap.insert(leafIter.key()->id(), mapIter.val());
313  break;
314  }
315  if (!retMap.exists(leafIter.key()->id()))
316  retMap.insert(leafIter.key()->id(), leafIter.key());
317  }
318 
319  return retMap;
320  }
Sequence< FusionContext< false > *> __fusionSeq
HashTable< AbstractLeaf *, Set< LeafPair *> *> __leaf2Pair
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ needsUpdate()

bool gum::LeafAggregator::needsUpdate ( )
inline

Definition at line 122 of file leafAggregator.h.

References __addInitialPair(), __needsUpdate, __removeContext(), __removeInitialPair(), __updateInitialPair(), leavesMap(), and toString().

Referenced by gum::IMDDI< AttributeSelection, isScalar >::updateGraph().

122 { return __needsUpdate; }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ next()

double gum::LeafAggregator::next ( )

◆ removeLeaf()

void gum::LeafAggregator::removeLeaf ( AbstractLeaf l)

Definition at line 192 of file leafAggregator.cpp.

References __fusionSeq, __leaf2Pair, __needsUpdate, __removeContext(), __removeInitialPair(), gum::Set< Key, Alloc >::beginSafe(), and gum::Set< Key, Alloc >::endSafe().

Referenced by gum::IMDDI< AttributeSelection, isScalar >::__removeLeaf().

192  {
193  // ***********************************************************************************
194  // First we update every base pair linked to that leaf
195  Set< LeafPair* > bag(*(__leaf2Pair[l]));
196  for (SetIteratorSafe< LeafPair* > pairIter = bag.beginSafe();
197  pairIter != bag.endSafe();
198  ++pairIter) {
199  __removeInitialPair(*pairIter);
200  (*__leaf2Pair[(*pairIter)->otherLeaf(l)]) >> *pairIter;
201  }
202 
203  // **********************************************************************************
204  // The we have top update FusionContext pairs associated to that leaf
205  Set< LeafPair* > toBeDeleted;
206  for (SequenceIteratorSafe< FusionContext< false >* > fusIter =
207  __fusionSeq.beginSafe();
208  fusIter != __fusionSeq.endSafe();
209  ++fusIter) {
210  for (SetIteratorSafe< LeafPair* > pairIter = bag.beginSafe();
211  pairIter != bag.endSafe();
212  ++pairIter) {
213  if ((*fusIter)->leaf()->contains((*pairIter)->secondLeaf()->id())
214  || (*fusIter)->leaf()->contains((*pairIter)->firstLeaf()->id())) {
215  bag >> *pairIter;
216  continue;
217  }
218 
219  if ((*fusIter)->removePair(*pairIter)) {
220  __removeContext(fusIter.pos() + 1);
221  }
222  }
223 
224  bag << (*fusIter)->leafAssociatedPair(l);
225  toBeDeleted << (*fusIter)->leafAssociatedPair(l);
226 
227  if ((*fusIter)->deassociateLeaf(l)) { __removeContext(fusIter.pos() + 1); }
228  }
229 
230  for (SetIteratorSafe< LeafPair* > pairIter = toBeDeleted.beginSafe();
231  pairIter != toBeDeleted.endSafe();
232  ++pairIter)
233  delete *pairIter;
234 
235  for (SetIteratorSafe< LeafPair* > pairIter = __leaf2Pair[l]->beginSafe();
236  pairIter != __leaf2Pair[l]->endSafe();
237  ++pairIter)
238  delete *pairIter;
239  delete __leaf2Pair[l];
240  __leaf2Pair.erase(l);
241 
242  __needsUpdate = true;
243  }
void __removeInitialPair(LeafPair *)
Sequence< FusionContext< false > *> __fusionSeq
HashTable< AbstractLeaf *, Set< LeafPair *> *> __leaf2Pair
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ toString()

std::string gum::LeafAggregator::toString ( )

Definition at line 323 of file leafAggregator.cpp.

References __fusionSeq, __initialContext, and gum::FusionContext< isInitial >::toString().

Referenced by needsUpdate().

323  {
324  std::stringstream ss;
325  ss << "################\nTas Initial : " << std::endl
326  << __initialContext->toString() << std::endl;
327 
328  for (auto fusIter = __fusionSeq.beginSafe(); fusIter != __fusionSeq.endSafe();
329  ++fusIter) {
330  ss << "################\nTas " << fusIter.pos() << " : " << std::endl
331  << (*fusIter)->toString();
332  }
333 
334  return ss.str();
335  }
Sequence< FusionContext< false > *> __fusionSeq
FusionContext< true > * __initialContext
std::string toString()
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ update()

void gum::LeafAggregator::update ( )

Definition at line 249 of file leafAggregator.cpp.

References __fusionSeq, __initialContext, __leavesCpt, __needsUpdate, __similarityThreshold, gum::NodeGraphPart::addNode(), gum::FusionContext< isInitial >::addPair(), gum::FusionContext< isInitial >::associateLeaf(), gum::FusionContext< isInitial >::beginPairs(), gum::AbstractLeaf::contains(), gum::FusionContext< isInitial >::containsAssociatedLeaf(), gum::LeafPair::convert2Leaf(), gum::FusionContext< isInitial >::endPairs(), gum::LeafPair::likelyhood(), and gum::FusionContext< isInitial >::top().

Referenced by gum::IMDDI< AttributeSelection, isScalar >::__rebuildFunctionGraph(), and gum::IMDDI< AttributeSelection, isScalar >::updateGraph().

249  {
250  LeafPair* nextPair = __initialContext->top();
253  if (!__fusionSeq.empty()) {
254  nextPair = __fusionSeq.back()->top();
255  pb = __fusionSeq.back()->beginPairs();
256  pe = __fusionSeq.back()->endPairs();
257  }
258 
259 
260  while (nextPair && nextPair->likelyhood() < __similarityThreshold) {
261  AbstractLeaf* newLeaf = nextPair->convert2Leaf(__leavesCpt->addNode());
262  FusionContext< false >* newContext = new FusionContext< false >(newLeaf);
263 
264  for (pair_iterator pairIter = pb; pairIter != pe; ++pairIter) {
265  if (!newLeaf->contains(pairIter.key()->firstLeaf()->id())
266  && !newLeaf->contains(pairIter.key()->secondLeaf()->id()))
267  newContext->addPair(pairIter.key());
268  if (!newLeaf->contains(pairIter.key()->firstLeaf()->id())
269  && !newContext->containsAssociatedLeaf(pairIter.key()->firstLeaf()))
270  newContext->associateLeaf(pairIter.key()->firstLeaf());
271  if (!newLeaf->contains(pairIter.key()->secondLeaf()->id())
272  && !newContext->containsAssociatedLeaf(pairIter.key()->secondLeaf()))
273  newContext->associateLeaf(pairIter.key()->secondLeaf());
274  }
275 
276  __fusionSeq.insert(newContext);
277  nextPair = __fusionSeq.back()->top();
278  pb = __fusionSeq.back()->beginPairs();
279  pe = __fusionSeq.back()->endPairs();
280  }
281  __needsUpdate = false;
282  }
HashTableConstIteratorSafe< LeafPair *, std::vector< Size > > pair_iterator
Definition: fusionContext.h:51
pair_iterator endPairs()
NodeGraphPart * __leavesCpt
virtual NodeId addNode()
insert a new node and return its id
Sequence< FusionContext< false > *> __fusionSeq
bool addPair(LeafPair *p)
FusionContext< true > * __initialContext
bool containsAssociatedLeaf(AbstractLeaf *l)
Definition: fusionContext.h:93
LeafPair * top()
bool associateLeaf(AbstractLeaf *l)
pair_iterator beginPairs()
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ updateLeaf()

bool gum::LeafAggregator::updateLeaf ( AbstractLeaf l)

Definition at line 142 of file leafAggregator.cpp.

References __fusionSeq, __leaf2Pair, __needsUpdate, __removeContext(), __updateInitialPair(), gum::Set< Key, Alloc >::beginSafe(), gum::Set< Key, Alloc >::clear(), gum::Set< Key, Alloc >::endSafe(), and gum::AbstractLeaf::id().

Referenced by gum::IMDDI< AttributeSelection, isScalar >::_updateNodeWithObservation().

142  {
143  // ***********************************************************************************
144  // First we update every base pair linked to that leaf
145  Set< LeafPair* > bag(*(__leaf2Pair[l]));
146  for (SetIteratorSafe< LeafPair* > pairIter = bag.beginSafe();
147  pairIter != bag.endSafe();
148  ++pairIter) {
149  (*pairIter)->updateLikelyhood();
150  __updateInitialPair(*pairIter);
151  }
152 
153  // **********************************************************************************
154  // The we have top update FusionContext pairs associated to that leaf
155  AbstractLeaf* curLeaf = l;
156  for (SequenceIteratorSafe< FusionContext< false >* > fusIter =
157  __fusionSeq.beginSafe();
158  fusIter != __fusionSeq.endSafe();
159  ++fusIter) {
160  if ((*fusIter)->leaf()->contains(curLeaf->id())) {
161  bag.clear();
162  if ((*fusIter)->updateAllAssociatedLeaves())
163  __removeContext(fusIter.pos() + 1);
164  bag = (*fusIter)->associatedPairs();
165  curLeaf = (*fusIter)->leaf();
166  continue;
167  }
168 
169  for (SetIteratorSafe< LeafPair* > pairIter = bag.beginSafe();
170  pairIter != bag.endSafe();
171  ++pairIter) {
172  if ((*fusIter)->leaf()->contains((*pairIter)->secondLeaf()->id())
173  || (*fusIter)->leaf()->contains((*pairIter)->firstLeaf()->id())) {
174  bag >> *pairIter;
175  continue;
176  }
177 
178  if ((*fusIter)->updatePair(*pairIter)) __removeContext(fusIter.pos() + 1);
179  }
180  if ((*fusIter)->updateAssociatedLeaf(curLeaf))
181  __removeContext(fusIter.pos() + 1);
182  bag << (*fusIter)->leafAssociatedPair(curLeaf);
183  }
184 
185  return __needsUpdate;
186  }
Sequence< FusionContext< false > *> __fusionSeq
HashTable< AbstractLeaf *, Set< LeafPair *> *> __leaf2Pair
void __updateInitialPair(LeafPair *)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ __fusionSeq

Sequence< FusionContext< false >* > gum::LeafAggregator::__fusionSeq
private

◆ __initialContext

FusionContext< true >* gum::LeafAggregator::__initialContext
private

◆ __leaf2Pair

HashTable< AbstractLeaf*, Set< LeafPair* >* > gum::LeafAggregator::__leaf2Pair
private

Definition at line 138 of file leafAggregator.h.

Referenced by addLeaf(), leavesMap(), removeLeaf(), updateLeaf(), and ~LeafAggregator().

◆ __leavesCpt

NodeGraphPart* gum::LeafAggregator::__leavesCpt
private

Definition at line 140 of file leafAggregator.h.

Referenced by __removeContext(), and update().

◆ __needsUpdate

bool gum::LeafAggregator::__needsUpdate
private

◆ __similarityThreshold

double gum::LeafAggregator::__similarityThreshold
private

Definition at line 142 of file leafAggregator.h.

Referenced by update().


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