aGrUM  0.16.0
gum::MarkovBlanket Class Reference

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

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

+ Collaboration diagram for gum::MarkovBlanket:

Public Member Functions

 MarkovBlanket (const DAGmodel &m, NodeId n)
 
 MarkovBlanket (const DAGmodel &m, const std::string &name)
 
 ~MarkovBlanket ()
 
DiGraph mb ()
 
std::string toDot () const
 
const NodeSetparents (const NodeId id) const
 wrapping DiGraph::parents(id) More...
 
const NodeSetchildren (const NodeId id) const
 wrapping DiGraph::parents(id) More...
 
Size sizeArcs () const
 wrapping DiGraph::sizeArcs() More...
 
const ArcSetarcs () const
 wrapping DiGraph::arcs() More...
 
Size sizeNodes () const
 wrapping DiGraph::sizeNodes() More...
 
Size size () const
 wrapping DiGraph::size() More...
 
const NodeGraphPartnodes () const
 wrapping DiGraph::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 49 of file MarkovBlanket.h.

Constructor & Destructor Documentation

◆ MarkovBlanket() [1/2]

gum::MarkovBlanket::MarkovBlanket ( const DAGmodel m,
NodeId  n 
)

Definition at line 37 of file MarkovBlanket.cpp.

References __buildMarkovBlanket().

37  :
38  __model(m), __node(id) {
40  }
const NodeId __node
Definition: MarkovBlanket.h:95
const DAGmodel & __model
Definition: MarkovBlanket.h:93
+ Here is the call graph for this function:

◆ MarkovBlanket() [2/2]

gum::MarkovBlanket::MarkovBlanket ( const DAGmodel m,
const std::string &  name 
)

Definition at line 42 of file MarkovBlanket.cpp.

42  :
43  MarkovBlanket(m, m.idFromName(name)) {}
MarkovBlanket(const DAGmodel &m, NodeId n)

◆ ~MarkovBlanket()

gum::MarkovBlanket::~MarkovBlanket ( )

Definition at line 45 of file MarkovBlanket.cpp.

45 {}

Member Function Documentation

◆ __buildMarkovBlanket()

void gum::MarkovBlanket::__buildMarkovBlanket ( )
private

Definition at line 47 of file MarkovBlanket.cpp.

References __mb, __model, __node, __specialArcs, gum::DiGraph::addArc(), gum::NodeGraphPart::addNodeWithId(), gum::DAGmodel::children(), gum::NodeGraphPart::exists(), gum::ArcGraphPart::existsArc(), gum::NodeGraphPart::existsNode(), GUM_ERROR, gum::Set< Key, Alloc >::insert(), gum::DAGmodel::nodes(), gum::NodeGraphPart::nodes(), and gum::DAGmodel::parents().

Referenced by MarkovBlanket().

47  {
48  if (!__model.nodes().exists(__node))
49  GUM_ERROR(InvalidArgument, "Node " << __node << " does not exist.");
50 
52  for (const auto& parent : __model.parents(__node)) {
53  __mb.addNodeWithId(parent);
54  __mb.addArc(parent, __node);
55  }
56 
57  for (const auto& child : __model.children(__node)) {
58  if (!__mb.nodes().exists(child)) __mb.addNodeWithId(child);
59  __mb.addArc(__node, child);
60  for (const auto& opar : __model.parents(child)) {
61  if (opar != __node) {
62  if (!__mb.nodes().exists(opar)) __mb.addNodeWithId(opar);
63  __mb.addArc(opar, child);
64  }
65  }
66  }
67 
68  // we add now some arcs that are between the nodes in __mb but are not part of
69  // the last ones.
70  // For instance, an arc between a parent and a parent of children
71  for (const auto node : __mb.nodes()) {
72  for (const auto child : __model.children(node)) {
73  if (__mb.existsNode(child) && !__mb.existsArc(Arc(node, child))) {
74  __mb.addArc(node, child);
75  __specialArcs.insert(Arc(node, child));
76  }
77  }
78  }
79  }
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
virtual void addArc(const NodeId tail, const NodeId head)
insert a new arc into the directed graph
Definition: diGraph_inl.h:35
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
Definition: DAGmodel_inl.h:111
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:106
bool exists(const NodeId id) const
alias for existsNode
const NodeId __node
Definition: MarkovBlanket.h:95
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:115
const DAGmodel & __model
Definition: MarkovBlanket.h:93
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 existsArc(const Arc &arc) const
indicates whether a given arc exists
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ 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 DiGraph::arcs()

Definition at line 47 of file MarkovBlanket_inl.h.

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

Referenced by hasSameStructure().

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

◆ children()

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

wrapping DiGraph::parents(id)

Definition at line 41 of file MarkovBlanket_inl.h.

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

41  {
42  return __mb.children(id);
43  }
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
+ 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 81 of file MarkovBlanket.cpp.

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

81  {
82  if (size() != other.size()) return false;
83 
84  if (sizeArcs() != other.sizeArcs()) return false;
85 
86  for (const auto& nid : nodes()) {
87  try {
88  other.idFromName(__model.variable(nid).name());
89  } catch (NotFound) { return false; }
90  }
91 
92  for (const auto& arc : arcs()) {
93  if (!other.arcs().exists(
94  Arc(other.idFromName(__model.variable(arc.tail()).name()),
95  other.idFromName(__model.variable(arc.head()).name()))))
96  return false;
97  }
98 
99  return true;
100  }
Size sizeArcs() const
wrapping DiGraph::sizeArcs()
Size size() const
wrapping DiGraph::size()
const ArcSet & arcs() const
wrapping DiGraph::arcs()
const DAGmodel & __model
Definition: MarkovBlanket.h:93
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variabe given it&#39;s node id.
const std::string & name() const
returns the name of the variable
const NodeGraphPart & nodes() const
wrapping DiGraph::nodes()
+ Here is the call graph for this function:

◆ mb()

INLINE DiGraph gum::MarkovBlanket::mb ( )
Returns
a copy of the mixed graph

Definition at line 35 of file MarkovBlanket_inl.h.

References __mb.

35 { return __mb; }

◆ nodes()

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

wrapping DiGraph::nodes()

Definition at line 53 of file MarkovBlanket_inl.h.

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

Referenced by hasSameStructure().

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

◆ parents()

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

wrapping DiGraph::parents(id)

Definition at line 37 of file MarkovBlanket_inl.h.

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

37  {
38  return __mb.parents(id);
39  }
const NodeSet & parents(const 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 DiGraph::size()

Definition at line 51 of file MarkovBlanket_inl.h.

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

Referenced by hasSameStructure().

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

◆ sizeArcs()

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

wrapping DiGraph::sizeArcs()

Definition at line 45 of file MarkovBlanket_inl.h.

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

Referenced by hasSameStructure().

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

◆ sizeNodes()

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

wrapping DiGraph::sizeNodes()

Definition at line 49 of file MarkovBlanket_inl.h.

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

49 { 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 102 of file MarkovBlanket.cpp.

References __mb, __model, __node, __specialArcs, gum::ArcGraphPart::children(), gum::Set< Key, Alloc >::exists(), gum::Variable::name(), gum::NodeGraphPart::nodes(), and gum::DAGmodel::variable().

102  {
103  std::stringstream output;
104  std::stringstream nodeStream;
105  std::stringstream arcStream;
106  List< NodeId > treatedNodes;
107  output << "digraph \""
108  << "no_name\" {" << std::endl;
109  nodeStream << "node [shape = ellipse];" << std::endl;
110  std::string tab = " ";
111 
112  for (const auto node : __mb.nodes()) {
113  nodeStream << tab << node << "[label=\"" << __model.variable(node).name()
114  << "\"";
115  if (node == __node) { nodeStream << ", color=red"; }
116  nodeStream << "];" << std::endl;
117 
118  for (const auto chi : __mb.children(node)) {
119  arcStream << tab << node << " -> " << chi;
120  if (__specialArcs.exists(Arc(node, chi))) { arcStream << " [color=grey]"; }
121  arcStream << ";" << std::endl;
122  }
123  }
124 
125  output << nodeStream.str() << std::endl
126  << arcStream.str() << std::endl
127  << "}" << std::endl;
128 
129  return output.str();
130  }
const NodeId __node
Definition: MarkovBlanket.h:95
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:607
const DAGmodel & __model
Definition: MarkovBlanket.h:93
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variabe given it&#39;s node id.
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
const std::string & name() const
returns the name of the variable
+ Here is the call graph for this function:

Member Data Documentation

◆ __mb

DiGraph gum::MarkovBlanket::__mb
private

◆ __model

const DAGmodel& gum::MarkovBlanket::__model
private

Definition at line 93 of file MarkovBlanket.h.

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

◆ __node

const NodeId gum::MarkovBlanket::__node
private

Definition at line 95 of file MarkovBlanket.h.

Referenced by __buildMarkovBlanket(), and toDot().

◆ __specialArcs

ArcSet gum::MarkovBlanket::__specialArcs
private

Definition at line 96 of file MarkovBlanket.h.

Referenced by __buildMarkovBlanket(), and toDot().


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