aGrUM  0.16.0
instanceBayesNet_tpl.h
Go to the documentation of this file.
1 
30 #include <agrum/PRM/instanceBayesNet.h> // to ease IDE parser
31 
32 namespace gum {
33  namespace prm {
34 
35  template < typename GUM_SCALAR >
36  void
38  for (const auto node : i.type().containerDag().nodes()) {
39  try {
40  // Adding the attribute
41  const PRMAttribute< GUM_SCALAR >& attr = i.get(node);
42  this->_dag.addNodeWithId(attr.id());
43  __varNodeMap.insert(&(attr.type().variable()), &attr);
44  } catch (NotFound&) {
45  // Not an attribute
46  }
47  }
48 
49  for (const auto& arc : i.type().containerDag().arcs()) {
50  try {
51  this->_dag.addArc(arc.tail(), arc.head());
52  } catch (InvalidNode&) {
53  // Not added means not an attribute
54  }
55  }
56  }
57 
58  template < typename GUM_SCALAR >
60  const PRMInstance< GUM_SCALAR >& i) :
61  IBayesNet< GUM_SCALAR >(),
62  __inst(&i) {
63  GUM_CONSTRUCTOR(InstanceBayesNet);
64  __init(i);
65  }
66 
67  template < typename GUM_SCALAR >
69  const InstanceBayesNet& from) :
70  IBayesNet< GUM_SCALAR >(from),
71  __varNodeMap(from.__varNodeMap), __inst(from.__inst) {
72  GUM_CONS_CPY(InstanceBayesNet);
73  }
74 
75  template < typename GUM_SCALAR >
77  GUM_DESTRUCTOR(InstanceBayesNet);
78  }
79 
80  template < typename GUM_SCALAR >
83  if (this != &from) {
85 
87  }
88 
89  return *this;
90  }
91 
92  template < typename GUM_SCALAR >
93  INLINE const Potential< GUM_SCALAR >&
95  return __get(varId).cpf();
96  }
97 
98  template < typename GUM_SCALAR >
99  INLINE const VariableNodeMap&
101  GUM_ERROR(NotFound, "no VariableNodeMap in an InstanceBayesNet");
102  }
103 
104  template < typename GUM_SCALAR >
105  INLINE const DiscreteVariable&
107  return __get(id).type().variable();
108  }
109 
110  template < typename GUM_SCALAR >
111  INLINE NodeId
113  return __varNodeMap[&var]->id();
114  }
115 
116  template < typename GUM_SCALAR >
117  INLINE NodeId
118  InstanceBayesNet< GUM_SCALAR >::idFromName(const std::string& name) const {
119  return __get(name).id();
120  }
121 
122  template < typename GUM_SCALAR >
123  INLINE const DiscreteVariable&
125  const std::string& name) const {
126  return __get(name).type().variable();
127  }
128 
129  template < typename GUM_SCALAR >
130  INLINE const PRMClassElement< GUM_SCALAR >&
132  return __inst->get(id);
133  }
134 
135  template < typename GUM_SCALAR >
136  INLINE const PRMClassElement< GUM_SCALAR >&
137  InstanceBayesNet< GUM_SCALAR >::__get(const std::string& name) const {
138  try {
139  return __inst->get(name);
140  } catch (NotFound&) {
141  GUM_ERROR(NotFound, "no element found with that name");
142  }
143  }
144 
145  template < typename GUM_SCALAR >
146  INLINE const NodeProperty< Size >&
148  if (__modalities.empty()) {
149  for (const auto node : this->nodes()) {
150  __modalities.insert(node, variable(node).domainSize());
151  }
152  }
153 
154  return __modalities;
155  }
156 
157  template < typename GUM_SCALAR >
158  INLINE std::string InstanceBayesNet< GUM_SCALAR >::toDot() const {
159  std::string tab = " ";
160  std::stringstream output;
161  output << "digraph \"";
162  output << __inst->name() << "\" {" << std::endl;
163 
164  for (const auto node : this->nodes()) {
165  if (this->children(node).size() > 0) {
166  const NodeSet& children = this->children(node);
167 
168  for (const auto chi : children) {
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:60
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:111
virtual const VariableNodeMap & variableNodeMap() const
See gum::IBaseBayesNet::variableNodeMap().
DiscreteVariable & variable()
Return a reference on the DiscreteVariable contained in this.
Definition: PRMType_inl.h:45
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:106
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:63
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:67
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:62
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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:679
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:96
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:115
InstanceBayesNet & operator=(const InstanceBayesNet &from)
Copy operator.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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:61
const std::string & name() const
returns the name of the variable
Size NodeId
Type for node ids.
Definition: graphElements.h:98
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55