aGrUM 2.3.2
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-2025 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-2025 *
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_();
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 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). Rhis produces an _ConstraintSet_. This type is to be used internally.
302 using allConstraints = typename std::conditional<
303 std::is_base_of< _StructuralRoot_, CONSTRAINT >::value,
304 typename _ConcatConstraintSet_< CONSTRAINT, typename CONSTRAINT::allConstraints >::type,
305 _ConstraintSet_< CONSTRAINT > >::type;
306
315 using minConstraints = typename allConstraints::minset::set;
316
317 // ##########################################################################
319 // ##########################################################################
321
323 _StructuralConstraintSetStatic_();
324
326 _StructuralConstraintSetStatic_(const _StructuralConstraintSetStatic_< CONSTRAINT >&);
327
329 ~_StructuralConstraintSetStatic_();
330
332
333 // ##########################################################################
335 // ##########################################################################
337
339 _StructuralConstraintSetStatic_< CONSTRAINT >&
340 operator=(const _StructuralConstraintSetStatic_< CONSTRAINT >&);
341
343
344 // ##########################################################################
346 // ##########################################################################
348
350 void setGraph(const DiGraph& graph);
351
353 void modifyGraph(const ArcAddition& change);
354
356 void modifyGraph(const ArcDeletion& change);
357
359 void modifyGraph(const ArcReversal& change);
360
362 void modifyGraph(const GraphChange& change);
363
365 bool isAlwaysInvalid(const GraphChange& change) const;
366
368 bool checkArcAddition(NodeId x, NodeId y) const;
369
371 bool checkArcDeletion(NodeId x, NodeId y) const;
372
374 bool checkArcReversal(NodeId x, NodeId y) const;
375
377 bool checkModification(const ArcAddition& change) const;
378
380 bool checkModification(const ArcDeletion& change) const;
381
383 bool checkModification(const ArcReversal& change) const;
384
386 bool checkModification(const GraphChange& change) const;
387
389 };
390
391#endif /* DOXYGEN_SHOULD_SKIP_THIS */
392
420 template < typename CONSTRAINT1, typename... OTHER_CONSTRAINTS >
422 public virtual _StructuralConstraintSetStatic_< CONSTRAINT1,
423 OTHER_CONSTRAINTS... >::minConstraints {
424 public:
426 typename _StructuralConstraintSetStatic_< CONSTRAINT1,
427 OTHER_CONSTRAINTS... >::minConstraints;
428
429 // ##########################################################################
431 // ##########################################################################
433
436
440
443
445
446 // ##########################################################################
448 // ##########################################################################
450
452 StructuralConstraintSetStatic< CONSTRAINT1, OTHER_CONSTRAINTS... >&
454
456
457 // ##########################################################################
459 // ##########################################################################
461
463 void setGraph(const DiGraph& graph);
464
466 void modifyGraph(const ArcAddition& change);
467
469 void modifyGraph(const ArcDeletion& change);
470
472 void modifyGraph(const ArcReversal& change);
473
475 void modifyGraph(const GraphChange& change);
476
478 bool isAlwaysInvalid(const GraphChange& change) const;
479
482
485
488
490 bool checkModification(const ArcAddition& change) const;
491
493 bool checkModification(const ArcDeletion& change) const;
494
496 bool checkModification(const ArcReversal& change) const;
497
499 bool checkModification(const GraphChange& change) const;
500
502 };
503
504#ifndef DOXYGEN_SHOULD_SKIP_THIS
505
506 template < typename CONSTRAINT >
507 class StructuralConstraintSetStatic< CONSTRAINT >:
508 public virtual _StructuralConstraintSetStatic_< CONSTRAINT >::minConstraints {
509 public:
510 using constraints = typename _StructuralConstraintSetStatic_< CONSTRAINT >::minConstraints;
511
512 // ##########################################################################
514 // ##########################################################################
516
519
522
525
527
528 // ##########################################################################
530 // ##########################################################################
532
536
538
539 // ##########################################################################
541 // ##########################################################################
543
545 void setGraph(const DiGraph& graph);
546
548 void modifyGraph(const ArcAddition& change);
549
551 void modifyGraph(const ArcDeletion& change);
552
554 void modifyGraph(const ArcReversal& change);
555
557 void modifyGraph(const GraphChange& change);
558
560 bool isAlwaysInvalid(const GraphChange& change) const;
561
563 bool checkArcAddition(NodeId x, NodeId y) const;
564
566 bool checkArcDeletion(NodeId x, NodeId y) const;
567
569 bool checkArcReversal(NodeId x, NodeId y) const;
570
572 bool checkModification(const ArcAddition& change) const;
573
575 bool checkModification(const ArcDeletion& change) const;
576
578 bool checkModification(const ArcReversal& change) const;
579
581 bool checkModification(const GraphChange& change) const;
582
584 };
585
586#endif /* DOXYGEN_SHOULD_SKIP_THIS */
587
588 } /* namespace learning */
589
590} /* namespace gum */
591
594
595#endif /* GUM_LEARNING_STRUCTURAL_CONSTRAINT_SET_STATIC_H */
Base class for all oriented graphs.
Definition diGraph.h:130
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 "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 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
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)
StructuralConstraintSetStatic()
default constructor
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
bool checkModification(const ArcDeletion &change) const
checks whether the constraints enable to remove an arc
StructuralConstraintSetStatic(const StructuralConstraintSetStatic< CONSTRAINT1, OTHER_CONSTRAINTS... > &)
copy constructor
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:54
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
the "meta-programming" class for storing several structural constraints