aGrUM  0.20.2
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 120 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 113 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 116 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 59 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 68 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 64 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 657 of file SVE_tpl.h.

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

658  :
659  PRMInference< GUM_SCALAR >(prm, system),
660  class_elim_order__(0) {
661  GUM_CONSTRUCTOR(SVE);
662  }
Sequence< std::string > * class_elim_order__
Definition: SVE.h:127
SVE(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default Constructor.
Definition: SVE_tpl.h:657
+ Here is the call graph for this function:

◆ ~SVE()

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

Destructor.

Definition at line 107 of file SVE_tpl.h.

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

107  {
108  GUM_DESTRUCTOR(SVE);
109 
110  for (const auto& elt: elim_orders__)
111  delete elt.second;
112 
113  for (const auto& elt: lifted_pools__)
114  delete elt.second;
115 
116  if (class_elim_order__ != nullptr) delete class_elim_order__;
117 
118  for (const auto trash: lifted_trash__)
119  delete trash;
120 
121  for (auto set: delayedVariables__)
122  delete set.second;
123  }
Sequence< std::string > * class_elim_order__
Definition: SVE.h:127
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > *> elim_orders__
Definition: SVE.h:123
HashTable< const PRMClass< GUM_SCALAR > *, BucketSet *> lifted_pools__
Definition: SVE.h:125
SVE(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default Constructor.
Definition: SVE_tpl.h:657
HashTable< const PRMInstance< GUM_SCALAR > *, Set< const DiscreteVariable *> *> delayedVariables__
Definition: SVE.h:131
BucketSet lifted_trash__
Definition: SVE.h:140
+ 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 716 of file SVE_tpl.h.

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

718  {
719  try {
720  delayedVariables__[i]->insert(&(j->get(id).type().variable()));
721  } catch (NotFound&) {
722  delayedVariables__.insert(i, new Set< const DiscreteVariable* >());
723  delayedVariables__[i]->insert(&(j->get(id).type().variable()));
724  } catch (DuplicateElement&) {
725  // happends if j->get(id) is parent of more than one variable in i
726  }
727 
728  static std::string dot = ".";
729 
730  try {
731  delayedVariablesCounters__[j->name() + dot + j->get(id).safeName()] += 1;
732  } catch (NotFound&) {
733  delayedVariablesCounters__.insert(j->name() + dot + j->get(id).safeName(),
734  1);
735  }
736  }
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:138
HashTable< const PRMInstance< GUM_SCALAR > *, Set< const DiscreteVariable *> *> delayedVariables__
Definition: SVE.h:131
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:

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

109  {
110  if (chain.first->exists(chain.second->id())) {
111  if ((p.nbrDim() != 1) || (!p.contains(chain.second->type().variable())))
112  GUM_ERROR(OperationNotAllowed,
113  "illegal evidence for the given PRMAttribute.");
114 
115  Potential< GUM_SCALAR >* e = new Potential< GUM_SCALAR >();
116  e->add(chain.second->type().variable());
117  Instantiation i(*e);
118 
119  for (i.setFirst(); !i.end(); i.inc())
120  e->set(i, p.get(i));
121 
122  PRMInference< GUM_SCALAR >::EMap& emap = EMap__(chain.first);
123 
124  if (emap.exists(chain.second->id())) {
125  delete emap[chain.second->id()];
126  emap[chain.second->id()] = e;
127  } else {
128  emap.insert(chain.second->id(), e);
129  }
130 
131  evidenceAdded_(chain);
132  } else {
133  GUM_ERROR(NotFound,
134  "the given PRMAttribute does not belong to this "
135  "Instance<GUM_SCALAR>.");
136  }
137  }
virtual void evidenceAdded_(const Chain &chain)=0
This method is called whenever an evidence is added, but AFTER any processing made by PRMInference...
EMap & EMap__(const PRMInstance< GUM_SCALAR > *i)
Private getter over evidences__, if necessary creates an EMap for i.
NodeProperty< const Potential< GUM_SCALAR > *> EMap
Code alias.
Definition: PRMInference.h:59
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ 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 686 of file SVE_tpl.h.

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

688  {
689  if (class_elim_order__ == 0) { initElimOrder__(); }
690 
691  auto first_name = trim__(first->type().name());
692  auto second_name = trim__(second->type().name());
693  return (class_elim_order__->pos(first_name)
694  <= class_elim_order__->pos(second_name));
695  }
Idx pos(const Key &key) const
Returns the position of the object passed in argument (if it exists).
Definition: sequence_tpl.h:519
Sequence< std::string > * class_elim_order__
Definition: SVE.h:127
void initElimOrder__()
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:581
std::string trim__(const std::string &s)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:679
+ Here is the call graph for this function:

◆ 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:235

◆ 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 218 of file SVE_tpl.h.

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

221  {
222  Set< Potential< GUM_SCALAR >* > toRemove;
223 
224  for (const auto var: *delayedVariables__[i]) {
225  MultiDimBucket< GUM_SCALAR >* bucket = new MultiDimBucket< GUM_SCALAR >();
226 
227  for (const auto pot: pool)
228  if (pot->contains(*var)) {
229  bucket->add(*pot);
230  toRemove.insert(pot);
231  }
232 
233  for (const auto pot: toRemove)
234  pool.erase(pot);
235 
236  for (const auto other: bucket->allVariables())
237  if (other != var) bucket->add(*other);
238 
239  Potential< GUM_SCALAR >* bucket_pot = new Potential< GUM_SCALAR >(bucket);
240  trash.insert(bucket_pot);
241  pool.insert(bucket_pot);
242  }
243  }
HashTable< const PRMInstance< GUM_SCALAR > *, Set< const DiscreteVariable *> *> delayedVariables__
Definition: SVE.h:131
+ 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 127 of file SVE_tpl.h.

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

130  {
131  Set< const PRMInstance< GUM_SCALAR >* > ignore, eliminated;
132  Set< NodeId > delayedVars;
133  // Downward elimination
134  List< const PRMInstance< GUM_SCALAR >* > elim_list;
135  ignore.insert(query);
136 
137  for (auto iter = query->beginInvRef(); iter != query->endInvRef(); ++iter) {
138  for (auto child = (*(iter.val())).begin(); child != (*(iter.val())).end();
139  ++child) {
140  if (!ignore.exists(child->first)) {
142  child->first,
143  pool,
144  trash,
145  elim_list,
146  ignore,
147  eliminated);
148  } else if (!eliminated.exists(child->first)) {
149  addDelayedVariable__(child->first, query, iter.key());
150  delayedVars.insert(iter.key());
151  }
152  }
153  }
154 
155  // Eliminating all nodes in query instance, except query
156  InstanceBayesNet< GUM_SCALAR > bn(*query);
157  DefaultTriangulation t(&(bn.moralGraph()), &(bn.modalities()));
158  std::vector< const DiscreteVariable* > elim_order;
159 
160  if (this->hasEvidence(query)) { insertEvidence__(query, pool); }
161 
162  for (auto attr = query->begin(); attr != query->end(); ++attr) {
163  pool.insert(
164  &(const_cast< Potential< GUM_SCALAR >& >((*(attr.val())).cpf())));
165  }
166 
167  for (size_t idx = 0; idx < t.eliminationOrder().size(); ++idx) {
168  if ((t.eliminationOrder()[idx] != node)
169  && (!delayedVars.exists(t.eliminationOrder()[idx]))) {
170  auto var_id = t.eliminationOrder()[idx];
171  const auto& var = bn.variable(var_id);
172  elim_order.push_back(&var);
173  }
174  }
175 
176  eliminateNodes(elim_order, pool, trash);
177 
178  // Eliminating delayed variables, if any
179  if (delayedVariables__.exists(query)) {
180  eliminateDelayedVariables__(query, pool, trash);
181  }
182 
183  eliminated.insert(query);
184  // Eliminating instance in elim_list
185  List< const PRMInstance< GUM_SCALAR >* > tmp_list;
186 
187  while (!elim_list.empty()) {
188  if (checkElimOrder__(query, elim_list.front())) {
189  if (!ignore.exists(elim_list.front())) {
191  elim_list.front(),
192  pool,
193  trash,
194  elim_list,
195  ignore,
196  eliminated);
197  }
198  } else {
199  tmp_list.insert(elim_list.front());
200  }
201 
202  elim_list.popFront();
203  }
204 
205  // Upward elimination
206  for (const auto chain: query->type().slotChains())
207  for (const auto parent: query->getInstances(chain->id()))
208  if (!ignore.exists(parent))
209  eliminateNodesUpward__(parent,
210  pool,
211  trash,
212  tmp_list,
213  ignore,
214  eliminated);
215  }
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:246
void eliminateDelayedVariables__(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:218
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:686
void insertEvidence__(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:666
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:626
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)
HashTable< const PRMInstance< GUM_SCALAR > *, Set< const DiscreteVariable *> *> delayedVariables__
Definition: SVE.h:131
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:716
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:632
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:353
+ 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 246 of file SVE_tpl.h.

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

253  {
254  Set< NodeId > delayedVars;
255  ignore.insert(i);
256  // Calling elimination over child instance
257  List< const PRMInstance< GUM_SCALAR >* > my_list;
258 
259  for (auto iter = i->beginInvRef(); iter != i->endInvRef(); ++iter) {
260  for (auto child = (*(iter.val())).begin(); child != (*(iter.val())).end();
261  ++child) {
262  if (!ignore.exists(child->first)) {
264  child->first,
265  pool,
266  trash,
267  my_list,
268  ignore,
269  eliminated);
270  } else if (!eliminated.exists(child->first)) {
271  addDelayedVariable__(child->first, i, iter.key());
272  delayedVars.insert(iter.key());
273  }
274  }
275  }
276 
277  // Eliminating all nodes in current instance
279  pool,
280  trash,
281  (delayedVars.empty() ? 0 : &delayedVars));
282  eliminated.insert(i);
283 
284  // Calling elimination over child's parents
285  for (const auto node: my_list) {
286  if (checkElimOrder__(i, node) && (node != from)) {
287  if (!ignore.exists(node)) {
289  node,
290  pool,
291  trash,
292  elim_list,
293  ignore,
294  eliminated);
295  }
296  } else if (node != from) {
297  elim_list.insert(node);
298  }
299  }
300 
301  // Adding parents instance to elim_list
302  for (const auto chain: i->type().slotChains()) {
303  for (const auto inst: i->getInstances(chain->id())) {
304  if (inst != from) { elim_list.insert(inst); }
305  }
306  }
307  }
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:246
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition: set_tpl.h:726
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:686
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:311
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:716
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:632
+ 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 353 of file SVE_tpl.h.

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

359  {
360  // Downward elimination
361  ignore.insert(i);
362 
363  for (auto iter = i->beginInvRef(); iter != i->endInvRef(); ++iter) {
364  for (auto child = (*(iter.val())).begin(); child != (*(iter.val())).end();
365  ++child) {
366  if (!ignore.exists(child->first)) {
368  child->first,
369  pool,
370  trash,
371  elim_list,
372  ignore,
373  eliminated);
374  }
375  }
376  }
377 
378  // Eliminating all nodes in i instance
379  variableElimination__(i, pool, trash);
380  eliminated.insert(i);
381  // Eliminating instance in elim_list
382  List< const PRMInstance< GUM_SCALAR >* > tmp_list;
383 
384  while (!elim_list.empty()) {
385  if (checkElimOrder__(i, elim_list.front())) {
386  if (!ignore.exists(elim_list.front())) {
388  elim_list.front(),
389  pool,
390  trash,
391  elim_list,
392  ignore,
393  eliminated);
394  }
395  } else {
396  tmp_list.insert(elim_list.front());
397  }
398 
399  elim_list.popFront();
400  }
401 
402  // Upward elimination
403  for (const auto chain: i->type().slotChains()) {
404  for (const auto parent: i->getInstances(chain->id())) {
405  if (!ignore.exists(parent)) {
406  eliminateNodesUpward__(parent,
407  pool,
408  trash,
409  tmp_list,
410  ignore,
411  eliminated);
412  }
413  }
414  }
415  }
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:246
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:686
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:311
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:353
+ 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 418 of file SVE_tpl.h.

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

422  {
423  // First we check if evidences are on inner nodes
424  bool inner = false;
425 
426  for (const auto& elt: this->evidence(i)) {
427  inner = i->type().isInputNode(i->get(elt.first))
428  || i->type().isInnerNode(i->get(elt.first));
429 
430  if (inner) { break; }
431  }
432 
433  // Evidence on inner nodes
434  if (inner) {
435  BucketSet tmp_pool;
436  insertEvidence__(i, tmp_pool);
437 
438  // We need a local to not eliminate queried inner nodes of the same
439  // class
440  for (const auto& elt: *i) {
441  tmp_pool.insert(
442  &(const_cast< Potential< GUM_SCALAR >& >(elt.second->cpf())));
443  }
444 
445  InstanceBayesNet< GUM_SCALAR > bn(*i);
446  DefaultTriangulation t(&(bn.moralGraph()), &(bn.modalities()));
447  const std::vector< NodeId >& full_elim_order = t.eliminationOrder();
448  // Removing Output nodes of elimination order
449  std::vector< const DiscreteVariable* > inner_elim_order;
450  std::vector< const DiscreteVariable* > output_elim_order;
451 
452  for (size_t idx = 0; idx < full_elim_order.size(); ++idx) {
453  auto var_id = full_elim_order[idx];
454  const auto& var = bn.variable(var_id);
455 
456  if (!i->type().isOutputNode(i->get(full_elim_order[idx]))) {
457  inner_elim_order.push_back(&var);
458  } else if (delayedVars != nullptr) {
459  if (!delayedVars->exists(full_elim_order[idx])) {
460  output_elim_order.push_back(&var);
461  }
462  } else {
463  output_elim_order.push_back(&var);
464  }
465  }
466 
467  eliminateNodes(inner_elim_order, tmp_pool, trash);
468 
469  // Now we add the new potentials in pool and eliminate output nodes
470  for (const auto pot: tmp_pool)
471  pool.insert(pot);
472 
473  if (!output_elim_order.empty())
474  eliminateNodes(output_elim_order, pool, trash);
475 
476  } else {
477  InstanceBayesNet< GUM_SCALAR > bn(*i);
478  insertEvidence__(i, pool);
479  insertLiftedNodes__(i, pool, trash);
480 
481  for (const auto agg: i->type().aggregates())
482  pool.insert(getAggPotential__(i, agg));
483 
484  try {
485  std::vector< const DiscreteVariable* > elim;
486 
487  for (auto iter = getElimOrder__(i->type()).begin();
488  iter != getElimOrder__(i->type()).end();
489  ++iter) {
490  const auto& var = bn.variable(*iter);
491  if (delayedVars != nullptr) {
492  if (!delayedVars->exists(*iter)) { elim.push_back(&var); }
493  } else {
494  elim.push_back(&var);
495  }
496  }
497 
498  eliminateNodes(elim, pool, trash);
499  } catch (NotFound&) {
500  GUM_ERROR(FatalError, "there should be at least one node here.");
501  }
502  }
503  }
Set< Potential< GUM_SCALAR > *> BucketSet
Code alias.
Definition: SVE.h:113
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
void insertLiftedNodes__(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:506
void insertEvidence__(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:666
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:626
void eliminateNodes(const std::vector< const DiscreteVariable * > &elim_order, Set< Potential< GUM_SCALAR > * > &pool, Set< Potential< GUM_SCALAR > * > &trash)
std::vector< NodeId > & getElimOrder__(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:674
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:698
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ Here is the call graph for this function:

◆ evidence() [1/4]

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

Returns EMap of evidences over i.

Exceptions
NotFoundif i has no evidence.

Definition at line 156 of file PRMInference_tpl.h.

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

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

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

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

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

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

188  {
189  try {
190  return *(evidences__[i]);
191  } catch (NotFound&) {
192  GUM_ERROR(NotFound, "this instance has no evidence.");
193  }
194  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> evidences__
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:235
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ 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 705 of file SVE_tpl.h.

705  {
706  // Do nothing
707  }

◆ 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 710 of file SVE_tpl.h.

710  {
711  // Do nothing
712  }

◆ 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 698 of file SVE_tpl.h.

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

700  {
701  return &(const_cast< Potential< GUM_SCALAR >& >(i->get(agg->id()).cpf()));
702  }
+ 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 674 of file SVE_tpl.h.

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

674  {
675  return *(elim_orders__[&c]);
676  }
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > *> elim_orders__
Definition: SVE.h:123
+ 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 197 of file PRMInference_tpl.h.

198  {
199  return evidences__.exists(&i);
200  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> evidences__
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:235

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

204  {
205  return evidences__.exists(i);
206  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> evidences__
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:235

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

209  {
210  return (hasEvidence(chain.first))
211  ? evidence(chain.first).exists(chain.second->id())
212  : false;
213  }
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 216 of file PRMInference_tpl.h.

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

◆ 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 581 of file SVE_tpl.h.

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

581  {
582  ClassDependencyGraph< GUM_SCALAR > cdg(*(this->prm_));
583  Sequence< const PRMClassElementContainer< GUM_SCALAR >* > class_elim_order;
584  std::list< NodeId > l;
585 
586  for (const auto node: cdg.dag().nodes()) {
587  if (cdg.dag().parents(node).empty()) { l.push_back(node); }
588  }
589 
590  Set< NodeId > visited_node;
591 
592  while (!l.empty()) {
593  visited_node.insert(l.front());
594 
595  if (!class_elim_order.exists(cdg.get(l.front()).first)) {
596  class_elim_order.insert(cdg.get(l.front()).first);
597  }
598 
599  for (const auto child: cdg.dag().children(l.front())) {
600  if (!visited_node.contains(child)) { l.push_back(child); }
601  }
602 
603  l.pop_front();
604  }
605 
607  for (auto c: class_elim_order) {
608  std::string name = c->name();
609  auto pos = name.find_first_of("<");
610  if (pos != std::string::npos) { name = name.substr(0, pos); }
611  try {
612  class_elim_order__->insert(name);
613  } catch (DuplicateElement&) {}
614  }
615  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:580
Sequence< std::string > * class_elim_order__
Definition: SVE.h:127
PRM< GUM_SCALAR > const * prm_
The PRM<GUM_SCALAR> on which inference is done.
Definition: PRMInference.h:213
virtual std::string name() const
Returns the name of the current inference algorithm.
Definition: SVE_tpl.h:739
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:632
void insert(const Key &k)
Insert an element at the end of the sequence.
Definition: sequence_tpl.h:409
+ 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 526 of file SVE_tpl.h.

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

526  {
527  BucketSet* lifted_pool = new BucketSet();
528  lifted_pools__.insert(&c, lifted_pool);
529  NodeSet inners, outers;
530 
531  for (const auto node: c.containerDag().nodes())
533  if (c.isOutputNode(c.get(node)))
534  outers.insert(node);
535  else if (!outers.exists(node))
536  inners.insert(node);
537 
538  lifted_pool->insert(
539  const_cast< Potential< GUM_SCALAR >* >(&(c.get(node).cpf())));
540  } else if (PRMClassElement< GUM_SCALAR >::isAggregate(c.get(node))) {
541  outers.insert(node);
542 
543  // We need to put in the output_elim_order aggregator's parents which
544  // are
545  // innner nodes
546  for (const auto par: c.containerDag().parents(node))
548  && c.isInnerNode(c.get(par))) {
549  inners.erase(par);
550  outers.insert(par);
551  }
552  }
553 
554  // Now we proceed with the elimination of inner attributes
555  ClassBayesNet< GUM_SCALAR > bn(c);
556  List< NodeSet > partial_ordering;
557 
558  if (inners.size()) partial_ordering.push_back(inners);
559 
560  if (outers.size()) partial_ordering.push_back(outers);
561 
562  PartialOrderedTriangulation t(&(bn.moralGraph()),
563  &(bn.modalities()),
564  &partial_ordering);
565 
566  for (size_t idx = 0; idx < inners.size(); ++idx)
567  eliminateNode(&(c.get(t.eliminationOrder()[idx]).type().variable()),
568  *lifted_pool,
570 
571  // If there is not only inner and input Attributes
572  if (outers.size()) {
573  elim_orders__.insert(
574  &c,
575  new std::vector< NodeId >(t.eliminationOrder().begin() + inners.size(),
576  t.eliminationOrder().end()));
577  }
578  }
Set< Potential< GUM_SCALAR > *> BucketSet
Code alias.
Definition: SVE.h:113
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > *> elim_orders__
Definition: SVE.h:123
HashTable< const PRMClass< GUM_SCALAR > *, BucketSet *> lifted_pools__
Definition: SVE.h:125
static INLINE bool isAttribute(const PRMClassElement< GUM_SCALAR > &elt)
Returns true if obj_ptr is of type PRMAttribute.
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.
BucketSet lifted_trash__
Definition: SVE.h:140
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:632
+ 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 666 of file SVE_tpl.h.

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

667  {
668  for (const auto& elt: this->evidence(i))
669  pool.insert(const_cast< Potential< GUM_SCALAR >* >(elt.second));
670  }
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 506 of file SVE_tpl.h.

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

508  {
509  SVE< GUM_SCALAR >::BucketSet* lifted_pool = 0;
510 
511  try {
512  lifted_pool = lifted_pools__[&(i->type())];
513  } catch (NotFound&) {
514  initLiftedNodes__(i->type());
515  lifted_pool = lifted_pools__[&(i->type())];
516  }
517 
518  for (const auto lifted_pot: *lifted_pool) {
519  Potential< GUM_SCALAR >* pot = copyPotential(i->bijection(), *lifted_pot);
520  pool.insert(pot);
521  trash.insert(pot);
522  }
523  }
Set< Potential< GUM_SCALAR > *> BucketSet
Code alias.
Definition: SVE.h:113
HashTable< const PRMClass< GUM_SCALAR > *, BucketSet *> lifted_pools__
Definition: SVE.h:125
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:28
void initLiftedNodes__(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:526
+ Here is the call graph for this function:

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

266  {
267  if (j.nbrDim() > 0) {
268  GUM_ERROR(OperationNotAllowed, "the given Potential is not empty.");
269  }
270 
271  for (auto chain = chains.begin(); chain != chains.end(); ++chain) {
272  j.add(chain->second->type().variable());
273  }
274 
275  joint_(chains, j);
276  }
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:54

◆ 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 651 of file SVE_tpl.h.

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

652  {
653  GUM_ERROR(FatalError, "Not implemented.");
654  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
+ 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 739 of file SVE_tpl.h.

739  {
740  return "SVE";
741  }

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

236  {
237  if (m.nbrDim() > 0) {
238  GUM_ERROR(OperationNotAllowed, "the given Potential is not empty.");
239  }
240 
241  if (hasEvidence(chain)) {
242  m.add(chain.second->type().variable());
243  const Potential< GUM_SCALAR >& e
244  = *(evidence(chain.first)[chain.second->id()]);
245  Instantiation i(m), j(e);
246 
247  for (i.setFirst(), j.setFirst(); !i.end(); i.inc(), j.inc())
248  m.set(i, e.get(j));
249  } else {
250  if (chain.second != &(chain.first->get(chain.second->safeName()))) {
251  typename PRMInference< GUM_SCALAR >::Chain good_chain
252  = std::make_pair(chain.first,
253  &(chain.first->get(chain.second->safeName())));
254  m.add(good_chain.second->type().variable());
255  posterior_(good_chain, m);
256  } else {
257  m.add(chain.second->type().variable());
258  posterior_(chain, m);
259  }
260  }
261  }
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:56
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ 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 618 of file SVE_tpl.h.

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

619  {
620  const PRMInstance< GUM_SCALAR >* i = chain.first;
621  const PRMAttribute< GUM_SCALAR >* elt = chain.second;
622  SVE< GUM_SCALAR >::BucketSet pool, trash;
623 
624  eliminateNodes__(i, elt->id(), pool, trash);
625 
626  std::vector< Potential< GUM_SCALAR >* > result;
627 
628  for (const auto pot: pool) {
629  if (pot->contains(elt->type().variable())) { result.push_back(pot); }
630  }
631 
632  while (result.size() > 1) {
633  auto& p1 = *(result.back());
634  result.pop_back();
635  auto& p2 = *(result.back());
636  result.pop_back();
637  auto mult = new Potential< GUM_SCALAR >(p1 * p2);
638  trash.insert(mult);
639  result.push_back(mult);
640  }
641 
642  m = *(result.back());
643  m.normalize();
644 
645  for (const auto pot: trash) {
646  delete pot;
647  }
648  }
Set< Potential< GUM_SCALAR > *> BucketSet
Code alias.
Definition: SVE.h:113
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:127
+ 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 221 of file PRMInference_tpl.h.

221  {
222  try {
223  if (EMap__(chain.first).exists(chain.second->id())) {
224  evidenceRemoved_(chain);
225  delete EMap__(chain.first)[chain.second->id()];
226  EMap__(chain.first).erase(chain.second->id());
227  }
228  } catch (NotFound&) {
229  // Ok, we are only removing
230  }
231  }
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...
EMap & EMap__(const PRMInstance< GUM_SCALAR > *i)
Private getter over evidences__, if necessary creates an EMap for i.

◆ 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 679 of file SVE_tpl.h.

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

679  {
680  auto pos = s.find_first_of("<");
681  if (pos != std::string::npos) { return s.substr(0, pos); }
682  return s;
683  }
+ 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 311 of file SVE_tpl.h.

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

314  {
315  if (this->hasEvidence(i)) {
316  eliminateNodesWithEvidence__(i, pool, trash, delayedVars);
317  } else {
318  insertLiftedNodes__(i, pool, trash);
319 
320  for (const auto agg: i->type().aggregates())
321  pool.insert(getAggPotential__(i, agg));
322 
323  try {
324  InstanceBayesNet< GUM_SCALAR > bn(*i);
325 
326  std::vector< const DiscreteVariable* > elim;
327 
328  for (const auto node: getElimOrder__(i->type())) {
329  const auto& var = bn.variable(node);
330  if (delayedVars != nullptr) {
331  if (!delayedVars->exists(node)) {
332  const auto& var = bn.variable(node);
333  elim.push_back(&var);
334  }
335  } else {
336  elim.push_back(&var);
337  }
338  }
339 
340  eliminateNodes(elim, pool, trash);
341  } catch (NotFound&) {
342  // Raised if there is no inner nodes to eliminate
343  }
344  }
345 
346  // Eliminating delayed variables, if any
347  if (delayedVariables__.exists(i)) {
348  eliminateDelayedVariables__(i, pool, trash);
349  }
350  }
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:418
void eliminateDelayedVariables__(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:218
void insertLiftedNodes__(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:506
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:626
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)
std::vector< NodeId > & getElimOrder__(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:674
HashTable< const PRMInstance< GUM_SCALAR > *, Set< const DiscreteVariable *> *> delayedVariables__
Definition: SVE.h:131
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:698
+ Here is the call graph for this function:

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 127 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 131 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 138 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 123 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 125 of file SVE.h.

◆ lifted_trash__

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

Definition at line 140 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 213 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 216 of file PRMInference.h.


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