aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
structuralConstraintTabuList.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  * By default, the size of the tabu list is 2, but it can be changed by the user
27  * using method setTabuSize (). Each time you modify the graph you learn, the
28  * inverse change is put into the tabu list. For instance, if the learning
29  * algorithm adds an arc (X, Y), then the "Deletion of Arc (X,Y)" operation is
30  * inserted into the tabu list. If the operation performed is an arc (X,Y)
31  * reversal, then the "Reversal of Arc (Y,X)" operation is added to the tabu
32  *list.
33  *
34  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
35  */
36 #ifndef GUM_LEARNING_STRUCTURAL_CONSTRAINT_TABU_LIST_H
37 #define GUM_LEARNING_STRUCTURAL_CONSTRAINT_TABU_LIST_H
38 
39 #include <limits>
40 
41 #include <agrum/agrum.h>
42 #include <agrum/tools/core/bijection.h>
43 #include <agrum/BN/learning/constraints/structuralConstraint.h>
44 #include <agrum/BN/learning/structureUtils/graphChange.h>
45 
46 #define GUM_STRUCTURAL_CONSTRAINT_TABU_LIST_DEFAULT_SIZE 2
47 
48 namespace gum {
49 
50  namespace learning {
51 
52  /** @class StructuralConstraintTabuList
53  * @brief The class imposing a N-sized tabu list as a structural constraints
54  * for learning algorithms
55  *
56  * By default, the size of the tabu list is 2, but it can be changed by the
57  * user using method setTabuSize (). Each time you modify the graph you
58  *learn,
59  * the inverse change is put into the tabu list. For instance, if the
60  *learning
61  * algorithm adds an arc (X, Y), then the "Deletion of Arc (X,Y)" operation
62  *is
63  * inserted into the tabu list. If the operation performed is an arc (X,Y)
64  * reversal, then the "Reversal of Arc (Y,X)" operation is added to the tabu
65  * list.
66  * @ingroup learning_group
67  */
69  public:
70  // ##########################################################################
71  /// @name Constructors / Destructors
72  // ##########################################################################
73  /// @{
74 
75  /// default constructor
77 
78  /// constructor starting with a given graph
79  StructuralConstraintTabuList(const DiGraph& graph);
80 
81  /// copy constructor
83 
84  /// move constructor
86 
87  /// destructor
89 
90  /// @}
91 
92  // ##########################################################################
93  /// @name Operators
94  // ##########################################################################
95  /// @{
96 
97  /// copy operator
99 
100  /// move operator
102 
103  /// @}
104 
105  // ##########################################################################
106  /// @name Specific Accessors / Modifiers
107  // ##########################################################################
108  /// @{
109 
110  /// sets the size of the tabu list
111  void setTabuListSize(Size new_size);
112 
113  /// sets a new graph from which we will perform checkings
114  void setGraphAlone(const DiGraph& graph);
115 
116  /// notify the constraint of a modification of the graph
117  /** @warning If an already existing arc is added nothing is done. In
118  * particular, no exception is raised.
119  * @throws InvalidNode exception is thrown if an arc (x,y) is added and x
120  * or y does not belong to the graph nodes */
121  void modifyGraphAlone(const ArcAddition& change);
122 
123  /// notify the constraint of a modification of the graph
124  /** @warning If a nonexisting arc is removed, nothing is done. In
125  * particular, no exception is raised. */
126  void modifyGraphAlone(const ArcDeletion& change);
127 
128  /// notify the constraint of a modification of the graph
129  /** @warning If an already existing arc is added, or if a nonexisting arc
130  * is removed, nothing is done. In particular, no exception is raised.
131  * @throws InvalidNode exception is thrown if at least one extremity of
132  * the arc does not belong to the graph nodes */
133  void modifyGraphAlone(const ArcReversal& change);
134 
135  /// notify the constraint of a modification of the graph
136  /** @warning If an already existing arc is added, or if a nonexisting arc
137  * is removed, nothing is done. In particular, no exception is raised.
138  * @throws InvalidNode exception is thrown if an arc (x,y) is added and x
139  * or y does not belong to the graph nodes */
140  void modifyGraphAlone(const GraphChange& change);
141 
142  /// indicates whether a change will always violate the constraint
143  /** Some learning algorithms need examine several times whether a given
144  * graph change can be applied. For instance, the first time arc (X,Y)
145  * addition is considered, the learning algorithm may discard this change
146  * because it violates the structural constraint (e.g., if the latter
147  * enforces a DAG structure, this arc addition might induce a directed
148  * cycle), but, later on, other arc removal may induce that the arc
149  * addition
150  * is now possible. Such change is thus not always invalid. Conversely,
151  * there are changes that can be discarded once and for all. For instance,
152  * in a 2TBN structure, it is always impossible to add a backward-time
153  * arc.
154  * Such graph changes are always invalid and are therefore tagged as such
155  * by the isAlwaysInvalid method. */
156  bool isAlwaysInvalidAlone(const GraphChange& change) const;
157 
158  /// checks whether the constraints enable to add arc (x,y)
159  /** an arc can be added if and only if its extremal nodes belong to the
160  * graph and the arc does not already exist. */
161  bool checkArcAdditionAlone(NodeId x, NodeId y) const;
162 
163  /// checks whether the constraints enable to remove arc (x,y)
164  /** an arc can be removed if and only if the arc exists. */
165  bool checkArcDeletionAlone(NodeId x, NodeId y) const;
166 
167  /// checks whether the constraints enable to reverse arc (x,y)
168  /** an arc can be reversed if and only if it exists and arc (y,x)
169  * does not. */
170  bool checkArcReversalAlone(NodeId x, NodeId y) const;
171 
172  /// checks whether the constraints enable to perform a graph change
173  /** An arc can be added if and only if its extremal nodes belong to the
174  * graph and the arc does not already exist.
175  * An arc can be removed if and only if the arc exists.
176  * An arc (x,y) can be reversed if and only if it exists and arc (y,x)
177  * does not. */
178  bool checkModificationAlone(const GraphChange& change) const;
179 
180  /// checks whether the constraints enable to add an arc
181  /** an arc can be added if and only if its extremal nodes belong to the
182  * graph and the arc does not already exist. */
183  bool checkModificationAlone(const ArcAddition& change) const;
184 
185  /// checks whether the constraints enable to remove an arc
186  /** an arc can be removed if and only if the arc exists. */
187  bool checkModificationAlone(const ArcDeletion& change) const;
188 
189  /// checks whether the constraints enable to reverse an arc
190  /** an arc (x,y) can be reversed if and only if it exists and arc (y,x)
191  * does not. */
192  bool checkModificationAlone(const ArcReversal& change) const;
193 
194  /// @}
195 
196 #ifndef DOXYGEN_SHOULD_SKIP_THIS
197 // include the set of methods that enable the structural constraint to
198 // be standalone, i.e., that it needs not be included into a
199 // StructuralConstraintSetStatic to be used by learning algorithms
200 # define GUM_CONSTRAINT_CLASS_NAME StructuralConstraintTabuList
201 # include <agrum/BN/learning/constraints/structuralConstraintPatternHeader.h>
202 # undef GUM_CONSTRAINT_CLASS_NAME
203 #endif // DOXYGEN_SHOULD_SKIP_THIS
204 
205  protected:
206  /// the tabu list
208 
209  /// the index of the oldest element
211  };
212 
213  } /* namespace learning */
214 
215 } /* namespace gum */
216 
217 /// include the inlined functions if necessary
218 #ifndef GUM_NO_INLINE
219 # include <agrum/BN/learning/constraints/structuralConstraintTabuList_inl.h>
220 #endif /* GUM_NO_INLINE */
221 
222 #endif /* GUM_LEARNING_STRUCTURAL_TABU_LIST_H */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
void setTabuListSize(Size new_size)
sets the size of the tabu list
StructuralConstraintTabuList & operator=(StructuralConstraintTabuList &&from)
move operator
NodeId _TabuList_offset_
the index of the oldest element
void setGraphAlone(const DiGraph &graph)
sets a new graph from which we will perform checkings
StructuralConstraintTabuList(const StructuralConstraintTabuList &from)
copy constructor
bool checkArcReversalAlone(NodeId x, NodeId y) const
checks whether the constraints enable to reverse arc (x,y)
StructuralConstraintTabuList & operator=(const StructuralConstraintTabuList &from)
copy operator
bool checkArcDeletionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to remove arc (x,y)
StructuralConstraintTabuList(const DiGraph &graph)
constructor starting with a given graph
StructuralConstraintTabuList(StructuralConstraintTabuList &&from)
move constructor
bool isAlwaysInvalidAlone(const GraphChange &change) const
indicates whether a change will always violate the constraint
The class imposing a N-sized tabu list as a structural constraints for learning algorithms.
bool checkModificationAlone(const ArcReversal &change) const
checks whether the constraints enable to reverse an arc
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)
Bijection< GraphChange, NodeId > _TabuList_changes_
the tabu list
bool checkArcAdditionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to add arc (x,y)
void modifyGraphAlone(const GraphChange &change)
notify the constraint of a modification of the graph