aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
structuralConstraintTabuList.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 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
100 
101  /// move operator
103 
104  /// @}
105 
106  // ##########################################################################
107  /// @name Specific Accessors / Modifiers
108  // ##########################################################################
109  /// @{
110 
111  /// sets the size of the tabu list
112  void setTabuListSize(Size new_size);
113 
114  /// sets a new graph from which we will perform checkings
115  void setGraphAlone(const DiGraph& graph);
116 
117  /// notify the constraint of a modification of the graph
118  /** @warning If an already existing arc is added nothing is done. In
119  * particular, no exception is raised.
120  * @throws InvalidNode exception is thrown if an arc (x,y) is added and x
121  * or y does not belong to the graph nodes */
122  void modifyGraphAlone(const ArcAddition& change);
123 
124  /// notify the constraint of a modification of the graph
125  /** @warning If a nonexisting arc is removed, nothing is done. In
126  * particular, no exception is raised. */
127  void modifyGraphAlone(const ArcDeletion& change);
128 
129  /// notify the constraint of a modification of the graph
130  /** @warning If an already existing arc is added, or if a nonexisting arc
131  * is removed, nothing is done. In particular, no exception is raised.
132  * @throws InvalidNode exception is thrown if at least one extremity of
133  * the arc does not belong to the graph nodes */
134  void modifyGraphAlone(const ArcReversal& change);
135 
136  /// notify the constraint of a modification of the graph
137  /** @warning If an already existing arc is added, or if a nonexisting arc
138  * is removed, nothing is done. In particular, no exception is raised.
139  * @throws InvalidNode exception is thrown if an arc (x,y) is added and x
140  * or y does not belong to the graph nodes */
141  void modifyGraphAlone(const GraphChange& change);
142 
143  /// indicates whether a change will always violate the constraint
144  /** Some learning algorithms need examine several times whether a given
145  * graph change can be applied. For instance, the first time arc (X,Y)
146  * addition is considered, the learning algorithm may discard this change
147  * because it violates the structural constraint (e.g., if the latter
148  * enforces a DAG structure, this arc addition might induce a directed
149  * cycle), but, later on, other arc removal may induce that the arc
150  * addition
151  * is now possible. Such change is thus not always invalid. Conversely,
152  * there are changes that can be discarded once and for all. For instance,
153  * in a 2TBN structure, it is always impossible to add a backward-time
154  * arc.
155  * Such graph changes are always invalid and are therefore tagged as such
156  * by the isAlwaysInvalid method. */
157  bool isAlwaysInvalidAlone(const GraphChange& change) const;
158 
159  /// checks whether the constraints enable to add arc (x,y)
160  /** an arc can be added if and only if its extremal nodes belong to the
161  * graph and the arc does not already exist. */
162  bool checkArcAdditionAlone(NodeId x, NodeId y) const;
163 
164  /// checks whether the constraints enable to remove arc (x,y)
165  /** an arc can be removed if and only if the arc exists. */
166  bool checkArcDeletionAlone(NodeId x, NodeId y) const;
167 
168  /// checks whether the constraints enable to reverse arc (x,y)
169  /** an arc can be reversed if and only if it exists and arc (y,x)
170  * does not. */
171  bool checkArcReversalAlone(NodeId x, NodeId y) const;
172 
173  /// checks whether the constraints enable to perform a graph change
174  /** An arc can be added if and only if its extremal nodes belong to the
175  * graph and the arc does not already exist.
176  * An arc can be removed if and only if the arc exists.
177  * An arc (x,y) can be reversed if and only if it exists and arc (y,x)
178  * does not. */
179  bool checkModificationAlone(const GraphChange& change) const;
180 
181  /// checks whether the constraints enable to add an arc
182  /** an arc can be added if and only if its extremal nodes belong to the
183  * graph and the arc does not already exist. */
184  bool checkModificationAlone(const ArcAddition& change) const;
185 
186  /// checks whether the constraints enable to remove an arc
187  /** an arc can be removed if and only if the arc exists. */
188  bool checkModificationAlone(const ArcDeletion& change) const;
189 
190  /// checks whether the constraints enable to reverse an arc
191  /** an arc (x,y) can be reversed if and only if it exists and arc (y,x)
192  * does not. */
193  bool checkModificationAlone(const ArcReversal& change) const;
194 
195  /// @}
196 
197 #ifndef DOXYGEN_SHOULD_SKIP_THIS
198 // include the set of methods that enable the structural constraint to
199 // be standalone, i.e., that it needs not be included into a
200 // StructuralConstraintSetStatic to be used by learning algorithms
201 # define GUM_CONSTRAINT_CLASS_NAME StructuralConstraintTabuList
202 # include <agrum/BN/learning/constraints/structuralConstraintPatternHeader.h>
203 # undef GUM_CONSTRAINT_CLASS_NAME
204 #endif // DOXYGEN_SHOULD_SKIP_THIS
205 
206  protected:
207  /// the tabu list
209 
210  /// the index of the oldest element
212  };
213 
214  } /* namespace learning */
215 
216 } /* namespace gum */
217 
218 /// include the inlined functions if necessary
219 #ifndef GUM_NO_INLINE
220 # include <agrum/BN/learning/constraints/structuralConstraintTabuList_inl.h>
221 #endif /* GUM_NO_INLINE */
222 
223 #endif /* GUM_LEARNING_STRUCTURAL_TABU_LIST_H */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
void setTabuListSize(Size new_size)
sets the size of the tabu list
StructuralConstraintTabuList & operator=(StructuralConstraintTabuList &&from)
move operator
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
NodeId TabuList__offset_
the index of the oldest element
StructuralConstraintTabuList(StructuralConstraintTabuList &&from)
move constructor
bool isAlwaysInvalidAlone(const GraphChange &change) const
indicates whether a change will always violate the constraint
Bijection< GraphChange, NodeId > TabuList__changes_
the tabu list
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)
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