aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
gum::prm::PRMInference< GUM_SCALAR > Class Template Referenceabstract

This abstract class is used as base class for all inference class on PRM<GUM_SCALAR>. More...

#include <agrum/PRM/PRMInference.h>

Inheritance diagram for gum::prm::PRMInference< GUM_SCALAR >:
Collaboration diagram for gum::prm::PRMInference< GUM_SCALAR >:

Public Types

using Chain = std::pair< const PRMInstance< GUM_SCALAR >*, const PRMAttribute< GUM_SCALAR >* >
 Code alias.
using EMap = NodeProperty< const Tensor< GUM_SCALAR >* >
 Code alias.
using EMapIterator = typename NodeProperty< const Tensor< GUM_SCALAR >* >::iterator_safe
 Code alias.
using EMapConstIterator
 Code alias.

Public Member Functions

Constructor & destructor.
 PRMInference (const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
 Default constructor.
 PRMInference (const PRMInference &source)
 Copy constructor.
virtual ~PRMInference ()
 Destructor.
PRMInferenceoperator= (const PRMInference &source)
 Copy operator.
Getters & setters.
virtual std::string name () const =0
 Returns the name of the current inference algorithm.
Query methods.
void posterior (const Chain &chain, Tensor< GUM_SCALAR > &m)
 Compute the posterior of the formal attribute pointed by chain and stores it in m.
void joint (const std::vector< Chain > &chains, Tensor< GUM_SCALAR > &j)
 Compute the joint probability of the formals attributes pointed by chains and stores it in m.
Evidence handling.
EMapevidence (const PRMInstance< GUM_SCALAR > &i)
 Returns EMap of evidences over i.
EMapevidence (const PRMInstance< GUM_SCALAR > *i)
 Returns EMap of evidences over i.
const EMapevidence (const PRMInstance< GUM_SCALAR > &i) const
 Returns EMap of evidences over i.
const EMapevidence (const PRMInstance< GUM_SCALAR > *i) const
 Returns EMap of evidences over i.
bool hasEvidence (const PRMInstance< GUM_SCALAR > &i) const
 Returns true if i has evidence.
bool hasEvidence (const PRMInstance< GUM_SCALAR > *i) const
 Returns EMap of evidences over i.
bool hasEvidence (const Chain &chain) const
 Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
bool hasEvidence () const
 Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
void addEvidence (const Chain &chain, const Tensor< GUM_SCALAR > &p)
 Add an evidence to the given instance's elt.
void removeEvidence (const Chain &chain)
 Remove evidence on the given instance's elt.
void clearEvidence ()
 Remove all evidences.

Private evidence handling methods and members.

using EvidenceIterator
 Code alias.
using EvidenceConstIterator
 Code alias.
HashTable< const PRMInstance< GUM_SCALAR > *, EMap * > _evidences_
 Mapping of evidence over PRMInstance<GUM_SCALAR>'s nodes.
EMap_EMap_ (const PRMInstance< GUM_SCALAR > *i)
 Private getter over evidences, if necessary creates an EMap for i.

Protected members.

PRM< GUM_SCALAR > const * prm_
 The PRM<GUM_SCALAR> on which inference is done.
PRMSystem< GUM_SCALAR > const * sys_
 The Model on which inference is done.
virtual void evidenceAdded_ (const Chain &chain)=0
 This method is called whenever an evidence is added, but AFTER any processing made by PRMInference.
virtual void evidenceRemoved_ (const Chain &chain)=0
 This method is called whenever an evidence is removed, but BEFORE any processing made by PRMInference.
virtual void posterior_ (const Chain &chain, Tensor< GUM_SCALAR > &m)=0
 Generic method to compute the posterior of given element.
virtual void joint_ (const std::vector< Chain > &queries, Tensor< GUM_SCALAR > &j)=0
 Generic method to compute the posterior of given element.

Detailed Description

template<typename GUM_SCALAR>
class gum::prm::PRMInference< GUM_SCALAR >

This abstract class is used as base class for all inference class on PRM<GUM_SCALAR>.

The main purpose of this class is to give a common interface between all inference algorithms and to handle evidences.

Definition at line 68 of file PRMInference.h.

Member Typedef Documentation

◆ Chain

template<typename GUM_SCALAR>
using gum::prm::PRMInference< GUM_SCALAR >::Chain = std::pair< const PRMInstance< GUM_SCALAR >*, const PRMAttribute< GUM_SCALAR >* >

Code alias.

Definition at line 71 of file PRMInference.h.

◆ EMap

template<typename GUM_SCALAR>
using gum::prm::PRMInference< GUM_SCALAR >::EMap = NodeProperty< const Tensor< GUM_SCALAR >* >

Code alias.

Definition at line 74 of file PRMInference.h.

◆ EMapConstIterator

template<typename GUM_SCALAR>
using gum::prm::PRMInference< GUM_SCALAR >::EMapConstIterator
Initial value:
typename NodeProperty< const Tensor< GUM_SCALAR >* >::const_iterator_safe
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.

Code alias.

Definition at line 80 of file PRMInference.h.

◆ EMapIterator

template<typename GUM_SCALAR>
using gum::prm::PRMInference< GUM_SCALAR >::EMapIterator = typename NodeProperty< const Tensor< GUM_SCALAR >* >::iterator_safe

Code alias.

Definition at line 77 of file PRMInference.h.

◆ EvidenceConstIterator

template<typename GUM_SCALAR>
using gum::prm::PRMInference< GUM_SCALAR >::EvidenceConstIterator
private
Initial value:
typename HashTable< const PRMInstance< GUM_SCALAR >*, EMap* >::const_iterator_safe
The class for generic Hash Tables.
Definition hashTable.h:637
NodeProperty< const Tensor< GUM_SCALAR > * > EMap
Code alias.

Code alias.

Definition at line 241 of file PRMInference.h.

◆ EvidenceIterator

template<typename GUM_SCALAR>
using gum::prm::PRMInference< GUM_SCALAR >::EvidenceIterator
private
Initial value:

Code alias.

Definition at line 238 of file PRMInference.h.

Constructor & Destructor Documentation

◆ PRMInference() [1/2]

template<typename GUM_SCALAR>
INLINE gum::prm::PRMInference< GUM_SCALAR >::PRMInference ( const PRM< GUM_SCALAR > & prm,
const PRMSystem< GUM_SCALAR > & system )

Default constructor.

Definition at line 157 of file PRMInference_tpl.h.

158 :
159 prm_(&prm), sys_(&system) {
161 }
This abstract class is used as base class for all inference class on PRM<GUM_SCALAR>.
PRMInference(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default constructor.
PRMSystem< GUM_SCALAR > const * sys_
The Model on which inference is done.
PRM< GUM_SCALAR > const * prm_
The PRM<GUM_SCALAR> on which inference is done.

References PRMInference(), prm_, and sys_.

Referenced by gum::prm::GroundedInference< GUM_SCALAR >::GroundedInference(), gum::prm::GroundedInference< GUM_SCALAR >::GroundedInference(), PRMInference(), PRMInference(), gum::prm::StructuredInference< GUM_SCALAR >::StructuredInference(), gum::prm::StructuredInference< GUM_SCALAR >::StructuredInference(), gum::prm::SVE< GUM_SCALAR >::SVE(), gum::prm::SVED< GUM_SCALAR >::SVED(), and operator=().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ PRMInference() [2/2]

template<typename GUM_SCALAR>
gum::prm::PRMInference< GUM_SCALAR >::PRMInference ( const PRMInference< GUM_SCALAR > & source)

Copy constructor.

Definition at line 68 of file PRMInference_tpl.h.

68 :
71
72 for (const auto& elt: source._evidences_) {
74
75 for (const auto& elt2: *elt.second) {
77 e->add(*(elt2.second->variablesSequence().front()));
79
80 for (i.setFirst(); !i.end(); i.inc())
81 e->set(i, elt2.second->get(i));
82
83 _evidences_[elt.first]->insert(elt2.first, e);
84 }
85 }
86 }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap * > _evidences_
Mapping of evidence over PRMInstance<GUM_SCALAR>'s nodes.

References PRMInference(), _evidences_, gum::Instantiation::end(), gum::Instantiation::inc(), prm_, gum::Instantiation::setFirst(), and sys_.

Here is the call graph for this function:

◆ ~PRMInference()

template<typename GUM_SCALAR>
INLINE gum::prm::PRMInference< GUM_SCALAR >::~PRMInference ( )
virtual

Destructor.

Definition at line 164 of file PRMInference_tpl.h.

164 {
167 }
void clearEvidence()
Remove all evidences.

Member Function Documentation

◆ _EMap_()

template<typename GUM_SCALAR>
PRMInference< GUM_SCALAR >::EMap & gum::prm::PRMInference< GUM_SCALAR >::_EMap_ ( const PRMInstance< GUM_SCALAR > * i)
private

Private getter over evidences, if necessary creates an EMap for i.

Definition at line 116 of file PRMInference_tpl.h.

116 {
117 if (_evidences_.exists(i)) {
118 return *(_evidences_[i]);
119 } else {
121 return *(_evidences_[i]);
122 }
123 }

References _evidences_.

Referenced by removeEvidence().

Here is the caller graph for this function:

◆ addEvidence()

template<typename GUM_SCALAR>
void gum::prm::PRMInference< GUM_SCALAR >::addEvidence ( const Chain & chain,
const Tensor< GUM_SCALAR > & p )

Add an evidence to the given instance's elt.

Parameters
chainThe variable being observed.
pThe Tensor added (by copy) as evidence.
Exceptions
NotFoundRaised if elt does not belong to i.
OperationNotAllowedRaised if p is inconsistent with elt.

Definition at line 126 of file PRMInference_tpl.h.

127 {
128 if (chain.first->exists(chain.second->id())) {
129 if ((p.nbrDim() != 1) || (!p.contains(chain.second->type().variable())))
130 GUM_ERROR(OperationNotAllowed, "illegal evidence for the given PRMAttribute.")
131
133 e->add(chain.second->type().variable());
134 Instantiation i(*e);
135
136 for (i.setFirst(); !i.end(); i.inc())
137 e->set(i, p.get(i));
138
140
141 if (emap.exists(chain.second->id())) {
142 delete emap[chain.second->id()];
143 emap[chain.second->id()] = e;
144 } else {
145 emap.insert(chain.second->id(), e);
146 }
147
149 } else {
151 "the given PRMAttribute does not belong to this "
152 "Instance<GUM_SCALAR>.");
153 }
154 }
EMap & _EMap_(const PRMInstance< GUM_SCALAR > *i)
Private getter over evidences, if necessary creates an EMap for i.
virtual void evidenceAdded_(const Chain &chain)=0
This method is called whenever an evidence is added, but AFTER any processing made by PRMInference.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72

◆ clearEvidence()

template<typename GUM_SCALAR>
void gum::prm::PRMInference< GUM_SCALAR >::clearEvidence ( )

Remove all evidences.

Definition at line 56 of file PRMInference_tpl.h.

56 {
57 for (const auto& elt: _evidences_) {
58 for (const auto& elt2: *elt.second)
59 delete elt2.second;
60
61 delete elt.second;
62 }
63
64 _evidences_.clear();
65 }

References _evidences_.

Referenced by operator=().

Here is the caller graph for this function:

◆ evidence() [1/4]

template<typename GUM_SCALAR>
INLINE PRMInference< GUM_SCALAR >::EMap & gum::prm::PRMInference< GUM_SCALAR >::evidence ( const PRMInstance< GUM_SCALAR > & i)

Returns EMap of evidences over i.

Exceptions
NotFoundif i has no evidence.

Definition at line 171 of file PRMInference_tpl.h.

171 {
172 try {
173 return *(_evidences_[&i]);
174 } catch (NotFound const&) { GUM_ERROR(NotFound, "this instance has no evidence.") }
175 }

References _evidences_, and GUM_ERROR.

Referenced by gum::prm::SVE< GUM_SCALAR >::_eliminateNodesWithEvidence_(), gum::prm::SVED< GUM_SCALAR >::_eliminateNodesWithEvidence_(), gum::prm::SVE< GUM_SCALAR >::_insertEvidence_(), gum::prm::SVED< GUM_SCALAR >::_insertEvidence_(), gum::prm::StructuredInference< GUM_SCALAR >::_reduceAloneInstances_(), gum::prm::GroundedInference< GUM_SCALAR >::evidenceAdded_(), hasEvidence(), posterior(), and gum::prm::StructuredInference< GUM_SCALAR >::posterior_().

Here is the caller graph for this function:

◆ evidence() [2/4]

template<typename GUM_SCALAR>
INLINE const PRMInference< GUM_SCALAR >::EMap & gum::prm::PRMInference< GUM_SCALAR >::evidence ( const PRMInstance< GUM_SCALAR > & i) const

Returns EMap of evidences over i.

Exceptions
NotFoundif i has no evidence.

Definition at line 179 of file PRMInference_tpl.h.

179 {
180 try {
181 return *(_evidences_[&i]);
182 } catch (NotFound const&) { GUM_ERROR(NotFound, "this instance has no evidence.") }
183 }

References _evidences_.

◆ evidence() [3/4]

template<typename GUM_SCALAR>
INLINE PRMInference< GUM_SCALAR >::EMap & gum::prm::PRMInference< GUM_SCALAR >::evidence ( const PRMInstance< GUM_SCALAR > * i)

Returns EMap of evidences over i.

Exceptions
NotFoundif i has no evidence.

Definition at line 187 of file PRMInference_tpl.h.

187 {
188 try {
189 return *(_evidences_[i]);
190 } catch (NotFound const&) { GUM_ERROR(NotFound, "this instance has no evidence.") }
191 }

◆ evidence() [4/4]

template<typename GUM_SCALAR>
INLINE const PRMInference< GUM_SCALAR >::EMap & gum::prm::PRMInference< GUM_SCALAR >::evidence ( const PRMInstance< GUM_SCALAR > * i) const

Returns EMap of evidences over i.

Exceptions
NotFoundif i has no evidence.

Definition at line 195 of file PRMInference_tpl.h.

195 {
196 try {
197 return *(_evidences_[i]);
198 } catch (NotFound const&) { GUM_ERROR(NotFound, "this instance has no evidence.") }
199 }

References _evidences_, and GUM_ERROR.

◆ evidenceAdded_()

template<typename GUM_SCALAR>
virtual void gum::prm::PRMInference< GUM_SCALAR >::evidenceAdded_ ( const Chain & chain)
protectedpure virtual

This method is called whenever an evidence is added, but AFTER any processing made by PRMInference.

Implemented in gum::prm::SVE< GUM_SCALAR >, and gum::prm::SVED< GUM_SCALAR >.

◆ evidenceRemoved_()

template<typename GUM_SCALAR>
virtual void gum::prm::PRMInference< GUM_SCALAR >::evidenceRemoved_ ( const Chain & chain)
protectedpure virtual

This method is called whenever an evidence is removed, but BEFORE any processing made by PRMInference.

Implemented in gum::prm::SVE< GUM_SCALAR >, and gum::prm::SVED< GUM_SCALAR >.

Referenced by removeEvidence().

Here is the caller graph for this function:

◆ hasEvidence() [1/4]

template<typename GUM_SCALAR>
INLINE bool gum::prm::PRMInference< GUM_SCALAR >::hasEvidence ( ) const

Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.

Definition at line 217 of file PRMInference_tpl.h.

217 {
218 return (_evidences_.size() != (Size)0);
219 }

References _evidences_.

◆ hasEvidence() [2/4]

template<typename GUM_SCALAR>
INLINE bool gum::prm::PRMInference< GUM_SCALAR >::hasEvidence ( const Chain & chain) const

Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.

Definition at line 212 of file PRMInference_tpl.h.

212 {
213 return (hasEvidence(chain.first)) ? evidence(chain.first).exists(chain.second->id()) : false;
214 }
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
bool hasEvidence() const
Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.

References evidence(), and hasEvidence().

Here is the call graph for this function:

◆ hasEvidence() [3/4]

template<typename GUM_SCALAR>
INLINE bool gum::prm::PRMInference< GUM_SCALAR >::hasEvidence ( const PRMInstance< GUM_SCALAR > & i) const

◆ hasEvidence() [4/4]

template<typename GUM_SCALAR>
INLINE bool gum::prm::PRMInference< GUM_SCALAR >::hasEvidence ( const PRMInstance< GUM_SCALAR > * i) const

Returns EMap of evidences over i.

Definition at line 207 of file PRMInference_tpl.h.

207 {
208 return _evidences_.exists(i);
209 }

References _evidences_.

◆ joint()

template<typename GUM_SCALAR>
INLINE void gum::prm::PRMInference< GUM_SCALAR >::joint ( const std::vector< Chain > & chains,
Tensor< GUM_SCALAR > & j )

Compute the joint probability of the formals attributes pointed by chains and stores it in m.

Parameters
chainsA Set of strings of the form instance.attribute.
jAn empty CPF which will be filed by the joint probability over chains.
Exceptions
NotFoundRaised if some chain in chains does not point to a formal attribute.
OperationNotAllowedRaise if m is not empty.

Definition at line 261 of file PRMInference_tpl.h.

263 {
264 if (j.nbrDim() > 0) { GUM_ERROR(OperationNotAllowed, "the given Tensor is not empty.") }
265
266 for (auto chain = chains.begin(); chain != chains.end(); ++chain) {
267 j.add(chain->second->type().variable());
268 }
269
270 joint_(chains, j);
271 }
virtual void joint_(const std::vector< Chain > &queries, Tensor< GUM_SCALAR > &j)=0
Generic method to compute the posterior of given element.

References GUM_ERROR, and joint_().

Here is the call graph for this function:

◆ joint_()

template<typename GUM_SCALAR>
virtual void gum::prm::PRMInference< GUM_SCALAR >::joint_ ( const std::vector< Chain > & queries,
Tensor< GUM_SCALAR > & j )
protectedpure virtual

Generic method to compute the posterior of given element.

Parameters
queriesSet of pairs of PRMInstance<GUM_SCALAR> and PRMAttribute<GUM_SCALAR>.
jCPF filled with the joint probability of queries. It is initialized properly.

Implemented in gum::prm::SVE< GUM_SCALAR >, and gum::prm::SVED< GUM_SCALAR >.

Referenced by joint().

Here is the caller graph for this function:

◆ name()

template<typename GUM_SCALAR>
virtual std::string gum::prm::PRMInference< GUM_SCALAR >::name ( ) const
pure virtual

◆ operator=()

template<typename GUM_SCALAR>
PRMInference< GUM_SCALAR > & gum::prm::PRMInference< GUM_SCALAR >::operator= ( const PRMInference< GUM_SCALAR > & source)

Copy operator.

Definition at line 90 of file PRMInference_tpl.h.

90 {
94
95 for (const auto& elt: source._evidences_) {
97
98 for (const auto& elt2: *elt.second) {
100 e->add(*(elt2.second->variablesSequence().front()));
101 Instantiation i(*e);
102
103 for (i.setFirst(); !i.end(); i.inc()) {
104 e->set(i, elt2.second->get(i));
105 }
106
107 _evidences_[elt.first]->insert(elt2.first, e);
108 }
109 }
110
111 return *this;
112 }

References PRMInference(), clearEvidence(), prm_, and sys_.

Here is the call graph for this function:

◆ posterior()

template<typename GUM_SCALAR>
INLINE void gum::prm::PRMInference< GUM_SCALAR >::posterior ( const Chain & chain,
Tensor< GUM_SCALAR > & m )

Compute the posterior of the formal attribute pointed by chain and stores it in m.

Parameters
chainA string of the form instance.attribute.
mAn empty CPF which will be filed by the posterior of chain.
Exceptions
NotFoundRaised if chain is invalid.
TypeErrorRaised if chain does not point to an PRMAttribute<GUM_SCALAR>.
OperationNotAllowedRaise if m is not empty.

Definition at line 235 of file PRMInference_tpl.h.

237 {
238 if (m.nbrDim() > 0) { GUM_ERROR(OperationNotAllowed, "the given Tensor is not empty.") }
239
240 if (hasEvidence(chain)) {
241 m.add(chain.second->type().variable());
242 const Tensor< GUM_SCALAR >& e = *(evidence(chain.first)[chain.second->id()]);
243 Instantiation i(m), j(e);
244
245 for (i.setFirst(), j.setFirst(); !i.end(); i.inc(), j.inc())
246 m.set(i, e.get(j));
247 } else {
248 if (chain.second != &(chain.first->get(chain.second->safeName()))) {
250 = std::make_pair(chain.first, &(chain.first->get(chain.second->safeName())));
251 m.add(good_chain.second->type().variable());
253 } else {
254 m.add(chain.second->type().variable());
256 }
257 }
258 }
virtual void posterior_(const Chain &chain, Tensor< GUM_SCALAR > &m)=0
Generic method to compute the posterior of given element.

References gum::prm::PRMInstance< GUM_SCALAR >::end(), evidence(), GUM_ERROR, hasEvidence(), gum::Instantiation::inc(), and gum::Instantiation::setFirst().

Here is the call graph for this function:

◆ posterior_()

template<typename GUM_SCALAR>
virtual void gum::prm::PRMInference< GUM_SCALAR >::posterior_ ( const Chain & chain,
Tensor< GUM_SCALAR > & m )
protectedpure virtual

Generic method to compute the posterior of given element.

Parameters
chain
mCPF filled with the posterior of elt. It is initialized properly.

Implemented in gum::prm::SVE< GUM_SCALAR >, and gum::prm::SVED< GUM_SCALAR >.

◆ removeEvidence()

template<typename GUM_SCALAR>
INLINE void gum::prm::PRMInference< GUM_SCALAR >::removeEvidence ( const Chain & chain)

Remove evidence on the given instance's elt.

Parameters
chainThe variable being observed.
Exceptions
NotFoundRaised if the given names are not found.
TypeErrorRaised if the elt is not an PRMAttribute<GUM_SCALAR>.

Definition at line 222 of file PRMInference_tpl.h.

222 {
223 try {
224 if (_EMap_(chain.first).exists(chain.second->id())) {
226 delete _EMap_(chain.first)[chain.second->id()];
227 _EMap_(chain.first).erase(chain.second->id());
228 }
229 } catch (NotFound const&) {
230 // Ok, we are only removing
231 }
232 }
void erase(const Key &key)
Removes a given element from the hash table.
virtual void evidenceRemoved_(const Chain &chain)=0
This method is called whenever an evidence is removed, but BEFORE any processing made by PRMInference...

References _EMap_(), and evidenceRemoved_().

Here is the call graph for this function:

Member Data Documentation

◆ _evidences_

template<typename GUM_SCALAR>
HashTable< const PRMInstance< GUM_SCALAR >*, EMap* > gum::prm::PRMInference< GUM_SCALAR >::_evidences_
private

Mapping of evidence over PRMInstance<GUM_SCALAR>'s nodes.

Definition at line 245 of file PRMInference.h.

Referenced by PRMInference(), _EMap_(), clearEvidence(), evidence(), evidence(), evidence(), hasEvidence(), hasEvidence(), and hasEvidence().

◆ prm_

◆ sys_


The documentation for this class was generated from the following files: