aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
structuralConstraintDAG.cpp
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
48
50#ifdef GUM_NO_INLINE
52#endif /* GUM_NO_INLINE */
53
54namespace gum {
55
56 namespace learning {
57
60
66
72
78
81 _graph_(std::move(from._graph_)) {
82 GUM_CONS_MOV(StructuralConstraintDAG);
83 }
84
87
91 if (this != &from) { _graph_ = from._graph_; }
92 return *this;
93 }
94
97 if (this != &from) { _graph_ = std::move(from._graph_); }
98 return *this;
99 }
100
102 // check that the digraph contains no directed cycle
103 DiGraph g;
104
105 for (auto node: graph)
106 g.addNodeWithId(node);
107
108 for (auto& arc: graph.arcs()) {
109 if (graph::hasDirectedPath(g, arc.head(), arc.tail())) {
111 "Graphs with directed cycles cannot be passed to StructuralConstraintDAG");
112 }
113 g.addArc(arc.tail(), arc.head());
114 }
115
116 // ok, here, there is no directed cycle
117 _graph_ = std::move(g);
118 }
119
121 switch (change.type()) {
123 return checkArcAdditionAlone(change.node1(), change.node2());
124
126 return checkArcDeletionAlone(change.node1(), change.node2());
127
129 return checkArcReversalAlone(change.node1(), change.node2());
130
132 return checkArcTriangleDeletion1Alone(change.node1(), change.node2(), change.node3());
133
135 return checkArcTriangleDeletion2Alone(change.node1(), change.node2(), change.node3());
136
137 default :
139 "Graph change operation "
140 << change.typeAsString()
141 << "is not supported by the DAG structural constraint");
142 }
143 }
144
146 switch (change.type()) {
148 modifyGraphAlone(reinterpret_cast< const ArcAddition& >(change));
149 break;
150
152 modifyGraphAlone(reinterpret_cast< const ArcDeletion& >(change));
153 break;
154
156 modifyGraphAlone(reinterpret_cast< const ArcReversal& >(change));
157 break;
158
160 modifyGraphAlone(reinterpret_cast< const ArcTriangleDeletion1& >(change));
161 break;
162
164 modifyGraphAlone(reinterpret_cast< const ArcTriangleDeletion2& >(change));
165 break;
166
167 default :
169 "Graph change operation "
170 << change.typeAsString()
171 << " is not supported by the DAG structural constraints")
172 }
173 }
174 } /* namespace learning */
175
176} /* namespace gum */
Base class for dag.
Definition DAG.h:121
Base class for all oriented graphs.
Definition diGraph.h:132
void addArc(const NodeId tail, const NodeId head) override
insert a new arc into the directed graph
Definition diGraph_inl.h:59
Exception : existence of a directed cycle in a graph.
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
Exception : operation not allowed.
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...
std::string typeAsString() const
returns a string corresponding to the type of the change
NodeId node1() const noexcept
returns the first node involved in the modification
GraphChangeType type() const noexcept
returns the type of the operation
NodeId node2() const noexcept
returns the second node involved in the modification
NodeId node3() const
returns the third node involved in the modification (if any)
The base class for structural constraints imposed by DAGs.
StructuralConstraintDAG & operator=(const StructuralConstraintDAG &from)
copy operator
void setGraph(const DAG &graph)
sets a new graph from which we will perform checking
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 checking
bool checkArcTriangleDeletion1Alone(NodeId node1, NodeId node2, NodeId node3) const
checks whether the constraints enable to apply an ArcTriangleDeletion1
bool checkArcTriangleDeletion2Alone(NodeId node1, NodeId node2, NodeId node3) const
checks whether the constraints enable to apply an ArcTriangleDeletion2
bool checkArcDeletionAlone(NodeId x, NodeId y) const
checks whether the constraints enable to remove arc (x,y)
#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
bool hasDirectedPath(const G &g, NodeId from, NodeId to)
Returns true if there is a directed path from from to to.
include the inlined functions if necessary
Definition CSVParser.h:55
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
the base class for structural constraints imposed by DAGs
the base class for structural constraints imposed by DAGs