aGrUM  0.16.0
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 marginal (const Chain &chain, Potential< GUM_SCALAR > &m)
 Compute the marginal 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 _marginal (const Chain &chain, Potential< GUM_SCALAR > &m)
 See PRMInference<GUM_SCALAR>::_marginal(). 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 66 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 121 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 114 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 117 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 96 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 60 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 69 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 65 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 620 of file SVE_tpl.h.

621  :
622  PRMInference< GUM_SCALAR >(prm, system),
623  __class_elim_order(0) {
624  GUM_CONSTRUCTOR(SVE);
625  }
Sequence< std::string > * __class_elim_order
Definition: SVE.h:128
SVE(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default Constructor.
Definition: SVE_tpl.h:620

◆ ~SVE()

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

Destructor.

Definition at line 108 of file SVE_tpl.h.

108  {
109  GUM_DESTRUCTOR(SVE);
110 
111  for (const auto& elt : __elim_orders)
112  delete elt.second;
113 
114  for (const auto& elt : __lifted_pools)
115  delete elt.second;
116 
117  if (__class_elim_order != nullptr) delete __class_elim_order;
118 
119  for (const auto trash : __lifted_trash)
120  delete trash;
121 
122  for (auto set : __delayedVariables)
123  delete set.second;
124  }
BucketSet __lifted_trash
Definition: SVE.h:141
Sequence< std::string > * __class_elim_order
Definition: SVE.h:128
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > *> __elim_orders
Definition: SVE.h:124
SVE(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default Constructor.
Definition: SVE_tpl.h:620
HashTable< const PRMClass< GUM_SCALAR > *, BucketSet *> __lifted_pools
Definition: SVE.h:126
HashTable< const PRMInstance< GUM_SCALAR > *, Set< const DiscreteVariable *> *> __delayedVariables
Definition: SVE.h:132

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

References gum::prm::SVE< GUM_SCALAR >::__delayedVariables, gum::prm::SVE< GUM_SCALAR >::__delayedVariablesCounters, gum::prm::PRMInstance< GUM_SCALAR >::get(), gum::HashTable< Key, Val, Alloc >::insert(), and gum::prm::PRMObject::name().

680  {
681  try {
682  __delayedVariables[i]->insert(&(j->get(id).type().variable()));
683  } catch (NotFound&) {
684  __delayedVariables.insert(i, new Set< const DiscreteVariable* >());
685  __delayedVariables[i]->insert(&(j->get(id).type().variable()));
686  } catch (DuplicateElement&) {
687  // happends if j->get(id) is parent of more than one variable in i
688  }
689 
690  static std::string dot = ".";
691 
692  try {
693  __delayedVariablesCounters[j->name() + dot + j->get(id).safeName()] += 1;
694  } catch (NotFound&) {
695  __delayedVariablesCounters.insert(j->name() + dot + j->get(id).safeName(),
696  1);
697  }
698  }
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:139
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
HashTable< const PRMInstance< GUM_SCALAR > *, Set< const DiscreteVariable *> *> __delayedVariables
Definition: SVE.h:132
+ 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 649 of file SVE_tpl.h.

References gum::prm::SVE< GUM_SCALAR >::__class_elim_order, gum::prm::SVE< GUM_SCALAR >::__initElimOrder(), gum::prm::SVE< GUM_SCALAR >::__trim(), gum::SequenceImplementation< Key, Alloc, Gen >::pos(), and gum::prm::PRMInstance< GUM_SCALAR >::type().

651  {
652  if (__class_elim_order == 0) { __initElimOrder(); }
653 
654  auto first_name = __trim(first->type().name());
655  auto second_name = __trim(second->type().name());
656  return (__class_elim_order->pos(first_name)
657  <= __class_elim_order->pos(second_name));
658  }
Idx pos(const Key &key) const
Returns the position of the object passed in argument (if it exists).
Definition: sequence_tpl.h:518
Sequence< std::string > * __class_elim_order
Definition: SVE.h:128
void __initElimOrder()
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:544
std::string __trim(const std::string &s)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:642
+ 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 210 of file SVE_tpl.h.

References gum::MultiDimBucket< GUM_SCALAR >::add(), gum::MultiDimBucket< GUM_SCALAR >::allVariables(), and gum::Set< Key, Alloc >::insert().

211  {
212  Set< Potential< GUM_SCALAR >* > toRemove;
213 
214  for (const auto var : *__delayedVariables[i]) {
215  MultiDimBucket< GUM_SCALAR >* bucket = new MultiDimBucket< GUM_SCALAR >();
216 
217  for (const auto pot : pool)
218  if (pot->contains(*var)) {
219  bucket->add(*pot);
220  toRemove.insert(pot);
221  }
222 
223  for (const auto pot : toRemove)
224  pool.erase(pot);
225 
226  for (const auto other : bucket->allVariables())
227  if (other != var) bucket->add(*other);
228 
229  Potential< GUM_SCALAR >* bucket_pot = new Potential< GUM_SCALAR >(bucket);
230  trash.insert(bucket_pot);
231  pool.insert(bucket_pot);
232  }
233  }
HashTable< const PRMInstance< GUM_SCALAR > *, Set< const DiscreteVariable *> *> __delayedVariables
Definition: SVE.h:132
+ 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 128 of file SVE_tpl.h.

References gum::prm::PRMInstance< GUM_SCALAR >::begin(), gum::prm::PRMInstance< GUM_SCALAR >::beginInvRef(), gum::prm::eliminateNodes(), gum::StaticTriangulation::eliminationOrder(), gum::List< Val, Alloc >::empty(), gum::prm::PRMInstance< GUM_SCALAR >::end(), gum::prm::PRMInstance< GUM_SCALAR >::endInvRef(), gum::Set< Key, Alloc >::exists(), gum::List< Val, Alloc >::front(), gum::prm::PRMInstance< GUM_SCALAR >::getInstances(), gum::Set< Key, Alloc >::insert(), gum::List< Val, Alloc >::insert(), gum::prm::InstanceBayesNet< GUM_SCALAR >::modalities(), gum::DAGmodel::moralGraph(), gum::List< Val, Alloc >::popFront(), gum::prm::PRMInstance< GUM_SCALAR >::type(), and gum::prm::InstanceBayesNet< GUM_SCALAR >::variable().

131  {
132  Set< const PRMInstance< GUM_SCALAR >* > ignore, eliminated;
133  Set< NodeId > delayedVars;
134  // Downward elimination
135  List< const PRMInstance< GUM_SCALAR >* > elim_list;
136  ignore.insert(query);
137 
138  for (auto iter = query->beginInvRef(); iter != query->endInvRef(); ++iter) {
139  for (auto child = (*(iter.val())).begin(); child != (*(iter.val())).end();
140  ++child) {
141  if (!ignore.exists(child->first)) {
143  query, child->first, pool, trash, elim_list, ignore, eliminated);
144  } else if (!eliminated.exists(child->first)) {
145  __addDelayedVariable(child->first, query, iter.key());
146  delayedVars.insert(iter.key());
147  }
148  }
149  }
150 
151  // Eliminating all nodes in query instance, except query
152  InstanceBayesNet< GUM_SCALAR > bn(*query);
153  DefaultTriangulation t(&(bn.moralGraph()), &(bn.modalities()));
154  std::vector< const DiscreteVariable* > elim_order;
155 
156  if (this->hasEvidence(query)) { __insertEvidence(query, pool); }
157 
158  for (auto attr = query->begin(); attr != query->end(); ++attr) {
159  pool.insert(
160  &(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)) {
176  __eliminateDelayedVariables(query, pool, trash);
177  }
178 
179  eliminated.insert(query);
180  // Eliminating instance in elim_list
181  List< const PRMInstance< GUM_SCALAR >* > tmp_list;
182 
183  while (!elim_list.empty()) {
184  if (__checkElimOrder(query, elim_list.front())) {
185  if (!ignore.exists(elim_list.front())) {
187  elim_list.front(),
188  pool,
189  trash,
190  elim_list,
191  ignore,
192  eliminated);
193  }
194  } else {
195  tmp_list.insert(elim_list.front());
196  }
197 
198  elim_list.popFront();
199  }
200 
201  // Upward elimination
202  for (const auto chain : query->type().slotChains())
203  for (const auto parent : query->getInstances(chain->id()))
204  if (!ignore.exists(parent))
206  parent, pool, trash, tmp_list, ignore, eliminated);
207  }
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:678
void __insertEvidence(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:629
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:331
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:649
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:607
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:236
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:210
HashTable< const PRMInstance< GUM_SCALAR > *, Set< const DiscreteVariable *> *> __delayedVariables
Definition: SVE.h:132
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
+ 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 236 of file SVE_tpl.h.

References gum::prm::PRMInstance< GUM_SCALAR >::beginInvRef(), gum::Set< Key, Alloc >::empty(), gum::prm::PRMInstance< GUM_SCALAR >::endInvRef(), gum::prm::PRMInstance< GUM_SCALAR >::getInstances(), gum::Set< Key, Alloc >::insert(), and gum::prm::PRMInstance< GUM_SCALAR >::type().

243  {
244  Set< NodeId > delayedVars;
245  ignore.insert(i);
246  // Calling elimination over child instance
247  List< const PRMInstance< GUM_SCALAR >* > my_list;
248 
249  for (auto iter = i->beginInvRef(); iter != i->endInvRef(); ++iter) {
250  for (auto child = (*(iter.val())).begin(); child != (*(iter.val())).end();
251  ++child) {
252  if (!ignore.exists(child->first)) {
254  i, child->first, pool, trash, my_list, ignore, eliminated);
255  } else if (!eliminated.exists(child->first)) {
256  __addDelayedVariable(child->first, i, iter.key());
257  delayedVars.insert(iter.key());
258  }
259  }
260  }
261 
262  // Eliminating all nodes in current instance
264  i, pool, trash, (delayedVars.empty() ? 0 : &delayedVars));
265  eliminated.insert(i);
266 
267  // Calling elimination over child's parents
268  for (const auto node : my_list) {
269  if (__checkElimOrder(i, node) && (node != from)) {
270  if (!ignore.exists(node)) {
272  i, node, pool, trash, elim_list, ignore, eliminated);
273  }
274  } else if (node != from) {
275  elim_list.insert(node);
276  }
277  }
278 
279  // Adding parents instance to elim_list
280  for (const auto chain : i->type().slotChains()) {
281  for (const auto inst : i->getInstances(chain->id())) {
282  if (inst != from) { elim_list.insert(inst); }
283  }
284  }
285  }
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:678
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition: set_tpl.h:707
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:649
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:289
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:236
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
+ 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 331 of file SVE_tpl.h.

References gum::prm::PRMInstance< GUM_SCALAR >::beginInvRef(), gum::prm::PRMInstance< GUM_SCALAR >::endInvRef(), gum::prm::PRMInstance< GUM_SCALAR >::getInstances(), gum::List< Val, Alloc >::insert(), and gum::prm::PRMInstance< GUM_SCALAR >::type().

337  {
338  // Downward elimination
339  ignore.insert(i);
340 
341  for (auto iter = i->beginInvRef(); iter != i->endInvRef(); ++iter) {
342  for (auto child = (*(iter.val())).begin(); child != (*(iter.val())).end();
343  ++child) {
344  if (!ignore.exists(child->first)) {
346  i, child->first, pool, trash, elim_list, ignore, eliminated);
347  }
348  }
349  }
350 
351  // Eliminating all nodes in i instance
352  __variableElimination(i, pool, trash);
353  eliminated.insert(i);
354  // Eliminating instance in elim_list
355  List< const PRMInstance< GUM_SCALAR >* > tmp_list;
356 
357  while (!elim_list.empty()) {
358  if (__checkElimOrder(i, elim_list.front())) {
359  if (!ignore.exists(elim_list.front())) {
361  i, elim_list.front(), pool, trash, elim_list, ignore, eliminated);
362  }
363  } else {
364  tmp_list.insert(elim_list.front());
365  }
366 
367  elim_list.popFront();
368  }
369 
370  // Upward elimination
371  for (const auto chain : i->type().slotChains()) {
372  for (const auto parent : i->getInstances(chain->id())) {
373  if (!ignore.exists(parent)) {
375  parent, pool, trash, tmp_list, ignore, eliminated);
376  }
377  }
378  }
379  }
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:331
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:649
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:289
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:236
+ 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 382 of file SVE_tpl.h.

References gum::prm::eliminateNodes(), gum::StaticTriangulation::eliminationOrder(), gum::Set< Key, Alloc >::exists(), gum::prm::PRMInstance< GUM_SCALAR >::get(), GUM_ERROR, gum::Set< Key, Alloc >::insert(), gum::prm::InstanceBayesNet< GUM_SCALAR >::modalities(), gum::DAGmodel::moralGraph(), gum::prm::PRMInstance< GUM_SCALAR >::type(), and gum::prm::InstanceBayesNet< GUM_SCALAR >::variable().

386  {
387  // First we check if evidences are on inner nodes
388  bool inner = false;
389 
390  for (const auto& elt : this->evidence(i)) {
391  inner = i->type().isInputNode(i->get(elt.first))
392  || i->type().isInnerNode(i->get(elt.first));
393 
394  if (inner) { break; }
395  }
396 
397  // Evidence on inner nodes
398  if (inner) {
399  BucketSet tmp_pool;
400  __insertEvidence(i, tmp_pool);
401 
402  // We need a local to not eliminate queried inner nodes of the same
403  // class
404  for (const auto& elt : *i) {
405  tmp_pool.insert(
406  &(const_cast< Potential< GUM_SCALAR >& >(elt.second->cpf())));
407  }
408 
409  InstanceBayesNet< GUM_SCALAR > bn(*i);
410  DefaultTriangulation t(&(bn.moralGraph()), &(bn.modalities()));
411  const std::vector< NodeId >& full_elim_order = t.eliminationOrder();
412  // Removing Output nodes of elimination order
413  std::vector< const DiscreteVariable* > inner_elim_order;
414  std::vector< const DiscreteVariable* > output_elim_order;
415 
416  for (size_t idx = 0; idx < full_elim_order.size(); ++idx) {
417  auto var_id = full_elim_order[idx];
418  const auto& var = bn.variable(var_id);
419 
420  if (!i->type().isOutputNode(i->get(full_elim_order[idx]))) {
421  inner_elim_order.push_back(&var);
422  } else if (delayedVars != nullptr) {
423  if (!delayedVars->exists(full_elim_order[idx])) {
424  output_elim_order.push_back(&var);
425  }
426  } else {
427  output_elim_order.push_back(&var);
428  }
429  }
430 
431  eliminateNodes(inner_elim_order, tmp_pool, trash);
432 
433  // Now we add the new potentials in pool and eliminate output nodes
434  for (const auto pot : tmp_pool)
435  pool.insert(pot);
436 
437  if (!output_elim_order.empty())
438  eliminateNodes(output_elim_order, pool, trash);
439 
440  } else {
441  InstanceBayesNet< GUM_SCALAR > bn(*i);
442  __insertEvidence(i, pool);
443  __insertLiftedNodes(i, pool, trash);
444 
445  for (const auto agg : i->type().aggregates())
446  pool.insert(__getAggPotential(i, agg));
447 
448  try {
449  std::vector< const DiscreteVariable* > elim;
450 
451  for (auto iter = __getElimOrder(i->type()).begin();
452  iter != __getElimOrder(i->type()).end();
453  ++iter) {
454  const auto& var = bn.variable(*iter);
455  if (delayedVars != nullptr) {
456  if (!delayedVars->exists(*iter)) { elim.push_back(&var); }
457  } else {
458  elim.push_back(&var);
459  }
460  }
461 
462  eliminateNodes(elim, pool, trash);
463  } catch (NotFound&) {
464  GUM_ERROR(FatalError, "there should be at least one node here.");
465  }
466  }
467  }
Set< Potential< GUM_SCALAR > *> BucketSet
Code alias.
Definition: SVE.h:114
void __insertEvidence(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:629
std::vector< NodeId > & __getElimOrder(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:637
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
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:661
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:607
void __insertLiftedNodes(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:470
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:55
+ 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 661 of file SVE_tpl.h.

References gum::prm::PRMInstance< GUM_SCALAR >::get(), and gum::prm::PRMClassElement< GUM_SCALAR >::id().

662  {
663  return &(const_cast< Potential< GUM_SCALAR >& >(i->get(agg->id()).cpf()));
664  }
+ 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 637 of file SVE_tpl.h.

References gum::prm::SVE< GUM_SCALAR >::__elim_orders.

637  {
638  return *(__elim_orders[&c]);
639  }
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > *> __elim_orders
Definition: SVE.h:124

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

References gum::Set< Key, Alloc >::contains(), gum::prm::ClassDependencyGraph< GUM_SCALAR >::dag(), gum::SequenceImplementation< Key, Alloc, std::is_scalar< Key >::value >::exists(), gum::prm::ClassDependencyGraph< GUM_SCALAR >::get(), gum::Set< Key, Alloc >::insert(), gum::SequenceImplementation< Key, Alloc, Gen >::insert(), and gum::SequenceImplementation< Key, Alloc, std::is_scalar< Key >::value >::insert().

Referenced by gum::prm::SVE< GUM_SCALAR >::__checkElimOrder().

544  {
545  ClassDependencyGraph< GUM_SCALAR > cdg(*(this->_prm));
546  Sequence< const PRMClassElementContainer< GUM_SCALAR >* > class_elim_order;
547  std::list< NodeId > l;
548 
549  for (const auto node : cdg.dag().nodes()) {
550  if (cdg.dag().parents(node).empty()) { l.push_back(node); }
551  }
552 
553  Set< NodeId > visited_node;
554 
555  while (!l.empty()) {
556  visited_node.insert(l.front());
557 
558  if (!class_elim_order.exists(cdg.get(l.front()).first)) {
559  class_elim_order.insert(cdg.get(l.front()).first);
560  }
561 
562  for (const auto child : cdg.dag().children(l.front())) {
563  if (!visited_node.contains(child)) { l.push_back(child); }
564  }
565 
566  l.pop_front();
567  }
568 
570  for (auto c : class_elim_order) {
571  std::string name = c->name();
572  auto pos = name.find_first_of("<");
573  if (pos != std::string::npos) { name = name.substr(0, pos); }
574  try {
575  __class_elim_order->insert(name);
576  } catch (DuplicateElement&) {}
577  }
578  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:581
Sequence< std::string > * __class_elim_order
Definition: SVE.h:128
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:701
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
void insert(const Key &k)
Insert an element at the end of the sequence.
Definition: sequence_tpl.h:408
+ Here is the call graph for this function:
+ Here is the caller 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 490 of file SVE_tpl.h.

References gum::prm::PRMClassElementContainer< GUM_SCALAR >::containerDag(), gum::prm::eliminateNode(), gum::StaticTriangulation::eliminationOrder(), gum::Set< Key, Alloc >::erase(), gum::Set< Key, Alloc >::exists(), gum::prm::PRMClass< GUM_SCALAR >::get(), gum::Set< Key, Alloc >::insert(), gum::prm::PRMClassElementContainer< GUM_SCALAR >::isInnerNode(), gum::prm::PRMClass< GUM_SCALAR >::isOutputNode(), gum::prm::ClassBayesNet< GUM_SCALAR >::modalities(), gum::DAGmodel::moralGraph(), gum::List< Val, Alloc >::push_back(), and gum::Set< Key, Alloc >::size().

490  {
491  BucketSet* lifted_pool = new BucketSet();
492  __lifted_pools.insert(&c, lifted_pool);
493  NodeSet inners, outers;
494 
495  for (const auto node : c.containerDag().nodes())
497  if (c.isOutputNode(c.get(node)))
498  outers.insert(node);
499  else if (!outers.exists(node))
500  inners.insert(node);
501 
502  lifted_pool->insert(
503  const_cast< Potential< GUM_SCALAR >* >(&(c.get(node).cpf())));
504  } else if (PRMClassElement< GUM_SCALAR >::isAggregate(c.get(node))) {
505  outers.insert(node);
506 
507  // We need to put in the output_elim_order aggregator's parents which
508  // are
509  // innner nodes
510  for (const auto par : c.containerDag().parents(node))
512  && c.isInnerNode(c.get(par))) {
513  inners.erase(par);
514  outers.insert(par);
515  }
516  }
517 
518  // Now we proceed with the elimination of inner attributes
519  ClassBayesNet< GUM_SCALAR > bn(c);
520  List< NodeSet > partial_ordering;
521 
522  if (inners.size()) partial_ordering.push_back(inners);
523 
524  if (outers.size()) partial_ordering.push_back(outers);
525 
526  PartialOrderedTriangulation t(
527  &(bn.moralGraph()), &(bn.modalities()), &partial_ordering);
528 
529  for (size_t idx = 0; idx < inners.size(); ++idx)
530  eliminateNode(&(c.get(t.eliminationOrder()[idx]).type().variable()),
531  *lifted_pool,
533 
534  // If there is not only inner and input Attributes
535  if (outers.size()) {
536  __elim_orders.insert(
537  &c,
538  new std::vector< NodeId >(t.eliminationOrder().begin() + inners.size(),
539  t.eliminationOrder().end()));
540  }
541  }
Set< Potential< GUM_SCALAR > *> BucketSet
Code alias.
Definition: SVE.h:114
BucketSet __lifted_trash
Definition: SVE.h:141
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 > *, std::vector< NodeId > *> __elim_orders
Definition: SVE.h:124
void eliminateNode(const DiscreteVariable *var, Set< Potential< GUM_SCALAR > * > &pool, Set< Potential< GUM_SCALAR > * > &trash)
Proceeds with the elimination of var in pool.
HashTable< const PRMClass< GUM_SCALAR > *, BucketSet *> __lifted_pools
Definition: SVE.h:126
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:613
+ 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 629 of file SVE_tpl.h.

References gum::prm::PRMInference< GUM_SCALAR >::evidence(), and gum::Set< Key, Alloc >::insert().

630  {
631  for (const auto& elt : this->evidence(i))
632  pool.insert(const_cast< Potential< GUM_SCALAR >* >(elt.second));
633  }
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 470 of file SVE_tpl.h.

References gum::prm::PRMInstance< GUM_SCALAR >::bijection(), gum::prm::copyPotential(), gum::Set< Key, Alloc >::insert(), and gum::prm::PRMInstance< GUM_SCALAR >::type().

472  {
473  SVE< GUM_SCALAR >::BucketSet* lifted_pool = 0;
474 
475  try {
476  lifted_pool = __lifted_pools[&(i->type())];
477  } catch (NotFound&) {
478  __initLiftedNodes(i->type());
479  lifted_pool = __lifted_pools[&(i->type())];
480  }
481 
482  for (const auto lifted_pot : *lifted_pool) {
483  Potential< GUM_SCALAR >* pot = copyPotential(i->bijection(), *lifted_pot);
484  pool.insert(pot);
485  trash.insert(pot);
486  }
487  }
Set< Potential< GUM_SCALAR > *> BucketSet
Code alias.
Definition: SVE.h:114
void __initLiftedNodes(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:490
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
HashTable< const PRMClass< GUM_SCALAR > *, BucketSet *> __lifted_pools
Definition: SVE.h:126
+ 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 642 of file SVE_tpl.h.

Referenced by gum::prm::SVE< GUM_SCALAR >::__checkElimOrder().

642  {
643  auto pos = s.find_first_of("<");
644  if (pos != std::string::npos) { return s.substr(0, pos); }
645  return s;
646  }
+ Here is the caller 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 289 of file SVE_tpl.h.

References gum::prm::eliminateNodes(), gum::Set< Key, Alloc >::exists(), gum::Set< Key, Alloc >::insert(), gum::prm::PRMInstance< GUM_SCALAR >::type(), and gum::prm::InstanceBayesNet< GUM_SCALAR >::variable().

292  {
293  if (this->hasEvidence(i)) {
294  __eliminateNodesWithEvidence(i, pool, trash, delayedVars);
295  } else {
296  __insertLiftedNodes(i, pool, trash);
297 
298  for (const auto agg : i->type().aggregates())
299  pool.insert(__getAggPotential(i, agg));
300 
301  try {
302  InstanceBayesNet< GUM_SCALAR > bn(*i);
303 
304  std::vector< const DiscreteVariable* > elim;
305 
306  for (const auto node : __getElimOrder(i->type())) {
307  const auto& var = bn.variable(node);
308  if (delayedVars != nullptr) {
309  if (!delayedVars->exists(node)) {
310  const auto& var = bn.variable(node);
311  elim.push_back(&var);
312  }
313  } else {
314  elim.push_back(&var);
315  }
316  }
317 
318  eliminateNodes(elim, pool, trash);
319  } catch (NotFound&) {
320  // Raised if there is no inner nodes to eliminate
321  }
322  }
323 
324  // Eliminating delayed variables, if any
325  if (__delayedVariables.exists(i)) {
326  __eliminateDelayedVariables(i, pool, trash);
327  }
328  }
std::vector< NodeId > & __getElimOrder(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:637
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:661
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:607
void __insertLiftedNodes(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:470
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 __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:382
void __eliminateDelayedVariables(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:210
HashTable< const PRMInstance< GUM_SCALAR > *, Set< const DiscreteVariable *> *> __delayedVariables
Definition: SVE.h:132
+ Here is the call graph for this function:

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

667  {
668  // Do nothing
669  }

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

672  {
673  // Do nothing
674  }

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

References GUM_ERROR.

615  {
616  GUM_ERROR(FatalError, "Not implemented.");
617  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55

◆ _marginal()

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

See PRMInference<GUM_SCALAR>::_marginal().

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

Definition at line 581 of file SVE_tpl.h.

References gum::prm::PRMClassElement< GUM_SCALAR >::id(), gum::Set< Key, Alloc >::insert(), gum::Potential< GUM_SCALAR >::normalize(), gum::prm::PRMAttribute< GUM_SCALAR >::type(), and gum::prm::PRMType::variable().

582  {
583  const PRMInstance< GUM_SCALAR >* i = chain.first;
584  const PRMAttribute< GUM_SCALAR >* elt = chain.second;
585  SVE< GUM_SCALAR >::BucketSet pool, trash;
586 
587  __eliminateNodes(i, elt->id(), pool, trash);
588 
589  std::vector< Potential< GUM_SCALAR >* > result;
590 
591  for (const auto pot : pool) {
592  if (pot->contains(elt->type().variable())) { result.push_back(pot); }
593  }
594 
595  while (result.size() > 1) {
596  auto& p1 = *(result.back());
597  result.pop_back();
598  auto& p2 = *(result.back());
599  result.pop_back();
600  auto mult = new Potential< GUM_SCALAR >(p1 * p2);
601  trash.insert(mult);
602  result.push_back(mult);
603  }
604 
605  m = *(result.back());
606  m.normalize();
607 
608  for (const auto pot : trash) {
609  delete pot;
610  }
611  }
Set< Potential< GUM_SCALAR > *> BucketSet
Code alias.
Definition: SVE.h:114
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:128
+ 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 109 of file PRMInference_tpl.h.

Referenced by gum::prm::o3prmr::O3prmrInterpreter::observe().

110  {
111  if (chain.first->exists(chain.second->id())) {
112  if ((p.nbrDim() != 1) || (!p.contains(chain.second->type().variable())))
113  GUM_ERROR(OperationNotAllowed,
114  "illegal evidence for the given PRMAttribute.");
115 
116  Potential< GUM_SCALAR >* e = new Potential< GUM_SCALAR >();
117  e->add(chain.second->type().variable());
118  Instantiation i(*e);
119 
120  for (i.setFirst(); !i.end(); i.inc())
121  e->set(i, p.get(i));
122 
123  PRMInference< GUM_SCALAR >::EMap& emap = __EMap(chain.first);
124 
125  if (emap.exists(chain.second->id())) {
126  delete emap[chain.second->id()];
127  emap[chain.second->id()] = e;
128  } else {
129  emap.insert(chain.second->id(), e);
130  }
131 
132  _evidenceAdded(chain);
133  } else {
134  GUM_ERROR(NotFound,
135  "the given PRMAttribute does not belong to this "
136  "Instance<GUM_SCALAR>.");
137  }
138  }
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:60
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ clearEvidence()

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

Remove all evidences.

Definition at line 36 of file PRMInference_tpl.h.

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

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

Referenced by gum::prm::SVED< GUM_SCALAR >::__insertEvidence(), gum::prm::SVE< GUM_SCALAR >::__insertEvidence(), gum::prm::StructuredInference< GUM_SCALAR >::__reduceAloneInstances(), and gum::prm::StructuredInference< GUM_SCALAR >::_marginal().

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:55
+ Here is the caller graph for this function:

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

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

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

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

Referenced by gum::prm::o3prmr::O3prmrInterpreter::observe(), and gum::prm::o3prmr::O3prmrInterpreter::unobserve().

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
+ Here is the caller graph for this function:

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

Referenced by gum::prm::StructuredInference< GUM_SCALAR >::__insertNodeInElimLists(), gum::prm::StructuredInference< GUM_SCALAR >::__reduceAloneInstances(), and gum::prm::StructuredInference< GUM_SCALAR >::_marginal().

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:48
+ Here is the caller 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 263 of file PRMInference_tpl.h.

265  {
266  if (j.nbrDim() > 0) {
267  GUM_ERROR(OperationNotAllowed, "the given Potential is not empty.");
268  }
269 
270  for (auto chain = chains.begin(); chain != chains.end(); ++chain) {
271  j.add(chain->second->type().variable());
272  }
273 
274  _joint(chains, j);
275  }
virtual void _joint(const std::vector< Chain > &queries, Potential< GUM_SCALAR > &j)=0
Generic method to compute the marginal of given element.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55

◆ marginal()

template<typename GUM_SCALAR>
INLINE void gum::prm::PRMInference< GUM_SCALAR >::marginal ( const Chain chain,
Potential< GUM_SCALAR > &  m 
)
inherited

Compute the marginal 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 marginal of chain.
Exceptions
NotFoundRaised if chain is invalid.
WrongTypeRaised 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.

Referenced by gum::prm::o3prmr::O3prmrInterpreter::query().

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 = std::make_pair(
252  chain.first, &(chain.first->get(chain.second->safeName())));
253  m.add(good_chain.second->type().variable());
254  _marginal(good_chain, m);
255  } else {
256  m.add(chain.second->type().variable());
257  _marginal(chain, m);
258  }
259  }
260  }
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
virtual void _marginal(const Chain &chain, Potential< GUM_SCALAR > &m)=0
Generic method to compute the marginal 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:57
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller 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 701 of file SVE_tpl.h.

701  {
702  return "SVE";
703  }

◆ 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.
WrongTypeRaised if the elt is not an PRMAttribute<GUM_SCALAR>.

Definition at line 221 of file PRMInference_tpl.h.

Referenced by gum::prm::o3prmr::O3prmrInterpreter::unobserve().

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.
+ Here is the caller 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 128 of file SVE.h.

Referenced by gum::prm::SVE< GUM_SCALAR >::__checkElimOrder().

◆ __delayedVariables

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

Definition at line 132 of file SVE.h.

Referenced by gum::prm::SVE< GUM_SCALAR >::__addDelayedVariable().

◆ __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 139 of file SVE.h.

Referenced by gum::prm::SVE< GUM_SCALAR >::__addDelayedVariable().

◆ __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 124 of file SVE.h.

Referenced by gum::prm::SVE< GUM_SCALAR >::__getElimOrder().

◆ __lifted_pools

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

Definition at line 126 of file SVE.h.

◆ __lifted_trash

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

Definition at line 141 of file SVE.h.

◆ _prm

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

◆ _sys


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