aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
structuralConstraintSliceOrder_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 structural constraint imposing a partial order over nodes
24
*
25
* In DBNs, it is forbidden to add arcs from nodes at time slice t to nodes at
26
* time slice s, where s < t. This class allows for taking this kind of
27
*constraint
28
* into account by imposing a partial order over the nodes: arcs (X,Y) can then
29
* only be added if X <= Y in the partial order.
30
* @warning: there may exist free variables, that is variables whose order
31
* w.r.t. the other variables is unspecified. In this case, arcs adjacent
32
* to them can be constructed. The partial order is specified by assigning
33
* numbers to nodes (through a NodeProperty). Nodes without number (i.e., that
34
* do not belong to the property) are free.
35
*
36
* @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
37
*/
38
39
#
ifndef
DOXYGEN_SHOULD_SKIP_THIS
40
41
namespace
gum
{
42
43
namespace
learning
{
44
45
/// sets a new graph from which we will perform checkings
46
INLINE
void
47
StructuralConstraintSliceOrder
::
setGraphAlone
(
const
DiGraph
&
graph
) {}
48
49
/// checks whether the constraints enable to add arc (x,y)
50
INLINE
bool
51
StructuralConstraintSliceOrder
::
checkArcAdditionAlone
(
NodeId
x
,
52
NodeId
y
)
const
{
53
try
{
54
return
SliceOrder__order_
[
x
] <=
SliceOrder__order_
[
y
];
55
}
catch
(
const
Exception
&) {
return
true
; }
56
}
57
58
/// checks whether the constraints enable to remove arc (x,y)
59
INLINE
bool
60
StructuralConstraintSliceOrder
::
checkArcDeletionAlone
(
NodeId
x
,
61
NodeId
y
)
const
{
62
return
true
;
63
}
64
65
/// checks whether the constraints enable to reverse arc (x,y)
66
INLINE
bool
67
StructuralConstraintSliceOrder
::
checkArcReversalAlone
(
NodeId
x
,
68
NodeId
y
)
const
{
69
try
{
70
return
SliceOrder__order_
[
x
] ==
SliceOrder__order_
[
y
];
71
}
catch
(
const
Exception
&) {
return
true
; }
72
}
73
74
/// notify the constraint of a modification of the graph
75
INLINE
void
StructuralConstraintSliceOrder
::
modifyGraphAlone
(
76
const
ArcAddition
&
change
) {}
77
78
/// notify the constraint of a modification of the graph
79
INLINE
void
StructuralConstraintSliceOrder
::
modifyGraphAlone
(
80
const
ArcDeletion
&
change
) {}
81
82
/// notify the constraint of a modification of the graph
83
INLINE
void
StructuralConstraintSliceOrder
::
modifyGraphAlone
(
84
const
ArcReversal
&
change
) {}
85
86
/// notify the constraint of a modification of the graph
87
INLINE
void
StructuralConstraintSliceOrder
::
modifyGraphAlone
(
88
const
GraphChange
&
change
) {}
89
90
/// indicates whether a change will always violate the constraint
91
INLINE
bool
StructuralConstraintSliceOrder
::
isAlwaysInvalidAlone
(
92
const
GraphChange
&
change
)
const
{
93
switch
(
change
.
type
()) {
94
case
GraphChangeType
::
ARC_ADDITION
:
95
try
{
96
return
(
SliceOrder__order_
[
change
.
node1
()]
97
>
SliceOrder__order_
[
change
.
node2
()]);
98
}
catch
(
const
Exception
&) {
return
false
; }
99
100
case
GraphChangeType
::
ARC_DELETION
:
101
return
false
;
102
103
case
GraphChangeType
::
ARC_REVERSAL
:
104
try
{
105
return
(
SliceOrder__order_
[
change
.
node1
()]
106
!=
SliceOrder__order_
[
change
.
node2
()]);
107
}
catch
(
const
Exception
&) {
return
false
; }
108
109
default
:
110
GUM_ERROR
(
OperationNotAllowed
,
111
"edge modifications are not "
112
"supported by SliceOrder constraints"
);
113
}
114
}
115
116
/// checks whether the constraints enable to add an arc
117
INLINE
bool
StructuralConstraintSliceOrder
::
checkModificationAlone
(
118
const
ArcAddition
&
change
)
const
{
119
return
checkArcAdditionAlone
(
change
.
node1
(),
change
.
node2
());
120
}
121
122
/// checks whether the constraints enable to remove an arc
123
INLINE
bool
StructuralConstraintSliceOrder
::
checkModificationAlone
(
124
const
ArcDeletion
&
change
)
const
{
125
return
checkArcDeletionAlone
(
change
.
node1
(),
change
.
node2
());
126
}
127
128
/// checks whether the constraints enable to reverse an arc
129
INLINE
bool
StructuralConstraintSliceOrder
::
checkModificationAlone
(
130
const
ArcReversal
&
change
)
const
{
131
return
checkArcReversalAlone
(
change
.
node1
(),
change
.
node2
());
132
}
133
134
/// checks whether the constraints enable to perform a graph change
135
INLINE
bool
StructuralConstraintSliceOrder
::
checkModificationAlone
(
136
const
GraphChange
&
change
)
const
{
137
switch
(
change
.
type
()) {
138
case
GraphChangeType
::
ARC_ADDITION
:
139
return
checkArcAdditionAlone
(
change
.
node1
(),
change
.
node2
());
140
141
case
GraphChangeType
::
ARC_DELETION
:
142
return
checkArcDeletionAlone
(
change
.
node1
(),
change
.
node2
());
143
144
case
GraphChangeType
::
ARC_REVERSAL
:
145
return
checkArcReversalAlone
(
change
.
node1
(),
change
.
node2
());
146
147
default
:
148
GUM_ERROR
(
OperationNotAllowed
,
149
"edge modifications are not "
150
"supported by StructuralConstraintSliceOrder"
);
151
}
152
}
153
154
/// sets the time slices of all the nodes in the property
155
INLINE
void
StructuralConstraintSliceOrder
::
setSliceOrder
(
156
const
NodeProperty
<
NodeId
>&
order
) {
157
SliceOrder__order_
=
order
;
158
}
159
160
/// sets the default time slice
161
INLINE
void
StructuralConstraintSliceOrder
::
setDefaultSlice
(
NodeId
slice
) {
162
for
(
auto
&
node
:
SliceOrder__order_
) {
163
node
.
second
=
slice
;
164
}
165
}
166
167
/// adds a new node in the slice order
168
INLINE
void
StructuralConstraintSliceOrder
::
addNode
(
NodeId
node
,
169
NodeId
slice
) {
170
SliceOrder__order_
.
set
(
node
,
slice
);
171
}
172
173
/// returns the current slice order
174
INLINE
const
NodeProperty
<
NodeId
>&
175
StructuralConstraintSliceOrder
::
sliceOrder
()
const
{
176
return
SliceOrder__order_
;
177
}
178
179
// include all the methods applicable to the whole class hierarchy
180
#
define
GUM_CONSTRAINT_CLASS_NAME
StructuralConstraintSliceOrder
181
#
include
<
agrum
/
BN
/
learning
/
constraints
/
structuralConstraintPatternInline
.
h
>
182
#
undef
GUM_CONSTRAINT_CLASS_NAME
183
184
}
/* namespace learning */
185
186
}
/* namespace gum */
187
188
#
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