52 template <
typename GUM_SCALAR,
class TABLE >
62 const std::pair< bool, bool > empty_mark(
false,
false);
67 for (
const auto pot: tensors) {
69 for (
const auto var: vars) {
72 node2tensors[id].
insert(pot);
81 for (
const auto node: query) {
82 nodes_to_visit.
insert(std::pair< NodeId, bool >(node,
true));
89 while (!nodes_to_visit.
empty() && !node2tensors.
empty()) {
98 if (node2tensors.
exists(node)) {
99 auto& pot_set = node2tensors[node];
100 for (
const auto pot: pot_set) {
101 const auto& vars = pot->variablesSequence();
102 for (
const auto var: vars) {
105 node2tensors[id].
erase(pot);
106 if (node2tensors[
id].empty()) { node2tensors.
erase(
id); }
110 node2tensors.
erase(node);
114 if (node2tensors.
empty())
return;
119 if (nodes_to_visit.
front().second) {
122 if (hardEvidence.
exists(node)) {
continue; }
124 if (!marks[node].first) {
125 marks[node].first =
true;
126 for (
const auto par: dag.
parents(node)) {
127 nodes_to_visit.
insert(std::pair< NodeId, bool >(par,
true));
131 if (!marks[node].second) {
132 marks[node].second =
true;
133 for (
const auto chi: dag.
children(node)) {
134 nodes_to_visit.
insert(std::pair< NodeId, bool >(chi,
false));
140 const bool is_hard_evidence = hardEvidence.
exists(node);
141 const bool is_evidence = is_hard_evidence || softEvidence.
exists(node);
143 if (is_evidence && !marks[node].first) {
144 marks[node].first =
true;
146 for (
const auto par: dag.
parents(node)) {
147 nodes_to_visit.
insert(std::pair< NodeId, bool >(par,
true));
151 if (!is_hard_evidence && !marks[node].second) {
152 marks[node].second =
true;
154 for (
const auto chi: dag.
children(node)) {
155 nodes_to_visit.
insert(std::pair< NodeId, bool >(chi,
false));
164 for (
const auto& elt: node2tensors) {
165 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
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 & 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 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