aGrUM  0.16.0
classBayesNet_tpl.h
Go to the documentation of this file.
1 
29 #include <agrum/PRM/classBayesNet.h> // to ease IDE parser
30 #include <agrum/core/inline.h>
31 
32 namespace gum {
33  namespace prm {
34 
35  template < typename GUM_SCALAR >
37  for (const auto node : c.containerDag().nodes()) {
38  try {
39  // Adding the attribute
42  const PRMClassElement< GUM_SCALAR >& elt = c.get(node);
43  this->_dag.addNodeWithId(elt.id());
44  this->__varNodeMap.insert(&(elt.type().variable()), &elt);
45  }
46  } catch (NotFound&) {
47  // Not an attribute
48  }
49  }
50 
51  for (const auto& arc : c.containerDag().arcs()) {
52  try {
53  this->_dag.addArc(arc.tail(), arc.head());
54  } catch (InvalidNode&) {
55  // Not added means not an attribute
56  }
57  }
58  }
59 
60  template < typename GUM_SCALAR >
62  const PRMClass< GUM_SCALAR >& c) :
63  IBayesNet< GUM_SCALAR >(),
64  __class(&c) {
65  GUM_CONSTRUCTOR(ClassBayesNet);
66  __init(c);
67  }
68 
69  template < typename GUM_SCALAR >
71  const ClassBayesNet< GUM_SCALAR >& from) :
72  IBayesNet< GUM_SCALAR >(from),
73  __class(from.__class) {
74  GUM_CONS_CPY(ClassBayesNet);
75  }
76 
77  template < typename GUM_SCALAR >
79  GUM_DESTRUCTOR(ClassBayesNet);
80  }
81 
82  template < typename GUM_SCALAR >
85  if (this != &from) {
87 
88  __class = from.__class;
89  }
90 
91  return *this;
92  }
93 
94  template < typename GUM_SCALAR >
95  INLINE const Potential< GUM_SCALAR >&
97  return __get(varId).cpf();
98  }
99 
100  template < typename GUM_SCALAR >
101  INLINE const VariableNodeMap&
103  GUM_ERROR(FatalError, "Sorry no VarMap in a ClassBayesNet.");
104  }
105 
106  template < typename GUM_SCALAR >
107  INLINE const DiscreteVariable&
109  return __get(id).type().variable();
110  }
111 
112  template < typename GUM_SCALAR >
113  INLINE NodeId
115  return __varNodeMap[&var]->id();
116  }
117 
118  template < typename GUM_SCALAR >
119  INLINE NodeId
120  ClassBayesNet< GUM_SCALAR >::idFromName(const std::string& name) const {
121  return __get(name).id();
122  }
123 
124  template < typename GUM_SCALAR >
126  const std::string& name) const {
127  return __get(name).type().variable();
128  }
129 
130  template < typename GUM_SCALAR >
131  INLINE const PRMClassElement< GUM_SCALAR >&
133  if (this->_dag.exists(id)) {
134  return __class->get(id);
135  } else {
136  GUM_ERROR(NotFound, "no element found with that id.");
137  }
138  }
139 
140  template < typename GUM_SCALAR >
141  INLINE const PRMClassElement< GUM_SCALAR >&
142  ClassBayesNet< GUM_SCALAR >::__get(const std::string& name) const {
143  try {
144  return __class->get(name);
145  } catch (NotFound&) {
146  GUM_ERROR(NotFound, "no element found with that id.");
147  }
148  }
149 
150  template < typename GUM_SCALAR >
151  INLINE const NodeProperty< Size >&
153  if (__modalities.empty()) {
154  for (const auto node : this->nodes()) {
155  __modalities.insert(node, (Size)variable(node).domainSize());
156  }
157  }
158 
159  return __modalities;
160  }
161 
162  template < typename GUM_SCALAR >
163  INLINE std::string ClassBayesNet< GUM_SCALAR >::toDot() const {
164  std::string tab = " ";
165  std::stringstream output;
166  output << "digraph \"";
167  output << __class->name() << "\" {" << std::endl;
168 
169  for (const auto node : this->nodes()) {
170  if (this->children(node).size() > 0)
171  for (const auto chi : this->children(node)) {
172  output << tab << "\"" << variable(node).name() << "\" -> ";
173  output << "\"" << variable(chi).name() << "\";" << std::endl;
174  }
175  else if (this->parents(node).size() == 0) {
176  output << tab << "\"" << variable(node).name() << "\";" << std::endl;
177  }
178  }
179 
180  output << "}" << std::endl;
181  return output.str();
182  }
183 
184  } /* namespace prm */
185 } /* namespace gum */
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:60
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
Definition: DAGmodel_inl.h:111
virtual const VariableNodeMap & variableNodeMap() const
See gum::IBaseBayesNet::variableNodeMap().
virtual const DiscreteVariable & variable(NodeId id) const
See gum::IBaseBayesNet::variable().
DiscreteVariable & variable()
Return a reference on the DiscreteVariable contained in this.
Definition: PRMType_inl.h:45
void __init(const PRMClass< GUM_SCALAR > &c)
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.
Container used to map discrete variables with nodes.
Abstract class representing an element of PRM class.
const PRMClassElement< GUM_SCALAR > & __get(NodeId id) const
Private getter with type checking in case the id is not a formal PRMAttribute.
const PRMClass< GUM_SCALAR > * __class
The PRMClassElementContainer decorated by this.
NodeProperty< Size > __modalities
IBayesNet< GUM_SCALAR > & operator=(const IBayesNet< GUM_SCALAR > &source)
Copy operator.
Definition: IBayesNet_tpl.h:67
bool exists(const NodeId id) const
alias for existsNode
Base class for discrete random variable.
Class representing the minimal interface for Bayesian Network.
Definition: IBayesNet.h:62
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
virtual const DiscreteVariable & variableFromName(const std::string &name) const
See gum::IBaseBayesNet::variableFromName().
The class for generic Hash Tables.
Definition: hashTable.h:679
ClassBayesNet(const PRMClass< GUM_SCALAR > &c)
Default constructor.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Size size() const
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:96
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:203
This class decorates a gum::prm::Class<GUM_SCALAR> has an IBaseBayesNet.
Definition: classBayesNet.h:60
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:115
PRMClassElement< GUM_SCALAR > & get(NodeId id)
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(NodeId).
virtual PRMType & type()=0
Return a reference over the gum::PRMType of this class element.
virtual ~ClassBayesNet()
Destructor.
virtual NodeId nodeId(const DiscreteVariable &var) const
See gum::IBaseBayesNet::nodeId().
NodeId id() const
Returns the NodeId of this element in it&#39;s class DAG.
A PRMClass is an object of a PRM representing a fragment of a Bayesian Network which can be instantia...
Definition: PRMClass.h:66
virtual std::string toDot() const
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
const std::string & name() const
returns the name of the variable
ClassBayesNet< GUM_SCALAR > & operator=(const ClassBayesNet< GUM_SCALAR > &from)
Copy operator.
virtual const DAG & containerDag() const
Returns the gum::DAG of this PRMClassElementContainer.
virtual NodeId idFromName(const std::string &name) const
See gum::IBaseBayesNet::idFromName().
Size NodeId
Type for node ids.
Definition: graphElements.h:98
const NodeProperty< Size > & modalities() const
See gum::IBaseBayesNet::modalities().
HashTable< const DiscreteVariable *, const PRMClassElement< GUM_SCALAR > *> __varNodeMap
Mapping between DiscreteVariable and their NodeId.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const
Returns the CPT of a node in this ClassBayesNet<GUM_SCALAR>.