aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
eliminationSequenceStrategy.cpp
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
/** @file
23
* @brief implementation of the base class for all elimination sequence
24
*algorithms
25
*
26
* @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
27
*/
28
29
#
include
<
agrum
/
agrum
.
h
>
30
#
include
<
agrum
/
tools
/
graphs
/
algorithms
/
triangulations
/
eliminationStrategies
/
eliminationSequenceStrategy
.
h
>
31
32
#
ifdef
GUM_NO_INLINE
33
#
include
<
agrum
/
tools
/
graphs
/
algorithms
/
triangulations
/
eliminationStrategies
/
eliminationSequenceStrategy_inl
.
h
>
34
#
endif
// GUM_NOINLINE
35
36
namespace
gum
{
37
38
// an empty fill-ins set returned by default when we ask for a fill-ins set
39
const
EdgeSet
&
EliminationSequenceStrategy
::
empty_fill_ins__
() {
40
#
ifdef
GUM_DEBUG_MODE
41
static
bool
first_use
=
true
;
42
if
(
first_use
) {
43
first_use
=
false
;
44
__debug__
::
dec_creation__
(
"Set"
,
45
"__empty_edge_set"
,
46
0,
47
"static variable correction"
,
48
0);
49
__debug__
::
dec_creation__
(
"HashTable"
,
50
"__empty_edge_set"
,
51
0,
52
"static variable correction"
,
53
0);
54
}
55
#
endif
56
static
EdgeSet
empty_fill_ins
;
57
return
empty_fill_ins
;
58
}
59
60
// default constructor
61
EliminationSequenceStrategy
::
EliminationSequenceStrategy
() {
62
// for debugging purposes
63
GUM_CONSTRUCTOR
(
EliminationSequenceStrategy
);
64
}
65
66
// constructor for an a priori non empty graph
67
EliminationSequenceStrategy
::
EliminationSequenceStrategy
(
68
UndiGraph
*
graph
,
69
const
NodeProperty
<
Size
>*
domain_sizes
) {
70
EliminationSequenceStrategy
::
setGraph
(
graph
,
domain_sizes
);
71
72
// for debugging purposes
73
GUM_CONSTRUCTOR
(
EliminationSequenceStrategy
);
74
}
75
76
// copy constructor
77
EliminationSequenceStrategy
::
EliminationSequenceStrategy
(
78
const
EliminationSequenceStrategy
&
from
) :
79
graph_
(
from
.
graph_
),
80
domain_sizes_
(
from
.
domain_sizes_
),
81
log_domain_sizes_
(
from
.
log_domain_sizes_
) {
82
// for debugging purposes
83
GUM_CONS_CPY
(
EliminationSequenceStrategy
);
84
}
85
86
/// move constructor
87
EliminationSequenceStrategy
::
EliminationSequenceStrategy
(
88
EliminationSequenceStrategy
&&
from
) :
89
graph_
(
from
.
graph_
),
90
domain_sizes_
(
from
.
domain_sizes_
),
91
log_domain_sizes_
(
std
::
move
(
from
.
log_domain_sizes_
)) {
92
// for debugging purposes
93
GUM_CONS_MOV
(
EliminationSequenceStrategy
);
94
}
95
96
// destructor
97
EliminationSequenceStrategy
::~
EliminationSequenceStrategy
() {
98
// for debugging purposes
99
GUM_DESTRUCTOR
(
EliminationSequenceStrategy
);
100
}
101
102
// performs all the graph/fill-ins updates provided
103
void
EliminationSequenceStrategy
::
eliminationUpdate
(
const
NodeId
node
) {}
104
105
/** @brief in case fill-ins are provided, this function returns the fill-ins
106
* due to all the nodes eliminated so far */
107
const
EdgeSet
&
EliminationSequenceStrategy
::
fillIns
() {
108
return
empty_fill_ins__
();
109
}
110
111
// clears the sequence (to prepare, for instance, a new elimination sequence)
112
void
EliminationSequenceStrategy
::
clear
() {
113
graph_
=
nullptr
;
114
domain_sizes_
=
nullptr
;
115
log_domain_sizes_
.
clear
();
116
}
117
118
// sets a new graph to be triangulated
119
bool
120
EliminationSequenceStrategy
::
setGraph
(
UndiGraph
*
graph
,
121
const
NodeProperty
<
Size
>*
dom_sizes
) {
122
// check that both the graph and the domain sizes are different from nullptr
123
// or else that both are equal to nullptr
124
if
(((
graph
!=
nullptr
) && (
dom_sizes
==
nullptr
))
125
|| ((
graph
==
nullptr
) && (
dom_sizes
!=
nullptr
))) {
126
GUM_ERROR
(
GraphError
,
127
"EliminationSequenceStrategy: one of the graph or the set of "
128
"domain sizes is a null pointer."
);
129
}
130
131
// check that each node has a domain size
132
if
(
graph
!=
nullptr
) {
133
for
(
const
auto
node
: *
graph
)
134
if
(!
dom_sizes
->
exists
(
node
))
135
GUM_ERROR
(
GraphError
,
136
"EliminationSequenceStrategy needs a domain size "
137
"for every node in the graph."
);
138
}
139
140
// avoid empty modifications
141
if
((
graph
!=
graph_
) || (
dom_sizes
!=
domain_sizes_
)) {
142
// remove, if any, the current graph
143
clear
();
144
145
// assign a new graph
146
graph_
=
graph
;
147
domain_sizes_
=
dom_sizes
;
148
149
if
(
graph_
!=
nullptr
) {
150
// compute the log of the modalities
151
log_domain_sizes_
.
resize
(
graph_
->
sizeNodes
() / 2);
152
153
for
(
const
auto
node
: *
graph_
)
154
log_domain_sizes_
.
insert
(
node
,
std
::
log
((*
domain_sizes_
)[
node
]));
155
}
156
157
return
true
;
158
}
159
160
return
false
;
161
}
162
163
}
/* namespace gum */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669