aGrUM  0.13.2
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 63 of file SVE.h.

Member Typedef Documentation

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

Code alias.

Definition at line 118 of file SVE.h.

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

Code alias.

Definition at line 111 of file SVE.h.

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

Code alias.

Definition at line 114 of file SVE.h.

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

Code alias.

Definition at line 93 of file SVE.h.

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

Code alias.

Definition at line 57 of file PRMInference.h.

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 66 of file PRMInference.h.

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

Code alias.

Definition at line 62 of file PRMInference.h.

Constructor & Destructor Documentation

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

618  :
619  PRMInference< GUM_SCALAR >(prm, system),
620  __class_elim_order(0) {
621  GUM_CONSTRUCTOR(SVE);
622  }
Sequence< std::string > * __class_elim_order
Definition: SVE.h:125
SVE(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default Constructor.
Definition: SVE_tpl.h:617
template<typename GUM_SCALAR >
gum::prm::SVE< GUM_SCALAR >::~SVE ( )

Destructor.

Definition at line 105 of file SVE_tpl.h.

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

Member Function Documentation

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 676 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().

678  {
679  try {
680  __delayedVariables[i]->insert(&(j->get(id).type().variable()));
681  } catch (NotFound&) {
682  __delayedVariables.insert(i, new Set< const DiscreteVariable* >());
683  __delayedVariables[i]->insert(&(j->get(id).type().variable()));
684  } catch (DuplicateElement&) {
685  // happends if j->get(id) is parent of more than one variable in i
686  }
687 
688  static std::string dot = ".";
689 
690  try {
691  __delayedVariablesCounters[j->name() + dot + j->get(id).safeName()] += 1;
692  } catch (NotFound&) {
693  __delayedVariablesCounters.insert(j->name() + dot + j->get(id).safeName(),
694  1);
695  }
696  }
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:136
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:129

+ Here is the call graph for this function:

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 646 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().

648  {
649  if (__class_elim_order == 0) { __initElimOrder(); }
650 
651  auto first_name = __trim(first->type().name());
652  auto second_name = __trim(second->type().name());
653  return (__class_elim_order->pos(first_name)
654  <= __class_elim_order->pos(second_name));
655  }
Sequence< std::string > * __class_elim_order
Definition: SVE.h:125
void __initElimOrder()
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:541
std::string __trim(const std::string &s)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:639
Idx pos(const Key &key) const
Returns the position of the object passed in argument (if it exists).
Definition: sequence_tpl.h:515

+ Here is the call graph for this function:

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

Returns true if second can be eliminated before first.

Definition at line 207 of file SVE_tpl.h.

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

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

+ Here is the call graph for this function:

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

Returns true if second can be eliminated before first.

Definition at line 125 of file SVE_tpl.h.

References gum::prm::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().

128  {
129  Set< const PRMInstance< GUM_SCALAR >* > ignore, eliminated;
130  Set< NodeId > delayedVars;
131  // Downward elimination
132  List< const PRMInstance< GUM_SCALAR >* > elim_list;
133  ignore.insert(query);
134 
135  for (auto iter = query->beginInvRef(); iter != query->endInvRef(); ++iter) {
136  for (auto child = (*(iter.val())).begin(); child != (*(iter.val())).end();
137  ++child) {
138  if (!ignore.exists(child->first)) {
140  query, child->first, pool, trash, elim_list, ignore, eliminated);
141  } else if (!eliminated.exists(child->first)) {
142  __addDelayedVariable(child->first, query, iter.key());
143  delayedVars.insert(iter.key());
144  }
145  }
146  }
147 
148  // Eliminating all nodes in query instance, except query
149  InstanceBayesNet< GUM_SCALAR > bn(*query);
150  DefaultTriangulation t(&(bn.moralGraph()), &(bn.modalities()));
151  std::vector< const DiscreteVariable* > elim_order;
152 
153  if (this->hasEvidence(query)) { __insertEvidence(query, pool); }
154 
155  for (auto attr = query->begin(); attr != query->end(); ++attr) {
156  pool.insert(
157  &(const_cast< Potential< GUM_SCALAR >& >((*(attr.val())).cpf())));
158  }
159 
160  for (size_t idx = 0; idx < t.eliminationOrder().size(); ++idx) {
161  if ((t.eliminationOrder()[idx] != node)
162  && (!delayedVars.exists(t.eliminationOrder()[idx]))) {
163  auto var_id = t.eliminationOrder()[idx];
164  const auto& var = bn.variable(var_id);
165  elim_order.push_back(&var);
166  }
167  }
168 
169  eliminateNodes(elim_order, pool, trash);
170 
171  // Eliminating delayed variables, if any
172  if (__delayedVariables.exists(query)) {
173  __eliminateDelayedVariables(query, pool, trash);
174  }
175 
176  eliminated.insert(query);
177  // Eliminating instance in elim_list
178  List< const PRMInstance< GUM_SCALAR >* > tmp_list;
179 
180  while (!elim_list.empty()) {
181  if (__checkElimOrder(query, elim_list.front())) {
182  if (!ignore.exists(elim_list.front())) {
184  elim_list.front(),
185  pool,
186  trash,
187  elim_list,
188  ignore,
189  eliminated);
190  }
191  } else {
192  tmp_list.insert(elim_list.front());
193  }
194 
195  elim_list.popFront();
196  }
197 
198  // Upward elimination
199  for (const auto chain : query->type().slotChains())
200  for (const auto parent : query->getInstances(chain->id()))
201  if (!ignore.exists(parent))
203  parent, pool, trash, tmp_list, ignore, eliminated);
204  }
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:676
bool hasEvidence() const
Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
void __insertEvidence(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:626
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:328
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:646
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:233
void eliminateNodes(const std::vector< const DiscreteVariable * > &elim_order, Set< Potential< GUM_SCALAR > * > &pool, Set< Potential< GUM_SCALAR > * > &trash)
void __eliminateDelayedVariables(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:207
HashTable< const PRMInstance< GUM_SCALAR > *, Set< const DiscreteVariable * > * > __delayedVariables
Definition: SVE.h:129
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:

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 233 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().

240  {
241  Set< NodeId > delayedVars;
242  ignore.insert(i);
243  // Calling elimination over child instance
244  List< const PRMInstance< GUM_SCALAR >* > my_list;
245 
246  for (auto iter = i->beginInvRef(); iter != i->endInvRef(); ++iter) {
247  for (auto child = (*(iter.val())).begin(); child != (*(iter.val())).end();
248  ++child) {
249  if (!ignore.exists(child->first)) {
251  i, child->first, pool, trash, my_list, ignore, eliminated);
252  } else if (!eliminated.exists(child->first)) {
253  __addDelayedVariable(child->first, i, iter.key());
254  delayedVars.insert(iter.key());
255  }
256  }
257  }
258 
259  // Eliminating all nodes in current instance
261  i, pool, trash, (delayedVars.empty() ? 0 : &delayedVars));
262  eliminated.insert(i);
263 
264  // Calling elimination over child's parents
265  for (const auto node : my_list) {
266  if (__checkElimOrder(i, node) && (node != from)) {
267  if (!ignore.exists(node)) {
269  i, node, pool, trash, elim_list, ignore, eliminated);
270  }
271  } else if (node != from) {
272  elim_list.insert(node);
273  }
274  }
275 
276  // Adding parents instance to elim_list
277  for (const auto chain : i->type().slotChains()) {
278  for (const auto inst : i->getInstances(chain->id())) {
279  if (inst != from) { elim_list.insert(inst); }
280  }
281  }
282  }
void __addDelayedVariable(const PRMInstance< GUM_SCALAR > *i, const PRMInstance< GUM_SCALAR > *j, NodeId id)
When there is a loop in the references some variable elimination must be delayed, this methods add su...
Definition: SVE_tpl.h:676
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:646
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:286
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:233
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:

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 328 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().

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

+ Here is the call graph for this function:

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 379 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().

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

+ Here is the call graph for this function:

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

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

660  {
661  return &(const_cast< Potential< GUM_SCALAR >& >(i->get(agg->id()).cpf()));
662  }

+ Here is the call graph for this function:

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

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

634  {
635  return *(__elim_orders[&c]);
636  }
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > * > __elim_orders
Definition: SVE.h:121
template<typename GUM_SCALAR >
void gum::prm::SVE< GUM_SCALAR >::__initElimOrder ( )
private

Returns true if second can be eliminated before first.

Definition at line 541 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().

541  {
542  ClassDependencyGraph< GUM_SCALAR > cdg(*(this->_prm));
543  Sequence< const PRMClassElementContainer< GUM_SCALAR >* > class_elim_order;
544  std::list< NodeId > l;
545 
546  for (const auto node : cdg.dag().nodes()) {
547  if (cdg.dag().parents(node).empty()) { l.push_back(node); }
548  }
549 
550  Set< NodeId > visited_node;
551 
552  while (!l.empty()) {
553  visited_node.insert(l.front());
554 
555  if (!class_elim_order.exists(cdg.get(l.front()).first)) {
556  class_elim_order.insert(cdg.get(l.front()).first);
557  }
558 
559  for (const auto child : cdg.dag().children(l.front())) {
560  if (!visited_node.contains(child)) { l.push_back(child); }
561  }
562 
563  l.pop_front();
564  }
565 
567  for (auto c : class_elim_order) {
568  std::string name = c->name();
569  auto pos = name.find_first_of("<");
570  if (pos != std::string::npos) { name = name.substr(0, pos); }
571  try {
572  __class_elim_order->insert(name);
573  } catch (DuplicateElement&) {}
574  }
575  }
Sequence< std::string > * __class_elim_order
Definition: SVE.h:125
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:581
PRM< GUM_SCALAR > const * _prm
The PRM<GUM_SCALAR> on which inference is done.
Definition: PRMInference.h:210
virtual std::string name() const
Returns the name of the current inference algorithm.
Definition: SVE_tpl.h:699
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:405

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 487 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().

487  {
488  BucketSet* lifted_pool = new BucketSet();
489  __lifted_pools.insert(&c, lifted_pool);
490  NodeSet inners, outers;
491 
492  for (const auto node : c.containerDag().nodes())
494  if (c.isOutputNode(c.get(node)))
495  outers.insert(node);
496  else if (!outers.exists(node))
497  inners.insert(node);
498 
499  lifted_pool->insert(
500  const_cast< Potential< GUM_SCALAR >* >(&(c.get(node).cpf())));
501  } else if (PRMClassElement< GUM_SCALAR >::isAggregate(c.get(node))) {
502  outers.insert(node);
503 
504  // We need to put in the output_elim_order aggregator's parents which
505  // are
506  // innner nodes
507  for (const auto par : c.containerDag().parents(node))
509  && c.isInnerNode(c.get(par))) {
510  inners.erase(par);
511  outers.insert(par);
512  }
513  }
514 
515  // Now we proceed with the elimination of inner attributes
516  ClassBayesNet< GUM_SCALAR > bn(c);
517  List< NodeSet > partial_ordering;
518 
519  if (inners.size()) partial_ordering.push_back(inners);
520 
521  if (outers.size()) partial_ordering.push_back(outers);
522 
523  PartialOrderedTriangulation t(
524  &(bn.moralGraph()), &(bn.modalities()), &partial_ordering);
525 
526  for (size_t idx = 0; idx < inners.size(); ++idx)
527  eliminateNode(&(c.get(t.eliminationOrder()[idx]).type().variable()),
528  *lifted_pool,
530 
531  // If there is not only inner and input Attributes
532  if (outers.size()) {
533  __elim_orders.insert(
534  &c,
535  new std::vector< NodeId >(t.eliminationOrder().begin() + inners.size(),
536  t.eliminationOrder().end()));
537  }
538  }
Set< Potential< GUM_SCALAR > * > BucketSet
Code alias.
Definition: SVE.h:111
BucketSet __lifted_trash
Definition: SVE.h:138
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > * > __elim_orders
Definition: SVE.h:121
static INLINE bool isAttribute(const PRMClassElement &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 &elt)
Return true if obj is of type PRMAggregate.
HashTable< const PRMClass< GUM_SCALAR > *, BucketSet * > __lifted_pools
Definition: SVE.h:123
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:

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

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

627  {
628  for (const auto& elt : this->evidence(i))
629  pool.insert(const_cast< Potential< GUM_SCALAR >* >(elt.second));
630  }
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.

+ Here is the call graph for this function:

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 467 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().

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

+ Here is the call graph for this function:

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

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

639  {
640  auto pos = s.find_first_of("<");
641  if (pos != std::string::npos) { return s.substr(0, pos); }
642  return s;
643  }

+ Here is the caller graph for this function:

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 286 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().

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

+ Here is the call graph for this function:

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

665  {
666  // Do nothing
667  }
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 670 of file SVE_tpl.h.

670  {
671  // Do nothing
672  }
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 611 of file SVE_tpl.h.

References GUM_ERROR.

612  {
613  GUM_ERROR(FatalError, "Not implemented.");
614  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66
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 578 of file SVE_tpl.h.

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

579  {
580  const PRMInstance< GUM_SCALAR >* i = chain.first;
581  const PRMAttribute< GUM_SCALAR >* elt = chain.second;
582  SVE< GUM_SCALAR >::BucketSet pool, trash;
583 
584  __eliminateNodes(i, elt->id(), pool, trash);
585 
586  std::vector< Potential< GUM_SCALAR >* > result;
587 
588  for (const auto pot : pool) {
589  if (pot->contains(elt->type().variable())) { result.push_back(pot); }
590  }
591 
592  while (result.size() > 1) {
593  auto& p1 = *(result.back());
594  result.pop_back();
595  auto& p2 = *(result.back());
596  result.pop_back();
597  auto mult = new Potential< GUM_SCALAR >(p1 * p2);
598  trash.insert(mult);
599  result.push_back(mult);
600  }
601 
602  m = *(result.back());
603  m.normalize();
604 
605  for (const auto pot : trash) {
606  delete pot;
607  }
608  }
Set< Potential< GUM_SCALAR > * > BucketSet
Code alias.
Definition: SVE.h:111
void __eliminateNodes(const PRMInstance< GUM_SCALAR > *query, NodeId id, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:125

+ Here is the call graph for this function:

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

References gum::prm::PRMInference< GUM_SCALAR >::__EMap(), gum::prm::PRMInference< GUM_SCALAR >::_evidenceAdded(), gum::MultiDimDecorator< GUM_SCALAR >::add(), gum::MultiDimDecorator< GUM_SCALAR >::contains(), gum::Instantiation::end(), gum::HashTable< Key, Val, Alloc >::exists(), gum::MultiDimDecorator< GUM_SCALAR >::get(), GUM_ERROR, gum::Instantiation::inc(), gum::HashTable< Key, Val, Alloc >::insert(), gum::MultiDimDecorator< GUM_SCALAR >::nbrDim(), gum::MultiDimDecorator< GUM_SCALAR >::set(), and gum::Instantiation::setFirst().

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

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Remove all evidences.

Definition at line 33 of file PRMInference_tpl.h.

Referenced by gum::prm::PRMInference< GUM_SCALAR >::operator=(), and gum::prm::PRMInference< GUM_SCALAR >::~PRMInference().

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

+ Here is the caller graph for this function:

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

References gum::prm::PRMInference< GUM_SCALAR >::__evidences, and GUM_ERROR.

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

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

+ Here is the caller graph for this function:

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

References gum::prm::PRMInference< GUM_SCALAR >::__evidences, and GUM_ERROR.

174  {
175  try {
176  return *(__evidences[i]);
177  } catch (NotFound&) {
178  GUM_ERROR(NotFound, "this instance has no evidence.");
179  }
180  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap * > __evidences
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:232
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66
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 163 of file PRMInference_tpl.h.

References gum::prm::PRMInference< GUM_SCALAR >::__evidences, and GUM_ERROR.

164  {
165  try {
166  return *(__evidences[&i]);
167  } catch (NotFound&) {
168  GUM_ERROR(NotFound, "this instance has no evidence.");
169  }
170  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap * > __evidences
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:232
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66
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 184 of file PRMInference_tpl.h.

References gum::prm::PRMInference< GUM_SCALAR >::__evidences, and GUM_ERROR.

185  {
186  try {
187  return *(__evidences[i]);
188  } catch (NotFound&) {
189  GUM_ERROR(NotFound, "this instance has no evidence.");
190  }
191  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap * > __evidences
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:232
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66
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 194 of file PRMInference_tpl.h.

References gum::prm::PRMInference< GUM_SCALAR >::__evidences.

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

195  {
196  return __evidences.exists(&i);
197  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap * > __evidences
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:232

+ Here is the caller graph for this function:

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

References gum::prm::PRMInference< GUM_SCALAR >::__evidences.

201  {
202  return __evidences.exists(i);
203  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap * > __evidences
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:232
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 206 of file PRMInference_tpl.h.

References gum::prm::PRMInference< GUM_SCALAR >::evidence(), gum::HashTable< Key, Val, Alloc >::exists(), and gum::prm::PRMInference< GUM_SCALAR >::hasEvidence().

206  {
207  return (hasEvidence(chain.first))
208  ? evidence(chain.first).exists(chain.second->id())
209  : false;
210  }
bool hasEvidence() const
Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
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.

+ Here is the call graph for this function:

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

References gum::prm::PRMInference< GUM_SCALAR >::__evidences.

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

213  {
214  return (__evidences.size() != (Size)0);
215  }
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
HashTable< const PRMInstance< GUM_SCALAR > *, EMap * > __evidences
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:232

+ Here is the caller graph for this function:

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

References gum::prm::PRMInference< GUM_SCALAR >::_joint(), gum::MultiDimDecorator< GUM_SCALAR >::add(), GUM_ERROR, and gum::MultiDimDecorator< GUM_SCALAR >::nbrDim().

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

+ Here is the call graph for this function:

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

References gum::prm::PRMInference< GUM_SCALAR >::_marginal(), gum::MultiDimDecorator< GUM_SCALAR >::add(), gum::prm::PRMInference< GUM_SCALAR >::evidence(), gum::MultiDimDecorator< GUM_SCALAR >::get(), GUM_ERROR, gum::prm::PRMInference< GUM_SCALAR >::hasEvidence(), gum::Instantiation::inc(), gum::MultiDimDecorator< GUM_SCALAR >::nbrDim(), gum::MultiDimDecorator< GUM_SCALAR >::set(), and gum::Instantiation::setFirst().

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

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

699  {
700  return "SVE";
701  }
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 218 of file PRMInference_tpl.h.

References gum::prm::PRMInference< GUM_SCALAR >::__EMap(), gum::prm::PRMInference< GUM_SCALAR >::_evidenceRemoved(), gum::HashTable< Key, Val, Alloc >::erase(), and gum::HashTable< Key, Val, Alloc >::exists().

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

218  {
219  try {
220  if (__EMap(chain.first).exists(chain.second->id())) {
221  _evidenceRemoved(chain);
222  delete __EMap(chain.first)[chain.second->id()];
223  __EMap(chain.first).erase(chain.second->id());
224  }
225  } catch (NotFound&) {
226  // Ok, we are only removing
227  }
228  }
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 call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

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

Definition at line 125 of file SVE.h.

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

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

Definition at line 129 of file SVE.h.

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

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 136 of file SVE.h.

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

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

Definition at line 121 of file SVE.h.

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

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

Definition at line 123 of file SVE.h.

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

Definition at line 138 of file SVE.h.

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

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