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