aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
structuralConstraintIndegree_inl.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#pragma once
42
49
51#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
53
54namespace gum::learning {
55
57 INLINE void StructuralConstraintIndegree::setGraphAlone(const DiGraph& graph) {
58 // check that the max_indegree corresponds to the graph
59 for (const auto id: graph) {
60 if (!_Indegree_max_parents_.exists(id)) {
62 }
63 }
64 }
65
68 return (_Indegree_max_parents_[y] > _graph_.parents(y).size());
69 }
70
73 return true;
74 }
75
78 return (_Indegree_max_parents_[x] > _graph_.parents(x).size());
79 }
80
83 NodeId node2,
84 NodeId node3) const {
85 return _Indegree_max_parents_[node1] >= _graph_.parents(node1).size() + 2;
86 }
87
90 NodeId node2,
91 NodeId node3) const {
92 return _Indegree_max_parents_[node2] > _graph_.parents(node1).size();
93 }
94
96 INLINE bool
98 return checkArcAdditionAlone(change.node1(), change.node2());
99 }
100
102 INLINE bool
104 return checkArcDeletionAlone(change.node1(), change.node2());
105 }
106
108 INLINE bool
110 return checkArcReversalAlone(change.node1(), change.node2());
111 }
112
115 const ArcTriangleDeletion1& change) const {
116 return checkArcTriangleDeletion1Alone(change.node1(), change.node2(), change.node3());
117 }
118
121 const ArcTriangleDeletion2& change) const {
122 return checkArcTriangleDeletion2Alone(change.node1(), change.node2(), change.node3());
123 }
124
126
129
132
135
138
141
144
146 INLINE bool StructuralConstraintIndegree::isAlwaysInvalidAlone(const GraphChange& change) const {
147 switch (change.type()) {
148 case GraphChangeType::ARC_ADDITION : return _Indegree_max_parents_[change.node2()] == 0;
149
150 case GraphChangeType::ARC_DELETION : return true;
151
152 case GraphChangeType::ARC_REVERSAL : return _Indegree_max_parents_[change.node1()] == 0;
153
155 return _Indegree_max_parents_[change.node1()] < 2;
156
158 return _Indegree_max_parents_[change.node2()] < 2;
159
160 default :
161 GUM_ERROR(OperationNotAllowed,
162 "Graph change operation "
163 << change.typeAsString()
164 << " is not supported by the Indegree structural constraint");
165 }
166 }
167
169 INLINE void StructuralConstraintIndegree::setIndegree(const NodeProperty< Size >& max_indegree) {
170 for (const auto& degree: max_indegree) {
171 _Indegree_max_parents_.set(degree.first, degree.second);
172 }
173 }
174
176 INLINE void StructuralConstraintIndegree::setMaxIndegree(Size max_indegree, bool update_all) {
177 if (update_all) {
178 for (auto& degree: _Indegree_max_parents_) {
179 degree.second = max_indegree;
180 }
181 }
182
183 _Indegree_max_indegree_ = max_indegree;
184 }
185
186// include all the methods applicable to the whole class hierarchy
187# define GUM_CONSTRAINT_CLASS_NAME StructuralConstraintIndegree
189# undef GUM_CONSTRAINT_CLASS_NAME
190
192
193} // namespace gum::learning
194
195
196#endif /* DOXYGEN_SHOULD_SKIP_THIS */
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...
Size _Indegree_max_indegree_
a default max indegree to assign for nodes without specified indegree
bool checkArcTriangleDeletion1Alone(NodeId node1, NodeId node2, NodeId node3) const
checks whether the constraints enable to apply an ArcTriangleDeletion1
bool checkModificationAlone(const ArcAddition &change) const
checks whether the constraints enable to add an arc
void setMaxIndegree(Size max_indegree, bool update_all_node=false)
resets the default max indegree and possibly updates the indegree of all nodes
void modifyGraphAlone(const ArcAddition &change)
notify the constraint of a modification of the graph
bool isAlwaysInvalidAlone(const GraphChange &change) const
indicates whether a change will always violate the constraint
bool checkArcDeletionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to remove arc (x,y)
void setIndegree(const NodeProperty< Size > &max_indegree)
sets the default max indegree for all the nodes in the property
void setGraphAlone(const DiGraph &graph)
sets a new graph from which we will perform checkings
Size maxIndegree() const
sets the default max indegree for all the nodes in the property
bool checkArcReversalAlone(NodeId x, NodeId y) const
checks whether the constraints enable to reverse arc (x,y)
NodeProperty< Size > _Indegree_max_parents_
the max number of parents per node
bool checkArcAdditionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to add arc (x,y)
bool checkArcTriangleDeletion2Alone(NodeId node1, NodeId node2, NodeId node3) const
checks whether the constraints enable to apply an ArcTriangleDeletion2
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size NodeId
Type for node ids.
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
include the inlined functions if necessary
Definition CSVParser.h:55
the class for structural constraints limiting the number of parents of nodes in a directed graph