aGrUM  0.14.2
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 47 of file MarkovBlanket.h.

Constructor & Destructor Documentation

◆ MarkovBlanket() [1/2]

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

Definition at line 34 of file MarkovBlanket.cpp.

References __buildMarkovBlanket().

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

◆ MarkovBlanket() [2/2]

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

Definition at line 39 of file MarkovBlanket.cpp.

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

◆ ~MarkovBlanket()

gum::MarkovBlanket::~MarkovBlanket ( )

Definition at line 42 of file MarkovBlanket.cpp.

42 {}

Member Function Documentation

◆ __buildMarkovBlanket()

void gum::MarkovBlanket::__buildMarkovBlanket ( )
private

Definition at line 44 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().

44  {
45  if (!__model.nodes().exists(__node))
46  GUM_ERROR(InvalidArgument, "Node " << __node << " does not exist.");
47 
49  for (const auto& parent : __model.parents(__node)) {
50  __mb.addNodeWithId(parent);
51  __mb.addArc(parent, __node);
52  }
53 
54  for (const auto& child : __model.children(__node)) {
55  if (!__mb.nodes().exists(child)) __mb.addNodeWithId(child);
56  __mb.addArc(__node, child);
57  for (const auto& opar : __model.parents(child)) {
58  if (opar != __node) {
59  if (!__mb.nodes().exists(opar)) __mb.addNodeWithId(opar);
60  __mb.addArc(opar, child);
61  }
62  }
63  }
64 
65  // we add now some arcs that are between the nodes in __mb but are not part of
66  // the last ones.
67  // For instance, an arc between a parent and a parent of children
68  for (const auto node : __mb.nodes()) {
69  for (const auto child : __model.children(node)) {
70  if (__mb.existsNode(child) && !__mb.existsArc(Arc(node, child))) {
71  __mb.addArc(node, child);
72  __specialArcs.insert(Arc(node, child));
73  }
74  }
75  }
76  }
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:32
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
Definition: DAGmodel_inl.h:108
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:103
bool exists(const NodeId id) const
alias for existsNode
const NodeId __node
Definition: MarkovBlanket.h:93
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:112
const DAGmodel & __model
Definition: MarkovBlanket.h:91
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:610
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ 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 44 of file MarkovBlanket_inl.h.

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

Referenced by hasSameStructure().

44 { 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 38 of file MarkovBlanket_inl.h.

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

38  {
39  return __mb.children(id);
40  }
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 78 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().

78  {
79  if (size() != other.size()) return false;
80 
81  if (sizeArcs() != other.sizeArcs()) return false;
82 
83  for (const auto& nid : nodes()) {
84  try {
85  other.idFromName(__model.variable(nid).name());
86  } catch (NotFound) { return false; }
87  }
88 
89  for (const auto& arc : arcs()) {
90  if (!other.arcs().exists(
91  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 DiGraph::sizeArcs()
Size size() const
wrapping DiGraph::size()
const ArcSet & arcs() const
wrapping DiGraph::arcs()
const DAGmodel & __model
Definition: MarkovBlanket.h:91
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 32 of file MarkovBlanket_inl.h.

References __mb.

32 { return __mb; }

◆ nodes()

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

wrapping DiGraph::nodes()

Definition at line 50 of file MarkovBlanket_inl.h.

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

Referenced by hasSameStructure().

50 { 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 34 of file MarkovBlanket_inl.h.

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

34  {
35  return __mb.parents(id);
36  }
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 48 of file MarkovBlanket_inl.h.

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

Referenced by hasSameStructure().

48 { 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 42 of file MarkovBlanket_inl.h.

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

Referenced by hasSameStructure().

42 { 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 46 of file MarkovBlanket_inl.h.

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

46 { 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 99 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().

99  {
100  std::stringstream output;
101  std::stringstream nodeStream;
102  std::stringstream arcStream;
103  List< NodeId > treatedNodes;
104  output << "digraph \""
105  << "no_name\" {" << std::endl;
106  nodeStream << "node [shape = ellipse];" << std::endl;
107  std::string tab = " ";
108 
109  for (const auto node : __mb.nodes()) {
110  nodeStream << tab << node << "[label=\"" << __model.variable(node).name()
111  << "\"";
112  if (node == __node) { nodeStream << ", color=red"; }
113  nodeStream << "];" << std::endl;
114 
115  for (const auto chi : __mb.children(node)) {
116  arcStream << tab << node << " -> " << chi;
117  if (__specialArcs.exists(Arc(node, chi))) { arcStream << " [color=grey]"; }
118  arcStream << ";" << std::endl;
119  }
120  }
121 
122  output << nodeStream.str() << std::endl
123  << arcStream.str() << std::endl
124  << "}" << std::endl;
125 
126  return output.str();
127  }
const NodeId __node
Definition: MarkovBlanket.h:93
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:604
const DAGmodel & __model
Definition: MarkovBlanket.h:91
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 91 of file MarkovBlanket.h.

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

◆ __node

const NodeId gum::MarkovBlanket::__node
private

Definition at line 93 of file MarkovBlanket.h.

Referenced by __buildMarkovBlanket(), and toDot().

◆ __specialArcs

ArcSet gum::MarkovBlanket::__specialArcs
private

Definition at line 94 of file MarkovBlanket.h.

Referenced by __buildMarkovBlanket(), and toDot().


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