aGrUM  0.21.0
a C++ library for (probabilistic) graphical models
gum::prm::SVE< GUM_SCALAR > Class Template Reference

This class is an implementation of the Structured Variable Elimination algorithm on PRM<GUM_SCALAR>. More...

#include <agrum/PRM/SVE.h>

+ Inheritance diagram for gum::prm::SVE< GUM_SCALAR >:
+ Collaboration diagram for gum::prm::SVE< GUM_SCALAR >:

Public Member Functions

Constructors & destructor.
 SVE (const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
 Default Constructor. More...
 
 ~SVE ()
 Destructor. More...
 
Getters & setters.
virtual std::string name () const
 Returns the name of the current inference algorithm. 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 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 Attributes

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

Query methods.

typedef PRMInference< GUM_SCALAR >::Chain Chain
 Code alias. More...
 
virtual void evidenceAdded_ (const Chain &chain)
 See PRMInference<GUM_SCALAR>::evidenceAdded_(). More...
 
virtual void evidenceRemoved_ (const Chain &chain)
 See PRMInference<GUM_SCALAR>::evidenceRemoved_(). More...
 
virtual void posterior_ (const Chain &chain, Potential< GUM_SCALAR > &m)
 See PRMInference<GUM_SCALAR>::posterior_(). More...
 
virtual void joint_ (const std::vector< Chain > &queries, Potential< GUM_SCALAR > &j)
 See PRMInference<GUM_SCALAR>::joint_(). More...
 

Detailed Description

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

This class is an implementation of the Structured Variable Elimination algorithm on PRM<GUM_SCALAR>.

Definition at line 65 of file SVE.h.

Member Typedef Documentation

◆ ArraySetIterator

template<typename GUM_SCALAR >
typedef Set< MultiDimArray< GUM_SCALAR >* >::iterator_safe gum::prm::SVE< GUM_SCALAR >::ArraySetIterator
private

Code alias.

Definition at line 117 of file SVE.h.

◆ BucketSet

template<typename GUM_SCALAR >
typedef Set< Potential< GUM_SCALAR >* > gum::prm::SVE< GUM_SCALAR >::BucketSet
private

Code alias.

Definition at line 112 of file SVE.h.

◆ BucketSetIterator

template<typename GUM_SCALAR >
typedef Set< Potential< GUM_SCALAR >* >::iterator_safe gum::prm::SVE< GUM_SCALAR >::BucketSetIterator
private

Code alias.

Definition at line 114 of file SVE.h.

◆ Chain

template<typename GUM_SCALAR >
typedef PRMInference< GUM_SCALAR >::Chain gum::prm::SVE< GUM_SCALAR >::Chain
protected

Code alias.

Definition at line 95 of file SVE.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

◆ SVE()

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

Default Constructor.

Definition at line 602 of file SVE_tpl.h.

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

603  :
604  PRMInference< GUM_SCALAR >(prm, system),
605  _class_elim_order_(0) {
606  GUM_CONSTRUCTOR(SVE);
607  }
SVE(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default Constructor.
Definition: SVE_tpl.h:602
Sequence< std::string > * _class_elim_order_
Definition: SVE.h:123
+ Here is the call graph for this function:

◆ ~SVE()

template<typename GUM_SCALAR >
gum::prm::SVE< GUM_SCALAR >::~SVE ( )

Destructor.

Definition at line 106 of file SVE_tpl.h.

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

106  {
107  GUM_DESTRUCTOR(SVE);
108 
109  for (const auto& elt: _elim_orders_)
110  delete elt.second;
111 
112  for (const auto& elt: _lifted_pools_)
113  delete elt.second;
114 
115  if (_class_elim_order_ != nullptr) delete _class_elim_order_;
116 
117  for (const auto trash: _lifted_trash_)
118  delete trash;
119 
120  for (auto set: _delayedVariables_)
121  delete set.second;
122  }
BucketSet _lifted_trash_
Definition: SVE.h:135
HashTable< const PRMInstance< GUM_SCALAR > *, Set< const DiscreteVariable *> *> _delayedVariables_
Definition: SVE.h:126
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > *> _elim_orders_
Definition: SVE.h:119
HashTable< const PRMClass< GUM_SCALAR > *, BucketSet *> _lifted_pools_
Definition: SVE.h:121
SVE(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default Constructor.
Definition: SVE_tpl.h:602
Sequence< std::string > * _class_elim_order_
Definition: SVE.h:123
+ Here is the call graph for this function:

Member Function Documentation

◆ _addDelayedVariable_()

template<typename GUM_SCALAR >
INLINE void gum::prm::SVE< GUM_SCALAR >::_addDelayedVariable_ ( const PRMInstance< GUM_SCALAR > *  i,
const PRMInstance< GUM_SCALAR > *  j,
NodeId  id 
)
private

When there is a loop in the references some variable elimination must be delayed, this methods add such variable to delayedVariables to keep track of them.

Parameters
iAn PRMInstance<GUM_SCALAR> with a child of j->get(id).
jThe PRMInstance<GUM_SCALAR> with the delayed variable.
idThe NodeId of the delayed variable.

Definition at line 657 of file SVE_tpl.h.

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

659  {
660  try {
661  _delayedVariables_[i]->insert(&(j->get(id).type().variable()));
662  } catch (NotFound&) {
663  _delayedVariables_.insert(i, new Set< const DiscreteVariable* >());
664  _delayedVariables_[i]->insert(&(j->get(id).type().variable()));
665  } catch (DuplicateElement&) {
666  // happends if j->get(id) is parent of more than one variable in i
667  }
668 
669  static std::string dot = ".";
670 
671  try {
672  _delayedVariablesCounters_[j->name() + dot + j->get(id).safeName()] += 1;
673  } catch (NotFound&) {
674  _delayedVariablesCounters_.insert(j->name() + dot + j->get(id).safeName(), 1);
675  }
676  }
HashTable< const PRMInstance< GUM_SCALAR > *, Set< const DiscreteVariable *> *> _delayedVariables_
Definition: SVE.h:126
HashTable< std::string, Size > _delayedVariablesCounters_
Some variable must be delayed for more than one PRMInstance<GUM_SCALAR>, when the delayed variable co...
Definition: SVE.h:133
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
+ Here is the call graph for this function:

◆ _checkElimOrder_()

template<typename GUM_SCALAR >
INLINE bool gum::prm::SVE< GUM_SCALAR >::_checkElimOrder_ ( const PRMInstance< GUM_SCALAR > *  first,
const PRMInstance< GUM_SCALAR > *  second 
)
private

Returns true if second can be eliminated before first.

Definition at line 630 of file SVE_tpl.h.

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

631  {
632  if (_class_elim_order_ == 0) { _initElimOrder_(); }
633 
634  auto first_name = _trim_(first->type().name());
635  auto second_name = _trim_(second->type().name());
636  return (_class_elim_order_->pos(first_name) <= _class_elim_order_->pos(second_name));
637  }
Idx pos(const Key &key) const
Returns the position of the object passed in argument (if it exists).
Definition: sequence_tpl.h:499
std::string _trim_(const std::string &s)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:623
Sequence< std::string > * _class_elim_order_
Definition: SVE.h:123
void _initElimOrder_()
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:527
+ Here is the call graph for this function:

◆ _eliminateDelayedVariables_()

template<typename GUM_SCALAR >
void gum::prm::SVE< GUM_SCALAR >::_eliminateDelayedVariables_ ( const PRMInstance< GUM_SCALAR > *  i,
BucketSet pool,
BucketSet trash 
)
private

Returns true if second can be eliminated before first.

Definition at line 207 of file SVE_tpl.h.

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

209  {
210  Set< Potential< GUM_SCALAR >* > toRemove;
211 
212  for (const auto var: *_delayedVariables_[i]) {
213  MultiDimBucket< GUM_SCALAR >* bucket = new MultiDimBucket< GUM_SCALAR >();
214 
215  for (const auto pot: pool)
216  if (pot->contains(*var)) {
217  bucket->add(*pot);
218  toRemove.insert(pot);
219  }
220 
221  for (const auto pot: toRemove)
222  pool.erase(pot);
223 
224  for (const auto other: bucket->allVariables())
225  if (other != var) bucket->add(*other);
226 
227  Potential< GUM_SCALAR >* bucket_pot = new Potential< GUM_SCALAR >(bucket);
228  trash.insert(bucket_pot);
229  pool.insert(bucket_pot);
230  }
231  }
HashTable< const PRMInstance< GUM_SCALAR > *, Set< const DiscreteVariable *> *> _delayedVariables_
Definition: SVE.h:126
+ Here is the call graph for this function:

◆ _eliminateNodes_()

template<typename GUM_SCALAR >
void gum::prm::SVE< GUM_SCALAR >::_eliminateNodes_ ( const PRMInstance< GUM_SCALAR > *  query,
NodeId  id,
BucketSet pool,
BucketSet trash 
)
private

Returns true if second can be eliminated before first.

Definition at line 125 of file SVE_tpl.h.

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

128  {
129  Set< const PRMInstance< GUM_SCALAR >* > ignore, eliminated;
130  Set< NodeId > delayedVars;
131  // Downward elimination
132  List< const PRMInstance< GUM_SCALAR >* > elim_list;
133  ignore.insert(query);
134 
135  for (auto iter = query->beginInvRef(); iter != query->endInvRef(); ++iter) {
136  for (auto child = (*(iter.val())).begin(); child != (*(iter.val())).end(); ++child) {
137  if (!ignore.exists(child->first)) {
139  child->first,
140  pool,
141  trash,
142  elim_list,
143  ignore,
144  eliminated);
145  } else if (!eliminated.exists(child->first)) {
146  _addDelayedVariable_(child->first, query, iter.key());
147  delayedVars.insert(iter.key());
148  }
149  }
150  }
151 
152  // Eliminating all nodes in query instance, except query
153  InstanceBayesNet< GUM_SCALAR > bn(*query);
154  DefaultTriangulation t(&(bn.moralGraph()), &(bn.modalities()));
155  std::vector< const DiscreteVariable* > elim_order;
156 
157  if (this->hasEvidence(query)) { _insertEvidence_(query, pool); }
158 
159  for (auto attr = query->begin(); attr != query->end(); ++attr) {
160  pool.insert(&(const_cast< Potential< GUM_SCALAR >& >((*(attr.val())).cpf())));
161  }
162 
163  for (size_t idx = 0; idx < t.eliminationOrder().size(); ++idx) {
164  if ((t.eliminationOrder()[idx] != node)
165  && (!delayedVars.exists(t.eliminationOrder()[idx]))) {
166  auto var_id = t.eliminationOrder()[idx];
167  const auto& var = bn.variable(var_id);
168  elim_order.push_back(&var);
169  }
170  }
171 
172  eliminateNodes(elim_order, pool, trash);
173 
174  // Eliminating delayed variables, if any
175  if (_delayedVariables_.exists(query)) { _eliminateDelayedVariables_(query, pool, trash); }
176 
177  eliminated.insert(query);
178  // Eliminating instance in elim_list
179  List< const PRMInstance< GUM_SCALAR >* > tmp_list;
180 
181  while (!elim_list.empty()) {
182  if (_checkElimOrder_(query, elim_list.front())) {
183  if (!ignore.exists(elim_list.front())) {
185  elim_list.front(),
186  pool,
187  trash,
188  elim_list,
189  ignore,
190  eliminated);
191  }
192  } else {
193  tmp_list.insert(elim_list.front());
194  }
195 
196  elim_list.popFront();
197  }
198 
199  // Upward elimination
200  for (const auto chain: query->type().slotChains())
201  for (const auto parent: query->getInstances(chain->id()))
202  if (!ignore.exists(parent))
203  _eliminateNodesUpward_(parent, pool, trash, tmp_list, ignore, eliminated);
204  }
HashTable< const PRMInstance< GUM_SCALAR > *, Set< const DiscreteVariable *> *> _delayedVariables_
Definition: SVE.h:126
bool _checkElimOrder_(const PRMInstance< GUM_SCALAR > *first, const PRMInstance< GUM_SCALAR > *second)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:630
void _insertEvidence_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:610
void _addDelayedVariable_(const PRMInstance< GUM_SCALAR > *i, const PRMInstance< GUM_SCALAR > *j, NodeId id)
When there is a loop in the references some variable elimination must be delayed, this methods add su...
Definition: SVE_tpl.h:657
void _eliminateNodesUpward_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, List< const PRMInstance< GUM_SCALAR > * > &elim_list, Set< const PRMInstance< GUM_SCALAR > * > &ignore, Set< const PRMInstance< GUM_SCALAR > * > &eliminated)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:322
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:600
void _eliminateNodesDownward_(const PRMInstance< GUM_SCALAR > *from, const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, List< const PRMInstance< GUM_SCALAR > * > &elim_list, Set< const PRMInstance< GUM_SCALAR > * > &ignore, Set< const PRMInstance< GUM_SCALAR > * > &eliminated)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:234
bool hasEvidence() const
Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
void eliminateNodes(const std::vector< const DiscreteVariable * > &elim_order, Set< Potential< GUM_SCALAR > * > &pool, Set< Potential< GUM_SCALAR > * > &trash)
void _eliminateDelayedVariables_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:207
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:606
+ Here is the call graph for this function:

◆ _eliminateNodesDownward_()

template<typename GUM_SCALAR >
void gum::prm::SVE< GUM_SCALAR >::_eliminateNodesDownward_ ( const PRMInstance< GUM_SCALAR > *  from,
const PRMInstance< GUM_SCALAR > *  i,
BucketSet pool,
BucketSet trash,
List< const PRMInstance< GUM_SCALAR > * > &  elim_list,
Set< const PRMInstance< GUM_SCALAR > * > &  ignore,
Set< const PRMInstance< GUM_SCALAR > * > &  eliminated 
)
private

Returns true if second can be eliminated before first.

Definition at line 234 of file SVE_tpl.h.

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

241  {
242  Set< NodeId > delayedVars;
243  ignore.insert(i);
244  // Calling elimination over child instance
245  List< const PRMInstance< GUM_SCALAR >* > my_list;
246 
247  for (auto iter = i->beginInvRef(); iter != i->endInvRef(); ++iter) {
248  for (auto child = (*(iter.val())).begin(); child != (*(iter.val())).end(); ++child) {
249  if (!ignore.exists(child->first)) {
250  _eliminateNodesDownward_(i, child->first, pool, trash, my_list, ignore, eliminated);
251  } else if (!eliminated.exists(child->first)) {
252  _addDelayedVariable_(child->first, i, iter.key());
253  delayedVars.insert(iter.key());
254  }
255  }
256  }
257 
258  // Eliminating all nodes in current instance
259  _variableElimination_(i, pool, trash, (delayedVars.empty() ? 0 : &delayedVars));
260  eliminated.insert(i);
261 
262  // Calling elimination over child's parents
263  for (const auto node: my_list) {
264  if (_checkElimOrder_(i, node) && (node != from)) {
265  if (!ignore.exists(node)) {
266  _eliminateNodesDownward_(i, node, pool, trash, elim_list, ignore, eliminated);
267  }
268  } else if (node != from) {
269  elim_list.insert(node);
270  }
271  }
272 
273  // Adding parents instance to elim_list
274  for (const auto chain: i->type().slotChains()) {
275  for (const auto inst: i->getInstances(chain->id())) {
276  if (inst != from) { elim_list.insert(inst); }
277  }
278  }
279  }
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition: set_tpl.h:700
bool _checkElimOrder_(const PRMInstance< GUM_SCALAR > *first, const PRMInstance< GUM_SCALAR > *second)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:630
void _variableElimination_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, Set< NodeId > *delayedVars=0)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:282
void _addDelayedVariable_(const PRMInstance< GUM_SCALAR > *i, const PRMInstance< GUM_SCALAR > *j, NodeId id)
When there is a loop in the references some variable elimination must be delayed, this methods add su...
Definition: SVE_tpl.h:657
void _eliminateNodesDownward_(const PRMInstance< GUM_SCALAR > *from, const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, List< const PRMInstance< GUM_SCALAR > * > &elim_list, Set< const PRMInstance< GUM_SCALAR > * > &ignore, Set< const PRMInstance< GUM_SCALAR > * > &eliminated)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:234
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:606
+ Here is the call graph for this function:

◆ _eliminateNodesUpward_()

template<typename GUM_SCALAR >
void gum::prm::SVE< GUM_SCALAR >::_eliminateNodesUpward_ ( const PRMInstance< GUM_SCALAR > *  i,
BucketSet pool,
BucketSet trash,
List< const PRMInstance< GUM_SCALAR > * > &  elim_list,
Set< const PRMInstance< GUM_SCALAR > * > &  ignore,
Set< const PRMInstance< GUM_SCALAR > * > &  eliminated 
)
private

Returns true if second can be eliminated before first.

Definition at line 322 of file SVE_tpl.h.

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

328  {
329  // Downward elimination
330  ignore.insert(i);
331 
332  for (auto iter = i->beginInvRef(); iter != i->endInvRef(); ++iter) {
333  for (auto child = (*(iter.val())).begin(); child != (*(iter.val())).end(); ++child) {
334  if (!ignore.exists(child->first)) {
335  _eliminateNodesDownward_(i, child->first, pool, trash, elim_list, ignore, eliminated);
336  }
337  }
338  }
339 
340  // Eliminating all nodes in i instance
341  _variableElimination_(i, pool, trash);
342  eliminated.insert(i);
343  // Eliminating instance in elim_list
344  List< const PRMInstance< GUM_SCALAR >* > tmp_list;
345 
346  while (!elim_list.empty()) {
347  if (_checkElimOrder_(i, elim_list.front())) {
348  if (!ignore.exists(elim_list.front())) {
350  elim_list.front(),
351  pool,
352  trash,
353  elim_list,
354  ignore,
355  eliminated);
356  }
357  } else {
358  tmp_list.insert(elim_list.front());
359  }
360 
361  elim_list.popFront();
362  }
363 
364  // Upward elimination
365  for (const auto chain: i->type().slotChains()) {
366  for (const auto parent: i->getInstances(chain->id())) {
367  if (!ignore.exists(parent)) {
368  _eliminateNodesUpward_(parent, pool, trash, tmp_list, ignore, eliminated);
369  }
370  }
371  }
372  }
bool _checkElimOrder_(const PRMInstance< GUM_SCALAR > *first, const PRMInstance< GUM_SCALAR > *second)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:630
void _variableElimination_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, Set< NodeId > *delayedVars=0)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:282
void _eliminateNodesUpward_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, List< const PRMInstance< GUM_SCALAR > * > &elim_list, Set< const PRMInstance< GUM_SCALAR > * > &ignore, Set< const PRMInstance< GUM_SCALAR > * > &eliminated)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:322
void _eliminateNodesDownward_(const PRMInstance< GUM_SCALAR > *from, const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, List< const PRMInstance< GUM_SCALAR > * > &elim_list, Set< const PRMInstance< GUM_SCALAR > * > &ignore, Set< const PRMInstance< GUM_SCALAR > * > &eliminated)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:234
+ Here is the call graph for this function:

◆ _eliminateNodesWithEvidence_()

template<typename GUM_SCALAR >
void gum::prm::SVE< GUM_SCALAR >::_eliminateNodesWithEvidence_ ( const PRMInstance< GUM_SCALAR > *  i,
BucketSet pool,
BucketSet trash,
Set< NodeId > *  delayedVars = 0 
)
private

Returns true if second can be eliminated before first.

Definition at line 375 of file SVE_tpl.h.

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

378  {
379  // First we check if evidences are on inner nodes
380  bool inner = false;
381 
382  for (const auto& elt: this->evidence(i)) {
383  inner
384  = i->type().isInputNode(i->get(elt.first)) || i->type().isInnerNode(i->get(elt.first));
385 
386  if (inner) { break; }
387  }
388 
389  // Evidence on inner nodes
390  if (inner) {
391  BucketSet tmp_pool;
392  _insertEvidence_(i, tmp_pool);
393 
394  // We need a local to not eliminate queried inner nodes of the same
395  // class
396  for (const auto& elt: *i) {
397  tmp_pool.insert(&(const_cast< Potential< GUM_SCALAR >& >(elt.second->cpf())));
398  }
399 
400  InstanceBayesNet< GUM_SCALAR > bn(*i);
401  DefaultTriangulation t(&(bn.moralGraph()), &(bn.modalities()));
402  const std::vector< NodeId >& full_elim_order = t.eliminationOrder();
403  // Removing Output nodes of elimination order
404  std::vector< const DiscreteVariable* > inner_elim_order;
405  std::vector< const DiscreteVariable* > output_elim_order;
406 
407  for (size_t idx = 0; idx < full_elim_order.size(); ++idx) {
408  auto var_id = full_elim_order[idx];
409  const auto& var = bn.variable(var_id);
410 
411  if (!i->type().isOutputNode(i->get(full_elim_order[idx]))) {
412  inner_elim_order.push_back(&var);
413  } else if (delayedVars != nullptr) {
414  if (!delayedVars->exists(full_elim_order[idx])) { output_elim_order.push_back(&var); }
415  } else {
416  output_elim_order.push_back(&var);
417  }
418  }
419 
420  eliminateNodes(inner_elim_order, tmp_pool, trash);
421 
422  // Now we add the new potentials in pool and eliminate output nodes
423  for (const auto pot: tmp_pool)
424  pool.insert(pot);
425 
426  if (!output_elim_order.empty()) eliminateNodes(output_elim_order, pool, trash);
427 
428  } else {
429  InstanceBayesNet< GUM_SCALAR > bn(*i);
430  _insertEvidence_(i, pool);
431  _insertLiftedNodes_(i, pool, trash);
432 
433  for (const auto agg: i->type().aggregates())
434  pool.insert(_getAggPotential_(i, agg));
435 
436  try {
437  std::vector< const DiscreteVariable* > elim;
438 
439  for (auto iter = _getElimOrder_(i->type()).begin();
440  iter != _getElimOrder_(i->type()).end();
441  ++iter) {
442  const auto& var = bn.variable(*iter);
443  if (delayedVars != nullptr) {
444  if (!delayedVars->exists(*iter)) { elim.push_back(&var); }
445  } else {
446  elim.push_back(&var);
447  }
448  }
449 
450  eliminateNodes(elim, pool, trash);
451  } catch (NotFound&) { GUM_ERROR(FatalError, "there should be at least one node here.") }
452  }
453  }
void _insertLiftedNodes_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:456
Set< Potential< GUM_SCALAR > *> BucketSet
Code alias.
Definition: SVE.h:112
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
void _insertEvidence_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:610
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:600
std::vector< NodeId > & _getElimOrder_(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:618
void eliminateNodes(const std::vector< const DiscreteVariable * > &elim_order, Set< Potential< GUM_SCALAR > * > &pool, Set< Potential< GUM_SCALAR > * > &trash)
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
Potential< GUM_SCALAR > * _getAggPotential_(const PRMInstance< GUM_SCALAR > *i, const PRMAggregate< GUM_SCALAR > *agg)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:641
+ Here is the call graph for this function:

◆ _getAggPotential_()

template<typename GUM_SCALAR >
INLINE Potential< GUM_SCALAR > * gum::prm::SVE< GUM_SCALAR >::_getAggPotential_ ( const PRMInstance< GUM_SCALAR > *  i,
const PRMAggregate< GUM_SCALAR > *  agg 
)
private

Returns true if second can be eliminated before first.

Definition at line 641 of file SVE_tpl.h.

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

642  {
643  return &(const_cast< Potential< GUM_SCALAR >& >(i->get(agg->id()).cpf()));
644  }
+ Here is the call graph for this function:

◆ _getElimOrder_()

template<typename GUM_SCALAR >
INLINE std::vector< NodeId > & gum::prm::SVE< GUM_SCALAR >::_getElimOrder_ ( const PRMClass< GUM_SCALAR > &  c)
private

Returns true if second can be eliminated before first.

Definition at line 618 of file SVE_tpl.h.

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

618  {
619  return *(_elim_orders_[&c]);
620  }
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > *> _elim_orders_
Definition: SVE.h:119
+ Here is the call graph for this function:

◆ _initElimOrder_()

template<typename GUM_SCALAR >
void gum::prm::SVE< GUM_SCALAR >::_initElimOrder_ ( )
private

Returns true if second can be eliminated before first.

Definition at line 527 of file SVE_tpl.h.

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

527  {
528  ClassDependencyGraph< GUM_SCALAR > cdg(*(this->prm_));
529  Sequence< const PRMClassElementContainer< GUM_SCALAR >* > class_elim_order;
530  std::list< NodeId > l;
531 
532  for (const auto node: cdg.dag().nodes()) {
533  if (cdg.dag().parents(node).empty()) { l.push_back(node); }
534  }
535 
536  Set< NodeId > visited_node;
537 
538  while (!l.empty()) {
539  visited_node.insert(l.front());
540 
541  if (!class_elim_order.exists(cdg.get(l.front()).first)) {
542  class_elim_order.insert(cdg.get(l.front()).first);
543  }
544 
545  for (const auto child: cdg.dag().children(l.front())) {
546  if (!visited_node.contains(child)) { l.push_back(child); }
547  }
548 
549  l.pop_front();
550  }
551 
553  for (auto c: class_elim_order) {
554  std::string name = c->name();
555  auto pos = name.find_first_of("<");
556  if (pos != std::string::npos) { name = name.substr(0, pos); }
557  try {
558  _class_elim_order_->insert(name);
559  } catch (DuplicateElement&) {}
560  }
561  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:558
PRM< GUM_SCALAR > const * prm_
The PRM<GUM_SCALAR> on which inference is done.
Definition: PRMInference.h:207
virtual std::string name() const
Returns the name of the current inference algorithm.
Definition: SVE_tpl.h:679
Sequence< std::string > * _class_elim_order_
Definition: SVE.h:123
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:606
void insert(const Key &k)
Insert an element at the end of the sequence.
Definition: sequence_tpl.h:393
+ Here is the call graph for this function:

◆ _initLiftedNodes_()

template<typename GUM_SCALAR >
void gum::prm::SVE< GUM_SCALAR >::_initLiftedNodes_ ( const PRMClass< GUM_SCALAR > &  c)
private

Returns true if second can be eliminated before first.

Definition at line 476 of file SVE_tpl.h.

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

476  {
477  BucketSet* lifted_pool = new BucketSet();
478  _lifted_pools_.insert(&c, lifted_pool);
479  NodeSet inners, outers;
480 
481  for (const auto node: c.containerDag().nodes())
483  if (c.isOutputNode(c.get(node)))
484  outers.insert(node);
485  else if (!outers.exists(node))
486  inners.insert(node);
487 
488  lifted_pool->insert(const_cast< Potential< GUM_SCALAR >* >(&(c.get(node).cpf())));
489  } else if (PRMClassElement< GUM_SCALAR >::isAggregate(c.get(node))) {
490  outers.insert(node);
491 
492  // We need to put in the output_elim_order aggregator's parents which
493  // are
494  // innner nodes
495  for (const auto par: c.containerDag().parents(node))
497  && c.isInnerNode(c.get(par))) {
498  inners.erase(par);
499  outers.insert(par);
500  }
501  }
502 
503  // Now we proceed with the elimination of inner attributes
504  ClassBayesNet< GUM_SCALAR > bn(c);
505  List< NodeSet > partial_ordering;
506 
507  if (inners.size()) partial_ordering.push_back(inners);
508 
509  if (outers.size()) partial_ordering.push_back(outers);
510 
511  PartialOrderedTriangulation t(&(bn.moralGraph()), &(bn.modalities()), &partial_ordering);
512 
513  for (size_t idx = 0; idx < inners.size(); ++idx)
514  eliminateNode(&(c.get(t.eliminationOrder()[idx]).type().variable()),
515  *lifted_pool,
517 
518  // If there is not only inner and input Attributes
519  if (outers.size()) {
520  _elim_orders_.insert(&c,
521  new std::vector< NodeId >(t.eliminationOrder().begin() + inners.size(),
522  t.eliminationOrder().end()));
523  }
524  }
BucketSet _lifted_trash_
Definition: SVE.h:135
Set< Potential< GUM_SCALAR > *> BucketSet
Code alias.
Definition: SVE.h:112
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > *> _elim_orders_
Definition: SVE.h:119
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
static INLINE bool isAttribute(const PRMClassElement< GUM_SCALAR > &elt)
Returns true if obj_ptr is of type PRMAttribute.
HashTable< const PRMClass< GUM_SCALAR > *, BucketSet *> _lifted_pools_
Definition: SVE.h:121
void eliminateNode(const DiscreteVariable *var, Set< Potential< GUM_SCALAR > * > &pool, Set< Potential< GUM_SCALAR > * > &trash)
Proceeds with the elimination of var in pool.
static INLINE bool isAggregate(const PRMClassElement< GUM_SCALAR > &elt)
Return true if obj is of type PRMAggregate.
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:606
+ Here is the call graph for this function:

◆ _insertEvidence_()

template<typename GUM_SCALAR >
INLINE void gum::prm::SVE< GUM_SCALAR >::_insertEvidence_ ( const PRMInstance< GUM_SCALAR > *  i,
BucketSet pool 
)
private

Returns true if second can be eliminated before first.

Definition at line 610 of file SVE_tpl.h.

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

611  {
612  for (const auto& elt: this->evidence(i))
613  pool.insert(const_cast< Potential< GUM_SCALAR >* >(elt.second));
614  }
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
+ Here is the call graph for this function:

◆ _insertLiftedNodes_()

template<typename GUM_SCALAR >
void gum::prm::SVE< GUM_SCALAR >::_insertLiftedNodes_ ( const PRMInstance< GUM_SCALAR > *  i,
BucketSet pool,
BucketSet trash 
)
private

Returns true if second can be eliminated before first.

Definition at line 456 of file SVE_tpl.h.

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

458  {
459  SVE< GUM_SCALAR >::BucketSet* lifted_pool = 0;
460 
461  try {
462  lifted_pool = _lifted_pools_[&(i->type())];
463  } catch (NotFound&) {
464  _initLiftedNodes_(i->type());
465  lifted_pool = _lifted_pools_[&(i->type())];
466  }
467 
468  for (const auto lifted_pot: *lifted_pool) {
469  Potential< GUM_SCALAR >* pot = copyPotential(i->bijection(), *lifted_pot);
470  pool.insert(pot);
471  trash.insert(pot);
472  }
473  }
Set< Potential< GUM_SCALAR > *> BucketSet
Code alias.
Definition: SVE.h:112
HashTable< const PRMClass< GUM_SCALAR > *, BucketSet *> _lifted_pools_
Definition: SVE.h:121
Potential< GUM_SCALAR > * copyPotential(const Bijection< const DiscreteVariable *, const DiscreteVariable * > &bij, const Potential< GUM_SCALAR > &source)
Returns a copy of a Potential after applying a bijection over the variables in source.
Definition: utils_prm_tpl.h:29
void _initLiftedNodes_(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:476
+ Here is the call graph for this function:

◆ _trim_()

template<typename GUM_SCALAR >
INLINE std::string gum::prm::SVE< GUM_SCALAR >::_trim_ ( const std::string &  s)
private

Returns true if second can be eliminated before first.

Definition at line 623 of file SVE_tpl.h.

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

623  {
624  auto pos = s.find_first_of("<");
625  if (pos != std::string::npos) { return s.substr(0, pos); }
626  return s;
627  }
+ Here is the call graph for this function:

◆ _variableElimination_()

template<typename GUM_SCALAR >
void gum::prm::SVE< GUM_SCALAR >::_variableElimination_ ( const PRMInstance< GUM_SCALAR > *  i,
BucketSet pool,
BucketSet trash,
Set< NodeId > *  delayedVars = 0 
)
private

Returns true if second can be eliminated before first.

Definition at line 282 of file SVE_tpl.h.

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

285  {
286  if (this->hasEvidence(i)) {
287  _eliminateNodesWithEvidence_(i, pool, trash, delayedVars);
288  } else {
289  _insertLiftedNodes_(i, pool, trash);
290 
291  for (const auto agg: i->type().aggregates())
292  pool.insert(_getAggPotential_(i, agg));
293 
294  try {
295  InstanceBayesNet< GUM_SCALAR > bn(*i);
296 
297  std::vector< const DiscreteVariable* > elim;
298 
299  for (const auto node: _getElimOrder_(i->type())) {
300  const auto& var = bn.variable(node);
301  if (delayedVars != nullptr) {
302  if (!delayedVars->exists(node)) {
303  const auto& var = bn.variable(node);
304  elim.push_back(&var);
305  }
306  } else {
307  elim.push_back(&var);
308  }
309  }
310 
311  eliminateNodes(elim, pool, trash);
312  } catch (NotFound&) {
313  // Raised if there is no inner nodes to eliminate
314  }
315  }
316 
317  // Eliminating delayed variables, if any
318  if (_delayedVariables_.exists(i)) { _eliminateDelayedVariables_(i, pool, trash); }
319  }
HashTable< const PRMInstance< GUM_SCALAR > *, Set< const DiscreteVariable *> *> _delayedVariables_
Definition: SVE.h:126
void _insertLiftedNodes_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:456
void _eliminateNodesWithEvidence_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, Set< NodeId > *delayedVars=0)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:375
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:600
std::vector< NodeId > & _getElimOrder_(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:618
bool hasEvidence() const
Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
void eliminateNodes(const std::vector< const DiscreteVariable * > &elim_order, Set< Potential< GUM_SCALAR > * > &pool, Set< Potential< GUM_SCALAR > * > &trash)
void _eliminateDelayedVariables_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:207
Potential< GUM_SCALAR > * _getAggPotential_(const PRMInstance< GUM_SCALAR > *i, const PRMAggregate< GUM_SCALAR > *agg)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:641
+ Here is the call graph for this function:

◆ 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_()

template<typename GUM_SCALAR >
INLINE void gum::prm::SVE< GUM_SCALAR >::evidenceAdded_ ( const Chain chain)
protectedvirtual

See PRMInference<GUM_SCALAR>::evidenceAdded_().

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

Definition at line 647 of file SVE_tpl.h.

647  {
648  // Do nothing
649  }

◆ evidenceRemoved_()

template<typename GUM_SCALAR >
INLINE void gum::prm::SVE< GUM_SCALAR >::evidenceRemoved_ ( const Chain chain)
protectedvirtual

See PRMInference<GUM_SCALAR>::evidenceRemoved_().

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

Definition at line 652 of file SVE_tpl.h.

652  {
653  // Do nothing
654  }

◆ 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_()

template<typename GUM_SCALAR >
void gum::prm::SVE< GUM_SCALAR >::joint_ ( const std::vector< Chain > &  queries,
Potential< GUM_SCALAR > &  j 
)
protectedvirtual

See PRMInference<GUM_SCALAR>::joint_().

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

Definition at line 596 of file SVE_tpl.h.

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

597  {
598  GUM_ERROR(FatalError, "Not implemented.")
599  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

◆ name()

template<typename GUM_SCALAR >
INLINE std::string gum::prm::SVE< GUM_SCALAR >::name ( ) const
virtual

Returns the name of the current inference algorithm.

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

Definition at line 679 of file SVE_tpl.h.

679  {
680  return "SVE";
681  }

◆ 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_()

template<typename GUM_SCALAR >
void gum::prm::SVE< GUM_SCALAR >::posterior_ ( const Chain chain,
Potential< GUM_SCALAR > &  m 
)
protectedvirtual

See PRMInference<GUM_SCALAR>::posterior_().

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

Definition at line 564 of file SVE_tpl.h.

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

564  {
565  const PRMInstance< GUM_SCALAR >* i = chain.first;
566  const PRMAttribute< GUM_SCALAR >* elt = chain.second;
567  SVE< GUM_SCALAR >::BucketSet pool, trash;
568 
569  _eliminateNodes_(i, elt->id(), pool, trash);
570 
571  std::vector< Potential< GUM_SCALAR >* > result;
572 
573  for (const auto pot: pool) {
574  if (pot->contains(elt->type().variable())) { result.push_back(pot); }
575  }
576 
577  while (result.size() > 1) {
578  auto& p1 = *(result.back());
579  result.pop_back();
580  auto& p2 = *(result.back());
581  result.pop_back();
582  auto mult = new Potential< GUM_SCALAR >(p1 * p2);
583  trash.insert(mult);
584  result.push_back(mult);
585  }
586 
587  m = *(result.back());
588  m.normalize();
589 
590  for (const auto pot: trash) {
591  delete pot;
592  }
593  }
Set< Potential< GUM_SCALAR > *> BucketSet
Code alias.
Definition: SVE.h:112
void _eliminateNodes_(const PRMInstance< GUM_SCALAR > *query, NodeId id, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:125
+ Here is the call graph for this function:

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

Member Data Documentation

◆ _class_elim_order_

template<typename GUM_SCALAR >
Sequence< std::string >* gum::prm::SVE< GUM_SCALAR >::_class_elim_order_
private

Definition at line 123 of file SVE.h.

◆ _delayedVariables_

template<typename GUM_SCALAR >
HashTable< const PRMInstance< GUM_SCALAR >*, Set< const DiscreteVariable* >* > gum::prm::SVE< GUM_SCALAR >::_delayedVariables_
private

Definition at line 126 of file SVE.h.

◆ _delayedVariablesCounters_

template<typename GUM_SCALAR >
HashTable< std::string, Size > gum::prm::SVE< GUM_SCALAR >::_delayedVariablesCounters_
private

Some variable must be delayed for more than one PRMInstance<GUM_SCALAR>, when the delayed variable counter reach 0 it can be eliminated.

Definition at line 133 of file SVE.h.

◆ _elim_orders_

template<typename GUM_SCALAR >
HashTable< const PRMClass< GUM_SCALAR >*, std::vector< NodeId >* > gum::prm::SVE< GUM_SCALAR >::_elim_orders_
private

Definition at line 119 of file SVE.h.

◆ _lifted_pools_

template<typename GUM_SCALAR >
HashTable< const PRMClass< GUM_SCALAR >*, BucketSet* > gum::prm::SVE< GUM_SCALAR >::_lifted_pools_
private

Definition at line 121 of file SVE.h.

◆ _lifted_trash_

template<typename GUM_SCALAR >
BucketSet gum::prm::SVE< GUM_SCALAR >::_lifted_trash_
private

Definition at line 135 of file SVE.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: