aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
structuralConstraintSliceOrder.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 imposing a partial order over nodes
24  *
25  * In DBNs, it is forbidden to add arcs from nodes at time slice t to nodes at
26  * time slice s, where s < t. This class allows for taking this kind of
27  *constraint
28  * into account by imposing a partial order over the nodes: arcs (X,Y) can then
29  * only be added if X <= Y in the partial order.
30  * @warning: there may exist free variables, that is variables whose order
31  * w.r.t. the other variables is unspecified. In this case, arcs adjacent
32  * to them can be constructed. The partial order is specified by assigning
33  * numbers to nodes (through a NodeProperty). Nodes without number (i.e., that
34  * do not belong to the property) are free.
35  *
36  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
37  */
38 #ifndef GUM_LEARNING_STRUCTURAL_CONSTRAINT_SLICE_ORDER_H
39 #define GUM_LEARNING_STRUCTURAL_CONSTRAINT_SLICE_ORDER_H
40 
41 #include <agrum/agrum.h>
42 #include <agrum/BN/learning/constraints/structuralConstraintDiGraph.h>
43 #include <agrum/BN/learning/constraints/structuralConstraintSetStatic.h>
44 
45 namespace gum {
46 
47  namespace learning {
48 
49  /** @class StructuralConstraintSliceOrder
50  * @brief the structural constraint imposing a partial order over nodes
51  *
52  * In DBNs, it is forbidden to add arcs from nodes at time slice t to nodes
53  *at
54  * time slice s, where s < t. This class allows for taking this kind of
55  * constraint into account by imposing a partial order over the nodes:
56  * arcs (X,Y) can then only be added if X <= Y in the partial order.
57  * @warning: there may exist free variables, that is variables whose order
58  * w.r.t. the other variables is unspecified. In this case, arcs adjacent
59  * to them can be constructed. The partial order is specified by assigning
60  * numbers to nodes (through a NodeProperty). Nodes without number (i.e.,
61  *that
62  * do not belong to the property) are free.
63  *
64  * @ingroup learning_group
65  */
67  protected virtual StructuralConstraintSetStatic<
69  public:
70  // ##########################################################################
71  /// @name Constructors / Destructors
72  // ##########################################################################
73  /// @{
74 
75  /// default constructor
77 
78  /// constructor starting with an empty graph with a given number of nodes
79  /** param order the partial order */
80  StructuralConstraintSliceOrder(const NodeProperty< NodeId >& order);
81 
82  /// constructor starting with a given graph
83  StructuralConstraintSliceOrder(const DiGraph& graph,
84  const NodeProperty< NodeId >& order);
85 
86  /// copy constructor
88 
89  /// move constructor
91 
92  /// destructor
94 
95  /// @}
96 
97  // ##########################################################################
98  /// @name Operators
99  // ##########################################################################
100  /// @{
101 
102  /// copy operator
105 
106  /// move operator
109 
110  /// @}
111 
112  // ##########################################################################
113  /// @name Specific Accessors / Modifiers
114  // ##########################################################################
115  /// @{
116 
117  /// sets the time slices of all the nodes in the property
118  void setSliceOrder(const NodeProperty< NodeId >& slice);
119 
120  /// returns the current slice order
121  const NodeProperty< NodeId >& sliceOrder() const;
122 
123  /// adds a new node in the slice order
124  void addNode(NodeId node, NodeId slice);
125 
126  /** @brief assign a given slice to all the nodes specified in the
127  * partial order */
128  void setDefaultSlice(NodeId slice);
129 
130  /// sets a new graph from which we will perform checkings
131  void setGraphAlone(const DiGraph& graph);
132 
133  /// notify the constraint of a modification of the graph
134  /** @warning If an already existing arc is added, nothing is done. In
135  * particular, no exception is raised.
136  * @throws InvalidNode exception is thrown if an arc (x,y) is added and x
137  * or y does not belong to the graph nodes
138  * @throws InvalidArc exception is thrown if any time-backward arc
139  * is created by the arc addition. */
140  void modifyGraphAlone(const ArcAddition& change);
141 
142  /// notify the constraint of a modification of the graph
143  /** @warning If a nonexisting arc is removed, nothing is done. In
144  * particular, no exception is raised. */
145  void modifyGraphAlone(const ArcDeletion& change);
146 
147  /// notify the constraint of a modification of the graph
148  /** @warning If an already existing arc is added, or if a nonexisting arc
149  * is removed, nothing is done. In particular, no exception is raised.
150  * @throws InvalidNode exception is thrown if an arc (x,y) is added and x
151  * or y does not belong to the graph nodes
152  * @throws InvalidArc exception is thrown if any time-backward arc
153  * is created by the arc reversal. */
154  void modifyGraphAlone(const ArcReversal& change);
155 
156  /// notify the constraint of a modification of the graph
157  /** @warning If an already existing arc is added, or if a nonexisting arc
158  * is removed, nothing is done. In particular, no exception is raised.
159  * @throws InvalidNode exception is thrown if an arc (x,y) is added or
160  * reversed and x or y does not belong to the graph nodes
161  * @throws InvalidArc exception is thrown if any time-backward arc
162  * is created by an arc addition or reversal. */
163  void modifyGraphAlone(const GraphChange& change);
164 
165  /// indicates whether a change will always violate the constraint
166  /** Some learning algorithms need examine several times whether a given
167  * graph change can be applied. For instance, the first time arc (X,Y)
168  * addition is considered, the learning algorithm may discard this change
169  * because it violates the structural constraint (e.g., if the latter
170  * enforces a DAG structure, this arc addition might induce a directed
171  * cycle), but, later on, other arc removal may induce that the arc
172  * addition
173  * is now possible. Such change is thus not always invalid. Conversely,
174  * there are changes that can be discarded once and for all. For instance,
175  * in a 2TBN structure, it is always impossible to add a backward-time
176  * arc.
177  * Such graph changes are always invalid and are therefore tagged as such
178  * by the isAlwaysInvalid method. */
179  bool isAlwaysInvalidAlone(const GraphChange& change) const;
180 
181  /// checks whether the constraints enable to add arc (x,y)
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 is not a
184  * backward-time arc. */
185  bool checkArcAdditionAlone(NodeId x, NodeId y) const;
186 
187  /// checks whether the constraints enable to remove arc (x,y)
188  /** an arc can be removed if and only if the arc exists. */
189  bool checkArcDeletionAlone(NodeId x, NodeId y) const;
190 
191  /// checks whether the constraints enable to reverse arc (x,y)
192  /** an arc can be reversed if and only if it exists and arc (y,x)
193  * does not and is not a backward-time arc. */
194  bool checkArcReversalAlone(NodeId x, NodeId y) const;
195 
196  /// checks whether the constraints enable to add an arc
197  /** an arc can be added if and only if its extremal nodes belong to the
198  * graph and the arc does not already exist and is not a
199  * backward-time arc. */
200  bool checkModificationAlone(const ArcAddition& change) const;
201 
202  /// checks whether the constraints enable to remove an arc
203  /** an arc can be removed if and only if the arc exists. */
204  bool checkModificationAlone(const ArcDeletion& change) const;
205 
206  /// checks whether the constraints enable to reverse an arc
207  /** an arc can be reversed if and only if it exists and arc (y,x)
208  * does not and is not a backward-time arc. */
209  bool checkModificationAlone(const ArcReversal& change) const;
210 
211  /// checks whether the constraints enable to perform a graph change
212  /** An arc can be added if and only if its extremal nodes belong to the
213  * graph and the arc does not already exist and is not a
214  * backward-time arc.
215  * An arc can be removed if and only if the arc exists.
216  * An arc (x,y) can be reversed if and only if it exists and arc (y,x)
217  * does not and is not a backward-time arc. */
218  bool checkModificationAlone(const GraphChange& change) const;
219 
220  /// @}
221 
222 #ifndef DOXYGEN_SHOULD_SKIP_THIS
223 // include the set of methods that enable the structural constraint to
224 // be standalone, i.e., that it needs not be included into a
225 // StructuralConstraintSetStatic to be used by learning algorithms
226 # define GUM_CONSTRAINT_CLASS_NAME StructuralConstraintSliceOrder
227 # include <agrum/BN/learning/constraints/structuralConstraintPatternHeader.h>
228 # undef GUM_CONSTRAINT_CLASS_NAME
229 #endif // DOXYGEN_SHOULD_SKIP_THIS
230 
231  protected:
232  /// slices to which belong the nodes
234  };
235 
236  } /* namespace learning */
237 
238 } /* namespace gum */
239 
240 /// include the inlined functions if necessary
241 #ifndef GUM_NO_INLINE
242 # include <agrum/BN/learning/constraints/structuralConstraintSliceOrder_inl.h>
243 #endif /* GUM_NO_INLINE */
244 
245 #endif /* GUM_LEARNING_STRUCTURAL_CONSTRAINT_SLICE_ORDER_H */
bool checkArcAdditionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to add arc (x,y)
bool checkArcReversalAlone(NodeId x, NodeId y) const
checks whether the constraints enable to reverse arc (x,y)
bool checkArcDeletionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to remove arc (x,y)
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 setDefaultSlice(NodeId slice)
assign a given slice to all the nodes specified in the partial order
StructuralConstraintSliceOrder(const NodeProperty< NodeId > &order)
constructor starting with an empty graph with a given number of nodes
StructuralConstraintSliceOrder(StructuralConstraintSliceOrder &&from)
move constructor
StructuralConstraintSliceOrder & operator=(const StructuralConstraintSliceOrder &from)
copy operator
void setSliceOrder(const NodeProperty< NodeId > &slice)
sets the time slices of all the nodes in the property
NodeProperty< NodeId > SliceOrder__order_
slices to which belong the nodes
const NodeProperty< NodeId > & sliceOrder() const
returns the current slice order
void modifyGraphAlone(const GraphChange &change)
notify the constraint of a modification of the graph
bool isAlwaysInvalidAlone(const GraphChange &change) const
indicates whether a change will always violate the constraint
StructuralConstraintSliceOrder(const DiGraph &graph, const NodeProperty< NodeId > &order)
constructor starting with a given graph
StructuralConstraintSliceOrder(const StructuralConstraintSliceOrder &from)
copy constructor
StructuralConstraintSliceOrder & operator=(StructuralConstraintSliceOrder &&from)
move operator
bool checkModificationAlone(const GraphChange &change) const
checks whether the constraints enable to perform a graph change
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)
void addNode(NodeId node, NodeId slice)
adds a new node in the slice order
the structural constraint imposing a partial order over nodes