33 template <
typename GUM_SCALAR,
template <
typename >
class TABLE >
39 Set<
const TABLE< GUM_SCALAR >* >& potentials) {
46 for (
const auto node : hardEvidence)
48 for (
const auto node : softEvidence)
50 while (!anc_to_visit.
empty()) {
54 if (!ev_ancestors.exists(node)) {
55 ev_ancestors.insert(node);
56 for (
const auto par : dag.
parents(node)) {
70 for (
const auto pot : potentials) {
72 for (
const auto var : vars) {
74 if (!node2potentials.exists(
id)) {
75 node2potentials.insert(
id,
Set<
const TABLE< GUM_SCALAR >* >());
77 node2potentials[id].insert(pot);
86 for (
const auto node : query) {
87 nodes_to_visit.
insert(std::pair< NodeId, bool >(node,
true));
92 while (!nodes_to_visit.
empty() && !node2potentials.empty()) {
95 const bool direction = nodes_to_visit.
front().second;
101 already_visited = visited_from_child.exists(node);
102 if (!already_visited) { visited_from_child.insert(node); }
104 already_visited = visited_from_parent.exists(node);
105 if (!already_visited) { visited_from_parent.insert(node); }
110 if (node2potentials.exists(node)) {
111 auto& pot_set = node2potentials[node];
112 for (
const auto pot : pot_set) {
113 const auto& vars = pot->variablesSequence();
114 for (
const auto var : vars) {
117 node2potentials[id].erase(pot);
118 if (node2potentials[
id].empty()) { node2potentials.erase(
id); }
122 node2potentials.erase(node);
126 if (node2potentials.empty())
return;
130 if (!already_visited) {
132 const bool is_hard_evidence = hardEvidence.
exists(node);
135 if (direction && !is_hard_evidence) {
137 for (
const auto par : dag.
parents(node)) {
138 nodes_to_visit.
insert(std::pair< NodeId, bool >(par,
true));
142 for (
const auto chi : dag.
children(node)) {
143 nodes_to_visit.
insert(std::pair< NodeId, bool >(chi,
false));
146 if (!hardEvidence.
exists(node)) {
148 for (
const auto chi : dag.
children(node)) {
149 nodes_to_visit.
insert(std::pair< NodeId, bool >(chi,
false));
152 if (ev_ancestors.exists(node)) {
154 for (
const auto par : dag.
parents(node)) {
155 nodes_to_visit.
insert(std::pair< NodeId, bool >(par,
true));
164 for (
const auto elt : node2potentials) {
165 for (
const auto pot : elt.second) {
166 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.
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
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.