55 template < GUM_Numeric GUM_SCALAR,
class TABLE >
65 const std::pair< bool, bool > empty_mark(
false,
false);
70 for (
const auto pot: tensors) {
72 for (
const auto var: vars) {
75 node2tensors[id].
insert(pot);
84 for (
const auto node: query) {
85 nodes_to_visit.
insert(std::pair< NodeId, bool >(node,
true));
92 while (!nodes_to_visit.
empty() && !node2tensors.
empty()) {
101 if (node2tensors.
exists(node)) {
102 auto& pot_set = node2tensors[node];
103 for (
const auto pot: pot_set) {
104 const auto& vars = pot->variablesSequence();
105 for (
const auto var: vars) {
108 node2tensors[id].
erase(pot);
109 if (node2tensors[
id].empty()) { node2tensors.
erase(
id); }
113 node2tensors.
erase(node);
117 if (node2tensors.
empty())
return;
122 if (nodes_to_visit.
front().second) {
125 if (hardEvidence.
exists(node)) {
continue; }
127 if (!marks[node].first) {
128 marks[node].first =
true;
129 for (
const auto par: dag.
parents(node)) {
130 nodes_to_visit.
insert(std::pair< NodeId, bool >(par,
true));
134 if (!marks[node].second) {
135 marks[node].second =
true;
136 for (
const auto chi: dag.
children(node)) {
137 nodes_to_visit.
insert(std::pair< NodeId, bool >(chi,
false));
143 const bool is_hard_evidence = hardEvidence.
exists(node);
144 const bool is_evidence = is_hard_evidence || softEvidence.
exists(node);
146 if (is_evidence && !marks[node].first) {
147 marks[node].first =
true;
149 for (
const auto par: dag.
parents(node)) {
150 nodes_to_visit.
insert(std::pair< NodeId, bool >(par,
true));
154 if (!is_hard_evidence && !marks[node].second) {
155 marks[node].second =
true;
157 for (
const auto chi: dag.
children(node)) {
158 nodes_to_visit.
insert(std::pair< NodeId, bool >(chi,
false));
167 for (
const auto& elt: node2tensors) {
168 for (
const auto pot: elt.second) {
The BayesBall algorithm (as described by Schachter).
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 ...
static 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
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 erase(const Key &k)
Erases an element from the set.
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