aGrUM 3.0.0
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.
 ~LeafAggregator ()
 Default destructor.
Leaf Handling methods
void addLeaf (AbstractLeaf *)
bool updateLeaf (AbstractLeaf *)
void removeLeaf (AbstractLeaf *)
void begin ()
bool hasNext ()
double next ()

Private Member Functions

void _removeContext_ (Idx)
void _addInitialPair_ (LeafPair *)
void _updateInitialPair_ (LeafPair *)
void _removeInitialPair_ (LeafPair *)

Private Attributes

Sequence< FusionContext< false > * > _fusionSeq_
FusionContext< true > * _initialContext_
HashTable< AbstractLeaf *, Set< LeafPair * > * > _leaf2Pair_
NodeGraphPart_leavesCpt_
double _similarityThreshold_
bool _needsUpdate_

Detailed Description

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

Gather together leaves with similar distributions

Definition at line 72 of file leafAggregator.h.

Constructor & Destructor Documentation

◆ LeafAggregator()

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

Default constructor.

Definition at line 69 of file leafAggregator.cpp.

69 :
70 _leavesCpt_(idSource), _similarityThreshold_(similarityThreshold) {
71 GUM_CONSTRUCTOR(LeafAggregator);
72 _initialContext_ = new FusionContext< true >(nullptr);
73 _needsUpdate_ = false;
74 }
LeafAggregator(NodeGraphPart *idSource, double similarityThreshold)
Default constructor.
NodeGraphPart * _leavesCpt_
FusionContext< true > * _initialContext_

References LeafAggregator(), _initialContext_, _leavesCpt_, _needsUpdate_, and _similarityThreshold_.

Referenced by LeafAggregator(), and ~LeafAggregator().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~LeafAggregator()

gum::LeafAggregator::~LeafAggregator ( )

Default destructor.

Definition at line 79 of file leafAggregator.cpp.

79 {
81
82 delete _initialContext_;
83
84 for (HashTableIteratorSafe< AbstractLeaf*, Set< LeafPair* >* > leafIter
85 = _leaf2Pair_.beginSafe();
86 leafIter != _leaf2Pair_.endSafe();
87 ++leafIter) {
88 for (SetIteratorSafe< LeafPair* > pairIter = leafIter.val()->beginSafe();
89 pairIter != leafIter.val()->endSafe();
90 ++pairIter) {
91 LeafPair* curPair = *pairIter;
92 _leaf2Pair_[curPair->otherLeaf(leafIter.key())]->erase(*pairIter);
93 leafIter.val()->erase(curPair);
94 delete curPair;
95 }
96 delete leafIter.val();
97 }
98
99
100 GUM_DESTRUCTOR(LeafAggregator);
101 }
HashTable< AbstractLeaf *, Set< LeafPair * > * > _leaf2Pair_

References LeafAggregator(), _initialContext_, _leaf2Pair_, _removeContext_(), and gum::LeafPair::otherLeaf().

Here is the call graph for this function:

Member Function Documentation

◆ _addInitialPair_()

void gum::LeafAggregator::_addInitialPair_ ( LeafPair * p)
private

Definition at line 357 of file leafAggregator.cpp.

357 {
358 bool res = _initialContext_->addPair(p);
359 if (res) _removeContext_(0);
360 }

References _initialContext_, and _removeContext_().

Referenced by addLeaf().

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.

344 {
345 for (Idx i = _fusionSeq_.size() - 1; !_fusionSeq_.empty() && i >= startingPos; --i) {
346 _leavesCpt_->eraseNode(_fusionSeq_.atPos(i)->leaf()->id());
347 delete _fusionSeq_.atPos(i);
348 _fusionSeq_.erase(_fusionSeq_.atPos(i));
349 }
350
351 _needsUpdate_ = true;
352 }
Sequence< FusionContext< false > * > _fusionSeq_
Size Idx
Type for indexes.
Definition types.h:79

References _fusionSeq_, _leavesCpt_, and _needsUpdate_.

Referenced by ~LeafAggregator(), _addInitialPair_(), _removeInitialPair_(), _updateInitialPair_(), addLeaf(), removeLeaf(), and updateLeaf().

Here is the caller graph for this function:

◆ _removeInitialPair_()

void gum::LeafAggregator::_removeInitialPair_ ( LeafPair * p)
private

Definition at line 373 of file leafAggregator.cpp.

373 {
374 bool res = _initialContext_->removePair(p);
375 if (res) _removeContext_(0);
376 }

References _initialContext_, and _removeContext_().

Referenced by removeLeaf().

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 365 of file leafAggregator.cpp.

365 {
366 bool res = _initialContext_->updatePair(p);
367 if (res) _removeContext_(0);
368 }

References _initialContext_, and _removeContext_().

Referenced by updateLeaf().

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 110 of file leafAggregator.cpp.

110 {
111 Set< LeafPair* >* leafPairSet = new Set< LeafPair* >();
112 Set< LeafPair* > bag;
113
114 // ****************************************************************************************
115 // Création et ajout des pairs de base (Feuille de base + nouvelle Feuille)
116 for (HashTableConstIteratorSafe< AbstractLeaf*, Set< LeafPair* >* > leafIter
117 = _leaf2Pair_.cbeginSafe();
118 leafIter != _leaf2Pair_.cendSafe();
119 ++leafIter) {
120 // Création de la pair et ajout dans les listes de pair des feuilles de
121 // base
122 LeafPair* p = new LeafPair(l, leafIter.key());
123 p->updateLikelyhood();
124 leafPairSet->insert(p);
125 (leafIter.val())->insert(p);
126
127 // Ajout de la nouvelle pair au tas initial
129
130 bag.insert(p);
131 }
132
133 // ****************************************************************************************
134 // Enregistrement de la nouvelle Feuille en tant que feuille de base
135 _leaf2Pair_.insert(l, leafPairSet);
136
137 // ****************************************************************************************
138 // Ajout de la feuille aux FusionContext
139
140 for (SequenceIteratorSafe< FusionContext< false >* > fusIter = _fusionSeq_.beginSafe();
141 fusIter != _fusionSeq_.endSafe();
142 ++fusIter) {
143 // Ajout de la nouvelle pair composée de la feuille de FusIter et de la
144 // nouvelle feuille aux FusionContext suivant
145 for (SetIteratorSafe< LeafPair* > pairIter = bag.beginSafe(); pairIter != bag.endSafe();
146 ++pairIter) {
147 if ((*fusIter)->leaf()->contains((*pairIter)->secondLeaf()->id())) {
148 bag >> *pairIter;
149 continue;
150 }
151
152 if ((*fusIter)->addPair(*pairIter)) _removeContext_(fusIter.pos() + 1);
153 }
154
155 if ((*fusIter)->associateLeaf(l)) _removeContext_(fusIter.pos() + 1);
156
157 bag << (*fusIter)->leafAssociatedPair(l);
158 }
159
160 _needsUpdate_ = true;
161 }
void _addInitialPair_(LeafPair *)

References _addInitialPair_(), _fusionSeq_, _leaf2Pair_, _needsUpdate_, _removeContext_(), gum::Set< Key >::beginSafe(), gum::Set< Key >::endSafe(), gum::Set< Key >::insert(), and gum::LeafPair::updateLikelyhood().

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 296 of file leafAggregator.cpp.

296 {
297 HashTable< NodeId, AbstractLeaf* > retMap;
298 for (SequenceIteratorSafe< FusionContext< false >* > fusIter = _fusionSeq_.rbeginSafe();
299 fusIter != _fusionSeq_.rendSafe();
300 --fusIter) {
301 bool alreadyIn = false;
302 for (HashTableIteratorSafe< NodeId, AbstractLeaf* > mapIter = retMap.beginSafe();
303 mapIter != retMap.endSafe();
304 ++mapIter)
305 if (mapIter.val()->contains((*fusIter)->leaf()->id())) {
306 alreadyIn = true;
307 break;
308 }
309 if (!alreadyIn) retMap.insert((*fusIter)->leaf()->id(), (*fusIter)->leaf());
310 }
311
312 for (HashTableIteratorSafe< AbstractLeaf*, Set< LeafPair* >* > leafIter
313 = _leaf2Pair_.beginSafe();
314 leafIter != _leaf2Pair_.endSafe();
315 ++leafIter) {
316 for (HashTableIteratorSafe< NodeId, AbstractLeaf* > mapIter = retMap.beginSafe();
317 mapIter != retMap.endSafe();
318 ++mapIter)
319 if (mapIter.val()->contains(leafIter.key()->id())) {
320 retMap.insert(leafIter.key()->id(), mapIter.val());
321 break;
322 }
323 if (!retMap.exists(leafIter.key()->id())) retMap.insert(leafIter.key()->id(), leafIter.key());
324 }
325
326 return retMap;
327 }

References _fusionSeq_, _leaf2Pair_, gum::HashTable< Key, Val >::beginSafe(), gum::HashTable< Key, Val >::endSafe(), gum::HashTable< Key, Val >::exists(), and gum::HashTable< Key, Val >::insert().

Here is the call graph for this function:

◆ needsUpdate()

INLINE bool gum::LeafAggregator::needsUpdate ( )

Definition at line 48 of file leafAggregator_inl.h.

48{ return _needsUpdate_; }

References _needsUpdate_.

◆ next()

double gum::LeafAggregator::next ( )

◆ removeLeaf()

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

Definition at line 210 of file leafAggregator.cpp.

210 {
211 // ***********************************************************************************
212 // First we update every base pair linked to that leaf
213 Set< LeafPair* > bag(*(_leaf2Pair_[l]));
214 for (SetIteratorSafe< LeafPair* > pairIter = bag.beginSafe(); pairIter != bag.endSafe();
215 ++pairIter) {
216 _removeInitialPair_(*pairIter);
217 (*_leaf2Pair_[(*pairIter)->otherLeaf(l)]) >> *pairIter;
218 }
219
220 // **********************************************************************************
221 // The we have top update FusionContext pairs associated to that leaf
222 Set< LeafPair* > toBeDeleted;
223 for (SequenceIteratorSafe< FusionContext< false >* > fusIter = _fusionSeq_.beginSafe();
224 fusIter != _fusionSeq_.endSafe();
225 ++fusIter) {
226 for (SetIteratorSafe< LeafPair* > pairIter = bag.beginSafe(); pairIter != bag.endSafe();
227 ++pairIter) {
228 if ((*fusIter)->leaf()->contains((*pairIter)->secondLeaf()->id())
229 || (*fusIter)->leaf()->contains((*pairIter)->firstLeaf()->id())) {
230 bag >> *pairIter;
231 continue;
232 }
233
234 if ((*fusIter)->removePair(*pairIter)) { _removeContext_(fusIter.pos() + 1); }
235 }
236
237 bag << (*fusIter)->leafAssociatedPair(l);
238 toBeDeleted << (*fusIter)->leafAssociatedPair(l);
239
240 if ((*fusIter)->deassociateLeaf(l)) { _removeContext_(fusIter.pos() + 1); }
241 }
242
243 for (SetIteratorSafe< LeafPair* > pairIter = toBeDeleted.beginSafe();
244 pairIter != toBeDeleted.endSafe();
245 ++pairIter)
246 delete *pairIter;
247
248 for (SetIteratorSafe< LeafPair* > pairIter = _leaf2Pair_[l]->beginSafe();
249 pairIter != _leaf2Pair_[l]->endSafe();
250 ++pairIter)
251 delete *pairIter;
252 delete _leaf2Pair_[l];
253 _leaf2Pair_.erase(l);
254
255 _needsUpdate_ = true;
256 }
void _removeInitialPair_(LeafPair *)

References _fusionSeq_, _leaf2Pair_, _needsUpdate_, _removeContext_(), _removeInitialPair_(), gum::Set< Key >::beginSafe(), and gum::Set< Key >::endSafe().

Here is the call graph for this function:

◆ toString()

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

Definition at line 329 of file leafAggregator.cpp.

329 {
330 std::string ss
331 = std::format("################\nTas Initial : \n{}\n", _initialContext_->toString());
332 for (auto fusIter = _fusionSeq_.beginSafe(); fusIter != _fusionSeq_.endSafe(); ++fusIter)
333 ss += std::format("################\nTas {} : \n{}", fusIter.pos(), (*fusIter)->toString());
334 return ss;
335 }

References _fusionSeq_, and _initialContext_.

◆ update()

void gum::LeafAggregator::update ( )

Definition at line 261 of file leafAggregator.cpp.

261 {
262 LeafPair* nextPair = _initialContext_->top();
263 pair_iterator pb = _initialContext_->beginPairs();
264 pair_iterator pe = _initialContext_->endPairs();
265 if (!_fusionSeq_.empty()) {
266 nextPair = _fusionSeq_.back()->top();
267 pb = _fusionSeq_.back()->beginPairs();
268 pe = _fusionSeq_.back()->endPairs();
269 }
270
271
272 while (nextPair && nextPair->likelyhood() < _similarityThreshold_) {
273 AbstractLeaf* newLeaf = nextPair->convert2Leaf(_leavesCpt_->addNode());
274 FusionContext< false >* newContext = new FusionContext< false >(newLeaf);
275
276 for (pair_iterator pairIter = pb; pairIter != pe; ++pairIter) {
277 if (!newLeaf->contains(pairIter.key()->firstLeaf()->id())
278 && !newLeaf->contains(pairIter.key()->secondLeaf()->id()))
279 newContext->addPair(pairIter.key());
280 if (!newLeaf->contains(pairIter.key()->firstLeaf()->id())
281 && !newContext->containsAssociatedLeaf(pairIter.key()->firstLeaf()))
282 newContext->associateLeaf(pairIter.key()->firstLeaf());
283 if (!newLeaf->contains(pairIter.key()->secondLeaf()->id())
284 && !newContext->containsAssociatedLeaf(pairIter.key()->secondLeaf()))
285 newContext->associateLeaf(pairIter.key()->secondLeaf());
286 }
287
288 _fusionSeq_.insert(newContext);
289 nextPair = _fusionSeq_.back()->top();
290 pb = _fusionSeq_.back()->beginPairs();
291 pe = _fusionSeq_.back()->endPairs();
292 }
293 _needsUpdate_ = false;
294 }
HashTableConstIteratorSafe< LeafPair *, std::vector< Size > > pair_iterator

References _fusionSeq_, _initialContext_, _leavesCpt_, _needsUpdate_, _similarityThreshold_, gum::FusionContext< isInitial >::addPair(), gum::FusionContext< isInitial >::associateLeaf(), gum::AbstractLeaf::contains(), gum::FusionContext< isInitial >::containsAssociatedLeaf(), gum::LeafPair::convert2Leaf(), and gum::LeafPair::likelyhood().

Here is the call graph for this function:

◆ updateLeaf()

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

Definition at line 166 of file leafAggregator.cpp.

166 {
167 // ***********************************************************************************
168 // First we update every base pair linked to that leaf
169 Set< LeafPair* > bag(*(_leaf2Pair_[l]));
170 for (SetIteratorSafe< LeafPair* > pairIter = bag.beginSafe(); pairIter != bag.endSafe();
171 ++pairIter) {
172 (*pairIter)->updateLikelyhood();
173 _updateInitialPair_(*pairIter);
174 }
175
176 // **********************************************************************************
177 // The we have top update FusionContext pairs associated to that leaf
178 AbstractLeaf* curLeaf = l;
179 for (SequenceIteratorSafe< FusionContext< false >* > fusIter = _fusionSeq_.beginSafe();
180 fusIter != _fusionSeq_.endSafe();
181 ++fusIter) {
182 if ((*fusIter)->leaf()->contains(curLeaf->id())) {
183 bag.clear();
184 if ((*fusIter)->updateAllAssociatedLeaves()) _removeContext_(fusIter.pos() + 1);
185 bag = (*fusIter)->associatedPairs();
186 curLeaf = (*fusIter)->leaf();
187 continue;
188 }
189
190 for (SetIteratorSafe< LeafPair* > pairIter = bag.beginSafe(); pairIter != bag.endSafe();
191 ++pairIter) {
192 if ((*fusIter)->leaf()->contains((*pairIter)->secondLeaf()->id())
193 || (*fusIter)->leaf()->contains((*pairIter)->firstLeaf()->id())) {
194 bag >> *pairIter;
195 continue;
196 }
197
198 if ((*fusIter)->updatePair(*pairIter)) _removeContext_(fusIter.pos() + 1);
199 }
200 if ((*fusIter)->updateAssociatedLeaf(curLeaf)) _removeContext_(fusIter.pos() + 1);
201 bag << (*fusIter)->leafAssociatedPair(curLeaf);
202 }
203
204 return _needsUpdate_;
205 }
void _updateInitialPair_(LeafPair *)

References _fusionSeq_, _leaf2Pair_, _needsUpdate_, _removeContext_(), _updateInitialPair_(), gum::Set< Key >::beginSafe(), gum::Set< Key >::clear(), gum::Set< Key >::endSafe(), and gum::AbstractLeaf::id().

Here is the call 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 153 of file leafAggregator.h.

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

◆ _leavesCpt_

NodeGraphPart* gum::LeafAggregator::_leavesCpt_
private

Definition at line 155 of file leafAggregator.h.

Referenced by LeafAggregator(), _removeContext_(), and update().

◆ _needsUpdate_

bool gum::LeafAggregator::_needsUpdate_
private

◆ _similarityThreshold_

double gum::LeafAggregator::_similarityThreshold_
private

Definition at line 157 of file leafAggregator.h.

Referenced by LeafAggregator(), and update().


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