aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
SVED_tpl.h
Go to the documentation of this file.
1
/**
2
*
3
* Copyright 2005-2020 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 Inline implementation of SVED.
25
*
26
* @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27
*/
28
#
include
<
agrum
/
PRM
/
inference
/
SVED
.
h
>
29
30
namespace
gum
{
31
namespace
prm
{
32
33
template
<
typename
GUM_SCALAR
>
34
SVED
<
GUM_SCALAR
>::~
SVED
() {
35
GUM_DESTRUCTOR
(
SVED
);
36
37
for
(
const
auto
&
elt
:
elim_orders__
)
38
delete
elt
.
second
;
39
40
if
(
class_elim_order__
!=
nullptr
)
delete
class_elim_order__
;
41
}
42
43
template
<
typename
GUM_SCALAR
>
44
void
45
SVED
<
GUM_SCALAR
>::
eliminateNodes__
(
const
PRMInstance
<
GUM_SCALAR
>*
query
,
46
NodeId
node
,
47
BucketSet
&
pool
,
48
BucketSet
&
trash
) {
49
Set
<
const
PRMInstance
<
GUM_SCALAR
>* >
ignore
;
50
ignore
.
insert
(
query
);
51
// Extracting required attributes and slotchains
52
Set
<
NodeId
>&
attr_set
=
getAttrSet__
(
query
);
53
Set
<
NodeId
>&
sc_set
=
getSCSet__
(
query
);
54
// Downward elimination
55
List
<
const
PRMInstance
<
GUM_SCALAR
>* >
elim_list
;
56
57
for
(
const
auto
attr
:
attr_set
) {
58
try
{
59
for
(
auto
iter
=
query
->
getRefAttr
(
attr
).
begin
();
60
iter
!=
query
->
getRefAttr
(
attr
).
end
();
61
++
iter
)
62
if
((!
ignore
.
exists
(
iter
->
first
)) && (
bb__
.
exists
(
iter
->
first
)))
63
eliminateNodesDownward__
(
query
,
64
iter
->
first
,
65
pool
,
66
trash
,
67
elim_list
,
68
ignore
);
69
}
catch
(
NotFound
&) {
70
// Ok
71
}
72
}
73
74
// Eliminating all nodes in query instance, except query
75
InstanceBayesNet
<
GUM_SCALAR
>
bn
(*
query
);
76
DefaultTriangulation
t
(&(
bn
.
moralGraph
()), &(
bn
.
modalities
()));
77
std
::
vector
<
const
DiscreteVariable
* >
elim_order
;
78
79
if
(
this
->
hasEvidence
(
query
))
insertEvidence__
(
query
,
pool
);
80
81
for
(
const
auto
attr
:
attr_set
)
82
pool
.
insert
(
83
&(
const_cast
<
Potential
<
GUM_SCALAR
>& >(
query
->
get
(
attr
).
cpf
())));
84
85
for
(
size_t
idx
= 0;
idx
<
t
.
eliminationOrder
().
size
(); ++
idx
) {
86
if
(
t
.
eliminationOrder
()[
idx
] !=
node
) {
87
auto
var_id
=
t
.
eliminationOrder
()[
idx
];
88
const
auto
&
var
=
bn
.
variable
(
var_id
);
89
elim_order
.
push_back
(&
var
);
90
}
91
}
92
93
eliminateNodes
(
elim_order
,
pool
,
trash
);
94
// Eliminating instance in elim_list
95
List
<
const
PRMInstance
<
GUM_SCALAR
>* >
tmp_list
;
96
reduceElimList__
(
query
,
elim_list
,
tmp_list
,
ignore
,
pool
,
trash
);
97
98
while
(!
elim_list
.
empty
()) {
99
if
(
checkElimOrder__
(
query
,
elim_list
.
front
())) {
100
if
((!
ignore
.
exists
(
elim_list
.
front
()))
101
&& (
bb__
.
exists
(
elim_list
.
front
())))
102
eliminateNodesDownward__
(
query
,
103
elim_list
.
front
(),
104
pool
,
105
trash
,
106
elim_list
,
107
ignore
);
108
}
else
if
(
bb__
.
exists
(
elim_list
.
front
())) {
109
tmp_list
.
insert
(
elim_list
.
front
());
110
}
111
112
elim_list
.
popFront
();
113
}
114
115
// Upward elimination
116
for
(
const
auto
chain
:
sc_set
)
117
for
(
const
auto
parent
:
query
->
getInstances
(
chain
))
118
if
((!
ignore
.
exists
(
parent
)) && (
bb__
.
exists
(*
parent
)))
119
eliminateNodesUpward__
(
parent
,
pool
,
trash
,
tmp_list
,
ignore
);
120
}
121
122
template
<
typename
GUM_SCALAR
>
123
void
SVED
<
GUM_SCALAR
>::
eliminateNodesDownward__
(
124
const
PRMInstance
<
GUM_SCALAR
>*
from
,
125
const
PRMInstance
<
GUM_SCALAR
>*
i
,
126
BucketSet
&
pool
,
127
BucketSet
&
trash
,
128
List
<
const
PRMInstance
<
GUM_SCALAR
>* >&
elim_list
,
129
Set
<
const
PRMInstance
<
GUM_SCALAR
>* >&
ignore
) {
130
ignore
.
insert
(
i
);
131
// Extracting required attributes and slotchains
132
Set
<
NodeId
>&
attr_set
=
getAttrSet__
(
i
);
133
Set
<
NodeId
>&
sc_set
=
getSCSet__
(
i
);
134
// Calling elimination over child instance
135
List
<
const
PRMInstance
<
GUM_SCALAR
>* >
my_list
;
136
137
for
(
const
auto
attr
:
attr_set
) {
138
try
{
139
for
(
auto
iter
=
i
->
getRefAttr
(
attr
).
begin
();
140
iter
!=
i
->
getRefAttr
(
attr
).
end
();
141
++
iter
)
142
if
((!
ignore
.
exists
(
iter
->
first
)) && (
bb__
.
exists
(
iter
->
first
)))
143
eliminateNodesDownward__
(
i
,
144
iter
->
first
,
145
pool
,
146
trash
,
147
my_list
,
148
ignore
);
149
}
catch
(
NotFound
&) {
150
// Ok
151
}
152
}
153
154
// Eliminating all nodes in current instance
155
if
(
this
->
hasEvidence
(
i
)) {
156
eliminateNodesWithEvidence__
(
i
,
pool
,
trash
);
157
}
else
{
158
insertLiftedNodes__
(
i
,
pool
,
trash
);
159
160
for
(
const
auto
agg
:
i
->
type
().
aggregates
())
161
if
(
bb__
.
requisiteNodes
(
i
).
exists
(
agg
->
id
()))
162
pool
.
insert
(
getAggPotential__
(
i
,
agg
));
163
164
try
{
165
InstanceBayesNet
<
GUM_SCALAR
>
bn
(*
i
);
166
std
::
vector
<
const
DiscreteVariable
* >
elim_order
;
167
168
for
(
auto
node
:
getElimOrder__
(
i
->
type
())) {
169
const
auto
&
var
=
bn
.
variable
(
node
);
170
elim_order
.
push_back
(&
var
);
171
}
172
173
eliminateNodes
(
elim_order
,
pool
,
trash
);
174
}
catch
(
NotFound
&) {
175
// Raised if there is no inner nodes to eliminate
176
}
177
}
178
179
// Calling elimination over child's parents
180
while
(!
my_list
.
empty
()) {
181
if
(
checkElimOrder__
(
i
,
my_list
.
front
())) {
182
if
((!
ignore
.
exists
(
my_list
.
front
())) && (
bb__
.
exists
(
my_list
.
front
())))
183
eliminateNodesDownward__
(
i
,
184
my_list
.
front
(),
185
pool
,
186
trash
,
187
my_list
,
188
ignore
);
189
}
else
if
(
bb__
.
exists
(
my_list
.
front
())) {
190
elim_list
.
insert
(
my_list
.
front
());
191
}
192
193
my_list
.
popFront
();
194
}
195
196
// Adding parents instance to elim_list
197
for
(
const
auto
chain
:
sc_set
)
198
for
(
const
auto
parent
:
i
->
getInstances
(
chain
))
199
if
((!
ignore
.
exists
(
parent
)) &&
bb__
.
exists
(
parent
) && (
parent
!=
from
))
200
elim_list
.
insert
(
parent
);
201
}
202
203
template
<
typename
GUM_SCALAR
>
204
void
SVED
<
GUM_SCALAR
>::
eliminateNodesUpward__
(
205
const
PRMInstance
<
GUM_SCALAR
>*
i
,
206
BucketSet
&
pool
,
207
BucketSet
&
trash
,
208
List
<
const
PRMInstance
<
GUM_SCALAR
>* >&
elim_list
,
209
Set
<
const
PRMInstance
<
GUM_SCALAR
>* >&
ignore
) {
210
ignore
.
insert
(
i
);
211
// Extracting required attributes and slotchains
212
Set
<
NodeId
>&
attr_set
=
getAttrSet__
(
i
);
213
Set
<
NodeId
>&
sc_set
=
getSCSet__
(
i
);
214
215
// Downward elimination
216
for
(
const
auto
attr
:
attr_set
) {
217
try
{
218
for
(
auto
iter
=
i
->
getRefAttr
(
attr
).
begin
();
219
iter
!=
i
->
getRefAttr
(
attr
).
end
();
220
++
iter
)
221
if
((!
ignore
.
exists
(
iter
->
first
)) && (
bb__
.
exists
(
iter
->
first
)))
222
eliminateNodesDownward__
(
i
,
223
iter
->
first
,
224
pool
,
225
trash
,
226
elim_list
,
227
ignore
);
228
}
catch
(
NotFound
&) {
229
// Ok
230
}
231
}
232
233
// Eliminating all nodes in i instance
234
if
(
this
->
hasEvidence
(
i
)) {
235
eliminateNodesWithEvidence__
(
i
,
pool
,
trash
);
236
}
else
{
237
insertLiftedNodes__
(
i
,
pool
,
trash
);
238
239
for
(
const
auto
agg
:
i
->
type
().
aggregates
())
240
if
(
bb__
.
requisiteNodes
(
i
).
exists
(
agg
->
id
()))
241
pool
.
insert
(
getAggPotential__
(
i
,
agg
));
242
243
try
{
244
InstanceBayesNet
<
GUM_SCALAR
>
bn
(*
i
);
245
std
::
vector
<
const
DiscreteVariable
* >
elim_order
;
246
247
for
(
auto
node
:
getElimOrder__
(
i
->
type
())) {
248
const
auto
&
var
=
bn
.
variable
(
node
);
249
elim_order
.
push_back
(&
var
);
250
}
251
eliminateNodes
(
elim_order
,
pool
,
trash
);
252
}
catch
(
NotFound
&) {
253
// Raised if there is no inner nodes to eliminate
254
}
255
}
256
257
// Eliminating instance in elim_list
258
List
<
const
PRMInstance
<
GUM_SCALAR
>* >
tmp_list
;
259
260
while
(!
elim_list
.
empty
()) {
261
if
(
checkElimOrder__
(
i
,
elim_list
.
front
())) {
262
if
((!
ignore
.
exists
(
elim_list
.
front
()))
263
&& (
bb__
.
exists
(
elim_list
.
front
())))
264
eliminateNodesDownward__
(
i
,
265
elim_list
.
front
(),
266
pool
,
267
trash
,
268
elim_list
,
269
ignore
);
270
}
else
if
(
bb__
.
exists
(
elim_list
.
front
())) {
271
ignore
.
insert
(
elim_list
.
front
());
272
}
273
274
elim_list
.
popFront
();
275
}
276
277
// Upward elimination
278
for
(
const
auto
chain
:
sc_set
)
279
for
(
const
auto
parent
:
i
->
getInstances
(
chain
))
280
if
((!
ignore
.
exists
(
parent
)) && (
bb__
.
exists
(
parent
)))
281
eliminateNodesUpward__
(
parent
,
pool
,
trash
,
tmp_list
,
ignore
);
282
}
283
284
template
<
typename
GUM_SCALAR
>
285
void
SVED
<
GUM_SCALAR
>::
eliminateNodesWithEvidence__
(
286
const
PRMInstance
<
GUM_SCALAR
>*
i
,
287
BucketSet
&
pool
,
288
BucketSet
&
trash
) {
289
// Adding required evidences
290
for
(
const
auto
&
elt
:
this
->
evidence
(
i
))
291
if
(
bb__
.
requisiteNodes
(
i
).
exists
(
elt
.
first
))
292
pool
.
insert
(
const_cast
<
Potential
<
GUM_SCALAR
>* >(
elt
.
second
));
293
294
// Adding potentials and eliminating the remaining nodes
295
for
(
const
auto
&
a
: *
i
)
296
if
(
bb__
.
requisiteNodes
(
i
).
exists
(
a
.
first
))
297
pool
.
insert
(&(
const_cast
<
Potential
<
GUM_SCALAR
>& >(
a
.
second
->
cpf
())));
298
299
InstanceBayesNet
<
GUM_SCALAR
>
bn
(*
i
);
300
DefaultTriangulation
t
(&(
bn
.
moralGraph
()), &(
bn
.
modalities
()));
301
const
std
::
vector
<
NodeId
>&
full_elim_order
=
t
.
eliminationOrder
();
302
303
for
(
auto
var
=
full_elim_order
.
begin
();
var
!=
full_elim_order
.
end
(); ++
var
)
304
eliminateNode
(&(
i
->
get
(*
var
).
type
().
variable
()),
pool
,
trash
);
305
}
306
307
template
<
typename
GUM_SCALAR
>
308
void
309
SVED
<
GUM_SCALAR
>::
insertLiftedNodes__
(
const
PRMInstance
<
GUM_SCALAR
>*
i
,
310
BucketSet
&
pool
,
311
BucketSet
&
trash
) {
312
BucketSet
*
lifted_pool
=
nullptr
;
313
314
try
{
315
lifted_pool
=
lifted_pools__
[&(
bb__
.
requisiteNodes
(
i
))];
316
}
catch
(
NotFound
&) {
317
initLiftedNodes__
(
i
,
trash
);
318
lifted_pool
=
lifted_pools__
[&(
bb__
.
requisiteNodes
(
i
))];
319
}
320
321
for
(
const
auto
lifted_pot
: *
lifted_pool
) {
322
Potential
<
GUM_SCALAR
>*
pot
=
copyPotential
(
i
->
bijection
(), *
lifted_pot
);
323
pool
.
insert
(
pot
);
324
trash
.
insert
(
pot
);
325
}
326
}
327
328
template
<
typename
GUM_SCALAR
>
329
void
SVED
<
GUM_SCALAR
>::
initLiftedNodes__
(
const
PRMInstance
<
GUM_SCALAR
>*
i
,
330
BucketSet
&
trash
) {
331
PRMClass
<
GUM_SCALAR
>&
c
=
const_cast
<
PRMClass
<
GUM_SCALAR
>& >(
i
->
type
());
332
BucketSet
*
lifted_pool
=
new
BucketSet
();
333
lifted_pools__
.
insert
(&(
bb__
.
requisiteNodes
(
i
)),
lifted_pool
);
334
335
for
(
const
auto
node
:
bb__
.
requisiteNodes
(
i
))
336
if
(
PRMClassElement
<
GUM_SCALAR
>::
isAttribute
(
c
.
get
(
node
)))
337
lifted_pool
->
insert
(
338
const_cast
<
Potential
<
GUM_SCALAR
>* >(&(
c
.
get
(
node
).
cpf
())));
339
340
NodeSet
inners
,
outers
,
ignore
;
341
342
for
(
const
auto
&
elt
: *
i
) {
343
if
(
bb__
.
requisiteNodes
(*
i
).
exists
(
elt
.
first
)) {
344
if
(
PRMClassElement
<
GUM_SCALAR
>::
isAttribute
(
c
.
get
(
elt
.
first
))) {
345
if
(
c
.
isOutputNode
(
c
.
get
(
elt
.
first
)))
346
outers
.
insert
(
elt
.
first
);
347
else
if
(!
outers
.
exists
(
elt
.
first
))
348
inners
.
insert
(
elt
.
first
);
349
}
else
if
(
PRMClassElement
<
GUM_SCALAR
>::
isAggregate
(
350
c
.
get
(
elt
.
first
))) {
351
outers
.
insert
(
elt
.
first
);
352
353
// We need to put in the output_elim_order aggregator's parents
354
// which are
355
// innner nodes
356
for
(
const
auto
par
:
c
.
containerDag
().
parents
(
elt
.
first
))
357
if
(
PRMClassElement
<
GUM_SCALAR
>::
isAttribute
(
i
->
type
().
get
(
par
))
358
&&
i
->
type
().
isInnerNode
(
i
->
type
().
get
(
par
))
359
&&
bb__
.
requisiteNodes
(
i
).
exists
(
par
)) {
360
inners
.
erase
(
par
);
361
outers
.
insert
(
par
);
362
}
363
}
364
}
else
{
365
ignore
.
insert
(
elt
.
first
);
366
}
367
}
368
369
// Now we proceed with the elimination of inner attributes
370
ClassBayesNet
<
GUM_SCALAR
>
bn
(
c
);
371
List
<
NodeSet
>
partial_ordering
;
372
373
if
(
inners
.
size
())
partial_ordering
.
pushBack
(
inners
);
374
375
if
(
outers
.
size
())
partial_ordering
.
pushBack
(
outers
);
376
377
if
(
ignore
.
size
())
partial_ordering
.
pushBack
(
ignore
);
378
379
GUM_ASSERT
(
inners
.
size
() ||
outers
.
size
());
380
PartialOrderedTriangulation
t
(&(
bn
.
moralGraph
()),
381
&(
bn
.
modalities
()),
382
&
partial_ordering
);
383
384
for
(
size_t
idx
= 0;
idx
<
inners
.
size
(); ++
idx
)
385
eliminateNode
(&(
c
.
get
(
t
.
eliminationOrder
()[
idx
]).
type
().
variable
()),
386
*
lifted_pool
,
387
trash
);
388
389
// If there is not only inner and input Attributes
390
if
(
outers
.
size
()) {
391
elim_orders__
.
insert
(
392
&
c
,
393
new
std
::
vector
<
NodeId
>(
t
.
eliminationOrder
().
begin
() +
inners
.
size
(),
394
t
.
eliminationOrder
().
end
()));
395
}
396
}
397
398
template
<
typename
GUM_SCALAR
>
399
void
SVED
<
GUM_SCALAR
>::
initElimOrder__
() {
400
ClassDependencyGraph
<
GUM_SCALAR
>
cdg
(*(
this
->
prm_
));
401
Sequence
<
const
PRMClassElementContainer
<
GUM_SCALAR
>* >
class_elim_order
;
402
std
::
list
<
NodeId
>
l
;
403
404
for
(
const
auto
node
:
cdg
.
dag
().
nodes
()) {
405
if
(
cdg
.
dag
().
parents
(
node
).
empty
()) {
l
.
push_back
(
node
); }
406
}
407
408
Set
<
NodeId
>
visited_node
;
409
410
while
(!
l
.
empty
()) {
411
visited_node
.
insert
(
l
.
front
());
412
413
if
(!
class_elim_order
.
exists
(
cdg
.
get
(
l
.
front
()).
first
)) {
414
class_elim_order
.
insert
(
cdg
.
get
(
l
.
front
()).
first
);
415
}
416
417
for
(
const
auto
child
:
cdg
.
dag
().
children
(
l
.
front
())) {
418
if
(!
visited_node
.
contains
(
child
)) {
l
.
push_back
(
child
); }
419
}
420
421
l
.
pop_front
();
422
}
423
424
class_elim_order__
=
new
Sequence
<
std
::
string
>();
425
for
(
auto
c
:
class_elim_order
) {
426
std
::
string
name
=
c
->
name
();
427
auto
pos
=
name
.
find_first_of
(
"<"
);
428
if
(
pos
!=
std
::
string
::
npos
) {
name
=
name
.
substr
(0,
pos
); }
429
try
{
430
class_elim_order__
->
insert
(
name
);
431
}
catch
(
DuplicateElement
&) {}
432
}
433
}
434
435
template
<
typename
GUM_SCALAR
>
436
void
SVED
<
GUM_SCALAR
>::
posterior_
(
const
Chain
&
chain
,
437
Potential
<
GUM_SCALAR
>&
m
) {
438
const
PRMInstance
<
GUM_SCALAR
>*
i
=
chain
.
first
;
439
const
PRMAttribute
<
GUM_SCALAR
>*
elt
=
chain
.
second
;
440
SVED
<
GUM_SCALAR
>::
BucketSet
pool
,
trash
;
441
bb__
.
compute
(
i
,
elt
->
id
());
442
eliminateNodes__
(
i
,
elt
->
id
(),
pool
,
trash
);
443
444
std
::
vector
<
const
Potential
<
GUM_SCALAR
>* >
result
;
445
for
(
auto
pot
:
pool
) {
446
if
(
pot
->
contains
(*(
m
.
variablesSequence
().
atPos
(0))))
447
result
.
push_back
(
pot
);
448
}
449
450
while
(
result
.
size
() > 1) {
451
const
auto
&
p1
= *(
result
.
back
());
452
result
.
pop_back
();
453
const
auto
&
p2
= *(
result
.
back
());
454
result
.
pop_back
();
455
auto
mult
=
new
Potential
<
GUM_SCALAR
>(
p1
*
p2
);
456
result
.
push_back
(
mult
);
457
trash
.
insert
(
mult
);
458
}
459
460
m
= *(
result
.
back
());
461
m
.
normalize
();
462
463
GUM_ASSERT
(
m
.
nbrDim
() == (
Size
)1);
464
465
// cleaning up the mess
466
for
(
const
auto
pot
:
trash
)
467
delete
pot
;
468
469
for
(
const
auto
&
elt
:
lifted_pools__
)
470
delete
elt
.
second
;
471
472
lifted_pools__
.
clear
();
473
474
for
(
const
auto
&
elt
:
req_set__
) {
475
delete
elt
.
second
.
first
;
476
delete
elt
.
second
.
second
;
477
}
478
479
req_set__
.
clear
();
480
481
for
(
const
auto
elt
:
elim_orders__
)
482
delete
elt
.
second
;
483
484
elim_orders__
.
clear
();
485
}
486
487
template
<
typename
GUM_SCALAR
>
488
void
SVED
<
GUM_SCALAR
>::
joint_
(
const
std
::
vector
<
Chain
>&
queries
,
489
Potential
<
GUM_SCALAR
>&
j
) {
490
GUM_ERROR
(
FatalError
,
"Not implemented."
);
491
}
492
493
template
<
typename
GUM_SCALAR
>
494
void
SVED
<
GUM_SCALAR
>::
initReqSets__
(
const
PRMInstance
<
GUM_SCALAR
>*
i
) {
495
Set
<
NodeId
>*
attr_set
=
new
Set
<
NodeId
>();
496
Set
<
NodeId
>*
sc_set
=
new
Set
<
NodeId
>();
497
498
for
(
const
auto
node
:
bb__
.
requisiteNodes
(
i
)) {
499
switch
(
i
->
type
().
get
(
node
).
elt_type
()) {
500
case
PRMClassElement
<
GUM_SCALAR
>::
prm_aggregate
:
501
case
PRMClassElement
<
GUM_SCALAR
>::
prm_attribute
: {
502
attr_set
->
insert
(
node
);
503
break
;
504
}
505
506
case
PRMClassElement
<
GUM_SCALAR
>::
prm_slotchain
: {
507
sc_set
->
insert
(
node
);
508
break
;
509
}
510
511
default
: {
512
GUM_ERROR
(
FatalError
,
513
"There should not be elements other"
514
" than PRMAttribute<GUM_SCALAR> and SlotChain."
);
515
}
516
}
517
}
518
519
req_set__
.
insert
(
520
&(
bb__
.
requisiteNodes
(
i
)),
521
std
::
pair
<
Set
<
NodeId
>*,
Set
<
NodeId
>* >(
attr_set
,
sc_set
));
522
}
523
524
template
<
typename
GUM_SCALAR
>
525
INLINE
SVED
<
GUM_SCALAR
>::
SVED
(
const
PRM
<
GUM_SCALAR
>&
prm
,
526
const
PRMSystem
<
GUM_SCALAR
>&
model
) :
527
PRMInference
<
GUM_SCALAR
>(
prm
,
model
),
528
class_elim_order__
(0),
bb__
(*
this
) {
529
GUM_CONSTRUCTOR
(
SVED
);
530
}
531
532
533
template
<
typename
GUM_SCALAR
>
534
INLINE
void
535
SVED
<
GUM_SCALAR
>::
insertEvidence__
(
const
PRMInstance
<
GUM_SCALAR
>*
i
,
536
BucketSet
&
pool
) {
537
for
(
const
auto
&
elt
:
this
->
evidence
(
i
))
538
pool
.
insert
(
const_cast
<
Potential
<
GUM_SCALAR
>* >(
elt
.
second
));
539
}
540
541
template
<
typename
GUM_SCALAR
>
542
INLINE
std
::
vector
<
NodeId
>&
543
SVED
<
GUM_SCALAR
>::
getElimOrder__
(
const
PRMClass
<
GUM_SCALAR
>&
c
) {
544
return
*(
elim_orders__
[&
c
]);
545
}
546
547
template
<
typename
GUM_SCALAR
>
548
INLINE
std
::
string
SVED
<
GUM_SCALAR
>::
trim__
(
const
std
::
string
&
s
) {
549
auto
pos
=
s
.
find_first_of
(
"<"
);
550
if
(
pos
!=
std
::
string
::
npos
) {
return
s
.
substr
(0,
pos
); }
551
return
s
;
552
}
553
554
template
<
typename
GUM_SCALAR
>
555
INLINE
bool
SVED
<
GUM_SCALAR
>::
checkElimOrder__
(
556
const
PRMInstance
<
GUM_SCALAR
>*
first
,
557
const
PRMInstance
<
GUM_SCALAR
>*
second
) {
558
if
(
class_elim_order__
== 0) {
initElimOrder__
(); }
559
560
auto
first_name
=
trim__
(
first
->
type
().
name
());
561
auto
second_name
=
trim__
(
second
->
type
().
name
());
562
return
(
class_elim_order__
->
pos
(
first_name
)
563
<=
class_elim_order__
->
pos
(
second_name
));
564
}
565
566
template
<
typename
GUM_SCALAR
>
567
INLINE
Potential
<
GUM_SCALAR
>*
SVED
<
GUM_SCALAR
>::
getAggPotential__
(
568
const
PRMInstance
<
GUM_SCALAR
>*
i
,
569
const
PRMAggregate
<
GUM_SCALAR
>*
agg
) {
570
return
&(
571
const_cast
<
Potential
<
GUM_SCALAR
>& >(
i
->
get
(
agg
->
safeName
()).
cpf
()));
572
}
573
574
template
<
typename
GUM_SCALAR
>
575
INLINE
void
SVED
<
GUM_SCALAR
>::
evidenceAdded_
(
576
const
typename
SVED
<
GUM_SCALAR
>::
Chain
&
chain
) {
577
// Do nothing
578
}
579
580
template
<
typename
GUM_SCALAR
>
581
INLINE
void
SVED
<
GUM_SCALAR
>::
evidenceRemoved_
(
582
const
typename
SVED
<
GUM_SCALAR
>::
Chain
&
chain
) {
583
// Do nothing
584
}
585
586
template
<
typename
GUM_SCALAR
>
587
INLINE
Set
<
NodeId
>&
588
SVED
<
GUM_SCALAR
>::
getAttrSet__
(
const
PRMInstance
<
GUM_SCALAR
>*
i
) {
589
try
{
590
return
*(
req_set__
[&(
bb__
.
requisiteNodes
(
i
))].
first
);
591
}
catch
(
NotFound
&) {
592
initReqSets__
(
i
);
593
return
*(
req_set__
[&(
bb__
.
requisiteNodes
(
i
))].
first
);
594
}
595
}
596
597
template
<
typename
GUM_SCALAR
>
598
INLINE
Set
<
NodeId
>&
599
SVED
<
GUM_SCALAR
>::
getSCSet__
(
const
PRMInstance
<
GUM_SCALAR
>*
i
) {
600
try
{
601
return
*(
req_set__
[&(
bb__
.
requisiteNodes
(
i
))].
second
);
602
}
catch
(
NotFound
&) {
603
initReqSets__
(
i
);
604
return
*(
req_set__
[&(
bb__
.
requisiteNodes
(
i
))].
second
);
605
}
606
}
607
608
template
<
typename
GUM_SCALAR
>
609
INLINE
void
SVED
<
GUM_SCALAR
>::
reduceElimList__
(
610
const
PRMInstance
<
GUM_SCALAR
>*
i
,
611
List
<
const
PRMInstance
<
GUM_SCALAR
>* >&
elim_list
,
612
List
<
const
PRMInstance
<
GUM_SCALAR
>* >&
reduced_list
,
613
Set
<
const
PRMInstance
<
GUM_SCALAR
>* >&
ignore
,
614
BucketSet
&
pool
,
615
BucketSet
&
trash
) {
616
while
(!
elim_list
.
empty
()) {
617
if
(
checkElimOrder__
(
i
,
elim_list
.
front
())) {
618
if
((!
ignore
.
exists
(
elim_list
.
front
()))
619
&& (
bb__
.
exists
(
elim_list
.
front
()))) {
620
eliminateNodesDownward__
(
i
,
621
elim_list
.
front
(),
622
pool
,
623
trash
,
624
elim_list
,
625
ignore
);
626
}
627
}
else
if
(
bb__
.
exists
(
elim_list
.
front
())) {
628
reduced_list
.
insert
(
elim_list
.
front
());
629
}
630
631
elim_list
.
popFront
();
632
}
633
}
634
635
template
<
typename
GUM_SCALAR
>
636
INLINE
std
::
string
SVED
<
GUM_SCALAR
>::
name
()
const
{
637
return
"SVED"
;
638
}
639
640
}
/* namespace prm */
641
}
/* namespace gum */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669
gum::prm::ParamScopeData::ParamScopeData
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)
Definition:
PRMClass_tpl.h:1101