58 template < GUM_Numeric GUM_SCALAR,
class TABLE >
70 for (
const auto node: hardEvidence)
72 for (
const auto node: softEvidence)
74 while (!anc_to_visit.
empty()) {
78 if (!ev_ancestors.
exists(node)) {
80 for (
const auto par: dag.
parents(node)) {
94 for (
const auto pot: tensors) {
96 for (
const auto var: vars) {
99 node2tensors[id].
insert(pot);
108 for (
const auto node: query) {
109 nodes_to_visit.
insert(std::pair< NodeId, bool >(node,
true));
114 while (!nodes_to_visit.
empty() && !node2tensors.
empty()) {
117 const bool direction = nodes_to_visit.
front().second;
121 bool already_visited;
123 already_visited = visited_from_child.
exists(node);
124 if (!already_visited) { visited_from_child.
insert(node); }
126 already_visited = visited_from_parent.
exists(node);
127 if (!already_visited) { visited_from_parent.
insert(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) {
139 node2tensors[id].
erase(pot);
140 if (node2tensors[
id].empty()) { node2tensors.
erase(
id); }
144 node2tensors.
erase(node);
148 if (node2tensors.
empty())
return;
152 if (!already_visited) {
154 const bool is_hard_evidence = hardEvidence.
exists(node);
157 if (direction && !is_hard_evidence) {
159 for (
const auto par: dag.
parents(node)) {
160 nodes_to_visit.
insert(std::pair< NodeId, bool >(par,
true));
164 for (
const auto chi: dag.
children(node)) {
165 nodes_to_visit.
insert(std::pair< NodeId, bool >(chi,
false));
168 if (!hardEvidence.
exists(node)) {
170 for (
const auto chi: dag.
children(node)) {
171 nodes_to_visit.
insert(std::pair< NodeId, bool >(chi,
false));
174 if (ev_ancestors.
exists(node)) {
176 for (
const auto par: dag.
parents(node)) {
177 nodes_to_visit.
insert(std::pair< NodeId, bool >(par,
true));
186 for (
const auto& elt: node2tensors) {
187 for (
const auto pot: elt.second) {
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 ...
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.
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.
Generic doubly linked lists.
Val & front() const
Returns a reference to first element of a list, if any.
Val & insert(const Val &val)
Inserts a new element at the end of the chained list (alias of pushBack).
bool empty() const noexcept
Returns a boolean indicating whether the chained list is empty.
void popFront()
Removes the first element of a List, if any.
Size size() const
alias for sizeNodes
The generic class for storing (ordered) sequences of objects.
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
void insert(const Key &k)
Inserts a new element into the set.
void erase(const Key &k)
Erases an element from the set.
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