aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
structuralConstraintPossibleEdges.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 structural constraint for forbidding the creation of some arcs
24  * during structure learning
25  *
26  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 #ifndef GUM_LEARNING_STRUCTURAL_CONSTRAINT_POSSIBLE_EDGES_H
29 #define GUM_LEARNING_STRUCTURAL_CONSTRAINT_POSSIBLE_EDGES_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 StructuralConstraintPossibleEdges
41  * @brief the structural constraint for forbidding the creation of some arcs
42  * except those defined in the class during structure learning
43  * @ingroup learning_group
44  */
46  public:
47  // ##########################################################################
48  /// @name Constructors / Destructors
49  // ##########################################################################
50  /// @{
51 
52  /// default constructor
54 
55  /// constructor starting with a given graph
56  StructuralConstraintPossibleEdges(const DiGraph& graph);
57 
58  /// copy constructor
60 
61  /// move constructor
63 
64  /// destructor
66 
67  /// @}
68 
69  // ##########################################################################
70  /// @name Operators
71  // ##########################################################################
72  /// @{
73 
74  /// copy operator
76 
77  /// move operator
79 
80  /// @}
81 
82  // ##########################################################################
83  /// @name Specific Accessors / Modifiers
84  // ##########################################################################
85  /// @{
86 
87  /// assign a set of forbidden arcs
88  void setEdges(const EdgeSet& set);
89 
90  /// assign a new forbidden arc
91  void addEdge(const Edge& edge);
92 
93  /// remove a forbidden arc
94  void eraseEdge(const Edge& edge);
95 
96  /// returns the set of mandatory arcs
97  const EdgeSet& edges() const;
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 StructuralConstraintPossibleEdges
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 PossibleEdges 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/structuralConstraintPossibleEdges_inl.h>
203 #endif /* GUM_NO_INLINE */
204 
205 #endif /* GUM_LEARNING_STRUCTURAL_CONSTRAINT_POSSIBLE_EDGES_H */
bool checkArcDeletionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to remove arc (x,y)
StructuralConstraintPossibleEdges(const DiGraph &graph)
constructor starting with a given graph
bool checkArcReversalAlone(NodeId x, NodeId y) const
checks whether the constraints enable to reverse arc (x,y)
void addEdge(const Edge &edge)
assign a new forbidden arc
void setEdges(const EdgeSet &set)
assign a set of forbidden arcs
void setGraphAlone(const DiGraph &graph)
sets a new graph from which we will perform checkings
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
the structural constraint for forbidding the creation of some arcs except those defined in the class ...
StructuralConstraintPossibleEdges & operator=(StructuralConstraintPossibleEdges &&from)
move operator
StructuralConstraintPossibleEdges(StructuralConstraintPossibleEdges &&from)
move constructor
StructuralConstraintPossibleEdges & operator=(const StructuralConstraintPossibleEdges &from)
copy operator
const EdgeSet & edges() const
returns the set of mandatory arcs
void modifyGraphAlone(const GraphChange &change)
notify the constraint of a modification of the graph
EdgeSet _PossibleEdges_possible_edges_
the PossibleEdges on which we perform checks
bool checkModificationAlone(const ArcReversal &change) const
checks whether the constraints enable to reverse an arc
bool checkArcAdditionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to add arc (x,y)
void eraseEdge(const Edge &edge)
remove a forbidden arc
StructuralConstraintPossibleEdges(const StructuralConstraintPossibleEdges &from)
copy constructor
bool isAlwaysInvalidAlone(const GraphChange &change) const
indicates whether a change will always violate the constraint
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)