aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
structuralConstraintDiGraph.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 base class for structural constraints used by learning algorithms
24  * that learn a directed graph structure
25  *
26  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 #ifndef GUM_LEARNING_STRUCTURAL_CONSTRAINT_DIGRAPH_H
29 #define GUM_LEARNING_STRUCTURAL_CONSTRAINT_DIGRAPH_H
30 
31 #include <agrum/agrum.h>
32 #include <agrum/tools/graphs/diGraph.h>
33 #include <agrum/BN/learning/constraints/structuralConstraint.h>
34 #include <agrum/BN/learning/structureUtils/graphChange.h>
35 
36 namespace gum {
37 
38  namespace learning {
39 
40  /** @class StructuralConstraintDiGraph
41  * @brief The base class for structural constraints used by learning
42  * algorithms that learn a directed graph structure
43  *
44  * This base should always be a virtual parents of the structural
45  *constraints
46  * classes. This will allow to combine different constraints into a single
47  * class
48  * @ingroup learning_group
49  */
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  StructuralConstraintDiGraph(Size nb_nodes);
62 
63  /// constructor starting with a given graph
64  StructuralConstraintDiGraph(const DiGraph& graph);
65 
66  /// copy constructor
68 
69  /// move constructor
71 
72  /// destructor
73  virtual ~StructuralConstraintDiGraph();
74 
75  /// @}
76 
77  // ##########################################################################
78  /// @name Operators
79  // ##########################################################################
80  /// @{
81 
82  /// copy operator
84 
85  /// move operator
87 
88  /// @}
89 
90  // ##########################################################################
91  /// @name Specific Accessors / Modifiers
92  // ##########################################################################
93  /// @{
94 
95  /// sets a new empty graph from which we will perform checkings
96  void setGraph(Size nb_nodes);
97 
98  /// sets a new graph from which we will perform checkings
99  void setGraphAlone(const DiGraph& graph);
100 
101  /// notify the constraint of a modification of the graph
102  /** @warning If an already existing arc is added nothing is done. In
103  * particular, no exception is raised.
104  * @throws InvalidNode exception is thrown if an arc (x,y) is added and x
105  * or y does not belong to the graph nodes */
106  void modifyGraphAlone(const ArcAddition& change);
107 
108  /// notify the constraint of a modification of the graph
109  /** @warning If a nonexisting arc is removed, nothing is done. In
110  * particular, no exception is raised. */
111  void modifyGraphAlone(const ArcDeletion& change);
112 
113  /// notify the constraint of a modification of the graph
114  /** @warning If an already existing arc is added, or if a nonexisting arc
115  * is removed, nothing is done. In particular, no exception is raised.
116  * @throws InvalidNode exception is thrown if at least one extremity of
117  * the arc does not belong to the graph nodes */
118  void modifyGraphAlone(const ArcReversal& 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  void modifyGraphAlone(const GraphChange& change);
126 
127  /// indicates whether a change will always violate the constraint
128  /** Some learning algorithms need examine several times whether a given
129  * graph change can be applied. For instance, the first time arc (X,Y)
130  * addition is considered, the learning algorithm may discard this change
131  * because it violates the structural constraint (e.g., if the latter
132  * enforces a DAG structure, this arc addition might induce a directed
133  * cycle), but, later on, other arc removal may induce that the arc
134  * addition
135  * is now possible. Such change is thus not always invalid. Conversely,
136  * there are changes that can be discarded once and for all. For instance,
137  * in a 2TBN structure, it is always impossible to add a backward-time
138  * arc.
139  * Such graph changes are always invalid and are therefore tagged as such
140  * by the isAlwaysInvalid method. */
141  bool isAlwaysInvalidAlone(const GraphChange& change) const;
142 
143  /// checks whether the constraints enable to add arc (x,y)
144  /** an arc can be added if and only if its extremal nodes belong to the
145  * graph and the arc does not already exist. */
146  bool checkArcAdditionAlone(NodeId x, NodeId y) const;
147 
148  /// checks whether the constraints enable to remove arc (x,y)
149  /** an arc can be removed if and only if the arc exists. */
150  bool checkArcDeletionAlone(NodeId x, NodeId y) const;
151 
152  /// checks whether the constraints enable to reverse arc (x,y)
153  /** an arc can be reversed if and only if it exists and arc (y,x)
154  * does not. */
155  bool checkArcReversalAlone(NodeId x, NodeId y) const;
156 
157  /// checks whether the constraints enable to perform a graph change
158  /** An arc can be added if and only if its extremal nodes belong to the
159  * graph and the arc does not already exist.
160  * An arc can be removed if and only if the arc exists.
161  * An arc (x,y) can be reversed if and only if it exists and arc (y,x)
162  * does not. */
163  bool checkModificationAlone(const GraphChange& change) const;
164 
165  /// checks whether the constraints enable to add an arc
166  /** an arc can be added if and only if its extremal nodes belong to the
167  * graph and the arc does not already exist. */
168  bool checkModificationAlone(const ArcAddition& change) const;
169 
170  /// checks whether the constraints enable to remove an arc
171  /** an arc can be removed if and only if the arc exists. */
172  bool checkModificationAlone(const ArcDeletion& change) const;
173 
174  /// checks whether the constraints enable to reverse an arc
175  /** an arc (x,y) can be reversed if and only if it exists and arc (y,x)
176  * does not. */
177  bool checkModificationAlone(const ArcReversal& change) const;
178 
179  /// @}
180 
181 #ifndef DOXYGEN_SHOULD_SKIP_THIS
182 // include the set of methods that enable the structural constraint to
183 // be standalone, i.e., that it needs not be included into a
184 // StructuralConstraintSetStatic to be used by learning algorithms
185 # define GUM_CONSTRAINT_CLASS_NAME StructuralConstraintDiGraph
186 # include <agrum/BN/learning/constraints/structuralConstraintPatternHeader.h>
187 # undef GUM_CONSTRAINT_CLASS_NAME
188 #endif // DOXYGEN_SHOULD_SKIP_THIS
189 
190  protected:
191  /// the DiGraph on which we perform checks
193  };
194 
195  } /* namespace learning */
196 
197 } /* namespace gum */
198 
199 /// include the inlined functions if necessary
200 #ifndef GUM_NO_INLINE
201 # include <agrum/BN/learning/constraints/structuralConstraintDiGraph_inl.h>
202 #endif /* GUM_NO_INLINE */
203 
204 #endif /* GUM_LEARNING_STRUCTURAL_CONSTRAINT_DIGRAPH_H */
void setGraph(Size nb_nodes)
sets a new empty graph from which we will perform checkings
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
StructuralConstraintDiGraph(StructuralConstraintDiGraph &&from)
move constructor
StructuralConstraintDiGraph(const StructuralConstraintDiGraph &from)
copy constructor
void setGraphAlone(const DiGraph &graph)
sets a new graph from which we will perform checkings
void modifyGraphAlone(const GraphChange &change)
notify the constraint of a modification of the graph
StructuralConstraintDiGraph & operator=(const StructuralConstraintDiGraph &from)
copy operator
bool checkArcReversalAlone(NodeId x, NodeId y) const
checks whether the constraints enable to reverse arc (x,y)
bool checkArcAdditionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to add arc (x,y)
DiGraph _DiGraph_graph_
the DiGraph on which we perform checks
StructuralConstraintDiGraph & operator=(StructuralConstraintDiGraph &&from)
move operator
bool isAlwaysInvalidAlone(const GraphChange &change) const
indicates whether a change will always violate the constraint
bool checkModificationAlone(const ArcReversal &change) const
checks whether the constraints enable to reverse an arc
bool checkArcDeletionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to remove arc (x,y)
StructuralConstraintDiGraph(const DiGraph &graph)
constructor starting with a given graph
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)
StructuralConstraintDiGraph(Size nb_nodes)
constructor starting with an empty graph with a given number of nodes
The base class for structural constraints used by learning algorithms that learn a directed graph str...