aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
SVE_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-2025 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-2025 *
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#pragma once
41
42
51
52namespace gum {
53 namespace prm {
54
55 template < typename GUM_SCALAR >
58 std::stringstream s;
59 const auto& class_a = i.type().get(a.safeName());
60 s << &(a.type().variable()) << " - ";
61 s << i.name() << "." << a.safeName() << ": input=" << i.type().isInputNode(class_a);
62 s << " output=" << i.type().isOutputNode(class_a)
63 << " inner=" << i.type().isInnerNode(class_a);
64 return s.str();
65 }
66
67 template < typename GUM_SCALAR >
69 std::stringstream s;
70 s << i.name() << std::endl;
71 s << "Attributes: " << std::endl;
72 for (auto a: i) {
73 s << __print_attribute__(i, *(a.second));
74 }
75 if (i.type().slotChains().size()) {
76 s << std::endl << "SlotChains: " << std::endl;
77 for (auto sc: i.type().slotChains()) {
78 s << sc->name() << " ";
79 }
80 }
81 return s.str();
82 }
83
84 template < typename GUM_SCALAR >
86 std::stringstream str;
87 for (auto i: s) {
88 str << __print_instance__(*(i.second)) << std::endl;
89 }
90 return str.str();
91 }
92
93 template < typename LIST >
94 std::string __print_list__(LIST l) {
95 std::stringstream s;
96 s << "[";
97 for (auto i: l) {
98 s << i->name() << " ";
99 }
100 s << "]";
101 return s.str();
102 }
103
104 template < typename GUM_SCALAR >
105 std::string __print_pot__(const Tensor< GUM_SCALAR >& pot) {
106 std::stringstream s;
107 s << "{";
108 for (auto var: pot.variablesSequence()) {
109 s << var << ", ";
110 }
111 s << "}";
112 return s.str();
113 }
114
115 template < typename SET >
116 std::string __print_set__(SET set) {
117 std::stringstream s;
118 s << "[";
119 for (auto p: set) {
120 s << __print_pot__(*p) << " ";
121 }
122 s << "]";
123 return s.str();
124 }
125
126 template < typename GUM_SCALAR >
128 GUM_DESTRUCTOR(SVE);
129
130 for (const auto& elt: _elim_orders_)
131 delete elt.second;
132
133 for (const auto& elt: _lifted_pools_)
134 delete elt.second;
135
136 if (_class_elim_order_ != nullptr) delete _class_elim_order_;
137
138 for (const auto trash: _lifted_trash_)
139 delete trash;
140
141 for (auto set: _delayedVariables_)
142 delete set.second;
143 }
144
145 template < typename GUM_SCALAR >
147 NodeId node,
148 BucketSet& pool,
149 BucketSet& trash) {
150 Set< const PRMInstance< GUM_SCALAR >* > ignore, eliminated;
151 Set< NodeId > delayedVars;
152 // Downward elimination
154 ignore.insert(query);
155
156 for (auto iter = query->beginInvRef(); iter != query->endInvRef(); ++iter) {
157 for (auto child = (*(iter.val())).begin(); child != (*(iter.val())).end(); ++child) {
158 if (!ignore.exists(child->first)) {
160 child->first,
161 pool,
162 trash,
163 elim_list,
164 ignore,
165 eliminated);
166 } else if (!eliminated.exists(child->first)) {
167 _addDelayedVariable_(child->first, query, iter.key());
168 delayedVars.insert(iter.key());
169 }
170 }
171 }
172
173 // Eliminating all nodes in query instance, except query
175 const auto moralg = bn.moralGraph();
176 DefaultTriangulation t(&moralg, &(bn.modalities()));
177 std::vector< const DiscreteVariable* > elim_order;
178
179 if (this->hasEvidence(query)) { _insertEvidence_(query, pool); }
180
181 for (auto attr = query->begin(); attr != query->end(); ++attr) {
182 pool.insert(&(const_cast< Tensor< GUM_SCALAR >& >((*(attr.val())).cpf())));
183 }
184
185 for (size_t idx = 0; idx < t.eliminationOrder().size(); ++idx) {
186 if ((t.eliminationOrder()[idx] != node)
187 && (!delayedVars.exists(t.eliminationOrder()[idx]))) {
188 auto var_id = t.eliminationOrder()[idx];
189 const auto& var = bn.variable(var_id);
190 elim_order.push_back(&var);
191 }
192 }
193
194 eliminateNodes(elim_order, pool, trash);
195
196 // Eliminating delayed variables, if any
197 if (_delayedVariables_.exists(query)) { _eliminateDelayedVariables_(query, pool, trash); }
198
199 eliminated.insert(query);
200 // Eliminating instance in elim_list
202
203 while (!elim_list.empty()) {
204 if (_checkElimOrder_(query, elim_list.front())) {
205 if (!ignore.exists(elim_list.front())) {
207 elim_list.front(),
208 pool,
209 trash,
210 elim_list,
211 ignore,
212 eliminated);
213 }
214 } else {
215 tmp_list.insert(elim_list.front());
216 }
217
218 elim_list.popFront();
219 }
220
221 // Upward elimination
222 for (const auto chain: query->type().slotChains())
223 for (const auto parent: query->getInstances(chain->id()))
224 if (!ignore.exists(parent))
225 _eliminateNodesUpward_(parent, pool, trash, tmp_list, ignore, eliminated);
226 }
227
228 template < typename GUM_SCALAR >
230 BucketSet& pool,
231 BucketSet& trash) {
232 Set< Tensor< GUM_SCALAR >* > toRemove;
233
234 for (const auto var: *_delayedVariables_[i]) {
236
237 for (const auto pot: pool)
238 if (pot->contains(*var)) {
239 bucket->add(*pot);
240 toRemove.insert(pot);
241 }
242
243 for (const auto pot: toRemove)
244 pool.erase(pot);
245
246 for (const auto other: bucket->allVariables())
247 if (other != var) bucket->add(*other);
248
249 Tensor< GUM_SCALAR >* bucket_pot = new Tensor< GUM_SCALAR >(bucket);
250 trash.insert(bucket_pot);
251 pool.insert(bucket_pot);
252 }
253 }
254
255 template < typename GUM_SCALAR >
257 const PRMInstance< GUM_SCALAR >* from,
259 BucketSet& pool,
260 BucketSet& trash,
261 List< const PRMInstance< GUM_SCALAR >* >& elim_list,
262 Set< const PRMInstance< GUM_SCALAR >* >& ignore,
263 Set< const PRMInstance< GUM_SCALAR >* >& eliminated) {
264 Set< NodeId > delayedVars;
265 ignore.insert(i);
266 // Calling elimination over child instance
268
269 for (auto iter = i->beginInvRef(); iter != i->endInvRef(); ++iter) {
270 for (auto child = (*(iter.val())).begin(); child != (*(iter.val())).end(); ++child) {
271 if (!ignore.exists(child->first)) {
272 _eliminateNodesDownward_(i, child->first, pool, trash, my_list, ignore, eliminated);
273 } else if (!eliminated.exists(child->first)) {
274 _addDelayedVariable_(child->first, i, iter.key());
275 delayedVars.insert(iter.key());
276 }
277 }
278 }
279
280 // Eliminating all nodes in current instance
281 _variableElimination_(i, pool, trash, (delayedVars.empty() ? 0 : &delayedVars));
282 eliminated.insert(i);
283
284 // Calling elimination over child's parents
285 for (const auto node: my_list) {
286 if (_checkElimOrder_(i, node) && (node != from)) {
287 if (!ignore.exists(node)) {
288 _eliminateNodesDownward_(i, node, pool, trash, elim_list, ignore, eliminated);
289 }
290 } else if (node != from) {
291 elim_list.insert(node);
292 }
293 }
294
295 // Adding parents instance to elim_list
296 for (const auto chain: i->type().slotChains()) {
297 for (const auto inst: i->getInstances(chain->id())) {
298 if (inst != from) { elim_list.insert(inst); }
299 }
300 }
301 }
302
303 template < typename GUM_SCALAR >
305 BucketSet& pool,
306 BucketSet& trash,
307 Set< NodeId >* delayedVars) {
308 if (this->hasEvidence(i)) {
309 _eliminateNodesWithEvidence_(i, pool, trash, delayedVars);
310 } else {
311 _insertLiftedNodes_(i, pool, trash);
312
313 for (const auto agg: i->type().aggregates())
314 pool.insert(_getAggTensor_(i, agg));
315
316 try {
318
319 std::vector< const DiscreteVariable* > elim;
320
321 for (const auto node: _getElimOrder_(i->type())) {
322 const auto& var = bn.variable(node);
323 if (delayedVars != nullptr) {
324 if (!delayedVars->exists(node)) {
325 const auto& var = bn.variable(node);
326 elim.push_back(&var);
327 }
328 } else {
329 elim.push_back(&var);
330 }
331 }
332
333 eliminateNodes(elim, pool, trash);
334 } catch (NotFound const&) {
335 // Raised if there is no inner nodes to eliminate
336 }
337 }
338
339 // Eliminating delayed variables, if any
340 if (_delayedVariables_.exists(i)) { _eliminateDelayedVariables_(i, pool, trash); }
341 }
342
343 template < typename GUM_SCALAR >
346 BucketSet& pool,
347 BucketSet& trash,
348 List< const PRMInstance< GUM_SCALAR >* >& elim_list,
349 Set< const PRMInstance< GUM_SCALAR >* >& ignore,
350 Set< const PRMInstance< GUM_SCALAR >* >& eliminated) {
351 // Downward elimination
352 ignore.insert(i);
353
354 for (auto iter = i->beginInvRef(); iter != i->endInvRef(); ++iter) {
355 for (auto child = (*(iter.val())).begin(); child != (*(iter.val())).end(); ++child) {
356 if (!ignore.exists(child->first)) {
357 _eliminateNodesDownward_(i, child->first, pool, trash, elim_list, ignore, eliminated);
358 }
359 }
360 }
361
362 // Eliminating all nodes in i instance
363 _variableElimination_(i, pool, trash);
364 eliminated.insert(i);
365 // Eliminating instance in elim_list
367
368 while (!elim_list.empty()) {
369 if (_checkElimOrder_(i, elim_list.front())) {
370 if (!ignore.exists(elim_list.front())) {
372 elim_list.front(),
373 pool,
374 trash,
375 elim_list,
376 ignore,
377 eliminated);
378 }
379 } else {
380 tmp_list.insert(elim_list.front());
381 }
382
383 elim_list.popFront();
384 }
385
386 // Upward elimination
387 for (const auto chain: i->type().slotChains()) {
388 for (const auto parent: i->getInstances(chain->id())) {
389 if (!ignore.exists(parent)) {
390 _eliminateNodesUpward_(parent, pool, trash, tmp_list, ignore, eliminated);
391 }
392 }
393 }
394 }
395
396 template < typename GUM_SCALAR >
398 BucketSet& pool,
399 BucketSet& trash,
400 Set< NodeId >* delayedVars) {
401 // First we check if evidences are on inner nodes
402 bool inner = false;
403
404 for (const auto& elt: this->evidence(i)) {
405 inner
406 = i->type().isInputNode(i->get(elt.first)) || i->type().isInnerNode(i->get(elt.first));
407
408 if (inner) { break; }
409 }
410
411 // Evidence on inner nodes
412 if (inner) {
413 BucketSet tmp_pool;
414 _insertEvidence_(i, tmp_pool);
415
416 // We need a local to not eliminate queried inner nodes of the same
417 // class
418 for (const auto& elt: *i) {
419 tmp_pool.insert(&(const_cast< Tensor< GUM_SCALAR >& >(elt.second->cpf())));
420 }
421
423 const auto moralg = bn.moralGraph();
424 DefaultTriangulation t(&moralg, &(bn.modalities()));
425 const std::vector< NodeId >& full_elim_order = t.eliminationOrder();
426 // Removing Output nodes of elimination order
427 std::vector< const DiscreteVariable* > inner_elim_order;
428 std::vector< const DiscreteVariable* > output_elim_order;
429
430 for (size_t idx = 0; idx < full_elim_order.size(); ++idx) {
431 auto var_id = full_elim_order[idx];
432 const auto& var = bn.variable(var_id);
433
434 if (!i->type().isOutputNode(i->get(full_elim_order[idx]))) {
435 inner_elim_order.push_back(&var);
436 } else if (delayedVars != nullptr) {
437 if (!delayedVars->exists(full_elim_order[idx])) { output_elim_order.push_back(&var); }
438 } else {
439 output_elim_order.push_back(&var);
440 }
441 }
442
443 eliminateNodes(inner_elim_order, tmp_pool, trash);
444
445 // Now we add the new tensors in pool and eliminate output nodes
446 for (const auto pot: tmp_pool)
447 pool.insert(pot);
448
449 if (!output_elim_order.empty()) eliminateNodes(output_elim_order, pool, trash);
450
451 } else {
453 _insertEvidence_(i, pool);
454 _insertLiftedNodes_(i, pool, trash);
455
456 for (const auto agg: i->type().aggregates())
457 pool.insert(_getAggTensor_(i, agg));
458
459 try {
460 std::vector< const DiscreteVariable* > elim;
461
462 for (auto iter = _getElimOrder_(i->type()).begin();
463 iter != _getElimOrder_(i->type()).end();
464 ++iter) {
465 const auto& var = bn.variable(*iter);
466 if (delayedVars != nullptr) {
467 if (!delayedVars->exists(*iter)) { elim.push_back(&var); }
468 } else {
469 elim.push_back(&var);
470 }
471 }
472
473 eliminateNodes(elim, pool, trash);
474 } catch (NotFound const&) {
475 GUM_ERROR(FatalError, "there should be at least one node here.")
476 }
477 }
478 }
479
480 template < typename GUM_SCALAR >
482 BucketSet& pool,
483 BucketSet& trash) {
484 SVE< GUM_SCALAR >::BucketSet* lifted_pool = 0;
485
486 try {
487 lifted_pool = _lifted_pools_[&(i->type())];
488 } catch (NotFound const&) {
490 lifted_pool = _lifted_pools_[&(i->type())];
491 }
492
493 for (const auto lifted_pot: *lifted_pool) {
494 Tensor< GUM_SCALAR >* pot = copyTensor(i->bijection(), *lifted_pot);
495 pool.insert(pot);
496 trash.insert(pot);
497 }
498 }
499
500 template < typename GUM_SCALAR >
502 BucketSet* lifted_pool = new BucketSet();
503 _lifted_pools_.insert(&c, lifted_pool);
504 NodeSet inners, outers;
505
506 for (const auto node: c.containerDag().nodes())
508 if (c.isOutputNode(c.get(node))) outers.insert(node);
509 else if (!outers.exists(node)) inners.insert(node);
510
511 lifted_pool->insert(const_cast< Tensor< GUM_SCALAR >* >(&(c.get(node).cpf())));
513 outers.insert(node);
514
515 // We need to put in the output_elim_order aggregator's parents which
516 // are
517 // innner nodes
518 for (const auto par: c.containerDag().parents(node))
520 && c.isInnerNode(c.get(par))) {
521 inners.erase(par);
522 outers.insert(par);
523 }
524 }
525
526 // Now we proceed with the elimination of inner attributes
528 List< NodeSet > partial_ordering;
529
530 if (inners.size()) partial_ordering.push_back(inners);
531
532 if (outers.size()) partial_ordering.push_back(outers);
533
534 const auto moralg = bn.moralGraph();
535 PartialOrderedTriangulation t(&moralg, &(bn.modalities()), &partial_ordering);
536
537 for (size_t idx = 0; idx < inners.size(); ++idx)
538 eliminateNode(&(c.get(t.eliminationOrder()[idx]).type().variable()),
539 *lifted_pool,
541
542 // If there is not only inner and input Attributes
543 if (outers.size()) {
544 _elim_orders_.insert(&c,
545 new std::vector< NodeId >(t.eliminationOrder().begin() + inners.size(),
546 t.eliminationOrder().end()));
547 }
548 }
549
550 template < typename GUM_SCALAR >
554 std::list< NodeId > l;
555
556 for (const auto node: cdg.dag().nodes()) {
557 if (cdg.dag().parents(node).empty()) { l.push_back(node); }
558 }
559
560 Set< NodeId > visited_node;
561
562 while (!l.empty()) {
563 visited_node.insert(l.front());
564
565 if (!class_elim_order.exists(cdg.get(l.front()).first)) {
566 class_elim_order.insert(cdg.get(l.front()).first);
567 }
568
569 for (const auto child: cdg.dag().children(l.front())) {
570 if (!visited_node.contains(child)) { l.push_back(child); }
571 }
572
573 l.pop_front();
574 }
575
577 for (auto c: class_elim_order) {
578 std::string name = c->name();
579 auto pos = name.find_first_of("<");
580 if (pos != std::string::npos) { name = name.substr(0, pos); }
581 try {
582 _class_elim_order_->insert(name);
583 } catch (DuplicateElement const&) {}
584 }
585 }
586
587 template < typename GUM_SCALAR >
588 void SVE< GUM_SCALAR >::posterior_(const Chain& chain, Tensor< GUM_SCALAR >& m) {
589 const PRMInstance< GUM_SCALAR >* i = chain.first;
590 const PRMAttribute< GUM_SCALAR >* elt = chain.second;
592
593 _eliminateNodes_(i, elt->id(), pool, trash);
594
595 std::vector< Tensor< GUM_SCALAR >* > result;
596
597 for (const auto pot: pool) {
598 if (pot->contains(elt->type().variable())) { result.push_back(pot); }
599 }
600
601 while (result.size() > 1) {
602 auto& p1 = *(result.back());
603 result.pop_back();
604 auto& p2 = *(result.back());
605 result.pop_back();
606 auto mult = new Tensor< GUM_SCALAR >(p1 * p2);
607 trash.insert(mult);
608 result.push_back(mult);
609 }
610
611 m = *(result.back());
612 m.normalize();
613
614 for (const auto pot: trash) {
615 delete pot;
616 }
617 }
618
619 template < typename GUM_SCALAR >
620 void SVE< GUM_SCALAR >::joint_(const std::vector< Chain >& queries, Tensor< GUM_SCALAR >& j) {
621 GUM_ERROR(FatalError, "Not implemented.")
622 }
623
624 template < typename GUM_SCALAR >
626 const PRMSystem< GUM_SCALAR >& system) :
627 PRMInference< GUM_SCALAR >(prm, system), _class_elim_order_(0) {
628 GUM_CONSTRUCTOR(SVE);
629 }
630
631 template < typename GUM_SCALAR >
633 BucketSet& pool) {
634 for (const auto& elt: this->evidence(i))
635 pool.insert(const_cast< Tensor< GUM_SCALAR >* >(elt.second));
636 }
637
638 template < typename GUM_SCALAR >
639 INLINE std::vector< NodeId >&
643
644 template < typename GUM_SCALAR >
645 INLINE std::string SVE< GUM_SCALAR >::_trim_(const std::string& s) {
646 auto pos = s.find_first_of("<");
647 if (pos != std::string::npos) { return s.substr(0, pos); }
648 return s;
649 }
650
651 template < typename GUM_SCALAR >
653 const PRMInstance< GUM_SCALAR >* second) {
654 if (_class_elim_order_ == 0) { _initElimOrder_(); }
655
656 auto first_name = _trim_(first->type().name());
657 auto second_name = _trim_(second->type().name());
658 return (_class_elim_order_->pos(first_name) <= _class_elim_order_->pos(second_name));
659 }
660
661 template < typename GUM_SCALAR >
662 INLINE Tensor< GUM_SCALAR >*
664 const PRMAggregate< GUM_SCALAR >* agg) {
665 return &(const_cast< Tensor< GUM_SCALAR >& >(i->get(agg->id()).cpf()));
666 }
667
668 template < typename GUM_SCALAR >
669 INLINE void SVE< GUM_SCALAR >::evidenceAdded_(const Chain& chain) {
670 // Do nothing
671 }
672
673 template < typename GUM_SCALAR >
674 INLINE void SVE< GUM_SCALAR >::evidenceRemoved_(const Chain& chain) {
675 // Do nothing
676 }
677
678 template < typename GUM_SCALAR >
681 NodeId id) {
682 try {
683 _delayedVariables_[i]->insert(&(j->get(id).type().variable()));
684 } catch (NotFound const&) {
685 _delayedVariables_.insert(i, new gum::VariableSet());
686 _delayedVariables_[i]->insert(&(j->get(id).type().variable()));
687 } catch (DuplicateElement const&) {
688 // happends if j->get(id) is parent of more than one variable in i
689 }
690
691 static std::string dot = ".";
692
693 try {
694 _delayedVariablesCounters_[j->name() + dot + j->get(id).safeName()] += 1;
695 } catch (NotFound const&) {
696 _delayedVariablesCounters_.insert(j->name() + dot + j->get(id).safeName(), 1);
697 }
698 }
699
700 template < typename GUM_SCALAR >
701 INLINE std::string SVE< GUM_SCALAR >::name() const {
702 return "SVE";
703 }
704
705 } /* namespace prm */
706} /* namespace gum */
Headers of SVE (Structured Variable Elimination).
Headers of ClassDependencyGraph<GUM_SCALAR>.
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 children of a set of nodes
UndiGraph moralGraph() const
The node's id are coherent with the variables and nodes of the topology.
Definition DAGmodel.cpp:64
The default triangulation algorithm used by aGrUM.
Exception : a similar element already exists.
Exception : fatal (unknown ?) error.
Generic doubly linked lists.
Definition list.h:379
Val & front() const
Returns a reference to first element of a list, if any.
Definition list_tpl.h:1703
bool empty() const noexcept
Returns a boolean indicating whether the chained list is empty.
Definition list_tpl.h:1831
Val & push_back(Args &&... args)
An alias for pushBack used for STL compliance.
void popFront()
Removes the first element of a List, if any.
Definition list_tpl.h:1825
Val & insert(const Val &val)
Inserts a new element at the end of the chained list (alias of pushBack).
Definition list_tpl.h:1515
A multidim implementation for buckets.
const gum::VariableSet & allVariables() const
Returns the sequence of all the variables contained in the bucket.
void add(const MultiDimContainer< GUM_SCALAR > &impl)
Add a MultiDimContainer in the bucket.
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
Exception : the element we looked for cannot be found.
class for graph triangulations for which we enforce a given partial ordering on the nodes elimination...
bool exists(const Key &k) const
Check the existence of k in the sequence.
void insert(const Key &k)
Insert an element at the end of the sequence.
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:972
Representation of a set.
Definition set.h:131
Size size() const noexcept
Returns the number of elements in the set.
Definition set_tpl.h:636
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:533
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:497
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:539
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition set_tpl.h:642
void erase(const Key &k)
Erases an element from the set.
Definition set_tpl.h:582
const std::vector< NodeId > & eliminationOrder()
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 & dag() 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.
virtual const DiscreteVariable & variable(NodeId id) const
See gum::IBaseBayesNet::variable().
const NodeProperty< Size > & modalities() const
See gum::IBaseBayesNet::cpt().
PRMAttribute is a member of a Class in a PRM.
virtual PRMType & type()=0
See gum::PRMClassElement::type().
virtual bool isInnerNode(const PRMClassElement< GUM_SCALAR > &elt) const
Returns true if the node is an inner node.
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.
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.
static INLINE bool isAttribute(const PRMClassElement< GUM_SCALAR > &elt)
Returns true if obj_ptr is of type PRMAttribute.
A PRMClass is an object of a PRM representing a fragment of a Bayesian network which can be instantia...
Definition PRMClass.h:75
PRMClassElement< GUM_SCALAR > & get(NodeId id)
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(NodeId).
virtual bool isOutputNode(const PRMClassElement< GUM_SCALAR > &elt) const
Returns true if elt is an output node.
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
PRMInference(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default constructor.
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
const iterator & end()
Returns a reference over the iterator at the end of the list of gum::prm::PRMAttribute<GUM_SCALAR> in...
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...
InvRefIterator beginInvRef()
Alias to iterate over the gum::prm::PRMAttribute<GUM_SCALAR> in this PRMInstance<GUM_SCALAR>.
const InvRefIterator & endInvRef()
Alias to iterate over the gum::prm::PRMAttribute<GUM_SCALAR> in this PRMInstance<GUM_SCALAR>.
PRMClass< GUM_SCALAR > & type()
Returns the type of this instance.
const Set< PRMInstance< GUM_SCALAR > * > & getInstances(NodeId id) const
Returns the Set of PRMInstance<GUM_SCALAR> referenced by id.
PRMAttribute< GUM_SCALAR > & get(NodeId id)
Getter on an PRMAttribute<GUM_SCALAR> of this PRMInstance<GUM_SCALAR>.
iterator begin()
Returns an iterator at the begining of the list of gum::prm::PRMAttribute<GUM_SCALAR> in this PRMInst...
const std::string & name() const
Returns the name of this object.
A PRMSystem is a container of PRMInstance and describe a relational skeleton.
Definition PRMSystem.h:70
DiscreteVariable & variable()
Return a reference on the DiscreteVariable contained in this.
Definition PRMType_inl.h:64
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition PRM.h:74
Sequence< std::string > * _class_elim_order_
Definition SVE.h:129
Set< Tensor< GUM_SCALAR > * > BucketSet
Code alias.
Definition SVE.h:121
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:397
virtual void evidenceAdded_(const Chain &chain)
See PRMInference<GUM_SCALAR>::evidenceAdded_().
Definition SVE_tpl.h:669
void _initLiftedNodes_(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition SVE_tpl.h:501
void _insertLiftedNodes_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition SVE_tpl.h:481
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:304
BucketSet _lifted_trash_
Definition SVE.h:140
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > * > _elim_orders_
Definition SVE.h:125
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:146
~SVE()
Destructor.
Definition SVE_tpl.h:127
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:679
std::string _trim_(const std::string &s)
Returns true if second can be eliminated before first.
Definition SVE_tpl.h:645
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:344
virtual void joint_(const std::vector< Chain > &queries, Tensor< GUM_SCALAR > &j)
See PRMInference<GUM_SCALAR>::joint_().
Definition SVE_tpl.h:620
void _initElimOrder_()
Returns true if second can be eliminated before first.
Definition SVE_tpl.h:551
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:138
void _insertEvidence_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool)
Returns true if second can be eliminated before first.
Definition SVE_tpl.h:632
typename PRMInference< GUM_SCALAR >::Chain Chain
Code alias.
Definition SVE.h:103
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:256
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:652
SVE(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default Constructor.
Definition SVE_tpl.h:625
HashTable< const PRMClass< GUM_SCALAR > *, BucketSet * > _lifted_pools_
Definition SVE.h:127
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:663
virtual void posterior_(const Chain &chain, Tensor< GUM_SCALAR > &m)
See PRMInference<GUM_SCALAR>::posterior_().
Definition SVE_tpl.h:588
HashTable< const PRMInstance< GUM_SCALAR > *, gum::VariableSet * > _delayedVariables_
Definition SVE.h:131
void _eliminateDelayedVariables_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition SVE_tpl.h:229
virtual void evidenceRemoved_(const Chain &chain)
See PRMInference<GUM_SCALAR>::evidenceRemoved_().
Definition SVE_tpl.h:674
std::vector< NodeId > & _getElimOrder_(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition SVE_tpl.h:640
virtual std::string name() const
Returns the name of the current inference algorithm.
Definition SVE_tpl.h:701
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
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
std::string __print_attribute__(const PRMInstance< GUM_SCALAR > &i, const PRMAttribute< GUM_SCALAR > &a)
Definition SVE_tpl.h:56
std::string __print_pot__(const Tensor< GUM_SCALAR > &pot)
Definition SVE_tpl.h:105
void eliminateNode(const DiscreteVariable *var, Set< Tensor< GUM_SCALAR > * > &pool, Set< Tensor< GUM_SCALAR > * > &trash)
Proceeds with the elimination of var in pool.
std::string __print_instance__(const PRMInstance< GUM_SCALAR > &i)
Definition SVE_tpl.h:68
std::string __print_set__(SET set)
Definition SVE_tpl.h:116
std::string __print_list__(LIST l)
Definition SVE_tpl.h:94
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.
std::string __print_system__(const PRMSystem< GUM_SCALAR > &s)
Definition SVE_tpl.h:85
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Set< const DiscreteVariable * > VariableSet