aGrUM  0.20.3
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:

Public Member Functions

 MarkovBlanket (const DAGmodel &m, NodeId n, int level=1)
 
 MarkovBlanket (const DAGmodel &m, const std::string &name, int level=1)
 
 ~MarkovBlanket ()
 
DAG dag ()
 
std::string toDot () const
 
const NodeSetparents (NodeId id) const
 wrapping DAG::parents(id) More...
 
const NodeSetchildren (NodeId id) const
 wrapping DAG::children(id) More...
 
NodeSet parents (const NodeSet &ids) const
 wrapping DAG::parents(ids) More...
 
NodeSet children (const NodeSet &ids) const
 wrapping DAG::children(ids) More...
 
Size sizeArcs () const
 wrapping DAG::sizeArcs() More...
 
const ArcSetarcs () const
 wrapping DAG::arcs() More...
 
Size sizeNodes () const
 wrapping DAG::sizeNodes() More...
 
Size size () const
 wrapping DAG::size() More...
 
const NodeGraphPartnodes () const
 wrapping DAG::nodes() More...
 
bool hasSameStructure (const DAGmodel &other)
 

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 48 of file MarkovBlanket.h.

Constructor & Destructor Documentation

◆ MarkovBlanket() [1/2]

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

Definition at line 36 of file MarkovBlanket.cpp.

References gum::Set< Key, Alloc >::emplace().

36  : _model_(m), _node_(id) {
37  if (level < 1) GUM_ERROR(InvalidArgument, "Argument level(=" << level << ") must be >0.")
38 
39  NodeSet done;
41  done.insert(_node_);
42 
43  while (level > 1) {
44  level--;
45  auto todo = _mb_.nodes().asNodeSet() - done;
46  bool anythingnew = false;
47  for (NodeId n: todo) {
48  done.insert(n);
49  if (_buildMarkovBlanket_(n)) anythingnew = true;
50  }
51  if (!anythingnew) break;
52  }
53 
54  // we add now some arcs that are between the nodes in _mb_ but are not part of
55  // the last ones.
56  // For instance, an arc between a parent and a parent of children
57  for (const auto node: _mb_.nodes()) {
58  for (const auto child: _model_.children(node)) {
59  if (_mb_.existsNode(child) && !_mb_.existsArc(Arc(node, child))) {
60  _mb_.addArc(node, child);
61  _specialArcs_.insert(Arc(node, child));
62  }
63  }
64  }
65  }
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
Definition: DAGmodel_inl.h:65
const DAGmodel & _model_
Definition: MarkovBlanket.h:98
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
void addArc(NodeId tail, NodeId head) final
insert a new arc into the directed graph
Definition: DAG_inl.h:42
const NodeId _node_
NodeSet asNodeSet() const
returns a copy of the set of nodes represented by the NodeGraphPart
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
bool existsNode(const NodeId id) const
returns true iff the NodeGraphPart contains the given nodeId
bool _buildMarkovBlanket_(const NodeId id)
bool existsArc(const Arc &arc) const
indicates whether a given arc exists
Size NodeId
Type for node ids.
Definition: graphElements.h:97
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:606
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ MarkovBlanket() [2/2]

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

Definition at line 67 of file MarkovBlanket.cpp.

References gum::Set< Key, Alloc >::emplace().

67  :
68  MarkovBlanket(m, m.idFromName(name), level) {}
MarkovBlanket(const DAGmodel &m, NodeId n, int level=1)
+ Here is the call graph for this function:

◆ ~MarkovBlanket()

gum::MarkovBlanket::~MarkovBlanket ( )

Definition at line 70 of file MarkovBlanket.cpp.

70 {}

Member Function Documentation

◆ _buildMarkovBlanket_()

bool gum::MarkovBlanket::_buildMarkovBlanket_ ( const NodeId  id)
private

Definition at line 72 of file MarkovBlanket.cpp.

References gum::Set< Key, Alloc >::emplace().

72  {
73  bool change = false;
74  if (!_model_.nodes().exists(node))
75  GUM_ERROR(InvalidArgument, "Node " << node << " does not exist.")
76 
77  if (!_mb_.nodes().exists(node)) {
78  _mb_.addNodeWithId(node);
79  change = true;
80  }
81 
82  for (const auto& parent: _model_.parents(node)) {
83  if (!_mb_.nodes().exists(parent)) {
84  _mb_.addNodeWithId(parent);
85  change = true;
86  }
87  _mb_.addArc(parent, node);
88  }
89 
90  for (const auto& child: _model_.children(node)) {
91  if (!_mb_.nodes().exists(child)) {
92  _mb_.addNodeWithId(child);
93  change = true;
94  }
95  _mb_.addArc(node, child);
96  for (const auto& opar: _model_.parents(child)) {
97  if (opar != node) {
98  if (!_mb_.nodes().exists(opar)) {
99  _mb_.addNodeWithId(opar);
100  change = true;
101  }
102  _mb_.addArc(opar, child);
103  }
104  }
105  }
106 
107  return change;
108  }
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
Definition: DAGmodel_inl.h:65
const DAGmodel & _model_
Definition: MarkovBlanket.h:98
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:84
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:53
void addArc(NodeId tail, NodeId head) final
insert a new arc into the directed graph
Definition: DAG_inl.h:42
bool exists(const NodeId id) const
alias for existsNode
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ arcs()

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

wrapping DAG::arcs()

Definition at line 46 of file MarkovBlanket_inl.h.

References gum::Set< Key, Alloc >::emplace().

46 { return _mb_.arcs(); }
const ArcSet & arcs() const
returns the set of arcs stored within the ArcGraphPart
+ Here is the call graph for this function:

◆ children() [1/2]

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

wrapping DAG::children(id)

Definition at line 38 of file MarkovBlanket_inl.h.

References gum::Set< Key, Alloc >::emplace().

38 { return _mb_.children(id); }
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
+ Here is the call graph for this function:

◆ children() [2/2]

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

wrapping DAG::children(ids)

Definition at line 42 of file MarkovBlanket_inl.h.

References gum::Set< Key, Alloc >::emplace().

42 { return _mb_.children(ids); }
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
+ Here is the call graph for this function:

◆ dag()

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

Definition at line 34 of file MarkovBlanket_inl.h.

References gum::Set< Key, Alloc >::emplace().

34 { return _mb_; }
+ Here is the call graph for this function:

◆ hasSameStructure()

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

Definition at line 110 of file MarkovBlanket.cpp.

References gum::Set< Key, Alloc >::emplace().

110  {
111  if (size() != other.size()) return false;
112 
113  if (sizeArcs() != other.sizeArcs()) return false;
114 
115  for (const auto& nid: nodes()) {
116  try {
117  other.idFromName(_model_.variable(nid).name());
118  } catch (NotFound) { return false; }
119  }
120 
121  for (const auto& arc: arcs()) {
122  if (!other.arcs().exists(Arc(other.idFromName(_model_.variable(arc.tail()).name()),
123  other.idFromName(_model_.variable(arc.head()).name()))))
124  return false;
125  }
126 
127  return true;
128  }
const DAGmodel & _model_
Definition: MarkovBlanket.h:98
Size sizeArcs() const
wrapping DAG::sizeArcs()
Size size() const
wrapping DAG::size()
const ArcSet & arcs() const
wrapping DAG::arcs()
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.
const std::string & name() const
returns the name of the variable
const NodeGraphPart & nodes() const
wrapping DAG::nodes()
+ Here is the call graph for this function:

◆ nodes()

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

wrapping DAG::nodes()

Definition at line 52 of file MarkovBlanket_inl.h.

References gum::Set< Key, Alloc >::emplace().

52 { return _mb_.nodes(); }
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
+ Here is the call graph for this function:

◆ parents() [1/2]

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

wrapping DAG::parents(id)

Definition at line 36 of file MarkovBlanket_inl.h.

References gum::Set< Key, Alloc >::emplace().

36 { return _mb_.parents(id); }
const NodeSet & parents(NodeId id) const
returns the set of nodes with arc ingoing to a given node
+ Here is the call graph for this function:

◆ parents() [2/2]

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

wrapping DAG::parents(ids)

Definition at line 40 of file MarkovBlanket_inl.h.

References gum::Set< Key, Alloc >::emplace().

40 { return _mb_.parents(ids); }
const NodeSet & parents(NodeId id) const
returns the set of nodes with arc ingoing to a given node
+ Here is the call graph for this function:

◆ size()

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

wrapping DAG::size()

Definition at line 50 of file MarkovBlanket_inl.h.

References gum::Set< Key, Alloc >::emplace().

50 { return _mb_.size(); }
Size size() const
alias for sizeNodes
+ Here is the call graph for this function:

◆ sizeArcs()

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

wrapping DAG::sizeArcs()

Definition at line 44 of file MarkovBlanket_inl.h.

References gum::Set< Key, Alloc >::emplace().

44 { return _mb_.sizeArcs(); }
Size sizeArcs() const
indicates the number of arcs stored within the ArcGraphPart
+ Here is the call graph for this function:

◆ sizeNodes()

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

wrapping DAG::sizeNodes()

Definition at line 48 of file MarkovBlanket_inl.h.

References gum::Set< Key, Alloc >::emplace().

48 { return _mb_.sizeNodes(); }
Size sizeNodes() const
returns the number of nodes in the NodeGraphPart
+ Here is the call graph for this function:

◆ toDot()

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

Definition at line 130 of file MarkovBlanket.cpp.

References gum::Set< Key, Alloc >::emplace().

130  {
131  std::stringstream output;
132  std::stringstream nodeStream;
133  std::stringstream arcStream;
134  List< NodeId > treatedNodes;
135  output << "digraph \""
136  << "no_name\" {" << std::endl;
137  nodeStream << "node [shape = ellipse];" << std::endl;
138  std::string tab = " ";
139 
140  for (const auto node: _mb_.nodes()) {
141  nodeStream << tab << node << "[label=\"" << _model_.variable(node).name() << "\"";
142  if (node == _node_) { nodeStream << ", color=red"; }
143  nodeStream << "];" << std::endl;
144 
145  for (const auto chi: _mb_.children(node)) {
146  arcStream << tab << node << " -> " << chi;
147  if (_specialArcs_.exists(Arc(node, chi))) { arcStream << " [color=grey]"; }
148  arcStream << ";" << std::endl;
149  }
150  }
151 
152  output << nodeStream.str() << std::endl << arcStream.str() << std::endl << "}" << std::endl;
153 
154  return output.str();
155  }
const DAGmodel & _model_
Definition: MarkovBlanket.h:98
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
const NodeId _node_
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:600
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variable given it&#39;s node id.
const std::string & name() const
returns the name of the variable
+ Here is the call graph for this function:

Member Data Documentation

◆ _mb_

DAG gum::MarkovBlanket::_mb_
private

Definition at line 99 of file MarkovBlanket.h.

◆ _model_

const DAGmodel& gum::MarkovBlanket::_model_
private

Definition at line 98 of file MarkovBlanket.h.

◆ _node_

const NodeId gum::MarkovBlanket::_node_
private

Definition at line 100 of file MarkovBlanket.h.

◆ _specialArcs_

ArcSet gum::MarkovBlanket::_specialArcs_
private

Definition at line 101 of file MarkovBlanket.h.


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