55 template <
typename GUM_SCALAR,
class TABLE >
67 for (
const auto node: hardEvidence)
69 for (
const auto node: softEvidence)
71 while (!anc_to_visit.
empty()) {
75 if (!ev_ancestors.
exists(node)) {
77 for (
const auto par: dag.
parents(node)) {
91 for (
const auto pot: tensors) {
93 for (
const auto var: vars) {
96 node2tensors[id].
insert(pot);
105 for (
const auto node: query) {
106 nodes_to_visit.
insert(std::pair< NodeId, bool >(node,
true));
111 while (!nodes_to_visit.
empty() && !node2tensors.
empty()) {
114 const bool direction = nodes_to_visit.
front().second;
118 bool already_visited;
120 already_visited = visited_from_child.
exists(node);
121 if (!already_visited) { visited_from_child.
insert(node); }
123 already_visited = visited_from_parent.
exists(node);
124 if (!already_visited) { visited_from_parent.
insert(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) {
136 node2tensors[id].
erase(pot);
137 if (node2tensors[
id].empty()) { node2tensors.
erase(
id); }
141 node2tensors.
erase(node);
145 if (node2tensors.
empty())
return;
149 if (!already_visited) {
151 const bool is_hard_evidence = hardEvidence.
exists(node);
154 if (direction && !is_hard_evidence) {
156 for (
const auto par: dag.
parents(node)) {
157 nodes_to_visit.
insert(std::pair< NodeId, bool >(par,
true));
161 for (
const auto chi: dag.
children(node)) {
162 nodes_to_visit.
insert(std::pair< NodeId, bool >(chi,
false));
165 if (!hardEvidence.
exists(node)) {
167 for (
const auto chi: dag.
children(node)) {
168 nodes_to_visit.
insert(std::pair< NodeId, bool >(chi,
false));
171 if (ev_ancestors.
exists(node)) {
173 for (
const auto par: dag.
parents(node)) {
174 nodes_to_visit.
insert(std::pair< NodeId, bool >(par,
true));
183 for (
const auto& elt: node2tensors) {
184 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 children of a set of nodes
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
The class for generic Hash Tables.
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.
virtual NodeId nodeId(const DiscreteVariable &var) const =0
Return id node from discrete var pointer.
Generic doubly linked lists.
Val & front() const
Returns a reference to first element of a list, if any.
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.
Val & insert(const Val &val)
Inserts a new element at the end of the chained list (alias of pushBack).
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
Size NodeId
Type for node ids.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
gum is the global namespace for all aGrUM entities