aGrUM  0.20.3
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 Member Functions

Constructor & destructor.
 GroundedInference (const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
 Default constructor. More...
 
virtual ~GroundedInference ()
 Destructor. More...
 
Getters & setters.
MarginalTargetedInference< GUM_SCALAR > & getBNInference ()
 Returns the bayesnet inference engine used by this class. More...
 
void setBNInference (MarginalTargetedInference< GUM_SCALAR > *bn_inf)
 Defines the bayesnet inference engine used by this class. More...
 
virtual std::string name () const
 Returns the bayesnet inference engine used by this class. More...
 
Query methods.
void posterior (const Chain &chain, Potential< GUM_SCALAR > &m)
 Compute the posterior of the formal attribute pointed by chain and stores it in m. More...
 
void joint (const std::vector< Chain > &chains, Potential< GUM_SCALAR > &j)
 Compute the joint probability of the formals attributes pointed by chains and stores it in m. More...
 
Evidence handling.
EMapevidence (const PRMInstance< GUM_SCALAR > &i)
 Returns EMap of evidences over i. More...
 
EMapevidence (const PRMInstance< GUM_SCALAR > *i)
 Returns EMap of evidences over i. More...
 
const EMapevidence (const PRMInstance< GUM_SCALAR > &i) const
 Returns EMap of evidences over i. More...
 
const EMapevidence (const PRMInstance< GUM_SCALAR > *i) const
 Returns EMap of evidences over i. More...
 
bool hasEvidence (const PRMInstance< GUM_SCALAR > &i) const
 Returns true if i has evidence. More...
 
bool hasEvidence (const PRMInstance< GUM_SCALAR > *i) const
 Returns EMap of evidences over i. More...
 
bool hasEvidence (const Chain &chain) const
 Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a. More...
 
bool hasEvidence () const
 Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a. More...
 
void addEvidence (const Chain &chain, const Potential< GUM_SCALAR > &p)
 Add an evidence to the given instance's elt. More...
 
void removeEvidence (const Chain &chain)
 Remove evidence on the given instance's elt. More...
 
void clearEvidence ()
 Remove all evidences. More...
 

Public Types

typedef std::pair< const PRMInstance< GUM_SCALAR > *, const PRMAttribute< GUM_SCALAR > *> Chain
 Code alias. More...
 
typedef NodeProperty< const Potential< GUM_SCALAR > *> EMap
 Code alias. More...
 
typedef NodeProperty< const Potential< GUM_SCALAR > *>::iterator_safe EMapIterator
 Code alias. More...
 
typedef NodeProperty< const Potential< GUM_SCALAR > *>::const_iterator_safe EMapConstIterator
 Code alias. More...
 

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. More...
 
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. More...
 
virtual void posterior_ (const typename PRMInference< GUM_SCALAR >::Chain &chain, Potential< GUM_SCALAR > &m)
 Generic method to compute the marginal of given element. More...
 
virtual void joint_ (const std::vector< typename PRMInference< GUM_SCALAR >::Chain > &queries, Potential< GUM_SCALAR > &j)
 Generic method to compute the marginal of given element. More...
 

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

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 46 of file groundedInference.h.

Member Typedef Documentation

◆ Chain

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

Code alias.

Definition at line 55 of file PRMInference.h.

◆ EMap

template<typename GUM_SCALAR>
typedef NodeProperty< const Potential< GUM_SCALAR >* > gum::prm::PRMInference< GUM_SCALAR >::EMap
inherited

Code alias.

Definition at line 58 of file PRMInference.h.

◆ EMapConstIterator

template<typename GUM_SCALAR>
typedef NodeProperty< const Potential< GUM_SCALAR >* >::const_iterator_safe gum::prm::PRMInference< GUM_SCALAR >::EMapConstIterator
inherited

Code alias.

Definition at line 65 of file PRMInference.h.

◆ EMapIterator

template<typename GUM_SCALAR>
typedef NodeProperty< const Potential< GUM_SCALAR >* >::iterator_safe gum::prm::PRMInference< GUM_SCALAR >::EMapIterator
inherited

Code alias.

Definition at line 61 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 88 of file groundedInference_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

89  :
90  PRMInference< GUM_SCALAR >(prm, system),
91  _inf_(0) {
92  GUM_CONSTRUCTOR(GroundedInference);
93  }
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.
+ Here is the call graph for this function:

◆ ~GroundedInference()

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

Destructor.

Definition at line 35 of file groundedInference_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

35  {
36  GUM_DESTRUCTOR(GroundedInference);
37 
38  if (_inf_ != nullptr) delete _inf_;
39 
40  if (!_obs_.empty())
41  for (const auto pot: _obs_)
42  // We used const ptrs only because of
43  // MarginalTargetedInference::addEvidence()
44  // requires it
45  delete const_cast< Potential< GUM_SCALAR >* >(pot);
46  }
List< const Potential< GUM_SCALAR > *> _obs_
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.
+ 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 96 of file groundedInference_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

97  :
98  PRMInference< GUM_SCALAR >(source),
99  _inf_(0) {
100  GUM_CONS_CPY(GroundedInference);
101  GUM_ERROR(FatalError, "illegal to copy constructor")
102  }
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.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

Member Function Documentation

◆ addEvidence()

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

Add an evidence to the given instance's elt.

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

Definition at line 105 of file PRMInference_tpl.h.

106  {
107  if (chain.first->exists(chain.second->id())) {
108  if ((p.nbrDim() != 1) || (!p.contains(chain.second->type().variable())))
109  GUM_ERROR(OperationNotAllowed, "illegal evidence for the given PRMAttribute.")
110 
111  Potential< GUM_SCALAR >* e = new Potential< GUM_SCALAR >();
112  e->add(chain.second->type().variable());
113  Instantiation i(*e);
114 
115  for (i.setFirst(); !i.end(); i.inc())
116  e->set(i, p.get(i));
117 
118  PRMInference< GUM_SCALAR >::EMap& emap = _EMap_(chain.first);
119 
120  if (emap.exists(chain.second->id())) {
121  delete emap[chain.second->id()];
122  emap[chain.second->id()] = e;
123  } else {
124  emap.insert(chain.second->id(), e);
125  }
126 
127  evidenceAdded_(chain);
128  } else {
129  GUM_ERROR(NotFound,
130  "the given PRMAttribute does not belong to this "
131  "Instance<GUM_SCALAR>.");
132  }
133  }
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...
NodeProperty< const Potential< GUM_SCALAR > *> EMap
Code alias.
Definition: PRMInference.h:58
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ clearEvidence()

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

Remove all evidences.

Definition at line 35 of file PRMInference_tpl.h.

35  {
36  for (const auto& elt: _evidences_) {
37  for (const auto& elt2: *elt.second)
38  delete elt2.second;
39 
40  delete elt.second;
41  }
42 
43  _evidences_.clear();
44  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> _evidences_
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:228

◆ 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 151 of file PRMInference_tpl.h.

151  {
152  try {
153  return *(_evidences_[&i]);
154  } catch (NotFound&) { GUM_ERROR(NotFound, "this instance has no evidence.") }
155  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> _evidences_
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:228
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ evidence() [2/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 167 of file PRMInference_tpl.h.

167  {
168  try {
169  return *(_evidences_[i]);
170  } catch (NotFound&) { GUM_ERROR(NotFound, "this instance has no evidence.") }
171  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> _evidences_
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:228
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ evidence() [3/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 159 of file PRMInference_tpl.h.

159  {
160  try {
161  return *(_evidences_[&i]);
162  } catch (NotFound&) { GUM_ERROR(NotFound, "this instance has no evidence.") }
163  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> _evidences_
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:228
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ 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 175 of file PRMInference_tpl.h.

175  {
176  try {
177  return *(_evidences_[i]);
178  } catch (NotFound&) { GUM_ERROR(NotFound, "this instance has no evidence.") }
179  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> _evidences_
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:228
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

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

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

50  {
51  Potential< GUM_SCALAR >* bn_obs = new Potential< GUM_SCALAR >();
52  // Retrieving the BN's variable
53  std::stringstream var_name;
54  var_name << chain.first->name() << "." << chain.second->safeName();
55  bn_obs->add(_inf_->BN().variableFromName(var_name.str()));
56  // Retrievin the PRM<GUM_SCALAR>'s evidence and copying it in bn_obs
57  const Potential< GUM_SCALAR >* prm_obs = this->evidence(chain.first)[chain.second->id()];
58  Instantiation i(*bn_obs), j(*prm_obs);
59 
60  for (i.setFirst(), j.setFirst(); !i.end(); i.inc(), j.inc()) {
61  bn_obs->set(i, prm_obs->get(j));
62  }
63 
64  _obs_.insert(bn_obs);
65  }
List< const Potential< GUM_SCALAR > *> _obs_
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
MarginalTargetedInference< GUM_SCALAR > * _inf_
The bayesnet inference engine used by this class.
+ 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 68 of file groundedInference_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

69  {
70  std::stringstream var_name;
71  var_name << chain.first->name() << "." << chain.second->safeName();
72  const DiscreteVariable& var = _inf_->BN().variableFromName(var_name.str());
73 
74  for (auto iter = _obs_.beginSafe(); iter != _obs_.endSafe();
75  ++iter) { // safe iterator needed here
76  if ((**iter).contains(var)) {
77  _inf_->eraseEvidence(var_name.str());
78  const Potential< GUM_SCALAR >* e = *iter;
79  _obs_.erase(iter);
80  delete e;
81  break;
82  }
83  }
84  }
List< const Potential< GUM_SCALAR > *> _obs_
MarginalTargetedInference< GUM_SCALAR > * _inf_
The bayesnet inference engine used by this class.
+ Here is the call graph for this function:

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

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

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

112  {
113  if (_inf_ != 0) {
114  return *_inf_;
115  } else {
116  GUM_ERROR(NotFound, "the inference engine is not yet defined")
117  }
118  }
MarginalTargetedInference< GUM_SCALAR > * _inf_
The bayesnet inference engine used by this class.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ hasEvidence() [1/4]

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

Returns true if i has evidence.

Definition at line 182 of file PRMInference_tpl.h.

182  {
183  return _evidences_.exists(&i);
184  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> _evidences_
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:228

◆ hasEvidence() [2/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 187 of file PRMInference_tpl.h.

187  {
188  return _evidences_.exists(i);
189  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> _evidences_
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:228

◆ hasEvidence() [3/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 192 of file PRMInference_tpl.h.

192  {
193  return (hasEvidence(chain.first)) ? evidence(chain.first).exists(chain.second->id()) : false;
194  }
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
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.

◆ hasEvidence() [4/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 197 of file PRMInference_tpl.h.

197  {
198  return (_evidences_.size() != (Size)0);
199  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> _evidences_
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:228
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47

◆ joint()

template<typename GUM_SCALAR>
INLINE void gum::prm::PRMInference< GUM_SCALAR >::joint ( const std::vector< Chain > &  chains,
Potential< 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 241 of file PRMInference_tpl.h.

243  {
244  if (j.nbrDim() > 0) { GUM_ERROR(OperationNotAllowed, "the given Potential is not empty.") }
245 
246  for (auto chain = chains.begin(); chain != chains.end(); ++chain) {
247  j.add(chain->second->type().variable());
248  }
249 
250  joint_(chains, j);
251  }
virtual void joint_(const std::vector< Chain > &queries, Potential< GUM_SCALAR > &j)=0
Generic method to compute the posterior of given element.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ joint_() [1/2]

template<typename GUM_SCALAR >
INLINE void gum::prm::GroundedInference< GUM_SCALAR >::joint_ ( const std::vector< typename PRMInference< GUM_SCALAR >::Chain > &  queries,
Potential< 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 149 of file groundedInference_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

151  {
152  GUM_ERROR(FatalError, "not yet implemented")
153  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ joint_() [2/2]

template<typename GUM_SCALAR>
virtual void gum::prm::PRMInference< GUM_SCALAR >::joint_ ( const std::vector< Chain > &  queries,
Potential< 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 >.

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

156  {
157  return "grounded inference";
158  }

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

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

106  {
107  GUM_ERROR(FatalError, "illegal call to copy operator")
108  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ posterior()

template<typename GUM_SCALAR>
INLINE void gum::prm::PRMInference< GUM_SCALAR >::posterior ( const Chain chain,
Potential< 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 215 of file PRMInference_tpl.h.

217  {
218  if (m.nbrDim() > 0) { GUM_ERROR(OperationNotAllowed, "the given Potential is not empty.") }
219 
220  if (hasEvidence(chain)) {
221  m.add(chain.second->type().variable());
222  const Potential< GUM_SCALAR >& e = *(evidence(chain.first)[chain.second->id()]);
223  Instantiation i(m), j(e);
224 
225  for (i.setFirst(), j.setFirst(); !i.end(); i.inc(), j.inc())
226  m.set(i, e.get(j));
227  } else {
228  if (chain.second != &(chain.first->get(chain.second->safeName()))) {
229  typename PRMInference< GUM_SCALAR >::Chain good_chain
230  = std::make_pair(chain.first, &(chain.first->get(chain.second->safeName())));
231  m.add(good_chain.second->type().variable());
232  posterior_(good_chain, m);
233  } else {
234  m.add(chain.second->type().variable());
235  posterior_(chain, m);
236  }
237  }
238  }
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
virtual void posterior_(const Chain &chain, Potential< GUM_SCALAR > &m)=0
Generic method to compute the posterior of given element.
bool hasEvidence() const
Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
std::pair< const PRMInstance< GUM_SCALAR > *, const PRMAttribute< GUM_SCALAR > *> Chain
Code alias.
Definition: PRMInference.h:55
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ posterior_() [1/2]

template<typename GUM_SCALAR >
INLINE void gum::prm::GroundedInference< GUM_SCALAR >::posterior_ ( const typename PRMInference< GUM_SCALAR >::Chain chain,
Potential< 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 129 of file groundedInference_tpl.h.

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

131  {
132  if (_inf_ == 0) { GUM_ERROR(OperationNotAllowed, "no inference engine defined") }
133 
134  std::stringstream sBuff;
135 
136  if (!_obs_.empty()) {
137  for (auto e: _obs_) {
138  try {
139  _inf_->addEvidence(*e);
140  } catch (InvalidArgument&) { _inf_->chgEvidence(*e); }
141  }
142  }
143 
144  sBuff << chain.first->name() << "." << chain.second->safeName();
145  m = _inf_->posterior(_inf_->BN().idFromName(sBuff.str()));
146  }
List< const Potential< GUM_SCALAR > *> _obs_
MarginalTargetedInference< GUM_SCALAR > * _inf_
The bayesnet inference engine used by this class.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ posterior_() [2/2]

template<typename GUM_SCALAR>
virtual void gum::prm::PRMInference< GUM_SCALAR >::posterior_ ( const Chain chain,
Potential< 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 202 of file PRMInference_tpl.h.

202  {
203  try {
204  if (_EMap_(chain.first).exists(chain.second->id())) {
205  evidenceRemoved_(chain);
206  delete _EMap_(chain.first)[chain.second->id()];
207  _EMap_(chain.first).erase(chain.second->id());
208  }
209  } catch (NotFound&) {
210  // Ok, we are only removing
211  }
212  }
EMap & _EMap_(const PRMInstance< GUM_SCALAR > *i)
Private getter over evidences, if necessary creates an EMap for i.
void erase(const Key &key)
Removes a given element from the hash table.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
virtual void evidenceRemoved_(const Chain &chain)=0
This method is called whenever an evidence is removed, but BEFORE any processing made by PRMInference...

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

References gum::prm::ParamScopeData< GUM_SCALAR >::ParamScopeData().

122  {
123  if (_inf_ != 0) { delete _inf_; }
124 
125  _inf_ = bn_inf;
126  }
MarginalTargetedInference< GUM_SCALAR > * _inf_
The bayesnet inference engine used by this class.
+ Here is the call graph for this function:

Member Data Documentation

◆ _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 126 of file groundedInference.h.

◆ _obs_

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

Definition at line 128 of file groundedInference.h.

◆ prm_

template<typename GUM_SCALAR>
PRM< GUM_SCALAR > const* gum::prm::PRMInference< GUM_SCALAR >::prm_
protectedinherited

The PRM<GUM_SCALAR> on which inference is done.

Definition at line 207 of file PRMInference.h.

◆ sys_

template<typename GUM_SCALAR>
PRMSystem< GUM_SCALAR > const* gum::prm::PRMInference< GUM_SCALAR >::sys_
protectedinherited

The Model on which inference is done.

Definition at line 210 of file PRMInference.h.


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