aGrUM  0.20.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/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  :
37  model__(m), node__(id) {
38  if (level < 1)
39  GUM_ERROR(InvalidArgument, "Argument level(=" << level << ") must be >0.")
40 
41  NodeSet done;
43  done.insert(node__);
44 
45  while (level > 1) {
46  level--;
47  auto todo = mb__.nodes().asNodeSet() - done;
48  bool anythingnew = false;
49  for (NodeId n: todo) {
50  done.insert(n);
51  if (buildMarkovBlanket__(n)) anythingnew = true;
52  }
53  if (!anythingnew) break;
54  }
55 
56  // we add now some arcs that are between the nodes in mb__ but are not part of
57  // the last ones.
58  // For instance, an arc between a parent and a parent of children
59  for (const auto node: mb__.nodes()) {
60  for (const auto child: model__.children(node)) {
61  if (mb__.existsNode(child) && !mb__.existsArc(Arc(node, child))) {
62  mb__.addArc(node, child);
63  specialArcs__.insert(Arc(node, child));
64  }
65  }
66  }
67  }
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
Definition: DAGmodel_inl.h:70
const NodeId node__
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 DAGmodel & model__
Definition: MarkovBlanket.h:98
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:632
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ 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 69 of file MarkovBlanket.cpp.

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

71  :
72  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 74 of file MarkovBlanket.cpp.

74 {}

Member Function Documentation

◆ arcs()

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

wrapping DAG::arcs()

Definition at line 54 of file MarkovBlanket_inl.h.

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

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

◆ buildMarkovBlanket__()

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

Definition at line 76 of file MarkovBlanket.cpp.

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

76  {
77  bool change = false;
78  if (!model__.nodes().exists(node))
79  GUM_ERROR(InvalidArgument, "Node " << node << " does not exist.");
80 
81  if (!mb__.nodes().exists(node)) {
82  mb__.addNodeWithId(node);
83  change = true;
84  }
85 
86  for (const auto& parent: model__.parents(node)) {
87  if (!mb__.nodes().exists(parent)) {
88  mb__.addNodeWithId(parent);
89  change = true;
90  }
91  mb__.addArc(parent, node);
92  }
93 
94  for (const auto& child: model__.children(node)) {
95  if (!mb__.nodes().exists(child)) {
96  mb__.addNodeWithId(child);
97  change = true;
98  }
99  mb__.addArc(node, child);
100  for (const auto& opar: model__.parents(child)) {
101  if (opar != node) {
102  if (!mb__.nodes().exists(opar)) {
103  mb__.addNodeWithId(opar);
104  change = true;
105  }
106  mb__.addArc(opar, child);
107  }
108  }
109  }
110 
111  return change;
112  }
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:70
const NodeGraphPart & nodes() const final
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:96
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:54
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 DAGmodel & model__
Definition: MarkovBlanket.h:98
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ 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 40 of file MarkovBlanket_inl.h.

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

40  {
41  return mb__.children(id);
42  }
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 48 of file MarkovBlanket_inl.h.

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

48  {
49  return mb__.children(ids);
50  }
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 114 of file MarkovBlanket.cpp.

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

114  {
115  if (size() != other.size()) return false;
116 
117  if (sizeArcs() != other.sizeArcs()) return false;
118 
119  for (const auto& nid: nodes()) {
120  try {
121  other.idFromName(model__.variable(nid).name());
122  } catch (NotFound) { return false; }
123  }
124 
125  for (const auto& arc: arcs()) {
126  if (!other.arcs().exists(
127  Arc(other.idFromName(model__.variable(arc.tail()).name()),
128  other.idFromName(model__.variable(arc.head()).name()))))
129  return false;
130  }
131 
132  return true;
133  }
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:98
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 60 of file MarkovBlanket_inl.h.

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

60 { 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  {
37  return mb__.parents(id);
38  }
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 44 of file MarkovBlanket_inl.h.

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

44  {
45  return mb__.parents(ids);
46  }
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 58 of file MarkovBlanket_inl.h.

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

58 { 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 52 of file MarkovBlanket_inl.h.

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

52 { 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 56 of file MarkovBlanket_inl.h.

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

56 { 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 135 of file MarkovBlanket.cpp.

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

135  {
136  std::stringstream output;
137  std::stringstream nodeStream;
138  std::stringstream arcStream;
139  List< NodeId > treatedNodes;
140  output << "digraph \""
141  << "no_name\" {" << std::endl;
142  nodeStream << "node [shape = ellipse];" << std::endl;
143  std::string tab = " ";
144 
145  for (const auto node: mb__.nodes()) {
146  nodeStream << tab << node << "[label=\"" << model__.variable(node).name()
147  << "\"";
148  if (node == node__) { nodeStream << ", color=red"; }
149  nodeStream << "];" << std::endl;
150 
151  for (const auto chi: mb__.children(node)) {
152  arcStream << tab << node << " -> " << chi;
153  if (specialArcs__.exists(Arc(node, chi))) { arcStream << " [color=grey]"; }
154  arcStream << ";" << std::endl;
155  }
156  }
157 
158  output << nodeStream.str() << std::endl
159  << arcStream.str() << std::endl
160  << "}" << std::endl;
161 
162  return output.str();
163  }
const NodeId node__
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:626
const DAGmodel & model__
Definition: MarkovBlanket.h:98
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: