aGrUM
3.2.0
a C++ library for (probabilistic) graphical models
Toggle main menu visibility
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-2026 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-2026 *
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
41
#pragma once
42
43
51
// =======================================================
52
#include <
agrum/FMDP/learning/datastructure/leaves/fusionContext.h
>
53
54
#include <
agrum/base/core/math/math_utils.h
>
55
56
// =======================================================
57
58
namespace
gum
{
59
60
// ############################################################################
61
// Constructors / Destructors
62
// ############################################################################
63
64
// ============================================================================
65
// Default constructor.
66
// ============================================================================
67
template
<
bool
isInitial >
68
FusionContext< isInitial >::FusionContext
(
AbstractLeaf
*
leaf
) :
_leaf_
(
leaf
) {
69
GUM_CONSTRUCTOR(
FusionContext
);
70
}
71
72
// ============================================================================
73
// Default constructor.
74
// ============================================================================
75
template
<
bool
isInitial >
76
FusionContext< isInitial >::~FusionContext
() {
77
GUM_DESTRUCTOR(
FusionContext
);
78
79
for
(
auto
leafIter =
_leaf2Pair_
.beginSafe(); leafIter !=
_leaf2Pair_
.endSafe(); ++leafIter)
80
delete
leafIter.val();
81
82
delete
_leaf_
;
83
}
84
85
// ############################################################################
86
//
87
// ############################################################################
88
89
// ============================================================================
90
//
91
// ============================================================================
92
template
<
bool
isInitial >
93
bool
FusionContext< isInitial >::_associateLeaf_
(
AbstractLeaf
* l,
Int2Type< false >
) {
94
LeafPair
* ptop =
_pairsHeap_
.empty() ? nullptr :
_pairsHeap_
.top();
95
;
96
LeafPair
* p =
new
LeafPair
(l,
_leaf_
);
97
_leaf2Pair_
.insert(l, p);
98
_leaf2Pair_
[l]->updateLikelyhood();
99
_pairsHeap_
.insert(p, p->
likelyhood
());
100
101
return
ptop !=
_pairsHeap_
.top();
102
}
103
104
// ============================================================================
105
//
106
// ============================================================================
107
template
<
bool
isInitial >
108
bool
FusionContext< isInitial >::_updateAssociatedLeaf_
(
AbstractLeaf
* l,
Int2Type< false >
) {
109
LeafPair
* ptop =
_pairsHeap_
.empty() ? nullptr :
_pairsHeap_
.top();
110
;
111
_leaf2Pair_
[l]->
updateLikelyhood
();
112
_pairsHeap_
.setPriority(
_leaf2Pair_
[l],
_leaf2Pair_
[l]->likelyhood());
113
114
return
ptop !=
_pairsHeap_
.top();
115
}
116
117
// ============================================================================
118
//
119
// ============================================================================
120
template
<
bool
isInitial >
121
bool
FusionContext< isInitial >::_updateAllAssociatedLeaves_
(
Int2Type< false >
) {
122
LeafPair
* ptop =
_pairsHeap_
.empty() ? nullptr :
_pairsHeap_
.top();
123
;
124
for
(
HashTableConstIteratorSafe< AbstractLeaf*, LeafPair* >
pairIter =
_leaf2Pair_
.cbeginSafe();
125
pairIter !=
_leaf2Pair_
.cendSafe();
126
++pairIter) {
127
pairIter.val()->updateLikelyhood();
128
_pairsHeap_
.setPriority(pairIter.val(), pairIter.val()->likelyhood());
129
}
130
LeafPair
* ctop = _pairsHeap_.empty() ? nullptr : _pairsHeap_.top();
131
132
return
ptop != ctop;
133
}
134
135
// ============================================================================
136
//
137
// ============================================================================
138
template
<
bool
isInitial >
139
bool
FusionContext< isInitial >::_deassociateLeaf_
(
AbstractLeaf
* l,
Int2Type< false >
) {
140
LeafPair
* ptop =
_pairsHeap_
.empty() ? nullptr :
_pairsHeap_
.top();
141
_pairsHeap_
.erase(
_leaf2Pair_
[l]);
142
_leaf2Pair_
.erase(l);
143
144
LeafPair
* ctop =
_pairsHeap_
.empty() ? nullptr :
_pairsHeap_
.top();
145
146
return
ptop != ctop;
147
}
148
149
// ############################################################################
150
//
151
// ############################################################################
152
153
// ============================================================================
154
//
155
// ============================================================================
156
template
<
bool
isInitial >
157
bool
FusionContext< isInitial >::addPair
(
LeafPair
* p) {
158
LeafPair
* ptop =
_pairsHeap_
.empty() ? nullptr :
_pairsHeap_
.top();
159
_pairsHeap_
.insert(p, p->
likelyhood
());
160
161
return
ptop !=
_pairsHeap_
.top();
162
}
163
164
// ============================================================================
165
//
166
// ============================================================================
167
template
<
bool
isInitial >
168
bool
FusionContext< isInitial >::updatePair
(
LeafPair
* p) {
169
LeafPair
* ptop =
_pairsHeap_
.empty() ? nullptr :
_pairsHeap_
.top();
170
_pairsHeap_
.setPriority(p, p->
likelyhood
());
171
172
return
ptop !=
_pairsHeap_
.top();
173
}
174
175
// ============================================================================
176
//
177
// ============================================================================
178
template
<
bool
isInitial >
179
bool
FusionContext< isInitial >::removePair
(
LeafPair
* p) {
180
LeafPair
* ptop =
_pairsHeap_
.empty() ? nullptr :
_pairsHeap_
.top();
181
_pairsHeap_
.erase(p);
182
183
LeafPair
* ctop =
_pairsHeap_
.empty() ? nullptr :
_pairsHeap_
.top();
184
185
return
ptop != ctop;
186
}
187
188
// ############################################################################
189
//
190
// ############################################################################
191
192
// ============================================================================
193
//
194
// ============================================================================
195
template
<
bool
isInitial >
196
Set< LeafPair* >
FusionContext< isInitial >::_associatedPairs_
(
Int2Type< false >
) {
197
Set< LeafPair* >
retBag;
198
for
(
auto
pairIter =
_leaf2Pair_
.beginSafe(); pairIter !=
_leaf2Pair_
.endSafe(); ++pairIter)
199
retBag << pairIter.val();
200
201
return
retBag;
202
}
203
204
template
<
bool
isInitial >
205
std::string
FusionContext< isInitial >::toString
() {
206
std::string ss;
207
if
(
_leaf_
) ss = std::format(
"Associated Leaf : {}\nLeaves Heap : \n"
,
_leaf_
->toString());
208
209
// ss += std::format("{}\n", leafIter.key()->toString());
210
if
(!
_pairsHeap_
.empty()) ss += std::format(
"Top pair : {}\n"
,
_pairsHeap_
.top()->toString());
211
212
return
ss;
213
}
214
215
template
<
bool
isInitial >
216
void
*
FusionContext< isInitial >::operator
new
(
size_t
s) {
217
return
SmallObjectAllocator::instance
().
allocate
(s);
218
}
219
220
template
<
bool
isInitial >
221
void
FusionContext< isInitial >::operator
delete
(
void
* p) {
222
SmallObjectAllocator::instance
().
deallocate
(p,
sizeof
(
FusionContext
));
223
}
224
225
template
<
bool
isInitial >
226
bool
FusionContext< isInitial >::containsAssociatedLeaf
(
AbstractLeaf
* l) {
227
return
_containsAssociatedLeaf_
(l,
Int2Type< isInitial >
());
228
}
229
230
template
<
bool
isInitial >
231
bool
FusionContext< isInitial >::_containsAssociatedLeaf_
(
AbstractLeaf
* l,
Int2Type< false >
) {
232
return
_leaf2Pair_
.exists(l);
233
}
234
235
template
<
bool
isInitial >
236
bool
FusionContext< isInitial >::_containsAssociatedLeaf_
(
AbstractLeaf
*,
Int2Type< true >
) {
237
return
false
;
238
}
239
240
template
<
bool
isInitial >
241
bool
FusionContext< isInitial >::associateLeaf
(
AbstractLeaf
* l) {
242
return
_associateLeaf_
(l,
Int2Type< isInitial >
());
243
}
244
245
template
<
bool
isInitial >
246
bool
FusionContext< isInitial >::_associateLeaf_
(
AbstractLeaf
*,
Int2Type< true >
) {
247
return
false
;
248
}
249
250
template
<
bool
isInitial >
251
bool
FusionContext< isInitial >::updateAssociatedLeaf
(
AbstractLeaf
* l) {
252
return
_updateAssociatedLeaf_
(l,
Int2Type< isInitial >
());
253
}
254
255
template
<
bool
isInitial >
256
bool
FusionContext< isInitial >::_updateAssociatedLeaf_
(
AbstractLeaf
*,
Int2Type< true >
) {
257
return
false
;
258
}
259
260
template
<
bool
isInitial >
261
bool
FusionContext< isInitial >::updateAllAssociatedLeaves
() {
262
return
_updateAllAssociatedLeaves_
(
Int2Type< isInitial >
());
263
}
264
265
template
<
bool
isInitial >
266
bool
FusionContext< isInitial >::_updateAllAssociatedLeaves_
(
Int2Type< true >
) {
267
return
false
;
268
}
269
270
template
<
bool
isInitial >
271
bool
FusionContext< isInitial >::deassociateLeaf
(
AbstractLeaf
* l) {
272
return
_deassociateLeaf_
(l,
Int2Type< isInitial >
());
273
}
274
275
template
<
bool
isInitial >
276
bool
FusionContext< isInitial >::_deassociateLeaf_
(
AbstractLeaf
*,
Int2Type< true >
) {
277
return
false
;
278
}
279
280
template
<
bool
isInitial >
281
pair_iterator
FusionContext< isInitial >::beginPairs
() {
282
return
_pairsHeap_
.allValues().beginSafe();
283
}
284
285
template
<
bool
isInitial >
286
pair_iterator
FusionContext< isInitial >::endPairs
() {
287
return
_pairsHeap_
.allValues().endSafe();
288
}
289
290
template
<
bool
isInitial >
291
LeafPair
*
FusionContext< isInitial >::top
() {
292
return
!
_pairsHeap_
.empty() ?
_pairsHeap_
.top() :
nullptr
;
293
}
294
295
template
<
bool
isInitial >
296
double
FusionContext< isInitial >::topLikelyhood
() {
297
return
!
_pairsHeap_
.empty() ?
_pairsHeap_
.topPriority() : 1.0;
298
}
299
300
template
<
bool
isInitial >
301
AbstractLeaf
*
FusionContext< isInitial >::leaf
() {
302
return
_leaf_
;
303
}
304
305
template
<
bool
isInitial >
306
LeafPair
*
FusionContext< isInitial >::leafAssociatedPair
(
AbstractLeaf
* l) {
307
return
_leaf2Pair_
.getWithDefault(l,
nullptr
);
308
}
309
310
template
<
bool
isInitial >
311
Set< LeafPair* >
FusionContext< isInitial >::associatedPairs
() {
312
return
_associatedPairs_
(
Int2Type< isInitial >
());
313
}
314
315
template
<
bool
isInitial >
316
Set< LeafPair* >
FusionContext< isInitial >::_associatedPairs_
(
Int2Type< true >
) {
317
return
Set< LeafPair* >
();
318
}
319
320
}
// namespace gum
gum::AbstractLeaf
<agrum/FMDP/learning/datastructure/leaves/abstractLeaf.h>
Definition
abstractLeaf.h:72
gum::FusionContext::_containsAssociatedLeaf_
bool _containsAssociatedLeaf_(AbstractLeaf *l, Int2Type< false >)
Definition
fusionContext_tpl.h:231
gum::FusionContext::endPairs
pair_iterator endPairs()
Definition
fusionContext_tpl.h:286
gum::FusionContext::addPair
bool addPair(LeafPair *p)
Definition
fusionContext_tpl.h:157
gum::FusionContext::removePair
bool removePair(LeafPair *p)
Definition
fusionContext_tpl.h:179
gum::FusionContext::_associatedPairs_
Set< LeafPair * > _associatedPairs_(Int2Type< false >)
Definition
fusionContext_tpl.h:196
gum::FusionContext::FusionContext
FusionContext(AbstractLeaf *)
Default constructor.
Definition
fusionContext_tpl.h:68
gum::FusionContext::_pairsHeap_
MultiPriorityQueue< LeafPair *, double, std::less< double > > _pairsHeap_
Definition
fusionContext.h:241
gum::FusionContext::updatePair
bool updatePair(LeafPair *p)
Definition
fusionContext_tpl.h:168
gum::FusionContext::updateAllAssociatedLeaves
bool updateAllAssociatedLeaves()
Definition
fusionContext_tpl.h:261
gum::FusionContext::leaf
AbstractLeaf * leaf()
Definition
fusionContext_tpl.h:301
gum::FusionContext::updateAssociatedLeaf
bool updateAssociatedLeaf(AbstractLeaf *l)
Definition
fusionContext_tpl.h:251
gum::FusionContext::topLikelyhood
double topLikelyhood()
Definition
fusionContext_tpl.h:296
gum::FusionContext::_deassociateLeaf_
bool _deassociateLeaf_(AbstractLeaf *, Int2Type< false >)
Definition
fusionContext_tpl.h:139
gum::FusionContext::leafAssociatedPair
LeafPair * leafAssociatedPair(AbstractLeaf *l)
Definition
fusionContext_tpl.h:306
gum::FusionContext::_updateAllAssociatedLeaves_
bool _updateAllAssociatedLeaves_(Int2Type< false >)
Definition
fusionContext_tpl.h:121
gum::FusionContext::containsAssociatedLeaf
bool containsAssociatedLeaf(AbstractLeaf *l)
Definition
fusionContext_tpl.h:226
gum::FusionContext::~FusionContext
~FusionContext()
Default destructor.
Definition
fusionContext_tpl.h:76
gum::FusionContext::top
LeafPair * top()
Definition
fusionContext_tpl.h:291
gum::FusionContext::_leaf_
AbstractLeaf * _leaf_
Definition
fusionContext.h:245
gum::FusionContext::deassociateLeaf
bool deassociateLeaf(AbstractLeaf *l)
Definition
fusionContext_tpl.h:271
gum::FusionContext::beginPairs
pair_iterator beginPairs()
Definition
fusionContext_tpl.h:281
gum::FusionContext::associatedPairs
Set< LeafPair * > associatedPairs()
Definition
fusionContext_tpl.h:311
gum::FusionContext::_updateAssociatedLeaf_
bool _updateAssociatedLeaf_(AbstractLeaf *, Int2Type< false >)
Definition
fusionContext_tpl.h:108
gum::FusionContext::associateLeaf
bool associateLeaf(AbstractLeaf *l)
Definition
fusionContext_tpl.h:241
gum::FusionContext::toString
std::string toString()
Definition
fusionContext_tpl.h:205
gum::FusionContext::_associateLeaf_
bool _associateLeaf_(AbstractLeaf *, Int2Type< false >)
Definition
fusionContext_tpl.h:93
gum::FusionContext::_leaf2Pair_
HashTable< AbstractLeaf *, LeafPair * > _leaf2Pair_
Definition
fusionContext.h:243
gum::HashTableConstIteratorSafe
Safe Const Iterators for hashtables.
Definition
hashTable.h:1662
gum::LeafPair
<agrum/FMDP/learning/datastructure/leaves/leafPair.h>
Definition
leafPair.h:69
gum::LeafPair::LeafPair
LeafPair(AbstractLeaf *l1, AbstractLeaf *l2)
Default constructor.
Definition
leafPair_inl.h:48
gum::LeafPair::updateLikelyhood
void updateLikelyhood()
Updates GStatistic.
Definition
leafPair.cpp:68
gum::LeafPair::likelyhood
double likelyhood()
Updates GStatistic.
Definition
leafPair.cpp:99
gum::Set
Representation of a set.
Definition
set.h:129
gum::Set::Set
Set(Size capacity=HashTableConst::default_size, bool resize_policy=true)
Default constructor.
Definition
set_tpl.h:277
gum::SmallObjectAllocator::instance
static SmallObjectAllocator & instance()
Definition
smallObjectAllocator_inl.h:96
gum::SmallObjectAllocator::allocate
void * allocate(const size_t &objectSize)
Allocates a block.
Definition
smallObjectAllocator.cpp:72
gum::SmallObjectAllocator::deallocate
void deallocate(void *pDeallocatedObject, const size_t &objectSize)
Deallocates an object.
Definition
smallObjectAllocator_inl.h:116
fusionContext.h
Headers of the Fusion Context class.
math_utils.h
Useful macros for maths.
gum
gum is the global namespace for all aGrUM entities
Definition
agrum.h:46
gum::pair_iterator
HashTableConstIteratorSafe< LeafPair *, std::vector< Size > > pair_iterator
Definition
fusionContext.h:70
gum::Int2Type
Definition
templateStrategy.h:56
aGrUM
3.2.0
© PHW&CG&others - 2022
DoXyGeN 1.18.0