aGrUM 2.3.2
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-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
46
47namespace gum {
48
49
50 // update a set of tensors, keeping only those d-connected with
51 // query variables
52 template < typename GUM_SCALAR, class TABLE >
54 const NodeSet& query,
55 const NodeSet& hardEvidence,
56 const NodeSet& softEvidence,
57 Set< const TABLE* >& tensors) {
58 const DAG& dag = bn.dag();
59
60 // create the marks (top = first and bottom = second)
62 const std::pair< bool, bool > empty_mark(false, false);
63
67 for (const auto pot: tensors) {
68 const Sequence< const DiscreteVariable* >& vars = pot->variablesSequence();
69 for (const auto var: vars) {
70 const NodeId id = bn.nodeId(*var);
71 if (!node2tensors.exists(id)) { node2tensors.insert(id, Set< const TABLE* >()); }
72 node2tensors[id].insert(pot);
73 }
74 }
75
76 // indicate that we will send the ball to all the query nodes (as children):
77 // in list nodes_to_visit, the first element is the next node to send the
78 // ball to and the Boolean indicates whether we shall reach it from one of
79 // its children (true) or from one parent (false)
80 List< std::pair< NodeId, bool > > nodes_to_visit;
81 for (const auto node: query) {
82 nodes_to_visit.insert(std::pair< NodeId, bool >(node, true));
83 }
84
85 // perform the bouncing ball until _node2tensors_ becomes empty (which
86 // means that we have reached all the tensors and, therefore, those
87 // are d-connected to query) or until there is no node in the graph to send
88 // the ball to
89 while (!nodes_to_visit.empty() && !node2tensors.empty()) {
90 // get the next node to visit
91 NodeId node = nodes_to_visit.front().first;
92
93 // if the marks of the node do not exist, create them
94 if (!marks.exists(node)) marks.insert(node, empty_mark);
95
96 // if the node belongs to the query, update _node2tensors_: remove all
97 // the tensors containing the node
98 if (node2tensors.exists(node)) {
99 auto& pot_set = node2tensors[node];
100 for (const auto pot: pot_set) {
101 const auto& vars = pot->variablesSequence();
102 for (const auto var: vars) {
103 const NodeId id = bn.nodeId(*var);
104 if (id != node) {
105 node2tensors[id].erase(pot);
106 if (node2tensors[id].empty()) { node2tensors.erase(id); }
107 }
108 }
109 }
110 node2tensors.erase(node);
111
112 // if _node2tensors_ is empty, no need to go on: all the tensors
113 // are d-connected to the query
114 if (node2tensors.empty()) return;
115 }
116
117
118 // bounce the ball toward the neighbors
119 if (nodes_to_visit.front().second) { // visit from a child
120 nodes_to_visit.popFront();
121
122 if (hardEvidence.exists(node)) { continue; }
123
124 if (!marks[node].first) {
125 marks[node].first = true; // top marked
126 for (const auto par: dag.parents(node)) {
127 nodes_to_visit.insert(std::pair< NodeId, bool >(par, true));
128 }
129 }
130
131 if (!marks[node].second) {
132 marks[node].second = true; // bottom marked
133 for (const auto chi: dag.children(node)) {
134 nodes_to_visit.insert(std::pair< NodeId, bool >(chi, false));
135 }
136 }
137 } else { // visit from a parent
138 nodes_to_visit.popFront();
139
140 const bool is_hard_evidence = hardEvidence.exists(node);
141 const bool is_evidence = is_hard_evidence || softEvidence.exists(node);
142
143 if (is_evidence && !marks[node].first) {
144 marks[node].first = true;
145
146 for (const auto par: dag.parents(node)) {
147 nodes_to_visit.insert(std::pair< NodeId, bool >(par, true));
148 }
149 }
150
151 if (!is_hard_evidence && !marks[node].second) {
152 marks[node].second = true;
153
154 for (const auto chi: dag.children(node)) {
155 nodes_to_visit.insert(std::pair< NodeId, bool >(chi, false));
156 }
157 }
158 }
159 }
160
161
162 // here, all the tensors that belong to _node2tensors_ are d-separated
163 // from the query
164 for (const auto& elt: node2tensors) {
165 for (const auto pot: elt.second) {
166 tensors.erase(pot);
167 }
168 }
169 }
170
171
172} /* namespace gum */
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
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 & dag() const
Returns a constant reference to the dag of this Bayes Net.
The class for generic Hash Tables.
Definition hashTable.h:637
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.
bool empty() const noexcept
Indicates whether the hash table is empty.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
Class representing the minimal interface for Bayesian network with no numerical data.
Definition IBayesNet.h:75
virtual NodeId nodeId(const DiscreteVariable &var) const =0
Return id node from discrete var pointer.
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
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
Size size() const
alias for sizeNodes
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:972
Representation of a set.
Definition set.h:131
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:533
void erase(const Key &k)
Erases an element from the set.
Definition set_tpl.h:582
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