aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
structuralConstraintUndiGraph.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 an undirected graph structure
25  *
26  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 #ifndef GUM_LEARNING_STRUCTURAL_CONSTRAINT_UNDIGRAPH_H
29 #define GUM_LEARNING_STRUCTURAL_CONSTRAINT_UNDIGRAPH_H
30 
31 #include <agrum/agrum.h>
32 #include <agrum/tools/graphs/undiGraph.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 StructuralConstraintUndiGraph
41  * @brief The base class for structural constraints used by learning
42  * algorithms that learn an undirected graph structure
43  *
44  * This base should always be a virtual parent of the structural constraints
45  * classes. This will allow to combine different constraints into a single
46  * class
47  * @ingroup learning_group
48  */
50  public:
51  // ##########################################################################
52  /// @name Constructors / Destructors
53  // ##########################################################################
54  /// @{
55 
56  /// default constructor
58 
59  /// constructor starting with an empty graph with a given number of nodes
60  StructuralConstraintUndiGraph(Size nb_nodes);
61 
62  /// constructor starting with a given graph
63  StructuralConstraintUndiGraph(const UndiGraph& graph);
64 
65  /// copy constructor
67 
68  /// move constructor
70 
71  /// destructor
73 
74  /// @}
75 
76  // ##########################################################################
77  /// @name Operators
78  // ##########################################################################
79  /// @{
80 
81  /// copy operator
83 
84  /// move operator
86 
87  /// @}
88 
89  // ##########################################################################
90  /// @name Specific Accessors / Modifiers
91  // ##########################################################################
92  /// @{
93 
94  /// sets a new empty graph from which we will perform checkings
95  void setGraph(Size nb_nodes);
96 
97  /// sets a new graph from which we will perform checkings
98  void setGraphAlone(const UndiGraph& graph);
99 
100  /// notify the constraint of a modification of the graph
101  /** @warning If an already existing edge is added nothing is done. In
102  * particular, no exception is raised.
103  * @throws InvalidNode exception is thrown if an edge (x,y) is added and x
104  * or y does not belong to the graph nodes */
105  void modifyGraphAlone(const EdgeAddition& change);
106 
107  /// notify the constraint of a modification of the graph
108  /** @warning If a nonexisting edge is removed, nothing is done. In
109  * particular, no exception is raised. */
110  void modifyGraphAlone(const EdgeDeletion& change);
111 
112  /// notify the constraint of a modification of the graph
113  /** @warning If an already existing edge is added, or if a nonexisting
114  * edge
115  * is removed, nothing is done. In particular, no exception is raised.
116  * @throws InvalidNode exception is thrown if an edge (x,y) is added and x
117  * or y does not belong to the graph nodes */
118  void modifyGraphAlone(const GraphChange& change);
119 
120  /// indicates whether a change will always violate the constraint
121  /** Some learning algorithms need examine several times whether a given
122  * graph change can be applied. For instance, the first time edge (X,Y)
123  * addition is considered, the learning algorithm may discard this change
124  * because it violates the structural constraint, but, later on, other
125  * edge
126  * removals may induce that the edge addition is now possible. Such change
127  * is thus not always invalid. Conversely, there are changes that can be
128  * discarded once and for all. For instance,
129  * in a 2TBN structure, it is always impossible to add a backward-time
130  * arc.
131  * Such graph changes are always invalid and are therefore tagged as such
132  * by the isAlwaysInvalid method. */
133  bool isAlwaysInvalidAlone(const GraphChange& change) const;
134 
135  /// checks whether the constraints enable to add edge (x,y)
136  /** an arc can be added if and only if its extremal nodes belong to the
137  * graph and the edge does not already exist. */
138  bool checkEdgeAdditionAlone(NodeId x, NodeId y) const;
139 
140  /// checks whether the constraints enable to remove edge (x,y)
141  /** an edge can be removed if and only if the edge exists. */
142  bool checkEdgeDeletionAlone(NodeId x, NodeId y) const;
143 
144  /// checks whether the constraints enable to perform a graph change
145  /** An edge can be added if and only if its extremal nodes belong to the
146  * graph and the edge does not already exist.
147  * An edge can be removed if and only if the edge exists. */
148  bool checkModificationAlone(const GraphChange& change) const;
149 
150  /// checks whether the constraints enable to add an edge
151  /** an arc can be added if and only if its extremal nodes belong to the
152  * graph and the edge does not already exist. */
153  bool checkModificationAlone(const EdgeAddition& change) const;
154 
155  /// checks whether the constraints enable to remove an edge
156  /** an edge can be removed if and only if the edge exists. */
157  bool checkModificationAlone(const EdgeDeletion& change) const;
158 
159  /// @}
160 
161 #ifndef DOXYGEN_SHOULD_SKIP_THIS
162 // include the set of methods that enable the structural constraint to
163 // be standalone, i.e., that it needs not be included into a
164 // StructuralConstraintSetStatic to be used by learning algorithms
165 # define GUM_CONSTRAINT_CLASS_NAME StructuralConstraintUndiGraph
166 # include <agrum/BN/learning/constraints/structuralConstraintPattern4UndiGraphHeader.h>
167 # undef GUM_CONSTRAINT_CLASS_NAME
168 #endif // DOXYGEN_SHOULD_SKIP_THIS
169 
170  protected:
171  /// the UndiGraph on which we perform checks
173  };
174 
175  } /* namespace learning */
176 
177 } /* namespace gum */
178 
179 /// include the inlined functions if necessary
180 #ifndef GUM_NO_INLINE
181 # include <agrum/BN/learning/constraints/structuralConstraintUndiGraph_inl.h>
182 #endif /* GUM_NO_INLINE */
183 
184 #endif /* GUM_LEARNING_STRUCTURAL_CONSTRAINT_UNDIGRAPH_H */
void setGraphAlone(const UndiGraph &graph)
sets a new graph from which we will perform checkings
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
bool isAlwaysInvalidAlone(const GraphChange &change) const
indicates whether a change will always violate the constraint
StructuralConstraintUndiGraph & operator=(const StructuralConstraintUndiGraph &from)
copy operator
UndiGraph _UndiGraph_graph_
the UndiGraph on which we perform checks
bool checkModificationAlone(const EdgeDeletion &change) const
checks whether the constraints enable to remove an edge
void modifyGraphAlone(const GraphChange &change)
notify the constraint of a modification of the graph
The base class for structural constraints used by learning algorithms that learn an undirected graph ...
StructuralConstraintUndiGraph(const UndiGraph &graph)
constructor starting with a given graph
StructuralConstraintUndiGraph(StructuralConstraintUndiGraph &&from)
move constructor
void setGraph(Size nb_nodes)
sets a new empty graph from which we will perform checkings
bool checkEdgeDeletionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to remove edge (x,y)
StructuralConstraintUndiGraph & operator=(StructuralConstraintUndiGraph &&from)
move operator
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)
StructuralConstraintUndiGraph(Size nb_nodes)
constructor starting with an empty graph with a given number of nodes
StructuralConstraintUndiGraph(const StructuralConstraintUndiGraph &from)
copy constructor
bool checkEdgeAdditionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to add edge (x,y)