aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
graphChangesGenerator4K2_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
GraphChangesGenerator4K2< STRUCT_CONSTRAINT >::GraphChangesGenerator4K2(
37
STRUCT_CONSTRAINT& constraint) :
38
constraint_(&constraint) {
39
GUM_CONSTRUCTOR(GraphChangesGenerator4K2);
40
}
41
42
/// copy constructor
43
template
<
typename
STRUCT_CONSTRAINT
>
44
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>::
GraphChangesGenerator4K2
(
45
const
GraphChangesGenerator4K2
&
from
) :
46
graph_
(
from
.
graph_
),
47
constraint_
(
from
.
constraint_
),
order_
(
from
.
order_
),
legal_changes_
(
from
.
legal_changes_
),
48
_max_threads_number_
(
from
.
_max_threads_number_
) {
49
GUM_CONS_CPY
(
GraphChangesGenerator4K2
);
50
}
51
52
/// move operator
53
template
<
typename
STRUCT_CONSTRAINT
>
54
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>::
GraphChangesGenerator4K2
(
55
GraphChangesGenerator4K2
&&
from
) :
56
graph_
(
std
::
move
(
from
.
graph_
)),
57
constraint_
(
from
.
constraint_
),
order_
(
std
::
move
(
from
.
order_
)),
58
legal_changes_
(
std
::
move
(
from
.
legal_changes_
)),
59
_max_threads_number_
(
from
.
_max_threads_number_
) {
60
GUM_CONS_MOV
(
GraphChangesGenerator4K2
);
61
}
62
63
/// destructor
64
template
<
typename
STRUCT_CONSTRAINT
>
65
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>::~
GraphChangesGenerator4K2
() {
66
GUM_DESTRUCTOR
(
GraphChangesGenerator4K2
);
67
}
68
69
/// copy operator
70
template
<
typename
STRUCT_CONSTRAINT
>
71
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>&
72
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>::
operator
=(
73
const
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>&
from
) {
74
if
(
this
!= &
from
) {
75
graph_
=
from
.
graph_
;
76
constraint_
=
from
.
constraint_
;
77
order_
=
from
.
order_
;
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
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>&
87
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>::
operator
=(
88
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>&&
from
) {
89
if
(
this
!= &
from
) {
90
graph_
=
std
::
move
(
from
.
graph_
);
91
constraint_
=
std
::
move
(
from
.
constraint_
);
92
order_
=
std
::
move
(
from
.
order_
);
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
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>::
createChanges_
() {
102
legal_changes_
.
clear
();
103
104
// for all the pairs of nodes, consider adding, reverse and removing arcs
105
std
::
vector
<
Set
<
GraphChange
> >
legal_changes
;
106
#
pragma
omp
parallel
num_threads
(
int
(
_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
Size
this_thread
=
getThreadNumber
();
118
119
for
(
Idx
i
= 0,
j
= 0;
j
<
order_
.
size
();
i
= (
i
+ 1) %
num_threads
, ++
j
) {
120
if
(
i
==
this_thread
) {
121
for
(
Idx
k
=
j
+ 1;
k
<
order_
.
size
(); ++
k
) {
122
// try arc additions
123
ArcAddition
arc_add
(
order_
[
j
],
order_
[
k
]);
124
if
(!
constraint_
->
isAlwaysInvalid
(
arc_add
)) {
125
legal_changes
[
this_thread
].
insert
(
std
::
move
(
arc_add
));
126
}
127
}
128
}
129
}
130
}
131
132
// now store the changes into the protected vectors of the
133
// GraphChangesGenerator4K2
134
for
(
const
auto
&
changes
:
legal_changes
) {
135
for
(
const
auto
&
change
:
changes
) {
136
legal_changes_
.
insert
(
std
::
move
(
change
));
137
}
138
}
139
}
140
141
/// sets a new graph from which the operator will compute possible changes
142
template
<
typename
STRUCT_CONSTRAINT
>
143
void
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>::
setGraph
(
const
DiGraph
&
graph
) {
144
// sets the current graph
145
graph_
=
graph
;
146
147
// check that all the nodes of the graph belong to the sequence.
148
// If some are missing, add them in increasing order into the sequence.
149
// If some element of order_ do not belong to the graph, remove them
150
for
(
auto
node
=
order_
.
beginSafe
();
node
!=
order_
.
endSafe
(); ++
node
) {
151
if
(!
graph
.
exists
(*
node
)) {
order_
.
erase
(
node
); }
152
}
153
for
(
const
auto
node
:
graph
) {
154
if
(!
order_
.
exists
(
node
)) {
order_
.
insert
(
node
); }
155
}
156
157
// generate the set of all changes
158
createChanges_
();
159
}
160
161
/// set a new order on the random variables
162
template
<
typename
STRUCT_CONSTRAINT
>
163
INLINE
void
164
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>::
setOrder
(
const
Sequence
<
NodeId
>&
order
) {
165
order_
=
order
;
166
}
167
168
/// set a new order on the random variables
169
template
<
typename
STRUCT_CONSTRAINT
>
170
INLINE
void
171
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>::
setOrder
(
const
std
::
vector
<
NodeId
>&
order
) {
172
order_
.
clear
();
173
for
(
const
auto
node
:
order
) {
174
order_
.
insert
(
node
);
175
}
176
}
177
178
/// empty the set of possible change operators that can be applied
179
template
<
typename
STRUCT_CONSTRAINT
>
180
INLINE
void
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>::
clearChanges
()
noexcept
{
181
legal_changes_
.
clear
();
182
}
183
184
/// returns an (unsafe) iterator on the beginning of the list of operators
185
template
<
typename
STRUCT_CONSTRAINT
>
186
INLINE
typename
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>::
iterator
187
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>::
begin
()
const
{
188
return
legal_changes_
.
cbegin
();
189
}
190
191
/// returns an (unsafe) iterator on the end of the list of operators
192
template
<
typename
STRUCT_CONSTRAINT
>
193
INLINE
const
typename
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>::
iterator
&
194
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>::
end
()
const
{
195
return
legal_changes_
.
cend
();
196
}
197
198
/// notify the operator set of a change applied to the graph
199
template
<
typename
STRUCT_CONSTRAINT
>
200
INLINE
void
201
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>::
modifyGraph
(
const
ArcAddition
&
change
) {}
202
203
/// notify the operator set of a change applied to the graph
204
template
<
typename
STRUCT_CONSTRAINT
>
205
INLINE
void
206
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>::
modifyGraph
(
const
ArcDeletion
&
change
) {}
207
208
/// notify the operator set of a change applied to the graph
209
template
<
typename
STRUCT_CONSTRAINT
>
210
INLINE
void
211
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>::
modifyGraph
(
const
ArcReversal
&
change
) {}
212
213
/// notify the operator set of a change applied to the graph
214
template
<
typename
STRUCT_CONSTRAINT
>
215
INLINE
void
216
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>::
modifyGraph
(
const
GraphChange
&
change
) {}
217
218
/// notifies the generator that we have parsed all its legal changes
219
template
<
typename
STRUCT_CONSTRAINT
>
220
INLINE
void
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>::
notifyGetCompleted
() {
221
if
(
legal_changes_
.
size
())
legal_changes_
.
clear
();
222
}
223
224
/// sets the maximum number of threads used to perform countings
225
template
<
typename
STRUCT_CONSTRAINT
>
226
INLINE
void
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>::
setMaxNbThreads
(
Size
nb
)
noexcept
{
227
#
if
defined
(
_OPENMP
)
&&
!
defined
(
GUM_DEBUG_MODE
)
228
if
(
nb
== 0)
nb
=
getMaxNumberOfThreads
();
229
_max_threads_number_
=
nb
;
230
#
else
231
_max_threads_number_
= 1;
232
#
endif
/* _OPENMP && GUM_DEBUG_MODE */
233
}
234
235
/// returns the constraint that is used by the generator
236
template
<
typename
STRUCT_CONSTRAINT
>
237
INLINE
STRUCT_CONSTRAINT
&
238
GraphChangesGenerator4K2
<
STRUCT_CONSTRAINT
>::
constraint
()
const
noexcept
{
239
return
*
constraint_
;
240
}
241
242
}
/* namespace learning */
243
244
}
/* namespace gum */
245
246
#
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