aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
structuralConstraintSetStatic.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
60#ifndef GUM_LEARNING_STRUCTURAL_CONSTRAINT_SET_STATIC_H
61#define GUM_LEARNING_STRUCTURAL_CONSTRAINT_SET_STATIC_H
62
63#include <agrum/agrum.h>
64
67
68#include <type_traits>
69
70namespace gum {
71
72 namespace learning {
73
74#ifndef DOXYGEN_SHOULD_SKIP_THIS
75
76 // a class that indicates the we inherit from a
77 // StructuralConstraintSetStatic
78 struct _StructuralRoot_ {};
79
80 // a temporary structure used to help computing the minimal set of
81 // constraints
82 template < typename FIRST_CONSTRAINT, typename... OTHER_CONSTRAINTS >
83 struct _ConstraintSet_;
84
85 // a structure to concatenate _ConstraintSets_ or simply constraints and
86 // produce as a result a new _ConstraintSet_
87 template < typename SET1, typename SET2 >
88 struct _ConcatConstraintSet_;
89
90 // a helper function to create minimum structural constraint sets and the
91 // methods actually used on all these constraints. This is a helper for
92 // the class that the user should use, i.e., StructuralConstraintSetStatic
93 template < typename FIRST_CONSTRAINT, typename... OTHER_CONSTRAINTS >
94 class _StructuralConstraintSetStatic_;
95
96 // ============================================================================
97 // checks whether a given structural constraint belongs to a given set of
98 // structural constraints
99 template < typename CONSTRAINT, typename SET >
100 struct _IsInConstraintSet_;
101
102 template < typename CONSTRAINT, typename SET >
103 struct _IsInConstraintSet_< CONSTRAINT, _ConstraintSet_< SET > > {
104 constexpr static bool value = std::is_same< CONSTRAINT, SET >::value;
105 };
106
107 template < typename CONSTRAINT, typename SET1, typename... SETS >
108 struct _IsInConstraintSet_< CONSTRAINT, _ConstraintSet_< SET1, SETS... > > {
109 constexpr static bool value
110 = std::is_same< CONSTRAINT, SET1 >::value
111 || _IsInConstraintSet_< CONSTRAINT, _ConstraintSet_< SETS... > >::value;
112 };
113
114 // ============================================================================
115 // a temporary structure used to help computing the minimal set of
116 // constraints
117 // (removing all duplicates) belonging to a given set of structural
118 // constraints. For instance, if we have the following structural hierarchy:
119 // Z->Y->X, T->Y->X and we have the set of constraints S = <Z,T>, the set
120 // of
121 // all structural constraints reachable from S is S' = <Z,Y,X,T,Y,X>. The
122 // goal
123 // of the following class is to transform S' into S'' = <Z,T,Y,X>, i.e., the
124 // set S' without any duplicates.
125 template < typename FIRST_CONSTRAINT, typename... OTHER_CONSTRAINTS >
126 struct _ConstraintSet_: public _ConstraintSet_< OTHER_CONSTRAINTS... > {
127 using minset = typename std::conditional<
128 _IsInConstraintSet_< FIRST_CONSTRAINT, _ConstraintSet_< OTHER_CONSTRAINTS... > >::value,
129 typename _ConstraintSet_< OTHER_CONSTRAINTS... >::minset,
130 typename _ConcatConstraintSet_<
131 FIRST_CONSTRAINT,
132 typename _ConstraintSet_< OTHER_CONSTRAINTS... >::minset >::type >::type;
133 using set = _StructuralConstraintSetStatic_< FIRST_CONSTRAINT, OTHER_CONSTRAINTS... >;
134 };
135
136 template < typename CONSTRAINT >
137 struct _ConstraintSet_< CONSTRAINT > {
138 using minset = _ConstraintSet_< CONSTRAINT >;
139 using set = _StructuralConstraintSetStatic_< CONSTRAINT >;
140 };
141
142 // ============================================================================
143 // a structure to concatenate _ConstraintSets_ or simply constraints and
144 // produce as a result a new _ConstraintSet_
145 template < typename SET1, typename SET2 >
146 struct _ConcatConstraintSet_;
147
148 template < typename CONSTRAINT1, typename CONSTRAINT2 >
149 struct _ConcatConstraintSet_< CONSTRAINT1, _ConstraintSet_< CONSTRAINT2 > > {
150 using type = _ConstraintSet_< CONSTRAINT1, CONSTRAINT2 >;
151 };
152
153 template < typename CONSTRAINT1, typename CONSTRAINT2 >
154 struct _ConcatConstraintSet_< _ConstraintSet_< CONSTRAINT1 >, _ConstraintSet_< CONSTRAINT2 > > {
155 using type = _ConstraintSet_< CONSTRAINT1, CONSTRAINT2 >;
156 };
157
158 template < typename CONSTRAINT1, typename CONSTRAINT2, typename... OTHER_CONSTRAINT2 >
159 struct _ConcatConstraintSet_< CONSTRAINT1,
160 _ConstraintSet_< CONSTRAINT2, OTHER_CONSTRAINT2... > > {
161 using type = _ConstraintSet_< CONSTRAINT1, CONSTRAINT2, OTHER_CONSTRAINT2... >;
162 };
163
164 template < typename CONSTRAINT1, typename CONSTRAINT2, typename... OTHER_CONSTRAINT1 >
165 struct _ConcatConstraintSet_< _ConstraintSet_< CONSTRAINT1, OTHER_CONSTRAINT1... >,
166 _ConstraintSet_< CONSTRAINT2 > > {
167 using type = _ConstraintSet_< CONSTRAINT1, OTHER_CONSTRAINT1..., CONSTRAINT2 >;
168 };
169
170 template < typename CONSTRAINT1,
171 typename CONSTRAINT2,
172 typename... OTHER_CONSTR1,
173 typename... OTHER_CONSTR2 >
174 struct _ConcatConstraintSet_< _ConstraintSet_< CONSTRAINT1, OTHER_CONSTR1... >,
175 _ConstraintSet_< CONSTRAINT2, OTHER_CONSTR2... > > {
176 using type = _ConstraintSet_< CONSTRAINT1, OTHER_CONSTR1..., CONSTRAINT2, OTHER_CONSTR2... >;
177 };
178
179 // ============================================================================
180 // a helper function to create minimum structural constraint sets and the
181 // methods actually used on all these constraints. This is a helper for
182 // the class that the user should use, i.e., StructuralConstraintSetStatic
183 template < typename CONSTRAINT1, typename... OTHER_CONSTRAINTS >
184 class _StructuralConstraintSetStatic_:
185 public virtual CONSTRAINT1,
186 public virtual _StructuralConstraintSetStatic_< OTHER_CONSTRAINTS... > {
187 public:
189 using first_constraint = CONSTRAINT1;
190
192 using next_constraints = _StructuralConstraintSetStatic_< OTHER_CONSTRAINTS... >;
193
194 // determines the set of all constraints in the set (included inherited
195 // ones)
196 using allConstraints = typename _ConcatConstraintSet_<
197 typename std::conditional<
198 std::is_base_of< _StructuralRoot_, CONSTRAINT1 >::value,
199 typename _ConcatConstraintSet_< CONSTRAINT1,
200 typename CONSTRAINT1::allConstraints >::type,
201 _ConstraintSet_< CONSTRAINT1 > >::type,
202 typename next_constraints::allConstraints >::type;
203
212 using minConstraints = typename allConstraints::minset::set;
213
214 // ##########################################################################
216 // ##########################################################################
218
220 _StructuralConstraintSetStatic_();
221
223 _StructuralConstraintSetStatic_(
224 const _StructuralConstraintSetStatic_< CONSTRAINT1, OTHER_CONSTRAINTS... >&);
225
227 ~_StructuralConstraintSetStatic_() override;
228
230
231 // ##########################################################################
233 // ##########################################################################
235
237 _StructuralConstraintSetStatic_< CONSTRAINT1, OTHER_CONSTRAINTS... >&
238 operator=(const _StructuralConstraintSetStatic_< CONSTRAINT1, 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
263 void modifyGraph(const ArcTriangleDeletion1& change);
264
266
269 void modifyGraph(const ArcTriangleDeletion2& change);
270
272 void modifyGraph(const GraphChange& change);
273
275 bool isAlwaysInvalid(const GraphChange& change) const;
276
278 bool checkArcAddition(NodeId x, NodeId y) const;
279
281 bool checkArcDeletion(NodeId x, NodeId y) const;
282
284 bool checkArcReversal(NodeId x, NodeId y) const;
285
287
290 bool checkArcTriangleDeletion1(NodeId node1, NodeId node2, NodeId node3) const;
291
293
296 bool checkArcTriangleDeletion2(NodeId node1, NodeId node2, NodeId node3) const;
297
299 bool checkModification(const ArcAddition& change) const;
300
302 bool checkModification(const ArcDeletion& change) const;
303
305 bool checkModification(const ArcReversal& change) const;
306
308
311 bool checkModification(const ArcTriangleDeletion1& change) const;
312
314
317 bool checkModification(const ArcTriangleDeletion2& change) const;
318
320 bool checkModification(const GraphChange& change) const;
321
323 };
324
325 template < typename CONSTRAINT >
326 class _StructuralConstraintSetStatic_< CONSTRAINT >:
327 public virtual CONSTRAINT,
328 public virtual _StructuralRoot_ {
329 public:
331 using first_constraint = CONSTRAINT;
332
334 using next_constraints = _StructuralRoot_;
335
336 // determines the set of all constraints in the set (included inherited
337 // ones). Rhis produces an _ConstraintSet_. This type is to be used internally.
338 using allConstraints = typename std::conditional<
339 std::is_base_of< _StructuralRoot_, CONSTRAINT >::value,
340 typename _ConcatConstraintSet_< CONSTRAINT, typename CONSTRAINT::allConstraints >::type,
341 _ConstraintSet_< CONSTRAINT > >::type;
342
351 using minConstraints = typename allConstraints::minset::set;
352
353 // ##########################################################################
355 // ##########################################################################
357
359 _StructuralConstraintSetStatic_();
360
362 _StructuralConstraintSetStatic_(const _StructuralConstraintSetStatic_< CONSTRAINT >&);
363
365 ~_StructuralConstraintSetStatic_() override;
366
368
369 // ##########################################################################
371 // ##########################################################################
373
375 _StructuralConstraintSetStatic_< CONSTRAINT >&
376 operator=(const _StructuralConstraintSetStatic_< CONSTRAINT >&);
377
379
380 // ##########################################################################
382 // ##########################################################################
384
386 void setGraph(const DiGraph& graph);
387
389 void modifyGraph(const ArcAddition& change);
390
392 void modifyGraph(const ArcDeletion& change);
393
395 void modifyGraph(const ArcReversal& change);
396
398
401 void modifyGraph(const ArcTriangleDeletion1& change);
402
404
407 void modifyGraph(const ArcTriangleDeletion2& change);
408
410 void modifyGraph(const GraphChange& change);
411
413 bool isAlwaysInvalid(const GraphChange& change) const;
414
416 bool checkArcAddition(NodeId x, NodeId y) const;
417
419 bool checkArcDeletion(NodeId x, NodeId y) const;
420
422 bool checkArcReversal(NodeId x, NodeId y) const;
423
425
428 bool checkArcTriangleDeletion1(NodeId node1, NodeId node2, NodeId node3) const;
429
431
434 bool checkArcTriangleDeletion2(NodeId node1, NodeId node2, NodeId node3) const;
435
437 bool checkModification(const ArcAddition& change) const;
438
440 bool checkModification(const ArcDeletion& change) const;
441
443 bool checkModification(const ArcReversal& change) const;
444
446
449 bool checkModification(const ArcTriangleDeletion1& change) const;
450
452
455 bool checkModification(const ArcTriangleDeletion2& change) const;
456
458 bool checkModification(const GraphChange& change) const;
459
461 };
462
463#endif /* DOXYGEN_SHOULD_SKIP_THIS */
464
492 template < typename CONSTRAINT1, typename... OTHER_CONSTRAINTS >
494 public virtual _StructuralConstraintSetStatic_< CONSTRAINT1,
495 OTHER_CONSTRAINTS... >::minConstraints {
496 public:
498 typename _StructuralConstraintSetStatic_< CONSTRAINT1,
499 OTHER_CONSTRAINTS... >::minConstraints;
500
501 // ##########################################################################
503 // ##########################################################################
505
508
512
515
517
518 // ##########################################################################
520 // ##########################################################################
522
524 StructuralConstraintSetStatic< CONSTRAINT1, OTHER_CONSTRAINTS... >&
526
528
529 // ##########################################################################
531 // ##########################################################################
533
535 void setGraph(const DiGraph& graph);
536
538 void modifyGraph(const ArcAddition& change);
539
541 void modifyGraph(const ArcDeletion& change);
542
544 void modifyGraph(const ArcReversal& change);
545
547
551
553
557
559 void modifyGraph(const GraphChange& change);
560
562 bool isAlwaysInvalid(const GraphChange& change) const;
563
566
569
572
574
577 bool checkArcTriangleDeletion1(NodeId node1, NodeId node2, NodeId node3) const;
578
580
583 bool checkArcTriangleDeletion2(NodeId node1, NodeId node2, NodeId node3) const;
584
586 bool checkModification(const ArcAddition& change) const;
587
589 bool checkModification(const ArcDeletion& change) const;
590
592 bool checkModification(const ArcReversal& change) const;
593
595
598 bool checkModification(const ArcTriangleDeletion1& change) const;
599
601
604 bool checkModification(const ArcTriangleDeletion2& change) const;
605
607 bool checkModification(const GraphChange& change) const;
608
610 };
611
612#ifndef DOXYGEN_SHOULD_SKIP_THIS
613
614 template < typename CONSTRAINT >
615 class StructuralConstraintSetStatic< CONSTRAINT >:
616 public virtual _StructuralConstraintSetStatic_< CONSTRAINT >::minConstraints {
617 public:
618 using constraints = typename _StructuralConstraintSetStatic_< CONSTRAINT >::minConstraints;
619
620 // ##########################################################################
622 // ##########################################################################
624
627
630
633
635
636 // ##########################################################################
638 // ##########################################################################
640
644
646
647 // ##########################################################################
649 // ##########################################################################
651
653 void setGraph(const DiGraph& graph);
654
656 void modifyGraph(const ArcAddition& change);
657
659 void modifyGraph(const ArcDeletion& change);
660
662 void modifyGraph(const ArcReversal& change);
663
665
668 void modifyGraph(const ArcTriangleDeletion1& change);
669
671
674 void modifyGraph(const ArcTriangleDeletion2& change);
675
677 void modifyGraph(const GraphChange& change);
678
680 bool isAlwaysInvalid(const GraphChange& change) const;
681
683 bool checkArcAddition(NodeId x, NodeId y) const;
684
686 bool checkArcDeletion(NodeId x, NodeId y) const;
687
689 bool checkArcReversal(NodeId x, NodeId y) const;
690
692
695 bool checkArcTriangleDeletion1(NodeId node1, NodeId node2, NodeId node3) const;
696
698
701 bool checkArcTriangleDeletion2(NodeId node1, NodeId node2, NodeId node3) const;
702
704 bool checkModification(const ArcAddition& change) const;
705
707 bool checkModification(const ArcDeletion& change) const;
708
710 bool checkModification(const ArcReversal& change) const;
711
713
716 bool checkModification(const ArcTriangleDeletion1& change) const;
717
719
722 bool checkModification(const ArcTriangleDeletion2& change) const;
723
725 bool checkModification(const GraphChange& change) const;
726
728 };
729
730#endif /* DOXYGEN_SHOULD_SKIP_THIS */
731
732 } /* namespace learning */
733
734} /* namespace gum */
735
738
739#endif /* GUM_LEARNING_STRUCTURAL_CONSTRAINT_SET_STATIC_H */
Base class for all oriented graphs.
Definition diGraph.h:132
The class for notifying learning algorithms of new arc additions.
The class for notifying learning algorithms of arc removals.
The class for notifying learning algorithms of arc reversals.
The graph change substituting a triangle node1->node2->node3 + node1->node3 into v-structure node2->n...
The graph change substituting a triangle node1->node2->node3 + node1->node3 into v-structure node1->n...
the "meta-programming" class for storing structural constraints
bool checkModification(const GraphChange &change) const
checks whether the constraints enable to perform a graph change
bool checkArcReversal(NodeId x, NodeId y) const
checks whether the constraints enable to reverse arc (x,y)
void modifyGraph(const ArcDeletion &change)
notify the constraint of a modification of the graph
void modifyGraph(const ArcTriangleDeletion2 &change)
notify the constraint of a modification of the graph
void modifyGraph(const ArcAddition &change)
notify the constraint of a modification of the graph
bool checkModification(const ArcAddition &change) const
checks whether the constraints enable to add an arc
bool checkModification(const ArcReversal &change) const
checks whether the constraints enable to reverse an arc
void modifyGraph(const ArcTriangleDeletion1 &change)
notify the constraint of a modification of the graph
typename _StructuralConstraintSetStatic_< CONSTRAINT1, OTHER_CONSTRAINTS... >::minConstraints constraints
void modifyGraph(const ArcReversal &change)
notify the constraint of a modification of the graph
StructuralConstraintSetStatic< CONSTRAINT1, OTHER_CONSTRAINTS... > & operator=(const StructuralConstraintSetStatic< CONSTRAINT1, OTHER_CONSTRAINTS... > &)
copy operator
void modifyGraph(const GraphChange &change)
notify the constraint of a modification of the graph
bool isAlwaysInvalid(const GraphChange &change) const
indicates whether a change will always violate the constraint
bool checkArcDeletion(NodeId x, NodeId y) const
checks whether the constraints enable to remove arc (x,y)
bool checkModification(const ArcTriangleDeletion1 &change) const
checks whether the constraints enable to apply an ArcTriangleDeletion1
bool checkModification(const ArcTriangleDeletion2 &change) const
checks whether the constraints enable to apply an ArcTriangleDeletion2
StructuralConstraintSetStatic()
default constructor
bool checkArcTriangleDeletion1(NodeId node1, NodeId node2, NodeId node3) const
checks whether the constraints enable to apply an ArcTriangleDeletion1
bool checkArcAddition(NodeId x, NodeId y) const
checks whether the constraints enable to add arc (x,y)
void setGraph(const DiGraph &graph)
sets a new graph from which we will perform checkings
~StructuralConstraintSetStatic() override
destructor
bool checkModification(const ArcDeletion &change) const
checks whether the constraints enable to remove an arc
StructuralConstraintSetStatic(const StructuralConstraintSetStatic< CONSTRAINT1, OTHER_CONSTRAINTS... > &)
copy constructor
bool checkArcTriangleDeletion2(NodeId node1, NodeId node2, NodeId node3) const
checks whether the constraints enable to apply an ArcTriangleDeletion2
Base classes for oriented graphs.
the classes to account for structure changes in a graph
Size NodeId
Type for node ids.
include the inlined functions if necessary
Definition CSVParser.h:55
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
the "meta-programming" class for storing several structural constraints