aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
structuralConstraintSliceOrder.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 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  */
68  public:
69  // ##########################################################################
70  /// @name Constructors / Destructors
71  // ##########################################################################
72  /// @{
73 
74  /// default constructor
76 
77  /// constructor starting with an empty graph with a given number of nodes
78  /** param order the partial order */
79  StructuralConstraintSliceOrder(const NodeProperty< NodeId >& order);
80 
81  /// constructor starting with a given graph
82  StructuralConstraintSliceOrder(const DiGraph& graph, const NodeProperty< NodeId >& order);
83 
84  /// copy constructor
86 
87  /// move constructor
89 
90  /// destructor
92 
93  /// @}
94 
95  // ##########################################################################
96  /// @name Operators
97  // ##########################################################################
98  /// @{
99 
100  /// copy operator
102 
103  /// move operator
105 
106  /// @}
107 
108  // ##########################################################################
109  /// @name Specific Accessors / Modifiers
110  // ##########################################################################
111  /// @{
112 
113  /// sets the time slices of all the nodes in the property
114  void setSliceOrder(const NodeProperty< NodeId >& slice);
115 
116  /// returns the current slice order
117  const NodeProperty< NodeId >& sliceOrder() const;
118 
119  /// adds a new node in the slice order
120  void addNode(NodeId node, NodeId slice);
121 
122  /** @brief assign a given slice to all the nodes specified in the
123  * partial order */
124  void setDefaultSlice(NodeId slice);
125 
126  /// sets a new graph from which we will perform checkings
127  void setGraphAlone(const DiGraph& graph);
128 
129  /// notify the constraint of a modification of the graph
130  /** @warning If an already existing arc is added, nothing is done. In
131  * particular, no exception is raised.
132  * @throws InvalidNode exception is thrown if an arc (x,y) is added and x
133  * or y does not belong to the graph nodes
134  * @throws InvalidArc exception is thrown if any time-backward arc
135  * is created by the arc addition. */
136  void modifyGraphAlone(const ArcAddition& change);
137 
138  /// notify the constraint of a modification of the graph
139  /** @warning If a nonexisting arc is removed, nothing is done. In
140  * particular, no exception is raised. */
141  void modifyGraphAlone(const ArcDeletion& change);
142 
143  /// notify the constraint of a modification of the graph
144  /** @warning If an already existing arc is added, or if a nonexisting arc
145  * is removed, nothing is done. In particular, no exception is raised.
146  * @throws InvalidNode exception is thrown if an arc (x,y) is added and x
147  * or y does not belong to the graph nodes
148  * @throws InvalidArc exception is thrown if any time-backward arc
149  * is created by the arc reversal. */
150  void modifyGraphAlone(const ArcReversal& change);
151 
152  /// notify the constraint of a modification of the graph
153  /** @warning If an already existing arc is added, or if a nonexisting arc
154  * is removed, nothing is done. In particular, no exception is raised.
155  * @throws InvalidNode exception is thrown if an arc (x,y) is added or
156  * reversed and x or y does not belong to the graph nodes
157  * @throws InvalidArc exception is thrown if any time-backward arc
158  * is created by an arc addition or reversal. */
159  void modifyGraphAlone(const GraphChange& change);
160 
161  /// indicates whether a change will always violate the constraint
162  /** Some learning algorithms need examine several times whether a given
163  * graph change can be applied. For instance, the first time arc (X,Y)
164  * addition is considered, the learning algorithm may discard this change
165  * because it violates the structural constraint (e.g., if the latter
166  * enforces a DAG structure, this arc addition might induce a directed
167  * cycle), but, later on, other arc removal may induce that the arc
168  * addition
169  * is now possible. Such change is thus not always invalid. Conversely,
170  * there are changes that can be discarded once and for all. For instance,
171  * in a 2TBN structure, it is always impossible to add a backward-time
172  * arc.
173  * Such graph changes are always invalid and are therefore tagged as such
174  * by the isAlwaysInvalid method. */
175  bool isAlwaysInvalidAlone(const GraphChange& change) const;
176 
177  /// checks whether the constraints enable to add arc (x,y)
178  /** an arc can be added if and only if its extremal nodes belong to the
179  * graph and the arc does not already exist and is not a
180  * backward-time arc. */
181  bool checkArcAdditionAlone(NodeId x, NodeId y) const;
182 
183  /// checks whether the constraints enable to remove arc (x,y)
184  /** an arc can be removed if and only if the arc exists. */
185  bool checkArcDeletionAlone(NodeId x, NodeId y) const;
186 
187  /// checks whether the constraints enable to reverse arc (x,y)
188  /** an arc can be reversed if and only if it exists and arc (y,x)
189  * does not and is not a backward-time arc. */
190  bool checkArcReversalAlone(NodeId x, NodeId y) const;
191 
192  /// checks whether the constraints enable to add an arc
193  /** an arc can be added if and only if its extremal nodes belong to the
194  * graph and the arc does not already exist and is not a
195  * backward-time arc. */
196  bool checkModificationAlone(const ArcAddition& change) const;
197 
198  /// checks whether the constraints enable to remove an arc
199  /** an arc can be removed if and only if the arc exists. */
200  bool checkModificationAlone(const ArcDeletion& change) const;
201 
202  /// checks whether the constraints enable to reverse an arc
203  /** an arc can be reversed if and only if it exists and arc (y,x)
204  * does not and is not a backward-time arc. */
205  bool checkModificationAlone(const ArcReversal& change) const;
206 
207  /// checks whether the constraints enable to perform a graph change
208  /** An arc can be added if and only if its extremal nodes belong to the
209  * graph and the arc does not already exist and is not a
210  * backward-time arc.
211  * An arc can be removed if and only if the arc exists.
212  * An arc (x,y) can be reversed if and only if it exists and arc (y,x)
213  * does not and is not a backward-time arc. */
214  bool checkModificationAlone(const GraphChange& change) const;
215 
216  /// @}
217 
218 #ifndef DOXYGEN_SHOULD_SKIP_THIS
219 // include the set of methods that enable the structural constraint to
220 // be standalone, i.e., that it needs not be included into a
221 // StructuralConstraintSetStatic to be used by learning algorithms
222 # define GUM_CONSTRAINT_CLASS_NAME StructuralConstraintSliceOrder
223 # include <agrum/BN/learning/constraints/structuralConstraintPatternHeader.h>
224 # undef GUM_CONSTRAINT_CLASS_NAME
225 #endif // DOXYGEN_SHOULD_SKIP_THIS
226 
227  protected:
228  /// slices to which belong the nodes
230  };
231 
232  } /* namespace learning */
233 
234 } /* namespace gum */
235 
236 /// include the inlined functions if necessary
237 #ifndef GUM_NO_INLINE
238 # include <agrum/BN/learning/constraints/structuralConstraintSliceOrder_inl.h>
239 #endif /* GUM_NO_INLINE */
240 
241 #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:643
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
const NodeProperty< NodeId > & sliceOrder() const
returns the current slice order
void modifyGraphAlone(const GraphChange &change)
notify the constraint of a modification of the graph
NodeProperty< NodeId > _SliceOrder_order_
slices to which belong the nodes
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