aGrUM  0.16.0
BayesBall.cpp
Go to the documentation of this file.
1 
29 
30 #ifdef GUM_NO_INLINE
32 #endif // GUM_NO_INLINE
33 
34 namespace gum {
35 
36  void BayesBall::requisiteNodes(const DAG& dag,
37  const NodeSet& query,
38  const NodeSet& hardEvidence,
39  const NodeSet& softEvidence,
40  NodeSet& requisite) {
41  // for the moment, no node is requisite
42  requisite.clear();
43 
44  // create the marks (top = first and bottom = second )
46  const std::pair< bool, bool > empty_mark(false, false);
47 
48  // indicate that we will send the ball to all the query nodes (as children):
49  // in list nodes_to_visit, the first element is the next node to send the
50  // ball to and the Boolean indicates whether we shall reach it from one of
51  // its children (true) or from one parent (false)
52  List< std::pair< NodeId, bool > > nodes_to_visit;
53  for (const auto node : query) {
54  nodes_to_visit.insert(std::pair< NodeId, bool >(node, true));
55  }
56 
57  // perform the bouncing ball until there is no node in the graph to send
58  // the ball to
59  while (!nodes_to_visit.empty()) {
60  // get the next node to visit
61  NodeId node = nodes_to_visit.front().first;
62 
63  // if the marks of the node do not exist, create them
64  if (!marks.exists(node)) marks.insert(node, empty_mark);
65 
66  // bounce the ball toward the neighbors
67  if (nodes_to_visit.front().second) { // visit from a child
68  nodes_to_visit.popFront();
69  requisite.insert(node);
70 
71  if (hardEvidence.exists(node)) { continue; }
72 
73  if (!marks[node].first) {
74  marks[node].first = true; // top marked
75  for (const auto par : dag.parents(node)) {
76  nodes_to_visit.insert(std::pair< NodeId, bool >(par, true));
77  }
78  }
79 
80  if (!marks[node].second) {
81  marks[node].second = true; // bottom marked
82  for (const auto chi : dag.children(node)) {
83  nodes_to_visit.insert(std::pair< NodeId, bool >(chi, false));
84  }
85  }
86  } else { // visit from a parent
87  nodes_to_visit.popFront();
88 
89  const bool is_hard_evidence = hardEvidence.exists(node);
90  const bool is_evidence = is_hard_evidence || softEvidence.exists(node);
91 
92  if (is_evidence && !marks[node].first) {
93  marks[node].first = true;
94  requisite.insert(node);
95 
96  for (const auto par : dag.parents(node)) {
97  nodes_to_visit.insert(std::pair< NodeId, bool >(par, true));
98  }
99  }
100 
101  if (!is_hard_evidence && !marks[node].second) {
102  marks[node].second = true;
103 
104  for (const auto chi : dag.children(node)) {
105  nodes_to_visit.insert(std::pair< NodeId, bool >(chi, false));
106  }
107  }
108  }
109  }
110  }
111 
112 } /* namespace gum */
bool empty() const noexcept
Returns a boolean indicating whether the chained list is empty.
Definition: list_tpl.h:1970
Size size() const
alias for sizeNodes
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Generic doubly linked lists.
Definition: list.h:372
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
void popFront()
Removes the first element of a List, if any.
Definition: list_tpl.h:1964
The class for generic Hash Tables.
Definition: hashTable.h:679
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:607
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).
Definition: list_tpl.h:1619
Val & front() const
Returns a reference to first element of a list, if any.
Definition: list_tpl.h:1831
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void clear()
Removes all the elements, if any, from the set.
Definition: set_tpl.h:375
Base class for dag.
Definition: DAG.h:102
Size NodeId
Type for node ids.
Definition: graphElements.h:98
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
static void requisiteNodes(const DAG &dag, const NodeSet &query, const NodeSet &hardEvidence, const NodeSet &softEvidence, NodeSet &requisite)
Fill the &#39;requisite&#39; nodeset with the requisite nodes in dag given a query and evidence.
Definition: BayesBall.cpp:36