aGrUM  0.14.2
instanceBayesNet_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  ***************************************************************************/
27 #include <agrum/PRM/instanceBayesNet.h> // to ease IDE parser
28 
29 namespace gum {
30  namespace prm {
31 
32  template < typename GUM_SCALAR >
33  void
35  for (const auto node : i.type().containerDag().nodes()) {
36  try {
37  // Adding the attribute
38  const PRMAttribute< GUM_SCALAR >& attr = i.get(node);
39  this->_dag.addNodeWithId(attr.id());
40  __varNodeMap.insert(&(attr.type().variable()), &attr);
41  } catch (NotFound&) {
42  // Not an attribute
43  }
44  }
45 
46  for (const auto& arc : i.type().containerDag().arcs()) {
47  try {
48  this->_dag.addArc(arc.tail(), arc.head());
49  } catch (InvalidNode&) {
50  // Not added means not an attribute
51  }
52  }
53  }
54 
55  template < typename GUM_SCALAR >
57  const PRMInstance< GUM_SCALAR >& i) :
58  IBayesNet< GUM_SCALAR >(),
59  __inst(&i) {
60  GUM_CONSTRUCTOR(InstanceBayesNet);
61  __init(i);
62  }
63 
64  template < typename GUM_SCALAR >
66  const InstanceBayesNet& from) :
67  IBayesNet< GUM_SCALAR >(from),
68  __varNodeMap(from.__varNodeMap), __inst(from.__inst) {
69  GUM_CONS_CPY(InstanceBayesNet);
70  }
71 
72  template < typename GUM_SCALAR >
74  GUM_DESTRUCTOR(InstanceBayesNet);
75  }
76 
77  template < typename GUM_SCALAR >
80  if (this != &from) {
82 
84  }
85 
86  return *this;
87  }
88 
89  template < typename GUM_SCALAR >
90  INLINE const Potential< GUM_SCALAR >&
92  return __get(varId).cpf();
93  }
94 
95  template < typename GUM_SCALAR >
96  INLINE const VariableNodeMap&
98  GUM_ERROR(NotFound, "no VariableNodeMap in an InstanceBayesNet");
99  }
100 
101  template < typename GUM_SCALAR >
102  INLINE const DiscreteVariable&
104  return __get(id).type().variable();
105  }
106 
107  template < typename GUM_SCALAR >
108  INLINE NodeId
110  return __varNodeMap[&var]->id();
111  }
112 
113  template < typename GUM_SCALAR >
114  INLINE NodeId
115  InstanceBayesNet< GUM_SCALAR >::idFromName(const std::string& name) const {
116  return __get(name).id();
117  }
118 
119  template < typename GUM_SCALAR >
120  INLINE const DiscreteVariable&
122  const std::string& name) const {
123  return __get(name).type().variable();
124  }
125 
126  template < typename GUM_SCALAR >
127  INLINE const PRMClassElement< GUM_SCALAR >&
129  return __inst->get(id);
130  }
131 
132  template < typename GUM_SCALAR >
133  INLINE const PRMClassElement< GUM_SCALAR >&
134  InstanceBayesNet< GUM_SCALAR >::__get(const std::string& name) const {
135  try {
136  return __inst->get(name);
137  } catch (NotFound&) {
138  GUM_ERROR(NotFound, "no element found with that name");
139  }
140  }
141 
142  template < typename GUM_SCALAR >
143  INLINE const NodeProperty< Size >&
145  if (__modalities.empty()) {
146  for (const auto node : this->nodes()) {
147  __modalities.insert(node, variable(node).domainSize());
148  }
149  }
150 
151  return __modalities;
152  }
153 
154  template < typename GUM_SCALAR >
155  INLINE std::string InstanceBayesNet< GUM_SCALAR >::toDot() const {
156  std::string tab = " ";
157  std::stringstream output;
158  output << "digraph \"";
159  output << __inst->name() << "\" {" << std::endl;
160 
161  for (const auto node : this->nodes()) {
162  if (this->children(node).size() > 0) {
163  const NodeSet& children = this->children(node);
164 
165  for (const auto chi : children) {
166  output << tab << "\"" << variable(node).name() << "\" -> ";
167  output << "\"" << variable(chi).name() << "\";" << std::endl;
168  }
169  } else if (this->parents(node).size() == 0) {
170  output << tab << "\"" << variable(node).name() << "\";" << std::endl;
171  }
172  }
173 
174  output << "}" << std::endl;
175  return output.str();
176  }
177 
178  } /* namespace prm */
179 } /* namespace gum */
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:57
virtual NodeId idFromName(const std::string &name) const
See gum::IBaseBayesNet::idFromName().
virtual const Potential< GUM_SCALAR > & cpt(NodeId varId) const
See gum::IBaseBayesNet::cpt().
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().
DiscreteVariable & variable()
Return a reference on the DiscreteVariable contained in this.
Definition: PRMType_inl.h:42
virtual const DiscreteVariable & variable(NodeId id) const
See gum::IBaseBayesNet::variable().
PRMAttribute< GUM_SCALAR > & get(NodeId id)
Getter on an PRMAttribute<GUM_SCALAR> of this PRMInstance<GUM_SCALAR>.
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Definition: DAGmodel_inl.h:103
virtual PRMType & type()=0
See gum::PRMClassElement::type().
An PRMInstance is a Bayesian Network fragment defined by a Class and used in a PRMSystem.
Definition: PRMInstance.h:60
const PRMClassElement< GUM_SCALAR > & __get(NodeId id) const
Private getter with type checking in case the id is not a formal PRMAttribute<GUM_SCALAR>.
PRMClass< GUM_SCALAR > & type()
Returns the type of this instance.
Container used to map discrete variables with nodes.
const NodeProperty< Size > & modalities() const
See gum::IBaseBayesNet::cpt().
virtual std::string toDot() const
Abstract class representing an element of PRM class.
IBayesNet< GUM_SCALAR > & operator=(const IBayesNet< GUM_SCALAR > &source)
Copy operator.
Definition: IBayesNet_tpl.h:64
InstanceBayesNet(const PRMInstance< GUM_SCALAR > &i)
Default constructor.
Base class for discrete random variable.
virtual const DiscreteVariable & variableFromName(const std::string &name) const
See gum::IBaseBayesNet::variableFromName().
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
HashTable< const DiscreteVariable *, const PRMAttribute< GUM_SCALAR > *> __varNodeMap
Mapping between DiscreteVariable and their NodeId.
The class for generic Hash Tables.
Definition: hashTable.h:676
NodeProperty< Size > __modalities
const PRMInstance< GUM_SCALAR > * __inst
The PRMClassElementContainer decorated by this.
Size size() const
Returns the number of variables in this Directed Graphical Model.
Definition: DAGmodel_inl.h:93
virtual NodeId nodeId(const DiscreteVariable &var) const
See gum::IBaseBayesNet::nodeId().
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
Definition: DAGmodel_inl.h:112
InstanceBayesNet & operator=(const InstanceBayesNet &from)
Copy operator.
Headers of InstanceBayesNet.
virtual ~InstanceBayesNet()
Destructor.
NodeId id() const
Returns the NodeId of this element in it&#39;s class DAG.
void __init(const PRMInstance< GUM_SCALAR > &i)
This class decorates an PRMInstance<GUM_SCALAR> as an IBaseBayesNet.
PRMAttribute is a member of a Class in a PRM.
Definition: PRMAttribute.h:58
const std::string & name() const
returns the name of the variable
Size NodeId
Type for node ids.
Definition: graphElements.h:97
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52