aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
structuralConstraintIndegree.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 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  */
49  protected virtual StructuralConstraintSetStatic<
51  public:
52  // ##########################################################################
53  /// @name Constructors / Destructors
54  // ##########################################################################
55  /// @{
56 
57  /// default constructor
59 
60  /// constructor starting with an empty graph with a given number of nodes
61  StructuralConstraintIndegree(Size nb_nodes, Size max_indegree);
62 
63  /// constructor starting with a given graph
64  StructuralConstraintIndegree(const DiGraph& graph, Size max_indegree);
65 
66  /// copy constructor
68 
69  /// move constructor
71 
72  /// destructor
74 
75  /// @}
76 
77  // ##########################################################################
78  /// @name Operators
79  // ##########################################################################
80  /// @{
81 
82  /// copy operator
85 
86  /// move operator
88 
89  /// @}
90 
91  // ##########################################################################
92  /// @name Specific Accessors / Modifiers
93  // ##########################################################################
94  /// @{
95 
96  /// sets the default max indegree for all the nodes in the property
97  void setIndegree(const NodeProperty< Size >& max_indegree);
98 
99  /** @brief resets the default max indegree and possibly updates the
100  * indegree of all nodes */
101  void setMaxIndegree(Size max_indegree, bool update_all_node = false);
102 
103  /// sets a new graph from which we will perform checkings
104  void setGraphAlone(const DiGraph& graph);
105 
106  /// notify the constraint of a modification of the graph
107  /** @warning If an already existing arc is added, nothing is done. In
108  * particular, no exception is raised.
109  * @throws InvalidNode exception is thrown if an arc (x,y) is added and x
110  * or y does not belong to the graph nodes
111  * @throws OutOfUpperBound exception is thrown if the indegree constraint
112  * is violated by the arc addition. */
113  void modifyGraphAlone(const ArcAddition& change);
114 
115  /// notify the constraint of a modification of the graph
116  /** @warning If a nonexisting arc is removed, nothing is done. In
117  * particular, no exception is raised. */
118  void modifyGraphAlone(const ArcDeletion& change);
119 
120  /// notify the constraint of a modification of the graph
121  /** @warning If an already existing arc is added, or if a nonexisting arc
122  * is removed, nothing is done. In particular, no exception is raised.
123  * @throws InvalidNode exception is thrown if an arc (x,y) is added and x
124  * or y does not belong to the graph nodes
125  * @throws OutOfUpperBound exception is thrown if the indegree constraint
126  * is violated by the arc reversal. */
127  void modifyGraphAlone(const ArcReversal& 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 an arc (x,y) is added and x
133  * or y does not belong to the graph nodes
134  * @throws OutOfUpperBound exception is thrown if the indegree constraint
135  * is violated by an arc addition or reversal. */
136  void modifyGraphAlone(const GraphChange& change);
137 
138  /// indicates whether a change will always violate the constraint
139  /** Some learning algorithms need examine several times whether a given
140  * graph change can be applied. For instance, the first time arc (X,Y)
141  * addition is considered, the learning algorithm may discard this change
142  * because it violates the structural constraint (e.g., if the latter
143  * enforces a DAG structure, this arc addition might induce a directed
144  * cycle), but, later on, other arc removal may induce that the arc
145  * addition
146  * is now possible. Such change is thus not always invalid. Conversely,
147  * there are changes that can be discarded once and for all. For instance,
148  * in a 2TBN structure, it is always impossible to add a backward-time
149  * arc.
150  * Such graph changes are always invalid and are therefore tagged as such
151  * by the isAlwaysInvalid method. */
152  bool isAlwaysInvalidAlone(const GraphChange& change) const;
153 
154  /// checks whether the constraints enable to add arc (x,y)
155  /** an arc can be added if and only if its extremal nodes belong to the
156  * graph and the arc does not already exist and its addition would not
157  * violate the indegree constraint of y. */
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 and its addition would not violate the indegree
167  * constraint of x. */
168  bool checkArcReversalAlone(NodeId x, NodeId y) const;
169 
170  /// checks whether the constraints enable to add an arc
171  /** an arc can be added if and only if its extremal nodes belong to the
172  * graph and the arc does not already exist and its addition would not
173  * violate the indegree constraint of y. */
174  bool checkModificationAlone(const ArcAddition& change) const;
175 
176  /// checks whether the constraints enable to remove an arc
177  /** an arc can be removed if and only if the arc exists. */
178  bool checkModificationAlone(const ArcDeletion& change) const;
179 
180  /// checks whether the constraints enable to reverse an arc
181  /** an arc can be reversed if and only if it exists and arc (y,x)
182  * does not and its addition would not violate the indegree
183  * constraint of x. */
184  bool checkModificationAlone(const ArcReversal& change) const;
185 
186  /// checks whether the constraints enable to perform a graph change
187  /** An arc can be added if and only if its extremal nodes belong to the
188  * graph and the arc does not already exist and its addition would not
189  * violate the indegree constraint of y.
190  * An arc can be removed if and only if the arc exists.
191  * An arc can be reversed if and only if it exists and arc (y,x)
192  * does not and its addition would not violate the indegree
193  * constraint of x. */
194  bool checkModificationAlone(const GraphChange& change) const;
195 
196  /// @}
197 
198 #ifndef DOXYGEN_SHOULD_SKIP_THIS
199 // include the set of methods that enable the structural constraint to
200 // be standalone, i.e., that it needs not be included into a
201 // StructuralConstraintSetStatic to be used by learning algorithms
202 # define GUM_CONSTRAINT_CLASS_NAME StructuralConstraintIndegree
203 # include <agrum/BN/learning/constraints/structuralConstraintPatternHeader.h>
204 # undef GUM_CONSTRAINT_CLASS_NAME
205 #endif // DOXYGEN_SHOULD_SKIP_THIS
206 
207  protected:
208  /// the max number of parents per node
210 
211  /// a default max indegree to assign for nodes without specified indegree
213  };
214 
215  } /* namespace learning */
216 
217 } /* namespace gum */
218 
219 /// include the inlined functions if necessary
220 #ifndef GUM_NO_INLINE
221 # include <agrum/BN/learning/constraints/structuralConstraintIndegree_inl.h>
222 #endif /* GUM_NO_INLINE */
223 
224 #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:669
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
NodeProperty< Size > Indegree__max_parents_
the max number of parents per node
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
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)