aGrUM  0.21.0
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, the inverse change is put into the tabu list. For instance, if the
59  * learning algorithm adds an arc (X, Y), then the "Deletion of Arc (X,Y)" operation
60  * is inserted into the tabu list. If the operation performed is an arc (X,Y)
61  * reversal, then the "Reversal of Arc (Y,X)" operation is added to the tabu
62  * list.
63  * @ingroup learning_group
64  */
66  public:
67  // ##########################################################################
68  /// @name Constructors / Destructors
69  // ##########################################################################
70  /// @{
71 
72  /// default constructor
74 
75  /// constructor starting with a given graph
76  StructuralConstraintTabuList(const DiGraph& graph);
77 
78  /// copy constructor
80 
81  /// move constructor
83 
84  /// destructor
86 
87  /// @}
88 
89  // ##########################################################################
90  /// @name Operators
91  // ##########################################################################
92  /// @{
93 
94  /// copy operator
96 
97  /// move operator
99 
100  /// @}
101 
102  // ##########################################################################
103  /// @name Specific Accessors / Modifiers
104  // ##########################################################################
105  /// @{
106 
107  /// sets the size of the tabu list
108  void setTabuListSize(Size new_size);
109 
110  /// sets a new graph from which we will perform checkings
111  void setGraphAlone(const DiGraph& graph);
112 
113  /// notify the constraint of a modification of the graph
114  /** @warning If an already existing arc is added nothing is done. In
115  * particular, no exception is raised.
116  * @throws InvalidNode exception is thrown if an arc (x,y) is added and x
117  * or y does not belong to the graph nodes */
118  void modifyGraphAlone(const ArcAddition& change);
119 
120  /// notify the constraint of a modification of the graph
121  /** @warning If a nonexisting arc is removed, nothing is done. In
122  * particular, no exception is raised. */
123  void modifyGraphAlone(const ArcDeletion& change);
124 
125  /// notify the constraint of a modification of the graph
126  /** @warning If an already existing arc is added, or if a nonexisting arc
127  * is removed, nothing is done. In particular, no exception is raised.
128  * @throws InvalidNode exception is thrown if at least one extremity of
129  * the arc does not belong to the graph nodes */
130  void modifyGraphAlone(const ArcReversal& change);
131 
132  /// notify the constraint of a modification of the graph
133  /** @warning If an already existing arc is added, or if a nonexisting arc
134  * is removed, nothing is done. In particular, no exception is raised.
135  * @throws InvalidNode exception is thrown if an arc (x,y) is added and x
136  * or y does not belong to the graph nodes */
137  void modifyGraphAlone(const GraphChange& change);
138 
139  /// indicates whether a change will always violate the constraint
140  /** Some learning algorithms need examine several times whether a given
141  * graph change can be applied. For instance, the first time arc (X,Y)
142  * addition is considered, the learning algorithm may discard this change
143  * because it violates the structural constraint (e.g., if the latter
144  * enforces a DAG structure, this arc addition might induce a directed
145  * cycle), but, later on, other arc removal may induce that the arc
146  * addition
147  * is now possible. Such change is thus not always invalid. Conversely,
148  * there are changes that can be discarded once and for all. For instance,
149  * in a 2TBN structure, it is always impossible to add a backward-time
150  * arc.
151  * Such graph changes are always invalid and are therefore tagged as such
152  * by the isAlwaysInvalid method. */
153  bool isAlwaysInvalidAlone(const GraphChange& change) const;
154 
155  /// checks whether the constraints enable to add arc (x,y)
156  /** an arc can be added if and only if its extremal nodes belong to the
157  * graph and the arc does not already exist. */
158  bool checkArcAdditionAlone(NodeId x, NodeId y) const;
159 
160  /// checks whether the constraints enable to remove arc (x,y)
161  /** an arc can be removed if and only if the arc exists. */
162  bool checkArcDeletionAlone(NodeId x, NodeId y) const;
163 
164  /// checks whether the constraints enable to reverse arc (x,y)
165  /** an arc can be reversed if and only if it exists and arc (y,x)
166  * does not. */
167  bool checkArcReversalAlone(NodeId x, NodeId y) const;
168 
169  /// checks whether the constraints enable to perform a graph change
170  /** An arc can be added if and only if its extremal nodes belong to the
171  * graph and the arc does not already exist.
172  * An arc can be removed if and only if the arc exists.
173  * An arc (x,y) can be reversed if and only if it exists and arc (y,x)
174  * does not. */
175  bool checkModificationAlone(const GraphChange& change) const;
176 
177  /// checks whether the constraints enable to add an arc
178  /** an arc can be added if and only if its extremal nodes belong to the
179  * graph and the arc does not already exist. */
180  bool checkModificationAlone(const ArcAddition& change) const;
181 
182  /// checks whether the constraints enable to remove an arc
183  /** an arc can be removed if and only if the arc exists. */
184  bool checkModificationAlone(const ArcDeletion& change) const;
185 
186  /// checks whether the constraints enable to reverse an arc
187  /** an arc (x,y) can be reversed if and only if it exists and arc (y,x)
188  * does not. */
189  bool checkModificationAlone(const ArcReversal& change) const;
190 
191  /// @}
192 
193 #ifndef DOXYGEN_SHOULD_SKIP_THIS
194 // include the set of methods that enable the structural constraint to
195 // be standalone, i.e., that it needs not be included into a
196 // StructuralConstraintSetStatic to be used by learning algorithms
197 # define GUM_CONSTRAINT_CLASS_NAME StructuralConstraintTabuList
198 # include <agrum/BN/learning/constraints/structuralConstraintPatternHeader.h>
199 # undef GUM_CONSTRAINT_CLASS_NAME
200 #endif // DOXYGEN_SHOULD_SKIP_THIS
201 
202  protected:
203  /// the tabu list
205 
206  /// the index of the oldest element
208  };
209 
210  } /* namespace learning */
211 
212 } /* namespace gum */
213 
214 /// include the inlined functions if necessary
215 #ifndef GUM_NO_INLINE
216 # include <agrum/BN/learning/constraints/structuralConstraintTabuList_inl.h>
217 #endif /* GUM_NO_INLINE */
218 
219 #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