aGrUM  0.17.2
a C++ library for (probabilistic) graphical models
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 60 of file leafAggregator.h.

Constructor & Destructor Documentation

◆ LeafAggregator()

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

Default constructor.

Definition at line 43 of file leafAggregator.cpp.

References __initialContext, and __needsUpdate.

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

◆ ~LeafAggregator()

gum::LeafAggregator::~LeafAggregator ( )

Default destructor.

Definition at line 55 of file leafAggregator.cpp.

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

55  {
56  __removeContext(0);
57 
58  delete __initialContext;
59 
60  for (HashTableIteratorSafe< AbstractLeaf*, Set< LeafPair* >* > leafIter =
61  __leaf2Pair.beginSafe();
62  leafIter != __leaf2Pair.endSafe();
63  ++leafIter) {
64  for (SetIteratorSafe< LeafPair* > pairIter = leafIter.val()->beginSafe();
65  pairIter != leafIter.val()->endSafe();
66  ++pairIter) {
67  LeafPair* curPair = *pairIter;
68  __leaf2Pair[curPair->otherLeaf(leafIter.key())]->erase(*pairIter);
69  leafIter.val()->erase(curPair);
70  delete curPair;
71  }
72  delete leafIter.val();
73  }
74 
75 
76  GUM_DESTRUCTOR(LeafAggregator);
77  }
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 362 of file leafAggregator.cpp.

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

Referenced by addLeaf(), and needsUpdate().

362  {
363  bool res = __initialContext->addPair(p);
364  if (res) __removeContext(0);
365  }
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 347 of file leafAggregator.cpp.

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

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

347  {
348  for (Idx i = __fusionSeq.size() - 1; !__fusionSeq.empty() && i >= startingPos;
349  --i) {
350  __leavesCpt->eraseNode(__fusionSeq.atPos(i)->leaf()->id());
351  delete __fusionSeq.atPos(i);
352  __fusionSeq.erase(__fusionSeq.atPos(i));
353  }
354 
355  __needsUpdate = true;
356  }
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 379 of file leafAggregator.cpp.

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

Referenced by needsUpdate(), and removeLeaf().

379  {
380  bool res = __initialContext->removePair(p);
381  if (res) __removeContext(0);
382  }
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 371 of file leafAggregator.cpp.

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

Referenced by needsUpdate(), and updateLeaf().

371  {
372  bool res = __initialContext->updatePair(p);
373  if (res) __removeContext(0);
374  }
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 86 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().

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

288  {
289  HashTable< NodeId, AbstractLeaf* > retMap;
290  for (SequenceIteratorSafe< FusionContext< false >* > fusIter =
291  __fusionSeq.rbeginSafe();
292  fusIter != __fusionSeq.rendSafe();
293  --fusIter) {
294  bool alreadyIn = false;
295  for (HashTableIteratorSafe< NodeId, AbstractLeaf* > mapIter =
296  retMap.beginSafe();
297  mapIter != retMap.endSafe();
298  ++mapIter)
299  if (mapIter.val()->contains((*fusIter)->leaf()->id())) {
300  alreadyIn = true;
301  break;
302  }
303  if (!alreadyIn) retMap.insert((*fusIter)->leaf()->id(), (*fusIter)->leaf());
304  }
305 
306  for (HashTableIteratorSafe< AbstractLeaf*, Set< LeafPair* >* > leafIter =
307  __leaf2Pair.beginSafe();
308  leafIter != __leaf2Pair.endSafe();
309  ++leafIter) {
310  for (HashTableIteratorSafe< NodeId, AbstractLeaf* > mapIter =
311  retMap.beginSafe();
312  mapIter != retMap.endSafe();
313  ++mapIter)
314  if (mapIter.val()->contains(leafIter.key()->id())) {
315  retMap.insert(leafIter.key()->id(), mapIter.val());
316  break;
317  }
318  if (!retMap.exists(leafIter.key()->id()))
319  retMap.insert(leafIter.key()->id(), leafIter.key());
320  }
321 
322  return retMap;
323  }
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 125 of file leafAggregator.h.

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

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

125 { 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 195 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().

195  {
196  // ***********************************************************************************
197  // First we update every base pair linked to that leaf
198  Set< LeafPair* > bag(*(__leaf2Pair[l]));
199  for (SetIteratorSafe< LeafPair* > pairIter = bag.beginSafe();
200  pairIter != bag.endSafe();
201  ++pairIter) {
202  __removeInitialPair(*pairIter);
203  (*__leaf2Pair[(*pairIter)->otherLeaf(l)]) >> *pairIter;
204  }
205 
206  // **********************************************************************************
207  // The we have top update FusionContext pairs associated to that leaf
208  Set< LeafPair* > toBeDeleted;
209  for (SequenceIteratorSafe< FusionContext< false >* > fusIter =
210  __fusionSeq.beginSafe();
211  fusIter != __fusionSeq.endSafe();
212  ++fusIter) {
213  for (SetIteratorSafe< LeafPair* > pairIter = bag.beginSafe();
214  pairIter != bag.endSafe();
215  ++pairIter) {
216  if ((*fusIter)->leaf()->contains((*pairIter)->secondLeaf()->id())
217  || (*fusIter)->leaf()->contains((*pairIter)->firstLeaf()->id())) {
218  bag >> *pairIter;
219  continue;
220  }
221 
222  if ((*fusIter)->removePair(*pairIter)) {
223  __removeContext(fusIter.pos() + 1);
224  }
225  }
226 
227  bag << (*fusIter)->leafAssociatedPair(l);
228  toBeDeleted << (*fusIter)->leafAssociatedPair(l);
229 
230  if ((*fusIter)->deassociateLeaf(l)) { __removeContext(fusIter.pos() + 1); }
231  }
232 
233  for (SetIteratorSafe< LeafPair* > pairIter = toBeDeleted.beginSafe();
234  pairIter != toBeDeleted.endSafe();
235  ++pairIter)
236  delete *pairIter;
237 
238  for (SetIteratorSafe< LeafPair* > pairIter = __leaf2Pair[l]->beginSafe();
239  pairIter != __leaf2Pair[l]->endSafe();
240  ++pairIter)
241  delete *pairIter;
242  delete __leaf2Pair[l];
243  __leaf2Pair.erase(l);
244 
245  __needsUpdate = true;
246  }
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 326 of file leafAggregator.cpp.

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

Referenced by needsUpdate().

326  {
327  std::stringstream ss;
328  ss << "################\nTas Initial : " << std::endl
329  << __initialContext->toString() << std::endl;
330 
331  for (auto fusIter = __fusionSeq.beginSafe(); fusIter != __fusionSeq.endSafe();
332  ++fusIter) {
333  ss << "################\nTas " << fusIter.pos() << " : " << std::endl
334  << (*fusIter)->toString();
335  }
336 
337  return ss.str();
338  }
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 252 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().

252  {
253  LeafPair* nextPair = __initialContext->top();
256  if (!__fusionSeq.empty()) {
257  nextPair = __fusionSeq.back()->top();
258  pb = __fusionSeq.back()->beginPairs();
259  pe = __fusionSeq.back()->endPairs();
260  }
261 
262 
263  while (nextPair && nextPair->likelyhood() < __similarityThreshold) {
264  AbstractLeaf* newLeaf = nextPair->convert2Leaf(__leavesCpt->addNode());
265  FusionContext< false >* newContext = new FusionContext< false >(newLeaf);
266 
267  for (pair_iterator pairIter = pb; pairIter != pe; ++pairIter) {
268  if (!newLeaf->contains(pairIter.key()->firstLeaf()->id())
269  && !newLeaf->contains(pairIter.key()->secondLeaf()->id()))
270  newContext->addPair(pairIter.key());
271  if (!newLeaf->contains(pairIter.key()->firstLeaf()->id())
272  && !newContext->containsAssociatedLeaf(pairIter.key()->firstLeaf()))
273  newContext->associateLeaf(pairIter.key()->firstLeaf());
274  if (!newLeaf->contains(pairIter.key()->secondLeaf()->id())
275  && !newContext->containsAssociatedLeaf(pairIter.key()->secondLeaf()))
276  newContext->associateLeaf(pairIter.key()->secondLeaf());
277  }
278 
279  __fusionSeq.insert(newContext);
280  nextPair = __fusionSeq.back()->top();
281  pb = __fusionSeq.back()->beginPairs();
282  pe = __fusionSeq.back()->endPairs();
283  }
284  __needsUpdate = false;
285  }
HashTableConstIteratorSafe< LeafPair *, std::vector< Size > > pair_iterator
Definition: fusionContext.h:54
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:96
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 145 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().

145  {
146  // ***********************************************************************************
147  // First we update every base pair linked to that leaf
148  Set< LeafPair* > bag(*(__leaf2Pair[l]));
149  for (SetIteratorSafe< LeafPair* > pairIter = bag.beginSafe();
150  pairIter != bag.endSafe();
151  ++pairIter) {
152  (*pairIter)->updateLikelyhood();
153  __updateInitialPair(*pairIter);
154  }
155 
156  // **********************************************************************************
157  // The we have top update FusionContext pairs associated to that leaf
158  AbstractLeaf* curLeaf = l;
159  for (SequenceIteratorSafe< FusionContext< false >* > fusIter =
160  __fusionSeq.beginSafe();
161  fusIter != __fusionSeq.endSafe();
162  ++fusIter) {
163  if ((*fusIter)->leaf()->contains(curLeaf->id())) {
164  bag.clear();
165  if ((*fusIter)->updateAllAssociatedLeaves())
166  __removeContext(fusIter.pos() + 1);
167  bag = (*fusIter)->associatedPairs();
168  curLeaf = (*fusIter)->leaf();
169  continue;
170  }
171 
172  for (SetIteratorSafe< LeafPair* > pairIter = bag.beginSafe();
173  pairIter != bag.endSafe();
174  ++pairIter) {
175  if ((*fusIter)->leaf()->contains((*pairIter)->secondLeaf()->id())
176  || (*fusIter)->leaf()->contains((*pairIter)->firstLeaf()->id())) {
177  bag >> *pairIter;
178  continue;
179  }
180 
181  if ((*fusIter)->updatePair(*pairIter)) __removeContext(fusIter.pos() + 1);
182  }
183  if ((*fusIter)->updateAssociatedLeaf(curLeaf))
184  __removeContext(fusIter.pos() + 1);
185  bag << (*fusIter)->leafAssociatedPair(curLeaf);
186  }
187 
188  return __needsUpdate;
189  }
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 141 of file leafAggregator.h.

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

◆ __leavesCpt

NodeGraphPart* gum::LeafAggregator::__leavesCpt
private

Definition at line 143 of file leafAggregator.h.

Referenced by __removeContext(), and update().

◆ __needsUpdate

bool gum::LeafAggregator::__needsUpdate
private

◆ __similarityThreshold

double gum::LeafAggregator::__similarityThreshold
private

Definition at line 145 of file leafAggregator.h.

Referenced by update().


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