aGrUM 3.0.0
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.
 ~GroundedInference () override
 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.
std::string name () const override
 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.
void evidenceAdded_ (const typename PRMInference< GUM_SCALAR >::Chain &chain) override
 This method is called whenever an evidence is added, but AFTER any processing made by PRMInference.
void evidenceRemoved_ (const typename PRMInference< GUM_SCALAR >::Chain &chain) override
 This method is called whenever an evidence is removed, but BEFORE any processing made by PRMInference.
void posterior_ (const typename PRMInference< GUM_SCALAR >::Chain &chain, Tensor< GUM_SCALAR > &m) override
 Generic method to compute the marginal of given element.
void joint_ (const std::vector< typename PRMInference< GUM_SCALAR >::Chain > &queries, Tensor< GUM_SCALAR > &j) override
 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<GUM_Numeric 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<GUM_Numeric 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<GUM_Numeric 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<GUM_Numeric 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<GUM_Numeric 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<GUM_Numeric 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: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
privateinherited
Initial value:

Code alias.

Definition at line 238 of file PRMInference.h.

Constructor & Destructor Documentation

◆ GroundedInference() [1/2]

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

Default constructor.

Definition at line 108 of file groundedInference_tpl.h.

109 :
112 }
<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<GUM_Numeric GUM_SCALAR>
gum::prm::GroundedInference< GUM_SCALAR >::~GroundedInference ( )
override

Destructor.

Definition at line 57 of file groundedInference_tpl.h.

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

References GroundedInference(), _inf_, and _obs_.

Here is the call graph for this function:

◆ GroundedInference() [2/2]

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

Copy constructor.

Definition at line 115 of file groundedInference_tpl.h.

116 :
119 GUM_ERROR(FatalError, "illegal to copy constructor")
120 }
#define GUM_ERROR(type, msg)
Definition exceptions.h:76

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

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

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

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
inherited

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

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
inherited

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

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

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

Definition at line 71 of file groundedInference_tpl.h.

72 {
74 // Retrieving the BN's variable
75 auto var_name = std::format("{}.{}", chain.first->name(), chain.second->safeName());
76 bn_obs->add(_inf_->BN().variableFromName(var_name));
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 }
std::string name() const override
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<GUM_Numeric 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<GUM_Numeric GUM_SCALAR>
void gum::prm::GroundedInference< GUM_SCALAR >::evidenceRemoved_ ( const typename PRMInference< GUM_SCALAR >::Chain & chain)
overrideprotected

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<GUM_Numeric 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<GUM_Numeric GUM_SCALAR>
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 129 of file groundedInference_tpl.h.

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

References _inf_, and GUM_ERROR.

◆ hasEvidence() [1/4]

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

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
inherited

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.

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
inherited

◆ hasEvidence() [4/4]

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

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

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

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 166 of file groundedInference_tpl.h.

168 {
169 GUM_ERROR(FatalError, "not yet implemented")
170 }

References GUM_ERROR.

◆ joint_() [2/2]

template<GUM_Numeric 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<GUM_Numeric GUM_SCALAR>
std::string gum::prm::GroundedInference< GUM_SCALAR >::name ( ) const
overridevirtual

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 173 of file groundedInference_tpl.h.

173 {
174 return "grounded inference";
175 }

◆ operator=()

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

Copy operator.

Definition at line 124 of file groundedInference_tpl.h.

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

References GroundedInference(), and GUM_ERROR.

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

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

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 146 of file groundedInference_tpl.h.

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

References _inf_, _obs_, and GUM_ERROR.

◆ posterior_() [2/2]

template<GUM_Numeric 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<GUM_Numeric GUM_SCALAR>
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 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:

◆ setBNInference()

template<GUM_Numeric GUM_SCALAR>
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 138 of file groundedInference_tpl.h.

139 {
140 if (_inf_ != 0) { delete _inf_; }
141
142 _inf_ = bn_inf;
143 }

References _inf_.

Member Data Documentation

◆ _evidences_

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

◆ _inf_

template<GUM_Numeric 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<GUM_Numeric 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: