aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
BayesBall_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
47
48#include <agrum/BN/algorithms/BayesBall.h> // to ease IDE parser
49
50namespace gum {
51
52
53 // update a set of tensors, keeping only those d-connected with
54 // query variables
55 template < GUM_Numeric GUM_SCALAR, class TABLE >
57 const NodeSet& query,
58 const NodeSet& hardEvidence,
59 const NodeSet& softEvidence,
60 Set< const TABLE* >& tensors) {
61 const DAG& dag = bn.internalDag();
62
63 // create the marks (top = first and bottom = second)
65 const std::pair< bool, bool > empty_mark(false, false);
66
70 for (const auto pot: tensors) {
71 const Sequence< const DiscreteVariable* >& vars = pot->variablesSequence();
72 for (const auto var: vars) {
73 const NodeId id = bn.nodeId(*var);
74 if (!node2tensors.exists(id)) { node2tensors.insert(id, Set< const TABLE* >()); }
75 node2tensors[id].insert(pot);
76 }
77 }
78
79 // indicate that we will send the ball to all the query nodes (as children):
80 // in list nodes_to_visit, the first element is the next node to send the
81 // ball to and the Boolean indicates whether we shall reach it from one of
82 // its children (true) or from one parent (false)
83 List< std::pair< NodeId, bool > > nodes_to_visit;
84 for (const auto node: query) {
85 nodes_to_visit.insert(std::pair< NodeId, bool >(node, true));
86 }
87
88 // perform the bouncing ball until _node2tensors_ becomes empty (which
89 // means that we have reached all the tensors and, therefore, those
90 // are d-connected to query) or until there is no node in the graph to send
91 // the ball to
92 while (!nodes_to_visit.empty() && !node2tensors.empty()) {
93 // get the next node to visit
94 NodeId node = nodes_to_visit.front().first;
95
96 // if the marks of the node do not exist, create them
97 if (!marks.exists(node)) marks.insert(node, empty_mark);
98
99 // if the node belongs to the query, update _node2tensors_: remove all
100 // the tensors containing the node
101 if (node2tensors.exists(node)) {
102 auto& pot_set = node2tensors[node];
103 for (const auto pot: pot_set) {
104 const auto& vars = pot->variablesSequence();
105 for (const auto var: vars) {
106 const NodeId id = bn.nodeId(*var);
107 if (id != node) {
108 node2tensors[id].erase(pot);
109 if (node2tensors[id].empty()) { node2tensors.erase(id); }
110 }
111 }
112 }
113 node2tensors.erase(node);
114
115 // if _node2tensors_ is empty, no need to go on: all the tensors
116 // are d-connected to the query
117 if (node2tensors.empty()) return;
118 }
119
120
121 // bounce the ball toward the neighbors
122 if (nodes_to_visit.front().second) { // visit from a child
123 nodes_to_visit.popFront();
124
125 if (hardEvidence.exists(node)) { continue; }
126
127 if (!marks[node].first) {
128 marks[node].first = true; // top marked
129 for (const auto par: dag.parents(node)) {
130 nodes_to_visit.insert(std::pair< NodeId, bool >(par, true));
131 }
132 }
133
134 if (!marks[node].second) {
135 marks[node].second = true; // bottom marked
136 for (const auto chi: dag.children(node)) {
137 nodes_to_visit.insert(std::pair< NodeId, bool >(chi, false));
138 }
139 }
140 } else { // visit from a parent
141 nodes_to_visit.popFront();
142
143 const bool is_hard_evidence = hardEvidence.exists(node);
144 const bool is_evidence = is_hard_evidence || softEvidence.exists(node);
145
146 if (is_evidence && !marks[node].first) {
147 marks[node].first = true;
148
149 for (const auto par: dag.parents(node)) {
150 nodes_to_visit.insert(std::pair< NodeId, bool >(par, true));
151 }
152 }
153
154 if (!is_hard_evidence && !marks[node].second) {
155 marks[node].second = true;
156
157 for (const auto chi: dag.children(node)) {
158 nodes_to_visit.insert(std::pair< NodeId, bool >(chi, false));
159 }
160 }
161 }
162 }
163
164
165 // here, all the tensors that belong to _node2tensors_ are d-separated
166 // from the query
167 for (const auto& elt: node2tensors) {
168 for (const auto pot: elt.second) {
169 tensors.erase(pot);
170 }
171 }
172 }
173
174
175} /* namespace gum */
The BayesBall algorithm (as described by Schachter).
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 ...
static void relevantTensors(const IBayesNet< GUM_SCALAR > &bn, const NodeSet &query, const NodeSet &hardEvidence, const NodeSet &softEvidence, Set< const TABLE * > &tensors)
update a set of tensors, keeping only those d-connected with query variables given evidence
Base class for dag.
Definition DAG.h:121
const DAG & internalDag() const
Returns a const reference to the internal (unnamed) DAG. O(1), no copy. Use for stable references or ...
NodeId nodeId(const DiscreteVariable &var) const override
Returns the NodeId of a variable.
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 empty() const noexcept
Indicates whether the hash table is empty.
void erase(const Key &key)
Removes a given element from the hash table.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
Class representing the minimal interface for Bayesian network with no numerical data.
Definition IBayesNet.h:75
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 & 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
Size size() const
alias for sizeNodes
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:994
Representation of a set.
Definition set.h:129
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:504
void erase(const Key &k)
Erases an element from the set.
Definition set_tpl.h:553
Size NodeId
Type for node ids.
HashTable< NodeId, VAL > NodeProperty
Property on graph elements.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
gum is the global namespace for all aGrUM entities
Definition agrum.h:46