aGrUM  0.16.0
MarkovBlanket.cpp
Go to the documentation of this file.
1 
31 
32 #ifdef GUM_NO_INLINE
34 #endif // GUM_NOINLINE
35 
36 namespace gum {
38  __model(m), __node(id) {
40  }
41 
42  MarkovBlanket::MarkovBlanket(const DAGmodel& m, const std::string& name) :
43  MarkovBlanket(m, m.idFromName(name)) {}
44 
46 
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  }
80 
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  }
101 
102  std::string MarkovBlanket::toDot() const {
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  }
131 } // namespace gum
const ArcSet & arcs() const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:104
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
Virtual base class for PGMs using a DAG.
Definition: DAGmodel.h:48
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
Definition: DAGmodel_inl.h:111
Size sizeArcs() const
wrapping DiGraph::sizeArcs()
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:106
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Size sizeArcs() const
Returns the number of arcs in this Directed Graphical Model.
Definition: DAGmodel_inl.h:102
MarkovBlanket(const DAGmodel &m, NodeId n)
bool exists(const NodeId id) const
alias for existsNode
Generic doubly linked lists.
Definition: list.h:372
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
const NodeId __node
Definition: MarkovBlanket.h:95
Class building the markov Blanket from a BN and a nodeName.
Definition: MarkovBlanket.h:49
std::string toDot() const
Size size() const
wrapping DiGraph::size()
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
bool hasSameStructure(const DAGmodel &other)
Size size() const
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:96
const ArcSet & arcs() const
wrapping DiGraph::arcs()
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:607
The base class for all directed edgesThis class is used as a basis for manipulating all directed edge...
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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
virtual const DiscreteVariable & variable(NodeId id) const =0
Returns a constant reference over a variabe given it&#39;s node id.
bool existsNode(const NodeId id) const
returns true iff the NodeGraphPart contains the given nodeId
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
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:613
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
const NodeGraphPart & nodes() const
wrapping DiGraph::nodes()