aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
MarkovBlanket.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
51
52#ifdef GUM_NO_INLINE
54#endif // GU%_NO_INLINE
55
56namespace gum {
57 MarkovBlanket::MarkovBlanket(const DAGmodel& m, NodeId id, int level) : _model_(m), _node_(id) {
58 if (level < 1) GUM_ERROR(InvalidArgument, "Argument level(=" << level << ") must be >0.")
59
60 _mb_ = graph::markovBlanket(m.internalDag(), id, level);
61
62 // Add arcs that exist in the model between MB nodes but are not part of
63 // the canonical MB definition (e.g. arc between a parent and a co-parent).
64 for (const auto node: _mb_.nodes()) {
65 for (const auto child: _model_.children(node)) {
66 if (_mb_.existsNode(child) && !_mb_.existsArc(Arc(node, child))) {
67 _mb_.addArc(node, child);
68 _specialArcs_.insert(Arc(node, child));
69 }
70 }
71 }
72
73 for (auto node: _mb_)
74 _mb_.setName(node, _model_.variable(node).name());
75 }
76
77 MarkovBlanket::MarkovBlanket(const DAGmodel& m, std::string_view name, int level) :
78 MarkovBlanket(m, m.idFromName(name), level) {}
79
81
82 bool MarkovBlanket::hasSameStructure(const DAGmodel& other) const {
83 if (size() != other.size()) return false;
84
85 if (sizeArcs() != other.sizeArcs()) return false;
86
87 for (const auto& nid: nodes()) {
88 if (!other.exists(_model_.variable(nid).name())) { return false; }
89 }
90
91 for (const auto& arc: arcs()) {
92 if (!other.arcs().exists(Arc(other.idFromName(_model_.variable(arc.tail()).name()),
93 other.idFromName(_model_.variable(arc.head()).name()))))
94 return false;
95 }
96
97 return true;
98 }
99
100 std::string MarkovBlanket::toDot() const { return toDot(dot::DotOptions()); }
101
102 std::string MarkovBlanket::toDot(const dot::DotOptions& options) const {
103 const auto style = dot::DotStyle::read();
104 dot::DotWriter writer(true);
105 dot::writePreamble(writer, style, options);
106
107 for (const auto node: _mb_.nodes()) {
108 const auto& name = _model_.variable(node).name();
109 auto attributes = dot::nodeAttributes(style,
110 options.node(node),
111 name,
112 "(" + std::to_string(node) + ") " + name);
113 if (node == _node_) dot::addDefaultAttribute(attributes, "color", "red");
114 writer.node(node, attributes);
115 }
116
117 for (const auto node: _mb_.nodes()) {
118 for (const auto chi: _mb_.children(node)) {
119 auto attributes = dot::arcAttributes(options.arc(node, chi));
120 if (_specialArcs_.exists(Arc(node, chi)))
121 dot::addDefaultAttribute(attributes, "color", "grey");
122 writer.edge(node, chi, attributes);
123 }
124 }
125
126 return writer.str();
127 }
128} // namespace gum
Class building the markovBlanket from a DAGmodel and a node name.
Inline implementation of the class building the essential Graph from a DAGmodel.
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 ...
The base class for all directed edges.
Virtual base class for PGMs using a DAG.
Definition DAGmodel.h:64
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.
bool exists(NodeId node) const final
Return true if this node exists in this graphical model.
const DAG & internalDag() const
Returns a const reference to the internal (unnamed) DAG. O(1), no copy. Use for stable references or ...
NodeId idFromName(std::string_view name) const override
Returns the NodeId of a variable given its name.
const DiscreteVariable & variable(NodeId id) const override
Returns a constant reference over a variable given its node id.
Exception: at least one argument passed to a function is not what was expected.
const DAGmodel & _model_
MarkovBlanket(const DAGmodel &m, NodeId n, int level=1)
std::string toDot() const
bool hasSameStructure(const DAGmodel &other) const
const NodeId _node_
Size sizeArcs() const
wrapping DAG::sizeArcs()
const ArcSet & arcs() const
wrapping DAG::arcs()
const NodeGraphPart & nodes() const
wrapping DAG::nodes()
Size size() const
wrapping DAG::size()
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
void setName(NodeId id, const std::string &name)
sets the name of node id
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:504
const std::string & name() const
returns the name of the variable
The options given to a toDot(const DotOptions&).
Definition dotOptions.h:110
const DotArcOptions * arc(Size tail, Size head) const
the options of the arc from tail to head, or nullptr if none was given for it
const DotNodeOptions * node(Size node) const
the options of a node, or nullptr if none was given for it
Writes a graph in the dot language, statement after statement, in the order of the calls.
Definition dotWriter.h:100
void edge(std::string_view from, std::string_view to, const DotAttributes &attributes={})
an edge from from to to (-> in a directed graph, -- in an undirected one).
std::string str() const
The graph written so far, closed. It can be called at any moment, and the writer can be used after.
void node(std::string_view id, const DotAttributes &attributes={})
a node, whose identifier is id (quoted).
The statements common to the graphs drawn with graphviz : preamble, attributes of nodes and arcs.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
Size NodeId
Type for node ids.
DotAttributes nodeAttributes(const DotStyle &style, const DotNodeOptions *options, std::string_view defaultLabel, std::string_view defaultTooltip)
The attributes of a node : its label and tooltip, and its colors if the options give some.
void writePreamble(DotWriter &writer, const DotStyle &style, const DotOptions &options, std::string_view defaultLabel)
Writes the beginning of a graph, as pyAgrum displays it.
void addDefaultAttribute(DotAttributes &attributes, std::string_view key, std::string_view value)
Adds an attribute at the beginning of the list, unless the list already gives a value for this key : ...
DotAttributes arcAttributes(const DotArcOptions *options)
The attributes of an arc or an edge given by the options : color, penwidth, label,...
DAG markovBlanket(const G &g, NodeId node, int level=1)
Returns the level-level Markov blanket of node in g as a DAG.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
static DotStyle read()
the style of the graphs of the section notebook
Definition dotStyle.cpp:89