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