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