aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
structuralConstraintDAG_inl.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 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
,
NodeId
y
)
const
{
64
return
!
_DAG_cycle_detector_
.
hasCycleFromAddition
(
x
,
y
);
65
}
66
67
/// checks whether the constraints enable to remove arc (x,y)
68
INLINE
bool
StructuralConstraintDAG
::
checkArcDeletionAlone
(
NodeId
x
,
NodeId
y
)
const
{
69
return
!
_DAG_cycle_detector_
.
hasCycleFromDeletion
(
x
,
y
);
70
}
71
72
/// checks whether the constraints enable to reverse arc (x,y)
73
INLINE
bool
StructuralConstraintDAG
::
checkArcReversalAlone
(
NodeId
x
,
NodeId
y
)
const
{
74
return
!
_DAG_cycle_detector_
.
hasCycleFromReversal
(
x
,
y
);
75
}
76
77
/// checks whether the constraints enable to add an arc
78
INLINE
bool
StructuralConstraintDAG
::
checkModificationAlone
(
const
ArcAddition
&
change
)
const
{
79
return
checkArcAdditionAlone
(
change
.
node1
(),
change
.
node2
());
80
}
81
82
/// checks whether the constraints enable to remove an arc
83
INLINE
bool
StructuralConstraintDAG
::
checkModificationAlone
(
const
ArcDeletion
&
change
)
const
{
84
return
checkArcDeletionAlone
(
change
.
node1
(),
change
.
node2
());
85
}
86
87
/// checks whether the constraints enable to reverse an arc
88
INLINE
bool
StructuralConstraintDAG
::
checkModificationAlone
(
const
ArcReversal
&
change
)
const
{
89
return
checkArcReversalAlone
(
change
.
node1
(),
change
.
node2
());
90
}
91
92
/// checks whether the constraints enable to perform a graph change
93
INLINE
bool
StructuralConstraintDAG
::
checkModificationAlone
(
const
GraphChange
&
change
)
const
{
94
switch
(
change
.
type
()) {
95
case
GraphChangeType
::
ARC_ADDITION
:
96
return
checkArcAdditionAlone
(
change
.
node1
(),
change
.
node2
());
97
98
case
GraphChangeType
::
ARC_DELETION
:
99
return
checkArcDeletionAlone
(
change
.
node1
(),
change
.
node2
());
100
101
case
GraphChangeType
::
ARC_REVERSAL
:
102
return
checkArcReversalAlone
(
change
.
node1
(),
change
.
node2
());
103
104
default
:
105
GUM_ERROR
(
OperationNotAllowed
,
106
"edge modifications are not "
107
"supported by StructuralConstraintDAG"
);
108
}
109
}
110
111
/// notify the constraint of a modification of the graph
112
INLINE
void
StructuralConstraintDAG
::
modifyGraphAlone
(
const
ArcAddition
&
change
) {
113
_DAG_cycle_detector_
.
addArc
(
change
.
node1
(),
change
.
node2
());
114
}
115
116
/// notify the constraint of a modification of the graph
117
INLINE
void
StructuralConstraintDAG
::
modifyGraphAlone
(
const
ArcDeletion
&
change
) {
118
_DAG_cycle_detector_
.
eraseArc
(
change
.
node1
(),
change
.
node2
());
119
}
120
121
/// notify the constraint of a modification of the graph
122
INLINE
void
StructuralConstraintDAG
::
modifyGraphAlone
(
const
ArcReversal
&
change
) {
123
_DAG_cycle_detector_
.
reverseArc
(
change
.
node1
(),
change
.
node2
());
124
}
125
126
/// notify the constraint of a modification of the graph
127
INLINE
void
StructuralConstraintDAG
::
modifyGraphAlone
(
const
GraphChange
&
change
) {
128
switch
(
change
.
type
()) {
129
case
GraphChangeType
::
ARC_ADDITION
:
130
modifyGraphAlone
(
reinterpret_cast
<
const
ArcAddition
& >(
change
));
131
break
;
132
133
case
GraphChangeType
::
ARC_DELETION
:
134
modifyGraphAlone
(
reinterpret_cast
<
const
ArcDeletion
& >(
change
));
135
break
;
136
137
case
GraphChangeType
::
ARC_REVERSAL
:
138
modifyGraphAlone
(
reinterpret_cast
<
const
ArcReversal
& >(
change
));
139
break
;
140
141
default
:
142
GUM_ERROR
(
OperationNotAllowed
,
"edge modifications are not supported by DAG constraints"
)
143
}
144
}
145
146
/// indicates whether a change will always violate the constraint
147
INLINE
bool
StructuralConstraintDAG
::
isAlwaysInvalidAlone
(
const
GraphChange
&)
const
{
148
return
false
;
149
}
150
151
/// sets a new graph from which we will perform checkings
152
INLINE
void
StructuralConstraintDAG
::
setGraph
(
const
DAG
&
graph
) {
153
constraints
::
setGraph
(
graph
);
154
_DAG_cycle_detector_
.
setDAG
(
graph
);
155
}
156
157
/// sets a new graph from which we will perform checkings
158
INLINE
void
StructuralConstraintDAG
::
setGraph
(
Size
nb_nodes
) {
159
StructuralConstraintDiGraph
::
setGraph
(
nb_nodes
);
160
setGraphAlone
(
nb_nodes
);
161
}
162
163
// include all the methods applicable to the whole class hierarchy
164
#
define
GUM_CONSTRAINT_CLASS_NAME
StructuralConstraintDAG
165
#
include
<
agrum
/
BN
/
learning
/
constraints
/
structuralConstraintPatternInline
.
h
>
166
#
undef
GUM_CONSTRAINT_CLASS_NAME
167
168
}
/* namespace learning */
169
170
}
/* namespace gum */
171
172
#
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