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