aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
structuralConstraintIndegree.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 for structural constraints limiting the number of parents
24  * of nodes in a directed graph
25  *
26  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 #ifndef GUM_LEARNING_STRUCTURAL_CONSTRAINT_INDEGREE_H
29 #define GUM_LEARNING_STRUCTURAL_CONSTRAINT_INDEGREE_H
30 
31 #include <limits>
32 
33 #include <agrum/agrum.h>
34 #include <agrum/BN/learning/constraints/structuralConstraintDiGraph.h>
35 #include <agrum/BN/learning/constraints/structuralConstraintSetStatic.h>
36 
37 namespace gum {
38 
39  namespace learning {
40 
41  /** @class StructuralConstraintIndegree
42  * @brief the class for structural constraints limiting the number of
43  *parents
44  * of nodes in a directed graph
45  *
46  * @ingroup learning_group
47  */
50  public:
51  // ##########################################################################
52  /// @name Constructors / Destructors
53  // ##########################################################################
54  /// @{
55 
56  /// default constructor
58 
59  /// constructor starting with an empty graph with a given number of nodes
60  StructuralConstraintIndegree(Size nb_nodes, Size max_indegree);
61 
62  /// constructor starting with a given graph
63  StructuralConstraintIndegree(const DiGraph& graph, Size max_indegree);
64 
65  /// copy constructor
67 
68  /// move constructor
70 
71  /// destructor
73 
74  /// @}
75 
76  // ##########################################################################
77  /// @name Operators
78  // ##########################################################################
79  /// @{
80 
81  /// copy operator
83 
84  /// move operator
86 
87  /// @}
88 
89  // ##########################################################################
90  /// @name Specific Accessors / Modifiers
91  // ##########################################################################
92  /// @{
93 
94  /// sets the default max indegree for all the nodes in the property
95  void setIndegree(const NodeProperty< Size >& max_indegree);
96 
97  /** @brief resets the default max indegree and possibly updates the
98  * indegree of all nodes */
99  void setMaxIndegree(Size max_indegree, bool update_all_node = false);
100 
101  /// sets a new graph from which we will perform checkings
102  void setGraphAlone(const DiGraph& graph);
103 
104  /// notify the constraint of a modification of the graph
105  /** @warning If an already existing arc is added, nothing is done. In
106  * particular, no exception is raised.
107  * @throws InvalidNode exception is thrown if an arc (x,y) is added and x
108  * or y does not belong to the graph nodes
109  * @throws OutOfUpperBound exception is thrown if the indegree constraint
110  * is violated by the arc addition. */
111  void modifyGraphAlone(const ArcAddition& change);
112 
113  /// notify the constraint of a modification of the graph
114  /** @warning If a nonexisting arc is removed, nothing is done. In
115  * particular, no exception is raised. */
116  void modifyGraphAlone(const ArcDeletion& change);
117 
118  /// notify the constraint of a modification of the graph
119  /** @warning If an already existing arc is added, or if a nonexisting arc
120  * is removed, nothing is done. In particular, no exception is raised.
121  * @throws InvalidNode exception is thrown if an arc (x,y) is added and x
122  * or y does not belong to the graph nodes
123  * @throws OutOfUpperBound exception is thrown if the indegree constraint
124  * is violated by the arc reversal. */
125  void modifyGraphAlone(const ArcReversal& change);
126 
127  /// notify the constraint of a modification of the graph
128  /** @warning If an already existing arc is added, or if a nonexisting arc
129  * is removed, nothing is done. In particular, no exception is raised.
130  * @throws InvalidNode exception is thrown if an arc (x,y) is added and x
131  * or y does not belong to the graph nodes
132  * @throws OutOfUpperBound exception is thrown if the indegree constraint
133  * is violated by an arc addition or reversal. */
134  void modifyGraphAlone(const GraphChange& change);
135 
136  /// indicates whether a change will always violate the constraint
137  /** Some learning algorithms need examine several times whether a given
138  * graph change can be applied. For instance, the first time arc (X,Y)
139  * addition is considered, the learning algorithm may discard this change
140  * because it violates the structural constraint (e.g., if the latter
141  * enforces a DAG structure, this arc addition might induce a directed
142  * cycle), but, later on, other arc removal may induce that the arc
143  * addition
144  * is now possible. Such change is thus not always invalid. Conversely,
145  * there are changes that can be discarded once and for all. For instance,
146  * in a 2TBN structure, it is always impossible to add a backward-time
147  * arc.
148  * Such graph changes are always invalid and are therefore tagged as such
149  * by the isAlwaysInvalid method. */
150  bool isAlwaysInvalidAlone(const GraphChange& change) const;
151 
152  /// checks whether the constraints enable to add arc (x,y)
153  /** an arc can be added if and only if its extremal nodes belong to the
154  * graph and the arc does not already exist and its addition would not
155  * violate the indegree constraint of y. */
156  bool checkArcAdditionAlone(NodeId x, NodeId y) const;
157 
158  /// checks whether the constraints enable to remove arc (x,y)
159  /** an arc can be removed if and only if the arc exists. */
160  bool checkArcDeletionAlone(NodeId x, NodeId y) const;
161 
162  /// checks whether the constraints enable to reverse arc (x,y)
163  /** an arc can be reversed if and only if it exists and arc (y,x)
164  * does not and its addition would not violate the indegree
165  * constraint of x. */
166  bool checkArcReversalAlone(NodeId x, NodeId y) const;
167 
168  /// checks whether the constraints enable to add an arc
169  /** an arc can be added if and only if its extremal nodes belong to the
170  * graph and the arc does not already exist and its addition would not
171  * violate the indegree constraint of y. */
172  bool checkModificationAlone(const ArcAddition& change) const;
173 
174  /// checks whether the constraints enable to remove an arc
175  /** an arc can be removed if and only if the arc exists. */
176  bool checkModificationAlone(const ArcDeletion& change) const;
177 
178  /// checks whether the constraints enable to reverse an arc
179  /** an arc can be reversed if and only if it exists and arc (y,x)
180  * does not and its addition would not violate the indegree
181  * constraint of x. */
182  bool checkModificationAlone(const ArcReversal& change) const;
183 
184  /// checks whether the constraints enable to perform a graph change
185  /** An arc can be added if and only if its extremal nodes belong to the
186  * graph and the arc does not already exist and its addition would not
187  * violate the indegree constraint of y.
188  * An arc can be removed if and only if the arc exists.
189  * An arc can be reversed if and only if it exists and arc (y,x)
190  * does not and its addition would not violate the indegree
191  * constraint of x. */
192  bool checkModificationAlone(const GraphChange& 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 StructuralConstraintIndegree
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 max number of parents per node
208 
209  /// a default max indegree to assign for nodes without specified indegree
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/structuralConstraintIndegree_inl.h>
220 #endif /* GUM_NO_INLINE */
221 
222 #endif /* GUM_LEARNING_STRUCTURAL_CONSTRAINT_INDEGREE_H */
void setIndegree(const NodeProperty< Size > &max_indegree)
sets the default max indegree for all the nodes in the property
the class for structural constraints limiting the number of parents of nodes in a directed graph ...
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
void setMaxIndegree(Size max_indegree, bool update_all_node=false)
resets the default max indegree and possibly updates the indegree of all nodes
StructuralConstraintIndegree & operator=(StructuralConstraintIndegree &&from)
move operator
bool isAlwaysInvalidAlone(const GraphChange &change) const
indicates whether a change will always violate the constraint
bool checkModificationAlone(const GraphChange &change) const
checks whether the constraints enable to perform a graph change
StructuralConstraintIndegree(Size nb_nodes, Size max_indegree)
constructor starting with an empty graph with a given number of nodes
bool checkArcDeletionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to remove arc (x,y)
bool checkArcAdditionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to add arc (x,y)
StructuralConstraintIndegree(const DiGraph &graph, Size max_indegree)
constructor starting with a given graph
StructuralConstraintIndegree(const StructuralConstraintIndegree &from)
copy constructor
Size _Indegree_max_indegree_
a default max indegree to assign for nodes without specified indegree
StructuralConstraintIndegree(StructuralConstraintIndegree &&from)
move constructor
NodeProperty< Size > _Indegree_max_parents_
the max number of parents per node
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)
void setGraphAlone(const DiGraph &graph)
sets a new graph from which we will perform checkings
StructuralConstraintIndegree & operator=(const StructuralConstraintIndegree &from)
copy operator
void modifyGraphAlone(const GraphChange &change)
notify the constraint of a modification of the graph
bool checkArcReversalAlone(NodeId x, NodeId y) const
checks whether the constraints enable to reverse arc (x,y)