aGrUM  0.14.2
classBayesNet_tpl.h
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  ***************************************************************************/
26 #include <agrum/PRM/classBayesNet.h> // to ease IDE parser
27 #include <agrum/core/inline.h>
28 
29 namespace gum {
30  namespace prm {
31 
32  template < typename GUM_SCALAR >
34  for (const auto node : c.containerDag().nodes()) {
35  try {
36  // Adding the attribute
39  const PRMClassElement< GUM_SCALAR >& elt = c.get(node);
40  this->_dag.addNodeWithId(elt.id());
41  this->__varNodeMap.insert(&(elt.type().variable()), &elt);
42  }
43  } catch (NotFound&) {
44  // Not an attribute
45  }
46  }
47 
48  for (const auto& arc : c.containerDag().arcs()) {
49  try {
50  this->_dag.addArc(arc.tail(), arc.head());
51  } catch (InvalidNode&) {
52  // Not added means not an attribute
53  }
54  }
55  }
56 
57  template < typename GUM_SCALAR >
59  const PRMClass< GUM_SCALAR >& c) :
60  IBayesNet< GUM_SCALAR >(),
61  __class(&c) {
62  GUM_CONSTRUCTOR(ClassBayesNet);
63  __init(c);
64  }
65 
66  template < typename GUM_SCALAR >
68  const ClassBayesNet< GUM_SCALAR >& from) :
69  IBayesNet< GUM_SCALAR >(from),
70  __class(from.__class) {
71  GUM_CONS_CPY(ClassBayesNet);
72  }
73 
74  template < typename GUM_SCALAR >
76  GUM_DESTRUCTOR(ClassBayesNet);
77  }
78 
79  template < typename GUM_SCALAR >
82  if (this != &from) {
84 
85  __class = from.__class;
86  }
87 
88  return *this;
89  }
90 
91  template < typename GUM_SCALAR >
92  INLINE const Potential< GUM_SCALAR >&
94  return __get(varId).cpf();
95  }
96 
97  template < typename GUM_SCALAR >
98  INLINE const VariableNodeMap&
100  GUM_ERROR(FatalError, "Sorry no VarMap in a ClassBayesNet.");
101  }
102 
103  template < typename GUM_SCALAR >
104  INLINE const DiscreteVariable&
106  return __get(id).type().variable();
107  }
108 
109  template < typename GUM_SCALAR >
110  INLINE NodeId
112  return __varNodeMap[&var]->id();
113  }
114 
115  template < typename GUM_SCALAR >
116  INLINE NodeId
117  ClassBayesNet< GUM_SCALAR >::idFromName(const std::string& name) const {
118  return __get(name).id();
119  }
120 
121  template < typename GUM_SCALAR >
123  const std::string& name) const {
124  return __get(name).type().variable();
125  }
126 
127  template < typename GUM_SCALAR >
128  INLINE const PRMClassElement< GUM_SCALAR >&
130  if (this->_dag.exists(id)) {
131  return __class->get(id);
132  } else {
133  GUM_ERROR(NotFound, "no element found with that id.");
134  }
135  }
136 
137  template < typename GUM_SCALAR >
138  INLINE const PRMClassElement< GUM_SCALAR >&
139  ClassBayesNet< GUM_SCALAR >::__get(const std::string& name) const {
140  try {
141  return __class->get(name);
142  } catch (NotFound&) {
143  GUM_ERROR(NotFound, "no element found with that id.");
144  }
145  }
146 
147  template < typename GUM_SCALAR >
148  INLINE const NodeProperty< Size >&
150  if (__modalities.empty()) {
151  for (const auto node : this->nodes()) {
152  __modalities.insert(node, (Size)variable(node).domainSize());
153  }
154  }
155 
156  return __modalities;
157  }
158 
159  template < typename GUM_SCALAR >
160  INLINE std::string ClassBayesNet< GUM_SCALAR >::toDot() const {
161  std::string tab = " ";
162  std::stringstream output;
163  output << "digraph \"";
164  output << __class->name() << "\" {" << std::endl;
165 
166  for (const auto node : this->nodes()) {
167  if (this->children(node).size() > 0)
168  for (const auto chi : this->children(node)) {
169  output << tab << "\"" << variable(node).name() << "\" -> ";
170  output << "\"" << variable(chi).name() << "\";" << std::endl;
171  }
172  else if (this->parents(node).size() == 0) {
173  output << tab << "\"" << variable(node).name() << "\";" << std::endl;
174  }
175  }
176 
177  output << "}" << std::endl;
178  return output.str();
179  }
180 
181  } /* namespace prm */
182 } /* namespace gum */
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:57
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
Definition: DAGmodel_inl.h:108
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:42
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:103
aGrUM&#39;s inline/outline selection
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:64
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:59
gum is the global namespace for all aGrUM entities
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:676
ClassBayesNet(const PRMClass< GUM_SCALAR > &c)
Default constructor.
Headers of ClassBayesNet<GUM_SCALAR>.
Size size() const
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:93
DAG _dag
The DAG of this Directed Graphical Model.
Definition: DAGmodel.h:200
This class decorates a gum::prm::Class<GUM_SCALAR> has an IBaseBayesNet.
Definition: classBayesNet.h:57
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:112
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:63
virtual std::string toDot() const
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
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:97
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:52
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const
Returns the CPT of a node in this ClassBayesNet<GUM_SCALAR>.