aGrUM  0.16.0
structuralConstraintSetStatic.h
Go to the documentation of this file.
1 
41 #ifndef GUM_LEARNING_STRUCTURAL_CONSTRAINT_SET_STATIC_H
42 #define GUM_LEARNING_STRUCTURAL_CONSTRAINT_SET_STATIC_H
43 
44 #include <type_traits>
45 
46 #include <agrum/agrum.h>
47 #include <agrum/graphs/diGraph.h>
49 
50 namespace gum {
51 
52  namespace learning {
53 
54 #ifndef DOXYGEN_SHOULD_SKIP_THIS
55 
56  // a class that indicates the we inherit from a
57  // StructuralConstraintSetStatic
58  struct __StructuralRoot {};
59 
60  // a temporary structure used to help computing the minimal set of
61  // constraints
62  template < typename FIRST_CONSTRAINT, typename... OTHER_CONSTRAINTS >
63  struct __ConstraintSet;
64 
65  // a structure to concatenate __ConstraintSets or simply constraints and
66  // produce as a result a new __ConstraintSet
67  template < typename SET1, typename SET2 >
68  struct __ConcatConstraintSet;
69 
70  // a helper function to create minimum structural constraint sets and the
71  // methods actually used on all these constraints. This is a helper for
72  // the class that the user should use, i.e., StructuralConstraintSetStatic
73  template < typename FIRST_CONSTRAINT, typename... OTHER_CONSTRAINTS >
74  class __StructuralConstraintSetStatic;
75 
76  // ============================================================================
77  // checks whether a given structural constraint belongs to a given set of
78  // structural constraints
79  template < typename CONSTRAINT, typename SET >
80  struct __IsInConstraintSet;
81 
82  template < typename CONSTRAINT, typename SET >
83  struct __IsInConstraintSet< CONSTRAINT, __ConstraintSet< SET > > {
84  constexpr static bool value = std::is_same< CONSTRAINT, SET >::value;
85  };
86 
87  template < typename CONSTRAINT, typename SET1, typename... SETS >
88  struct __IsInConstraintSet< CONSTRAINT, __ConstraintSet< SET1, SETS... > > {
89  constexpr static bool value =
90  std::is_same< CONSTRAINT, SET1 >::value
91  || __IsInConstraintSet< CONSTRAINT, __ConstraintSet< SETS... > >::value;
92  };
93 
94  // ============================================================================
95  // a temporary structure used to help computing the minimal set of
96  // constraints
97  // (removing all duplicates) belonging to a given set of structural
98  // constraints. For instance, if we have the following structural hierarchy:
99  // Z->Y->X, T->Y->X and we have the set of constraints S = <Z,T>, the set
100  // of
101  // all structural constraints reachable from S is S' = <Z,Y,X,T,Y,X>. The
102  // goal
103  // of the following class is to transform S' into S'' = <Z,T,Y,X>, i.e., the
104  // set S' without any duplicates.
105  template < typename FIRST_CONSTRAINT, typename... OTHER_CONSTRAINTS >
106  struct __ConstraintSet : public __ConstraintSet< OTHER_CONSTRAINTS... > {
107  using minset = typename std::conditional<
108  __IsInConstraintSet< FIRST_CONSTRAINT,
109  __ConstraintSet< OTHER_CONSTRAINTS... > >::value,
110  typename __ConstraintSet< OTHER_CONSTRAINTS... >::minset,
111  typename __ConcatConstraintSet<
112  FIRST_CONSTRAINT,
113  typename __ConstraintSet< OTHER_CONSTRAINTS... >::minset >::type >::
114  type;
115  using set =
116  __StructuralConstraintSetStatic< FIRST_CONSTRAINT, OTHER_CONSTRAINTS... >;
117  };
118 
119  template < typename CONSTRAINT >
120  struct __ConstraintSet< CONSTRAINT > {
121  using minset = __ConstraintSet< CONSTRAINT >;
122  using set = __StructuralConstraintSetStatic< CONSTRAINT >;
123  };
124 
125  // ============================================================================
126  // a structure to concatenate __ConstraintSets or simply constraints and
127  // produce as a result a new __ConstraintSet
128  template < typename SET1, typename SET2 >
129  struct __ConcatConstraintSet;
130 
131  template < typename CONSTRAINT1, typename CONSTRAINT2 >
132  struct __ConcatConstraintSet< CONSTRAINT1, __ConstraintSet< CONSTRAINT2 > > {
133  using type = __ConstraintSet< CONSTRAINT1, CONSTRAINT2 >;
134  };
135 
136  template < typename CONSTRAINT1, typename CONSTRAINT2 >
137  struct __ConcatConstraintSet< __ConstraintSet< CONSTRAINT1 >,
138  __ConstraintSet< CONSTRAINT2 > > {
139  using type = __ConstraintSet< CONSTRAINT1, CONSTRAINT2 >;
140  };
141 
142  template < typename CONSTRAINT1,
143  typename CONSTRAINT2,
144  typename... OTHER_CONSTRAINT2 >
145  struct __ConcatConstraintSet<
146  CONSTRAINT1,
147  __ConstraintSet< CONSTRAINT2, OTHER_CONSTRAINT2... > > {
148  using type =
149  __ConstraintSet< CONSTRAINT1, CONSTRAINT2, OTHER_CONSTRAINT2... >;
150  };
151 
152  template < typename CONSTRAINT1,
153  typename CONSTRAINT2,
154  typename... OTHER_CONSTRAINT1 >
155  struct __ConcatConstraintSet<
156  __ConstraintSet< CONSTRAINT1, OTHER_CONSTRAINT1... >,
157  __ConstraintSet< CONSTRAINT2 > > {
158  using type =
159  __ConstraintSet< CONSTRAINT1, OTHER_CONSTRAINT1..., CONSTRAINT2 >;
160  };
161 
162  template < typename CONSTRAINT1,
163  typename CONSTRAINT2,
164  typename... OTHER_CONSTR1,
165  typename... OTHER_CONSTR2 >
166  struct __ConcatConstraintSet<
167  __ConstraintSet< CONSTRAINT1, OTHER_CONSTR1... >,
168  __ConstraintSet< CONSTRAINT2, OTHER_CONSTR2... > > {
169  using type = __ConstraintSet< CONSTRAINT1,
170  OTHER_CONSTR1...,
171  CONSTRAINT2,
172  OTHER_CONSTR2... >;
173  };
174 
175  // ============================================================================
176  // a helper function to create minimum structural constraint sets and the
177  // methods actually used on all these constraints. This is a helper for
178  // the class that the user should use, i.e., StructuralConstraintSetStatic
179  template < typename CONSTRAINT1, typename... OTHER_CONSTRAINTS >
180  class __StructuralConstraintSetStatic
181  : public virtual CONSTRAINT1
182  , public virtual __StructuralConstraintSetStatic< OTHER_CONSTRAINTS... > {
183  public:
185  using first_constraint = CONSTRAINT1;
186 
188  using next_constraints =
189  __StructuralConstraintSetStatic< OTHER_CONSTRAINTS... >;
190 
191  // determines the set of all constraints in the set (included inherited
192  // ones)
193  using allConstraints = typename __ConcatConstraintSet<
194  typename std::conditional<
195  std::is_base_of< __StructuralRoot, CONSTRAINT1 >::value,
196  typename __ConcatConstraintSet<
197  CONSTRAINT1,
198  typename CONSTRAINT1::allConstraints >::type,
199  __ConstraintSet< CONSTRAINT1 > >::type,
200  typename next_constraints::allConstraints >::type;
201 
210  using minConstraints = typename allConstraints::minset::set;
211 
212  // ##########################################################################
214  // ##########################################################################
216 
218  __StructuralConstraintSetStatic();
219 
221  __StructuralConstraintSetStatic(
222  const __StructuralConstraintSetStatic< CONSTRAINT1,
223  OTHER_CONSTRAINTS... >&);
224 
226  ~__StructuralConstraintSetStatic();
227 
229 
230  // ##########################################################################
232  // ##########################################################################
234 
236  __StructuralConstraintSetStatic< CONSTRAINT1, OTHER_CONSTRAINTS... >&
237  operator=(const __StructuralConstraintSetStatic< CONSTRAINT1,
238  OTHER_CONSTRAINTS... >&);
239 
241 
242  // ##########################################################################
244  // ##########################################################################
246 
248  void setGraph(const DiGraph& graph);
249 
251  void modifyGraph(const ArcAddition& change);
252 
254  void modifyGraph(const ArcDeletion& change);
255 
257  void modifyGraph(const ArcReversal& change);
258 
260  void modifyGraph(const GraphChange& change);
261 
263  bool isAlwaysInvalid(const GraphChange& change) const;
264 
266  bool checkArcAddition(NodeId x, NodeId y) const;
267 
269  bool checkArcDeletion(NodeId x, NodeId y) const;
270 
272  bool checkArcReversal(NodeId x, NodeId y) const;
273 
275  bool checkModification(const ArcAddition& change) const;
276 
278  bool checkModification(const ArcDeletion& change) const;
279 
281  bool checkModification(const ArcReversal& change) const;
282 
284  bool checkModification(const GraphChange& change) const;
285 
287  };
288 
289  template < typename CONSTRAINT >
290  class __StructuralConstraintSetStatic< CONSTRAINT >
291  : public virtual CONSTRAINT
292  , public virtual __StructuralRoot {
293  public:
295  using first_constraint = CONSTRAINT;
296 
298  using next_constraints = __StructuralRoot;
299 
300  // determines the set of all constraints in the set (included inherited
301  // ones)
304  using allConstraints = typename std::conditional<
305  std::is_base_of< __StructuralRoot, CONSTRAINT >::value,
306  typename __ConcatConstraintSet<
307  CONSTRAINT,
308  typename CONSTRAINT::allConstraints >::type,
309  __ConstraintSet< CONSTRAINT > >::type;
310 
319  using minConstraints = typename allConstraints::minset::set;
320 
321  // ##########################################################################
323  // ##########################################################################
325 
327  __StructuralConstraintSetStatic();
328 
330  __StructuralConstraintSetStatic(
331  const __StructuralConstraintSetStatic< CONSTRAINT >&);
332 
334  ~__StructuralConstraintSetStatic();
335 
337 
338  // ##########################################################################
340  // ##########################################################################
342 
344  __StructuralConstraintSetStatic< CONSTRAINT >&
345  operator=(const __StructuralConstraintSetStatic< CONSTRAINT >&);
346 
348 
349  // ##########################################################################
351  // ##########################################################################
353 
355  void setGraph(const DiGraph& graph);
356 
358  void modifyGraph(const ArcAddition& change);
359 
361  void modifyGraph(const ArcDeletion& change);
362 
364  void modifyGraph(const ArcReversal& change);
365 
367  void modifyGraph(const GraphChange& change);
368 
370  bool isAlwaysInvalid(const GraphChange& change) const;
371 
373  bool checkArcAddition(NodeId x, NodeId y) const;
374 
376  bool checkArcDeletion(NodeId x, NodeId y) const;
377 
379  bool checkArcReversal(NodeId x, NodeId y) const;
380 
382  bool checkModification(const ArcAddition& change) const;
383 
385  bool checkModification(const ArcDeletion& change) const;
386 
388  bool checkModification(const ArcReversal& change) const;
389 
391  bool checkModification(const GraphChange& change) const;
392 
394  };
395 
396 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
397 
425  template < typename CONSTRAINT1, typename... OTHER_CONSTRAINTS >
427  : public virtual __StructuralConstraintSetStatic<
428  CONSTRAINT1,
429  OTHER_CONSTRAINTS... >::minConstraints {
430  public:
431  using constraints = typename __StructuralConstraintSetStatic<
432  CONSTRAINT1,
433  OTHER_CONSTRAINTS... >::minConstraints;
434 
435  // ##########################################################################
437  // ##########################################################################
439 
442 
445  const StructuralConstraintSetStatic< CONSTRAINT1,
446  OTHER_CONSTRAINTS... >&);
447 
450 
452 
453  // ##########################################################################
455  // ##########################################################################
457 
459  StructuralConstraintSetStatic< CONSTRAINT1, OTHER_CONSTRAINTS... >&
460  operator=(const StructuralConstraintSetStatic< CONSTRAINT1,
461  OTHER_CONSTRAINTS... >&);
462 
464 
465  // ##########################################################################
467  // ##########################################################################
469 
471  void setGraph(const DiGraph& graph);
472 
474  void modifyGraph(const ArcAddition& change);
475 
477  void modifyGraph(const ArcDeletion& change);
478 
480  void modifyGraph(const ArcReversal& change);
481 
483  void modifyGraph(const GraphChange& change);
484 
486  bool isAlwaysInvalid(const GraphChange& change) const;
487 
489  bool checkArcAddition(NodeId x, NodeId y) const;
490 
492  bool checkArcDeletion(NodeId x, NodeId y) const;
493 
495  bool checkArcReversal(NodeId x, NodeId y) const;
496 
498  bool checkModification(const ArcAddition& change) const;
499 
501  bool checkModification(const ArcDeletion& change) const;
502 
504  bool checkModification(const ArcReversal& change) const;
505 
507  bool checkModification(const GraphChange& change) const;
508 
510  };
511 
512 #ifndef DOXYGEN_SHOULD_SKIP_THIS
513 
514  template < typename CONSTRAINT >
515  class StructuralConstraintSetStatic< CONSTRAINT >
516  : public virtual __StructuralConstraintSetStatic<
517  CONSTRAINT >::minConstraints {
518  public:
519  using constraints =
520  typename __StructuralConstraintSetStatic< CONSTRAINT >::minConstraints;
521 
522  // ##########################################################################
524  // ##########################################################################
526 
529 
533 
536 
538 
539  // ##########################################################################
541  // ##########################################################################
543 
547 
549 
550  // ##########################################################################
552  // ##########################################################################
554 
556  void setGraph(const DiGraph& graph);
557 
559  void modifyGraph(const ArcAddition& change);
560 
562  void modifyGraph(const ArcDeletion& change);
563 
565  void modifyGraph(const ArcReversal& change);
566 
568  void modifyGraph(const GraphChange& change);
569 
571  bool isAlwaysInvalid(const GraphChange& change) const;
572 
574  bool checkArcAddition(NodeId x, NodeId y) const;
575 
577  bool checkArcDeletion(NodeId x, NodeId y) const;
578 
580  bool checkArcReversal(NodeId x, NodeId y) const;
581 
583  bool checkModification(const ArcAddition& change) const;
584 
586  bool checkModification(const ArcDeletion& change) const;
587 
589  bool checkModification(const ArcReversal& change) const;
590 
592  bool checkModification(const GraphChange& change) const;
593 
595  };
596 
597 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
598 
599  } /* namespace learning */
600 
601 } /* namespace gum */
602 
605 
606 #endif /* GUM_LEARNING_STRUCTURAL_CONSTRAINT_SET_STATIC_H */
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
The class for notifying learning algorithms of new arc additionsThis class is convenient to know at c...
Definition: graphChange.h:150
The class for notifying learning algorithms of arc removalsThis class is convenient to know at compil...
Definition: graphChange.h:216
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
typename __StructuralConstraintSetStatic< CONSTRAINT1, OTHER_CONSTRAINTS... >::minConstraints constraints
the "meta-programming" class for storing structural constraintsIn aGrUM, there are two ways to store ...
Base class for all oriented graphs.
Definition: diGraph.h:111
The class for notifying learning algorithms of arc reversalsThis class is convenient to know at compi...
Definition: graphChange.h:282
Size NodeId
Type for node ids.
Definition: graphElements.h:98
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.