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