aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
structuralConstraintIndegree_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 class for structural constraints limiting the number of parents
24
* of nodes in a directed graph
25
*
26
* @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
27
*/
28
29
#
ifndef
DOXYGEN_SHOULD_SKIP_THIS
30
31
namespace
gum
{
32
33
namespace
learning
{
34
35
/// sets a new graph from which we will perform checkings
36
INLINE
void
StructuralConstraintIndegree
::
setGraphAlone
(
const
DiGraph
&
graph
) {
37
// check that the max_indegree corresponds to the graph
38
for
(
const
auto
id
:
graph
) {
39
if
(!
Indegree__max_parents_
.
exists
(
id
)) {
40
Indegree__max_parents_
.
insert
(
id
,
Indegree__max_indegree_
);
41
}
42
}
43
}
44
45
/// checks whether the constraints enable to add arc (x,y)
46
INLINE
bool
47
StructuralConstraintIndegree
::
checkArcAdditionAlone
(
NodeId
x
,
48
NodeId
y
)
const
{
49
return
(
Indegree__max_parents_
[
y
] >
DiGraph__graph_
.
parents
(
y
).
size
());
50
}
51
52
/// checks whether the constraints enable to remove arc (x,y)
53
INLINE
bool
54
StructuralConstraintIndegree
::
checkArcDeletionAlone
(
NodeId
x
,
55
NodeId
y
)
const
{
56
return
true
;
57
}
58
59
/// checks whether the constraints enable to reverse arc (x,y)
60
INLINE
bool
61
StructuralConstraintIndegree
::
checkArcReversalAlone
(
NodeId
x
,
62
NodeId
y
)
const
{
63
return
(
Indegree__max_parents_
[
x
] >
DiGraph__graph_
.
parents
(
x
).
size
());
64
}
65
66
/// checks whether the constraints enable to add an arc
67
INLINE
bool
StructuralConstraintIndegree
::
checkModificationAlone
(
68
const
ArcAddition
&
change
)
const
{
69
return
checkArcAdditionAlone
(
change
.
node1
(),
change
.
node2
());
70
}
71
72
/// checks whether the constraints enable to remove an arc
73
INLINE
bool
StructuralConstraintIndegree
::
checkModificationAlone
(
74
const
ArcDeletion
&
change
)
const
{
75
return
checkArcDeletionAlone
(
change
.
node1
(),
change
.
node2
());
76
}
77
78
/// checks whether the constraints enable to reverse an arc
79
INLINE
bool
StructuralConstraintIndegree
::
checkModificationAlone
(
80
const
ArcReversal
&
change
)
const
{
81
return
checkArcReversalAlone
(
change
.
node1
(),
change
.
node2
());
82
}
83
84
/// checks whether the constraints enable to perform a graph change
85
INLINE
bool
StructuralConstraintIndegree
::
checkModificationAlone
(
86
const
GraphChange
&
change
)
const
{
87
switch
(
change
.
type
()) {
88
case
GraphChangeType
::
ARC_ADDITION
:
89
return
checkArcAdditionAlone
(
change
.
node1
(),
change
.
node2
());
90
91
case
GraphChangeType
::
ARC_DELETION
:
92
return
checkArcDeletionAlone
(
change
.
node1
(),
change
.
node2
());
93
94
case
GraphChangeType
::
ARC_REVERSAL
:
95
return
checkArcReversalAlone
(
change
.
node1
(),
change
.
node2
());
96
97
default
:
98
GUM_ERROR
(
OperationNotAllowed
,
99
"edge modifications are not "
100
"supported by StructuralConstraintIndegree"
);
101
}
102
}
103
104
/// notify the constraint of a modification of the graph
105
INLINE
void
106
StructuralConstraintIndegree
::
modifyGraphAlone
(
const
ArcAddition
&
change
) {}
107
108
/// notify the constraint of a modification of the graph
109
INLINE
void
110
StructuralConstraintIndegree
::
modifyGraphAlone
(
const
ArcDeletion
&
change
) {}
111
112
/// notify the constraint of a modification of the graph
113
INLINE
void
114
StructuralConstraintIndegree
::
modifyGraphAlone
(
const
ArcReversal
&
change
) {}
115
116
/// notify the constraint of a modification of the graph
117
INLINE
void
118
StructuralConstraintIndegree
::
modifyGraphAlone
(
const
GraphChange
&
change
) {}
119
120
/// indicates whether a change will always violate the constraint
121
INLINE
bool
StructuralConstraintIndegree
::
isAlwaysInvalidAlone
(
122
const
GraphChange
&
change
)
const
{
123
if
((
change
.
type
() ==
GraphChangeType
::
ARC_ADDITION
)
124
&& (
Indegree__max_parents_
[
change
.
node2
()] == 0)) {
125
return
true
;
126
}
else
if
((
change
.
type
() ==
GraphChangeType
::
ARC_REVERSAL
)
127
&& (
Indegree__max_parents_
[
change
.
node1
()] == 0)) {
128
return
true
;
129
}
else
{
130
return
false
;
131
}
132
}
133
134
/// sets the indegree for a given set of nodes
135
INLINE
void
StructuralConstraintIndegree
::
setIndegree
(
136
const
NodeProperty
<
Size
>&
max_indegree
) {
137
for
(
const
auto
&
degree
:
max_indegree
) {
138
Indegree__max_parents_
.
set
(
degree
.
first
,
degree
.
second
);
139
}
140
}
141
142
/// resets the max indegree
143
INLINE
void
StructuralConstraintIndegree
::
setMaxIndegree
(
Size
max_indegree
,
144
bool
update_all
) {
145
if
(
update_all
) {
146
for
(
auto
&
degree
:
Indegree__max_parents_
) {
147
degree
.
second
=
max_indegree
;
148
}
149
}
150
151
Indegree__max_indegree_
=
max_indegree
;
152
}
153
154
// include all the methods applicable to the whole class hierarchy
155
#
define
GUM_CONSTRAINT_CLASS_NAME
StructuralConstraintIndegree
156
#
include
<
agrum
/
BN
/
learning
/
constraints
/
structuralConstraintPatternInline
.
h
>
157
#
undef
GUM_CONSTRAINT_CLASS_NAME
158
159
}
/* namespace learning */
160
161
}
/* namespace gum */
162
163
#
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