aGrUM  0.14.2
gum::prm::SVED< GUM_SCALAR > Class Template Reference

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

#include <agrum/PRM/SVED.h>

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

Public Member Functions

Constructors & destructor.
 SVED (const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &model)
 Default Constructor. More...
 
 ~SVED ()
 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::_evidenceAdded(). More...
 
virtual void _evidenceRemoved (const Chain &chain)
 See PRMInference::_evidenceRemoved(). More...
 
virtual void _marginal (const Chain &chain, Potential< GUM_SCALAR > &m)
 See PRMInference::_marginal(). More...
 
virtual void _joint (const std::vector< Chain > &queries, Potential< GUM_SCALAR > &j)
 See PRMInference::_joint(). More...
 

Detailed Description

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

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

Definition at line 60 of file SVED.h.

Member Typedef Documentation

◆ ArraySetIterator

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

Code alias.

Definition at line 115 of file SVED.h.

◆ BucketSet

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

Code alias.

Definition at line 108 of file SVED.h.

◆ BucketSetIterator

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

Code alias.

Definition at line 111 of file SVED.h.

◆ Chain

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

Code alias.

Definition at line 90 of file SVED.h.

◆ EMap

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.

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

Constructor & Destructor Documentation

◆ SVED()

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

Default Constructor.

Definition at line 494 of file SVED_tpl.h.

495  :
496  PRMInference< GUM_SCALAR >(prm, model),
497  __class_elim_order(0), __bb(*this) {
498  GUM_CONSTRUCTOR(SVED);
499  }
SVED(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &model)
Default Constructor.
Definition: SVED_tpl.h:494
Sequence< std::string > * __class_elim_order
Definition: SVED.h:127
StructuredBayesBall< GUM_SCALAR > __bb
Definition: SVED.h:129

◆ ~SVED()

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

Destructor.

Definition at line 32 of file SVED_tpl.h.

32  {
33  GUM_DESTRUCTOR(SVED);
34 
35  for (const auto& elt : __elim_orders)
36  delete elt.second;
37 
38  if (__class_elim_order != nullptr) delete __class_elim_order;
39  }
SVED(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &model)
Default Constructor.
Definition: SVED_tpl.h:494
Sequence< std::string > * __class_elim_order
Definition: SVED.h:127
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > *> __elim_orders
Definition: SVED.h:118

Member Function Documentation

◆ __checkElimOrder()

template<typename GUM_SCALAR >
INLINE bool gum::prm::SVED< 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 524 of file SVED_tpl.h.

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

Referenced by gum::prm::SVED< GUM_SCALAR >::__reduceElimList().

526  {
527  if (__class_elim_order == 0) { __initElimOrder(); }
528 
529  auto first_name = __trim(first->type().name());
530  auto second_name = __trim(second->type().name());
531  return (__class_elim_order->pos(first_name)
532  <= __class_elim_order->pos(second_name));
533  }
Idx pos(const Key &key) const
Returns the position of the object passed in argument (if it exists).
Definition: sequence_tpl.h:515
void __initElimOrder()
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:368
Sequence< std::string > * __class_elim_order
Definition: SVED.h:127
std::string __trim(const std::string &s)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:517
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __eliminateNodes()

template<typename GUM_SCALAR >
void gum::prm::SVED< 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 43 of file SVED_tpl.h.

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

46  {
47  Set< const PRMInstance< GUM_SCALAR >* > ignore;
48  ignore.insert(query);
49  // Extracting required attributes and slotchains
50  Set< NodeId >& attr_set = __getAttrSet(query);
51  Set< NodeId >& sc_set = __getSCSet(query);
52  // Downward elimination
53  List< const PRMInstance< GUM_SCALAR >* > elim_list;
54 
55  for (const auto attr : attr_set) {
56  try {
57  for (auto iter = query->getRefAttr(attr).begin();
58  iter != query->getRefAttr(attr).end();
59  ++iter)
60  if ((!ignore.exists(iter->first)) && (__bb.exists(iter->first)))
62  query, iter->first, pool, trash, elim_list, ignore);
63  } catch (NotFound&) {
64  // Ok
65  }
66  }
67 
68  // Eliminating all nodes in query instance, except query
69  InstanceBayesNet< GUM_SCALAR > bn(*query);
70  DefaultTriangulation t(&(bn.moralGraph()), &(bn.modalities()));
71  std::vector< const DiscreteVariable* > elim_order;
72 
73  if (this->hasEvidence(query)) __insertEvidence(query, pool);
74 
75  for (const auto attr : attr_set)
76  pool.insert(
77  &(const_cast< Potential< GUM_SCALAR >& >(query->get(attr).cpf())));
78 
79  for (size_t idx = 0; idx < t.eliminationOrder().size(); ++idx) {
80  if (t.eliminationOrder()[idx] != node) {
81  auto var_id = t.eliminationOrder()[idx];
82  const auto& var = bn.variable(var_id);
83  elim_order.push_back(&var);
84  }
85  }
86 
87  eliminateNodes(elim_order, pool, trash);
88  // Eliminating instance in elim_list
89  List< const PRMInstance< GUM_SCALAR >* > tmp_list;
90  __reduceElimList(query, elim_list, tmp_list, ignore, pool, trash);
91 
92  while (!elim_list.empty()) {
93  if (__checkElimOrder(query, elim_list.front())) {
94  if ((!ignore.exists(elim_list.front()))
95  && (__bb.exists(elim_list.front())))
97  query, elim_list.front(), pool, trash, elim_list, ignore);
98  } else if (__bb.exists(elim_list.front())) {
99  tmp_list.insert(elim_list.front());
100  }
101 
102  elim_list.popFront();
103  }
104 
105  // Upward elimination
106  for (const auto chain : sc_set)
107  for (const auto parent : query->getInstances(chain))
108  if ((!ignore.exists(parent)) && (__bb.exists(*parent)))
109  __eliminateNodesUpward(parent, pool, trash, tmp_list, ignore);
110  }
void __insertEvidence(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:504
Set< NodeId > & __getSCSet(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:567
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)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:113
StructuredBayesBall< GUM_SCALAR > __bb
Definition: SVED.h:129
bool __checkElimOrder(const PRMInstance< GUM_SCALAR > *first, const PRMInstance< GUM_SCALAR > *second)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:524
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 __eliminateNodesUpward(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, List< const PRMInstance< GUM_SCALAR > * > &elim_list, Set< const PRMInstance< GUM_SCALAR > * > &ignore)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:186
Set< NodeId > & __getAttrSet(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:556
void __reduceElimList(const PRMInstance< GUM_SCALAR > *i, List< const PRMInstance< GUM_SCALAR > * > &elim_list, List< const PRMInstance< GUM_SCALAR > * > &reduced_list, Set< const PRMInstance< GUM_SCALAR > * > &ignore, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:577
+ Here is the call graph for this function:

◆ __eliminateNodesDownward()

template<typename GUM_SCALAR >
void gum::prm::SVED< 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 
)
private

Returns true if second can be eliminated before first.

Definition at line 113 of file SVED_tpl.h.

References gum::prm::eliminateNodes(), gum::List< Val, Alloc >::empty(), gum::List< Val, Alloc >::front(), gum::prm::PRMInstance< GUM_SCALAR >::getInstances(), gum::prm::PRMInstance< GUM_SCALAR >::getRefAttr(), gum::Set< Key, Alloc >::insert(), gum::List< Val, Alloc >::popFront(), gum::prm::PRMInstance< GUM_SCALAR >::type(), and gum::prm::InstanceBayesNet< GUM_SCALAR >::variable().

Referenced by gum::prm::SVED< GUM_SCALAR >::__reduceElimList().

119  {
120  ignore.insert(i);
121  // Extracting required attributes and slotchains
122  Set< NodeId >& attr_set = __getAttrSet(i);
123  Set< NodeId >& sc_set = __getSCSet(i);
124  // Calling elimination over child instance
125  List< const PRMInstance< GUM_SCALAR >* > my_list;
126 
127  for (const auto attr : attr_set) {
128  try {
129  for (auto iter = i->getRefAttr(attr).begin();
130  iter != i->getRefAttr(attr).end();
131  ++iter)
132  if ((!ignore.exists(iter->first)) && (__bb.exists(iter->first)))
134  i, iter->first, pool, trash, my_list, ignore);
135  } catch (NotFound&) {
136  // Ok
137  }
138  }
139 
140  // Eliminating all nodes in current instance
141  if (this->hasEvidence(i)) {
142  __eliminateNodesWithEvidence(i, pool, trash);
143  } else {
144  __insertLiftedNodes(i, pool, trash);
145 
146  for (const auto agg : i->type().aggregates())
147  if (__bb.requisiteNodes(i).exists(agg->id()))
148  pool.insert(__getAggPotential(i, agg));
149 
150  try {
151  InstanceBayesNet< GUM_SCALAR > bn(*i);
152  std::vector< const DiscreteVariable* > elim_order;
153 
154  for (auto node : __getElimOrder(i->type())) {
155  const auto& var = bn.variable(node);
156  elim_order.push_back(&var);
157  }
158 
159  eliminateNodes(elim_order, pool, trash);
160  } catch (NotFound&) {
161  // Raised if there is no inner nodes to eliminate
162  }
163  }
164 
165  // Calling elimination over child's parents
166  while (!my_list.empty()) {
167  if (__checkElimOrder(i, my_list.front())) {
168  if ((!ignore.exists(my_list.front())) && (__bb.exists(my_list.front())))
170  i, my_list.front(), pool, trash, my_list, ignore);
171  } else if (__bb.exists(my_list.front())) {
172  elim_list.insert(my_list.front());
173  }
174 
175  my_list.popFront();
176  }
177 
178  // Adding parents instance to elim_list
179  for (const auto chain : sc_set)
180  for (const auto parent : i->getInstances(chain))
181  if ((!ignore.exists(parent)) && __bb.exists(parent) && (parent != from))
182  elim_list.insert(parent);
183  }
Set< NodeId > & __getSCSet(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:567
Potential< GUM_SCALAR > * __getAggPotential(const PRMInstance< GUM_SCALAR > *i, const PRMAggregate< GUM_SCALAR > *agg)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:536
std::vector< NodeId > & __getElimOrder(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:512
void __insertLiftedNodes(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:280
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)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:113
StructuredBayesBall< GUM_SCALAR > __bb
Definition: SVED.h:129
bool __checkElimOrder(const PRMInstance< GUM_SCALAR > *first, const PRMInstance< GUM_SCALAR > *second)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:524
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)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:259
Set< NodeId > & __getAttrSet(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:556
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __eliminateNodesUpward()

template<typename GUM_SCALAR >
void gum::prm::SVED< 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 
)
private

Returns true if second can be eliminated before first.

Definition at line 186 of file SVED_tpl.h.

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

191  {
192  ignore.insert(i);
193  // Extracting required attributes and slotchains
194  Set< NodeId >& attr_set = __getAttrSet(i);
195  Set< NodeId >& sc_set = __getSCSet(i);
196 
197  // Downward elimination
198  for (const auto attr : attr_set) {
199  try {
200  for (auto iter = i->getRefAttr(attr).begin();
201  iter != i->getRefAttr(attr).end();
202  ++iter)
203  if ((!ignore.exists(iter->first)) && (__bb.exists(iter->first)))
205  i, iter->first, pool, trash, elim_list, ignore);
206  } catch (NotFound&) {
207  // Ok
208  }
209  }
210 
211  // Eliminating all nodes in i instance
212  if (this->hasEvidence(i)) {
213  __eliminateNodesWithEvidence(i, pool, trash);
214  } else {
215  __insertLiftedNodes(i, pool, trash);
216 
217  for (const auto agg : i->type().aggregates())
218  if (__bb.requisiteNodes(i).exists(agg->id()))
219  pool.insert(__getAggPotential(i, agg));
220 
221  try {
222  InstanceBayesNet< GUM_SCALAR > bn(*i);
223  std::vector< const DiscreteVariable* > elim_order;
224 
225  for (auto node : __getElimOrder(i->type())) {
226  const auto& var = bn.variable(node);
227  elim_order.push_back(&var);
228  }
229  eliminateNodes(elim_order, pool, trash);
230  } catch (NotFound&) {
231  // Raised if there is no inner nodes to eliminate
232  }
233  }
234 
235  // Eliminating instance in elim_list
236  List< const PRMInstance< GUM_SCALAR >* > tmp_list;
237 
238  while (!elim_list.empty()) {
239  if (__checkElimOrder(i, elim_list.front())) {
240  if ((!ignore.exists(elim_list.front()))
241  && (__bb.exists(elim_list.front())))
243  i, elim_list.front(), pool, trash, elim_list, ignore);
244  } else if (__bb.exists(elim_list.front())) {
245  ignore.insert(elim_list.front());
246  }
247 
248  elim_list.popFront();
249  }
250 
251  // Upward elimination
252  for (const auto chain : sc_set)
253  for (const auto parent : i->getInstances(chain))
254  if ((!ignore.exists(parent)) && (__bb.exists(parent)))
255  __eliminateNodesUpward(parent, pool, trash, tmp_list, ignore);
256  }
Set< NodeId > & __getSCSet(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:567
Potential< GUM_SCALAR > * __getAggPotential(const PRMInstance< GUM_SCALAR > *i, const PRMAggregate< GUM_SCALAR > *agg)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:536
std::vector< NodeId > & __getElimOrder(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:512
void __insertLiftedNodes(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:280
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)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:113
StructuredBayesBall< GUM_SCALAR > __bb
Definition: SVED.h:129
bool __checkElimOrder(const PRMInstance< GUM_SCALAR > *first, const PRMInstance< GUM_SCALAR > *second)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:524
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)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:259
void __eliminateNodesUpward(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, List< const PRMInstance< GUM_SCALAR > * > &elim_list, Set< const PRMInstance< GUM_SCALAR > * > &ignore)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:186
Set< NodeId > & __getAttrSet(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:556
+ Here is the call graph for this function:

◆ __eliminateNodesWithEvidence()

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

Returns true if second can be eliminated before first.

Definition at line 259 of file SVED_tpl.h.

References gum::prm::eliminateNode(), gum::StaticTriangulation::eliminationOrder(), gum::Set< Key, Alloc >::insert(), gum::prm::InstanceBayesNet< GUM_SCALAR >::modalities(), and gum::DAGmodel::moralGraph().

260  {
261  // Adding required evidences
262  for (const auto& elt : this->evidence(i))
263  if (__bb.requisiteNodes(i).exists(elt.first))
264  pool.insert(const_cast< Potential< GUM_SCALAR >* >(elt.second));
265 
266  // Adding potentials and eliminating the remaining nodes
267  for (const auto& a : *i)
268  if (__bb.requisiteNodes(i).exists(a.first))
269  pool.insert(&(const_cast< Potential< GUM_SCALAR >& >(a.second->cpf())));
270 
271  InstanceBayesNet< GUM_SCALAR > bn(*i);
272  DefaultTriangulation t(&(bn.moralGraph()), &(bn.modalities()));
273  const std::vector< NodeId >& full_elim_order = t.eliminationOrder();
274 
275  for (auto var = full_elim_order.begin(); var != full_elim_order.end(); ++var)
276  eliminateNode(&(i->get(*var).type().variable()), pool, trash);
277  }
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
StructuredBayesBall< GUM_SCALAR > __bb
Definition: SVED.h:129
void eliminateNode(const DiscreteVariable *var, Set< Potential< GUM_SCALAR > * > &pool, Set< Potential< GUM_SCALAR > * > &trash)
Proceeds with the elimination of var in pool.
+ Here is the call graph for this function:

◆ __getAggPotential()

template<typename GUM_SCALAR >
INLINE Potential< GUM_SCALAR > * gum::prm::SVED< 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 536 of file SVED_tpl.h.

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

537  {
538  return &(
539  const_cast< Potential< GUM_SCALAR >& >(i->get(agg->safeName()).cpf()));
540  }
+ Here is the call graph for this function:

◆ __getAttrSet()

template<typename GUM_SCALAR >
INLINE Set< NodeId > & gum::prm::SVED< GUM_SCALAR >::__getAttrSet ( const PRMInstance< GUM_SCALAR > *  i)
private

Returns true if second can be eliminated before first.

Definition at line 556 of file SVED_tpl.h.

References gum::prm::SVED< GUM_SCALAR >::__bb, gum::prm::SVED< GUM_SCALAR >::__initReqSets(), and gum::prm::SVED< GUM_SCALAR >::__req_set.

556  {
557  try {
558  return *(__req_set[&(__bb.requisiteNodes(i))].first);
559  } catch (NotFound&) {
560  __initReqSets(i);
561  return *(__req_set[&(__bb.requisiteNodes(i))].first);
562  }
563  }
HashTable< const Set< NodeId > *, std::pair< Set< NodeId > *, Set< NodeId > *> > __req_set
First pair -> requisite Attributes Second pair -> requisite SlotChains.
Definition: SVED.h:135
void __initReqSets(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:463
StructuredBayesBall< GUM_SCALAR > __bb
Definition: SVED.h:129
+ Here is the call graph for this function:

◆ __getElimOrder()

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

Returns true if second can be eliminated before first.

Definition at line 512 of file SVED_tpl.h.

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

512  {
513  return *(__elim_orders[&c]);
514  }
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > *> __elim_orders
Definition: SVED.h:118

◆ __getSCSet()

template<typename GUM_SCALAR >
INLINE Set< NodeId > & gum::prm::SVED< GUM_SCALAR >::__getSCSet ( const PRMInstance< GUM_SCALAR > *  i)
private

Returns true if second can be eliminated before first.

Definition at line 567 of file SVED_tpl.h.

References gum::prm::SVED< GUM_SCALAR >::__bb, gum::prm::SVED< GUM_SCALAR >::__initReqSets(), and gum::prm::SVED< GUM_SCALAR >::__req_set.

567  {
568  try {
569  return *(__req_set[&(__bb.requisiteNodes(i))].second);
570  } catch (NotFound&) {
571  __initReqSets(i);
572  return *(__req_set[&(__bb.requisiteNodes(i))].second);
573  }
574  }
HashTable< const Set< NodeId > *, std::pair< Set< NodeId > *, Set< NodeId > *> > __req_set
First pair -> requisite Attributes Second pair -> requisite SlotChains.
Definition: SVED.h:135
void __initReqSets(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:463
StructuredBayesBall< GUM_SCALAR > __bb
Definition: SVED.h:129
+ Here is the call graph for this function:

◆ __initElimOrder()

template<typename GUM_SCALAR >
void gum::prm::SVED< GUM_SCALAR >::__initElimOrder ( )
private

Returns true if second can be eliminated before first.

Definition at line 368 of file SVED_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::SVED< GUM_SCALAR >::__checkElimOrder().

368  {
369  ClassDependencyGraph< GUM_SCALAR > cdg(*(this->_prm));
370  Sequence< const PRMClassElementContainer< GUM_SCALAR >* > class_elim_order;
371  std::list< NodeId > l;
372 
373  for (const auto node : cdg.dag().nodes()) {
374  if (cdg.dag().parents(node).empty()) { l.push_back(node); }
375  }
376 
377  Set< NodeId > visited_node;
378 
379  while (!l.empty()) {
380  visited_node.insert(l.front());
381 
382  if (!class_elim_order.exists(cdg.get(l.front()).first)) {
383  class_elim_order.insert(cdg.get(l.front()).first);
384  }
385 
386  for (const auto child : cdg.dag().children(l.front())) {
387  if (!visited_node.contains(child)) { l.push_back(child); }
388  }
389 
390  l.pop_front();
391  }
392 
394  for (auto c : class_elim_order) {
395  std::string name = c->name();
396  auto pos = name.find_first_of("<");
397  if (pos != std::string::npos) { name = name.substr(0, pos); }
398  try {
399  __class_elim_order->insert(name);
400  } catch (DuplicateElement&) {}
401  }
402  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:578
virtual std::string name() const
Returns the name of the current inference algorithm.
Definition: SVED_tpl.h:600
PRM< GUM_SCALAR > const * _prm
The PRM<GUM_SCALAR> on which inference is done.
Definition: PRMInference.h:210
Sequence< std::string > * __class_elim_order
Definition: SVED.h:127
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:610
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:

◆ __initLiftedNodes()

template<typename GUM_SCALAR >
void gum::prm::SVED< GUM_SCALAR >::__initLiftedNodes ( const PRMInstance< GUM_SCALAR > *  i,
BucketSet trash 
)
private

Returns true if second can be eliminated before first.

Definition at line 299 of file SVED_tpl.h.

References gum::prm::PRMClassElementContainer< GUM_SCALAR >::containerDag(), gum::prm::eliminateNode(), gum::Set< Key, Alloc >::erase(), gum::Set< Key, Alloc >::exists(), gum::prm::PRMClass< GUM_SCALAR >::get(), gum::Set< Key, Alloc >::insert(), gum::prm::PRMClass< GUM_SCALAR >::isOutputNode(), gum::prm::ClassBayesNet< GUM_SCALAR >::modalities(), gum::DAGmodel::moralGraph(), gum::List< Val, Alloc >::pushBack(), gum::Set< Key, Alloc >::size(), gum::prm::PRMInstance< GUM_SCALAR >::type(), and gum::prm::PRMClassElement< GUM_SCALAR >::type().

300  {
301  PRMClass< GUM_SCALAR >& c = const_cast< PRMClass< GUM_SCALAR >& >(i->type());
302  BucketSet* lifted_pool = new BucketSet();
303  __lifted_pools.insert(&(__bb.requisiteNodes(i)), lifted_pool);
304 
305  for (const auto node : __bb.requisiteNodes(i))
307  lifted_pool->insert(
308  const_cast< Potential< GUM_SCALAR >* >(&(c.get(node).cpf())));
309 
310  NodeSet inners, outers, ignore;
311 
312  for (const auto& elt : *i) {
313  if (__bb.requisiteNodes(*i).exists(elt.first)) {
314  if (PRMClassElement< GUM_SCALAR >::isAttribute(c.get(elt.first))) {
315  if (c.isOutputNode(c.get(elt.first)))
316  outers.insert(elt.first);
317  else if (!outers.exists(elt.first))
318  inners.insert(elt.first);
320  c.get(elt.first))) {
321  outers.insert(elt.first);
322 
323  // We need to put in the output_elim_order aggregator's parents
324  // which are
325  // innner nodes
326  for (const auto par : c.containerDag().parents(elt.first))
327  if (PRMClassElement< GUM_SCALAR >::isAttribute(i->type().get(par))
328  && i->type().isInnerNode(i->type().get(par))
329  && __bb.requisiteNodes(i).exists(par)) {
330  inners.erase(par);
331  outers.insert(par);
332  }
333  }
334  } else {
335  ignore.insert(elt.first);
336  }
337  }
338 
339  // Now we proceed with the elimination of inner attributes
340  ClassBayesNet< GUM_SCALAR > bn(c);
341  List< NodeSet > partial_ordering;
342 
343  if (inners.size()) partial_ordering.pushBack(inners);
344 
345  if (outers.size()) partial_ordering.pushBack(outers);
346 
347  if (ignore.size()) partial_ordering.pushBack(ignore);
348 
349  GUM_ASSERT(inners.size() || outers.size());
350  PartialOrderedTriangulation t(
351  &(bn.moralGraph()), &(bn.modalities()), &partial_ordering);
352 
353  for (size_t idx = 0; idx < inners.size(); ++idx)
354  eliminateNode(&(c.get(t.eliminationOrder()[idx]).type().variable()),
355  *lifted_pool,
356  trash);
357 
358  // If there is not only inner and input Attributes
359  if (outers.size()) {
360  __elim_orders.insert(
361  &c,
362  new std::vector< NodeId >(t.eliminationOrder().begin() + inners.size(),
363  t.eliminationOrder().end()));
364  }
365  }
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
Set< Potential< GUM_SCALAR > *> BucketSet
Code alias.
Definition: SVED.h:108
static INLINE bool isAttribute(const PRMClassElement< GUM_SCALAR > &elt)
Returns true if obj_ptr is of type PRMAttribute.
StructuredBayesBall< GUM_SCALAR > __bb
Definition: SVED.h:129
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 Set< NodeId > *, BucketSet *> __lifted_pools
The Set<NodeId> returned by StructuredBayesBall<GUM_SCALAR> is unique for each family of instances wi...
Definition: SVED.h:125
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > *> __elim_orders
Definition: SVED.h:118
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:610
+ Here is the call graph for this function:

◆ __initReqSets()

template<typename GUM_SCALAR >
void gum::prm::SVED< GUM_SCALAR >::__initReqSets ( const PRMInstance< GUM_SCALAR > *  i)
private

Returns true if second can be eliminated before first.

Definition at line 463 of file SVED_tpl.h.

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

Referenced by gum::prm::SVED< GUM_SCALAR >::__getAttrSet(), and gum::prm::SVED< GUM_SCALAR >::__getSCSet().

463  {
464  Set< NodeId >* attr_set = new Set< NodeId >();
465  Set< NodeId >* sc_set = new Set< NodeId >();
466 
467  for (const auto node : __bb.requisiteNodes(i)) {
468  switch (i->type().get(node).elt_type()) {
471  attr_set->insert(node);
472  break;
473  }
474 
476  sc_set->insert(node);
477  break;
478  }
479 
480  default: {
481  GUM_ERROR(FatalError,
482  "There should not be elements other"
483  " than PRMAttribute<GUM_SCALAR> and SlotChain.");
484  }
485  }
486  }
487 
488  __req_set.insert(
489  &(__bb.requisiteNodes(i)),
490  std::pair< Set< NodeId >*, Set< NodeId >* >(attr_set, sc_set));
491  }
HashTable< const Set< NodeId > *, std::pair< Set< NodeId > *, Set< NodeId > *> > __req_set
First pair -> requisite Attributes Second pair -> requisite SlotChains.
Definition: SVED.h:135
StructuredBayesBall< GUM_SCALAR > __bb
Definition: SVED.h:129
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:610
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __insertEvidence()

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

Returns true if second can be eliminated before first.

Definition at line 504 of file SVED_tpl.h.

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

505  {
506  for (const auto& elt : this->evidence(i))
507  pool.insert(const_cast< Potential< GUM_SCALAR >* >(elt.second));
508  }
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::SVED< 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 280 of file SVED_tpl.h.

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

281  {
282  BucketSet* lifted_pool = nullptr;
283 
284  try {
285  lifted_pool = __lifted_pools[&(__bb.requisiteNodes(i))];
286  } catch (NotFound&) {
287  __initLiftedNodes(i, trash);
288  lifted_pool = __lifted_pools[&(__bb.requisiteNodes(i))];
289  }
290 
291  for (const auto lifted_pot : *lifted_pool) {
292  Potential< GUM_SCALAR >* pot = copyPotential(i->bijection(), *lifted_pot);
293  pool.insert(pot);
294  trash.insert(pot);
295  }
296  }
Set< Potential< GUM_SCALAR > *> BucketSet
Code alias.
Definition: SVED.h:108
void __initLiftedNodes(const PRMInstance< GUM_SCALAR > *i, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:299
StructuredBayesBall< GUM_SCALAR > __bb
Definition: SVED.h:129
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 Set< NodeId > *, BucketSet *> __lifted_pools
The Set<NodeId> returned by StructuredBayesBall<GUM_SCALAR> is unique for each family of instances wi...
Definition: SVED.h:125
+ Here is the call graph for this function:

◆ __reduceElimList()

template<typename GUM_SCALAR >
INLINE void gum::prm::SVED< GUM_SCALAR >::__reduceElimList ( const PRMInstance< GUM_SCALAR > *  i,
List< const PRMInstance< GUM_SCALAR > * > &  elim_list,
List< const PRMInstance< GUM_SCALAR > * > &  reduced_list,
Set< const PRMInstance< GUM_SCALAR > * > &  ignore,
BucketSet pool,
BucketSet trash 
)
private

Returns true if second can be eliminated before first.

Definition at line 577 of file SVED_tpl.h.

References gum::prm::SVED< GUM_SCALAR >::__bb, gum::prm::SVED< GUM_SCALAR >::__checkElimOrder(), and gum::prm::SVED< GUM_SCALAR >::__eliminateNodesDownward().

583  {
584  while (!elim_list.empty()) {
585  if (__checkElimOrder(i, elim_list.front())) {
586  if ((!ignore.exists(elim_list.front()))
587  && (__bb.exists(elim_list.front()))) {
589  i, elim_list.front(), pool, trash, elim_list, ignore);
590  }
591  } else if (__bb.exists(elim_list.front())) {
592  reduced_list.insert(elim_list.front());
593  }
594 
595  elim_list.popFront();
596  }
597  }
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)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:113
StructuredBayesBall< GUM_SCALAR > __bb
Definition: SVED.h:129
bool __checkElimOrder(const PRMInstance< GUM_SCALAR > *first, const PRMInstance< GUM_SCALAR > *second)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:524
+ Here is the call graph for this function:

◆ __trim()

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

Returns true if second can be eliminated before first.

Definition at line 517 of file SVED_tpl.h.

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

517  {
518  auto pos = s.find_first_of("<");
519  if (pos != std::string::npos) { return s.substr(0, pos); }
520  return s;
521  }
+ Here is the caller graph for this function:

◆ _evidenceAdded()

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

See PRMInference::_evidenceAdded().

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

Definition at line 543 of file SVED_tpl.h.

544  {
545  // Do nothing
546  }

◆ _evidenceRemoved()

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

See PRMInference::_evidenceRemoved().

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

Definition at line 549 of file SVED_tpl.h.

550  {
551  // Do nothing
552  }

◆ _joint()

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

See PRMInference::_joint().

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

Definition at line 457 of file SVED_tpl.h.

References GUM_ERROR.

458  {
459  GUM_ERROR(FatalError, "Not implemented.");
460  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52

◆ _marginal()

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

See PRMInference::_marginal().

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

Definition at line 405 of file SVED_tpl.h.

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

406  {
407  const PRMInstance< GUM_SCALAR >* i = chain.first;
408  const PRMAttribute< GUM_SCALAR >* elt = chain.second;
409  SVED< GUM_SCALAR >::BucketSet pool, trash;
410  __bb.compute(i, elt->id());
411  __eliminateNodes(i, elt->id(), pool, trash);
412 
413  std::vector< const Potential< GUM_SCALAR >* > result;
414  for (auto pot : pool) {
415  if (pot->contains(*(m.variablesSequence().atPos(0))))
416  result.push_back(pot);
417  }
418 
419  while (result.size() > 1) {
420  const auto& p1 = *(result.back());
421  result.pop_back();
422  const auto& p2 = *(result.back());
423  result.pop_back();
424  auto mult = new Potential< GUM_SCALAR >(p1 * p2);
425  result.push_back(mult);
426  trash.insert(mult);
427  }
428 
429  m = *(result.back());
430  m.normalize();
431 
432  GUM_ASSERT(m.nbrDim() == (Size)1);
433 
434  // cleaning up the mess
435  for (const auto pot : trash)
436  delete pot;
437 
438  for (const auto& elt : __lifted_pools)
439  delete elt.second;
440 
441  __lifted_pools.clear();
442 
443  for (const auto& elt : __req_set) {
444  delete elt.second.first;
445  delete elt.second.second;
446  }
447 
448  __req_set.clear();
449 
450  for (const auto elt : __elim_orders)
451  delete elt.second;
452 
453  __elim_orders.clear();
454  }
HashTable< const Set< NodeId > *, std::pair< Set< NodeId > *, Set< NodeId > *> > __req_set
First pair -> requisite Attributes Second pair -> requisite SlotChains.
Definition: SVED.h:135
void __eliminateNodes(const PRMInstance< GUM_SCALAR > *query, NodeId id, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:43
Set< Potential< GUM_SCALAR > *> BucketSet
Code alias.
Definition: SVED.h:108
StructuredBayesBall< GUM_SCALAR > __bb
Definition: SVED.h:129
HashTable< const Set< NodeId > *, BucketSet *> __lifted_pools
The Set<NodeId> returned by StructuredBayesBall<GUM_SCALAR> is unique for each family of instances wi...
Definition: SVED.h:125
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > *> __elim_orders
Definition: SVED.h:118
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
+ 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 106 of file PRMInference_tpl.h.

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:52
+ 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 33 of file PRMInference_tpl.h.

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

◆ 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 153 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().

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

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:52

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

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:52

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

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:52

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

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:

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

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

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

206  {
207  return (hasEvidence(chain.first))
208  ? evidence(chain.first).exists(chain.second->id())
209  : false;
210  }
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 213 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().

213  {
214  return (__evidences.size() != (Size)0);
215  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> __evidences
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:232
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
+ 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 260 of file PRMInference_tpl.h.

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:52

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

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  }
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:54
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ Here is the caller graph for this function:

◆ name()

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

Returns the name of the current inference algorithm.

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

Definition at line 600 of file SVED_tpl.h.

600  {
601  return "SVED";
602  }

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

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 caller graph for this function:

Member Data Documentation

◆ __bb

template<typename GUM_SCALAR>
StructuredBayesBall< GUM_SCALAR > gum::prm::SVED< GUM_SCALAR >::__bb
private

◆ __class_elim_order

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

Definition at line 127 of file SVED.h.

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

◆ __elim_orders

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

Definition at line 118 of file SVED.h.

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

◆ __lifted_pools

template<typename GUM_SCALAR>
HashTable< const Set< NodeId >*, BucketSet* > gum::prm::SVED< GUM_SCALAR >::__lifted_pools
private

The Set<NodeId> returned by StructuredBayesBall<GUM_SCALAR> is unique for each family of instances with the same requisite set (thus the same lifted potentials).

Definition at line 125 of file SVED.h.

◆ __req_set

template<typename GUM_SCALAR>
HashTable< const Set< NodeId >*, std::pair< Set< NodeId >*, Set< NodeId >* > > gum::prm::SVED< GUM_SCALAR >::__req_set
private

First pair -> requisite Attributes Second pair -> requisite SlotChains.

Definition at line 135 of file SVED.h.

Referenced by gum::prm::SVED< GUM_SCALAR >::__getAttrSet(), and gum::prm::SVED< GUM_SCALAR >::__getSCSet().

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