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