36 template <
typename GUM_SCALAR,
template <
typename >
class TABLE >
42 Set<
const TABLE< GUM_SCALAR >* >& potentials) {
49 for (
const auto node : hardEvidence)
51 for (
const auto node : softEvidence)
53 while (!anc_to_visit.
empty()) {
57 if (!ev_ancestors.exists(node)) {
58 ev_ancestors.insert(node);
59 for (
const auto par : dag.
parents(node)) {
73 for (
const auto pot : potentials) {
75 for (
const auto var : vars) {
77 if (!node2potentials.exists(
id)) {
78 node2potentials.insert(
id,
Set<
const TABLE< GUM_SCALAR >* >());
80 node2potentials[id].insert(pot);
89 for (
const auto node : query) {
90 nodes_to_visit.
insert(std::pair< NodeId, bool >(node,
true));
95 while (!nodes_to_visit.
empty() && !node2potentials.empty()) {
98 const bool direction = nodes_to_visit.
front().second;
102 bool already_visited;
104 already_visited = visited_from_child.exists(node);
105 if (!already_visited) { visited_from_child.insert(node); }
107 already_visited = visited_from_parent.exists(node);
108 if (!already_visited) { visited_from_parent.insert(node); }
113 if (node2potentials.exists(node)) {
114 auto& pot_set = node2potentials[node];
115 for (
const auto pot : pot_set) {
116 const auto& vars = pot->variablesSequence();
117 for (
const auto var : vars) {
120 node2potentials[id].erase(pot);
121 if (node2potentials[
id].empty()) { node2potentials.erase(
id); }
125 node2potentials.erase(node);
129 if (node2potentials.empty())
return;
133 if (!already_visited) {
135 const bool is_hard_evidence = hardEvidence.
exists(node);
138 if (direction && !is_hard_evidence) {
140 for (
const auto par : dag.
parents(node)) {
141 nodes_to_visit.
insert(std::pair< NodeId, bool >(par,
true));
145 for (
const auto chi : dag.
children(node)) {
146 nodes_to_visit.
insert(std::pair< NodeId, bool >(chi,
false));
149 if (!hardEvidence.
exists(node)) {
151 for (
const auto chi : dag.
children(node)) {
152 nodes_to_visit.
insert(std::pair< NodeId, bool >(chi,
false));
155 if (ev_ancestors.exists(node)) {
157 for (
const auto par : dag.
parents(node)) {
158 nodes_to_visit.
insert(std::pair< NodeId, bool >(par,
true));
167 for (
const auto elt : node2potentials) {
168 for (
const auto pot : elt.second) {
169 potentials.erase(pot);
bool empty() const noexcept
Returns a boolean indicating whether the chained list is empty.
Size size() const
alias for sizeNodes
The generic class for storing (ordered) sequences of objects.
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 ...
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
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.