aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
BayesBall.cpp
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
41
46
48
49#ifdef GUM_NO_INLINE
51#endif // GUM_NO_INLINE
52
53namespace gum {
54
56 const NodeSet& query,
57 const NodeSet& hardEvidence,
58 const NodeSet& softEvidence,
59 NodeSet& requisite) {
60 // for the moment, no node is requisite
61 requisite.clear();
62
63 // create the marks (top = first and bottom = second )
65 const std::pair< bool, bool > empty_mark(false, false);
66
67 // indicate that we will send the ball to all the query nodes (as children):
68 // in list nodes_to_visit, the first element is the next node to send the
69 // ball to and the Boolean indicates whether we shall reach it from one of
70 // its children (true) or from one parent (false)
71 List< std::pair< NodeId, bool > > nodes_to_visit;
72 for (const auto node: query) {
73 nodes_to_visit.insert(std::pair< NodeId, bool >(node, true));
74 }
75
76 // perform the bouncing ball until there is no node in the graph to send
77 // the ball to
78 while (!nodes_to_visit.empty()) {
79 // get the next node to visit
80 NodeId node = nodes_to_visit.front().first;
81
82 // if the marks of the node do not exist, create them
83 if (!marks.exists(node)) marks.insert(node, empty_mark);
84
85 // bounce the ball toward the neighbors
86 if (nodes_to_visit.front().second) { // visit from a child
87 nodes_to_visit.popFront();
88 requisite.insert(node);
89
90 if (hardEvidence.exists(node)) { continue; }
91
92 if (!marks[node].first) {
93 marks[node].first = true; // top marked
94 for (const auto par: dag.parents(node)) {
95 nodes_to_visit.insert(std::pair< NodeId, bool >(par, true));
96 }
97 }
98
99 if (!marks[node].second) {
100 marks[node].second = true; // bottom marked
101 for (const auto chi: dag.children(node)) {
102 nodes_to_visit.insert(std::pair< NodeId, bool >(chi, false));
103 }
104 }
105 } else { // visit from a parent
106 nodes_to_visit.popFront();
107
108 const bool is_hard_evidence = hardEvidence.exists(node);
109 const bool is_evidence = is_hard_evidence || softEvidence.exists(node);
110
111 if (is_evidence && !marks[node].first) {
112 marks[node].first = true;
113 requisite.insert(node);
114
115 for (const auto par: dag.parents(node)) {
116 nodes_to_visit.insert(std::pair< NodeId, bool >(par, true));
117 }
118 }
119
120 if (!is_hard_evidence && !marks[node].second) {
121 marks[node].second = true;
122
123 for (const auto chi: dag.children(node)) {
124 nodes_to_visit.insert(std::pair< NodeId, bool >(chi, false));
125 }
126 }
127 }
128 }
129 }
130
131} /* namespace gum */
The BayesBall algorithm (as described by Schachter).
Implementation of the BayesBall class.
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 requisiteNodes(const DAG &dag, const NodeSet &query, const NodeSet &hardEvidence, const NodeSet &softEvidence, NodeSet &requisite)
Fill the 'requisite' nodeset with the requisite nodes in dag given a query and evidence.
Definition BayesBall.cpp:55
Base class for dag.
Definition DAG.h:121
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
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
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:533
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:539
void clear()
Removes all the elements, if any, from the set.
Definition set_tpl.h:338
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