aGrUM  0.20.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 59 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 gum::Set< Key, Alloc >::emplace().

44  :
45  leavesCpt__(idSource),
46  similarityThreshold__(similarityThreshold) {
47  GUM_CONSTRUCTOR(LeafAggregator);
49  needsUpdate__ = false;
50  }
FusionContext< true > * initialContext__
LeafAggregator(NodeGraphPart *idSource, double similarityThreshold)
Default constructor.
NodeGraphPart * leavesCpt__
+ Here is the call graph for this function:

◆ ~LeafAggregator()

gum::LeafAggregator::~LeafAggregator ( )

Default destructor.

Definition at line 55 of file leafAggregator.cpp.

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

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  }
FusionContext< true > * initialContext__
LeafAggregator(NodeGraphPart *idSource, double similarityThreshold)
Default constructor.
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 gum::Set< Key, Alloc >::emplace().

362  {
363  bool res = initialContext__->addPair(p);
364  if (res) removeContext__(0);
365  }
FusionContext< true > * initialContext__
bool addPair(LeafPair *p)
+ Here is the call graph for this function:

◆ addLeaf()

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

Definition at line 86 of file leafAggregator.cpp.

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

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  }
Sequence< FusionContext< false > *> fusionSeq__
void addInitialPair__(LeafPair *)
HashTable< AbstractLeaf *, Set< LeafPair *> *> leaf2Pair__
+ Here is the call 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 gum::Set< Key, Alloc >::emplace().

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:

◆ needsUpdate()

bool gum::LeafAggregator::needsUpdate ( )
inline

Definition at line 124 of file leafAggregator.h.

References needsUpdate__.

124 { return needsUpdate__; }

◆ next()

double gum::LeafAggregator::next ( )

◆ removeContext__()

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

Definition at line 347 of file leafAggregator.cpp.

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

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  }
Sequence< FusionContext< false > *> fusionSeq__
NodeGraphPart * leavesCpt__
virtual void eraseNode(const NodeId id)
erase the node with the given id
+ Here is the call graph for this function:

◆ removeInitialPair__()

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

Definition at line 379 of file leafAggregator.cpp.

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

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:

◆ removeLeaf()

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

Definition at line 195 of file leafAggregator.cpp.

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

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:

◆ toString()

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

Definition at line 326 of file leafAggregator.cpp.

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

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:

◆ update()

void gum::LeafAggregator::update ( )

Definition at line 252 of file leafAggregator.cpp.

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

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:53
pair_iterator endPairs()
Sequence< FusionContext< false > *> fusionSeq__
virtual NodeId addNode()
insert a new node and return its id
FusionContext< true > * initialContext__
bool addPair(LeafPair *p)
bool containsAssociatedLeaf(AbstractLeaf *l)
Definition: fusionContext.h:95
LeafPair * top()
NodeGraphPart * leavesCpt__
bool associateLeaf(AbstractLeaf *l)
pair_iterator beginPairs()
+ Here is the call graph for this function:

◆ updateInitialPair__()

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

Definition at line 371 of file leafAggregator.cpp.

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

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:

◆ updateLeaf()

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

Definition at line 145 of file leafAggregator.cpp.

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

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__
void updateInitialPair__(LeafPair *)
HashTable< AbstractLeaf *, Set< LeafPair *> *> leaf2Pair__
+ Here is the call graph for this function:

Member Data Documentation

◆ fusionSeq__

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

Definition at line 136 of file leafAggregator.h.

◆ initialContext__

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

Definition at line 138 of file leafAggregator.h.

◆ leaf2Pair__

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

Definition at line 140 of file leafAggregator.h.

◆ leavesCpt__

NodeGraphPart* gum::LeafAggregator::leavesCpt__
private

Definition at line 142 of file leafAggregator.h.

◆ needsUpdate__

bool gum::LeafAggregator::needsUpdate__
private

Definition at line 145 of file leafAggregator.h.

Referenced by needsUpdate().

◆ similarityThreshold__

double gum::LeafAggregator::similarityThreshold__
private

Definition at line 144 of file leafAggregator.h.


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