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

<agrum/PRM/groundedInference.h> More...

#include <groundedInference.h>

Inheritance diagram for gum::prm::GroundedInference< GUM_SCALAR >:
Collaboration diagram for gum::prm::GroundedInference< 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.
 GroundedInference (const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
 Default constructor.
virtual ~GroundedInference ()
 Destructor.
Getters & setters.
MarginalTargetedInference< GUM_SCALAR > & getBNInference ()
 Returns the bayesnet inference engine used by this class.
void setBNInference (MarginalTargetedInference< GUM_SCALAR > *bn_inf)
 Defines the bayesnet inference engine used by this class.
virtual std::string name () const
 Returns the bayesnet inference engine used by this class.
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.

Protected Member Functions

Private evidence handling methods and members.
virtual void evidenceAdded_ (const typename PRMInference< GUM_SCALAR >::Chain &chain)
 This method is called whenever an evidence is added, but AFTER any processing made by PRMInference.
virtual void evidenceRemoved_ (const typename PRMInference< GUM_SCALAR >::Chain &chain)
 This method is called whenever an evidence is removed, but BEFORE any processing made by PRMInference.
virtual void posterior_ (const typename PRMInference< GUM_SCALAR >::Chain &chain, Tensor< GUM_SCALAR > &m)
 Generic method to compute the marginal of given element.
virtual void joint_ (const std::vector< typename PRMInference< GUM_SCALAR >::Chain > &queries, Tensor< GUM_SCALAR > &j)
 Generic method to compute the marginal of given element.

Private Member Functions

 GroundedInference (const GroundedInference &source)
 Copy constructor.
GroundedInferenceoperator= (const GroundedInference &source)
 Copy operator.

Private Attributes

MarginalTargetedInference< GUM_SCALAR > * _inf_
 The bayesnet inference engine used by this class.
List< const Tensor< GUM_SCALAR > * > _obs_

Private evidence handling methods and members.

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.
using EvidenceIterator
 Code alias.
using EvidenceConstIterator
 Code alias.

Protected members.

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.
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.

Detailed Description

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

<agrum/PRM/groundedInference.h>

This class is used to realise grounded inference in a PRM<GUM_SCALAR>.

The best way to build this class is to use the static creation methods.

Definition at line 66 of file groundedInference.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 >* >
inherited

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 >* >
inherited

Code alias.

Definition at line 74 of file PRMInference.h.

◆ EMapConstIterator

template<typename GUM_SCALAR>
using gum::prm::PRMInference< GUM_SCALAR >::EMapConstIterator
inherited
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
inherited

Code alias.

Definition at line 77 of file PRMInference.h.

◆ EvidenceConstIterator

template<typename GUM_SCALAR>
using gum::prm::PRMInference< GUM_SCALAR >::EvidenceConstIterator
privateinherited
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
privateinherited
Initial value:

Code alias.

Definition at line 238 of file PRMInference.h.

Constructor & Destructor Documentation

◆ GroundedInference() [1/2]

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

Default constructor.

Definition at line 109 of file groundedInference_tpl.h.

110 :
113 }
<agrum/PRM/groundedInference.h>
MarginalTargetedInference< GUM_SCALAR > * _inf_
The bayesnet inference engine used by this class.
GroundedInference(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default constructor.
PRMInference(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default constructor.

References GroundedInference(), gum::prm::PRMInference< GUM_SCALAR >::PRMInference(), and _inf_.

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

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

◆ ~GroundedInference()

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

Destructor.

Definition at line 56 of file groundedInference_tpl.h.

56 {
58
59 if (_inf_ != nullptr) delete _inf_;
60
61 if (!_obs_.empty())
62 for (const auto pot: _obs_)
63 // We used const ptrs only because of
64 // MarginalTargetedInference::addEvidence()
65 // requires it
66 delete const_cast< Tensor< GUM_SCALAR >* >(pot);
67 }
List< const Tensor< GUM_SCALAR > * > _obs_

References GroundedInference(), _inf_, and _obs_.

Here is the call graph for this function:

◆ GroundedInference() [2/2]

template<typename GUM_SCALAR>
INLINE gum::prm::GroundedInference< GUM_SCALAR >::GroundedInference ( const GroundedInference< GUM_SCALAR > & source)
private

Copy constructor.

Definition at line 116 of file groundedInference_tpl.h.

117 :
120 GUM_ERROR(FatalError, "illegal to copy constructor")
121 }
#define GUM_ERROR(type, msg)
Definition exceptions.h:72

References GroundedInference(), gum::prm::PRMInference< GUM_SCALAR >::PRMInference(), _inf_, and GUM_ERROR.

Here is the call graph for this function:

Member Function Documentation

◆ _EMap_()

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

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 }
This abstract class is used as base class for all inference class on PRM<GUM_SCALAR>.
HashTable< const PRMInstance< GUM_SCALAR > *, EMap * > _evidences_
Mapping of evidence over PRMInstance<GUM_SCALAR>'s nodes.

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 )
inherited

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.

◆ clearEvidence()

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

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)
inherited

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
inherited

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)
inherited

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
inherited

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_() [1/2]

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

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

Definition at line 70 of file groundedInference_tpl.h.

71 {
73 // Retrieving the BN's variable
75 var_name << chain.first->name() << "." << chain.second->safeName();
76 bn_obs->add(_inf_->BN().variableFromName(var_name.str()));
77 // Retrievin the PRM<GUM_SCALAR>'s evidence and copying it in bn_obs
78 const Tensor< GUM_SCALAR >* prm_obs = this->evidence(chain.first)[chain.second->id()];
80
81 for (i.setFirst(), j.setFirst(); !i.end(); i.inc(), j.inc()) {
82 bn_obs->set(i, prm_obs->get(j));
83 }
84
85 _obs_.insert(bn_obs);
86 }
virtual std::string name() const
Returns the bayesnet inference engine used by this class.
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.

References _inf_, _obs_, gum::prm::PRMInference< GUM_SCALAR >::evidence(), gum::Instantiation::inc(), and gum::Instantiation::setFirst().

Here is the call graph for this function:

◆ evidenceAdded_() [2/2]

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

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_() [1/2]

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

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

Definition at line 89 of file groundedInference_tpl.h.

90 {
92 var_name << chain.first->name() << "." << chain.second->safeName();
93 const DiscreteVariable& var = _inf_->BN().variableFromName(var_name.str());
94
95 for (auto iter = _obs_.beginSafe(); iter != _obs_.endSafe();
96 ++iter) { // safe iterator needed here
97 if ((**iter).contains(var)) {
98 _inf_->eraseEvidence(var_name.str());
99 const Tensor< GUM_SCALAR >* e = *iter;
100 _obs_.erase(iter);
101 delete e;
102 break;
103 }
104 }
105 }

References _inf_, and _obs_.

◆ evidenceRemoved_() [2/2]

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

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:

◆ getBNInference()

template<typename GUM_SCALAR>
INLINE MarginalTargetedInference< GUM_SCALAR > & gum::prm::GroundedInference< GUM_SCALAR >::getBNInference ( )

Returns the bayesnet inference engine used by this class.

Returns
the bayesnet inference engine used by this class.
Exceptions
NotFoundRaised if no inference engine have been defined for this class.

Definition at line 131 of file groundedInference_tpl.h.

131 {
132 if (_inf_ != 0) {
133 return *_inf_;
134 } else {
135 GUM_ERROR(NotFound, "the inference engine is not yet defined")
136 }
137 }

References _inf_, and GUM_ERROR.

◆ hasEvidence() [1/4]

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

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
inherited

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.
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
inherited

◆ hasEvidence() [4/4]

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

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 )
inherited

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_() [1/2]

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

Generic method to compute the marginal of given element.

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

Definition at line 168 of file groundedInference_tpl.h.

170 {
171 GUM_ERROR(FatalError, "not yet implemented")
172 }

References GUM_ERROR.

◆ joint_() [2/2]

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

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>
INLINE std::string gum::prm::GroundedInference< GUM_SCALAR >::name ( ) const
virtual

Returns the bayesnet inference engine used by this class.

Returns
the bayesnet inference engine used by this class.
Exceptions
NotFoundRaised if no inference engine have been defined for this class.

Implements gum::prm::PRMInference< GUM_SCALAR >.

Definition at line 175 of file groundedInference_tpl.h.

175 {
176 return "grounded inference";
177 }

◆ operator=()

template<typename GUM_SCALAR>
INLINE GroundedInference< GUM_SCALAR > & gum::prm::GroundedInference< GUM_SCALAR >::operator= ( const GroundedInference< GUM_SCALAR > & source)
private

Copy operator.

Definition at line 125 of file groundedInference_tpl.h.

125 {
126 GUM_ERROR(FatalError, "illegal call to copy operator")
127 }

References GroundedInference(), and GUM_ERROR.

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 )
inherited

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_() [1/2]

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

Generic method to compute the marginal of given element.

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

Definition at line 148 of file groundedInference_tpl.h.

150 {
151 if (_inf_ == 0) { GUM_ERROR(OperationNotAllowed, "no inference engine defined") }
152
154
155 if (!_obs_.empty()) {
156 for (auto e: _obs_) {
157 try {
158 _inf_->addEvidence(*e);
159 } catch (InvalidArgument const&) { _inf_->chgEvidence(*e); }
160 }
161 }
162
163 sBuff << chain.first->name() << "." << chain.second->safeName();
164 m = _inf_->posterior(_inf_->BN().idFromName(sBuff.str()));
165 }

References _inf_, _obs_, and GUM_ERROR.

◆ posterior_() [2/2]

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

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)
inherited

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:

◆ setBNInference()

template<typename GUM_SCALAR>
INLINE void gum::prm::GroundedInference< GUM_SCALAR >::setBNInference ( MarginalTargetedInference< GUM_SCALAR > * bn_inf)

Defines the bayesnet inference engine used by this class.

The inference engine is given to this class, it will be deleted when ~GroundedInference() is called.

Parameters
bn_infThe bayesnet inference engine used by this class.
Exceptions
OperationNotAllowedIf bn_inf does not inference over the SystemBayesNet of this class.
Todo
MarginalTargetedInference should have copy constructors.

Definition at line 140 of file groundedInference_tpl.h.

141 {
142 if (_inf_ != 0) { delete _inf_; }
143
144 _inf_ = bn_inf;
145 }

References _inf_.

Member Data Documentation

◆ _evidences_

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

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().

◆ _inf_

template<typename GUM_SCALAR>
MarginalTargetedInference< GUM_SCALAR >* gum::prm::GroundedInference< GUM_SCALAR >::_inf_
private

The bayesnet inference engine used by this class.

Definition at line 148 of file groundedInference.h.

Referenced by GroundedInference(), GroundedInference(), ~GroundedInference(), evidenceAdded_(), evidenceRemoved_(), getBNInference(), posterior_(), and setBNInference().

◆ _obs_

template<typename GUM_SCALAR>
List< const Tensor< GUM_SCALAR >* > gum::prm::GroundedInference< GUM_SCALAR >::_obs_
private

◆ prm_

◆ sys_


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