aGrUM  0.17.2
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/MarokovBlanket.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 (const NodeId id) const
 wrapping DAG::parents(id) More...
 
const NodeSetchildren (const NodeId id) const
 wrapping DAG::parents(id) 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 49 of file MarkovBlanket.h.

Constructor & Destructor Documentation

◆ MarkovBlanket() [1/2]

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

Definition at line 37 of file MarkovBlanket.cpp.

References __buildMarkovBlanket(), __mb, __model, __node, __specialArcs, gum::DAG::addArc(), gum::NodeGraphPart::asNodeSet(), gum::DAGmodel::children(), gum::ArcGraphPart::existsArc(), gum::NodeGraphPart::existsNode(), GUM_ERROR, gum::Set< Key, Alloc >::insert(), and gum::NodeGraphPart::nodes().

37  :
38  __model(m), __node(id) {
39  if (level < 1)
40  GUM_ERROR(InvalidArgument, "Argument level(=" << level << ") must be >0.")
41 
42  NodeSet done;
44  done.insert(__node);
45 
46  while (level > 1) {
47  level--;
48  auto todo = __mb.nodes().asNodeSet() - done;
49  bool anythingnew = false;
50  for (NodeId n: todo) {
51  done.insert(n);
52  if (__buildMarkovBlanket(n)) anythingnew = true;
53  }
54  if (!anythingnew) break;
55  }
56 
57  // we add now some arcs that are between the nodes in __mb but are not part of
58  // the last ones.
59  // For instance, an arc between a parent and a parent of children
60  for (const auto node: __mb.nodes()) {
61  for (const auto child: __model.children(node)) {
62  if (__mb.existsNode(child) && !__mb.existsArc(Arc(node, child))) {
63  __mb.addArc(node, child);
64  __specialArcs.insert(Arc(node, child));
65  }
66  }
67  }
68  }
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
Definition: DAGmodel_inl.h:53
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
const NodeId __node
Definition: MarkovBlanket.h:95
bool __buildMarkovBlanket(const NodeId id)
const DAGmodel & __model
Definition: MarkovBlanket.h:93
NodeSet asNodeSet() const
returns a copy of the set of nodes represented by the NodeGraphPart
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
virtual void addArc(const NodeId tail, const NodeId head)
insert a new arc into the directed graph
Definition: DAG_inl.h:43
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
Size NodeId
Type for node ids.
Definition: graphElements.h:98
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:615
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ 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 70 of file MarkovBlanket.cpp.

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

◆ ~MarkovBlanket()

gum::MarkovBlanket::~MarkovBlanket ( )

Definition at line 75 of file MarkovBlanket.cpp.

75 {}

Member Function Documentation

◆ __buildMarkovBlanket()

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

Definition at line 77 of file MarkovBlanket.cpp.

References __mb, __model, gum::DAG::addArc(), gum::NodeGraphPart::addNodeWithId(), gum::DAGmodel::children(), gum::NodeGraphPart::exists(), GUM_ERROR, gum::DAGmodel::nodes(), gum::NodeGraphPart::nodes(), and gum::DAGmodel::parents().

Referenced by MarkovBlanket().

77  {
78  bool change = false;
79  if (!__model.nodes().exists(node))
80  GUM_ERROR(InvalidArgument, "Node " << node << " does not exist.");
81 
82  if (!__mb.nodes().exists(node)) {
83  __mb.addNodeWithId(node);
84  change = true;
85  }
86 
87  for (const auto& parent: __model.parents(node)) {
88  if (!__mb.nodes().exists(parent)) {
89  __mb.addNodeWithId(parent);
90  change = true;
91  }
92  __mb.addArc(parent, node);
93  }
94 
95  for (const auto& child: __model.children(node)) {
96  if (!__mb.nodes().exists(child)) {
97  __mb.addNodeWithId(child);
98  change = true;
99  }
100  __mb.addArc(node, child);
101  for (const auto& opar: __model.parents(child)) {
102  if (opar != node) {
103  if (!__mb.nodes().exists(opar)) {
104  __mb.addNodeWithId(opar);
105  change = true;
106  }
107  __mb.addArc(opar, child);
108  }
109  }
110  }
111 
112  return change;
113  }
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:53
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:46
bool exists(const NodeId id) const
alias for existsNode
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:60
const DAGmodel & __model
Definition: MarkovBlanket.h:93
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
virtual void addArc(const NodeId tail, const NodeId head)
insert a new arc into the directed graph
Definition: DAG_inl.h:43
#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 DAG::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 DAG::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:

◆ dag()

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

Definition at line 35 of file MarkovBlanket_inl.h.

References __mb.

35 { return __mb; }

◆ 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 115 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().

115  {
116  if (size() != other.size()) return false;
117 
118  if (sizeArcs() != other.sizeArcs()) return false;
119 
120  for (const auto& nid: nodes()) {
121  try {
122  other.idFromName(__model.variable(nid).name());
123  } catch (NotFound) { return false; }
124  }
125 
126  for (const auto& arc: arcs()) {
127  if (!other.arcs().exists(
128  Arc(other.idFromName(__model.variable(arc.tail()).name()),
129  other.idFromName(__model.variable(arc.head()).name()))))
130  return false;
131  }
132 
133  return true;
134  }
Size sizeArcs() const
wrapping DAG::sizeArcs()
Size size() const
wrapping DAG::size()
const ArcSet & arcs() const
wrapping DAG::arcs()
const DAGmodel & __model
Definition: MarkovBlanket.h:93
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 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 DAG::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 DAG::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 DAG::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 DAG::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 136 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().

136  {
137  std::stringstream output;
138  std::stringstream nodeStream;
139  std::stringstream arcStream;
140  List< NodeId > treatedNodes;
141  output << "digraph \""
142  << "no_name\" {" << std::endl;
143  nodeStream << "node [shape = ellipse];" << std::endl;
144  std::string tab = " ";
145 
146  for (const auto node: __mb.nodes()) {
147  nodeStream << tab << node << "[label=\"" << __model.variable(node).name()
148  << "\"";
149  if (node == __node) { nodeStream << ", color=red"; }
150  nodeStream << "];" << std::endl;
151 
152  for (const auto chi: __mb.children(node)) {
153  arcStream << tab << node << " -> " << chi;
154  if (__specialArcs.exists(Arc(node, chi))) { arcStream << " [color=grey]"; }
155  arcStream << ";" << std::endl;
156  }
157  }
158 
159  output << nodeStream.str() << std::endl
160  << arcStream.str() << std::endl
161  << "}" << std::endl;
162 
163  return output.str();
164  }
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:609
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 variable 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

DAG gum::MarkovBlanket::__mb
private

◆ __model

const DAGmodel& gum::MarkovBlanket::__model
private

Definition at line 93 of file MarkovBlanket.h.

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

◆ __node

const NodeId gum::MarkovBlanket::__node
private

Definition at line 95 of file MarkovBlanket.h.

Referenced by MarkovBlanket(), and toDot().

◆ __specialArcs

ArcSet gum::MarkovBlanket::__specialArcs
private

Definition at line 96 of file MarkovBlanket.h.

Referenced by MarkovBlanket(), and toDot().


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