aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
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 posterior (const Chain &chain, Potential< GUM_SCALAR > &m)
 Compute the posterior of the formal attribute pointed by chain and stores it in m. More...
 
void joint (const std::vector< Chain > &chains, Potential< GUM_SCALAR > &j)
 Compute the joint probability of the formals attributes pointed by chains and stores it in m. More...
 
Evidence handling.
EMapevidence (const PRMInstance< GUM_SCALAR > &i)
 Returns EMap of evidences over i. More...
 
EMapevidence (const PRMInstance< GUM_SCALAR > *i)
 Returns EMap of evidences over i. More...
 
const EMapevidence (const PRMInstance< GUM_SCALAR > &i) const
 Returns EMap of evidences over i. More...
 
const EMapevidence (const PRMInstance< GUM_SCALAR > *i) const
 Returns EMap of evidences over i. More...
 
bool hasEvidence (const PRMInstance< GUM_SCALAR > &i) const
 Returns true if i has evidence. More...
 
bool hasEvidence (const PRMInstance< GUM_SCALAR > *i) const
 Returns EMap of evidences over i. More...
 
bool hasEvidence (const Chain &chain) const
 Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a. More...
 
bool hasEvidence () const
 Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a. More...
 
void addEvidence (const Chain &chain, const Potential< GUM_SCALAR > &p)
 Add an evidence to the given instance's elt. More...
 
void removeEvidence (const Chain &chain)
 Remove evidence on the given instance's elt. More...
 
void clearEvidence ()
 Remove all evidences. More...
 

Public Types

typedef NodeProperty< const Potential< GUM_SCALAR > *> EMap
 Code alias. More...
 
typedef NodeProperty< const Potential< GUM_SCALAR > *>::iterator_safe EMapIterator
 Code alias. More...
 
typedef NodeProperty< const Potential< GUM_SCALAR > *>::const_iterator_safe EMapConstIterator
 Code alias. More...
 

Protected Attributes

Protected members.
PRM< GUM_SCALAR > const * prm_
 The PRM<GUM_SCALAR> on which inference is done. More...
 
PRMSystem< GUM_SCALAR > const * sys_
 The Model on which inference is done. More...
 

Query methods.

typedef PRMInference< GUM_SCALAR >::Chain Chain
 Code alias. More...
 
virtual void evidenceAdded_ (const Chain &chain)
 See PRMInference::evidenceAdded_(). More...
 
virtual void evidenceRemoved_ (const Chain &chain)
 See PRMInference::evidenceRemoved_(). More...
 
virtual void posterior_ (const Chain &chain, Potential< GUM_SCALAR > &m)
 See PRMInference::posterior_(). 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 62 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 114 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 109 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 92 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 58 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 65 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 61 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 472 of file SVED_tpl.h.

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

473  :
474  PRMInference< GUM_SCALAR >(prm, model),
475  _class_elim_order_(0), _bb_(*this) {
476  GUM_CONSTRUCTOR(SVED);
477  }
SVED(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &model)
Default Constructor.
Definition: SVED_tpl.h:472
Sequence< std::string > * _class_elim_order_
Definition: SVED.h:125
StructuredBayesBall< GUM_SCALAR > _bb_
Definition: SVED.h:127
+ Here is the call graph for this function:

◆ ~SVED()

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

Destructor.

Definition at line 34 of file SVED_tpl.h.

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

34  {
35  GUM_DESTRUCTOR(SVED);
36 
37  for (const auto& elt: _elim_orders_)
38  delete elt.second;
39 
40  if (_class_elim_order_ != nullptr) delete _class_elim_order_;
41  }
SVED(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &model)
Default Constructor.
Definition: SVED_tpl.h:472
Sequence< std::string > * _class_elim_order_
Definition: SVED.h:125
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > *> _elim_orders_
Definition: SVED.h:116
+ Here is the call graph for this function:

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

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

502  {
503  if (_class_elim_order_ == 0) { _initElimOrder_(); }
504 
505  auto first_name = _trim_(first->type().name());
506  auto second_name = _trim_(second->type().name());
507  return (_class_elim_order_->pos(first_name) <= _class_elim_order_->pos(second_name));
508  }
Idx pos(const Key &key) const
Returns the position of the object passed in argument (if it exists).
Definition: sequence_tpl.h:499
Sequence< std::string > * _class_elim_order_
Definition: SVED.h:125
void _initElimOrder_()
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:349
std::string _trim_(const std::string &s)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:494
+ Here is the call 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 44 of file SVED_tpl.h.

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

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

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

115  {
116  ignore.insert(i);
117  // Extracting required attributes and slotchains
118  Set< NodeId >& attr_set = _getAttrSet_(i);
119  Set< NodeId >& sc_set = _getSCSet_(i);
120  // Calling elimination over child instance
121  List< const PRMInstance< GUM_SCALAR >* > my_list;
122 
123  for (const auto attr: attr_set) {
124  try {
125  for (auto iter = i->getRefAttr(attr).begin(); iter != i->getRefAttr(attr).end(); ++iter)
126  if ((!ignore.exists(iter->first)) && (_bb_.exists(iter->first)))
127  _eliminateNodesDownward_(i, iter->first, pool, trash, my_list, ignore);
128  } catch (NotFound&) {
129  // Ok
130  }
131  }
132 
133  // Eliminating all nodes in current instance
134  if (this->hasEvidence(i)) {
135  _eliminateNodesWithEvidence_(i, pool, trash);
136  } else {
137  _insertLiftedNodes_(i, pool, trash);
138 
139  for (const auto agg: i->type().aggregates())
140  if (_bb_.requisiteNodes(i).exists(agg->id())) pool.insert(_getAggPotential_(i, agg));
141 
142  try {
143  InstanceBayesNet< GUM_SCALAR > bn(*i);
144  std::vector< const DiscreteVariable* > elim_order;
145 
146  for (auto node: _getElimOrder_(i->type())) {
147  const auto& var = bn.variable(node);
148  elim_order.push_back(&var);
149  }
150 
151  eliminateNodes(elim_order, pool, trash);
152  } catch (NotFound&) {
153  // Raised if there is no inner nodes to eliminate
154  }
155  }
156 
157  // Calling elimination over child's parents
158  while (!my_list.empty()) {
159  if (_checkElimOrder_(i, my_list.front())) {
160  if ((!ignore.exists(my_list.front())) && (_bb_.exists(my_list.front())))
161  _eliminateNodesDownward_(i, my_list.front(), pool, trash, my_list, ignore);
162  } else if (_bb_.exists(my_list.front())) {
163  elim_list.insert(my_list.front());
164  }
165 
166  my_list.popFront();
167  }
168 
169  // Adding parents instance to elim_list
170  for (const auto chain: sc_set)
171  for (const auto parent: i->getInstances(chain))
172  if ((!ignore.exists(parent)) && _bb_.exists(parent) && (parent != from))
173  elim_list.insert(parent);
174  }
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:109
Set< NodeId > & _getAttrSet_(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:530
Set< NodeId > & _getSCSet_(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:540
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:501
void _eliminateNodesWithEvidence_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:244
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:512
StructuredBayesBall< GUM_SCALAR > _bb_
Definition: SVED.h:127
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 _insertLiftedNodes_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:266
std::vector< NodeId > & _getElimOrder_(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:489
+ Here is the call 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 177 of file SVED_tpl.h.

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

182  {
183  ignore.insert(i);
184  // Extracting required attributes and slotchains
185  Set< NodeId >& attr_set = _getAttrSet_(i);
186  Set< NodeId >& sc_set = _getSCSet_(i);
187 
188  // Downward elimination
189  for (const auto attr: attr_set) {
190  try {
191  for (auto iter = i->getRefAttr(attr).begin(); iter != i->getRefAttr(attr).end(); ++iter)
192  if ((!ignore.exists(iter->first)) && (_bb_.exists(iter->first)))
193  _eliminateNodesDownward_(i, iter->first, pool, trash, elim_list, ignore);
194  } catch (NotFound&) {
195  // Ok
196  }
197  }
198 
199  // Eliminating all nodes in i instance
200  if (this->hasEvidence(i)) {
201  _eliminateNodesWithEvidence_(i, pool, trash);
202  } else {
203  _insertLiftedNodes_(i, pool, trash);
204 
205  for (const auto agg: i->type().aggregates())
206  if (_bb_.requisiteNodes(i).exists(agg->id())) pool.insert(_getAggPotential_(i, agg));
207 
208  try {
209  InstanceBayesNet< GUM_SCALAR > bn(*i);
210  std::vector< const DiscreteVariable* > elim_order;
211 
212  for (auto node: _getElimOrder_(i->type())) {
213  const auto& var = bn.variable(node);
214  elim_order.push_back(&var);
215  }
216  eliminateNodes(elim_order, pool, trash);
217  } catch (NotFound&) {
218  // Raised if there is no inner nodes to eliminate
219  }
220  }
221 
222  // Eliminating instance in elim_list
223  List< const PRMInstance< GUM_SCALAR >* > tmp_list;
224 
225  while (!elim_list.empty()) {
226  if (_checkElimOrder_(i, elim_list.front())) {
227  if ((!ignore.exists(elim_list.front())) && (_bb_.exists(elim_list.front())))
228  _eliminateNodesDownward_(i, elim_list.front(), pool, trash, elim_list, ignore);
229  } else if (_bb_.exists(elim_list.front())) {
230  ignore.insert(elim_list.front());
231  }
232 
233  elim_list.popFront();
234  }
235 
236  // Upward elimination
237  for (const auto chain: sc_set)
238  for (const auto parent: i->getInstances(chain))
239  if ((!ignore.exists(parent)) && (_bb_.exists(parent)))
240  _eliminateNodesUpward_(parent, pool, trash, tmp_list, ignore);
241  }
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:109
Set< NodeId > & _getAttrSet_(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:530
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:177
Set< NodeId > & _getSCSet_(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:540
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:501
void _eliminateNodesWithEvidence_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:244
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:512
StructuredBayesBall< GUM_SCALAR > _bb_
Definition: SVED.h:127
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 _insertLiftedNodes_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:266
std::vector< NodeId > & _getElimOrder_(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:489
+ 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 244 of file SVED_tpl.h.

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

246  {
247  // Adding required evidences
248  for (const auto& elt: this->evidence(i))
249  if (_bb_.requisiteNodes(i).exists(elt.first))
250  pool.insert(const_cast< Potential< GUM_SCALAR >* >(elt.second));
251 
252  // Adding potentials and eliminating the remaining nodes
253  for (const auto& a: *i)
254  if (_bb_.requisiteNodes(i).exists(a.first))
255  pool.insert(&(const_cast< Potential< GUM_SCALAR >& >(a.second->cpf())));
256 
257  InstanceBayesNet< GUM_SCALAR > bn(*i);
258  DefaultTriangulation t(&(bn.moralGraph()), &(bn.modalities()));
259  const std::vector< NodeId >& full_elim_order = t.eliminationOrder();
260 
261  for (auto var = full_elim_order.begin(); var != full_elim_order.end(); ++var)
262  eliminateNode(&(i->get(*var).type().variable()), pool, trash);
263  }
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
StructuredBayesBall< GUM_SCALAR > _bb_
Definition: SVED.h:127
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 512 of file SVED_tpl.h.

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

513  {
514  return &(const_cast< Potential< GUM_SCALAR >& >(i->get(agg->safeName()).cpf()));
515  }
+ 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 530 of file SVED_tpl.h.

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

530  {
531  try {
532  return *(_req_set_[&(_bb_.requisiteNodes(i))].first);
533  } catch (NotFound&) {
534  _initReqSets_(i);
535  return *(_req_set_[&(_bb_.requisiteNodes(i))].first);
536  }
537  }
void _initReqSets_(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:442
StructuredBayesBall< GUM_SCALAR > _bb_
Definition: SVED.h:127
HashTable< const Set< NodeId > *, std::pair< Set< NodeId > *, Set< NodeId > *> > _req_set_
First pair -> requisite Attributes Second pair -> requisite SlotChains.
Definition: SVED.h:131
+ 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 489 of file SVED_tpl.h.

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

489  {
490  return *(_elim_orders_[&c]);
491  }
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > *> _elim_orders_
Definition: SVED.h:116
+ Here is the call graph for this function:

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

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

540  {
541  try {
542  return *(_req_set_[&(_bb_.requisiteNodes(i))].second);
543  } catch (NotFound&) {
544  _initReqSets_(i);
545  return *(_req_set_[&(_bb_.requisiteNodes(i))].second);
546  }
547  }
void _initReqSets_(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:442
StructuredBayesBall< GUM_SCALAR > _bb_
Definition: SVED.h:127
HashTable< const Set< NodeId > *, std::pair< Set< NodeId > *, Set< NodeId > *> > _req_set_
First pair -> requisite Attributes Second pair -> requisite SlotChains.
Definition: SVED.h:131
+ 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 349 of file SVED_tpl.h.

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

349  {
350  ClassDependencyGraph< GUM_SCALAR > cdg(*(this->prm_));
351  Sequence< const PRMClassElementContainer< GUM_SCALAR >* > class_elim_order;
352  std::list< NodeId > l;
353 
354  for (const auto node: cdg.dag().nodes()) {
355  if (cdg.dag().parents(node).empty()) { l.push_back(node); }
356  }
357 
358  Set< NodeId > visited_node;
359 
360  while (!l.empty()) {
361  visited_node.insert(l.front());
362 
363  if (!class_elim_order.exists(cdg.get(l.front()).first)) {
364  class_elim_order.insert(cdg.get(l.front()).first);
365  }
366 
367  for (const auto child: cdg.dag().children(l.front())) {
368  if (!visited_node.contains(child)) { l.push_back(child); }
369  }
370 
371  l.pop_front();
372  }
373 
375  for (auto c: class_elim_order) {
376  std::string name = c->name();
377  auto pos = name.find_first_of("<");
378  if (pos != std::string::npos) { name = name.substr(0, pos); }
379  try {
380  _class_elim_order_->insert(name);
381  } catch (DuplicateElement&) {}
382  }
383  }
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:558
virtual std::string name() const
Returns the name of the current inference algorithm.
Definition: SVED_tpl.h:571
Sequence< std::string > * _class_elim_order_
Definition: SVED.h:125
PRM< GUM_SCALAR > const * prm_
The PRM<GUM_SCALAR> on which inference is done.
Definition: PRMInference.h:207
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:606
void insert(const Key &k)
Insert an element at the end of the sequence.
Definition: sequence_tpl.h:393
+ Here is the call 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 286 of file SVED_tpl.h.

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

287  {
288  PRMClass< GUM_SCALAR >& c = const_cast< PRMClass< GUM_SCALAR >& >(i->type());
289  BucketSet* lifted_pool = new BucketSet();
290  _lifted_pools_.insert(&(_bb_.requisiteNodes(i)), lifted_pool);
291 
292  for (const auto node: _bb_.requisiteNodes(i))
294  lifted_pool->insert(const_cast< Potential< GUM_SCALAR >* >(&(c.get(node).cpf())));
295 
296  NodeSet inners, outers, ignore;
297 
298  for (const auto& elt: *i) {
299  if (_bb_.requisiteNodes(*i).exists(elt.first)) {
300  if (PRMClassElement< GUM_SCALAR >::isAttribute(c.get(elt.first))) {
301  if (c.isOutputNode(c.get(elt.first)))
302  outers.insert(elt.first);
303  else if (!outers.exists(elt.first))
304  inners.insert(elt.first);
305  } else if (PRMClassElement< GUM_SCALAR >::isAggregate(c.get(elt.first))) {
306  outers.insert(elt.first);
307 
308  // We need to put in the output_elim_order aggregator's parents
309  // which are
310  // innner nodes
311  for (const auto par: c.containerDag().parents(elt.first))
312  if (PRMClassElement< GUM_SCALAR >::isAttribute(i->type().get(par))
313  && i->type().isInnerNode(i->type().get(par))
314  && _bb_.requisiteNodes(i).exists(par)) {
315  inners.erase(par);
316  outers.insert(par);
317  }
318  }
319  } else {
320  ignore.insert(elt.first);
321  }
322  }
323 
324  // Now we proceed with the elimination of inner attributes
325  ClassBayesNet< GUM_SCALAR > bn(c);
326  List< NodeSet > partial_ordering;
327 
328  if (inners.size()) partial_ordering.pushBack(inners);
329 
330  if (outers.size()) partial_ordering.pushBack(outers);
331 
332  if (ignore.size()) partial_ordering.pushBack(ignore);
333 
334  GUM_ASSERT(inners.size() || outers.size());
335  PartialOrderedTriangulation t(&(bn.moralGraph()), &(bn.modalities()), &partial_ordering);
336 
337  for (size_t idx = 0; idx < inners.size(); ++idx)
338  eliminateNode(&(c.get(t.eliminationOrder()[idx]).type().variable()), *lifted_pool, trash);
339 
340  // If there is not only inner and input Attributes
341  if (outers.size()) {
342  _elim_orders_.insert(&c,
343  new std::vector< NodeId >(t.eliminationOrder().begin() + inners.size(),
344  t.eliminationOrder().end()));
345  }
346  }
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
Set< Potential< GUM_SCALAR > *> BucketSet
Code alias.
Definition: SVED.h:109
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > *> _elim_orders_
Definition: SVED.h:116
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:127
void eliminateNode(const DiscreteVariable *var, Set< Potential< GUM_SCALAR > * > &pool, Set< Potential< GUM_SCALAR > * > &trash)
Proceeds with the elimination of var in pool.
static INLINE bool isAggregate(const PRMClassElement< GUM_SCALAR > &elt)
Return true if obj is of type PRMAggregate.
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:123
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:606
+ 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 442 of file SVED_tpl.h.

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

442  {
443  Set< NodeId >* attr_set = new Set< NodeId >();
444  Set< NodeId >* sc_set = new Set< NodeId >();
445 
446  for (const auto node: _bb_.requisiteNodes(i)) {
447  switch (i->type().get(node).elt_type()) {
450  attr_set->insert(node);
451  break;
452  }
453 
455  sc_set->insert(node);
456  break;
457  }
458 
459  default: {
460  GUM_ERROR(FatalError,
461  "There should not be elements other"
462  " than PRMAttribute<GUM_SCALAR> and SlotChain.");
463  }
464  }
465  }
466 
467  _req_set_.insert(&(_bb_.requisiteNodes(i)),
468  std::pair< Set< NodeId >*, Set< NodeId >* >(attr_set, sc_set));
469  }
StructuredBayesBall< GUM_SCALAR > _bb_
Definition: SVED.h:127
HashTable< const Set< NodeId > *, std::pair< Set< NodeId > *, Set< NodeId > *> > _req_set_
First pair -> requisite Attributes Second pair -> requisite SlotChains.
Definition: SVED.h:131
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:606
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call 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 481 of file SVED_tpl.h.

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

482  {
483  for (const auto& elt: this->evidence(i))
484  pool.insert(const_cast< Potential< GUM_SCALAR >* >(elt.second));
485  }
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 266 of file SVED_tpl.h.

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

268  {
269  BucketSet* lifted_pool = nullptr;
270 
271  try {
272  lifted_pool = _lifted_pools_[&(_bb_.requisiteNodes(i))];
273  } catch (NotFound&) {
274  _initLiftedNodes_(i, trash);
275  lifted_pool = _lifted_pools_[&(_bb_.requisiteNodes(i))];
276  }
277 
278  for (const auto lifted_pot: *lifted_pool) {
279  Potential< GUM_SCALAR >* pot = copyPotential(i->bijection(), *lifted_pot);
280  pool.insert(pot);
281  trash.insert(pot);
282  }
283  }
Set< Potential< GUM_SCALAR > *> BucketSet
Code alias.
Definition: SVED.h:109
void _initLiftedNodes_(const PRMInstance< GUM_SCALAR > *i, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:286
StructuredBayesBall< GUM_SCALAR > _bb_
Definition: SVED.h:127
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:123
+ 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 551 of file SVED_tpl.h.

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

556  {
557  while (!elim_list.empty()) {
558  if (_checkElimOrder_(i, elim_list.front())) {
559  if ((!ignore.exists(elim_list.front())) && (_bb_.exists(elim_list.front()))) {
560  _eliminateNodesDownward_(i, elim_list.front(), pool, trash, elim_list, ignore);
561  }
562  } else if (_bb_.exists(elim_list.front())) {
563  reduced_list.insert(elim_list.front());
564  }
565 
566  elim_list.popFront();
567  }
568  }
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:109
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:501
StructuredBayesBall< GUM_SCALAR > _bb_
Definition: SVED.h:127
+ 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 494 of file SVED_tpl.h.

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

494  {
495  auto pos = s.find_first_of("<");
496  if (pos != std::string::npos) { return s.substr(0, pos); }
497  return s;
498  }
+ 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 105 of file PRMInference_tpl.h.

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

◆ clearEvidence()

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

Remove all evidences.

Definition at line 35 of file PRMInference_tpl.h.

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

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

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

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

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

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

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

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

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

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

519  {
520  // Do nothing
521  }

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

525  {
526  // Do nothing
527  }

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

182  {
183  return _evidences_.exists(&i);
184  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> _evidences_
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:228

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

187  {
188  return _evidences_.exists(i);
189  }
HashTable< const PRMInstance< GUM_SCALAR > *, EMap *> _evidences_
Mapping of evidence over PRMInstance<GUM_SCALAR>&#39;s nodes.
Definition: PRMInference.h:228

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

192  {
193  return (hasEvidence(chain.first)) ? evidence(chain.first).exists(chain.second->id()) : false;
194  }
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 197 of file PRMInference_tpl.h.

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

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

243  {
244  if (j.nbrDim() > 0) { GUM_ERROR(OperationNotAllowed, "the given Potential is not empty.") }
245 
246  for (auto chain = chains.begin(); chain != chains.end(); ++chain) {
247  j.add(chain->second->type().variable());
248  }
249 
250  joint_(chains, j);
251  }
virtual void joint_(const std::vector< Chain > &queries, Potential< GUM_SCALAR > &j)=0
Generic method to compute the posterior of given element.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

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

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

437  {
438  GUM_ERROR(FatalError, "Not implemented.")
439  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call 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 571 of file SVED_tpl.h.

571  {
572  return "SVED";
573  }

◆ posterior()

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

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

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

Definition at line 215 of file PRMInference_tpl.h.

217  {
218  if (m.nbrDim() > 0) { GUM_ERROR(OperationNotAllowed, "the given Potential is not empty.") }
219 
220  if (hasEvidence(chain)) {
221  m.add(chain.second->type().variable());
222  const Potential< GUM_SCALAR >& e = *(evidence(chain.first)[chain.second->id()]);
223  Instantiation i(m), j(e);
224 
225  for (i.setFirst(), j.setFirst(); !i.end(); i.inc(), j.inc())
226  m.set(i, e.get(j));
227  } else {
228  if (chain.second != &(chain.first->get(chain.second->safeName()))) {
229  typename PRMInference< GUM_SCALAR >::Chain good_chain
230  = std::make_pair(chain.first, &(chain.first->get(chain.second->safeName())));
231  m.add(good_chain.second->type().variable());
232  posterior_(good_chain, m);
233  } else {
234  m.add(chain.second->type().variable());
235  posterior_(chain, m);
236  }
237  }
238  }
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
virtual void posterior_(const Chain &chain, Potential< GUM_SCALAR > &m)=0
Generic method to compute the posterior of given element.
bool hasEvidence() const
Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
std::pair< const PRMInstance< GUM_SCALAR > *, const PRMAttribute< GUM_SCALAR > *> Chain
Code alias.
Definition: PRMInference.h:55
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ posterior_()

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

See PRMInference::posterior_().

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

Definition at line 386 of file SVED_tpl.h.

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

386  {
387  const PRMInstance< GUM_SCALAR >* i = chain.first;
388  const PRMAttribute< GUM_SCALAR >* elt = chain.second;
389  SVED< GUM_SCALAR >::BucketSet pool, trash;
390  _bb_.compute(i, elt->id());
391  _eliminateNodes_(i, elt->id(), pool, trash);
392 
393  std::vector< const Potential< GUM_SCALAR >* > result;
394  for (auto pot: pool) {
395  if (pot->contains(*(m.variablesSequence().atPos(0)))) result.push_back(pot);
396  }
397 
398  while (result.size() > 1) {
399  const auto& p1 = *(result.back());
400  result.pop_back();
401  const auto& p2 = *(result.back());
402  result.pop_back();
403  auto mult = new Potential< GUM_SCALAR >(p1 * p2);
404  result.push_back(mult);
405  trash.insert(mult);
406  }
407 
408  m = *(result.back());
409  m.normalize();
410 
411  GUM_ASSERT(m.nbrDim() == (Size)1);
412 
413  // cleaning up the mess
414  for (const auto pot: trash)
415  delete pot;
416 
417  for (const auto& elt: _lifted_pools_)
418  delete elt.second;
419 
420  _lifted_pools_.clear();
421 
422  for (const auto& elt: _req_set_) {
423  delete elt.second.first;
424  delete elt.second.second;
425  }
426 
427  _req_set_.clear();
428 
429  for (const auto elt: _elim_orders_)
430  delete elt.second;
431 
432  _elim_orders_.clear();
433  }
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:44
Set< Potential< GUM_SCALAR > *> BucketSet
Code alias.
Definition: SVED.h:109
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > *> _elim_orders_
Definition: SVED.h:116
StructuredBayesBall< GUM_SCALAR > _bb_
Definition: SVED.h:127
HashTable< const Set< NodeId > *, std::pair< Set< NodeId > *, Set< NodeId > *> > _req_set_
First pair -> requisite Attributes Second pair -> requisite SlotChains.
Definition: SVED.h:131
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
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:123
+ Here is the call graph for this function:

◆ removeEvidence()

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

Remove evidence on the given instance's elt.

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

Definition at line 202 of file PRMInference_tpl.h.

202  {
203  try {
204  if (_EMap_(chain.first).exists(chain.second->id())) {
205  evidenceRemoved_(chain);
206  delete _EMap_(chain.first)[chain.second->id()];
207  _EMap_(chain.first).erase(chain.second->id());
208  }
209  } catch (NotFound&) {
210  // Ok, we are only removing
211  }
212  }
EMap & _EMap_(const PRMInstance< GUM_SCALAR > *i)
Private getter over evidences, if necessary creates an EMap for i.
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...

Member Data Documentation

◆ _bb_

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

Definition at line 127 of file SVED.h.

◆ _class_elim_order_

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

Definition at line 125 of file SVED.h.

◆ _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 116 of file SVED.h.

◆ _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 123 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 131 of file SVED.h.

◆ prm_

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

The PRM<GUM_SCALAR> on which inference is done.

Definition at line 207 of file PRMInference.h.

◆ sys_

template<typename GUM_SCALAR>
PRMSystem< GUM_SCALAR > const* gum::prm::PRMInference< GUM_SCALAR >::sys_
protectedinherited

The Model on which inference is done.

Definition at line 210 of file PRMInference.h.


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