aGrUM 3.0.0
a C++ library for (probabilistic) graphical models
doAST_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
45
46#pragma once
47
48#include <algorithm>
49
50#include <agrum/BN/IBayesNet.h>
53
54namespace gum {
55
56 // ================================================================
57 // ASTtree
58 // ================================================================
59 template < GUM_Numeric GUM_SCALAR >
61
62 template < GUM_Numeric GUM_SCALAR >
64 GUM_DESTRUCTOR(ASTtree)
65 }
66
67 template < GUM_Numeric GUM_SCALAR >
68 const std::string& ASTtree< GUM_SCALAR >::type() const noexcept {
69 return _type;
70 }
71
72 template < GUM_Numeric GUM_SCALAR >
74 // We do not rely on nameOccur mutation at this stage; pass through.
75 return fastToLatex(nameOccur);
76 }
77
78 template < GUM_Numeric GUM_SCALAR >
79 std::string ASTtree< GUM_SCALAR >::_latexCorrect(std::string_view srcName,
81 int count = nameOccur.getWithDefault(std::string{srcName}, 0);
82 const int nbr = (count > 1) ? (count - 1) : 0;
83 // append nbr primes
84 return std::string{srcName} + std::string(static_cast< size_t >(nbr), '\'');
85 }
86
87 template < GUM_Numeric GUM_SCALAR >
88 std::vector< std::string >
91 // Transform each name using the single-name overload, then sort.
92 std::vector< std::string > out;
93 for (const auto& n: srcNames) {
94 out.push_back(_latexCorrect(n, nameOccur));
95 }
96 std::sort(out.begin(), out.end());
97 return out;
98 }
99
100 // ================================================================
101 // ASTBinaryOp
102 // ================================================================
103 template < GUM_Numeric GUM_SCALAR >
105 std::unique_ptr< ASTtree< GUM_SCALAR > > op1,
106 std::unique_ptr< ASTtree< GUM_SCALAR > > op2) :
107 ASTtree< GUM_SCALAR >(type), _op1(std::move(op1)), _op2(std::move(op2)) {}
108
109 template < GUM_Numeric GUM_SCALAR >
110 const ASTtree< GUM_SCALAR >& ASTBinaryOp< GUM_SCALAR >::op1() const {
111 return *_op1;
112 }
113
114 template < GUM_Numeric GUM_SCALAR >
115 const ASTtree< GUM_SCALAR >& ASTBinaryOp< GUM_SCALAR >::op2() const {
116 return *_op2;
117 }
118
119 template < GUM_Numeric GUM_SCALAR >
120 std::string ASTBinaryOp< GUM_SCALAR >::toString(std::string_view prefix) const {
121 std::string cont = std::string{prefix} + ASTtree< GUM_SCALAR >::CONTINUE_PREFIX;
122 return std::format("{}{}\n{}\n{}",
123 prefix,
124 this->_type,
125 _op1->toString(cont),
126 _op2->toString(cont));
127 }
128
129 // ================================================================
130 // ASTplus (ASTBinaryOp)
131 // ================================================================
132 template < GUM_Numeric GUM_SCALAR >
133 ASTplus< GUM_SCALAR >::ASTplus(std::unique_ptr< ASTtree< GUM_SCALAR > > op1,
134 std::unique_ptr< ASTtree< GUM_SCALAR > > op2) :
135 ASTBinaryOp< GUM_SCALAR >("+", std::move(op1), std::move(op2)) {}
136
137 template < GUM_Numeric GUM_SCALAR >
138 std::string
140 return "\\left(" + fastToLatex(nameOccur) + "\\right)";
141 }
142
143 template < GUM_Numeric GUM_SCALAR >
145 return this->_op1->fastToLatex(nameOccur) + "+" + this->_op2->fastToLatex(nameOccur);
146 }
147
148 template < GUM_Numeric GUM_SCALAR >
149 std::unique_ptr< ASTtree< GUM_SCALAR > > ASTplus< GUM_SCALAR >::copy() const {
150 auto l = this->_op1->copy();
151 auto r = this->_op2->copy();
152 return std::make_unique< ASTplus< GUM_SCALAR > >(std::move(l), std::move(r));
153 }
154
155 template < GUM_Numeric GUM_SCALAR >
156 Tensor< GUM_SCALAR > ASTplus< GUM_SCALAR >::eval(const IBayesNet< GUM_SCALAR >& bn) const {
157 return this->_op1->eval(bn) + this->_op2->eval(bn);
158 }
159
160 // ================================================================
161 // ASTminus (ASTBinaryOp)
162 // ================================================================
163 template < GUM_Numeric GUM_SCALAR >
164 ASTminus< GUM_SCALAR >::ASTminus(std::unique_ptr< ASTtree< GUM_SCALAR > > op1,
165 std::unique_ptr< ASTtree< GUM_SCALAR > > op2) :
166 ASTBinaryOp< GUM_SCALAR >("-", std::move(op1), std::move(op2)) {}
167
168 template < GUM_Numeric GUM_SCALAR >
169 std::string
171 return "\\left(" + fastToLatex(nameOccur) + "\\right)";
172 }
173
174 template < GUM_Numeric GUM_SCALAR >
176 return this->_op1->fastToLatex(nameOccur) + "-" + this->_op2->fastToLatex(nameOccur);
177 }
178
179 template < GUM_Numeric GUM_SCALAR >
180 std::unique_ptr< ASTtree< GUM_SCALAR > > ASTminus< GUM_SCALAR >::copy() const {
181 auto l = this->_op1->copy();
182 auto r = this->_op2->copy();
183 return std::make_unique< ASTminus< GUM_SCALAR > >(std::move(l), std::move(r));
184 }
185
186 template < GUM_Numeric GUM_SCALAR >
187 Tensor< GUM_SCALAR > ASTminus< GUM_SCALAR >::eval(const IBayesNet< GUM_SCALAR >& bn) const {
188 return this->_op1->eval(bn) - this->_op2->eval(bn);
189 }
190
191 // ================================================================
192 // ASTmult (ASTBinaryOp)
193 // ================================================================
194 template < GUM_Numeric GUM_SCALAR >
195 ASTmult< GUM_SCALAR >::ASTmult(std::unique_ptr< ASTtree< GUM_SCALAR > > op1,
196 std::unique_ptr< ASTtree< GUM_SCALAR > > op2) :
197 ASTBinaryOp< GUM_SCALAR >("*", std::move(op1), std::move(op2)) {}
198
199 template < GUM_Numeric GUM_SCALAR >
200 std::string
202 return this->_op1->protectToLatex(nameOccur) + " \\cdot "
203 + this->_op2->protectToLatex(nameOccur);
204 }
205
206 template < GUM_Numeric GUM_SCALAR >
208 return this->_op1->fastToLatex(nameOccur) + " \\cdot " + this->_op2->fastToLatex(nameOccur);
209 }
210
211 template < GUM_Numeric GUM_SCALAR >
212 std::unique_ptr< ASTtree< GUM_SCALAR > > ASTmult< GUM_SCALAR >::copy() const {
213 auto l = this->_op1->copy();
214 auto r = this->_op2->copy();
215 return std::make_unique< ASTmult< GUM_SCALAR > >(std::move(l), std::move(r));
216 }
217
218 template < GUM_Numeric GUM_SCALAR >
219 Tensor< GUM_SCALAR > ASTmult< GUM_SCALAR >::eval(const IBayesNet< GUM_SCALAR >& bn) const {
220 return this->_op1->eval(bn) * this->_op2->eval(bn);
221 }
222
223 // ================================================================
224 // ASTdiv (ASTBinaryOp)
225 // ================================================================
226 template < GUM_Numeric GUM_SCALAR >
227 ASTdiv< GUM_SCALAR >::ASTdiv(std::unique_ptr< ASTtree< GUM_SCALAR > > op1,
228 std::unique_ptr< ASTtree< GUM_SCALAR > > op2) :
229 ASTBinaryOp< GUM_SCALAR >("/", std::move(op1), std::move(op2)) {}
230
231 template < GUM_Numeric GUM_SCALAR >
233 return fastToLatex(nameOccur);
234 }
235
236 template < GUM_Numeric GUM_SCALAR >
238 return " \\frac {" + this->_op1->fastToLatex(nameOccur) + "}{"
239 + this->_op2->fastToLatex(nameOccur) + "}";
240 }
241
242 template < GUM_Numeric GUM_SCALAR >
243 std::unique_ptr< ASTtree< GUM_SCALAR > > ASTdiv< GUM_SCALAR >::copy() const {
244 auto l = this->_op1->copy();
245 auto r = this->_op2->copy();
246 return std::make_unique< ASTdiv< GUM_SCALAR > >(std::move(l), std::move(r));
247 }
248
249 template < GUM_Numeric GUM_SCALAR >
250 Tensor< GUM_SCALAR > ASTdiv< GUM_SCALAR >::eval(const IBayesNet< GUM_SCALAR >& bn) const {
251 return this->_op1->eval(bn) / this->_op2->eval(bn);
252 }
253
254 // ================================================================
255 // ASTposteriorProba : P_bn(vars | knw_min)
256 // ================================================================
257 template < GUM_Numeric GUM_SCALAR >
262
263 template < GUM_Numeric GUM_SCALAR >
270
271 template < GUM_Numeric GUM_SCALAR >
277
278 template < GUM_Numeric GUM_SCALAR >
280 return _vars;
281 }
282
283 template < GUM_Numeric GUM_SCALAR >
285 return _knw;
286 }
287
288 template < GUM_Numeric GUM_SCALAR >
289 std::string ASTposteriorProba< GUM_SCALAR >::toString(std::string_view prefix) const {
290 std::string result = "P(";
291
292 // Share the occurrence map across both sides so duplicate
293 // names (if any) get consistent LaTeX suffixes.
295
296 // LEFT: variables, sorted & latex-corrected
298 bool first = true;
299 for (const auto& v: left) {
300 if (!first) result += ',';
301 result += v;
302 first = false;
303 }
304
305 // RIGHT: conditioning set, sorted & latex-corrected
306 if (!_knw.empty()) {
307 result += '|';
308 auto right = ASTtree< GUM_SCALAR >::_latexCorrect(_knw, occur);
309 first = true;
310 for (const auto& k: right) {
311 if (!first) result += ',';
312 result += k;
313 first = false;
314 }
315 }
316
317 result += ')';
318 return std::string{prefix} + result;
319 }
320
321 template < GUM_Numeric GUM_SCALAR >
323 HashTable< std::string, int >& nameOccur) const {
324 return fastToLatex(nameOccur);
325 }
326
327 template < GUM_Numeric GUM_SCALAR >
328 std::string
330 std::string result = "P\\left(";
331
332 // vars
333 {
334 auto corr = ASTtree< GUM_SCALAR >::_latexCorrect(_vars, nameOccur);
335 bool first = true;
336 for (const auto& v: corr) {
337 if (!first) result += ',';
338 result += v;
339 first = false;
340 }
341 }
342
343 // | knw
344 if (!_knw.empty()) {
345 result += "\\mid ";
346 auto corr = ASTtree< GUM_SCALAR >::_latexCorrect(_knw, nameOccur);
347 bool first = true;
348 for (const auto& k: corr) {
349 if (!first) result += ',';
350 result += k;
351 first = false;
352 }
353 }
354
355 result += "\\right)";
356 return result;
357 }
358
359 template < GUM_Numeric GUM_SCALAR >
360 std::unique_ptr< ASTtree< GUM_SCALAR > > ASTposteriorProba< GUM_SCALAR >::copy() const {
361 // Avoid recomputing minimal set: use private direct-ctor tag declared in header
362 return std::make_unique< ASTposteriorProba< GUM_SCALAR > >(_vars, _knw);
363 }
364
365 template < GUM_Numeric GUM_SCALAR >
366 Tensor< GUM_SCALAR >
368 // Build NodeSets from names
369 NodeSet set_vars, set_knw;
370 for (const auto& v: _vars)
371 set_vars.insert(contextual_bn.idFromName(v));
372 for (const auto& k: _knw)
373 set_knw.insert(contextual_bn.idFromName(k));
374
375
376 // --- simple CPT case: P(var | parents(var)) ---
377 if (_vars.size() == 1) {
378 const auto& x = *_vars.begin();
379 NodeId ix = contextual_bn.idFromName(x);
380
381 // Directly compare NodeSets: parents(ix) vs set_knw
382 if (contextual_bn.parents(ix) == set_knw) { return contextual_bn.cpt(ix); }
383 }
384
385
386 // --- otherwise: use inference ---
387 LazyPropagation< GUM_SCALAR > ie(&contextual_bn);
388 Tensor< GUM_SCALAR > p;
389
390 if (_knw.empty()) {
391 ie.addJointTarget(set_vars);
392 ie.makeInference();
393 p = ie.jointPosterior(set_vars);
394 } else {
395 ie.addJointTarget(set_vars);
396 ie.makeInference();
397 p = ie.evidenceJointImpact(set_vars, set_knw);
398 }
399
400
401 return p;
402 }
403
404 template < GUM_Numeric GUM_SCALAR >
406 if (vars.empty()) { GUM_ERROR(InvalidArgument, "ASTposteriorProba: vars must not be empty"); }
407 }
408
409 template < GUM_Numeric GUM_SCALAR >
413 const Set< std::string >& knw) {
414 NodeSet varIds, knwIds;
415 for (const auto& v: vars)
416 varIds.insert(bn.idFromName(v));
417 for (const auto& k: knw)
418 knwIds.insert(bn.idFromName(k));
420 const auto minK = bn.minimalCondSet(varIds, knwIds);
421 for (auto nid: minK)
422 out.insert(bn.variable(nid).name());
423 return out;
424 }
425
426 template < GUM_Numeric GUM_SCALAR >
428 const DAG& dag,
431 const Set< std::string >& knw) {
432 NodeSet varIds, knwIds;
433 for (const auto& v: vars)
434 varIds.insert(id2name.first(v));
435 for (const auto& k: knw)
436 knwIds.insert(id2name.first(k));
438 const auto minK = dag.minimalCondSet(varIds, knwIds);
439 for (auto nid: minK)
440 out.insert(id2name.second(nid));
441 return out;
442 }
443
444 // ================================================================
445 // ASTjointProba : P(vars) in observational BN
446 // ================================================================
447 template < GUM_Numeric GUM_SCALAR >
449 ASTtree< GUM_SCALAR >("_joint_"), _varNames(varNames) {
450 // validate that vars is not empty
451 if (varNames.empty()) { GUM_ERROR(InvalidArgument, "ASTjointProba: vars must not be empty"); }
452 }
453
454 template < GUM_Numeric GUM_SCALAR >
456 return _varNames;
457 }
458
459 template < GUM_Numeric GUM_SCALAR >
460 std::string ASTjointProba< GUM_SCALAR >::toString(std::string_view prefix) const {
461 std::string result = "joint P(";
462
463 // Build a consistent, sorted, LaTeX-corrected list of names
464 gum::HashTable< std::string, int > occur; // tracks suffixes if needed
466
467 bool first = true;
468 for (const auto& v: names) {
469 if (!first) result += ',';
470 result += v;
471 first = false;
472 }
473
474 result += ')';
475 return std::string{prefix} + result;
476 }
477
478 template < GUM_Numeric GUM_SCALAR >
479 std::string
483
484 template < GUM_Numeric GUM_SCALAR >
485 std::string
487 std::string result = "P\\left(";
488 auto corr = ASTtree< GUM_SCALAR >::_latexCorrect(_varNames, nameOccur);
489 bool first = true;
490 for (const auto& v: corr) {
491 if (!first) result += ',';
492 result += v;
493 first = false;
494 }
495 result += "\\right)";
496 return result;
497 }
498
499 template < GUM_Numeric GUM_SCALAR >
500 std::unique_ptr< ASTtree< GUM_SCALAR > > ASTjointProba< GUM_SCALAR >::copy() const {
501 return std::make_unique< ASTjointProba< GUM_SCALAR > >(_varNames);
502 }
503
504 template < GUM_Numeric GUM_SCALAR >
505 Tensor< GUM_SCALAR >
507 NodeSet ids;
508 for (const auto& v: _varNames)
509 ids.insert(contextual_bn.idFromName(v));
510
511 LazyPropagation< GUM_SCALAR > ie(&contextual_bn);
512 if (ids.size() == 1) {
513 // Special case: single variable, use its CPT directly
514 NodeId id = *ids.begin();
515 ie.addTarget(id);
516 ie.makeInference();
517 return ie.posterior(id);
518 } else {
519 // General case: add all variables as joint target
520 ie.addJointTarget(ids);
521 ie.makeInference();
522 return ie.jointPosterior(ids);
523 }
524 }
525
526 // ================================================================
527 // ASTsum : sum out over variable of sub-term
528 // ================================================================
529 template < GUM_Numeric GUM_SCALAR >
531 std::unique_ptr< ASTtree< GUM_SCALAR > > term) :
532 ASTtree< GUM_SCALAR >("_sum_"), _var(var), _term(std::move(term)) {}
533
534 template < GUM_Numeric GUM_SCALAR >
535 ASTsum< GUM_SCALAR >::ASTsum(const std::vector< std::string >& vars,
536 std::unique_ptr< ASTtree< GUM_SCALAR > > term) :
537 ASTtree< GUM_SCALAR >("_sum_") {
538 if (vars.empty()) { throw gum::InvalidArgument("ASTsum: variable list cannot be empty"); }
539
540 _var = vars.front();
541
542 if (vars.size() > 1) {
543 std::vector< std::string > tail(vars.begin() + 1, vars.end());
544 _term = std::make_unique< ASTsum< GUM_SCALAR > >(tail, std::move(term));
545 } else {
546 _term = std::move(term);
547 }
548 }
549
550 template < GUM_Numeric GUM_SCALAR >
551 const std::string& ASTsum< GUM_SCALAR >::var() const {
552 return _var;
553 }
554
555 template < GUM_Numeric GUM_SCALAR >
556 const ASTtree< GUM_SCALAR >& ASTsum< GUM_SCALAR >::term() const {
557 return *_term;
558 }
559
560 template < GUM_Numeric GUM_SCALAR >
561 std::string ASTsum< GUM_SCALAR >::toString(std::string_view prefix) const {
562 return std::format(
563 "{}sum on {} for\n{}",
564 prefix,
565 _var,
566 _term->toString(std::string{prefix} + ASTtree< GUM_SCALAR >::CONTINUE_PREFIX));
567 }
568
569 template < GUM_Numeric GUM_SCALAR >
571 return "\\left(" + fastToLatex(nameOccur) + "\\right)";
572 }
573
574 template < GUM_Numeric GUM_SCALAR >
576 // Flatten chained sums: collect all summed variables along the _term chain.
577 std::vector< std::string > vars;
578 const ASTtree< GUM_SCALAR >* a = this;
579 while (auto s = dynamic_cast< const ASTsum< GUM_SCALAR >* >(a)) {
580 if (!nameOccur.exists(s->_var)) nameOccur.insert(s->_var, 0);
581 nameOccur[s->_var] += 1;
582 vars.push_back(s->_var);
583 a = &s->term(); // move down the chain
584 }
585
586 // Build corrected (prime-adjusted) names, then sort.
587 Set< std::string > varNames;
588 for (const auto& v: vars) {
589 varNames.insert(v);
590 }
591 auto corr = ASTtree< GUM_SCALAR >::_latexCorrect(varNames, nameOccur);
592
593 // Join corrected names with commas
594 std::string names;
595 for (size_t i = 0; i < corr.size(); ++i) {
596 if (i) names += ',';
597 names += corr[i];
598 }
599
600 // Inner formula is the first non-sum node's latex with current nameOccur
601 std::string out = std::format("\\sum_{{{}}}{{{}}}", names, a->protectToLatex(nameOccur));
602
603 // Restore nameOccur (decrement each var)
604 for (const auto& v: vars) {
605 // nameOccur[v] is guaranteed to exist here
606 nameOccur[v] -= 1;
607 }
608
609 return out;
610 }
611
612 template < GUM_Numeric GUM_SCALAR >
613 std::unique_ptr< ASTtree< GUM_SCALAR > > ASTsum< GUM_SCALAR >::copy() const {
614 return std::make_unique< ASTsum< GUM_SCALAR > >(_var, _term->copy());
615 }
616
617 template < GUM_Numeric GUM_SCALAR >
618 Tensor< GUM_SCALAR >
620 auto base = _term->eval(contextual_bn);
621
622 const auto& dv = contextual_bn.variable(contextual_bn.idFromName(_var));
623 VariableSet dv_set;
624 dv_set.insert(&dv);
625 return base.sumOut(dv_set);
626 }
627
628 // ================================================================
629 // Utility: productOfTrees
630 // ================================================================
631 template < GUM_Numeric GUM_SCALAR >
632 std::unique_ptr< ASTtree< GUM_SCALAR > >
633 productOfTrees(std::vector< std::unique_ptr< ASTtree< GUM_SCALAR > > >&& lterms) {
634 if (lterms.empty()) { GUM_ERROR(InvalidArgument, "productOfTrees requires at least one term"); }
635
636 auto it = lterms.begin();
637 auto end = lterms.end();
638
639 // Take the first term as accumulator
640 std::unique_ptr< ASTtree< GUM_SCALAR > > acc = std::move(*it);
641 ++it;
642
643 // Left-associate: (((t1 * t2) * t3) * ...)
644 for (; it != end; ++it) {
645 acc = std::make_unique< ASTmult< GUM_SCALAR > >(std::move(acc), std::move(*it));
646 }
647 return acc;
648 }
649
650} // namespace gum
Class representing the minimal interface for Bayesian network with no numerical data.
ASTBinaryOp(std::string_view typ, std::unique_ptr< ASTtree< GUM_SCALAR > > op1, std::unique_ptr< ASTtree< GUM_SCALAR > > op2)
Definition doAST_tpl.h:104
std::string toString(std::string_view prefix="") const override
Human-readable multi-line rendering (for debugging / logs).
Definition doAST_tpl.h:120
const ASTtree< GUM_SCALAR > & op2() const
Definition doAST_tpl.h:115
const ASTtree< GUM_SCALAR > & op1() const
Definition doAST_tpl.h:110
std::unique_ptr< ASTtree< GUM_SCALAR > > _op2
Definition doAST.h:201
std::unique_ptr< ASTtree< GUM_SCALAR > > _op1
Definition doAST.h:201
std::string fastToLatex(HashTable< std::string, int > &nameOccur) const override
Fast LaTeX rendering (lighter protection).
Definition doAST_tpl.h:237
std::string protectToLatex(HashTable< std::string, int > &nameOccur) const override
LaTeX rendering with full name protection.
Definition doAST_tpl.h:232
ASTdiv(std::unique_ptr< ASTtree< GUM_SCALAR > > op1, std::unique_ptr< ASTtree< GUM_SCALAR > > op2)
Definition doAST_tpl.h:227
Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR > &contextual_bn) const override
Evaluate the expression against a contextual BN.
Definition doAST_tpl.h:250
std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override
Deep clone of the sub-tree.
Definition doAST_tpl.h:243
std::string fastToLatex(HashTable< std::string, int > &nameOccur) const override
Fast LaTeX rendering (lighter protection).
Definition doAST_tpl.h:486
ASTjointProba(const Set< std::string > &varNames)
Build a joint (\mathbb{P}(\mathrm{varNames})).
Definition doAST_tpl.h:448
const Set< std::string > & varNames() const noexcept
Definition doAST_tpl.h:455
std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override
Deep clone of the sub-tree.
Definition doAST_tpl.h:500
std::string toString(std::string_view prefix="") const override
Human-readable multi-line rendering (for debugging / logs).
Definition doAST_tpl.h:460
std::string protectToLatex(HashTable< std::string, int > &nameOccur) const override
LaTeX rendering with full name protection.
Definition doAST_tpl.h:480
Set< std::string > _varNames
Definition doAST.h:389
Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR > &contextual_bn) const override
Evaluate the expression against a contextual BN.
Definition doAST_tpl.h:506
std::string fastToLatex(HashTable< std::string, int > &nameOccur) const override
Fast LaTeX rendering (lighter protection).
Definition doAST_tpl.h:175
std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override
Deep clone of the sub-tree.
Definition doAST_tpl.h:180
std::string protectToLatex(HashTable< std::string, int > &nameOccur) const override
LaTeX rendering with full name protection.
Definition doAST_tpl.h:170
ASTminus(std::unique_ptr< ASTtree< GUM_SCALAR > > op1, std::unique_ptr< ASTtree< GUM_SCALAR > > op2)
Definition doAST_tpl.h:164
Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR > &contextual_bn) const override
Evaluate the expression against a contextual BN.
Definition doAST_tpl.h:187
std::string fastToLatex(HashTable< std::string, int > &nameOccur) const override
Fast LaTeX rendering (lighter protection).
Definition doAST_tpl.h:207
Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR > &contextual_bn) const override
Evaluate the expression against a contextual BN.
Definition doAST_tpl.h:219
ASTmult(std::unique_ptr< ASTtree< GUM_SCALAR > > op1, std::unique_ptr< ASTtree< GUM_SCALAR > > op2)
Definition doAST_tpl.h:195
std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override
Deep clone of the sub-tree.
Definition doAST_tpl.h:212
std::string protectToLatex(HashTable< std::string, int > &nameOccur) const override
LaTeX rendering with full name protection.
Definition doAST_tpl.h:201
std::string fastToLatex(HashTable< std::string, int > &nameOccur) const override
Fast LaTeX rendering (lighter protection).
Definition doAST_tpl.h:144
Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR > &contextual_bn) const override
Evaluate the expression against a contextual BN.
Definition doAST_tpl.h:156
ASTplus(std::unique_ptr< ASTtree< GUM_SCALAR > > op1, std::unique_ptr< ASTtree< GUM_SCALAR > > op2)
Definition doAST_tpl.h:133
std::string protectToLatex(HashTable< std::string, int > &nameOccur) const override
LaTeX rendering with full name protection.
Definition doAST_tpl.h:139
std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override
Deep clone of the sub-tree.
Definition doAST_tpl.h:149
std::string toString(std::string_view prefix="") const override
Human-readable multi-line rendering (for debugging / logs).
Definition doAST_tpl.h:289
static void _ensure_nonempty(const Set< std::string > &vars)
Definition doAST_tpl.h:405
Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR > &contextual_bn) const override
Evaluate the expression against a contextual BN.
Definition doAST_tpl.h:367
Set< std::string > _vars
names of conditioned variables
Definition doAST.h:342
static Set< std::string > _compute_knw_from_dag(const DAG &dag, const Bijection< NodeId, std::string > &id2name, const Set< std::string > &vars, const Set< std::string > &knw)
Definition doAST_tpl.h:427
const Set< std::string > & knw() const noexcept
Definition doAST_tpl.h:284
std::string fastToLatex(HashTable< std::string, int > &nameOccur) const override
Fast LaTeX rendering (lighter protection).
Definition doAST_tpl.h:329
Set< std::string > _knw
names of conditioning variables (already minimalized)
Definition doAST.h:343
ASTposteriorProba(const DAGmodel &bn, const Set< std::string > &vars, const Set< std::string > &knw)
Constructor for ( \mathbb{P}_{bn}(\mathrm{vars}\mid\mathrm{knw}) ); knw will be minimalized using bn.
Definition doAST_tpl.h:258
static Set< std::string > _compute_knw_from_bn(const DAGmodel &bn, const Set< std::string > &vars, const Set< std::string > &knw)
Definition doAST_tpl.h:411
const Set< std::string > & vars() const noexcept
Definition doAST_tpl.h:279
std::string protectToLatex(HashTable< std::string, int > &nameOccur) const override
LaTeX rendering with full name protection.
Definition doAST_tpl.h:322
std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override
Deep clone of the sub-tree.
Definition doAST_tpl.h:360
std::string _var
variable to eliminate
Definition doAST.h:434
std::string protectToLatex(HashTable< std::string, int > &nameOccur) const override
LaTeX rendering with full name protection.
Definition doAST_tpl.h:570
std::string toString(std::string_view prefix="") const override
Human-readable multi-line rendering (for debugging / logs).
Definition doAST_tpl.h:561
Tensor< GUM_SCALAR > eval(const IBayesNet< GUM_SCALAR > &contextual_bn) const override
Evaluate the expression against a contextual BN.
Definition doAST_tpl.h:619
std::string fastToLatex(HashTable< std::string, int > &nameOccur) const override
Fast LaTeX rendering (lighter protection).
Definition doAST_tpl.h:575
std::unique_ptr< ASTtree< GUM_SCALAR > > _term
sub-expression
Definition doAST.h:435
ASTsum(std::string_view var, std::unique_ptr< ASTtree< GUM_SCALAR > > term)
Single-variable summation ( \sum_{\text{var}} \text{term} ).
Definition doAST_tpl.h:530
const ASTtree< GUM_SCALAR > & term() const
Definition doAST_tpl.h:556
std::unique_ptr< ASTtree< GUM_SCALAR > > copy() const override
Deep clone of the sub-tree.
Definition doAST_tpl.h:613
const std::string & var() const
Definition doAST_tpl.h:551
virtual ~ASTtree()
Definition doAST_tpl.h:63
virtual std::string fastToLatex(HashTable< std::string, int > &nameOccur) const =0
Fast LaTeX rendering (lighter protection).
ASTtree(std::string_view type)
Construct an AST node with a descriptive type string (used in dumps).
Definition doAST_tpl.h:60
std::string toLatex(HashTable< std::string, int > nameOccur=HashTable< std::string, int >()) const
Convenience wrapper using an empty occurrence table.
Definition doAST_tpl.h:73
static constexpr const char * CONTINUE_PREFIX
Prefix used to draw tree branches in toString.
Definition doAST.h:153
static std::string _latexCorrect(std::string_view srcName, HashTable< std::string, int > &nameOccur)
Sanitize a single variable name for LaTeX and ensure uniqueness.
Definition doAST_tpl.h:79
const std::string & type() const noexcept
Definition doAST_tpl.h:68
std::string _type
Definition doAST.h:154
Base class for dag.
Definition DAG.h:121
NodeSet minimalCondSet(NodeId target, const NodeSet &soids) const
Definition DAG.cpp:88
Virtual base class for PGMs using a DAG.
Definition DAGmodel.h:64
NodeSet minimalCondSet(NodeId target, const NodeSet &soids) const
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
NodeId idFromName(std::string_view name) const override
Returns the NodeId of a variable given its name.
const DiscreteVariable & variable(NodeId id) const override
Returns a constant reference over a variable given its node id.
virtual void makeInference() final
perform the heavy computations needed to compute the targets' posteriors
The class for generic Hash Tables.
Definition hashTable.h:640
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
mapped_type & getWithDefault(const Key &key, const Val &default_value)
Returns a reference on the element the key of which is passed in argument.
Class representing the minimal interface for Bayesian network with no numerical data.
Definition IBayesNet.h:75
virtual const Tensor< GUM_SCALAR > & cpt(NodeId varId) const =0
Returns the CPT of a variable.
Exception: at least one argument passed to a function is not what was expected.
const Tensor< GUM_SCALAR > & posterior(NodeId node) final
Computes and returns the posterior of a node.
Tensor< GUM_SCALAR > evidenceJointImpact(const NodeSet &targets, const NodeSet &evs)
Create a gum::Tensor for P(joint targets|evs) (for all instantiation of targets and evs).
virtual void addJointTarget(const NodeSet &joint_target) final
Add a set of nodes as a new joint target. As a collateral effect, every node is added as a marginal t...
virtual const Tensor< GUM_SCALAR > & jointPosterior(const NodeSet &nodes) final
Compute the joint posterior of a set of nodes.
Implementation of a Shafer-Shenoy's-like version of lazy propagation for inference in Bayesian networ...
virtual void addTarget(NodeId target) final
Add a marginal target to the list of targets.
iterator begin() const
The usual unsafe begin iterator to parse the set.
Definition set_tpl.h:409
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:510
Size size() const noexcept
Returns the number of elements in the set.
Definition set_tpl.h:607
const std::string & name() const
returns the name of the variable
Abstract Syntax Tree (AST) for algebraic probability expressions used in do-Calculus.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
Size NodeId
Type for node ids.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
Implementation of a Shafer-Shenoy's-like version of lazy propagation for inference in Bayesian networ...
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Set< const DiscreteVariable * > VariableSet
std::unique_ptr< ASTtree< GUM_SCALAR > > productOfTrees(std::vector< std::unique_ptr< ASTtree< GUM_SCALAR > > > &&lterms)
Build a product AST from a list of terms.
Definition doAST_tpl.h:633
STL namespace.