aGrUM  0.16.0
PRMInference_tpl.h
Go to the documentation of this file.
1 
31 
32 namespace gum {
33  namespace prm {
34 
35  template < typename GUM_SCALAR >
37  for (const auto& elt : __evidences) {
38  for (const auto& elt2 : *elt.second)
39  delete elt2.second;
40 
41  delete elt.second;
42  }
43 
44  __evidences.clear();
45  }
46 
47  template < typename GUM_SCALAR >
49  const PRMInference< GUM_SCALAR >& source) :
50  _prm(source._prm),
51  _sys(source._sys) {
52  GUM_CONS_CPY(PRMInference);
53 
54  for (const auto& elt : source.__evidences) {
55  __evidences.insert(elt.first, new PRMInference< GUM_SCALAR >::EMap());
56 
57  for (const auto& elt2 : *elt.second) {
59  e->add(*(elt2.second->variablesSequence().front()));
60  Instantiation i(*e);
61 
62  for (i.setFirst(); !i.end(); i.inc())
63  e->set(i, elt2.second->get(i));
64 
65  __evidences[elt.first]->insert(elt2.first, e);
66  }
67  }
68  }
69 
70  template < typename GUM_SCALAR >
73  clearEvidence();
74  _prm = source._prm;
75  _sys = source._sys;
76 
77  for (const auto& elt : source.__evidences) {
78  __evidences.insert(elt.first, new PRMInference< GUM_SCALAR >::EMap());
79 
80  for (const auto& elt2 : *elt.second) {
82  e->add(*(elt2.second->variablesSequence().front()));
83  Instantiation i(*e);
84 
85  for (i.setFirst(); !i.end(); i.inc()) {
86  e->set(i, elt2.second->get(i));
87  }
88 
89  __evidences[elt.first]->insert(elt2.first, e);
90  }
91  }
92 
93  return *this;
94  }
95 
96  template < typename GUM_SCALAR >
99  if (__evidences.exists(i)) {
100  return *(__evidences[i]);
101  } else {
102  __evidences.insert(i, new PRMInference< GUM_SCALAR >::EMap());
103  return *(__evidences[i]);
104  }
105  }
106 
107  template < typename GUM_SCALAR >
108  void
110  const Potential< GUM_SCALAR >& p) {
111  if (chain.first->exists(chain.second->id())) {
112  if ((p.nbrDim() != 1) || (!p.contains(chain.second->type().variable())))
114  "illegal evidence for the given PRMAttribute.");
115 
117  e->add(chain.second->type().variable());
118  Instantiation i(*e);
119 
120  for (i.setFirst(); !i.end(); i.inc())
121  e->set(i, p.get(i));
122 
123  PRMInference< GUM_SCALAR >::EMap& emap = __EMap(chain.first);
124 
125  if (emap.exists(chain.second->id())) {
126  delete emap[chain.second->id()];
127  emap[chain.second->id()] = e;
128  } else {
129  emap.insert(chain.second->id(), e);
130  }
131 
132  _evidenceAdded(chain);
133  } else {
135  "the given PRMAttribute does not belong to this "
136  "Instance<GUM_SCALAR>.");
137  }
138  }
139 
140  template < typename GUM_SCALAR >
142  const PRM< GUM_SCALAR >& prm, const PRMSystem< GUM_SCALAR >& system) :
143  _prm(&prm),
144  _sys(&system) {
145  GUM_CONSTRUCTOR(PRMInference);
146  }
147 
148  template < typename GUM_SCALAR >
150  GUM_DESTRUCTOR(PRMInference);
151  clearEvidence();
152  }
153 
154  template < typename GUM_SCALAR >
155  INLINE typename PRMInference< GUM_SCALAR >::EMap&
157  try {
158  return *(__evidences[&i]);
159  } catch (NotFound&) {
160  GUM_ERROR(NotFound, "this instance has no evidence.");
161  }
162  }
163 
164  template < typename GUM_SCALAR >
165  INLINE const typename PRMInference< GUM_SCALAR >::EMap&
167  const PRMInstance< GUM_SCALAR >& i) const {
168  try {
169  return *(__evidences[&i]);
170  } catch (NotFound&) {
171  GUM_ERROR(NotFound, "this instance has no evidence.");
172  }
173  }
174 
175  template < typename GUM_SCALAR >
176  INLINE typename PRMInference< GUM_SCALAR >::EMap&
178  try {
179  return *(__evidences[i]);
180  } catch (NotFound&) {
181  GUM_ERROR(NotFound, "this instance has no evidence.");
182  }
183  }
184 
185  template < typename GUM_SCALAR >
186  INLINE const typename PRMInference< GUM_SCALAR >::EMap&
188  const PRMInstance< GUM_SCALAR >* i) const {
189  try {
190  return *(__evidences[i]);
191  } catch (NotFound&) {
192  GUM_ERROR(NotFound, "this instance has no evidence.");
193  }
194  }
195 
196  template < typename GUM_SCALAR >
198  const PRMInstance< GUM_SCALAR >& i) const {
199  return __evidences.exists(&i);
200  }
201 
202  template < typename GUM_SCALAR >
204  const PRMInstance< GUM_SCALAR >* i) const {
205  return __evidences.exists(i);
206  }
207 
208  template < typename GUM_SCALAR >
209  INLINE bool PRMInference< GUM_SCALAR >::hasEvidence(const Chain& chain) const {
210  return (hasEvidence(chain.first))
211  ? evidence(chain.first).exists(chain.second->id())
212  : false;
213  }
214 
215  template < typename GUM_SCALAR >
217  return (__evidences.size() != (Size)0);
218  }
219 
220  template < typename GUM_SCALAR >
222  try {
223  if (__EMap(chain.first).exists(chain.second->id())) {
224  _evidenceRemoved(chain);
225  delete __EMap(chain.first)[chain.second->id()];
226  __EMap(chain.first).erase(chain.second->id());
227  }
228  } catch (NotFound&) {
229  // Ok, we are only removing
230  }
231  }
232 
233  template < typename GUM_SCALAR >
235  const typename PRMInference< GUM_SCALAR >::Chain& chain,
237  if (m.nbrDim() > 0) {
238  GUM_ERROR(OperationNotAllowed, "the given Potential is not empty.");
239  }
240 
241  if (hasEvidence(chain)) {
242  m.add(chain.second->type().variable());
243  const Potential< GUM_SCALAR >& e =
244  *(evidence(chain.first)[chain.second->id()]);
245  Instantiation i(m), j(e);
246 
247  for (i.setFirst(), j.setFirst(); !i.end(); i.inc(), j.inc())
248  m.set(i, e.get(j));
249  } else {
250  if (chain.second != &(chain.first->get(chain.second->safeName()))) {
251  typename PRMInference< GUM_SCALAR >::Chain good_chain = std::make_pair(
252  chain.first, &(chain.first->get(chain.second->safeName())));
253  m.add(good_chain.second->type().variable());
254  _marginal(good_chain, m);
255  } else {
256  m.add(chain.second->type().variable());
257  _marginal(chain, m);
258  }
259  }
260  }
261 
262  template < typename GUM_SCALAR >
264  const std::vector< typename PRMInference< GUM_SCALAR >::Chain >& chains,
266  if (j.nbrDim() > 0) {
267  GUM_ERROR(OperationNotAllowed, "the given Potential is not empty.");
268  }
269 
270  for (auto chain = chains.begin(); chain != chains.end(); ++chain) {
271  j.add(chain->second->type().variable());
272  }
273 
274  _joint(chains, j);
275  }
276 
277  } /* namespace prm */
278 } /* namespace gum */
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:60
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
virtual GUM_SCALAR get(const Instantiation &i) const final
Default implementation of MultiDimContainer::get().
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> __evidences
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:235
An PRMInstance is a Bayesian Network fragment defined by a Class and used in a PRMSystem.
Definition: PRMInstance.h:63
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
PRMInference(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default constructor.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
PRM< GUM_SCALAR > const * _prm
The PRM<GUM_SCALAR> on which inference is done.
Definition: PRMInference.h:213
The class for generic Hash Tables.
Definition: hashTable.h:679
void inc()
Operator increment.
void clearEvidence()
Remove all evidences.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
A PRMSystem is a container of PRMInstance and describe a relational skeleton.
Definition: PRMObject.h:229
PRMSystem< GUM_SCALAR > const * _sys
The Model on which inference is done.
Definition: PRMInference.h:216
std::pair< const PRMInstance< double > *, const PRMAttribute< double > * > Chain
Code alias.
Definition: PRMInference.h:57
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
virtual void add(const DiscreteVariable &v) final
Adds a new var to the variables of the multidimensional matrix.
This abstract class is used as base class for all inference class on PRM<GUM_SCALAR>.
Definition: PRMInference.h:52
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition: PRM.h:66
virtual void set(const Instantiation &i, const GUM_SCALAR &value) const final
Default implementation of MultiDimContainer::set().
void setFirst()
Assign the first values to the tuple of the Instantiation.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
virtual bool contains(const DiscreteVariable &var) const final
Returns true if var is in *this.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
bool end() const
Returns true if the Instantiation reached the end.