aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
gum::prm::SVE< GUM_SCALAR > Class Template Reference

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

#include <agrum/PRM/SVE.h>

Inheritance diagram for gum::prm::SVE< GUM_SCALAR >:
Collaboration diagram for gum::prm::SVE< GUM_SCALAR >:

Public Types

using EMap = NodeProperty< const Tensor< GUM_SCALAR >* >
 Code alias.
using EMapIterator = typename NodeProperty< const Tensor< GUM_SCALAR >* >::iterator_safe
 Code alias.
using EMapConstIterator
 Code alias.

Public Member Functions

Constructors & destructor.
 SVE (const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
 Default Constructor.
 ~SVE () override
 Destructor.
Getters & setters.
std::string name () const override
 Returns the name of the current inference algorithm.
Query methods.
void posterior (const Chain &chain, Tensor< GUM_SCALAR > &m)
 Compute the posterior of the formal attribute pointed by chain and stores it in m.
void joint (const std::vector< Chain > &chains, Tensor< GUM_SCALAR > &j)
 Compute the joint probability of the formals attributes pointed by chains and stores it in m.
Evidence handling.
EMapevidence (const PRMInstance< GUM_SCALAR > &i)
 Returns EMap of evidences over i.
EMapevidence (const PRMInstance< GUM_SCALAR > *i)
 Returns EMap of evidences over i.
const EMapevidence (const PRMInstance< GUM_SCALAR > &i) const
 Returns EMap of evidences over i.
const EMapevidence (const PRMInstance< GUM_SCALAR > *i) const
 Returns EMap of evidences over i.
bool hasEvidence (const PRMInstance< GUM_SCALAR > &i) const
 Returns true if i has evidence.
bool hasEvidence (const PRMInstance< GUM_SCALAR > *i) const
 Returns EMap of evidences over i.
bool hasEvidence (const Chain &chain) const
 Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
bool hasEvidence () const
 Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
void addEvidence (const Chain &chain, const Tensor< GUM_SCALAR > &p)
 Add an evidence to the given instance's elt.
void removeEvidence (const Chain &chain)
 Remove evidence on the given instance's elt.
void clearEvidence ()
 Remove all evidences.

Protected Attributes

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

Private Types

using BucketSet = Set< Tensor< GUM_SCALAR >* >
 Code alias.
using BucketSetIterator = typename Set< Tensor< GUM_SCALAR >* >::iterator_safe
using ArraySetIterator = typename Set< MultiDimArray< GUM_SCALAR >* >::iterator_safe

Private Member Functions

Inference sub methods.
void _eliminateNodes_ (const PRMInstance< GUM_SCALAR > *query, NodeId id, BucketSet &pool, BucketSet &trash)
 Returns true if second can be eliminated before first.
void _eliminateNodesDownward_ (const PRMInstance< GUM_SCALAR > *from, const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, List< const PRMInstance< GUM_SCALAR > * > &elim_list, Set< const PRMInstance< GUM_SCALAR > * > &ignore, Set< const PRMInstance< GUM_SCALAR > * > &eliminated)
 Returns true if second can be eliminated before first.
void _eliminateNodesUpward_ (const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, List< const PRMInstance< GUM_SCALAR > * > &elim_list, Set< const PRMInstance< GUM_SCALAR > * > &ignore, Set< const PRMInstance< GUM_SCALAR > * > &eliminated)
 Returns true if second can be eliminated before first.
void _eliminateNodesWithEvidence_ (const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, Set< NodeId > *delayedVars=0)
 Returns true if second can be eliminated before first.
void _eliminateDelayedVariables_ (const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
 Returns true if second can be eliminated before first.
void _insertLiftedNodes_ (const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
 Returns true if second can be eliminated before first.
void _variableElimination_ (const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, Set< NodeId > *delayedVars=0)
 Returns true if second can be eliminated before first.
bool _checkElimOrder_ (const PRMInstance< GUM_SCALAR > *first, const PRMInstance< GUM_SCALAR > *second)
 Returns true if second can be eliminated before first.
void _initElimOrder_ ()
 Returns true if second can be eliminated before first.
void _insertEvidence_ (const PRMInstance< GUM_SCALAR > *i, BucketSet &pool)
 Returns true if second can be eliminated before first.
void _addDelayedVariable_ (const PRMInstance< GUM_SCALAR > *i, const PRMInstance< GUM_SCALAR > *j, NodeId id)
 When there is a loop in the references some variable elimination must be delayed, this methods add such variable to delayedVariables to keep track of them.
std::vector< NodeId > & _getElimOrder_ (const PRMClass< GUM_SCALAR > &c)
 Returns true if second can be eliminated before first.
Tensor< GUM_SCALAR > * _getAggTensor_ (const PRMInstance< GUM_SCALAR > *i, const PRMAggregate< GUM_SCALAR > *agg)
 Returns true if second can be eliminated before first.
void _initLiftedNodes_ (const PRMClass< GUM_SCALAR > &c)
 Returns true if second can be eliminated before first.
std::string _trim_ (std::string_view s)
 Returns true if second can be eliminated before first.

Private Attributes

HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > * > _elim_orders_
HashTable< const PRMClass< GUM_SCALAR > *, BucketSet * > _lifted_pools_
Sequence< std::string > * _class_elim_order_
HashTable< const PRMInstance< GUM_SCALAR > *, gum::VariableSet * > _delayedVariables_
HashTable< std::string, Size_delayedVariablesCounters_
 Some variable must be delayed for more than one PRMInstance<GUM_SCALAR>, when the delayed variable counter reach 0 it can be eliminated.
BucketSet _lifted_trash_

Query methods.

using Chain = typename PRMInference< GUM_SCALAR >::Chain
 Code alias.
void evidenceAdded_ (const Chain &chain) override
 See PRMInference<GUM_SCALAR>::evidenceAdded_().
void evidenceRemoved_ (const Chain &chain) override
 See PRMInference<GUM_SCALAR>::evidenceRemoved_().
void posterior_ (const Chain &chain, Tensor< GUM_SCALAR > &m) override
 See PRMInference<GUM_SCALAR>::posterior_().
void joint_ (const std::vector< Chain > &queries, Tensor< GUM_SCALAR > &j) override
 See PRMInference<GUM_SCALAR>::joint_().

Private evidence handling methods and members.

using EvidenceIterator
 Code alias.
using EvidenceConstIterator
 Code alias.
HashTable< const PRMInstance< GUM_SCALAR > *, EMap * > _evidences_
 Mapping of evidence over PRMInstance<GUM_SCALAR>'s nodes.
EMap_EMap_ (const PRMInstance< GUM_SCALAR > *i)
 Private getter over evidences, if necessary creates an EMap for i.

Detailed Description

template<GUM_Numeric GUM_SCALAR>
class gum::prm::SVE< GUM_SCALAR >

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

Definition at line 74 of file SVE.h.

Member Typedef Documentation

◆ ArraySetIterator

template<GUM_Numeric GUM_SCALAR>
using gum::prm::SVE< GUM_SCALAR >::ArraySetIterator = typename Set< MultiDimArray< GUM_SCALAR >* >::iterator_safe
private

Definition at line 125 of file SVE.h.

◆ BucketSet

template<GUM_Numeric GUM_SCALAR>
using gum::prm::SVE< GUM_SCALAR >::BucketSet = Set< Tensor< GUM_SCALAR >* >
private

Code alias.

Definition at line 123 of file SVE.h.

◆ BucketSetIterator

template<GUM_Numeric GUM_SCALAR>
using gum::prm::SVE< GUM_SCALAR >::BucketSetIterator = typename Set< Tensor< GUM_SCALAR >* >::iterator_safe
private

Definition at line 124 of file SVE.h.

◆ Chain

template<GUM_Numeric GUM_SCALAR>
using gum::prm::SVE< GUM_SCALAR >::Chain = typename PRMInference< GUM_SCALAR >::Chain
protected

Code alias.

Definition at line 105 of file SVE.h.

◆ EMap

template<GUM_Numeric GUM_SCALAR>
using gum::prm::PRMInference< GUM_SCALAR >::EMap = NodeProperty< const Tensor< GUM_SCALAR >* >
inherited

Code alias.

Definition at line 74 of file PRMInference.h.

◆ EMapConstIterator

template<GUM_Numeric GUM_SCALAR>
using gum::prm::PRMInference< GUM_SCALAR >::EMapConstIterator
inherited
Initial value:
typename NodeProperty< const Tensor< GUM_SCALAR >* >::const_iterator_safe
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.

Code alias.

Definition at line 80 of file PRMInference.h.

◆ EMapIterator

template<GUM_Numeric GUM_SCALAR>
using gum::prm::PRMInference< GUM_SCALAR >::EMapIterator = typename NodeProperty< const Tensor< GUM_SCALAR >* >::iterator_safe
inherited

Code alias.

Definition at line 77 of file PRMInference.h.

◆ EvidenceConstIterator

template<GUM_Numeric GUM_SCALAR>
using gum::prm::PRMInference< GUM_SCALAR >::EvidenceConstIterator
privateinherited
Initial value:
typename HashTable< const PRMInstance< GUM_SCALAR >*, EMap* >::const_iterator_safe
The class for generic Hash Tables.
Definition hashTable.h:640
NodeProperty< const Tensor< GUM_SCALAR > * > EMap
Code alias.

Code alias.

Definition at line 241 of file PRMInference.h.

◆ EvidenceIterator

template<GUM_Numeric GUM_SCALAR>
using gum::prm::PRMInference< GUM_SCALAR >::EvidenceIterator
privateinherited
Initial value:

Code alias.

Definition at line 238 of file PRMInference.h.

Constructor & Destructor Documentation

◆ SVE()

template<GUM_Numeric GUM_SCALAR>
gum::prm::SVE< GUM_SCALAR >::SVE ( const PRM< GUM_SCALAR > & prm,
const PRMSystem< GUM_SCALAR > & system )

Default Constructor.

Definition at line 623 of file SVE_tpl.h.

623 :
626 }
PRMInference(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default constructor.
This class is an implementation of the Structured Variable Elimination algorithm on PRM<GUM_SCALAR>.
Definition SVE.h:74
Sequence< std::string > * _class_elim_order_
Definition SVE.h:131
SVE(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default Constructor.
Definition SVE_tpl.h:623

References gum::prm::PRMInference< GUM_SCALAR >::PRMInference(), SVE(), and _class_elim_order_.

Referenced by SVE(), and ~SVE().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~SVE()

template<GUM_Numeric GUM_SCALAR>
gum::prm::SVE< GUM_SCALAR >::~SVE ( )
override

Destructor.

Definition at line 128 of file SVE_tpl.h.

128 {
130
131 for (const auto& elt: _elim_orders_)
132 delete elt.second;
133
134 for (const auto& elt: _lifted_pools_)
135 delete elt.second;
136
137 if (_class_elim_order_ != nullptr) delete _class_elim_order_;
138
139 for (const auto trash: _lifted_trash_)
140 delete trash;
141
142 for (auto set: _delayedVariables_)
143 delete set.second;
144 }
BucketSet _lifted_trash_
Definition SVE.h:142
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > * > _elim_orders_
Definition SVE.h:127
HashTable< const PRMClass< GUM_SCALAR > *, BucketSet * > _lifted_pools_
Definition SVE.h:129
HashTable< const PRMInstance< GUM_SCALAR > *, gum::VariableSet * > _delayedVariables_
Definition SVE.h:133

References SVE(), _class_elim_order_, _delayedVariables_, _elim_orders_, _lifted_pools_, and _lifted_trash_.

Here is the call graph for this function:

Member Function Documentation

◆ _addDelayedVariable_()

template<GUM_Numeric GUM_SCALAR>
void gum::prm::SVE< GUM_SCALAR >::_addDelayedVariable_ ( const PRMInstance< GUM_SCALAR > * i,
const PRMInstance< GUM_SCALAR > * j,
NodeId id )
private

When there is a loop in the references some variable elimination must be delayed, this methods add such variable to delayedVariables to keep track of them.

Parameters
iAn PRMInstance<GUM_SCALAR> with a child of j->get(id).
jThe PRMInstance<GUM_SCALAR> with the delayed variable.
idThe NodeId of the delayed variable.

Definition at line 674 of file SVE_tpl.h.

676 {
677 auto p_dv = _delayedVariables_.tryGet(i);
678 if (!p_dv) {
680 p_dv = _delayedVariables_.tryGet(i);
681 }
682 try {
683 (*p_dv)->insert(&(j->get(id).type().variable()));
684 } catch (DuplicateElement const&) {
685 // happends if j->get(id) is parent of more than one variable in i
686 }
687
688 static std::string dot = ".";
689
690 auto key = j->name() + dot + j->get(id).safeName();
691 if (auto p_cnt = _delayedVariablesCounters_.tryGet(key)) {
692 *p_cnt += 1;
693 } else {
695 }
696 }
std::string name() const override
Returns the name of the current inference algorithm.
Definition SVE_tpl.h:699
HashTable< std::string, Size > _delayedVariablesCounters_
Some variable must be delayed for more than one PRMInstance<GUM_SCALAR>, when the delayed variable co...
Definition SVE.h:140

References _delayedVariables_, _delayedVariablesCounters_, gum::prm::PRMInstance< GUM_SCALAR >::get(), and gum::prm::PRMObject::name().

Referenced by _eliminateNodes_(), and _eliminateNodesDownward_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _checkElimOrder_()

template<GUM_Numeric GUM_SCALAR>
bool gum::prm::SVE< GUM_SCALAR >::_checkElimOrder_ ( const PRMInstance< GUM_SCALAR > * first,
const PRMInstance< GUM_SCALAR > * second )
private

Returns true if second can be eliminated before first.

Definition at line 648 of file SVE_tpl.h.

649 {
650 if (_class_elim_order_ == 0) { _initElimOrder_(); }
651
652 auto first_name = _trim_(first->type().name());
653 auto second_name = _trim_(second->type().name());
655 }
std::string _trim_(std::string_view s)
Returns true if second can be eliminated before first.
Definition SVE_tpl.h:640
void _initElimOrder_()
Returns true if second can be eliminated before first.
Definition SVE_tpl.h:548

References _class_elim_order_, _initElimOrder_(), _trim_(), and gum::prm::PRMInstance< GUM_SCALAR >::type().

Referenced by _eliminateNodes_(), _eliminateNodesDownward_(), and _eliminateNodesUpward_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _eliminateDelayedVariables_()

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

Returns true if second can be eliminated before first.

Definition at line 230 of file SVE_tpl.h.

232 {
234
235 for (const auto var: *_delayedVariables_[i]) {
237
238 for (const auto pot: pool)
239 if (pot->contains(*var)) {
240 bucket->add(*pot);
241 toRemove.insert(pot);
242 }
243
244 for (const auto pot: toRemove)
245 pool.erase(pot);
246
247 for (const auto other: bucket->allVariables())
248 if (other != var) bucket->add(*other);
249
251 trash.insert(bucket_pot);
252 pool.insert(bucket_pot);
253 }
254 }

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

Referenced by _eliminateNodes_(), and _variableElimination_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _eliminateNodes_()

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

Returns true if second can be eliminated before first.

Definition at line 147 of file SVE_tpl.h.

150 {
153 // Downward elimination
155 ignore.insert(query);
156
157 for (auto iter = query->beginInvRef(); iter != query->endInvRef(); ++iter) {
158 for (auto child = (*(iter.val())).begin(); child != (*(iter.val())).end(); ++child) {
159 if (!ignore.exists(child->first)) {
161 child->first,
162 pool,
163 trash,
164 elim_list,
165 ignore,
166 eliminated);
167 } else if (!eliminated.exists(child->first)) {
168 _addDelayedVariable_(child->first, query, iter.key());
169 delayedVars.insert(iter.key());
170 }
171 }
172 }
173
174 // Eliminating all nodes in query instance, except query
176 const auto moralg = bn.moralGraph();
177 DefaultTriangulation t(&moralg, &(bn.modalities()));
179
180 if (this->hasEvidence(query)) { _insertEvidence_(query, pool); }
181
182 for (auto attr = query->begin(); attr != query->end(); ++attr) {
183 pool.insert(&(const_cast< Tensor< GUM_SCALAR >& >((*(attr.val())).cpf())));
184 }
185
186 for (size_t idx = 0; idx < t.eliminationOrder().size(); ++idx) {
187 if ((t.eliminationOrder()[idx] != node)
188 && (!delayedVars.exists(t.eliminationOrder()[idx]))) {
189 auto var_id = t.eliminationOrder()[idx];
190 const auto& var = bn.variable(var_id);
191 elim_order.push_back(&var);
192 }
193 }
194
196
197 // Eliminating delayed variables, if any
199
200 eliminated.insert(query);
201 // Eliminating instance in elim_list
203
204 while (!elim_list.empty()) {
205 if (_checkElimOrder_(query, elim_list.front())) {
206 if (!ignore.exists(elim_list.front())) {
208 elim_list.front(),
209 pool,
210 trash,
211 elim_list,
212 ignore,
213 eliminated);
214 }
215 } else {
216 tmp_list.insert(elim_list.front());
217 }
218
219 elim_list.popFront();
220 }
221
222 // Upward elimination
223 for (const auto chain: query->type().slotChains())
224 for (const auto parent: query->getInstances(chain->id()))
225 if (!ignore.exists(parent))
227 }
bool hasEvidence() const
Returns true if i has evidence on PRMAttribute<GUM_SCALAR> a.
bool _checkElimOrder_(const PRMInstance< GUM_SCALAR > *first, const PRMInstance< GUM_SCALAR > *second)
Returns true if second can be eliminated before first.
Definition SVE_tpl.h:648
void _eliminateNodesUpward_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, List< const PRMInstance< GUM_SCALAR > * > &elim_list, Set< const PRMInstance< GUM_SCALAR > * > &ignore, Set< const PRMInstance< GUM_SCALAR > * > &eliminated)
Returns true if second can be eliminated before first.
Definition SVE_tpl.h:343
void _insertEvidence_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool)
Returns true if second can be eliminated before first.
Definition SVE_tpl.h:629
void _eliminateNodesDownward_(const PRMInstance< GUM_SCALAR > *from, const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, List< const PRMInstance< GUM_SCALAR > * > &elim_list, Set< const PRMInstance< GUM_SCALAR > * > &ignore, Set< const PRMInstance< GUM_SCALAR > * > &eliminated)
Returns true if second can be eliminated before first.
Definition SVE_tpl.h:257
void _addDelayedVariable_(const PRMInstance< GUM_SCALAR > *i, const PRMInstance< GUM_SCALAR > *j, NodeId id)
When there is a loop in the references some variable elimination must be delayed, this methods add su...
Definition SVE_tpl.h:674
void _eliminateDelayedVariables_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition SVE_tpl.h:230
void eliminateNodes(const std::vector< const DiscreteVariable * > &elim_order, Set< Tensor< GUM_SCALAR > * > &pool, Set< Tensor< GUM_SCALAR > * > &trash)

References _addDelayedVariable_(), _checkElimOrder_(), _delayedVariables_, _eliminateDelayedVariables_(), _eliminateNodesDownward_(), _eliminateNodesUpward_(), _insertEvidence_(), gum::prm::PRMInstance< GUM_SCALAR >::begin(), gum::prm::PRMInstance< GUM_SCALAR >::beginInvRef(), gum::prm::eliminateNodes(), gum::StaticTriangulation::eliminationOrder(), gum::List< Val >::empty(), gum::prm::PRMInstance< GUM_SCALAR >::end(), gum::prm::PRMInstance< GUM_SCALAR >::endInvRef(), gum::Set< Key >::exists(), gum::List< Val >::front(), gum::prm::PRMInstance< GUM_SCALAR >::getInstances(), gum::prm::PRMInference< GUM_SCALAR >::hasEvidence(), gum::List< Val >::insert(), gum::Set< Key >::insert(), gum::prm::InstanceBayesNet< GUM_SCALAR >::modalities(), gum::DAGmodel::moralGraph(), gum::List< Val >::popFront(), gum::prm::PRMInstance< GUM_SCALAR >::type(), and gum::prm::InstanceBayesNet< GUM_SCALAR >::variable().

Referenced by posterior_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _eliminateNodesDownward_()

template<GUM_Numeric GUM_SCALAR>
void gum::prm::SVE< GUM_SCALAR >::_eliminateNodesDownward_ ( const PRMInstance< GUM_SCALAR > * from,
const PRMInstance< GUM_SCALAR > * i,
BucketSet & pool,
BucketSet & trash,
List< const PRMInstance< GUM_SCALAR > * > & elim_list,
Set< const PRMInstance< GUM_SCALAR > * > & ignore,
Set< const PRMInstance< GUM_SCALAR > * > & eliminated )
private

Returns true if second can be eliminated before first.

Definition at line 257 of file SVE_tpl.h.

264 {
266 ignore.insert(i);
267 // Calling elimination over child instance
269
270 for (auto iter = i->beginInvRef(); iter != i->endInvRef(); ++iter) {
271 for (auto child = (*(iter.val())).begin(); child != (*(iter.val())).end(); ++child) {
272 if (!ignore.exists(child->first)) {
274 } else if (!eliminated.exists(child->first)) {
275 _addDelayedVariable_(child->first, i, iter.key());
276 delayedVars.insert(iter.key());
277 }
278 }
279 }
280
281 // Eliminating all nodes in current instance
283 eliminated.insert(i);
284
285 // Calling elimination over child's parents
286 for (const auto node: my_list) {
287 if (_checkElimOrder_(i, node) && (node != from)) {
288 if (!ignore.exists(node)) {
290 }
291 } else if (node != from) {
292 elim_list.insert(node);
293 }
294 }
295
296 // Adding parents instance to elim_list
297 for (const auto chain: i->type().slotChains()) {
298 for (const auto inst: i->getInstances(chain->id())) {
299 if (inst != from) { elim_list.insert(inst); }
300 }
301 }
302 }
void _variableElimination_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, Set< NodeId > *delayedVars=0)
Returns true if second can be eliminated before first.
Definition SVE_tpl.h:305

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

Referenced by _eliminateNodes_(), _eliminateNodesDownward_(), and _eliminateNodesUpward_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _eliminateNodesUpward_()

template<GUM_Numeric GUM_SCALAR>
void gum::prm::SVE< GUM_SCALAR >::_eliminateNodesUpward_ ( const PRMInstance< GUM_SCALAR > * i,
BucketSet & pool,
BucketSet & trash,
List< const PRMInstance< GUM_SCALAR > * > & elim_list,
Set< const PRMInstance< GUM_SCALAR > * > & ignore,
Set< const PRMInstance< GUM_SCALAR > * > & eliminated )
private

Returns true if second can be eliminated before first.

Definition at line 343 of file SVE_tpl.h.

349 {
350 // Downward elimination
351 ignore.insert(i);
352
353 for (auto iter = i->beginInvRef(); iter != i->endInvRef(); ++iter) {
354 for (auto child = (*(iter.val())).begin(); child != (*(iter.val())).end(); ++child) {
355 if (!ignore.exists(child->first)) {
357 }
358 }
359 }
360
361 // Eliminating all nodes in i instance
363 eliminated.insert(i);
364 // Eliminating instance in elim_list
366
367 while (!elim_list.empty()) {
368 if (_checkElimOrder_(i, elim_list.front())) {
369 if (!ignore.exists(elim_list.front())) {
371 elim_list.front(),
372 pool,
373 trash,
374 elim_list,
375 ignore,
376 eliminated);
377 }
378 } else {
379 tmp_list.insert(elim_list.front());
380 }
381
382 elim_list.popFront();
383 }
384
385 // Upward elimination
386 for (const auto chain: i->type().slotChains()) {
387 for (const auto parent: i->getInstances(chain->id())) {
388 if (!ignore.exists(parent)) {
390 }
391 }
392 }
393 }

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

Referenced by _eliminateNodes_(), and _eliminateNodesUpward_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _eliminateNodesWithEvidence_()

template<GUM_Numeric GUM_SCALAR>
void gum::prm::SVE< GUM_SCALAR >::_eliminateNodesWithEvidence_ ( const PRMInstance< GUM_SCALAR > * i,
BucketSet & pool,
BucketSet & trash,
Set< NodeId > * delayedVars = 0 )
private

Returns true if second can be eliminated before first.

Definition at line 396 of file SVE_tpl.h.

399 {
400 // First we check if evidences are on inner nodes
401 bool inner = false;
402
403 for (const auto& elt: this->evidence(i)) {
404 inner
405 = i->type().isInputNode(i->get(elt.first)) || i->type().isInnerNode(i->get(elt.first));
406
407 if (inner) { break; }
408 }
409
410 // Evidence on inner nodes
411 if (inner) {
414
415 // We need a local to not eliminate queried inner nodes of the same
416 // class
417 for (const auto& elt: *i) {
418 tmp_pool.insert(&(const_cast< Tensor< GUM_SCALAR >& >(elt.second->cpf())));
419 }
420
422 const auto moralg = bn.moralGraph();
423 DefaultTriangulation t(&moralg, &(bn.modalities()));
424 const std::vector< NodeId >& full_elim_order = t.eliminationOrder();
425 // Removing Output nodes of elimination order
428
429 for (size_t idx = 0; idx < full_elim_order.size(); ++idx) {
430 auto var_id = full_elim_order[idx];
431 const auto& var = bn.variable(var_id);
432
433 if (!i->type().isOutputNode(i->get(full_elim_order[idx]))) {
434 inner_elim_order.push_back(&var);
435 } else if (delayedVars != nullptr) {
436 if (!delayedVars->exists(full_elim_order[idx])) { output_elim_order.push_back(&var); }
437 } else {
438 output_elim_order.push_back(&var);
439 }
440 }
441
443
444 // Now we add the new tensors in pool and eliminate output nodes
445 for (const auto pot: tmp_pool)
446 pool.insert(pot);
447
449
450 } else {
454
455 for (const auto agg: i->type().aggregates())
456 pool.insert(_getAggTensor_(i, agg));
457
458 if (auto p_eo2 = _elim_orders_.tryGet(&(i->type())); !p_eo2) {
459 GUM_ERROR(FatalError, "there should be at least one node here.")
460 } else {
462
463 for (auto iter = (*p_eo2)->begin(); iter != (*p_eo2)->end(); ++iter) {
464 const auto& var = bn.variable(*iter);
465 if (delayedVars != nullptr) {
466 if (!delayedVars->exists(*iter)) { elim.push_back(&var); }
467 } else {
468 elim.push_back(&var);
469 }
470 }
471
473 }
474 }
475 }
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
Set< Tensor< GUM_SCALAR > * > BucketSet
Code alias.
Definition SVE.h:123
void _insertLiftedNodes_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition SVE_tpl.h:478
Tensor< GUM_SCALAR > * _getAggTensor_(const PRMInstance< GUM_SCALAR > *i, const PRMAggregate< GUM_SCALAR > *agg)
Returns true if second can be eliminated before first.
Definition SVE_tpl.h:658
#define GUM_ERROR(type, msg)
Definition exceptions.h:76

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

Referenced by _variableElimination_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _EMap_()

template<GUM_Numeric GUM_SCALAR>
PRMInference< GUM_SCALAR >::EMap & gum::prm::PRMInference< GUM_SCALAR >::_EMap_ ( const PRMInstance< GUM_SCALAR > * i)
privateinherited

Private getter over evidences, if necessary creates an EMap for i.

Definition at line 117 of file PRMInference_tpl.h.

117 {
118 if (auto p = _evidences_.tryGet(i)) {
119 return **p;
120 } else {
122 _evidences_.insert(i, emap);
123 return *emap;
124 }
125 }
This abstract class is used as base class for all inference class on PRM<GUM_SCALAR>.
HashTable< const PRMInstance< GUM_SCALAR > *, EMap * > _evidences_
Mapping of evidence over PRMInstance<GUM_SCALAR>'s nodes.

References _evidences_.

Referenced by removeEvidence().

Here is the caller graph for this function:

◆ _getAggTensor_()

template<GUM_Numeric GUM_SCALAR>
Tensor< GUM_SCALAR > * gum::prm::SVE< GUM_SCALAR >::_getAggTensor_ ( const PRMInstance< GUM_SCALAR > * i,
const PRMAggregate< GUM_SCALAR > * agg )
private

Returns true if second can be eliminated before first.

Definition at line 658 of file SVE_tpl.h.

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

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

Referenced by _eliminateNodesWithEvidence_(), and _variableElimination_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _getElimOrder_()

template<GUM_Numeric GUM_SCALAR>
std::vector< NodeId > & gum::prm::SVE< GUM_SCALAR >::_getElimOrder_ ( const PRMClass< GUM_SCALAR > & c)
private

Returns true if second can be eliminated before first.

Definition at line 635 of file SVE_tpl.h.

635 {
636 return *(_elim_orders_[&c]);
637 }

References _elim_orders_.

◆ _initElimOrder_()

template<GUM_Numeric GUM_SCALAR>
void gum::prm::SVE< GUM_SCALAR >::_initElimOrder_ ( )
private

Returns true if second can be eliminated before first.

Definition at line 548 of file SVE_tpl.h.

548 {
552
553 for (const auto node: cdg.internalDag().nodes()) {
554 if (cdg.internalDag().parents(node).empty()) { l.push_back(node); }
555 }
556
558
559 while (!l.empty()) {
560 visited_node.insert(l.front());
561
562 if (!class_elim_order.exists(cdg.get(l.front()).first)) {
563 class_elim_order.insert(cdg.get(l.front()).first);
564 }
565
566 for (const auto child: cdg.internalDag().children(l.front())) {
567 if (!visited_node.contains(child)) { l.push_back(child); }
568 }
569
570 l.pop_front();
571 }
572
574 for (auto c: class_elim_order) {
575 std::string name = c->name();
576 if (auto pos = name.find_first_of("<"); pos != std::string::npos) {
577 name = name.substr(0, pos);
578 }
579 try {
580 _class_elim_order_->insert(name);
581 } catch (DuplicateElement const&) {}
582 }
583 }
PRM< GUM_SCALAR > const * prm_
The PRM<GUM_SCALAR> on which inference is done.

References _class_elim_order_, gum::ArcGraphPart::children(), gum::Set< Key >::contains(), gum::Set< Key >::empty(), gum::SequenceImplementation< Key, Gen >::exists(), gum::prm::ClassDependencyGraph< GUM_SCALAR >::get(), gum::SequenceImplementation< Key, Gen >::insert(), gum::Set< Key >::insert(), gum::prm::ClassDependencyGraph< GUM_SCALAR >::internalDag(), name(), gum::NodeGraphPart::nodes(), gum::ArcGraphPart::parents(), and gum::prm::PRMInference< GUM_SCALAR >::prm_.

Referenced by _checkElimOrder_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _initLiftedNodes_()

template<GUM_Numeric GUM_SCALAR>
void gum::prm::SVE< GUM_SCALAR >::_initLiftedNodes_ ( const PRMClass< GUM_SCALAR > & c)
private

Returns true if second can be eliminated before first.

Definition at line 498 of file SVE_tpl.h.

498 {
500 _lifted_pools_.insert(&c, lifted_pool);
502
503 for (const auto node: c.containerDag().nodes())
505 if (c.isOutputNode(c.get(node))) outers.insert(node);
506 else if (!outers.exists(node)) inners.insert(node);
507
508 lifted_pool->insert(const_cast< Tensor< GUM_SCALAR >* >(&(c.get(node).cpf())));
510 outers.insert(node);
511
512 // We need to put in the output_elim_order aggregator's parents which
513 // are
514 // innner nodes
515 for (const auto par: c.containerDag().parents(node))
517 && c.isInnerNode(c.get(par))) {
518 inners.erase(par);
519 outers.insert(par);
520 }
521 }
522
523 // Now we proceed with the elimination of inner attributes
526
527 if (inners.size()) partial_ordering.push_back(inners);
528
529 if (outers.size()) partial_ordering.push_back(outers);
530
531 const auto moralg = bn.moralGraph();
533
534 for (size_t idx = 0; idx < inners.size(); ++idx)
535 eliminateNode(&(c.get(t.eliminationOrder()[idx]).type().variable()),
538
539 // If there is not only inner and input Attributes
540 if (outers.size()) {
541 _elim_orders_.insert(&c,
542 new std::vector< NodeId >(t.eliminationOrder().begin() + inners.size(),
543 t.eliminationOrder().end()));
544 }
545 }
void eliminateNode(const DiscreteVariable *var, Set< Tensor< GUM_SCALAR > * > &pool, Set< Tensor< GUM_SCALAR > * > &trash)
Proceeds with the elimination of var in pool.

References _elim_orders_, _lifted_pools_, _lifted_trash_, gum::prm::PRMClassElementContainer< GUM_SCALAR >::containerDag(), gum::prm::eliminateNode(), gum::StaticTriangulation::eliminationOrder(), gum::Set< Key >::erase(), gum::Set< Key >::exists(), gum::prm::PRMClass< GUM_SCALAR >::get(), gum::Set< Key >::insert(), gum::prm::PRMClassElement< GUM_SCALAR >::isAggregate(), gum::prm::PRMClassElement< GUM_SCALAR >::isAttribute(), gum::prm::PRMClassElementContainer< GUM_SCALAR >::isInnerNode(), gum::prm::PRMClass< GUM_SCALAR >::isOutputNode(), gum::prm::ClassBayesNet< GUM_SCALAR >::modalities(), gum::DAGmodel::moralGraph(), gum::NodeGraphPart::nodes(), gum::ArcGraphPart::parents(), gum::List< Val >::push_back(), and gum::Set< Key >::size().

Referenced by _insertLiftedNodes_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _insertEvidence_()

template<GUM_Numeric GUM_SCALAR>
void gum::prm::SVE< GUM_SCALAR >::_insertEvidence_ ( const PRMInstance< GUM_SCALAR > * i,
BucketSet & pool )
private

Returns true if second can be eliminated before first.

Definition at line 629 of file SVE_tpl.h.

629 {
630 for (const auto& elt: this->evidence(i))
631 pool.insert(const_cast< Tensor< GUM_SCALAR >* >(elt.second));
632 }

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

Referenced by _eliminateNodes_(), and _eliminateNodesWithEvidence_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _insertLiftedNodes_()

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

Returns true if second can be eliminated before first.

Definition at line 478 of file SVE_tpl.h.

480 {
482
483 auto p_lp = _lifted_pools_.tryGet(&(i->type()));
484 if (!p_lp) {
485 _initLiftedNodes_(i->type());
486 p_lp = _lifted_pools_.tryGet(&(i->type()));
487 }
488 lifted_pool = *p_lp;
489
490 for (const auto lifted_pot: *lifted_pool) {
492 pool.insert(pot);
493 trash.insert(pot);
494 }
495 }
void _initLiftedNodes_(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition SVE_tpl.h:498
Tensor< GUM_SCALAR > * copyTensor(const Bijection< const DiscreteVariable *, const DiscreteVariable * > &bij, const Tensor< GUM_SCALAR > &source)
Returns a copy of a Tensor after applying a bijection over the variables in source.

References _initLiftedNodes_(), _lifted_pools_, gum::prm::PRMInstance< GUM_SCALAR >::bijection(), gum::prm::copyTensor(), gum::Set< Key >::insert(), and gum::prm::PRMInstance< GUM_SCALAR >::type().

Referenced by _eliminateNodesWithEvidence_(), and _variableElimination_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _trim_()

template<GUM_Numeric GUM_SCALAR>
std::string gum::prm::SVE< GUM_SCALAR >::_trim_ ( std::string_view s)
private

Returns true if second can be eliminated before first.

Definition at line 640 of file SVE_tpl.h.

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

Referenced by _checkElimOrder_().

Here is the caller graph for this function:

◆ _variableElimination_()

template<GUM_Numeric GUM_SCALAR>
void gum::prm::SVE< GUM_SCALAR >::_variableElimination_ ( const PRMInstance< GUM_SCALAR > * i,
BucketSet & pool,
BucketSet & trash,
Set< NodeId > * delayedVars = 0 )
private

Returns true if second can be eliminated before first.

Definition at line 305 of file SVE_tpl.h.

308 {
309 if (this->hasEvidence(i)) {
311 } else {
313
314 for (const auto agg: i->type().aggregates())
315 pool.insert(_getAggTensor_(i, agg));
316
317 if (auto p_eo = _elim_orders_.tryGet(&(i->type()))) {
319
321
322 for (const auto node: *(*p_eo)) {
323 const auto& var = bn.variable(node);
324 if (delayedVars != nullptr) {
325 if (!delayedVars->exists(node)) {
326 const auto& var = bn.variable(node);
327 elim.push_back(&var);
328 }
329 } else {
330 elim.push_back(&var);
331 }
332 }
333
335 }
336 }
337
338 // Eliminating delayed variables, if any
340 }
void _eliminateNodesWithEvidence_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, Set< NodeId > *delayedVars=0)
Returns true if second can be eliminated before first.
Definition SVE_tpl.h:396

References _delayedVariables_, _elim_orders_, _eliminateDelayedVariables_(), _eliminateNodesWithEvidence_(), _getAggTensor_(), _insertLiftedNodes_(), gum::prm::eliminateNodes(), gum::Set< Key >::exists(), gum::prm::PRMInference< GUM_SCALAR >::hasEvidence(), gum::Set< Key >::insert(), gum::prm::PRMInstance< GUM_SCALAR >::type(), and gum::prm::InstanceBayesNet< GUM_SCALAR >::variable().

Referenced by _eliminateNodesDownward_(), and _eliminateNodesUpward_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addEvidence()

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

Add an evidence to the given instance's elt.

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

Definition at line 128 of file PRMInference_tpl.h.

129 {
130 if (chain.first->exists(chain.second->id())) {
131 if ((p.nbrDim() != 1) || (!p.contains(chain.second->type().variable())))
132 GUM_ERROR(OperationNotAllowed, "illegal evidence for the given PRMAttribute.")
133
135 e->add(chain.second->type().variable());
136 Instantiation i(*e);
137
138 for (i.setFirst(); !i.end(); i.inc())
139 e->set(i, p.get(i));
140
142
143 if (emap.exists(chain.second->id())) {
144 delete emap[chain.second->id()];
145 emap[chain.second->id()] = e;
146 } else {
147 emap.insert(chain.second->id(), e);
148 }
149
151 } else {
153 "the given PRMAttribute does not belong to this "
154 "Instance<GUM_SCALAR>.");
155 }
156 }
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.

References gum::Instantiation::end(), GUM_ERROR, gum::Instantiation::inc(), and gum::Instantiation::setFirst().

Here is the call graph for this function:

◆ clearEvidence()

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

Remove all evidences.

Definition at line 57 of file PRMInference_tpl.h.

57 {
58 for (const auto& elt: _evidences_) {
59 for (const auto& elt2: *elt.second)
60 delete elt2.second;
61
62 delete elt.second;
63 }
64
65 _evidences_.clear();
66 }

References _evidences_.

Referenced by ~PRMInference(), and operator=().

Here is the caller graph for this function:

◆ evidence() [1/4]

template<GUM_Numeric GUM_SCALAR>
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 173 of file PRMInference_tpl.h.

173 {
174 if (!_evidences_.exists(&i)) GUM_ERROR(NotFound, "this instance has no evidence.")
175 return *(_evidences_[&i]);
176 }

References _evidences_, and GUM_ERROR.

Referenced by gum::prm::SVE< GUM_SCALAR >::_eliminateNodesWithEvidence_(), gum::prm::SVED< GUM_SCALAR >::_eliminateNodesWithEvidence_(), gum::prm::SVE< GUM_SCALAR >::_insertEvidence_(), gum::prm::SVED< GUM_SCALAR >::_insertEvidence_(), gum::prm::StructuredInference< GUM_SCALAR >::_reduceAloneInstances_(), evidence(), gum::prm::GroundedInference< GUM_SCALAR >::evidenceAdded_(), hasEvidence(), posterior(), and gum::prm::StructuredInference< GUM_SCALAR >::posterior_().

Here is the caller graph for this function:

◆ evidence() [2/4]

template<GUM_Numeric GUM_SCALAR>
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 180 of file PRMInference_tpl.h.

180 {
181 if (!_evidences_.exists(&i)) GUM_ERROR(NotFound, "this instance has no evidence.")
182 return *(_evidences_[&i]);
183 }

References _evidences_, and GUM_ERROR.

◆ evidence() [3/4]

template<GUM_Numeric GUM_SCALAR>
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 187 of file PRMInference_tpl.h.

187 {
188 if (!_evidences_.exists(i)) GUM_ERROR(NotFound, "this instance has no evidence.")
189 return *(_evidences_[i]);
190 }

References PRMInference(), _evidences_, evidence(), and GUM_ERROR.

Here is the call graph for this function:

◆ evidence() [4/4]

template<GUM_Numeric GUM_SCALAR>
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 194 of file PRMInference_tpl.h.

194 {
195 if (!_evidences_.exists(i)) GUM_ERROR(NotFound, "this instance has no evidence.")
196 return *(_evidences_[i]);
197 }

References _evidences_, and GUM_ERROR.

◆ evidenceAdded_()

template<GUM_Numeric GUM_SCALAR>
void gum::prm::SVE< GUM_SCALAR >::evidenceAdded_ ( const Chain & chain)
overrideprotectedvirtual

See PRMInference<GUM_SCALAR>::evidenceAdded_().

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

Definition at line 664 of file SVE_tpl.h.

664 {
665 // Do nothing
666 }

◆ evidenceRemoved_()

template<GUM_Numeric GUM_SCALAR>
void gum::prm::SVE< GUM_SCALAR >::evidenceRemoved_ ( const Chain & chain)
overrideprotectedvirtual

See PRMInference<GUM_SCALAR>::evidenceRemoved_().

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

Definition at line 669 of file SVE_tpl.h.

669 {
670 // Do nothing
671 }

◆ hasEvidence() [1/4]

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

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

Definition at line 215 of file PRMInference_tpl.h.

215 {
216 return (_evidences_.size() != (Size)0);
217 }

References _evidences_.

◆ hasEvidence() [2/4]

template<GUM_Numeric GUM_SCALAR>
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 210 of file PRMInference_tpl.h.

210 {
211 return (hasEvidence(chain.first)) ? evidence(chain.first).exists(chain.second->id()) : false;
212 }
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.

References evidence(), and hasEvidence().

Here is the call graph for this function:

◆ hasEvidence() [3/4]

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

◆ hasEvidence() [4/4]

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

Returns EMap of evidences over i.

Definition at line 205 of file PRMInference_tpl.h.

205 {
206 return _evidences_.exists(i);
207 }

References _evidences_.

◆ joint()

template<GUM_Numeric GUM_SCALAR>
void gum::prm::PRMInference< GUM_SCALAR >::joint ( const std::vector< Chain > & chains,
Tensor< 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 257 of file PRMInference_tpl.h.

259 {
260 if (j.nbrDim() > 0) { GUM_ERROR(OperationNotAllowed, "the given Tensor is not empty.") }
261
262 for (auto chain = chains.begin(); chain != chains.end(); ++chain) {
263 j.add(chain->second->type().variable());
264 }
265
266 joint_(chains, j);
267 }
virtual void joint_(const std::vector< Chain > &queries, Tensor< GUM_SCALAR > &j)=0
Generic method to compute the posterior of given element.

References GUM_ERROR, and joint_().

Here is the call graph for this function:

◆ joint_()

template<GUM_Numeric GUM_SCALAR>
void gum::prm::SVE< GUM_SCALAR >::joint_ ( const std::vector< Chain > & queries,
Tensor< GUM_SCALAR > & j )
overrideprotectedvirtual

See PRMInference<GUM_SCALAR>::joint_().

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

Definition at line 618 of file SVE_tpl.h.

618 {
619 GUM_ERROR(FatalError, "Not implemented.")
620 }

References GUM_ERROR.

◆ name()

template<GUM_Numeric GUM_SCALAR>
std::string gum::prm::SVE< GUM_SCALAR >::name ( ) const
overridevirtual

Returns the name of the current inference algorithm.

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

Definition at line 699 of file SVE_tpl.h.

699 {
700 return "SVE";
701 }

Referenced by _initElimOrder_().

Here is the caller graph for this function:

◆ posterior()

template<GUM_Numeric GUM_SCALAR>
void gum::prm::PRMInference< GUM_SCALAR >::posterior ( const Chain & chain,
Tensor< 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 231 of file PRMInference_tpl.h.

233 {
234 if (m.nbrDim() > 0) { GUM_ERROR(OperationNotAllowed, "the given Tensor is not empty.") }
235
236 if (hasEvidence(chain)) {
237 m.add(chain.second->type().variable());
238 const Tensor< GUM_SCALAR >& e = *(evidence(chain.first)[chain.second->id()]);
239 Instantiation i(m), j(e);
240
241 for (i.setFirst(), j.setFirst(); !i.end(); i.inc(), j.inc())
242 m.set(i, e.get(j));
243 } else {
244 if (chain.second != &(chain.first->get(chain.second->safeName()))) {
246 = std::make_pair(chain.first, &(chain.first->get(chain.second->safeName())));
247 m.add(good_chain.second->type().variable());
249 } else {
250 m.add(chain.second->type().variable());
252 }
253 }
254 }
virtual void posterior_(const Chain &chain, Tensor< GUM_SCALAR > &m)=0
Generic method to compute the posterior of given element.

References gum::prm::PRMInstance< GUM_SCALAR >::end(), evidence(), GUM_ERROR, hasEvidence(), gum::Instantiation::inc(), and gum::Instantiation::setFirst().

Here is the call graph for this function:

◆ posterior_()

template<GUM_Numeric GUM_SCALAR>
void gum::prm::SVE< GUM_SCALAR >::posterior_ ( const Chain & chain,
Tensor< GUM_SCALAR > & m )
overrideprotectedvirtual

See PRMInference<GUM_SCALAR>::posterior_().

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

Definition at line 586 of file SVE_tpl.h.

586 {
587 const PRMInstance< GUM_SCALAR >* i = chain.first;
588 const PRMAttribute< GUM_SCALAR >* elt = chain.second;
590
591 _eliminateNodes_(i, elt->id(), pool, trash);
592
594
595 for (const auto pot: pool) {
596 if (pot->contains(elt->type().variable())) { result.push_back(pot); }
597 }
598
599 while (result.size() > 1) {
600 auto& p1 = *(result.back());
601 result.pop_back();
602 auto& p2 = *(result.back());
603 result.pop_back();
604 auto mult = new Tensor< GUM_SCALAR >(p1 * p2);
605 trash.insert(mult);
606 result.push_back(mult);
607 }
608
609 m = *(result.back());
610 m.normalize();
611
612 for (const auto pot: trash) {
613 delete pot;
614 }
615 }
void _eliminateNodes_(const PRMInstance< GUM_SCALAR > *query, NodeId id, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition SVE_tpl.h:147

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

Here is the call graph for this function:

◆ removeEvidence()

template<GUM_Numeric GUM_SCALAR>
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 220 of file PRMInference_tpl.h.

220 {
221 if (hasEvidence(chain.first)) {
222 if (_EMap_(chain.first).exists(chain.second->id())) {
224 delete _EMap_(chain.first)[chain.second->id()];
225 _EMap_(chain.first).erase(chain.second->id());
226 }
227 }
228 }
void erase(const Key &key)
Removes a given element from the hash table.
virtual void evidenceRemoved_(const Chain &chain)=0
This method is called whenever an evidence is removed, but BEFORE any processing made by PRMInference...

References _EMap_(), evidenceRemoved_(), and hasEvidence().

Here is the call graph for this function:

Member Data Documentation

◆ _class_elim_order_

template<GUM_Numeric GUM_SCALAR>
Sequence< std::string >* gum::prm::SVE< GUM_SCALAR >::_class_elim_order_
private

Definition at line 131 of file SVE.h.

Referenced by SVE(), ~SVE(), _checkElimOrder_(), and _initElimOrder_().

◆ _delayedVariables_

template<GUM_Numeric GUM_SCALAR>
HashTable< const PRMInstance< GUM_SCALAR >*, gum::VariableSet* > gum::prm::SVE< GUM_SCALAR >::_delayedVariables_
private

◆ _delayedVariablesCounters_

template<GUM_Numeric GUM_SCALAR>
HashTable< std::string, Size > gum::prm::SVE< GUM_SCALAR >::_delayedVariablesCounters_
private

Some variable must be delayed for more than one PRMInstance<GUM_SCALAR>, when the delayed variable counter reach 0 it can be eliminated.

Definition at line 140 of file SVE.h.

Referenced by _addDelayedVariable_().

◆ _elim_orders_

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

◆ _evidences_

template<GUM_Numeric GUM_SCALAR>
HashTable< const PRMInstance< GUM_SCALAR >*, EMap* > gum::prm::PRMInference< GUM_SCALAR >::_evidences_
privateinherited

Mapping of evidence over PRMInstance<GUM_SCALAR>'s nodes.

Definition at line 245 of file PRMInference.h.

Referenced by PRMInference(), _EMap_(), clearEvidence(), evidence(), evidence(), evidence(), evidence(), hasEvidence(), hasEvidence(), and hasEvidence().

◆ _lifted_pools_

template<GUM_Numeric GUM_SCALAR>
HashTable< const PRMClass< GUM_SCALAR >*, BucketSet* > gum::prm::SVE< GUM_SCALAR >::_lifted_pools_
private

Definition at line 129 of file SVE.h.

Referenced by ~SVE(), _initLiftedNodes_(), and _insertLiftedNodes_().

◆ _lifted_trash_

template<GUM_Numeric GUM_SCALAR>
BucketSet gum::prm::SVE< GUM_SCALAR >::_lifted_trash_
private

Definition at line 142 of file SVE.h.

Referenced by ~SVE(), and _initLiftedNodes_().

◆ prm_

◆ sys_


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