aGrUM  0.16.0
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 63 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 118 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 111 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 114 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 93 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 60 of file PRMInference.h.

◆ EMapConstIterator

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

Code alias.

Definition at line 69 of file PRMInference.h.

◆ EMapIterator

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

Code alias.

Definition at line 65 of file PRMInference.h.

Constructor & Destructor Documentation

◆ 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 497 of file SVED_tpl.h.

498  :
499  PRMInference< GUM_SCALAR >(prm, model),
500  __class_elim_order(0), __bb(*this) {
501  GUM_CONSTRUCTOR(SVED);
502  }
SVED(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &model)
Default Constructor.
Definition: SVED_tpl.h:497
Sequence< std::string > * __class_elim_order
Definition: SVED.h:130
StructuredBayesBall< GUM_SCALAR > __bb
Definition: SVED.h:132

◆ ~SVED()

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

Destructor.

Definition at line 35 of file SVED_tpl.h.

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

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

529  {
530  if (__class_elim_order == 0) { __initElimOrder(); }
531 
532  auto first_name = __trim(first->type().name());
533  auto second_name = __trim(second->type().name());
534  return (__class_elim_order->pos(first_name)
535  <= __class_elim_order->pos(second_name));
536  }
Idx pos(const Key &key) const
Returns the position of the object passed in argument (if it exists).
Definition: sequence_tpl.h:518
void __initElimOrder()
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:371
Sequence< std::string > * __class_elim_order
Definition: SVED.h:130
std::string __trim(const std::string &s)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:520
+ 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 46 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().

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

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

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

263  {
264  // Adding required evidences
265  for (const auto& elt : this->evidence(i))
266  if (__bb.requisiteNodes(i).exists(elt.first))
267  pool.insert(const_cast< Potential< GUM_SCALAR >* >(elt.second));
268 
269  // Adding potentials and eliminating the remaining nodes
270  for (const auto& a : *i)
271  if (__bb.requisiteNodes(i).exists(a.first))
272  pool.insert(&(const_cast< Potential< GUM_SCALAR >& >(a.second->cpf())));
273 
274  InstanceBayesNet< GUM_SCALAR > bn(*i);
275  DefaultTriangulation t(&(bn.moralGraph()), &(bn.modalities()));
276  const std::vector< NodeId >& full_elim_order = t.eliminationOrder();
277 
278  for (auto var = full_elim_order.begin(); var != full_elim_order.end(); ++var)
279  eliminateNode(&(i->get(*var).type().variable()), pool, trash);
280  }
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
StructuredBayesBall< GUM_SCALAR > __bb
Definition: SVED.h:132
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 539 of file SVED_tpl.h.

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

540  {
541  return &(
542  const_cast< Potential< GUM_SCALAR >& >(i->get(agg->safeName()).cpf()));
543  }
+ 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 559 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.

559  {
560  try {
561  return *(__req_set[&(__bb.requisiteNodes(i))].first);
562  } catch (NotFound&) {
563  __initReqSets(i);
564  return *(__req_set[&(__bb.requisiteNodes(i))].first);
565  }
566  }
HashTable< const Set< NodeId > *, std::pair< Set< NodeId > *, Set< NodeId > *> > __req_set
First pair -> requisite Attributes Second pair -> requisite SlotChains.
Definition: SVED.h:138
void __initReqSets(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:466
StructuredBayesBall< GUM_SCALAR > __bb
Definition: SVED.h:132
+ 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 515 of file SVED_tpl.h.

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

515  {
516  return *(__elim_orders[&c]);
517  }
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > *> __elim_orders
Definition: SVED.h:121

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

570  {
571  try {
572  return *(__req_set[&(__bb.requisiteNodes(i))].second);
573  } catch (NotFound&) {
574  __initReqSets(i);
575  return *(__req_set[&(__bb.requisiteNodes(i))].second);
576  }
577  }
HashTable< const Set< NodeId > *, std::pair< Set< NodeId > *, Set< NodeId > *> > __req_set
First pair -> requisite Attributes Second pair -> requisite SlotChains.
Definition: SVED.h:138
void __initReqSets(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:466
StructuredBayesBall< GUM_SCALAR > __bb
Definition: SVED.h:132
+ 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 371 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().

371  {
372  ClassDependencyGraph< GUM_SCALAR > cdg(*(this->_prm));
373  Sequence< const PRMClassElementContainer< GUM_SCALAR >* > class_elim_order;
374  std::list< NodeId > l;
375 
376  for (const auto node : cdg.dag().nodes()) {
377  if (cdg.dag().parents(node).empty()) { l.push_back(node); }
378  }
379 
380  Set< NodeId > visited_node;
381 
382  while (!l.empty()) {
383  visited_node.insert(l.front());
384 
385  if (!class_elim_order.exists(cdg.get(l.front()).first)) {
386  class_elim_order.insert(cdg.get(l.front()).first);
387  }
388 
389  for (const auto child : cdg.dag().children(l.front())) {
390  if (!visited_node.contains(child)) { l.push_back(child); }
391  }
392 
393  l.pop_front();
394  }
395 
397  for (auto c : class_elim_order) {
398  std::string name = c->name();
399  auto pos = name.find_first_of("<");
400  if (pos != std::string::npos) { name = name.substr(0, pos); }
401  try {
402  __class_elim_order->insert(name);
403  } catch (DuplicateElement&) {}
404  }
405  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:581
virtual std::string name() const
Returns the name of the current inference algorithm.
Definition: SVED_tpl.h:603
PRM< GUM_SCALAR > const * _prm
The PRM<GUM_SCALAR> on which inference is done.
Definition: PRMInference.h:213
Sequence< std::string > * __class_elim_order
Definition: SVED.h:130
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
void insert(const Key &k)
Insert an element at the end of the sequence.
Definition: sequence_tpl.h:408
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ __initLiftedNodes()

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

Returns true if second can be eliminated before first.

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

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

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

466  {
467  Set< NodeId >* attr_set = new Set< NodeId >();
468  Set< NodeId >* sc_set = new Set< NodeId >();
469 
470  for (const auto node : __bb.requisiteNodes(i)) {
471  switch (i->type().get(node).elt_type()) {
474  attr_set->insert(node);
475  break;
476  }
477 
479  sc_set->insert(node);
480  break;
481  }
482 
483  default: {
484  GUM_ERROR(FatalError,
485  "There should not be elements other"
486  " than PRMAttribute<GUM_SCALAR> and SlotChain.");
487  }
488  }
489  }
490 
491  __req_set.insert(
492  &(__bb.requisiteNodes(i)),
493  std::pair< Set< NodeId >*, Set< NodeId >* >(attr_set, sc_set));
494  }
HashTable< const Set< NodeId > *, std::pair< Set< NodeId > *, Set< NodeId > *> > __req_set
First pair -> requisite Attributes Second pair -> requisite SlotChains.
Definition: SVED.h:138
StructuredBayesBall< GUM_SCALAR > __bb
Definition: SVED.h:132
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ 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 507 of file SVED_tpl.h.

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

508  {
509  for (const auto& elt : this->evidence(i))
510  pool.insert(const_cast< Potential< GUM_SCALAR >* >(elt.second));
511  }
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 283 of file SVED_tpl.h.

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

284  {
285  BucketSet* lifted_pool = nullptr;
286 
287  try {
288  lifted_pool = __lifted_pools[&(__bb.requisiteNodes(i))];
289  } catch (NotFound&) {
290  __initLiftedNodes(i, trash);
291  lifted_pool = __lifted_pools[&(__bb.requisiteNodes(i))];
292  }
293 
294  for (const auto lifted_pot : *lifted_pool) {
295  Potential< GUM_SCALAR >* pot = copyPotential(i->bijection(), *lifted_pot);
296  pool.insert(pot);
297  trash.insert(pot);
298  }
299  }
Set< Potential< GUM_SCALAR > *> BucketSet
Code alias.
Definition: SVED.h:111
void __initLiftedNodes(const PRMInstance< GUM_SCALAR > *i, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:302
StructuredBayesBall< GUM_SCALAR > __bb
Definition: SVED.h:132
Potential< GUM_SCALAR > * copyPotential(const Bijection< const DiscreteVariable *, const DiscreteVariable * > &bij, const Potential< GUM_SCALAR > &source)
Returns a copy of a Potential after applying a bijection over the variables in source.
Definition: utils_prm_tpl.h:29
HashTable< const Set< NodeId > *, BucketSet *> __lifted_pools
The Set<NodeId> returned by StructuredBayesBall<GUM_SCALAR> is unique for each family of instances wi...
Definition: SVED.h:128
+ 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 580 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().

586  {
587  while (!elim_list.empty()) {
588  if (__checkElimOrder(i, elim_list.front())) {
589  if ((!ignore.exists(elim_list.front()))
590  && (__bb.exists(elim_list.front()))) {
592  i, elim_list.front(), pool, trash, elim_list, ignore);
593  }
594  } else if (__bb.exists(elim_list.front())) {
595  reduced_list.insert(elim_list.front());
596  }
597 
598  elim_list.popFront();
599  }
600  }
void __eliminateNodesDownward(const PRMInstance< GUM_SCALAR > *from, const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, List< const PRMInstance< GUM_SCALAR > * > &elim_list, Set< const PRMInstance< GUM_SCALAR > * > &ignore)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:116
StructuredBayesBall< GUM_SCALAR > __bb
Definition: SVED.h:132
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:527
+ 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 520 of file SVED_tpl.h.

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

520  {
521  auto pos = s.find_first_of("<");
522  if (pos != std::string::npos) { return s.substr(0, pos); }
523  return s;
524  }
+ 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 546 of file SVED_tpl.h.

547  {
548  // Do nothing
549  }

◆ _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 552 of file SVED_tpl.h.

553  {
554  // Do nothing
555  }

◆ _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 460 of file SVED_tpl.h.

References GUM_ERROR.

461  {
462  GUM_ERROR(FatalError, "Not implemented.");
463  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55

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

409  {
410  const PRMInstance< GUM_SCALAR >* i = chain.first;
411  const PRMAttribute< GUM_SCALAR >* elt = chain.second;
412  SVED< GUM_SCALAR >::BucketSet pool, trash;
413  __bb.compute(i, elt->id());
414  __eliminateNodes(i, elt->id(), pool, trash);
415 
416  std::vector< const Potential< GUM_SCALAR >* > result;
417  for (auto pot : pool) {
418  if (pot->contains(*(m.variablesSequence().atPos(0))))
419  result.push_back(pot);
420  }
421 
422  while (result.size() > 1) {
423  const auto& p1 = *(result.back());
424  result.pop_back();
425  const auto& p2 = *(result.back());
426  result.pop_back();
427  auto mult = new Potential< GUM_SCALAR >(p1 * p2);
428  result.push_back(mult);
429  trash.insert(mult);
430  }
431 
432  m = *(result.back());
433  m.normalize();
434 
435  GUM_ASSERT(m.nbrDim() == (Size)1);
436 
437  // cleaning up the mess
438  for (const auto pot : trash)
439  delete pot;
440 
441  for (const auto& elt : __lifted_pools)
442  delete elt.second;
443 
444  __lifted_pools.clear();
445 
446  for (const auto& elt : __req_set) {
447  delete elt.second.first;
448  delete elt.second.second;
449  }
450 
451  __req_set.clear();
452 
453  for (const auto elt : __elim_orders)
454  delete elt.second;
455 
456  __elim_orders.clear();
457  }
HashTable< const Set< NodeId > *, std::pair< Set< NodeId > *, Set< NodeId > *> > __req_set
First pair -> requisite Attributes Second pair -> requisite SlotChains.
Definition: SVED.h:138
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:46
Set< Potential< GUM_SCALAR > *> BucketSet
Code alias.
Definition: SVED.h:111
StructuredBayesBall< GUM_SCALAR > __bb
Definition: SVED.h:132
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:128
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > *> __elim_orders
Definition: SVED.h:121
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
+ Here is the call graph for this function:

◆ addEvidence()

template<typename GUM_SCALAR>
void gum::prm::PRMInference< GUM_SCALAR >::addEvidence ( const Chain chain,
const Potential< GUM_SCALAR > &  p 
)
inherited

Add an evidence to the given instance's elt.

Parameters
chainThe variable being observed.
pThe Potential added (by copy) as evidence.
Exceptions
NotFoundRaised if elt does not belong to i.
OperationNotAllowedRaised if p is inconsistent with elt.

Definition at line 109 of file PRMInference_tpl.h.

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

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

◆ clearEvidence()

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

Remove all evidences.

Definition at line 36 of file PRMInference_tpl.h.

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

◆ evidence() [1/4]

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

Returns EMap of evidences over i.

Exceptions
NotFoundif i has no evidence.

Definition at line 156 of file PRMInference_tpl.h.

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

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

◆ evidence() [2/4]

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

Returns EMap of evidences over i.

Exceptions
NotFoundif i has no evidence.

Definition at line 177 of file PRMInference_tpl.h.

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

◆ evidence() [3/4]

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

Returns EMap of evidences over i.

Exceptions
NotFoundif i has no evidence.

Definition at line 166 of file PRMInference_tpl.h.

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

◆ evidence() [4/4]

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

Returns EMap of evidences over i.

Exceptions
NotFoundif i has no evidence.

Definition at line 187 of file PRMInference_tpl.h.

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

◆ hasEvidence() [1/4]

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

Returns true if i has evidence.

Definition at line 197 of file PRMInference_tpl.h.

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

198  {
199  return __evidences.exists(&i);
200  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> __evidences
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:235
+ Here is the caller graph for this function:

◆ hasEvidence() [2/4]

template<typename GUM_SCALAR>
INLINE bool gum::prm::PRMInference< GUM_SCALAR >::hasEvidence ( const PRMInstance< GUM_SCALAR > *  i) const
inherited

Returns EMap of evidences over i.

Definition at line 203 of file PRMInference_tpl.h.

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

◆ hasEvidence() [3/4]

template<typename GUM_SCALAR>
INLINE bool gum::prm::PRMInference< GUM_SCALAR >::hasEvidence ( const Chain chain) const
inherited

Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.

Definition at line 209 of file PRMInference_tpl.h.

209  {
210  return (hasEvidence(chain.first))
211  ? evidence(chain.first).exists(chain.second->id())
212  : false;
213  }
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
bool hasEvidence() const
Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.

◆ hasEvidence() [4/4]

template<typename GUM_SCALAR>
INLINE bool gum::prm::PRMInference< GUM_SCALAR >::hasEvidence ( ) const
inherited

Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.

Definition at line 216 of file PRMInference_tpl.h.

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

216  {
217  return (__evidences.size() != (Size)0);
218  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> __evidences
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:235
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
+ Here is the caller graph for this function:

◆ joint()

template<typename GUM_SCALAR>
INLINE void gum::prm::PRMInference< GUM_SCALAR >::joint ( const std::vector< Chain > &  chains,
Potential< GUM_SCALAR > &  j 
)
inherited

Compute the joint probability of the formals attributes pointed by chains and stores it in m.

Parameters
chainsA Set of strings of the form instance.attribute.
jAn empty CPF which will be filed by the joint probability over chains.
Exceptions
NotFoundRaised if some chain in chains does not point to a formal attribute.
OperationNotAllowedRaise if m is not empty.

Definition at line 263 of file PRMInference_tpl.h.

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

◆ marginal()

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

Compute the marginal of the formal attribute pointed by chain and stores it in m.

Parameters
chainA string of the form instance.attribute.
mAn empty CPF which will be filed by the marginal of chain.
Exceptions
NotFoundRaised if chain is invalid.
WrongTypeRaised if chain does not point to an PRMAttribute<GUM_SCALAR>.
OperationNotAllowedRaise if m is not empty.

Definition at line 234 of file PRMInference_tpl.h.

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

236  {
237  if (m.nbrDim() > 0) {
238  GUM_ERROR(OperationNotAllowed, "the given Potential is not empty.");
239  }
240 
241  if (hasEvidence(chain)) {
242  m.add(chain.second->type().variable());
243  const Potential< GUM_SCALAR >& e =
244  *(evidence(chain.first)[chain.second->id()]);
245  Instantiation i(m), j(e);
246 
247  for (i.setFirst(), j.setFirst(); !i.end(); i.inc(), j.inc())
248  m.set(i, e.get(j));
249  } else {
250  if (chain.second != &(chain.first->get(chain.second->safeName()))) {
251  typename PRMInference< GUM_SCALAR >::Chain good_chain = std::make_pair(
252  chain.first, &(chain.first->get(chain.second->safeName())));
253  m.add(good_chain.second->type().variable());
254  _marginal(good_chain, m);
255  } else {
256  m.add(chain.second->type().variable());
257  _marginal(chain, m);
258  }
259  }
260  }
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
virtual void _marginal(const Chain &chain, Potential< GUM_SCALAR > &m)=0
Generic method to compute the marginal of given element.
bool hasEvidence() const
Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
std::pair< const PRMInstance< GUM_SCALAR > *, const PRMAttribute< GUM_SCALAR > *> Chain
Code alias.
Definition: PRMInference.h:57
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ name()

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

Returns the name of the current inference algorithm.

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

Definition at line 603 of file SVED_tpl.h.

603  {
604  return "SVED";
605  }

◆ removeEvidence()

template<typename GUM_SCALAR >
INLINE void gum::prm::PRMInference< GUM_SCALAR >::removeEvidence ( const Chain chain)
inherited

Remove evidence on the given instance's elt.

Parameters
chainThe variable being observed.
Exceptions
NotFoundRaised if the given names are not found.
WrongTypeRaised if the elt is not an PRMAttribute<GUM_SCALAR>.

Definition at line 221 of file PRMInference_tpl.h.

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

221  {
222  try {
223  if (__EMap(chain.first).exists(chain.second->id())) {
224  _evidenceRemoved(chain);
225  delete __EMap(chain.first)[chain.second->id()];
226  __EMap(chain.first).erase(chain.second->id());
227  }
228  } catch (NotFound&) {
229  // Ok, we are only removing
230  }
231  }
void erase(const Key &key)
Removes a given element from the hash table.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
virtual void _evidenceRemoved(const Chain &chain)=0
This method is called whenever an evidence is removed, but BEFORE any processing made by PRMInference...
EMap & __EMap(const PRMInstance< GUM_SCALAR > *i)
Private getter over __evidences, if necessary creates an EMap for i.
+ Here is the caller graph for this function:

Member Data Documentation

◆ __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 130 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 121 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 128 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 138 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: