40 #ifndef GUM_LEARNING_STRUCTURAL_CONSTRAINT_SET_STATIC_H 41 #define GUM_LEARNING_STRUCTURAL_CONSTRAINT_SET_STATIC_H 43 #include <type_traits> 45 #include <agrum/agrum.h> 46 #include <agrum/tools/graphs/diGraph.h> 47 #include <agrum/BN/learning/structureUtils/graphChange.h> 53 #ifndef DOXYGEN_SHOULD_SKIP_THIS 57 struct _StructuralRoot_ {};
61 template <
typename FIRST_CONSTRAINT,
typename... OTHER_CONSTRAINTS >
62 struct _ConstraintSet_;
66 template <
typename SET1,
typename SET2 >
67 struct _ConcatConstraintSet_;
72 template <
typename FIRST_CONSTRAINT,
typename... OTHER_CONSTRAINTS >
73 class _StructuralConstraintSetStatic_;
78 template <
typename CONSTRAINT,
typename SET >
79 struct _IsInConstraintSet_;
81 template <
typename CONSTRAINT,
typename SET >
82 struct _IsInConstraintSet_< CONSTRAINT, _ConstraintSet_< SET > > {
83 constexpr static bool value = std::is_same< CONSTRAINT, SET >::value;
86 template <
typename CONSTRAINT,
typename SET1,
typename... SETS >
87 struct _IsInConstraintSet_< CONSTRAINT, _ConstraintSet_< SET1, SETS... > > {
88 constexpr static bool value
89 = std::is_same< CONSTRAINT, SET1 >::value
90 || _IsInConstraintSet_< CONSTRAINT, _ConstraintSet_< SETS... > >::value;
104 template <
typename FIRST_CONSTRAINT,
typename... OTHER_CONSTRAINTS >
105 struct _ConstraintSet_:
public _ConstraintSet_< OTHER_CONSTRAINTS... > {
106 using minset =
typename std::conditional<
107 _IsInConstraintSet_< FIRST_CONSTRAINT, _ConstraintSet_< OTHER_CONSTRAINTS... > >::value,
108 typename _ConstraintSet_< OTHER_CONSTRAINTS... >::minset,
109 typename _ConcatConstraintSet_<
111 typename _ConstraintSet_< OTHER_CONSTRAINTS... >::minset >::type >::type;
112 using set = _StructuralConstraintSetStatic_< FIRST_CONSTRAINT, OTHER_CONSTRAINTS... >;
115 template <
typename CONSTRAINT >
116 struct _ConstraintSet_< CONSTRAINT > {
117 using minset = _ConstraintSet_< CONSTRAINT >;
118 using set = _StructuralConstraintSetStatic_< CONSTRAINT >;
124 template <
typename SET1,
typename SET2 >
125 struct _ConcatConstraintSet_;
127 template <
typename CONSTRAINT1,
typename CONSTRAINT2 >
128 struct _ConcatConstraintSet_< CONSTRAINT1, _ConstraintSet_< CONSTRAINT2 > > {
129 using type = _ConstraintSet_< CONSTRAINT1, CONSTRAINT2 >;
132 template <
typename CONSTRAINT1,
typename CONSTRAINT2 >
133 struct _ConcatConstraintSet_< _ConstraintSet_< CONSTRAINT1 >, _ConstraintSet_< CONSTRAINT2 > > {
134 using type = _ConstraintSet_< CONSTRAINT1, CONSTRAINT2 >;
137 template <
typename CONSTRAINT1,
typename CONSTRAINT2,
typename... OTHER_CONSTRAINT2 >
138 struct _ConcatConstraintSet_< CONSTRAINT1,
139 _ConstraintSet_< CONSTRAINT2, OTHER_CONSTRAINT2... > > {
140 using type = _ConstraintSet_< CONSTRAINT1, CONSTRAINT2, OTHER_CONSTRAINT2... >;
143 template <
typename CONSTRAINT1,
typename CONSTRAINT2,
typename... OTHER_CONSTRAINT1 >
144 struct _ConcatConstraintSet_< _ConstraintSet_< CONSTRAINT1, OTHER_CONSTRAINT1... >,
145 _ConstraintSet_< CONSTRAINT2 > > {
146 using type = _ConstraintSet_< CONSTRAINT1, OTHER_CONSTRAINT1..., CONSTRAINT2 >;
149 template <
typename CONSTRAINT1,
150 typename CONSTRAINT2,
151 typename... OTHER_CONSTR1,
152 typename... OTHER_CONSTR2 >
153 struct _ConcatConstraintSet_< _ConstraintSet_< CONSTRAINT1, OTHER_CONSTR1... >,
154 _ConstraintSet_< CONSTRAINT2, OTHER_CONSTR2... > > {
155 using type = _ConstraintSet_< CONSTRAINT1, OTHER_CONSTR1..., CONSTRAINT2, OTHER_CONSTR2... >;
162 template <
typename CONSTRAINT1,
typename... OTHER_CONSTRAINTS >
163 class _StructuralConstraintSetStatic_:
164 public virtual CONSTRAINT1,
165 public virtual _StructuralConstraintSetStatic_< OTHER_CONSTRAINTS... > {
168 using first_constraint = CONSTRAINT1;
171 using next_constraints = _StructuralConstraintSetStatic_< OTHER_CONSTRAINTS... >;
175 using allConstraints =
typename _ConcatConstraintSet_<
176 typename std::conditional<
177 std::is_base_of< _StructuralRoot_, CONSTRAINT1 >::value,
178 typename _ConcatConstraintSet_< CONSTRAINT1,
179 typename CONSTRAINT1::allConstraints >::type,
180 _ConstraintSet_< CONSTRAINT1 > >::type,
181 typename next_constraints::allConstraints >::type;
191 using minConstraints =
typename allConstraints::minset::set;
199 _StructuralConstraintSetStatic_();
202 _StructuralConstraintSetStatic_(
203 const _StructuralConstraintSetStatic_< CONSTRAINT1, OTHER_CONSTRAINTS... >&);
206 ~_StructuralConstraintSetStatic_();
216 _StructuralConstraintSetStatic_< CONSTRAINT1, OTHER_CONSTRAINTS... >&
217 operator=(
const _StructuralConstraintSetStatic_< CONSTRAINT1, OTHER_CONSTRAINTS... >&);
227 void setGraph(
const DiGraph& graph);
230 void modifyGraph(
const ArcAddition& change);
233 void modifyGraph(
const ArcDeletion& change);
236 void modifyGraph(
const ArcReversal& change);
239 void modifyGraph(
const GraphChange& change);
242 bool isAlwaysInvalid(
const GraphChange& change)
const;
245 bool checkArcAddition(NodeId x, NodeId y)
const;
248 bool checkArcDeletion(NodeId x, NodeId y)
const;
251 bool checkArcReversal(NodeId x, NodeId y)
const;
254 bool checkModification(
const ArcAddition& change)
const;
257 bool checkModification(
const ArcDeletion& change)
const;
260 bool checkModification(
const ArcReversal& change)
const;
263 bool checkModification(
const GraphChange& change)
const;
268 template <
typename CONSTRAINT >
269 class _StructuralConstraintSetStatic_< CONSTRAINT >:
270 public virtual CONSTRAINT,
271 public virtual _StructuralRoot_ {
274 using first_constraint = CONSTRAINT;
277 using next_constraints = _StructuralRoot_;
283 using allConstraints =
typename std::conditional<
284 std::is_base_of< _StructuralRoot_, CONSTRAINT >::value,
285 typename _ConcatConstraintSet_< CONSTRAINT,
typename CONSTRAINT::allConstraints >::type,
286 _ConstraintSet_< CONSTRAINT > >::type;
296 using minConstraints =
typename allConstraints::minset::set;
304 _StructuralConstraintSetStatic_();
307 _StructuralConstraintSetStatic_(
const _StructuralConstraintSetStatic_< CONSTRAINT >&);
310 ~_StructuralConstraintSetStatic_();
320 _StructuralConstraintSetStatic_< CONSTRAINT >&
321 operator=(
const _StructuralConstraintSetStatic_< CONSTRAINT >&);
331 void setGraph(
const DiGraph& graph);
334 void modifyGraph(
const ArcAddition& change);
337 void modifyGraph(
const ArcDeletion& change);
340 void modifyGraph(
const ArcReversal& change);
343 void modifyGraph(
const GraphChange& change);
346 bool isAlwaysInvalid(
const GraphChange& change)
const;
349 bool checkArcAddition(NodeId x, NodeId y)
const;
352 bool checkArcDeletion(NodeId x, NodeId y)
const;
355 bool checkArcReversal(NodeId x, NodeId y)
const;
358 bool checkModification(
const ArcAddition& change)
const;
361 bool checkModification(
const ArcDeletion& change)
const;
364 bool checkModification(
const ArcReversal& change)
const;
367 bool checkModification(
const GraphChange& change)
const;
401 template <
typename CONSTRAINT1,
typename... OTHER_CONSTRAINTS >
403 public virtual _StructuralConstraintSetStatic_< CONSTRAINT1,
404 OTHER_CONSTRAINTS... >::minConstraints {
407 typename _StructuralConstraintSetStatic_< CONSTRAINT1,
408 OTHER_CONSTRAINTS... >::minConstraints;
444 void setGraph(
const DiGraph& graph);
485 #ifndef DOXYGEN_SHOULD_SKIP_THIS 487 template <
typename CONSTRAINT >
489 public virtual _StructuralConstraintSetStatic_< CONSTRAINT >::minConstraints {
491 using constraints =
typename _StructuralConstraintSetStatic_< CONSTRAINT >::minConstraints;
499 StructuralConstraintSetStatic();
505 ~StructuralConstraintSetStatic();
526 void setGraph(
const DiGraph& graph);
529 void modifyGraph(
const ArcAddition& change);
532 void modifyGraph(
const ArcDeletion& change);
535 void modifyGraph(
const ArcReversal& change);
538 void modifyGraph(
const GraphChange& change);
541 bool isAlwaysInvalid(
const GraphChange& change)
const;
544 bool checkArcAddition(NodeId x, NodeId y)
const;
547 bool checkArcDeletion(NodeId x, NodeId y)
const;
550 bool checkArcReversal(NodeId x, NodeId y)
const;
553 bool checkModification(
const ArcAddition& change)
const;
556 bool checkModification(
const ArcDeletion& change)
const;
559 bool checkModification(
const ArcReversal& change)
const;
562 bool checkModification(
const GraphChange& change)
const;
574 #include <agrum/BN/learning/constraints/structuralConstraintSetStatic_tpl.h> bool isAlwaysInvalid(const GraphChange &change) const
indicates whether a change will always violate the constraint
INLINE void emplace(Args &&... args)
StructuralConstraintSetStatic()
default constructor
StructuralConstraintSetStatic(const StructuralConstraintSetStatic< CONSTRAINT1, OTHER_CONSTRAINTS... > &)
copy constructor
~StructuralConstraintSetStatic()
destructor
bool checkArcDeletion(NodeId x, NodeId y) const
checks whether the constraints enable to remove arc (x,y)
the "meta-programming" class for storing structural constraintsIn aGrUM, there are two ways to store ...
void setGraph(const DiGraph &graph)
sets a new graph from which we will perform checkings
bool checkArcReversal(NodeId x, NodeId y) const
checks whether the constraints enable to reverse arc (x,y)
StructuralConstraintSetStatic< CONSTRAINT1, OTHER_CONSTRAINTS... > & operator=(const StructuralConstraintSetStatic< CONSTRAINT1, OTHER_CONSTRAINTS... > &)
copy operator
bool checkModification(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)
bool checkArcAddition(NodeId x, NodeId y) const
checks whether the constraints enable to add arc (x,y)
void modifyGraph(const GraphChange &change)
notify the constraint of a modification of the graph