aGrUM 3.1.1
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<GUM_Numeric 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<GUM_Numeric 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<GUM_Numeric 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<GUM_Numeric 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<GUM_Numeric 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<GUM_Numeric 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:640
NodeProperty< const Tensor< GUM_SCALAR > * > EMap
Code alias.

Code alias.

Definition at line 241 of file PRMInference.h.

◆ EvidenceIterator

template<GUM_Numeric 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<GUM_Numeric GUM_SCALAR>
gum::prm::PRMInference< GUM_SCALAR >::PRMInference ( const PRM< GUM_SCALAR > & prm,
const PRMSystem< GUM_SCALAR > & system )

Default constructor.

Definition at line 159 of file PRMInference_tpl.h.

160 :
161 prm_(&prm), sys_(&system) {
163 }
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(), ~PRMInference(), evidence(), and operator=().

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

◆ PRMInference() [2/2]

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

Copy constructor.

Definition at line 69 of file PRMInference_tpl.h.

69 :
72
73 for (const auto& elt: source._evidences_) {
75
76 for (const auto& elt2: *elt.second) {
78 e->add(*(elt2.second->variablesSequence().front()));
80
81 for (i.setFirst(); !i.end(); i.inc())
82 e->set(i, elt2.second->get(i));
83
84 _evidences_[elt.first]->insert(elt2.first, e);
85 }
86 }
87 }
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<GUM_Numeric GUM_SCALAR>
gum::prm::PRMInference< GUM_SCALAR >::~PRMInference ( )
virtual

Destructor.

Definition at line 166 of file PRMInference_tpl.h.

166 {
169 }
void clearEvidence()
Remove all evidences.

References PRMInference(), and clearEvidence().

Here is the call graph for this function:

Member Function Documentation

◆ _EMap_()

template<GUM_Numeric 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 117 of file PRMInference_tpl.h.

117 {
118 if (auto p = _evidences_.tryGet(i)) {
119 return **p;
120 } else {
122 _evidences_.insert(i, emap);
123 return *emap;
124 }
125 }

References _evidences_.

Referenced by removeEvidence().

Here is the caller graph for this function:

◆ addEvidence()

template<GUM_Numeric 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 128 of file PRMInference_tpl.h.

129 {
130 if (chain.first->exists(chain.second->id())) {
131 if ((p.nbrDim() != 1) || (!p.contains(chain.second->type().variable())))
132 GUM_ERROR(OperationNotAllowed, "illegal evidence for the given PRMAttribute.")
133
135 e->add(chain.second->type().variable());
136 Instantiation i(*e);
137
138 for (i.setFirst(); !i.end(); i.inc())
139 e->set(i, p.get(i));
140
142
143 if (emap.exists(chain.second->id())) {
144 delete emap[chain.second->id()];
145 emap[chain.second->id()] = e;
146 } else {
147 emap.insert(chain.second->id(), e);
148 }
149
151 } else {
153 "the given PRMAttribute does not belong to this "
154 "Instance<GUM_SCALAR>.");
155 }
156 }
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:76

References gum::Instantiation::end(), GUM_ERROR, gum::Instantiation::inc(), and gum::Instantiation::setFirst().

Here is the call graph for this function:

◆ clearEvidence()

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

Remove all evidences.

Definition at line 57 of file PRMInference_tpl.h.

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

References _evidences_.

Referenced by ~PRMInference(), and operator=().

Here is the caller graph for this function:

◆ evidence() [1/4]

template<GUM_Numeric GUM_SCALAR>
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 173 of file PRMInference_tpl.h.

173 {
174 if (!_evidences_.exists(&i)) GUM_ERROR(NotFound, "this instance has no evidence.")
175 return *(_evidences_[&i]);
176 }

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_(), evidence(), 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<GUM_Numeric GUM_SCALAR>
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 180 of file PRMInference_tpl.h.

180 {
181 if (!_evidences_.exists(&i)) GUM_ERROR(NotFound, "this instance has no evidence.")
182 return *(_evidences_[&i]);
183 }

References _evidences_, and GUM_ERROR.

◆ evidence() [3/4]

template<GUM_Numeric GUM_SCALAR>
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 if (!_evidences_.exists(i)) GUM_ERROR(NotFound, "this instance has no evidence.")
189 return *(_evidences_[i]);
190 }

References PRMInference(), _evidences_, evidence(), and GUM_ERROR.

Here is the call graph for this function:

◆ evidence() [4/4]

template<GUM_Numeric GUM_SCALAR>
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 194 of file PRMInference_tpl.h.

194 {
195 if (!_evidences_.exists(i)) GUM_ERROR(NotFound, "this instance has no evidence.")
196 return *(_evidences_[i]);
197 }

References _evidences_, and GUM_ERROR.

◆ evidenceAdded_()

template<GUM_Numeric 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<GUM_Numeric 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<GUM_Numeric GUM_SCALAR>
bool gum::prm::PRMInference< GUM_SCALAR >::hasEvidence ( ) const

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

Definition at line 215 of file PRMInference_tpl.h.

215 {
216 return (_evidences_.size() != (Size)0);
217 }

References _evidences_.

◆ hasEvidence() [2/4]

template<GUM_Numeric GUM_SCALAR>
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 210 of file PRMInference_tpl.h.

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

References evidence(), and hasEvidence().

Here is the call graph for this function:

◆ hasEvidence() [3/4]

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

◆ hasEvidence() [4/4]

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

Returns EMap of evidences over i.

Definition at line 205 of file PRMInference_tpl.h.

205 {
206 return _evidences_.exists(i);
207 }

References _evidences_.

◆ joint()

template<GUM_Numeric GUM_SCALAR>
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 257 of file PRMInference_tpl.h.

259 {
260 if (j.nbrDim() > 0) { GUM_ERROR(OperationNotAllowed, "the given Tensor is not empty.") }
261
262 for (auto chain = chains.begin(); chain != chains.end(); ++chain) {
263 j.add(chain->second->type().variable());
264 }
265
266 joint_(chains, j);
267 }
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<GUM_Numeric 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<GUM_Numeric GUM_SCALAR>
virtual std::string gum::prm::PRMInference< GUM_SCALAR >::name ( ) const
pure virtual

◆ operator=()

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

Copy operator.

Definition at line 91 of file PRMInference_tpl.h.

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

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

Here is the call graph for this function:

◆ posterior()

template<GUM_Numeric GUM_SCALAR>
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 231 of file PRMInference_tpl.h.

233 {
234 if (m.nbrDim() > 0) { GUM_ERROR(OperationNotAllowed, "the given Tensor is not empty.") }
235
236 if (hasEvidence(chain)) {
237 m.add(chain.second->type().variable());
238 const Tensor< GUM_SCALAR >& e = *(evidence(chain.first)[chain.second->id()]);
239 Instantiation i(m), j(e);
240
241 for (i.setFirst(), j.setFirst(); !i.end(); i.inc(), j.inc())
242 m.set(i, e.get(j));
243 } else {
244 if (chain.second != &(chain.first->get(chain.second->safeName()))) {
246 = std::make_pair(chain.first, &(chain.first->get(chain.second->safeName())));
247 m.add(good_chain.second->type().variable());
249 } else {
250 m.add(chain.second->type().variable());
252 }
253 }
254 }
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<GUM_Numeric 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<GUM_Numeric GUM_SCALAR>
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 220 of file PRMInference_tpl.h.

220 {
221 if (hasEvidence(chain.first)) {
222 if (_EMap_(chain.first).exists(chain.second->id())) {
224 delete _EMap_(chain.first)[chain.second->id()];
225 _EMap_(chain.first).erase(chain.second->id());
226 }
227 }
228 }
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_(), evidenceRemoved_(), and hasEvidence().

Here is the call graph for this function:

Member Data Documentation

◆ _evidences_

template<GUM_Numeric 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(), evidence(), hasEvidence(), hasEvidence(), and hasEvidence().

◆ prm_

◆ sys_


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