aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
fusionContext_tpl.h
Go to the documentation of this file.
1
/**
2
*
3
* Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4
* info_at_agrum_dot_org
5
*
6
* This library is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU Lesser General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
10
*
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU Lesser General Public License for more details.
15
*
16
* You should have received a copy of the GNU Lesser General Public License
17
* along with this library. If not, see <http://www.gnu.org/licenses/>.
18
*
19
*/
20
21
22
/**
23
* @file
24
* @brief Templates for fusion context class
25
*
26
* @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
27
* GONZALES(@AMU)
28
*/
29
// =======================================================
30
#
include
<
agrum
/
tools
/
core
/
math
/
math_utils
.
h
>
31
#
include
<
agrum
/
FMDP
/
learning
/
datastructure
/
leaves
/
fusionContext
.
h
>
32
// =======================================================
33
34
namespace
gum
{
35
36
// ############################################################################
37
// Constructors / Destructors
38
// ############################################################################
39
40
// ============================================================================
41
// Default constructor.
42
// ============================================================================
43
template
<
bool
isInitial >
44
FusionContext< isInitial >::FusionContext(AbstractLeaf* leaf) : _leaf_(leaf) {
45
GUM_CONSTRUCTOR(FusionContext);
46
}
47
48
// ============================================================================
49
// Default constructor.
50
// ============================================================================
51
template
<
bool
isInitial
>
52
FusionContext
<
isInitial
>::~
FusionContext
() {
53
GUM_DESTRUCTOR
(
FusionContext
);
54
55
for
(
auto
leafIter
=
_leaf2Pair_
.
beginSafe
();
leafIter
!=
_leaf2Pair_
.
endSafe
(); ++
leafIter
)
56
delete
leafIter
.
val
();
57
58
delete
_leaf_
;
59
}
60
61
// ############################################################################
62
//
63
// ############################################################################
64
65
// ============================================================================
66
//
67
// ============================================================================
68
template
<
bool
isInitial
>
69
bool
FusionContext
<
isInitial
>::
_associateLeaf_
(
AbstractLeaf
*
l
,
Int2Type
<
false
>) {
70
LeafPair
*
ptop
=
_pairsHeap_
.
empty
() ?
nullptr
:
_pairsHeap_
.
top
();
71
;
72
LeafPair
*
p
=
new
LeafPair
(
l
,
_leaf_
);
73
_leaf2Pair_
.
insert
(
l
,
p
);
74
_leaf2Pair_
[
l
]->
updateLikelyhood
();
75
_pairsHeap_
.
insert
(
p
,
p
->
likelyhood
());
76
77
return
ptop
!=
_pairsHeap_
.
top
();
78
}
79
80
// ============================================================================
81
//
82
// ============================================================================
83
template
<
bool
isInitial
>
84
bool
FusionContext
<
isInitial
>::
_updateAssociatedLeaf_
(
AbstractLeaf
*
l
,
Int2Type
<
false
>) {
85
LeafPair
*
ptop
=
_pairsHeap_
.
empty
() ?
nullptr
:
_pairsHeap_
.
top
();
86
;
87
_leaf2Pair_
[
l
]->
updateLikelyhood
();
88
_pairsHeap_
.
setPriority
(
_leaf2Pair_
[
l
],
_leaf2Pair_
[
l
]->
likelyhood
());
89
90
return
ptop
!=
_pairsHeap_
.
top
();
91
}
92
93
// ============================================================================
94
//
95
// ============================================================================
96
template
<
bool
isInitial
>
97
bool
FusionContext
<
isInitial
>::
_updateAllAssociatedLeaves_
(
Int2Type
<
false
>) {
98
LeafPair
*
ptop
=
_pairsHeap_
.
empty
() ?
nullptr
:
_pairsHeap_
.
top
();
99
;
100
for
(
HashTableConstIteratorSafe
<
AbstractLeaf
*,
LeafPair
* >
pairIter
=
_leaf2Pair_
.
cbeginSafe
();
101
pairIter
!=
_leaf2Pair_
.
cendSafe
();
102
++
pairIter
) {
103
pairIter
.
val
()->
updateLikelyhood
();
104
_pairsHeap_
.
setPriority
(
pairIter
.
val
(),
pairIter
.
val
()->
likelyhood
());
105
}
106
LeafPair
*
ctop
=
_pairsHeap_
.
empty
() ?
nullptr
:
_pairsHeap_
.
top
();
107
108
return
ptop
!=
ctop
;
109
}
110
111
// ============================================================================
112
//
113
// ============================================================================
114
template
<
bool
isInitial
>
115
bool
FusionContext
<
isInitial
>::
_deassociateLeaf_
(
AbstractLeaf
*
l
,
Int2Type
<
false
>) {
116
LeafPair
*
ptop
=
_pairsHeap_
.
empty
() ?
nullptr
:
_pairsHeap_
.
top
();
117
_pairsHeap_
.
erase
(
_leaf2Pair_
[
l
]);
118
_leaf2Pair_
.
erase
(
l
);
119
120
LeafPair
*
ctop
=
_pairsHeap_
.
empty
() ?
nullptr
:
_pairsHeap_
.
top
();
121
122
return
ptop
!=
ctop
;
123
}
124
125
126
// ############################################################################
127
//
128
// ############################################################################
129
130
// ============================================================================
131
//
132
// ============================================================================
133
template
<
bool
isInitial
>
134
bool
FusionContext
<
isInitial
>::
addPair
(
LeafPair
*
p
) {
135
LeafPair
*
ptop
=
_pairsHeap_
.
empty
() ?
nullptr
:
_pairsHeap_
.
top
();
136
_pairsHeap_
.
insert
(
p
,
p
->
likelyhood
());
137
138
return
ptop
!=
_pairsHeap_
.
top
();
139
}
140
141
// ============================================================================
142
//
143
// ============================================================================
144
template
<
bool
isInitial
>
145
bool
FusionContext
<
isInitial
>::
updatePair
(
LeafPair
*
p
) {
146
LeafPair
*
ptop
=
_pairsHeap_
.
empty
() ?
nullptr
:
_pairsHeap_
.
top
();
147
_pairsHeap_
.
setPriority
(
p
,
p
->
likelyhood
());
148
149
return
ptop
!=
_pairsHeap_
.
top
();
150
}
151
152
// ============================================================================
153
//
154
// ============================================================================
155
template
<
bool
isInitial
>
156
bool
FusionContext
<
isInitial
>::
removePair
(
LeafPair
*
p
) {
157
LeafPair
*
ptop
=
_pairsHeap_
.
empty
() ?
nullptr
:
_pairsHeap_
.
top
();
158
_pairsHeap_
.
erase
(
p
);
159
160
LeafPair
*
ctop
=
_pairsHeap_
.
empty
() ?
nullptr
:
_pairsHeap_
.
top
();
161
162
return
ptop
!=
ctop
;
163
}
164
165
166
// ############################################################################
167
//
168
// ############################################################################
169
170
// ============================================================================
171
//
172
// ============================================================================
173
template
<
bool
isInitial
>
174
Set
<
LeafPair
* >
FusionContext
<
isInitial
>::
_associatedPairs_
(
Int2Type
<
false
>) {
175
Set
<
LeafPair
* >
retBag
;
176
for
(
auto
pairIter
=
_leaf2Pair_
.
beginSafe
();
pairIter
!=
_leaf2Pair_
.
endSafe
(); ++
pairIter
)
177
retBag
<<
pairIter
.
val
();
178
179
return
retBag
;
180
}
181
182
183
template
<
bool
isInitial
>
184
std
::
string
FusionContext
<
isInitial
>::
toString
() {
185
std
::
stringstream
ss
;
186
if
(
_leaf_
)
187
ss
<<
"Associated Leaf : "
<<
_leaf_
->
toString
() <<
std
::
endl
188
<<
"Leaves Heap : "
<<
std
::
endl
;
189
190
// for( HashTableConstIteratorSafe<LeafPair*, std::vector<Size>>
191
// leafIter = _pairsHeap_.allValues().cbeginSafe();
192
// leafIter != _pairsHeap_.allValues().cendSafe(); ++leafIter ){
193
// ss << leafIter.key()->toString() << std::endl;
194
// }
195
if
(!
_pairsHeap_
.
empty
())
ss
<<
"Top pair : "
<<
_pairsHeap_
.
top
()->
toString
() <<
std
::
endl
;
196
197
return
ss
.
str
();
198
}
199
200
}
// namespace gum
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643