aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
DAGmodel_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
43
51
53
55
56namespace gum {
57 INLINE
58 const DAG& DAGmodel::internalDag() const { return dag_; }
59
60 INLINE
62 DAG g = dag_;
63 _nameNodes_(g);
64 return g;
65 }
66
67 INLINE
68 Size DAGmodel::size() const { return dag_.size(); }
69
70 INLINE
71 Size DAGmodel::sizeArcs() const { return dag_.sizeArcs(); }
72
73 INLINE const ArcSet& DAGmodel::arcs() const { return dag_.arcs(); }
74
75 INLINE bool DAGmodel::existsArc(const NodeId tail, const NodeId head) const {
76 return dag_.existsArc(tail, head);
77 }
78
79 INLINE bool DAGmodel::existsArc(std::string_view nametail, std::string_view namehead) const {
80 return existsArc(idFromName(nametail), idFromName(namehead));
81 }
82
83 INLINE void DAGmodel::_checkNodeExists_(NodeId id) const {
84 if (!dag_.exists(id)) GUM_ERROR(NotFound, "No node with id " << id << " in the model")
85 }
86
87 // the arc part of the DAG stores no entry for a node without parents (or children): an empty
88 // result is the only case where the node may not exist, hence the only one to check
89 INLINE const NodeSet& DAGmodel::parents(const NodeId id) const {
90 const NodeSet& res = dag_.parents(id);
91 if (res.empty()) _checkNodeExists_(id);
92 return res;
93 }
94
95 // idFromName already checks that the node exists
96 INLINE const NodeSet& DAGmodel::parents(std::string_view name) const {
97 return dag_.parents(idFromName(name));
98 }
99
100 INLINE NodeSet DAGmodel::family(const NodeId id) const {
102 return dag_.family(id);
104
105 INLINE NodeSet DAGmodel::family(std::string_view name) const {
106 return dag_.family(idFromName(name));
107 }
109 INLINE const NodeSet& DAGmodel::children(const NodeId id) const {
110 const NodeSet& res = dag_.children(id);
111 if (res.empty()) _checkNodeExists_(id);
112 return res;
113 }
114
115 INLINE const NodeSet& DAGmodel::children(std::string_view name) const {
116 return dag_.children(idFromName(name));
117 }
118
119 INLINE NodeSet DAGmodel::children(const NodeSet& ids) const {
120 for (const auto id: ids)
123 }
124
125 // nodeset already checks that the nodes exist
126 INLINE NodeSet DAGmodel::children(const std::vector< std::string >& names) const {
127 return dag_.children(nodeset(names));
128 }
129
130 INLINE NodeSet DAGmodel::parents(const NodeSet& ids) const {
131 for (const auto id: ids)
133 return dag_.parents(ids);
134 }
135
136 // nodeset already checks that the nodes exist
137 INLINE NodeSet DAGmodel::parents(const std::vector< std::string >& names) const {
138 return dag_.parents(nodeset(names));
139 }
140
141 INLINE bool DAGmodel::exists(NodeId node) const { return dag_.exists(node); }
143 INLINE bool DAGmodel::exists(std::string_view name) const {
144 return variableNodeMap().exists(name);
145 }
147 INLINE const NodeGraphPart& DAGmodel::nodes() const {
148 return static_cast< const NodeGraphPart& >(dag_);
149 }
150
152
157 INLINE NodeSet DAGmodel::descendants(const NodeId id) const {
158 auto res = dag_.descendants(id);
159 if (res.empty()) _checkNodeExists_(id);
160 return res;
161 }
162
163 INLINE NodeSet DAGmodel::descendants(std::string_view name) const {
164 return dag_.descendants(idFromName(name));
167 INLINE NodeSet DAGmodel::ancestors(const NodeId id) const {
168 auto res = dag_.ancestors(id);
169 if (res.empty()) _checkNodeExists_(id);
170 return res;
172
173 INLINE NodeSet DAGmodel::ancestors(std::string_view name) const {
174 return dag_.ancestors(idFromName(name));
175 }
176
177 INLINE UndiGraph
178 DAGmodel::moralizedAncestralGraph(const std::vector< std::string >& nodenames) const {
179 return moralizedAncestralGraph(nodeset(nodenames));
180 }
184 _nameNodes_(g);
185 return g;
186 }
187
188 INLINE bool DAGmodel::isIndependent(NodeId X, NodeId Y, const NodeSet& Z) const {
189 return dag_.dSeparation(X, Y, Z);
190 }
191
192 INLINE bool DAGmodel::isIndependent(const NodeSet& X, const NodeSet& Y, const NodeSet& Z) const {
193 return dag_.dSeparation(X, Y, Z);
194 }
195
196 INLINE NodeSet DAGmodel::minimalCondSet(NodeId target, const NodeSet& soids) const {
197 return dag_.minimalCondSet(target, soids);
198 }
199
200 INLINE NodeSet DAGmodel::minimalCondSet(const NodeSet& targets, const NodeSet& soids) const {
201 return dag_.minimalCondSet(targets, soids);
202 }
203
204 INLINE NodeSet DAGmodel::minimalCondSet(std::string_view target,
205 const std::vector< std::string >& soids) const {
206 return dag_.minimalCondSet(idFromName(target), nodeset(soids));
207 }
208
209 INLINE NodeSet DAGmodel::minimalCondSet(const std::vector< std::string >& targets,
210 const std::vector< std::string >& soids) const {
211 return dag_.minimalCondSet(nodeset(targets), nodeset(soids));
212 }
213
214 INLINE bool DAGmodel::isIndependent(std::string_view Xname,
215 std::string_view Yname,
216 const std::vector< std::string >& Znames) const {
217 return isIndependent(idFromName(Xname), idFromName(Yname), nodeset(Znames));
218 }
219
220 INLINE bool DAGmodel::isIndependent(const std::vector< std::string >& Xnames,
221 const std::vector< std::string >& Ynames,
222 const std::vector< std::string >& Znames) const {
223 return isIndependent(nodeset(Xnames), nodeset(Ynames), nodeset(Znames));
224 }
225
226} /* namespace gum */
Class representing probabilistic DAG model.
bool existsArc(const Arc &arc) const
indicates whether a given arc exists
Size sizeArcs() const
indicates the number of arcs stored within the ArcGraphPart
const NodeSet & parents(NodeId id) const
returns the set of nodes with arc ingoing to a given node
NodeSet children(const NodeSet &ids) const
returns the set of nodes which consists in the node and its parents returns the set of children of a ...
const ArcSet & arcs() const
returns the set of arcs stored within the ArcGraphPart
NodeId idFromName(std::string_view name) const override
Base class for dag.
Definition DAG.h:121
bool dSeparation(NodeId X, NodeId Y, const NodeSet &Z) const
check if node X and node Y are independent given nodes Z (in the sense of d-separation)
Definition DAG.cpp:80
NodeSet minimalCondSet(NodeId target, const NodeSet &soids) const
Definition DAG.cpp:88
UndiGraph moralizedAncestralGraph(const NodeSet &nodes) const
build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes
Definition DAG.cpp:76
NodeProperty< NodeId > connectedComponents() const
Returns the weakly connected components of the underlying DAG. Each node maps to the id of its compon...
NodeSet family(const NodeId id) const final
returns the parents of a node and the node
DAG dag_
The DAG of this Directed Graphical Model.
Definition DAGmodel.h:291
const ArcSet & arcs() const
return true if the arc tail->head exists in the DAGmodel
Size size() const final
Returns the number of variables in this Directed Graphical Model.
Size sizeArcs() const
Returns the number of arcs in this Directed Graphical Model.
Sequence< NodeId > topologicalOrder() const
The topological order stays the same as long as no variable or arcs are added or erased src the topol...
NodeSet minimalCondSet(NodeId target, const NodeSet &soids) const
bool existsArc(const NodeId tail, const NodeId head) const
return true if the arc tail->head exists in the DAGmodel
DAG dag() const
Returns a named copy of the internal DAG: each node id is assigned the name of the corresponding vari...
void _checkNodeExists_(NodeId id) const
UndiGraph moralizedAncestralGraph(const NodeSet &nodes) const
build a UndiGraph by moralizing the Ancestral Graph of a set of Nodes
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
bool exists(NodeId node) const final
Return true if this node exists in this graphical model.
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
bool isIndependent(NodeId X, NodeId Y, const NodeSet &Z) const final
check if node X and node Y are independent given nodes Z
NodeSet descendants(const NodeId id) const
returns the set of nodes with directed path outgoing from a given node
const NodeGraphPart & nodes() const final
Returns a named copy of the internal DAG: each node id is assigned the name of the corresponding vari...
NodeSet ancestors(const NodeId id) const
returns the set of nodes with directed path ingoing to a given node
const DAG & internalDag() const
Returns a const reference to the internal (unnamed) DAG. O(1), no copy. Use for stable references or ...
NodeSet ancestors(NodeId id) const
returns the set of all ancestors of id (nodes from which id is reachable)
NodeSet family(NodeId id) const
returns { id } ∪ parents(id)
NodeProperty< NodeId > connectedComponents() const
returns a property {node:id of weakly connected component}
Sequence< NodeId > topologicalOrder() const
Build and return a topological order.
NodeSet descendants(NodeId id) const
returns the set of all descendants of id (nodes reachable from id)
NodeId idFromName(std::string_view name) const override
Returns the NodeId of a variable given its name.
const VariableNodeMap & variableNodeMap() const override
Returns a constant reference to the VariableNodeMap of this model.
void _nameNodes_(NodeGraphPart &g) const
Names every node of g using variable(id).name() for each node id in g.
std::vector< NodeId > ids(const std::vector< std::string > &names) const
transform a vector of names into a vector of nodeId
std::vector< std::string > names(const std::vector< NodeId > &ids) const
transform a vector of NodeId in a vector of names
NodeSet nodeset(const std::vector< std::string > &names) const
transform a vector of names into a NodeSet
Class for node sets in graph.
Size size() const
alias for sizeNodes
bool exists(const NodeId id) const
alias for existsNode
Exception : the element we looked for cannot be found.
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition set_tpl.h:613
Base class for undirected graphs.
Definition undiGraph.h:134
bool exists(NodeId id) const
Return true if id matches a node.
#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.
Set< Arc > ArcSet
Some typdefs and define for shortcuts ...
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
Useful macros for maths.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46