aGrUM 2.3.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.
 ~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 65 of file leafAggregator.cpp.

65 :
66 _leavesCpt_(idSource), _similarityThreshold_(similarityThreshold) {
67 GUM_CONSTRUCTOR(LeafAggregator);
68 _initialContext_ = new FusionContext< true >(nullptr);
69 _needsUpdate_ = false;
70 }
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 75 of file leafAggregator.cpp.

75 {
77
78 delete _initialContext_;
79
80 for (HashTableIteratorSafe< AbstractLeaf*, Set< LeafPair* >* > leafIter
81 = _leaf2Pair_.beginSafe();
82 leafIter != _leaf2Pair_.endSafe();
83 ++leafIter) {
84 for (SetIteratorSafe< LeafPair* > pairIter = leafIter.val()->beginSafe();
85 pairIter != leafIter.val()->endSafe();
86 ++pairIter) {
87 LeafPair* curPair = *pairIter;
88 _leaf2Pair_[curPair->otherLeaf(leafIter.key())]->erase(*pairIter);
89 leafIter.val()->erase(curPair);
90 delete curPair;
91 }
92 delete leafIter.val();
93 }
94
95
96 GUM_DESTRUCTOR(LeafAggregator);
97 }
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 358 of file leafAggregator.cpp.

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

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

345 {
346 for (Idx i = _fusionSeq_.size() - 1; !_fusionSeq_.empty() && i >= startingPos; --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 }
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 374 of file leafAggregator.cpp.

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

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

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

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

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

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

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()

bool gum::LeafAggregator::needsUpdate ( )
inline

Definition at line 137 of file leafAggregator.h.

137{ return _needsUpdate_; }

References _needsUpdate_.

◆ next()

double gum::LeafAggregator::next ( )

◆ removeLeaf()

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

Definition at line 206 of file leafAggregator.cpp.

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

325 {
326 std::stringstream ss;
327 ss << "################\nTas Initial : " << std::endl
328 << _initialContext_->toString() << std::endl;
329
330 for (auto fusIter = _fusionSeq_.beginSafe(); fusIter != _fusionSeq_.endSafe(); ++fusIter) {
331 ss << "################\nTas " << fusIter.pos() << " : " << std::endl
332 << (*fusIter)->toString();
333 }
334
335 return ss.str();
336 }

References _fusionSeq_, and _initialContext_.

◆ update()

void gum::LeafAggregator::update ( )

Definition at line 257 of file leafAggregator.cpp.

257 {
258 LeafPair* nextPair = _initialContext_->top();
259 pair_iterator pb = _initialContext_->beginPairs();
260 pair_iterator pe = _initialContext_->endPairs();
261 if (!_fusionSeq_.empty()) {
262 nextPair = _fusionSeq_.back()->top();
263 pb = _fusionSeq_.back()->beginPairs();
264 pe = _fusionSeq_.back()->endPairs();
265 }
266
267
268 while (nextPair && nextPair->likelyhood() < _similarityThreshold_) {
269 AbstractLeaf* newLeaf = nextPair->convert2Leaf(_leavesCpt_->addNode());
270 FusionContext< false >* newContext = new FusionContext< false >(newLeaf);
271
272 for (pair_iterator pairIter = pb; pairIter != pe; ++pairIter) {
273 if (!newLeaf->contains(pairIter.key()->firstLeaf()->id())
274 && !newLeaf->contains(pairIter.key()->secondLeaf()->id()))
275 newContext->addPair(pairIter.key());
276 if (!newLeaf->contains(pairIter.key()->firstLeaf()->id())
277 && !newContext->containsAssociatedLeaf(pairIter.key()->firstLeaf()))
278 newContext->associateLeaf(pairIter.key()->firstLeaf());
279 if (!newLeaf->contains(pairIter.key()->secondLeaf()->id())
280 && !newContext->containsAssociatedLeaf(pairIter.key()->secondLeaf()))
281 newContext->associateLeaf(pairIter.key()->secondLeaf());
282 }
283
284 _fusionSeq_.insert(newContext);
285 nextPair = _fusionSeq_.back()->top();
286 pb = _fusionSeq_.back()->beginPairs();
287 pe = _fusionSeq_.back()->endPairs();
288 }
289 _needsUpdate_ = false;
290 }
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 162 of file leafAggregator.cpp.

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