aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
fusionContext_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40#pragma once
41
42
50// =======================================================
52
54
55// =======================================================
56
57namespace gum {
58
59 // ############################################################################
60 // Constructors / Destructors
61 // ############################################################################
62
63 // ============================================================================
64 // Default constructor.
65 // ============================================================================
66 template < bool isInitial >
70
71 // ============================================================================
72 // Default constructor.
73 // ============================================================================
74 template < bool isInitial >
76 GUM_DESTRUCTOR(FusionContext);
77
78 for (auto leafIter = _leaf2Pair_.beginSafe(); leafIter != _leaf2Pair_.endSafe(); ++leafIter)
79 delete leafIter.val();
80
81 delete _leaf_;
82 }
84 // ############################################################################
85 //
86 // ############################################################################
87
88 // ============================================================================
89 //
90 // ============================================================================
91 template < bool isInitial >
93 LeafPair* ptop = _pairsHeap_.empty() ? nullptr : _pairsHeap_.top();
94 ;
95 LeafPair* p = new LeafPair(l, _leaf_);
96 _leaf2Pair_.insert(l, p);
97 _leaf2Pair_[l]->updateLikelyhood();
98 _pairsHeap_.insert(p, p->likelyhood());
99
100 return ptop != _pairsHeap_.top();
101 }
102
103 // ============================================================================
104 //
105 // ============================================================================
106 template < bool isInitial >
108 LeafPair* ptop = _pairsHeap_.empty() ? nullptr : _pairsHeap_.top();
109 ;
110 _leaf2Pair_[l]->updateLikelyhood();
111 _pairsHeap_.setPriority(_leaf2Pair_[l], _leaf2Pair_[l]->likelyhood());
112
113 return ptop != _pairsHeap_.top();
114 }
115
116 // ============================================================================
117 //
118 // ============================================================================
119 template < bool isInitial >
121 LeafPair* ptop = _pairsHeap_.empty() ? nullptr : _pairsHeap_.top();
122 ;
124 pairIter != _leaf2Pair_.cendSafe();
125 ++pairIter) {
126 pairIter.val()->updateLikelyhood();
127 _pairsHeap_.setPriority(pairIter.val(), pairIter.val()->likelyhood());
128 }
129 LeafPair* ctop = _pairsHeap_.empty() ? nullptr : _pairsHeap_.top();
131 return ptop != ctop;
132 }
133
134 // ============================================================================
135 //
136 // ============================================================================
137 template < bool isInitial >
139 LeafPair* ptop = _pairsHeap_.empty() ? nullptr : _pairsHeap_.top();
140 _pairsHeap_.erase(_leaf2Pair_[l]);
141 _leaf2Pair_.erase(l);
142
143 LeafPair* ctop = _pairsHeap_.empty() ? nullptr : _pairsHeap_.top();
145 return ptop != ctop;
146 }
147
148 // ############################################################################
149 //
150 // ############################################################################
151
152 // ============================================================================
153 //
154 // ============================================================================
155 template < bool isInitial >
157 LeafPair* ptop = _pairsHeap_.empty() ? nullptr : _pairsHeap_.top();
158 _pairsHeap_.insert(p, p->likelyhood());
159
160 return ptop != _pairsHeap_.top();
161 }
162
163 // ============================================================================
164 //
165 // ============================================================================
166 template < bool isInitial >
168 LeafPair* ptop = _pairsHeap_.empty() ? nullptr : _pairsHeap_.top();
169 _pairsHeap_.setPriority(p, p->likelyhood());
170
171 return ptop != _pairsHeap_.top();
172 }
173
174 // ============================================================================
175 //
176 // ============================================================================
177 template < bool isInitial >
179 LeafPair* ptop = _pairsHeap_.empty() ? nullptr : _pairsHeap_.top();
180 _pairsHeap_.erase(p);
181
182 LeafPair* ctop = _pairsHeap_.empty() ? nullptr : _pairsHeap_.top();
184 return ptop != ctop;
185 }
186
187 // ############################################################################
188 //
189 // ############################################################################
190
191 // ============================================================================
192 //
193 // ============================================================================
194 template < bool isInitial >
196 Set< LeafPair* > retBag;
197 for (auto pairIter = _leaf2Pair_.beginSafe(); pairIter != _leaf2Pair_.endSafe(); ++pairIter)
198 retBag << pairIter.val();
199
200 return retBag;
201 }
202
203 template < bool isInitial >
205 std::stringstream ss;
206 if (_leaf_)
207 ss << "Associated Leaf : " << _leaf_->toString() << std::endl
208 << "Leaves Heap : " << std::endl;
209
210 // for( HashTableConstIteratorSafe<LeafPair*, std::vector<Size>>
211 // leafIter = _pairsHeap_.allValues().cbeginSafe();
212 // leafIter != _pairsHeap_.allValues().cendSafe(); ++leafIter ){
213 // ss << leafIter.key()->toString() << std::endl;
214 // }
215 if (!_pairsHeap_.empty()) ss << "Top pair : " << _pairsHeap_.top()->toString() << std::endl;
216
217 return ss.str();
218 }
219
220} // namespace gum
<agrum/FMDP/learning/datastructure/leaves/abstractLeaf.h>
bool addPair(LeafPair *p)
bool removePair(LeafPair *p)
Set< LeafPair * > _associatedPairs_(Int2Type< false >)
FusionContext(AbstractLeaf *)
Default constructor.
MultiPriorityQueue< LeafPair *, double, std::less< double > > _pairsHeap_
bool updatePair(LeafPair *p)
AbstractLeaf * leaf()
bool _deassociateLeaf_(AbstractLeaf *, Int2Type< false >)
bool _updateAllAssociatedLeaves_(Int2Type< false >)
~FusionContext()
Default destructor.
AbstractLeaf * _leaf_
bool _updateAssociatedLeaf_(AbstractLeaf *, Int2Type< false >)
bool _associateLeaf_(AbstractLeaf *, Int2Type< false >)
HashTable< AbstractLeaf *, LeafPair * > _leaf2Pair_
Safe Const Iterators for hashtables.
Definition hashTable.h:1602
<agrum/FMDP/learning/datastructure/leaves/leafPair.h>
Definition leafPair.h:69
double likelyhood()
Updates GStatistic.
Definition leafPair.cpp:95
Representation of a set.
Definition set.h:131
Headers of the Fusion Context class.
Useful macros for maths.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46