aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
graphChangesGenerator4UndiGraph_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
/** @file
23
* @brief The basic class for computing the next graph changes possible in an
24
* undirected structure learning algorithm
25
*
26
* @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
27
*/
28
#
ifndef
DOXYGEN_SHOULD_SKIP_THIS
29
30
namespace
gum
{
31
32
namespace
learning
{
33
34
/// default constructor
35
template
<
typename
STRUCT_CONSTRAINT >
36
GraphChangesGenerator4UndiGraph< STRUCT_CONSTRAINT >::
37
GraphChangesGenerator4UndiGraph(STRUCT_CONSTRAINT& constraint) :
38
constraint_(&constraint) {
39
GUM_CONSTRUCTOR(GraphChangesGenerator4UndiGraph);
40
}
41
42
/// copy constructor
43
template
<
typename
STRUCT_CONSTRAINT
>
44
GraphChangesGenerator4UndiGraph
<
STRUCT_CONSTRAINT
>::
45
GraphChangesGenerator4UndiGraph
(
46
const
GraphChangesGenerator4UndiGraph
&
from
) :
47
graph_
(
from
.
graph_
),
48
constraint_
(
from
.
constraint_
),
legal_changes_
(
from
.
legal_changes_
),
49
max_threads_number__
(
from
.
max_threads_number__
) {
50
GUM_CONS_CPY
(
GraphChangesGenerator4UndiGraph
);
51
}
52
53
/// move operator
54
template
<
typename
STRUCT_CONSTRAINT
>
55
GraphChangesGenerator4UndiGraph
<
STRUCT_CONSTRAINT
>::
56
GraphChangesGenerator4UndiGraph
(
GraphChangesGenerator4UndiGraph
&&
from
) :
57
graph_
(
std
::
move
(
from
.
graph_
)),
58
constraint_
(
from
.
constraint_
),
59
legal_changes_
(
std
::
move
(
from
.
legal_changes_
)),
60
max_threads_number__
(
from
.
max_threads_number__
) {
61
GUM_CONS_MOV
(
GraphChangesGenerator4UndiGraph
);
62
}
63
64
/// destructor
65
template
<
typename
STRUCT_CONSTRAINT
>
66
GraphChangesGenerator4UndiGraph
<
67
STRUCT_CONSTRAINT
>::~
GraphChangesGenerator4UndiGraph
() {
68
GUM_DESTRUCTOR
(
GraphChangesGenerator4UndiGraph
);
69
}
70
71
/// copy operator
72
template
<
typename
STRUCT_CONSTRAINT
>
73
GraphChangesGenerator4UndiGraph
<
STRUCT_CONSTRAINT
>&
74
GraphChangesGenerator4UndiGraph
<
STRUCT_CONSTRAINT
>::
operator
=(
75
const
GraphChangesGenerator4UndiGraph
<
STRUCT_CONSTRAINT
>&
from
) {
76
if
(
this
!= &
from
) {
77
graph_
=
from
.
graph_
;
78
constraint_
=
from
.
constraint_
;
79
legal_changes_
=
from
.
legal_changes_
;
80
max_threads_number__
=
from
.
max_threads_number__
;
81
}
82
return
*
this
;
83
}
84
85
/// move operator
86
template
<
typename
STRUCT_CONSTRAINT
>
87
GraphChangesGenerator4UndiGraph
<
STRUCT_CONSTRAINT
>&
88
GraphChangesGenerator4UndiGraph
<
STRUCT_CONSTRAINT
>::
operator
=(
89
GraphChangesGenerator4UndiGraph
<
STRUCT_CONSTRAINT
>&&
from
) {
90
if
(
this
!= &
from
) {
91
graph_
=
std
::
move
(
from
.
graph_
);
92
constraint_
=
std
::
move
(
from
.
constraint_
);
93
legal_changes_
=
std
::
move
(
from
.
legal_changes_
);
94
max_threads_number__
=
from
.
max_threads_number__
;
95
}
96
return
*
this
;
97
}
98
99
/// create the set of legal and illegal changes from a given graph
100
template
<
typename
STRUCT_CONSTRAINT
>
101
void
GraphChangesGenerator4UndiGraph
<
STRUCT_CONSTRAINT
>::
createChanges_
() {
102
legal_changes_
.
clear
();
103
104
// for all the pairs of nodes, consider adding, reverse and removing edges
105
std
::
vector
<
Set
<
GraphChange
> >
legal_changes
;
106
#
pragma
omp
parallel
num_threads
(
max_threads_number__
)
107
{
108
int
num_threads
=
getNumberOfRunningThreads
();
109
110
#
pragma
omp
single
111
{
112
// resize the change vectors so that each thread can write to its
113
// own vector
114
legal_changes
.
resize
(
num_threads
);
115
}
116
117
const
Idx
this_thread
=
getThreadNumber
();
118
119
Idx
i
= 0;
120
for
(
const
auto
node1
:
graph_
) {
121
if
(
i
==
this_thread
) {
122
for
(
const
auto
node2
:
graph_
) {
123
if
(
node1
!=
node2
) {
124
// try edge additions
125
EdgeAddition
edge_add
(
node1
,
node2
);
126
if
(!
constraint_
->
isAlwaysInvalid
(
edge_add
)) {
127
legal_changes
[
this_thread
].
insert
(
std
::
move
(
edge_add
));
128
}
129
130
// try edge deletion
131
EdgeDeletion
edge_del
(
node1
,
node2
);
132
if
(!
constraint_
->
isAlwaysInvalid
(
edge_del
)) {
133
legal_changes
[
this_thread
].
insert
(
std
::
move
(
edge_del
));
134
}
135
}
136
}
137
}
138
++
i
;
139
i
%=
num_threads
;
140
}
141
}
142
143
// now store the changes into the protected vectors of the
144
// GraphChangesGenerator4UndiGraph
145
for
(
const
auto
&
changes
:
legal_changes
) {
146
for
(
const
auto
&
change
:
changes
) {
147
legal_changes_
.
insert
(
std
::
move
(
change
));
148
}
149
}
150
}
151
152
/// sets a new graph from which the operator will compute possible changes
153
template
<
typename
STRUCT_CONSTRAINT
>
154
INLINE
void
GraphChangesGenerator4UndiGraph
<
STRUCT_CONSTRAINT
>::
setGraph
(
155
const
UndiGraph
&
graph
) {
156
// sets the current graph
157
graph_
=
graph
;
158
159
// generate the set of all changes
160
createChanges_
();
161
}
162
163
/// empty the set of possible change operators that can be applied
164
template
<
typename
STRUCT_CONSTRAINT
>
165
INLINE
void
GraphChangesGenerator4UndiGraph
<
166
STRUCT_CONSTRAINT
>::
clearChanges
()
noexcept
{
167
legal_changes_
.
clear
();
168
}
169
170
/// returns an (unsafe) iterator on the beginning of the list of operators
171
template
<
typename
STRUCT_CONSTRAINT
>
172
INLINE
typename
GraphChangesGenerator4UndiGraph
<
STRUCT_CONSTRAINT
>::
iterator
173
GraphChangesGenerator4UndiGraph
<
STRUCT_CONSTRAINT
>::
begin
()
const
{
174
return
legal_changes_
.
cbegin
();
175
}
176
177
/// returns an (unsafe) iterator on the end of the list of operators
178
template
<
typename
STRUCT_CONSTRAINT
>
179
INLINE
const
typename
GraphChangesGenerator4UndiGraph
<
180
STRUCT_CONSTRAINT
>::
iterator
&
181
GraphChangesGenerator4UndiGraph
<
STRUCT_CONSTRAINT
>::
end
()
const
{
182
return
legal_changes_
.
cend
();
183
}
184
185
/// notify the operator set of a change applied to the graph
186
template
<
typename
STRUCT_CONSTRAINT
>
187
INLINE
void
GraphChangesGenerator4UndiGraph
<
STRUCT_CONSTRAINT
>::
modifyGraph
(
188
const
EdgeAddition
&
change
) {}
189
190
/// notify the operator set of a change applied to the graph
191
template
<
typename
STRUCT_CONSTRAINT
>
192
INLINE
void
GraphChangesGenerator4UndiGraph
<
STRUCT_CONSTRAINT
>::
modifyGraph
(
193
const
EdgeDeletion
&
change
) {}
194
195
/// notify the operator set of a change applied to the graph
196
template
<
typename
STRUCT_CONSTRAINT
>
197
INLINE
void
GraphChangesGenerator4UndiGraph
<
STRUCT_CONSTRAINT
>::
modifyGraph
(
198
const
GraphChange
&
change
) {}
199
200
/// notifies the generator that we have parsed all its legal changes
201
template
<
typename
STRUCT_CONSTRAINT
>
202
INLINE
void
203
GraphChangesGenerator4UndiGraph
<
STRUCT_CONSTRAINT
>::
notifyGetCompleted
() {
204
if
(
legal_changes_
.
size
())
legal_changes_
.
clear
();
205
}
206
207
/// sets the maximum number of threads used to perform countings
208
template
<
typename
STRUCT_CONSTRAINT
>
209
INLINE
void
210
GraphChangesGenerator4UndiGraph
<
STRUCT_CONSTRAINT
>::
setMaxNbThreads
(
211
Size
nb
)
noexcept
{
212
#
if
defined
(
_OPENMP
)
&&
!
defined
(
GUM_DEBUG_MODE
)
213
if
(
nb
== 0)
nb
=
getMaxNumberOfThreads
();
214
max_threads_number__
=
nb
;
215
#
else
216
max_threads_number__
= 1;
217
#
endif
/* _OPENMP && GUM_DEBUG_MODE */
218
}
219
220
/// returns the constraint that is used by the generator
221
template
<
typename
STRUCT_CONSTRAINT
>
222
INLINE
STRUCT_CONSTRAINT
&
223
GraphChangesGenerator4UndiGraph
<
STRUCT_CONSTRAINT
>::
constraint
()
224
const
noexcept
{
225
return
*
constraint_
;
226
}
227
228
}
/* namespace learning */
229
230
}
/* namespace gum */
231
232
#
endif
/* DOXYGEN_SHOULD_SKIP_THIS */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669
gum::learning::genericBNLearner::Database::Database
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)
Definition:
genericBNLearner_tpl.h:31