aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
structuralConstraintDAG_inl.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 base class for structural constraints imposed by DAGs
24
*
25
* @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26
*/
27
#
ifndef
DOXYGEN_SHOULD_SKIP_THIS
28
29
#
include
<
agrum
/
agrum
.
h
>
30
#
include
<
agrum
/
tools
/
graphs
/
algorithms
/
DAGCycleDetector
.
h
>
31
#
include
<
agrum
/
BN
/
learning
/
constraints
/
structuralConstraintDiGraph
.
h
>
32
33
namespace
gum
{
34
35
namespace
learning
{
36
37
/// sets a new graph from which we will perform checkings
38
INLINE
void
StructuralConstraintDAG
::
setGraphAlone
(
const
DiGraph
&
graph
) {
39
// check that the digraph has no cycle
40
DAG
g
;
41
42
for
(
auto
node
:
graph
)
43
g
.
addNodeWithId
(
node
);
44
45
for
(
auto
&
arc
:
graph
.
arcs
())
46
g
.
addArc
(
arc
.
tail
(),
arc
.
head
());
47
48
DAG__cycle_detector_
.
setDAG
(
g
);
49
}
50
51
/// sets a new graph from which we will perform checkings
52
INLINE
void
StructuralConstraintDAG
::
setGraphAlone
(
Size
nb_nodes
) {
53
DAG
g
;
54
55
for
(
NodeId
i
= 0;
i
<
nb_nodes
; ++
i
) {
56
g
.
addNodeWithId
(
i
);
57
}
58
59
DAG__cycle_detector_
.
setDAG
(
g
);
60
}
61
62
/// checks whether the constraints enable to add arc (x,y)
63
INLINE
bool
StructuralConstraintDAG
::
checkArcAdditionAlone
(
NodeId
x
,
64
NodeId
y
)
const
{
65
return
!
DAG__cycle_detector_
.
hasCycleFromAddition
(
x
,
y
);
66
}
67
68
/// checks whether the constraints enable to remove arc (x,y)
69
INLINE
bool
StructuralConstraintDAG
::
checkArcDeletionAlone
(
NodeId
x
,
70
NodeId
y
)
const
{
71
return
!
DAG__cycle_detector_
.
hasCycleFromDeletion
(
x
,
y
);
72
}
73
74
/// checks whether the constraints enable to reverse arc (x,y)
75
INLINE
bool
StructuralConstraintDAG
::
checkArcReversalAlone
(
NodeId
x
,
76
NodeId
y
)
const
{
77
return
!
DAG__cycle_detector_
.
hasCycleFromReversal
(
x
,
y
);
78
}
79
80
/// checks whether the constraints enable to add an arc
81
INLINE
bool
StructuralConstraintDAG
::
checkModificationAlone
(
82
const
ArcAddition
&
change
)
const
{
83
return
checkArcAdditionAlone
(
change
.
node1
(),
change
.
node2
());
84
}
85
86
/// checks whether the constraints enable to remove an arc
87
INLINE
bool
StructuralConstraintDAG
::
checkModificationAlone
(
88
const
ArcDeletion
&
change
)
const
{
89
return
checkArcDeletionAlone
(
change
.
node1
(),
change
.
node2
());
90
}
91
92
/// checks whether the constraints enable to reverse an arc
93
INLINE
bool
StructuralConstraintDAG
::
checkModificationAlone
(
94
const
ArcReversal
&
change
)
const
{
95
return
checkArcReversalAlone
(
change
.
node1
(),
change
.
node2
());
96
}
97
98
/// checks whether the constraints enable to perform a graph change
99
INLINE
bool
StructuralConstraintDAG
::
checkModificationAlone
(
100
const
GraphChange
&
change
)
const
{
101
switch
(
change
.
type
()) {
102
case
GraphChangeType
::
ARC_ADDITION
:
103
return
checkArcAdditionAlone
(
change
.
node1
(),
change
.
node2
());
104
105
case
GraphChangeType
::
ARC_DELETION
:
106
return
checkArcDeletionAlone
(
change
.
node1
(),
change
.
node2
());
107
108
case
GraphChangeType
::
ARC_REVERSAL
:
109
return
checkArcReversalAlone
(
change
.
node1
(),
change
.
node2
());
110
111
default
:
112
GUM_ERROR
(
OperationNotAllowed
,
113
"edge modifications are not "
114
"supported by StructuralConstraintDAG"
);
115
}
116
}
117
118
/// notify the constraint of a modification of the graph
119
INLINE
void
120
StructuralConstraintDAG
::
modifyGraphAlone
(
const
ArcAddition
&
change
) {
121
DAG__cycle_detector_
.
addArc
(
change
.
node1
(),
change
.
node2
());
122
}
123
124
/// notify the constraint of a modification of the graph
125
INLINE
void
126
StructuralConstraintDAG
::
modifyGraphAlone
(
const
ArcDeletion
&
change
) {
127
DAG__cycle_detector_
.
eraseArc
(
change
.
node1
(),
change
.
node2
());
128
}
129
130
/// notify the constraint of a modification of the graph
131
INLINE
void
132
StructuralConstraintDAG
::
modifyGraphAlone
(
const
ArcReversal
&
change
) {
133
DAG__cycle_detector_
.
reverseArc
(
change
.
node1
(),
change
.
node2
());
134
}
135
136
/// notify the constraint of a modification of the graph
137
INLINE
void
138
StructuralConstraintDAG
::
modifyGraphAlone
(
const
GraphChange
&
change
) {
139
switch
(
change
.
type
()) {
140
case
GraphChangeType
::
ARC_ADDITION
:
141
modifyGraphAlone
(
reinterpret_cast
<
const
ArcAddition
& >(
change
));
142
break
;
143
144
case
GraphChangeType
::
ARC_DELETION
:
145
modifyGraphAlone
(
reinterpret_cast
<
const
ArcDeletion
& >(
change
));
146
break
;
147
148
case
GraphChangeType
::
ARC_REVERSAL
:
149
modifyGraphAlone
(
reinterpret_cast
<
const
ArcReversal
& >(
change
));
150
break
;
151
152
default
:
153
GUM_ERROR
(
OperationNotAllowed
,
154
"edge modifications are not supported by DAG constraints"
);
155
}
156
}
157
158
/// indicates whether a change will always violate the constraint
159
INLINE
bool
160
StructuralConstraintDAG
::
isAlwaysInvalidAlone
(
const
GraphChange
&)
const
{
161
return
false
;
162
}
163
164
/// sets a new graph from which we will perform checkings
165
INLINE
void
StructuralConstraintDAG
::
setGraph
(
const
DAG
&
graph
) {
166
constraints
::
setGraph
(
graph
);
167
DAG__cycle_detector_
.
setDAG
(
graph
);
168
}
169
170
/// sets a new graph from which we will perform checkings
171
INLINE
void
StructuralConstraintDAG
::
setGraph
(
Size
nb_nodes
) {
172
StructuralConstraintDiGraph
::
setGraph
(
nb_nodes
);
173
setGraphAlone
(
nb_nodes
);
174
}
175
176
// include all the methods applicable to the whole class hierarchy
177
#
define
GUM_CONSTRAINT_CLASS_NAME
StructuralConstraintDAG
178
#
include
<
agrum
/
BN
/
learning
/
constraints
/
structuralConstraintPatternInline
.
h
>
179
#
undef
GUM_CONSTRAINT_CLASS_NAME
180
181
}
/* namespace learning */
182
183
}
/* namespace gum */
184
185
#
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