aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
structuralConstraintDAG.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 imposed by DAGs
24  *
25  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26  */
27 #ifndef GUM_LEARNING_STRUCTURAL_CONSTRAINT_DAG_H
28 #define GUM_LEARNING_STRUCTURAL_CONSTRAINT_DAG_H
29 
30 #include <agrum/agrum.h>
31 #include <agrum/tools/graphs/algorithms/DAGCycleDetector.h>
32 #include <agrum/BN/learning/constraints/structuralConstraintDiGraph.h>
33 #include <agrum/BN/learning/constraints/structuralConstraintSetStatic.h>
34 
35 namespace gum {
36 
37  namespace learning {
38 
39  /** @class StructuralConstraintDAG
40  * @brief The base class for structural constraints imposed by DAGs
41  *
42  * This base should always be a virtual parents of the structural
43  *constraints
44  * classes. This will allow to combine different constraints into a single
45  * class
46  * @ingroup learning_group
47  */
49  public 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  StructuralConstraintDAG(Size nb_nodes);
62 
63  /// constructor starting with a given graph
64  StructuralConstraintDAG(const DAG& graph);
65 
66  /// copy constructor
68 
69  /// move constructor
71 
72  /// destructor
73  virtual ~StructuralConstraintDAG();
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 graph from which we will perform checkings
96  void setGraphAlone(const DiGraph& graph);
97 
98  /// sets a new empty graph from which we will perform checkings
99  void setGraphAlone(Size nb_nodes);
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  * @throws InvalidDirectedCycle exception is thrown if any (directed)
107  * cycle
108  * is created by the arc addition. */
109  void modifyGraphAlone(const ArcAddition& change);
110 
111  /// notify the constraint of a modification of the graph
112  /** @warning If a nonexisting arc is removed, nothing is done. In
113  * particular, no exception is raised. */
114  void modifyGraphAlone(const ArcDeletion& change);
115 
116  /// notify the constraint of a modification of the graph
117  /** @warning If an already existing arc is added, or if a nonexisting arc
118  * is removed, nothing is done. In particular, no exception is raised.
119  * @throws InvalidNode exception is thrown if at least one extremity of
120  * the arc does not belong to the graph nodes
121  * @throws InvalidDirectedCycle exception is thrown if any (directed)
122  * cycle
123  * is created by the arc reversal. */
124  void modifyGraphAlone(const ArcReversal& change);
125 
126  /// notify the constraint of a modification of the graph
127  /** @warning If an already existing arc is added, or if a nonexisting arc
128  * is removed, nothing is done. In particular, no exception is raised.
129  * @throws InvalidNode exception is thrown if an arc (x,y) is added and x
130  * or y does not belong to the graph nodes
131  * @throws InvalidDirectedCycle exception is thrown if any (directed)
132  * cycle
133  * is created 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 would not create a cycle
155  */
156  bool checkArcAdditionAlone(NodeId x, NodeId y) const;
157 
158  /// checks whether the constraints enable to remove arc (x,y)
159  bool checkArcDeletionAlone(NodeId x, NodeId y) const;
160 
161  /// checks whether the constraints enable to reverse arc (x,y)
162  /** An arc (x,y) can be reversed if and only if it exists and, after
163  * deleting it, the addition of arc (y,x) does not induce a
164  * directed cycle. */
165  bool checkArcReversalAlone(NodeId x, NodeId y) const;
166 
167  /// checks whether the constraints enable to add an arc
168  /** an arc can be added if and only if its extremal nodes belong to the
169  * graph and the arc does not already exist. */
170  bool checkModificationAlone(const ArcAddition& change) const;
171 
172  /// checks whether the constraints enable to remove an arc
173  /** an arc can be removed if and only if the arc exists. */
174  bool checkModificationAlone(const ArcDeletion& change) const;
175 
176  /// checks whether the constraints enable to reverse an arc
177  /** An arc can be reversed if, after deleting arc (x,y), the addition of
178  * arc (y,x) does not induce a directed cycle. */
179  bool checkModificationAlone(const ArcReversal& change) const;
180 
181  /// checks whether the constraints enable to perform a graph change
182  /** an arc can be added if and only if its extremal nodes belong to the
183  * graph and the arc does not already exist and would not create a cycle.
184  * An arc can be removed if and only if the arc exists.
185  * An arc can be reversed if, after deleting arc (x,y), the addition of
186  * arc (y,x) does not induce a directed cycle. */
187  bool checkModificationAlone(const GraphChange& change) const;
188 
189  /// sets a new graph from which we will perform checkings
190  void setGraph(const DAG& graph);
191 
192  /// sets a new empty graph from which we will perform checkings
193  void setGraph(Size nb_nodes);
194 
195  /// @}
196 
197 #ifndef DOXYGEN_SHOULD_SKIP_THIS
198 // include the set of methods that enable the structural constraint to
199 // be standalone, i.e., that it needs not be included into a
200 // StructuralConstraintSetStatic to be used by learning algorithms
201 # define GUM_CONSTRAINT_CLASS_NAME StructuralConstraintDAG
202 # include <agrum/BN/learning/constraints/structuralConstraintPatternHeader.h>
203 # undef GUM_CONSTRAINT_CLASS_NAME
204 #endif // DOXYGEN_SHOULD_SKIP_THIS
205 
206  protected:
207  /// the cycle detector used to check quickly graph modifications
209  };
210 
211  } /* namespace learning */
212 
213 } /* namespace gum */
214 
215 /// include the inlined functions if necessary
216 #ifndef GUM_NO_INLINE
217 # include <agrum/BN/learning/constraints/structuralConstraintDAG_inl.h>
218 #endif /* GUM_NO_INLINE */
219 
220 #endif /* GUM_LEARNING_STRUCTURAL_CONSTRAINT_DAG_H */
void setGraph(const DAG &graph)
sets a new graph from which we will perform checkings
StructuralConstraintDAG(const StructuralConstraintDAG &from)
copy constructor
StructuralConstraintDAG(const DAG &graph)
constructor starting with a given graph
StructuralConstraintDAG(StructuralConstraintDAG &&from)
move constructor
DAGCycleDetector DAG__cycle_detector_
the cycle detector used to check quickly graph modifications
StructuralConstraintDAG & operator=(StructuralConstraintDAG &&from)
move operator
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
void setGraphAlone(const DiGraph &graph)
sets a new graph from which we will perform checkings
void setGraphAlone(Size nb_nodes)
sets a new empty graph from which we will perform checkings
bool checkArcAdditionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to add arc (x,y)
StructuralConstraintDAG(Size nb_nodes)
constructor starting with an empty graph with a given number of nodes
bool isAlwaysInvalidAlone(const GraphChange &change) const
indicates whether a change will always violate the constraint
bool checkArcReversalAlone(NodeId x, NodeId y) const
checks whether the constraints enable to reverse arc (x,y)
bool checkModificationAlone(const GraphChange &change) const
checks whether the constraints enable to perform a graph change
bool checkArcDeletionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to remove arc (x,y)
void modifyGraphAlone(const GraphChange &change)
notify the constraint of a modification of the graph
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)
StructuralConstraintDAG & operator=(const StructuralConstraintDAG &from)
copy operator
void setGraph(Size nb_nodes)
sets a new empty graph from which we will perform checkings
The base class for structural constraints imposed by DAGs.