aGrUM 3.2.0
a C++ library for (probabilistic) graphical models
gum::MarkovBlanket Class Reference

Class building the markov Blanket from a BN and a nodeName. More...

#include <agrum/BN/algorithms/MarkovBlanket.h>

Collaboration diagram for gum::MarkovBlanket:
[legend]

Public Member Functions

 MarkovBlanket (const DAGmodel &m, NodeId n, int level=1)
 MarkovBlanket (const DAGmodel &m, std::string_view name, int level=1)
 ~MarkovBlanket ()
DAG dag () const
std::string toDot () const
const NodeSet & parents (NodeId id) const
 wrapping DAG::parents(id)
const NodeSet & children (NodeId id) const
 wrapping DAG::children(id)
NodeSet parents (const NodeSet &ids) const
 wrapping DAG::parents(ids)
NodeSet children (const NodeSet &ids) const
 wrapping DAG::children(ids)
NodeSet descendants (NodeId id) const
 wrapping DAG::descendants(id)
NodeSet ancestors (NodeId id) const
 wrapping DAG::ancestors(id)
Size sizeArcs () const
 wrapping DAG::sizeArcs()
const ArcSet & arcs () const
 wrapping DAG::arcs()
Size sizeNodes () const
 wrapping DAG::sizeNodes()
Size size () const
 wrapping DAG::size()
const NodeGraphPart & nodes () const
 wrapping DAG::nodes()
bool hasSameStructure (const DAGmodel &other) const

Private Attributes

const DAGmodel & _model_
DAG _mb_
const NodeId _node_
ArcSet _specialArcs_

Detailed Description

Class building the markov Blanket from a BN and a nodeName.

The main goal of this class is to build and to encapsulate the DiGraph which represents the Markov Blanket.

Definition at line 67 of file MarkovBlanket.h.

Constructor & Destructor Documentation

◆ MarkovBlanket() [1/2]

gum::MarkovBlanket::MarkovBlanket ( const DAGmodel & m,
NodeId n,
int level = 1 )

Definition at line 56 of file MarkovBlanket.cpp.

56 : _model_(m), _node_(id) {
57 if (level < 1) GUM_ERROR(InvalidArgument, "Argument level(=" << level << ") must be >0.")
58
59 _mb_ = graph::markovBlanket(m.internalDag(), id, level);
60
61 // Add arcs that exist in the model between MB nodes but are not part of
62 // the canonical MB definition (e.g. arc between a parent and a co-parent).
63 for (const auto node: _mb_.nodes()) {
64 for (const auto child: _model_.children(node)) {
65 if (_mb_.existsNode(child) && !_mb_.existsArc(Arc(node, child))) {
66 _mb_.addArc(node, child);
67 _specialArcs_.insert(Arc(node, child));
68 }
69 }
70 }
71
72 for (auto node: _mb_)
73 _mb_.setName(node, _model_.variable(node).name());
74 }
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
const DiscreteVariable & variable(NodeId id) const override
Returns a constant reference over a variable given its node id.
const DAGmodel & _model_
const NodeId _node_
const NodeGraphPart & nodes() const
wrapping DAG::nodes()
void setName(NodeId id, const std::string &name)
sets the name of node id
const std::string & name() const
returns the name of the variable
#define GUM_ERROR(type, msg)
Definition exceptions.h:76

References _mb_, _model_, _node_, GUM_ERROR, gum::DAGmodel::internalDag(), gum::graph::markovBlanket(), and gum::NodeGraphPart::nodes().

Referenced by MarkovBlanket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ MarkovBlanket() [2/2]

gum::MarkovBlanket::MarkovBlanket ( const DAGmodel & m,
std::string_view name,
int level = 1 )

Definition at line 76 of file MarkovBlanket.cpp.

76 :
77 MarkovBlanket(m, m.idFromName(name), level) {}
MarkovBlanket(const DAGmodel &m, NodeId n, int level=1)

References MarkovBlanket().

Here is the call graph for this function:

◆ ~MarkovBlanket()

gum::MarkovBlanket::~MarkovBlanket ( )
default

References ancestors(), arcs(), children(), dag(), descendants(), hasSameStructure(), nodes(), parents(), size(), sizeArcs(), sizeNodes(), and toDot().

Here is the call graph for this function:

Member Function Documentation

◆ ancestors()

INLINE NodeSet gum::MarkovBlanket::ancestors ( NodeId id) const

wrapping DAG::ancestors(id)

Definition at line 68 of file MarkovBlanket_inl.h.

68{ return _mb_.ancestors(id); }
NodeSet ancestors(NodeId id) const
returns the set of all ancestors of id (nodes from which id is reachable)

References _mb_, and gum::DiGraph::ancestors().

Referenced by ~MarkovBlanket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ arcs()

INLINE const ArcSet & gum::MarkovBlanket::arcs ( ) const

wrapping DAG::arcs()

Definition at line 72 of file MarkovBlanket_inl.h.

72{ return _mb_.arcs(); }
const ArcSet & arcs() const
returns the set of arcs stored within the ArcGraphPart

References _mb_, and gum::ArcGraphPart::arcs().

Referenced by ~MarkovBlanket(), and hasSameStructure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ children() [1/2]

INLINE NodeSet gum::MarkovBlanket::children ( const NodeSet & ids) const

wrapping DAG::children(ids)

Definition at line 64 of file MarkovBlanket_inl.h.

64{ return _mb_.children(ids); }
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 ...

References _mb_, and gum::ArcGraphPart::children().

Here is the call graph for this function:

◆ children() [2/2]

INLINE const NodeSet & gum::MarkovBlanket::children ( NodeId id) const

wrapping DAG::children(id)

Definition at line 60 of file MarkovBlanket_inl.h.

60{ return _mb_.children(id); }

References _mb_, and gum::ArcGraphPart::children().

Referenced by ~MarkovBlanket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dag()

INLINE DAG gum::MarkovBlanket::dag ( ) const
Returns
a copy of the graph

Definition at line 56 of file MarkovBlanket_inl.h.

56{ return _mb_; }

References _mb_.

Referenced by ~MarkovBlanket().

Here is the caller graph for this function:

◆ descendants()

INLINE NodeSet gum::MarkovBlanket::descendants ( NodeId id) const

wrapping DAG::descendants(id)

Definition at line 66 of file MarkovBlanket_inl.h.

66{ return _mb_.descendants(id); }
NodeSet descendants(NodeId id) const
returns the set of all descendants of id (nodes reachable from id)

References _mb_, and gum::DiGraph::descendants().

Referenced by ~MarkovBlanket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hasSameStructure()

bool gum::MarkovBlanket::hasSameStructure ( const DAGmodel & other) const
Returns
true if all the named node are the same and all the named arcs are the same

Definition at line 81 of file MarkovBlanket.cpp.

81 {
82 if (size() != other.size()) return false;
83
84 if (sizeArcs() != other.sizeArcs()) return false;
85
86 for (const auto& nid: nodes()) {
87 if (!other.exists(_model_.variable(nid).name())) { return false; }
88 }
89
90 for (const auto& arc: arcs()) {
91 if (!other.arcs().exists(Arc(other.idFromName(_model_.variable(arc.tail()).name()),
92 other.idFromName(_model_.variable(arc.head()).name()))))
93 return false;
94 }
95
96 return true;
97 }
Size sizeArcs() const
wrapping DAG::sizeArcs()
const ArcSet & arcs() const
wrapping DAG::arcs()
Size size() const
wrapping DAG::size()

References _model_, gum::DAGmodel::arcs(), arcs(), gum::DAGmodel::exists(), gum::Set< Key >::exists(), gum::DiscreteGraphicalModel::idFromName(), gum::Variable::name(), nodes(), gum::DAGmodel::size(), size(), gum::DAGmodel::sizeArcs(), sizeArcs(), and gum::DiscreteGraphicalModel::variable().

Referenced by ~MarkovBlanket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nodes()

INLINE const NodeGraphPart & gum::MarkovBlanket::nodes ( ) const

wrapping DAG::nodes()

Definition at line 78 of file MarkovBlanket_inl.h.

78{ return _mb_.nodes(); }
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart

References _mb_, and gum::NodeGraphPart::nodes().

Referenced by ~MarkovBlanket(), and hasSameStructure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parents() [1/2]

INLINE NodeSet gum::MarkovBlanket::parents ( const NodeSet & ids) const

wrapping DAG::parents(ids)

Definition at line 62 of file MarkovBlanket_inl.h.

62{ return _mb_.parents(ids); }
const NodeSet & parents(NodeId id) const
returns the set of nodes with arc ingoing to a given node

References _mb_, and gum::ArcGraphPart::parents().

Here is the call graph for this function:

◆ parents() [2/2]

INLINE const NodeSet & gum::MarkovBlanket::parents ( NodeId id) const

wrapping DAG::parents(id)

Definition at line 58 of file MarkovBlanket_inl.h.

58{ return _mb_.parents(id); }

References _mb_, and gum::ArcGraphPart::parents().

Referenced by ~MarkovBlanket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ size()

INLINE Size gum::MarkovBlanket::size ( ) const

wrapping DAG::size()

Definition at line 76 of file MarkovBlanket_inl.h.

76{ return _mb_.size(); }
Size size() const
alias for sizeNodes

References _mb_, and gum::NodeGraphPart::size().

Referenced by ~MarkovBlanket(), and hasSameStructure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sizeArcs()

INLINE Size gum::MarkovBlanket::sizeArcs ( ) const

wrapping DAG::sizeArcs()

Definition at line 70 of file MarkovBlanket_inl.h.

70{ return _mb_.sizeArcs(); }
Size sizeArcs() const
indicates the number of arcs stored within the ArcGraphPart

References _mb_, and gum::ArcGraphPart::sizeArcs().

Referenced by ~MarkovBlanket(), and hasSameStructure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sizeNodes()

INLINE Size gum::MarkovBlanket::sizeNodes ( ) const

wrapping DAG::sizeNodes()

Definition at line 74 of file MarkovBlanket_inl.h.

74{ return _mb_.sizeNodes(); }
Size sizeNodes() const
returns the number of nodes in the NodeGraphPart

References _mb_, and gum::NodeGraphPart::sizeNodes().

Referenced by ~MarkovBlanket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toDot()

std::string gum::MarkovBlanket::toDot ( ) const

Definition at line 99 of file MarkovBlanket.cpp.

99 {
100 std::string output;
101 std::string nodeStream;
102 std::string arcStream;
103 List< NodeId > treatedNodes;
104 output = "digraph \"no_name\" {\n";
105 nodeStream = "node [shape = ellipse];\n";
106 std::string tab = " ";
107
108 for (const auto node: _mb_.nodes()) {
109 nodeStream += std::format("{}{}[label=\"{}\"", tab, node, _model_.variable(node).name());
110 if (node == _node_) { nodeStream += ", color=red"; }
111 nodeStream += "];\n";
112
113 for (const auto chi: _mb_.children(node)) {
114 arcStream += std::format("{}{} -> {}", tab, node, chi);
115 if (_specialArcs_.exists(Arc(node, chi))) { arcStream += " [color=grey]"; }
116 arcStream += ";\n";
117 }
118 }
119
120 output += nodeStream + '\n' + arcStream + "\n}\n";
121
122 return output;
123 }
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:504

References _mb_, _model_, _node_, _specialArcs_, gum::ArcGraphPart::children(), gum::Set< Key >::exists(), gum::Variable::name(), gum::NodeGraphPart::nodes(), and gum::DiscreteGraphicalModel::variable().

Referenced by ~MarkovBlanket().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ _mb_

DAG gum::MarkovBlanket::_mb_
private

◆ _model_

const DAGmodel& gum::MarkovBlanket::_model_
private

Definition at line 121 of file MarkovBlanket.h.

Referenced by MarkovBlanket(), hasSameStructure(), and toDot().

◆ _node_

const NodeId gum::MarkovBlanket::_node_
private

Definition at line 123 of file MarkovBlanket.h.

Referenced by MarkovBlanket(), and toDot().

◆ _specialArcs_

ArcSet gum::MarkovBlanket::_specialArcs_
private

Definition at line 124 of file MarkovBlanket.h.

Referenced by toDot().


The documentation for this class was generated from the following files: