aGrUM 3.0.0
a C++ library for (probabilistic) graphical models
SVED_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41#pragma once
42
43
51
52namespace gum {
53 namespace prm {
54
55 template < GUM_Numeric GUM_SCALAR >
57 GUM_DESTRUCTOR(SVED);
58
59 for (const auto& elt: _elim_orders_)
60 delete elt.second;
61
62 if (_class_elim_order_ != nullptr) delete _class_elim_order_;
63 }
64
65 template < GUM_Numeric GUM_SCALAR >
67 NodeId node,
68 BucketSet& pool,
69 BucketSet& trash) {
71 ignore.insert(query);
72 // Extracting required attributes and slotchains
73 Set< NodeId >& attr_set = _getAttrSet_(query);
74 Set< NodeId >& sc_set = _getSCSet_(query);
75 // Downward elimination
77
78 for (const auto attr: attr_set) {
79 if (query->hasRefAttr(attr)) {
80 for (auto iter = query->getRefAttr(attr).begin(); iter != query->getRefAttr(attr).end();
81 ++iter)
82 if ((!ignore.exists(iter->first)) && (_bb_.exists(iter->first)))
83 _eliminateNodesDownward_(query, iter->first, pool, trash, elim_list, ignore);
84 }
85 }
86
87 // Eliminating all nodes in query instance, except query
89 const auto moralg = bn.moralGraph();
90 DefaultTriangulation t(&moralg, &(bn.modalities()));
91 std::vector< const DiscreteVariable* > elim_order;
92
93 if (this->hasEvidence(query)) _insertEvidence_(query, pool);
94
95 for (const auto attr: attr_set)
96 pool.insert(&(const_cast< Tensor< GUM_SCALAR >& >(query->get(attr).cpf())));
97
98 for (size_t idx = 0; idx < t.eliminationOrder().size(); ++idx) {
99 if (t.eliminationOrder()[idx] != node) {
100 auto var_id = t.eliminationOrder()[idx];
101 const auto& var = bn.variable(var_id);
102 elim_order.push_back(&var);
103 }
104 }
105
106 eliminateNodes(elim_order, pool, trash);
107 // Eliminating instance in elim_list
109 _reduceElimList_(query, elim_list, tmp_list, ignore, pool, trash);
110
111 while (!elim_list.empty()) {
112 if (_checkElimOrder_(query, elim_list.front())) {
113 if ((!ignore.exists(elim_list.front())) && (_bb_.exists(elim_list.front())))
114 _eliminateNodesDownward_(query, elim_list.front(), pool, trash, elim_list, ignore);
115 } else if (_bb_.exists(elim_list.front())) {
116 tmp_list.insert(elim_list.front());
117 }
118
119 elim_list.popFront();
120 }
121
122 // Upward elimination
123 for (const auto chain: sc_set)
124 for (const auto parent: query->getInstances(chain))
125 if ((!ignore.exists(parent)) && (_bb_.exists(*parent)))
126 _eliminateNodesUpward_(parent, pool, trash, tmp_list, ignore);
127 }
128
129 template < GUM_Numeric GUM_SCALAR >
131 const PRMInstance< GUM_SCALAR >* from,
133 BucketSet& pool,
134 BucketSet& trash,
135 List< const PRMInstance< GUM_SCALAR >* >& elim_list,
136 Set< const PRMInstance< GUM_SCALAR >* >& ignore) {
137 ignore.insert(i);
138 // Extracting required attributes and slotchains
139 Set< NodeId >& attr_set = _getAttrSet_(i);
140 Set< NodeId >& sc_set = _getSCSet_(i);
141 // Calling elimination over child instance
143
144 for (const auto attr: attr_set) {
145 if (i->hasRefAttr(attr)) {
146 for (auto iter = i->getRefAttr(attr).begin(); iter != i->getRefAttr(attr).end(); ++iter)
147 if ((!ignore.exists(iter->first)) && (_bb_.exists(iter->first)))
148 _eliminateNodesDownward_(i, iter->first, pool, trash, my_list, ignore);
149 }
150 }
151
152 // Eliminating all nodes in current instance
153 if (this->hasEvidence(i)) {
154 _eliminateNodesWithEvidence_(i, pool, trash);
155 } else {
156 _insertLiftedNodes_(i, pool, trash);
157
158 for (const auto agg: i->type().aggregates())
159 if (_bb_.requisiteNodes(i).exists(agg->id())) pool.insert(_getAggTensor_(i, agg));
160
161 if (auto p_eo = _elim_orders_.tryGet(&(i->type()))) {
163 std::vector< const DiscreteVariable* > elim_order;
164
165 for (auto node: *(*p_eo)) {
166 const auto& var = bn.variable(node);
167 elim_order.push_back(&var);
168 }
169
170 eliminateNodes(elim_order, pool, trash);
171 }
172 }
173
174 // Calling elimination over child's parents
175 while (!my_list.empty()) {
176 if (_checkElimOrder_(i, my_list.front())) {
177 if ((!ignore.exists(my_list.front())) && (_bb_.exists(my_list.front())))
178 _eliminateNodesDownward_(i, my_list.front(), pool, trash, my_list, ignore);
179 } else if (_bb_.exists(my_list.front())) {
180 elim_list.insert(my_list.front());
181 }
182
183 my_list.popFront();
184 }
185
186 // Adding parents instance to elim_list
187 for (const auto chain: sc_set)
188 for (const auto parent: i->getInstances(chain))
189 if ((!ignore.exists(parent)) && _bb_.exists(parent) && (parent != from))
190 elim_list.insert(parent);
191 }
192
193 template < GUM_Numeric GUM_SCALAR >
196 BucketSet& pool,
197 BucketSet& trash,
198 List< const PRMInstance< GUM_SCALAR >* >& elim_list,
199 Set< const PRMInstance< GUM_SCALAR >* >& ignore) {
200 ignore.insert(i);
201 // Extracting required attributes and slotchains
202 Set< NodeId >& attr_set = _getAttrSet_(i);
203 Set< NodeId >& sc_set = _getSCSet_(i);
204
205 // Downward elimination
206 for (const auto attr: attr_set) {
207 if (i->hasRefAttr(attr)) {
208 for (auto iter = i->getRefAttr(attr).begin(); iter != i->getRefAttr(attr).end(); ++iter)
209 if ((!ignore.exists(iter->first)) && (_bb_.exists(iter->first)))
210 _eliminateNodesDownward_(i, iter->first, pool, trash, elim_list, ignore);
211 }
212 }
213
214 // Eliminating all nodes in i instance
215 if (this->hasEvidence(i)) {
216 _eliminateNodesWithEvidence_(i, pool, trash);
217 } else {
218 _insertLiftedNodes_(i, pool, trash);
219
220 for (const auto agg: i->type().aggregates())
221 if (_bb_.requisiteNodes(i).exists(agg->id())) pool.insert(_getAggTensor_(i, agg));
222
223 if (auto p_eo = _elim_orders_.tryGet(&(i->type()))) {
225 std::vector< const DiscreteVariable* > elim_order;
226
227 for (auto node: *(*p_eo)) {
228 const auto& var = bn.variable(node);
229 elim_order.push_back(&var);
230 }
231 eliminateNodes(elim_order, pool, trash);
232 }
233 }
234
235 // Eliminating instance in elim_list
237
238 while (!elim_list.empty()) {
239 if (_checkElimOrder_(i, elim_list.front())) {
240 if ((!ignore.exists(elim_list.front())) && (_bb_.exists(elim_list.front())))
241 _eliminateNodesDownward_(i, elim_list.front(), pool, trash, elim_list, ignore);
242 } else if (_bb_.exists(elim_list.front())) {
243 ignore.insert(elim_list.front());
244 }
245
246 elim_list.popFront();
247 }
248
249 // Upward elimination
250 for (const auto chain: sc_set)
251 for (const auto parent: i->getInstances(chain))
252 if ((!ignore.exists(parent)) && (_bb_.exists(parent)))
253 _eliminateNodesUpward_(parent, pool, trash, tmp_list, ignore);
254 }
255
256 template < GUM_Numeric GUM_SCALAR >
258 BucketSet& pool,
259 BucketSet& trash) {
260 // Adding required evidences
261 for (const auto& elt: this->evidence(i))
262 if (_bb_.requisiteNodes(i).exists(elt.first))
263 pool.insert(const_cast< Tensor< GUM_SCALAR >* >(elt.second));
264
265 // Adding tensors and eliminating the remaining nodes
266 for (const auto& a: *i)
267 if (_bb_.requisiteNodes(i).exists(a.first))
268 pool.insert(&(const_cast< Tensor< GUM_SCALAR >& >(a.second->cpf())));
269
271 const auto moralg = bn.moralGraph();
272 DefaultTriangulation t(&moralg, &(bn.modalities()));
273 const std::vector< NodeId >& full_elim_order = t.eliminationOrder();
274
275 for (auto var = full_elim_order.begin(); var != full_elim_order.end(); ++var)
276 eliminateNode(&(i->get(*var).type().variable()), pool, trash);
277 }
278
279 template < GUM_Numeric GUM_SCALAR >
281 BucketSet& pool,
282 BucketSet& trash) {
283 BucketSet* lifted_pool = nullptr;
284
285 auto p_lp = _lifted_pools_.tryGet(&(_bb_.requisiteNodes(i)));
286 if (!p_lp) {
287 _initLiftedNodes_(i, trash);
288 p_lp = _lifted_pools_.tryGet(&(_bb_.requisiteNodes(i)));
289 }
290 lifted_pool = *p_lp;
291
292 for (const auto lifted_pot: *lifted_pool) {
293 Tensor< GUM_SCALAR >* pot = copyTensor(i->bijection(), *lifted_pot);
294 pool.insert(pot);
295 trash.insert(pot);
296 }
297 }
298
299 template < GUM_Numeric GUM_SCALAR >
301 BucketSet& trash) {
302 PRMClass< GUM_SCALAR >& c = const_cast< PRMClass< GUM_SCALAR >& >(i->type());
303 BucketSet* lifted_pool = new BucketSet();
304 _lifted_pools_.insert(&(_bb_.requisiteNodes(i)), lifted_pool);
305
306 for (const auto node: _bb_.requisiteNodes(i))
308 lifted_pool->insert(const_cast< Tensor< GUM_SCALAR >* >(&(c.get(node).cpf())));
309
310 NodeSet inners, outers, ignore;
311
312 for (const auto& elt: *i) {
313 if (_bb_.requisiteNodes(*i).exists(elt.first)) {
315 if (c.isOutputNode(c.get(elt.first))) outers.insert(elt.first);
316 else if (!outers.exists(elt.first)) inners.insert(elt.first);
317 } else if (PRMClassElement< GUM_SCALAR >::isAggregate(c.get(elt.first))) {
318 outers.insert(elt.first);
319
320 // We need to put in the output_elim_order aggregator's parents
321 // which are
322 // innner nodes
323 for (const auto par: c.containerDag().parents(elt.first))
325 && i->type().isInnerNode(i->type().get(par))
326 && _bb_.requisiteNodes(i).exists(par)) {
327 inners.erase(par);
328 outers.insert(par);
329 }
330 }
331 } else {
332 ignore.insert(elt.first);
333 }
334 }
335
336 // Now we proceed with the elimination of inner attributes
338 List< NodeSet > partial_ordering;
339
340 if (inners.size()) partial_ordering.pushBack(inners);
341
342 if (outers.size()) partial_ordering.pushBack(outers);
343
344 if (ignore.size()) partial_ordering.pushBack(ignore);
345
346 GUM_ASSERT(inners.size() || outers.size());
347 const auto moralg = bn.moralGraph();
348 PartialOrderedTriangulation t(&moralg, &(bn.modalities()), &partial_ordering);
349
350 for (size_t idx = 0; idx < inners.size(); ++idx)
351 eliminateNode(&(c.get(t.eliminationOrder()[idx]).type().variable()), *lifted_pool, trash);
352
353 // If there is not only inner and input Attributes
354 if (outers.size()) {
355 _elim_orders_.insert(&c,
356 new std::vector< NodeId >(t.eliminationOrder().begin() + inners.size(),
357 t.eliminationOrder().end()));
358 }
359 }
360
361 template < GUM_Numeric GUM_SCALAR >
365 std::list< NodeId > l;
366
367 for (const auto node: cdg.internalDag().nodes()) {
368 if (cdg.internalDag().parents(node).empty()) { l.push_back(node); }
369 }
370
371 Set< NodeId > visited_node;
372
373 while (!l.empty()) {
374 visited_node.insert(l.front());
375
376 if (!class_elim_order.exists(cdg.get(l.front()).first)) {
377 class_elim_order.insert(cdg.get(l.front()).first);
378 }
379
380 for (const auto child: cdg.internalDag().children(l.front())) {
381 if (!visited_node.contains(child)) { l.push_back(child); }
382 }
383
384 l.pop_front();
385 }
386
388 for (auto c: class_elim_order) {
389 std::string name = c->name();
390 if (auto pos = name.find_first_of("<"); pos != std::string::npos) {
391 name = name.substr(0, pos);
392 }
393 try {
394 _class_elim_order_->insert(name);
395 } catch (DuplicateElement const&) {}
396 }
397 }
398
399 template < GUM_Numeric GUM_SCALAR >
400 void SVED< GUM_SCALAR >::posterior_(const Chain& chain, Tensor< GUM_SCALAR >& m) {
401 const PRMInstance< GUM_SCALAR >* i = chain.first;
402 const PRMAttribute< GUM_SCALAR >* elt = chain.second;
404 _bb_.compute(i, elt->id());
405 _eliminateNodes_(i, elt->id(), pool, trash);
406
407 std::vector< const Tensor< GUM_SCALAR >* > result;
408 for (auto pot: pool) {
409 if (pot->contains(*(m.variablesSequence().atPos(0)))) result.push_back(pot);
410 }
411
412 while (result.size() > 1) {
413 const auto& p1 = *(result.back());
414 result.pop_back();
415 const auto& p2 = *(result.back());
416 result.pop_back();
417 auto mult = new Tensor< GUM_SCALAR >(p1 * p2);
418 result.push_back(mult);
419 trash.insert(mult);
420 }
421
422 m = *(result.back());
423 m.normalize();
424
425 GUM_ASSERT(m.nbrDim() == (Size)1);
426
427 // cleaning up the mess
428 for (const auto pot: trash)
429 delete pot;
430
431 for (const auto& elt: _lifted_pools_)
432 delete elt.second;
433
434 _lifted_pools_.clear();
435
436 for (const auto& elt: _req_set_) {
437 delete elt.second.first;
438 delete elt.second.second;
439 }
440
441 _req_set_.clear();
442
443 for (const auto& elt: _elim_orders_)
444 delete elt.second;
445
446 _elim_orders_.clear();
447 }
448
449 template < GUM_Numeric GUM_SCALAR >
450 void SVED< GUM_SCALAR >::joint_(const std::vector< Chain >& queries, Tensor< GUM_SCALAR >& j) {
451 GUM_ERROR(FatalError, "Not implemented.")
452 }
453
454 template < GUM_Numeric GUM_SCALAR >
456 Set< NodeId >* attr_set = new Set< NodeId >();
457 Set< NodeId >* sc_set = new Set< NodeId >();
458
459 for (const auto node: _bb_.requisiteNodes(i)) {
460 switch (i->type().get(node).elt_type()) {
463 attr_set->insert(node);
464 break;
465 }
466
468 sc_set->insert(node);
469 break;
470 }
471
472 default : {
474 "There should not be elements other"
475 " than PRMAttribute<GUM_SCALAR> and SlotChain.");
476 }
477 }
478 }
479
480 _req_set_.insert(&(_bb_.requisiteNodes(i)),
481 std::pair< Set< NodeId >*, Set< NodeId >* >(attr_set, sc_set));
482 }
483
484 template < GUM_Numeric GUM_SCALAR >
486 PRMInference< GUM_SCALAR >(prm, model), _class_elim_order_(0), _bb_(*this) {
487 GUM_CONSTRUCTOR(SVED);
488 }
489
490 template < GUM_Numeric GUM_SCALAR >
492 for (const auto& elt: this->evidence(i))
493 pool.insert(const_cast< Tensor< GUM_SCALAR >* >(elt.second));
494 }
495
496 template < GUM_Numeric GUM_SCALAR >
498 return *(_elim_orders_[&c]);
499 }
500
501 template < GUM_Numeric GUM_SCALAR >
502 std::string SVED< GUM_SCALAR >::_trim_(std::string_view s) {
503 if (auto pos = s.find_first_of("<"); pos != std::string::npos) {
504 return std::string{s.substr(0, pos)};
505 }
506 return std::string{s};
507 }
508
509 template < GUM_Numeric GUM_SCALAR >
511 const PRMInstance< GUM_SCALAR >* second) {
512 if (_class_elim_order_ == 0) { _initElimOrder_(); }
513
514 auto first_name = _trim_(first->type().name());
515 auto second_name = _trim_(second->type().name());
516 return (_class_elim_order_->pos(first_name) <= _class_elim_order_->pos(second_name));
517 }
518
519 template < GUM_Numeric GUM_SCALAR >
520 Tensor< GUM_SCALAR >*
522 const PRMAggregate< GUM_SCALAR >* agg) {
523 return &(const_cast< Tensor< GUM_SCALAR >& >(i->get(agg->safeName()).cpf()));
524 }
525
526 template < GUM_Numeric GUM_SCALAR >
528 // Do nothing
529 }
530
531 template < GUM_Numeric GUM_SCALAR >
533 // Do nothing
534 }
535
536 template < GUM_Numeric GUM_SCALAR >
538 auto p = _req_set_.tryGet(&(_bb_.requisiteNodes(i)));
539 if (!p) {
540 _initReqSets_(i);
541 p = _req_set_.tryGet(&(_bb_.requisiteNodes(i)));
542 }
543 return *(p->first);
544 }
545
546 template < GUM_Numeric GUM_SCALAR >
548 auto p = _req_set_.tryGet(&(_bb_.requisiteNodes(i)));
549 if (!p) {
550 _initReqSets_(i);
551 p = _req_set_.tryGet(&(_bb_.requisiteNodes(i)));
552 }
553 return *(p->second);
554 }
555
556 template < GUM_Numeric GUM_SCALAR >
557 void
559 List< const PRMInstance< GUM_SCALAR >* >& elim_list,
560 List< const PRMInstance< GUM_SCALAR >* >& reduced_list,
561 Set< const PRMInstance< GUM_SCALAR >* >& ignore,
562 BucketSet& pool,
563 BucketSet& trash) {
564 while (!elim_list.empty()) {
565 if (_checkElimOrder_(i, elim_list.front())) {
566 if ((!ignore.exists(elim_list.front())) && (_bb_.exists(elim_list.front()))) {
567 _eliminateNodesDownward_(i, elim_list.front(), pool, trash, elim_list, ignore);
568 }
569 } else if (_bb_.exists(elim_list.front())) {
570 reduced_list.insert(elim_list.front());
571 }
572
573 elim_list.popFront();
574 }
575 }
576
577 template < GUM_Numeric GUM_SCALAR >
578 std::string SVED< GUM_SCALAR >::name() const {
579 return "SVED";
580 }
581
582 } /* namespace prm */
583} /* namespace gum */
Headers of SVED (Structured Value Elimination with d-separation).
const NodeSet & parents(NodeId id) const
returns the set of nodes with arc ingoing to a given node
NodeSet children(const NodeSet &ids) const
returns the set of nodes which consists in the node and its parents returns the set of children of a ...
UndiGraph moralGraph() const
The node's id are coherent with the variables and nodes of the topology.
Definition DAGmodel.cpp:81
The default triangulation algorithm used by aGrUM.
Exception : a similar element already exists.
Exception : fatal (unknown ?) error.
Generic doubly linked lists.
Definition list.h:378
Val & front() const
Returns a reference to first element of a list, if any.
Definition list_tpl.h:1694
Val & pushBack(const Val &val)
Inserts a new element (a copy) at the end of the chained list.
Definition list_tpl.h:1481
Val & insert(const Val &val)
Inserts a new element at the end of the chained list (alias of pushBack).
Definition list_tpl.h:1508
bool empty() const noexcept
Returns a boolean indicating whether the chained list is empty.
Definition list_tpl.h:1822
void popFront()
Removes the first element of a List, if any.
Definition list_tpl.h:1816
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
class for graph triangulations for which we enforce a given partial ordering on the nodes elimination...
void insert(const Key &k)
Insert an element at the end of the sequence.
bool exists(const Key &k) const
Check the existence of k in the sequence.
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:994
Representation of a set.
Definition set.h:129
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:468
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:504
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition set_tpl.h:613
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:510
void erase(const Key &k)
Erases an element from the set.
Definition set_tpl.h:553
Size size() const noexcept
Returns the number of elements in the set.
Definition set_tpl.h:607
const std::vector< NodeId > & eliminationOrder() override
returns an elimination ordering compatible with the triangulated graph
This class decorates a gum::prm::Class<GUM_SCALAR> has an IBaseBayesNet.
const NodeProperty< Size > & modalities() const
See gum::IBaseBayesNet::modalities().
This class represent the dependencies of all classes in a PRM<GUM_SCALAR>.
const EltPair & get(NodeId id) const
Returns a constant reference over the element assiociated with the node id in the ClassDependencyGrap...
const DAG & internalDag() const
Returns a constant reference over the graph of the DAG representing the ClassDependencyGraph<GUM_SCAL...
This class decorates an PRMInstance<GUM_SCALAR> as an IBaseBayesNet.
const NodeProperty< Size > & modalities() const
See gum::IBaseBayesNet::cpt().
const DiscreteVariable & variable(NodeId id) const override
See gum::IBaseBayesNet::variable().
PRMAttribute is a member of a Class in a PRM.
virtual const DAG & containerDag() const
Returns the gum::DAG of this PRMClassElementContainer.
static INLINE bool isAggregate(const PRMClassElement< GUM_SCALAR > &elt)
Return true if obj is of type PRMAggregate.
static INLINE bool isAttribute(const PRMClassElement< GUM_SCALAR > &elt)
Returns true if obj_ptr is of type PRMAttribute.
NodeId id() const
Returns the NodeId of this element in it's class DAG.
const std::string & safeName() const
Returns the safe name of this PRMClassElement, if any.
A PRMClass is an object of a PRM representing a fragment of a Bayesian network which can be instantia...
Definition PRMClass.h:77
PRMClassElement< GUM_SCALAR > & get(NodeId id) override
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(NodeId).
bool isOutputNode(const PRMClassElement< GUM_SCALAR > &elt) const override
Returns true if elt is an output node.
PRMInference(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default constructor.
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
PRM< GUM_SCALAR > const * prm_
The PRM<GUM_SCALAR> on which inference is done.
bool hasEvidence(const PRMInstance< GUM_SCALAR > &i) const
Returns true if i has evidence.
An PRMInstance is a Bayesian network fragment defined by a Class and used in a PRMSystem.
Definition PRMInstance.h:79
PRMAttribute< GUM_SCALAR > & get(NodeId id)
Getter on an PRMAttribute<GUM_SCALAR> of this PRMInstance<GUM_SCALAR>.
bool hasRefAttr(NodeId id) const
Returns true if id has at least one referring PRMAttribute<GUM_SCALAR>.
const Bijection< const DiscreteVariable *, const DiscreteVariable * > & bijection() const
Returns a mapping between DiscreteVariable used in this and the ones used in this PRMInstance<GUM_SCA...
PRMClass< GUM_SCALAR > & type()
Returns the type of this instance.
std::vector< std::pair< PRMInstance< GUM_SCALAR > *, std::string > > & getRefAttr(NodeId id)
Returns a vector of pairs of refering attributes of id.
const Set< PRMInstance< GUM_SCALAR > * > & getInstances(NodeId id) const
Returns the Set of PRMInstance<GUM_SCALAR> referenced by id.
A PRMSystem is a container of PRMInstance and describe a relational skeleton.
Definition PRMSystem.h:72
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition PRM.h:74
void _insertEvidence_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool)
Returns true if second can be eliminated before first.
Definition SVED_tpl.h:491
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > * > _elim_orders_
Definition SVED.h:127
Set< Tensor< GUM_SCALAR > * > BucketSet
Code alias.
Definition SVED.h:123
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:194
void evidenceRemoved_(const Chain &chain) override
See PRMInference::evidenceRemoved_().
Definition SVED_tpl.h:532
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:558
void _initLiftedNodes_(const PRMInstance< GUM_SCALAR > *i, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition SVED_tpl.h:300
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:134
Set< NodeId > & _getSCSet_(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition SVED_tpl.h:547
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:510
Sequence< std::string > * _class_elim_order_
Definition SVED.h:136
void _eliminateNodesWithEvidence_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition SVED_tpl.h:257
Set< NodeId > & _getAttrSet_(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition SVED_tpl.h:537
void _initElimOrder_()
Returns true if second can be eliminated before first.
Definition SVED_tpl.h:362
std::string name() const override
Returns the name of the current inference algorithm.
Definition SVED_tpl.h:578
~SVED() override
Destructor.
Definition SVED_tpl.h:56
std::vector< NodeId > & _getElimOrder_(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition SVED_tpl.h:497
HashTable< const Set< NodeId > *, std::pair< Set< NodeId > *, Set< NodeId > * > > _req_set_
First pair -> requisite Attributes Second pair -> requisite SlotChains.
Definition SVED.h:142
Tensor< GUM_SCALAR > * _getAggTensor_(const PRMInstance< GUM_SCALAR > *i, const PRMAggregate< GUM_SCALAR > *agg)
Returns true if second can be eliminated before first.
Definition SVED_tpl.h:521
void joint_(const std::vector< Chain > &queries, Tensor< GUM_SCALAR > &j) override
See PRMInference::joint_().
Definition SVED_tpl.h:450
void _insertLiftedNodes_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition SVED_tpl.h:280
void _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:66
void evidenceAdded_(const Chain &chain) override
See PRMInference::evidenceAdded_().
Definition SVED_tpl.h:527
typename PRMInference< GUM_SCALAR >::Chain Chain
Code alias.
Definition SVED.h:105
SVED(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &model)
Default Constructor.
Definition SVED_tpl.h:485
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:130
void posterior_(const Chain &chain, Tensor< GUM_SCALAR > &m) override
See PRMInference::posterior_().
Definition SVED_tpl.h:400
std::string _trim_(std::string_view s)
Returns true if second can be eliminated before first.
Definition SVED_tpl.h:502
StructuredBayesBall< GUM_SCALAR > _bb_
Definition SVED.h:138
void _initReqSets_(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition SVED_tpl.h:455
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size NodeId
Type for node ids.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
namespace for all probabilistic relational models entities
Definition agrum.h:68
void eliminateNode(const DiscreteVariable *var, Set< Tensor< GUM_SCALAR > * > &pool, Set< Tensor< GUM_SCALAR > * > &trash)
Proceeds with the elimination of var in pool.
void eliminateNodes(const std::vector< const DiscreteVariable * > &elim_order, Set< Tensor< GUM_SCALAR > * > &pool, Set< Tensor< GUM_SCALAR > * > &trash)
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.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46