aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
structuralConstraintForbiddenArcs.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 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_FORBIDDEN_ARCS_H
29 #define GUM_LEARNING_STRUCTURAL_CONSTRAINT_FORBIDDEN_ARCS_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 StructuralConstraintForbiddenArcs
41  * @brief the structural constraint for forbidding the creation of some arcs
42  * during structure learning
43  * @ingroup learning_group
44  */
46  public virtual StructuralConstraintEmpty {
47  public:
48  // ##########################################################################
49  /// @name Constructors / Destructors
50  // ##########################################################################
51  /// @{
52 
53  /// default constructor
55 
56  /// constructor starting with a given graph
57  StructuralConstraintForbiddenArcs(const DiGraph& graph);
58 
59  /// copy constructor
62 
63  /// move constructor
65 
66  /// destructor
68 
69  /// @}
70 
71  // ##########################################################################
72  /// @name Operators
73  // ##########################################################################
74  /// @{
75 
76  /// copy operator
79 
80  /// move operator
83 
84  /// @}
85 
86  // ##########################################################################
87  /// @name Specific Accessors / Modifiers
88  // ##########################################################################
89  /// @{
90 
91  /// assign a set of forbidden arcs
92  void setArcs(const ArcSet& set);
93 
94  /// assign a new forbidden arc
95  void addArc(const Arc& arc);
96 
97  /// remove a forbidden arc
98  void eraseArc(const Arc& arc);
99 
100  /// returns the set of mandatory arcs
101  const ArcSet& arcs() const;
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  void modifyGraphAlone(const ArcAddition& change);
112 
113  /// notify the constraint of a modification of the graph
114  /** @warning If a nonexisting arc is removed, nothing is done. In
115  * particular, no exception is raised. */
116  void modifyGraphAlone(const ArcDeletion& change);
117 
118  /// notify the constraint of a modification of the graph
119  /** @warning If an already existing arc is added, or if a nonexisting arc
120  * is removed, nothing is done. In particular, no exception is raised.
121  * @throws InvalidNode exception is thrown if at least one extremity of
122  * the arc does not belong to the graph nodes */
123  void modifyGraphAlone(const ArcReversal& change);
124 
125  /// notify the constraint of a modification of the graph
126  /** @warning If an already existing arc is added, or if a nonexisting arc
127  * is removed, nothing is done. In particular, no exception is raised.
128  * @throws InvalidNode exception is thrown if an arc (x,y) is added and x
129  * or y does not belong to the graph nodes */
130  void modifyGraphAlone(const GraphChange& change);
131 
132  /// indicates whether a change will always violate the constraint
133  /** Some learning algorithms need examine several times whether a given
134  * graph change can be applied. For instance, the first time arc (X,Y)
135  * addition is considered, the learning algorithm may discard this change
136  * because it violates the structural constraint (e.g., if the latter
137  * enforces a DAG structure, this arc addition might induce a directed
138  * cycle), but, later on, other arc removal may induce that the arc
139  * addition
140  * is now possible. Such change is thus not always invalid. Conversely,
141  * there are changes that can be discarded once and for all. For instance,
142  * in a 2TBN structure, it is always impossible to add a backward-time
143  * arc.
144  * Such graph changes are always invalid and are therefore tagged as such
145  * by the isAlwaysInvalid method. */
146  bool isAlwaysInvalidAlone(const GraphChange& change) const;
147 
148  /// checks whether the constraints enable to add arc (x,y)
149  /** an arc can be added if and only if its extremal nodes belong to the
150  * graph and the arc does not already exist. */
151  bool checkArcAdditionAlone(NodeId x, NodeId y) const;
152 
153  /// checks whether the constraints enable to remove arc (x,y)
154  /** an arc can be removed if and only if the arc exists. */
155  bool checkArcDeletionAlone(NodeId x, NodeId y) const;
156 
157  /// checks whether the constraints enable to reverse arc (x,y)
158  /** an arc can be reversed if and only if it exists and arc (y,x)
159  * does not. */
160  bool checkArcReversalAlone(NodeId x, NodeId y) const;
161 
162  /// checks whether the constraints enable to perform a graph change
163  /** An arc can be added if and only if its extremal nodes belong to the
164  * graph and the arc does not already exist.
165  * An arc can be removed if and only if the arc exists.
166  * An arc (x,y) can be reversed if and only if it exists and arc (y,x)
167  * does not. */
168  bool checkModificationAlone(const GraphChange& change) 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. */
173  bool checkModificationAlone(const ArcAddition& change) const;
174 
175  /// checks whether the constraints enable to remove an arc
176  /** an arc can be removed if and only if the arc exists. */
177  bool checkModificationAlone(const ArcDeletion& change) const;
178 
179  /// checks whether the constraints enable to reverse an arc
180  /** an arc (x,y) can be reversed if and only if it exists and arc (y,x)
181  * does not. */
182  bool checkModificationAlone(const ArcReversal& change) const;
183 
184  /// @}
185 
186 #ifndef DOXYGEN_SHOULD_SKIP_THIS
187 // include the set of methods that enable the structural constraint to
188 // be standalone, i.e., that it needs not be included into a
189 // StructuralConstraintSetStatic to be used by learning algorithms
190 # define GUM_CONSTRAINT_CLASS_NAME StructuralConstraintForbiddenArcs
191 # include <agrum/BN/learning/constraints/structuralConstraintPatternHeader.h>
192 # undef GUM_CONSTRAINT_CLASS_NAME
193 #endif // DOXYGEN_SHOULD_SKIP_THIS
194 
195  protected:
196  /// the ForbiddenArcs on which we perform checks
198  };
199 
200  } /* namespace learning */
201 
202 } /* namespace gum */
203 
204 /// include the inlined functions if necessary
205 #ifndef GUM_NO_INLINE
206 # include <agrum/BN/learning/constraints/structuralConstraintForbiddenArcs_inl.h>
207 #endif /* GUM_NO_INLINE */
208 
209 #endif /* GUM_LEARNING_STRUCTURAL_CONSTRAINT_FORBIDDEN_ARCS_H */
StructuralConstraintForbiddenArcs(const DiGraph &graph)
constructor starting with a given graph
the structural constraint for forbidding the creation of some arcs during structure learning ...
void addArc(const Arc &arc)
assign a new forbidden arc
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
StructuralConstraintForbiddenArcs(StructuralConstraintForbiddenArcs &&from)
move constructor
StructuralConstraintForbiddenArcs & operator=(const StructuralConstraintForbiddenArcs &from)
copy operator
void setArcs(const ArcSet &set)
assign a set of forbidden arcs
bool checkModificationAlone(const ArcReversal &change) const
checks whether the constraints enable to reverse an arc
void setGraphAlone(const DiGraph &graph)
sets a new graph from which we will perform checkings
void eraseArc(const Arc &arc)
remove a forbidden arc
const ArcSet & arcs() const
returns the set of mandatory arcs
bool isAlwaysInvalidAlone(const GraphChange &change) const
indicates whether a change will always violate the constraint
StructuralConstraintForbiddenArcs & operator=(StructuralConstraintForbiddenArcs &&from)
move operator
bool checkArcAdditionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to add arc (x,y)
StructuralConstraintForbiddenArcs(const StructuralConstraintForbiddenArcs &from)
copy constructor
bool checkArcReversalAlone(NodeId x, NodeId y) const
checks whether the constraints enable to reverse arc (x,y)
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)
void modifyGraphAlone(const GraphChange &change)
notify the constraint of a modification of the graph
ArcSet ForbiddenArcs__forbidden_arcs_
the ForbiddenArcs on which we perform checks
bool checkArcDeletionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to remove arc (x,y)