aGrUM  0.16.0
structuredBayesBall_tpl.h
Go to the documentation of this file.
1 
31 
32 namespace gum {
33  namespace prm {
34 
35  template < typename GUM_SCALAR >
37  GUM_DESTRUCTOR(StructuredBayesBall);
38 
39  for (const auto& elt : __reqMap)
40  delete elt.second.first;
41  }
42 
43  template < typename GUM_SCALAR >
45  for (const auto& elt : __reqMap)
46  delete elt.second.first;
47 
48  __keyMap.clear();
49  __reqMap.clear();
50  }
51 
52  template < typename GUM_SCALAR >
54  const PRMInstance< GUM_SCALAR >* i, NodeId n) {
55  try {
56  typename PRMInference< GUM_SCALAR >::Chain chain =
57  std::make_pair(i, &(i->get(n)));
58 
59  if (__inf->hasEvidence(chain)) {
60  const Potential< GUM_SCALAR >* e = __inf->evidence(i)[n];
61  Instantiation inst(e);
62  Size count = 0;
63 
64  for (inst.setFirst(); !inst.end(); inst.inc()) {
65  if ((e->get(inst) == (GUM_SCALAR)1.0))
66  ++count;
67  else if (e->get(inst) != (GUM_SCALAR)0.0)
68  return false;
69  }
70 
71  return (count == 1);
72  }
73 
74  return false;
75  } catch (NotFound&) { return false; }
76  }
77 
78  template < typename GUM_SCALAR >
80  const PRMInstance< GUM_SCALAR >* i, NodeId n) {
81  __clean();
85  __fromChild(i, n, marks);
86  __fillMaps(marks);
87 
88  for (const auto& elt : marks)
89  delete elt.second;
90  }
91 
92  template < typename GUM_SCALAR >
94  const PRMInstance< GUM_SCALAR >* i, NodeId n, InstanceMap& marks) {
95  if (!marks.exists(i)) {
97  }
98 
99  if (!marks[i]->exists(n)) {
100  marks[i]->insert(n, std::pair< bool, bool >(false, false));
101  }
102 
103  // Sending message to parents
104  switch (i->type().get(n).elt_type()) {
106  if (!__getMark(marks, i, n).first) {
107  __getMark(marks, i, n).first = true;
108 
109  for (const auto inst : i->getInstances(n))
110  __fromChild(
111  inst, inst->get(__getSC(i, n).lastElt().safeName()).id(), marks);
112  }
113 
114  if (!__getMark(marks, i, n).second) {
115  __getMark(marks, i, n).second = true;
116 
117  for (const auto chi : i->type().containerDag().children(n))
118  __fromParent(i, chi, marks);
119  }
120 
121  break;
122  }
123 
126  if (!__getMark(marks, i, n).first) {
127  __getMark(marks, i, n).first = true;
128 
129  if (!__isHardEvidence(i, n))
130  for (const auto par : i->type().containerDag().parents(n))
131  __fromChild(i, par, marks);
132  }
133 
134  if (!__getMark(marks, i, n).second) {
135  __getMark(marks, i, n).second = true;
136 
137  // In i.
138  for (const auto chi : i->type().containerDag().children(n))
139  __fromParent(i, chi, marks);
140 
141  // Out of i.
142  try {
143  const auto& refs = i->getRefAttr(n);
144 
145  for (auto iter = refs.begin(); iter != refs.end(); ++iter)
146  __fromParent(
147  iter->first, iter->first->type().get(iter->second).id(), marks);
148  } catch (NotFound&) {
149  // Not an inverse sc
150  }
151  }
152 
153  break;
154  }
155 
156  default: {
157  // We shouldn't reach any other PRMClassElement<GUM_DATA> than
158  // PRMAttribute
159  // or
160  // PRMSlotChain<GUM_SCALAR>.
161  GUM_ERROR(FatalError, "This case is impossible.");
162  }
163  }
164  }
165 
166  template < typename GUM_SCALAR >
168  const PRMInstance< GUM_SCALAR >* i, NodeId n, InstanceMap& marks) {
169  if (!marks.exists(i)) {
171  }
172 
173  if (!marks[i]->exists(n)) {
174  marks[i]->insert(n, std::pair< bool, bool >(false, false));
175  }
176 
177  // Concerns only PRMAttribute (because of the hard evidence)
178  if ((__isHardEvidence(i, n)) && (!__getMark(marks, i, n).first)) {
179  __getMark(marks, i, n).first = true;
180 
181  for (const auto par : i->type().containerDag().parents(n))
182  __fromChild(i, par, marks);
183  } else if (!__getMark(marks, i, n).second) {
184  __getMark(marks, i, n).second = true;
185 
186  // In i.
187  for (const auto chi : i->type().containerDag().children(n))
188  __fromParent(i, chi, marks);
189 
190  // Out of i.
191  try {
192  for (auto iter = i->getRefAttr(n).begin();
193  iter != i->getRefAttr(n).end();
194  ++iter)
195  __fromParent(
196  iter->first, iter->first->type().get(iter->second).id(), marks);
197  } catch (NotFound&) {
198  // Not an inverse sc
199  }
200  }
201  }
202 
203  template < typename GUM_SCALAR >
205  // First find for each instance it's requisite nodes
207 
208  for (const auto& elt : marks) {
209  Set< NodeId >* req_set = new Set< NodeId >();
210 
211  for (const auto& elt2 : *elt.second)
212  if (elt2.second.first) req_set->insert(elt2.first);
213 
214  req_map.insert(elt.first, req_set);
215  }
216 
217  // Remove all instances with 0 requisite nodes
219 
220  for (const auto& elt : req_map)
221  if (elt.second->size() == 0) to_remove.insert(elt.first);
222 
223  for (const auto remo : to_remove) {
224  delete req_map[remo];
225  req_map.erase(remo);
226  }
227 
228  // Fill __reqMap and __keyMap
229  for (const auto& elt : req_map) {
230  std::string key = __buildHashKey(elt.first, *elt.second);
231 
232  if (__reqMap.exists(key)) {
233  __keyMap.insert(
234  elt.first,
235  std::pair< std::string, Set< NodeId >* >(key, __reqMap[key].first));
236  __reqMap[key].second += 1;
237  delete elt.second;
238  req_map[elt.first] = 0;
239  } else {
240  __reqMap.insert(key, std::pair< Set< NodeId >*, Size >(elt.second, 1));
241  __keyMap.insert(
242  elt.first, std::pair< std::string, Set< NodeId >* >(key, elt.second));
243  }
244  }
245  }
246 
247  template < typename GUM_SCALAR >
249  const PRMInstance< GUM_SCALAR >* i, Set< NodeId >& req_nodes) {
250  std::stringstream sBuff;
251  sBuff << i->type().name();
252 
253  for (const auto node : i->type().containerDag().nodes())
254  if (req_nodes.exists(node)) sBuff << "-" << node;
255 
256  return sBuff.str();
257  }
258 
259  template < typename GUM_SCALAR >
261  const PRMInference< GUM_SCALAR >& inference) :
262  __inf(&inference) {
263  GUM_CONSTRUCTOR(StructuredBayesBall);
264  }
265 
266  template < typename GUM_SCALAR >
268  const StructuredBayesBall< GUM_SCALAR >& source) :
269  __inf(0) {
270  GUM_CONS_CPY(StructuredBayesBall);
271  GUM_ERROR(FatalError, "Not allowed.");
272  }
273 
274  template < typename GUM_SCALAR >
277  GUM_ERROR(FatalError, "Not allowed.");
278  }
279 
280  template < typename GUM_SCALAR >
281  INLINE const std::string& StructuredBayesBall< GUM_SCALAR >::key(
282  const PRMInstance< GUM_SCALAR >* i) const {
283  return __keyMap[i].first;
284  }
285 
286  template < typename GUM_SCALAR >
287  INLINE const std::string& StructuredBayesBall< GUM_SCALAR >::key(
288  const PRMInstance< GUM_SCALAR >& i) const {
289  return __keyMap[&i].first;
290  }
291 
292  template < typename GUM_SCALAR >
294  const PRMInstance< GUM_SCALAR >* i) const {
295  return *(__keyMap[i].second);
296  }
297 
298  template < typename GUM_SCALAR >
300  const PRMInstance< GUM_SCALAR >& i) const {
301  return *(__keyMap[&i].second);
302  }
303 
304  template < typename GUM_SCALAR >
306  const std::string& key) const {
307  return __reqMap[key].second;
308  }
309 
310  template < typename GUM_SCALAR >
312  return ((float)__reqMap.size()) / ((float)__keyMap.size());
313  }
314 
315  template < typename GUM_SCALAR >
317  const PRMInstance< GUM_SCALAR >* i) const {
318  return __keyMap.exists(i);
319  }
320 
321  template < typename GUM_SCALAR >
323  const PRMInstance< GUM_SCALAR >& i) const {
324  return __keyMap.exists(&i);
325  }
326 
327  template < typename GUM_SCALAR >
329  const PRMInstance< GUM_SCALAR >* i, NodeId n) {
330  __compute(i, n);
331  }
332 
333  template < typename GUM_SCALAR >
335  const PRMInstance< GUM_SCALAR >& i, NodeId n) {
336  __compute(&i, n);
337  }
338 
339  template < typename GUM_SCALAR >
340  INLINE const PRMSlotChain< GUM_SCALAR >&
342  const PRMInstance< GUM_SCALAR >* i, NodeId n) {
343  return static_cast< const PRMSlotChain< GUM_SCALAR >& >(i->type().get(n));
344  }
345 
346  template < typename GUM_SCALAR >
347  INLINE std::pair< bool, bool >& StructuredBayesBall< GUM_SCALAR >::__getMark(
348  InstanceMap& marks, const PRMInstance< GUM_SCALAR >* i, NodeId n) {
349  return (*(marks[i]))[n];
350  }
351 
352  } /* namespace prm */
353 } /* namespace gum */
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:60
<agrum/PRM/structuredBayesBall.h>
PRMAttribute< GUM_SCALAR > & get(NodeId id)
Getter on an PRMAttribute<GUM_SCALAR> of this PRMInstance<GUM_SCALAR>.
virtual GUM_SCALAR get(const Instantiation &i) const final
Default implementation of MultiDimContainer::get().
const Set< NodeId > & requisiteNodes(const PRMInstance< GUM_SCALAR > *i) const
Returns the set of requisite nodes w.r.t. d-separation for i.
An PRMInstance is a Bayesian Network fragment defined by a Class and used in a PRMSystem.
Definition: PRMInstance.h:63
PRMClass< GUM_SCALAR > & type()
Returns the type of this instance.
Abstract class representing an element of PRM class.
std::pair< bool, bool > & __getMark(InstanceMap &marks, const PRMInstance< GUM_SCALAR > *i, NodeId n)
Code alias.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
void compute(const PRMInstance< GUM_SCALAR > *i, NodeId n)
Compute the set or requisite nodes for each required instance given the current set of observations...
bool __isHardEvidence(const PRMInstance< GUM_SCALAR > *i, NodeId n)
Returns true if there is a hard evidence on i->get(n).
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
HashTable< const PRMInstance< GUM_SCALAR > *, std::pair< std::string, Set< NodeId > *> > __keyMap
Associate an PRMInstance<GUM_SCALAR> with a unique key w.r.t. d-separation and the set of requisite n...
The class for generic Hash Tables.
Definition: hashTable.h:679
HashTable< std::string, std::pair< Set< NodeId > *, Size > > __reqMap
Associate a Key with the set of requisite nodes associated with it. The Size value is the number of i...
void __fillMaps(InstanceMap &marks)
Fill __keyMap and __reqMap.
bool exists(const PRMInstance< GUM_SCALAR > *i) const
Returns true if i has requisite nodes.
std::vector< std::pair< PRMInstance< GUM_SCALAR > *, std::string > > & getRefAttr(NodeId id)
Returns a vector of pairs of refering attributes of id.
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:607
void inc()
Operator increment.
void __fromChild(const PRMInstance< GUM_SCALAR > *i, NodeId n, InstanceMap &marks)
When the ball is received on i->get(n) from a child.
Size occurrence(const std::string &key) const
Returns the number of occurrence of the given key, which is the number of PRMInstance<GUM_SCALAR> sha...
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
StructuredBayesBall & operator=(const StructuredBayesBall &source)
Copy operator.
std::string __buildHashKey(const PRMInstance< GUM_SCALAR > *i, Set< NodeId > &req_nodes)
Builds the HashKey for the given instance and requisite nodes set.
A PRMSlotChain represents a sequence of gum::prm::PRMClassElement<GUM_SCALAR> where the n-1 first gum...
Definition: PRMObject.h:221
const std::string & key(const PRMInstance< GUM_SCALAR > *i) const
Returns a unique key w.r.t. d-separation for i.
void __clean()
Cleans this before a new computation.
const PRMSlotChain< GUM_SCALAR > & __getSC(const PRMInstance< GUM_SCALAR > *i, NodeId n)
Code alias.
std::pair< const PRMInstance< GUM_SCALAR > *, const PRMAttribute< GUM_SCALAR > *> Chain
Code alias.
Definition: PRMInference.h:57
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:83
This abstract class is used as base class for all inference class on PRM<GUM_SCALAR>.
Definition: PRMInference.h:52
StructuredBayesBall(const PRMInference< GUM_SCALAR > &inference)
Default Constructor.
void setFirst()
Assign the first values to the tuple of the Instantiation.
const PRMInference< GUM_SCALAR > * __inf
The PRM at which __model belongs.
void __compute(const PRMInstance< GUM_SCALAR > *i, NodeId n)
The real compute method.
float liftRatio() const
Returns the ratio between the total number of instances and the number of instances with the same con...
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
void __fromParent(const PRMInstance< GUM_SCALAR > *i, NodeId n, InstanceMap &marks)
When the ball is receive on i->get(n) from a parent.
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
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
const Set< PRMInstance< GUM_SCALAR > *> & getInstances(NodeId id) const
Returns the Set of PRMInstance<GUM_SCALAR> referenced by id.
bool end() const
Returns true if the Instantiation reached the end.