aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
structuralConstraintDAG_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-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#pragma once
41
42
48#ifndef DOXYGEN_SHOULD_SKIP_THIS
49
50# include <agrum/agrum.h>
51
54
55namespace gum {
56
57 namespace learning {
58
60 INLINE void StructuralConstraintDAG::setGraphAlone(const DiGraph& graph) {
61 // check that the digraph has no cycle
62 DAG g;
63
64 for (auto node: graph)
65 g.addNodeWithId(node);
66
67 for (auto& arc: graph.arcs())
68 g.addArc(arc.tail(), arc.head());
69
70 _DAG_cycle_detector_.setDAG(g);
71 }
72
74 INLINE void StructuralConstraintDAG::setGraphAlone(Size nb_nodes) {
75 DAG g;
76
77 for (NodeId i = 0; i < nb_nodes; ++i) {
78 g.addNodeWithId(i);
79 }
80
81 _DAG_cycle_detector_.setDAG(g);
82 }
83
86 return !_DAG_cycle_detector_.hasCycleFromAddition(x, y);
87 }
88
91 return !_DAG_cycle_detector_.hasCycleFromDeletion(x, y);
92 }
93
96 return !_DAG_cycle_detector_.hasCycleFromReversal(x, y);
97 }
98
100 INLINE bool StructuralConstraintDAG::checkModificationAlone(const ArcAddition& change) const {
101 return checkArcAdditionAlone(change.node1(), change.node2());
102 }
103
105 INLINE bool StructuralConstraintDAG::checkModificationAlone(const ArcDeletion& change) const {
106 return checkArcDeletionAlone(change.node1(), change.node2());
107 }
108
110 INLINE bool StructuralConstraintDAG::checkModificationAlone(const ArcReversal& change) const {
111 return checkArcReversalAlone(change.node1(), change.node2());
112 }
113
115 INLINE bool StructuralConstraintDAG::checkModificationAlone(const GraphChange& change) const {
116 switch (change.type()) {
118 return checkArcAdditionAlone(change.node1(), change.node2());
119
121 return checkArcDeletionAlone(change.node1(), change.node2());
122
124 return checkArcReversalAlone(change.node1(), change.node2());
125
126 default :
127 GUM_ERROR(OperationNotAllowed,
128 "edge modifications are not "
129 "supported by StructuralConstraintDAG");
130 }
131 }
132
134 INLINE void StructuralConstraintDAG::modifyGraphAlone(const ArcAddition& change) {
135 _DAG_cycle_detector_.addArc(change.node1(), change.node2());
136 }
137
139 INLINE void StructuralConstraintDAG::modifyGraphAlone(const ArcDeletion& change) {
140 _DAG_cycle_detector_.eraseArc(change.node1(), change.node2());
141 }
142
144 INLINE void StructuralConstraintDAG::modifyGraphAlone(const ArcReversal& change) {
145 _DAG_cycle_detector_.reverseArc(change.node1(), change.node2());
146 }
147
149 INLINE void StructuralConstraintDAG::modifyGraphAlone(const GraphChange& change) {
150 switch (change.type()) {
152 modifyGraphAlone(reinterpret_cast< const ArcAddition& >(change));
153 break;
154
156 modifyGraphAlone(reinterpret_cast< const ArcDeletion& >(change));
157 break;
158
160 modifyGraphAlone(reinterpret_cast< const ArcReversal& >(change));
161 break;
162
163 default :
164 GUM_ERROR(OperationNotAllowed, "edge modifications are not supported by DAG constraints")
165 }
166 }
167
170 return false;
171 }
172
174 INLINE void StructuralConstraintDAG::setGraph(const DAG& graph) {
175 constraints::setGraph(graph);
176 _DAG_cycle_detector_.setDAG(graph);
177 }
178
180 INLINE void StructuralConstraintDAG::setGraph(Size nb_nodes) {
182 setGraphAlone(nb_nodes);
183 }
184
185// include all the methods applicable to the whole class hierarchy
186# define GUM_CONSTRAINT_CLASS_NAME StructuralConstraintDAG
188# undef GUM_CONSTRAINT_CLASS_NAME
189
190 } /* namespace learning */
191
192} /* namespace gum */
193
194#endif /* DOXYGEN_SHOULD_SKIP_THIS */
A class for detecting directed cycles in DAGs when trying to apply many changes to the graph.
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.
bool isAlwaysInvalidAlone(const GraphChange &change) const
indicates whether a change will always violate the constraint
void setGraph(const DAG &graph)
sets a new graph from which we will perform checkings
void modifyGraphAlone(const ArcAddition &change)
notify the constraint of a modification of the graph
bool checkArcAdditionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to add arc (x,y)
bool checkArcReversalAlone(NodeId x, NodeId y) const
checks whether the constraints enable to reverse arc (x,y)
bool checkModificationAlone(const ArcAddition &change) const
checks whether the constraints enable to add an arc
void setGraphAlone(const DiGraph &graph)
sets a new graph from which we will perform checkings
DAGCycleDetector _DAG_cycle_detector_
the cycle detector used to check quickly graph modifications
bool checkArcDeletionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to remove arc (x,y)
void setGraph(Size nb_nodes)
sets a new empty graph from which we will perform checkings
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
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 base class for structural constraints used by learning algorithms that learn a directed graph str...