aGrUM  0.13.2
MarkovBlanket.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
28 
29 #ifdef GUM_NO_INLINE
31 #endif // GUM_NOINLINE
32 
33 namespace gum {
35  __model(m), __node(id) {
37  }
38 
39  MarkovBlanket::MarkovBlanket(const DAGmodel& m, const std::string& name) :
40  MarkovBlanket(m, m.idFromName(name)) {}
41 
43 
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  }
77 
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  }
98 
99  std::string MarkovBlanket::toDot() const {
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  }
128 } // namespace gum
Size size() const
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:94
Size sizeArcs() const
wrapping DiGraph::sizeArcs()
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
Virtual base class for PGMs using a DAG.
Definition: DAGmodel.h:45
unsigned int NodeId
Type for node ids.
Definition: graphElements.h:97
Inline implementation of the class building the essential Graph from a DAGmodel.
const ArcSet & arcs() const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:102
MarkovBlanket(const DAGmodel &m, NodeId n)
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:607
std::string toDot() const
Size sizeArcs() const
Returns the number of arcs in this Directed Graphical Model.
Definition: DAGmodel_inl.h:100
Generic doubly linked lists.
Definition: list.h:369
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
Definition: DAGmodel_inl.h:109
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
const NodeId __node
Definition: MarkovBlanket.h:93
Class building the markov Blanket from a BN and a nodeName.
Definition: MarkovBlanket.h:47
virtual NodeId idFromName(const std::string &name) const =0
Getter by name.
bool hasSameStructure(const DAGmodel &other)
Size size() const
wrapping DiGraph::size()
The base class for all directed edgesThis class is used as a basis for manipulating all directed edge...
Class building the markovBlanket from a DAGmodel and a node name.
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 NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:104
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
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:113
const std::string & name() const
returns the name of the variable
bool exists(const NodeId id) const
alias for existsNode
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:66
const NodeGraphPart & nodes() const
wrapping DiGraph::nodes()