aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
structuralConstraintDiGraph_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 used by learning algorithms
24
* that learn a directed graph structure
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
/// sets a new graph from which we will perform checkings
35
INLINE
void
StructuralConstraintDiGraph
::
setGraphAlone
(
const
DiGraph
&
graph
) {
36
DiGraph__graph_
=
graph
;
37
}
38
39
/// checks whether the constraints enable to add arc (x,y)
40
INLINE
bool
41
StructuralConstraintDiGraph
::
checkArcAdditionAlone
(
NodeId
x
,
42
NodeId
y
)
const
{
43
return
DiGraph__graph_
.
existsNode
(
x
) &&
DiGraph__graph_
.
existsNode
(
y
)
44
&& !
DiGraph__graph_
.
existsArc
(
x
,
y
);
45
}
46
47
/// checks whether the constraints enable to remove arc (x,y)
48
INLINE
bool
49
StructuralConstraintDiGraph
::
checkArcDeletionAlone
(
NodeId
x
,
50
NodeId
y
)
const
{
51
return
DiGraph__graph_
.
existsArc
(
x
,
y
);
52
}
53
54
/// checks whether the constraints enable to reverse arc (x,y)
55
INLINE
bool
56
StructuralConstraintDiGraph
::
checkArcReversalAlone
(
NodeId
x
,
57
NodeId
y
)
const
{
58
return
DiGraph__graph_
.
existsArc
(
x
,
y
) && !
DiGraph__graph_
.
existsArc
(
y
,
x
);
59
}
60
61
/// checks whether the constraints enable to add an arc
62
INLINE
bool
StructuralConstraintDiGraph
::
checkModificationAlone
(
63
const
ArcAddition
&
change
)
const
{
64
return
checkArcAdditionAlone
(
change
.
node1
(),
change
.
node2
());
65
}
66
67
/// checks whether the constraints enable to remove an arc
68
INLINE
bool
StructuralConstraintDiGraph
::
checkModificationAlone
(
69
const
ArcDeletion
&
change
)
const
{
70
return
checkArcDeletionAlone
(
change
.
node1
(),
change
.
node2
());
71
}
72
73
/// checks whether the constraints enable to reverse an arc
74
INLINE
bool
StructuralConstraintDiGraph
::
checkModificationAlone
(
75
const
ArcReversal
&
change
)
const
{
76
return
checkArcReversalAlone
(
change
.
node1
(),
change
.
node2
());
77
}
78
79
/// checks whether the constraints enable to perform a graph change
80
INLINE
bool
StructuralConstraintDiGraph
::
checkModificationAlone
(
81
const
GraphChange
&
change
)
const
{
82
switch
(
change
.
type
()) {
83
case
GraphChangeType
::
ARC_ADDITION
:
84
return
checkArcAdditionAlone
(
change
.
node1
(),
change
.
node2
());
85
86
case
GraphChangeType
::
ARC_DELETION
:
87
return
checkArcDeletionAlone
(
change
.
node1
(),
change
.
node2
());
88
89
case
GraphChangeType
::
ARC_REVERSAL
:
90
return
checkArcReversalAlone
(
change
.
node1
(),
change
.
node2
());
91
92
default
:
93
GUM_ERROR
(
OperationNotAllowed
,
94
"edge modifications are not "
95
"supported by StructuralConstraintDiGraph"
);
96
}
97
}
98
99
/// notify the constraint of a modification of the graph
100
INLINE
void
101
StructuralConstraintDiGraph
::
modifyGraphAlone
(
const
ArcAddition
&
change
) {
102
DiGraph__graph_
.
addArc
(
change
.
node1
(),
change
.
node2
());
103
}
104
105
/// notify the constraint of a modification of the graph
106
INLINE
void
107
StructuralConstraintDiGraph
::
modifyGraphAlone
(
const
ArcDeletion
&
change
) {
108
DiGraph__graph_
.
eraseArc
(
Arc
(
change
.
node1
(),
change
.
node2
()));
109
}
110
111
/// notify the constraint of a modification of the graph
112
INLINE
void
113
StructuralConstraintDiGraph
::
modifyGraphAlone
(
const
ArcReversal
&
change
) {
114
DiGraph__graph_
.
eraseArc
(
Arc
(
change
.
node1
(),
change
.
node2
()));
115
DiGraph__graph_
.
addArc
(
change
.
node2
(),
change
.
node1
());
116
}
117
118
/// notify the constraint of a modification of the graph
119
INLINE
void
120
StructuralConstraintDiGraph
::
modifyGraphAlone
(
const
GraphChange
&
change
) {
121
switch
(
change
.
type
()) {
122
case
GraphChangeType
::
ARC_ADDITION
:
123
modifyGraphAlone
(
reinterpret_cast
<
const
ArcAddition
& >(
change
));
124
break
;
125
126
case
GraphChangeType
::
ARC_DELETION
:
127
modifyGraphAlone
(
reinterpret_cast
<
const
ArcDeletion
& >(
change
));
128
break
;
129
130
case
GraphChangeType
::
ARC_REVERSAL
:
131
modifyGraphAlone
(
reinterpret_cast
<
const
ArcReversal
& >(
change
));
132
break
;
133
134
default
:
135
GUM_ERROR
(
OperationNotAllowed
,
136
"edge modifications are not supported by digraph constraint"
);
137
}
138
}
139
140
/// indicates whether a change will always violate the constraint
141
INLINE
bool
StructuralConstraintDiGraph
::
isAlwaysInvalidAlone
(
142
const
GraphChange
&)
const
{
143
return
false
;
144
}
145
146
/// sets a new graph from which we will perform checkings
147
INLINE
void
StructuralConstraintDiGraph
::
setGraph
(
Size
nb_nodes
) {
148
DiGraph__graph_
.
clear
();
149
150
for
(
NodeId
i
= 0;
i
<
nb_nodes
; ++
i
) {
151
DiGraph__graph_
.
addNodeWithId
(
i
);
152
}
153
}
154
155
// include all the methods applicable to the whole class hierarchy
156
#
define
GUM_CONSTRAINT_CLASS_NAME
StructuralConstraintDiGraph
157
#
include
<
agrum
/
BN
/
learning
/
constraints
/
structuralConstraintPatternRootInline
.
h
>
158
#
undef
GUM_CONSTRAINT_CLASS_NAME
159
160
}
/* namespace learning */
161
162
}
/* namespace gum */
163
164
#
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