30 template <
typename GUM_SCALAR,
template <
typename >
class TABLE >
36 Set<
const TABLE< GUM_SCALAR >* >& potentials) {
42 const std::pair< bool, bool > empty_mark(
false,
false);
47 for (
const auto pot : potentials) {
49 for (
const auto var : vars) {
51 if (!node2potentials.
exists(
id)) {
52 node2potentials.
insert(
id,
Set<
const TABLE< GUM_SCALAR >* >());
54 node2potentials[id].
insert(pot);
63 for (
const auto node : query) {
64 nodes_to_visit.
insert(std::pair< NodeId, bool >(node,
true));
71 while (!nodes_to_visit.
empty() && !node2potentials.
empty()) {
80 if (node2potentials.
exists(node)) {
81 auto& pot_set = node2potentials[node];
82 for (
const auto pot : pot_set) {
83 const auto& vars = pot->variablesSequence();
84 for (
const auto var : vars) {
87 node2potentials[id].
erase(pot);
88 if (node2potentials[
id].empty()) { node2potentials.
erase(
id); }
92 node2potentials.
erase(node);
96 if (node2potentials.
empty())
return;
101 if (nodes_to_visit.
front().second) {
104 if (hardEvidence.
exists(node)) {
continue; }
106 if (!marks[node].first) {
107 marks[node].first =
true;
108 for (
const auto par : dag.
parents(node)) {
109 nodes_to_visit.
insert(std::pair< NodeId, bool >(par,
true));
113 if (!marks[node].second) {
114 marks[node].second =
true;
115 for (
const auto chi : dag.
children(node)) {
116 nodes_to_visit.
insert(std::pair< NodeId, bool >(chi,
false));
122 const bool is_hard_evidence = hardEvidence.
exists(node);
123 const bool is_evidence = is_hard_evidence || softEvidence.
exists(node);
125 if (is_evidence && !marks[node].first) {
126 marks[node].first =
true;
128 for (
const auto par : dag.
parents(node)) {
129 nodes_to_visit.
insert(std::pair< NodeId, bool >(par,
true));
133 if (!is_hard_evidence && !marks[node].second) {
134 marks[node].second =
true;
136 for (
const auto chi : dag.
children(node)) {
137 nodes_to_visit.
insert(std::pair< NodeId, bool >(chi,
false));
146 for (
const auto elt : node2potentials) {
147 for (
const auto pot : elt.second) {
148 potentials.erase(pot);
void resize(Size new_size)
Changes the number of slots in the 'nodes' vector of the hash table.
bool empty() const noexcept
Returns a boolean indicating whether the chained list is empty.
void erase(const Key &key)
Removes a given element from the hash table.
Size size() const
alias for sizeNodes
The generic class for storing (ordered) sequences of objects.
static void relevantPotentials(const IBayesNet< GUM_SCALAR > &bn, const NodeSet &query, const NodeSet &hardEvidence, const NodeSet &softEvidence, Set< const TABLE< GUM_SCALAR > * > &potentials)
update a set of potentials, keeping only those d-connected with query variables given evidence ...
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
Generic doubly linked lists.
Class representing the minimal interface for Bayesian Network.
gum is the global namespace for all aGrUM entities
void popFront()
Removes the first element of a List, if any.
The class for generic Hash Tables.
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Val & insert(const Val &val)
Inserts a new element at the end of the chained list (alias of pushBack).
Val & front() const
Returns a reference to first element of a list, if any.
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
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.
virtual NodeId nodeId(const DiscreteVariable &var) const =0
Return id node from discrete var pointer.
const DAG & dag() const
Returns a constant reference to the dag of this Bayes Net.
Size NodeId
Type for node ids.