33 template <
typename GUM_SCALAR,
template <
typename >
class TABLE >
39 Set<
const TABLE< GUM_SCALAR >* >& potentials) {
45 const std::pair< bool, bool > empty_mark(
false,
false);
50 for (
const auto pot : potentials) {
52 for (
const auto var : vars) {
54 if (!node2potentials.
exists(
id)) {
55 node2potentials.
insert(
id,
Set<
const TABLE< GUM_SCALAR >* >());
57 node2potentials[id].
insert(pot);
66 for (
const auto node : query) {
67 nodes_to_visit.
insert(std::pair< NodeId, bool >(node,
true));
74 while (!nodes_to_visit.
empty() && !node2potentials.
empty()) {
83 if (node2potentials.
exists(node)) {
84 auto& pot_set = node2potentials[node];
85 for (
const auto pot : pot_set) {
86 const auto& vars = pot->variablesSequence();
87 for (
const auto var : vars) {
90 node2potentials[id].
erase(pot);
91 if (node2potentials[
id].empty()) { node2potentials.
erase(
id); }
95 node2potentials.
erase(node);
99 if (node2potentials.
empty())
return;
104 if (nodes_to_visit.
front().second) {
107 if (hardEvidence.
exists(node)) {
continue; }
109 if (!marks[node].first) {
110 marks[node].first =
true;
111 for (
const auto par : dag.
parents(node)) {
112 nodes_to_visit.
insert(std::pair< NodeId, bool >(par,
true));
116 if (!marks[node].second) {
117 marks[node].second =
true;
118 for (
const auto chi : dag.
children(node)) {
119 nodes_to_visit.
insert(std::pair< NodeId, bool >(chi,
false));
125 const bool is_hard_evidence = hardEvidence.
exists(node);
126 const bool is_evidence = is_hard_evidence || softEvidence.
exists(node);
128 if (is_evidence && !marks[node].first) {
129 marks[node].first =
true;
131 for (
const auto par : dag.
parents(node)) {
132 nodes_to_visit.
insert(std::pair< NodeId, bool >(par,
true));
136 if (!is_hard_evidence && !marks[node].second) {
137 marks[node].second =
true;
139 for (
const auto chi : dag.
children(node)) {
140 nodes_to_visit.
insert(std::pair< NodeId, bool >(chi,
false));
149 for (
const auto elt : node2potentials) {
150 for (
const auto pot : elt.second) {
151 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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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.