aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
structuralConstraintIndegree_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 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
StructuralConstraintIndegree
::
checkArcAdditionAlone
(
NodeId
x
,
NodeId
y
)
const
{
47
return
(
_Indegree_max_parents_
[
y
] >
_DiGraph_graph_
.
parents
(
y
).
size
());
48
}
49
50
/// checks whether the constraints enable to remove arc (x,y)
51
INLINE
bool
StructuralConstraintIndegree
::
checkArcDeletionAlone
(
NodeId
x
,
NodeId
y
)
const
{
52
return
true
;
53
}
54
55
/// checks whether the constraints enable to reverse arc (x,y)
56
INLINE
bool
StructuralConstraintIndegree
::
checkArcReversalAlone
(
NodeId
x
,
NodeId
y
)
const
{
57
return
(
_Indegree_max_parents_
[
x
] >
_DiGraph_graph_
.
parents
(
x
).
size
());
58
}
59
60
/// checks whether the constraints enable to add an arc
61
INLINE
bool
62
StructuralConstraintIndegree
::
checkModificationAlone
(
const
ArcAddition
&
change
)
const
{
63
return
checkArcAdditionAlone
(
change
.
node1
(),
change
.
node2
());
64
}
65
66
/// checks whether the constraints enable to remove an arc
67
INLINE
bool
68
StructuralConstraintIndegree
::
checkModificationAlone
(
const
ArcDeletion
&
change
)
const
{
69
return
checkArcDeletionAlone
(
change
.
node1
(),
change
.
node2
());
70
}
71
72
/// checks whether the constraints enable to reverse an arc
73
INLINE
bool
74
StructuralConstraintIndegree
::
checkModificationAlone
(
const
ArcReversal
&
change
)
const
{
75
return
checkArcReversalAlone
(
change
.
node1
(),
change
.
node2
());
76
}
77
78
/// checks whether the constraints enable to perform a graph change
79
INLINE
bool
80
StructuralConstraintIndegree
::
checkModificationAlone
(
const
GraphChange
&
change
)
const
{
81
switch
(
change
.
type
()) {
82
case
GraphChangeType
::
ARC_ADDITION
:
83
return
checkArcAdditionAlone
(
change
.
node1
(),
change
.
node2
());
84
85
case
GraphChangeType
::
ARC_DELETION
:
86
return
checkArcDeletionAlone
(
change
.
node1
(),
change
.
node2
());
87
88
case
GraphChangeType
::
ARC_REVERSAL
:
89
return
checkArcReversalAlone
(
change
.
node1
(),
change
.
node2
());
90
91
default
:
92
GUM_ERROR
(
OperationNotAllowed
,
93
"edge modifications are not "
94
"supported by StructuralConstraintIndegree"
);
95
}
96
}
97
98
/// notify the constraint of a modification of the graph
99
INLINE
void
StructuralConstraintIndegree
::
modifyGraphAlone
(
const
ArcAddition
&
change
) {}
100
101
/// notify the constraint of a modification of the graph
102
INLINE
void
StructuralConstraintIndegree
::
modifyGraphAlone
(
const
ArcDeletion
&
change
) {}
103
104
/// notify the constraint of a modification of the graph
105
INLINE
void
StructuralConstraintIndegree
::
modifyGraphAlone
(
const
ArcReversal
&
change
) {}
106
107
/// notify the constraint of a modification of the graph
108
INLINE
void
StructuralConstraintIndegree
::
modifyGraphAlone
(
const
GraphChange
&
change
) {}
109
110
/// indicates whether a change will always violate the constraint
111
INLINE
bool
112
StructuralConstraintIndegree
::
isAlwaysInvalidAlone
(
const
GraphChange
&
change
)
const
{
113
if
((
change
.
type
() ==
GraphChangeType
::
ARC_ADDITION
)
114
&& (
_Indegree_max_parents_
[
change
.
node2
()] == 0)) {
115
return
true
;
116
}
else
if
((
change
.
type
() ==
GraphChangeType
::
ARC_REVERSAL
)
117
&& (
_Indegree_max_parents_
[
change
.
node1
()] == 0)) {
118
return
true
;
119
}
else
{
120
return
false
;
121
}
122
}
123
124
/// sets the indegree for a given set of nodes
125
INLINE
void
126
StructuralConstraintIndegree
::
setIndegree
(
const
NodeProperty
<
Size
>&
max_indegree
) {
127
for
(
const
auto
&
degree
:
max_indegree
) {
128
_Indegree_max_parents_
.
set
(
degree
.
first
,
degree
.
second
);
129
}
130
}
131
132
/// resets the max indegree
133
INLINE
void
StructuralConstraintIndegree
::
setMaxIndegree
(
Size
max_indegree
,
bool
update_all
) {
134
if
(
update_all
) {
135
for
(
auto
&
degree
:
_Indegree_max_parents_
) {
136
degree
.
second
=
max_indegree
;
137
}
138
}
139
140
_Indegree_max_indegree_
=
max_indegree
;
141
}
142
143
// include all the methods applicable to the whole class hierarchy
144
#
define
GUM_CONSTRAINT_CLASS_NAME
StructuralConstraintIndegree
145
#
include
<
agrum
/
BN
/
learning
/
constraints
/
structuralConstraintPatternInline
.
h
>
146
#
undef
GUM_CONSTRAINT_CLASS_NAME
147
148
}
/* namespace learning */
149
150
}
/* namespace gum */
151
152
#
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