aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
dSeparationAlgorithm_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
48
49
50namespace gum {
51
52
53 // update a set of tensors, keeping only those d-connected with
54 // query variables given evidence
55 template < typename 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.dag();
62
63 // mark the set of ancestors of the evidence
64 NodeSet ev_ancestors(dag.size());
65 {
66 List< NodeId > anc_to_visit;
67 for (const auto node: hardEvidence)
68 anc_to_visit.insert(node);
69 for (const auto node: softEvidence)
70 anc_to_visit.insert(node);
71 while (!anc_to_visit.empty()) {
72 const NodeId node = anc_to_visit.front();
73 anc_to_visit.popFront();
74
75 if (!ev_ancestors.exists(node)) {
76 ev_ancestors.insert(node);
77 for (const auto par: dag.parents(node)) {
78 anc_to_visit.insert(par);
79 }
80 }
81 }
82 }
83
84 // create the marks indicating that we have visited a node
85 NodeSet visited_from_child(dag.size());
86 NodeSet visited_from_parent(dag.size());
87
91 for (const auto pot: tensors) {
92 const Sequence< const DiscreteVariable* >& vars = pot->variablesSequence();
93 for (const auto var: vars) {
94 const NodeId id = bn.nodeId(*var);
95 if (!node2tensors.exists(id)) { node2tensors.insert(id, Set< const TABLE* >()); }
96 node2tensors[id].insert(pot);
97 }
98 }
99
100 // indicate that we will send the ball to all the query nodes (as children):
101 // in list nodes_to_visit, the first element is the next node to send the
102 // ball to and the Boolean indicates whether we shall reach it from one of
103 // its children (true) or from one parent (false)
104 List< std::pair< NodeId, bool > > nodes_to_visit;
105 for (const auto node: query) {
106 nodes_to_visit.insert(std::pair< NodeId, bool >(node, true));
107 }
108
109 // perform the bouncing ball until there is no node in the graph to send
110 // the ball to
111 while (!nodes_to_visit.empty() && !node2tensors.empty()) {
112 // get the next node to visit
113 const NodeId node = nodes_to_visit.front().first;
114 const bool direction = nodes_to_visit.front().second;
115 nodes_to_visit.popFront();
116
117 // check if the node has not already been visited in the same direction
118 bool already_visited;
119 if (direction) {
120 already_visited = visited_from_child.exists(node);
121 if (!already_visited) { visited_from_child.insert(node); }
122 } else {
123 already_visited = visited_from_parent.exists(node);
124 if (!already_visited) { visited_from_parent.insert(node); }
125 }
126
127 // if the node belongs to the query, update _node2tensors_: remove all
128 // the tensors containing the node
129 if (node2tensors.exists(node)) {
130 auto& pot_set = node2tensors[node];
131 for (const auto pot: pot_set) {
132 const auto& vars = pot->variablesSequence();
133 for (const auto var: vars) {
134 const NodeId id = bn.nodeId(*var);
135 if (id != node) {
136 node2tensors[id].erase(pot);
137 if (node2tensors[id].empty()) { node2tensors.erase(id); }
138 }
139 }
140 }
141 node2tensors.erase(node);
142
143 // if _node2tensors_ is empty, no need to go on: all the tensors
144 // are d-connected to the query
145 if (node2tensors.empty()) return;
146 }
147
148 // if this is the first time we meet the node, then visit it
149 if (!already_visited) {
150 // mark the node as reachable if this is not a hard evidence
151 const bool is_hard_evidence = hardEvidence.exists(node);
152
153 // bounce the ball toward the neighbors
154 if (direction && !is_hard_evidence) { // visit from a child
155 // visit the parents
156 for (const auto par: dag.parents(node)) {
157 nodes_to_visit.insert(std::pair< NodeId, bool >(par, true));
158 }
159
160 // visit the children
161 for (const auto chi: dag.children(node)) {
162 nodes_to_visit.insert(std::pair< NodeId, bool >(chi, false));
163 }
164 } else { // visit from a parent
165 if (!hardEvidence.exists(node)) {
166 // visit the children
167 for (const auto chi: dag.children(node)) {
168 nodes_to_visit.insert(std::pair< NodeId, bool >(chi, false));
169 }
170 }
171 if (ev_ancestors.exists(node)) {
172 // visit the parents
173 for (const auto par: dag.parents(node)) {
174 nodes_to_visit.insert(std::pair< NodeId, bool >(par, true));
175 }
176 }
177 }
178 }
179 }
180
181 // here, all the tensors that belong to _node2tensors_ are d-separated
182 // from the query
183 for (const auto& elt: node2tensors) {
184 for (const auto pot: elt.second) {
185 tensors.erase(pot);
186 }
187 }
188 }
189
190
191} /* 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
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 insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:539
void erase(const Key &k)
Erases an element from the set.
Definition set_tpl.h:582
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
Size NodeId
Type for node ids.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
gum is the global namespace for all aGrUM entities
Definition agrum.h:46