aGrUM  0.16.0
SVE_tpl.h
Go to the documentation of this file.
1 
31 
32 namespace gum {
33  namespace prm {
34 
35  template < typename GUM_SCALAR >
37  const PRMAttribute< GUM_SCALAR >& a) {
38  std::stringstream s;
39  const auto& class_a = i.type().get(a.safeName());
40  s << &(a.type().variable()) << " - ";
41  s << i.name() << "." << a.safeName()
42  << ": input=" << i.type().isInputNode(class_a);
43  s << " output=" << i.type().isOutputNode(class_a)
44  << " inner=" << i.type().isInnerNode(class_a);
45  return s.str();
46  }
47 
48  template < typename GUM_SCALAR >
50  std::stringstream s;
51  s << i.name() << std::endl;
52  s << "Attributes: " << std::endl;
53  for (auto a : i) {
54  s << __print_attribute__(i, *(a.second));
55  }
56  if (i.type().slotChains().size()) {
57  s << std::endl << "SlotChains: " << std::endl;
58  for (auto sc : i.type().slotChains()) {
59  s << sc->name() << " ";
60  }
61  }
62  return s.str();
63  }
64 
65  template < typename GUM_SCALAR >
66  std::string __print_system__(const PRMSystem< GUM_SCALAR >& s) {
67  std::stringstream str;
68  for (auto i : s) {
69  str << __print_instance__(*(i.second)) << std::endl;
70  }
71  return str.str();
72  }
73 
74  template < typename LIST >
75  std::string __print_list__(LIST l) {
76  std::stringstream s;
77  s << "[";
78  for (auto i : l) {
79  s << i->name() << " ";
80  }
81  s << "]";
82  return s.str();
83  }
84 
85  template < typename GUM_SCALAR >
86  std::string __print_pot__(const Potential< GUM_SCALAR >& pot) {
87  std::stringstream s;
88  s << "{";
89  for (auto var : pot.variablesSequence()) {
90  s << var << ", ";
91  }
92  s << "}";
93  return s.str();
94  }
95 
96  template < typename SET >
97  std::string __print_set__(SET set) {
98  std::stringstream s;
99  s << "[";
100  for (auto p : set) {
101  s << __print_pot__(*p) << " ";
102  }
103  s << "]";
104  return s.str();
105  }
106 
107  template < typename GUM_SCALAR >
109  GUM_DESTRUCTOR(SVE);
110 
111  for (const auto& elt : __elim_orders)
112  delete elt.second;
113 
114  for (const auto& elt : __lifted_pools)
115  delete elt.second;
116 
117  if (__class_elim_order != nullptr) delete __class_elim_order;
118 
119  for (const auto trash : __lifted_trash)
120  delete trash;
121 
122  for (auto set : __delayedVariables)
123  delete set.second;
124  }
125 
126  template < typename GUM_SCALAR >
127  void
129  NodeId node,
130  BucketSet& pool,
131  BucketSet& trash) {
132  Set< const PRMInstance< GUM_SCALAR >* > ignore, eliminated;
133  Set< NodeId > delayedVars;
134  // Downward elimination
136  ignore.insert(query);
137 
138  for (auto iter = query->beginInvRef(); iter != query->endInvRef(); ++iter) {
139  for (auto child = (*(iter.val())).begin(); child != (*(iter.val())).end();
140  ++child) {
141  if (!ignore.exists(child->first)) {
142  __eliminateNodesDownward(
143  query, child->first, pool, trash, elim_list, ignore, eliminated);
144  } else if (!eliminated.exists(child->first)) {
145  __addDelayedVariable(child->first, query, iter.key());
146  delayedVars.insert(iter.key());
147  }
148  }
149  }
150 
151  // Eliminating all nodes in query instance, except query
153  DefaultTriangulation t(&(bn.moralGraph()), &(bn.modalities()));
154  std::vector< const DiscreteVariable* > elim_order;
155 
156  if (this->hasEvidence(query)) { __insertEvidence(query, pool); }
157 
158  for (auto attr = query->begin(); attr != query->end(); ++attr) {
159  pool.insert(
160  &(const_cast< Potential< GUM_SCALAR >& >((*(attr.val())).cpf())));
161  }
162 
163  for (size_t idx = 0; idx < t.eliminationOrder().size(); ++idx) {
164  if ((t.eliminationOrder()[idx] != node)
165  && (!delayedVars.exists(t.eliminationOrder()[idx]))) {
166  auto var_id = t.eliminationOrder()[idx];
167  const auto& var = bn.variable(var_id);
168  elim_order.push_back(&var);
169  }
170  }
171 
172  eliminateNodes(elim_order, pool, trash);
173 
174  // Eliminating delayed variables, if any
175  if (__delayedVariables.exists(query)) {
176  __eliminateDelayedVariables(query, pool, trash);
177  }
178 
179  eliminated.insert(query);
180  // Eliminating instance in elim_list
182 
183  while (!elim_list.empty()) {
184  if (__checkElimOrder(query, elim_list.front())) {
185  if (!ignore.exists(elim_list.front())) {
186  __eliminateNodesDownward(query,
187  elim_list.front(),
188  pool,
189  trash,
190  elim_list,
191  ignore,
192  eliminated);
193  }
194  } else {
195  tmp_list.insert(elim_list.front());
196  }
197 
198  elim_list.popFront();
199  }
200 
201  // Upward elimination
202  for (const auto chain : query->type().slotChains())
203  for (const auto parent : query->getInstances(chain->id()))
204  if (!ignore.exists(parent))
205  __eliminateNodesUpward(
206  parent, pool, trash, tmp_list, ignore, eliminated);
207  }
208 
209  template < typename GUM_SCALAR >
211  const PRMInstance< GUM_SCALAR >* i, BucketSet& pool, BucketSet& trash) {
212  Set< Potential< GUM_SCALAR >* > toRemove;
213 
214  for (const auto var : *__delayedVariables[i]) {
216 
217  for (const auto pot : pool)
218  if (pot->contains(*var)) {
219  bucket->add(*pot);
220  toRemove.insert(pot);
221  }
222 
223  for (const auto pot : toRemove)
224  pool.erase(pot);
225 
226  for (const auto other : bucket->allVariables())
227  if (other != var) bucket->add(*other);
228 
229  Potential< GUM_SCALAR >* bucket_pot = new Potential< GUM_SCALAR >(bucket);
230  trash.insert(bucket_pot);
231  pool.insert(bucket_pot);
232  }
233  }
234 
235  template < typename GUM_SCALAR >
237  const PRMInstance< GUM_SCALAR >* from,
238  const PRMInstance< GUM_SCALAR >* i,
239  BucketSet& pool,
240  BucketSet& trash,
241  List< const PRMInstance< GUM_SCALAR >* >& elim_list,
242  Set< const PRMInstance< GUM_SCALAR >* >& ignore,
243  Set< const PRMInstance< GUM_SCALAR >* >& eliminated) {
244  Set< NodeId > delayedVars;
245  ignore.insert(i);
246  // Calling elimination over child instance
248 
249  for (auto iter = i->beginInvRef(); iter != i->endInvRef(); ++iter) {
250  for (auto child = (*(iter.val())).begin(); child != (*(iter.val())).end();
251  ++child) {
252  if (!ignore.exists(child->first)) {
253  __eliminateNodesDownward(
254  i, child->first, pool, trash, my_list, ignore, eliminated);
255  } else if (!eliminated.exists(child->first)) {
256  __addDelayedVariable(child->first, i, iter.key());
257  delayedVars.insert(iter.key());
258  }
259  }
260  }
261 
262  // Eliminating all nodes in current instance
263  __variableElimination(
264  i, pool, trash, (delayedVars.empty() ? 0 : &delayedVars));
265  eliminated.insert(i);
266 
267  // Calling elimination over child's parents
268  for (const auto node : my_list) {
269  if (__checkElimOrder(i, node) && (node != from)) {
270  if (!ignore.exists(node)) {
271  __eliminateNodesDownward(
272  i, node, pool, trash, elim_list, ignore, eliminated);
273  }
274  } else if (node != from) {
275  elim_list.insert(node);
276  }
277  }
278 
279  // Adding parents instance to elim_list
280  for (const auto chain : i->type().slotChains()) {
281  for (const auto inst : i->getInstances(chain->id())) {
282  if (inst != from) { elim_list.insert(inst); }
283  }
284  }
285  }
286 
287  template < typename GUM_SCALAR >
288  void
290  BucketSet& pool,
291  BucketSet& trash,
292  Set< NodeId >* delayedVars) {
293  if (this->hasEvidence(i)) {
294  __eliminateNodesWithEvidence(i, pool, trash, delayedVars);
295  } else {
296  __insertLiftedNodes(i, pool, trash);
297 
298  for (const auto agg : i->type().aggregates())
299  pool.insert(__getAggPotential(i, agg));
300 
301  try {
303 
304  std::vector< const DiscreteVariable* > elim;
305 
306  for (const auto node : __getElimOrder(i->type())) {
307  const auto& var = bn.variable(node);
308  if (delayedVars != nullptr) {
309  if (!delayedVars->exists(node)) {
310  const auto& var = bn.variable(node);
311  elim.push_back(&var);
312  }
313  } else {
314  elim.push_back(&var);
315  }
316  }
317 
318  eliminateNodes(elim, pool, trash);
319  } catch (NotFound&) {
320  // Raised if there is no inner nodes to eliminate
321  }
322  }
323 
324  // Eliminating delayed variables, if any
325  if (__delayedVariables.exists(i)) {
326  __eliminateDelayedVariables(i, pool, trash);
327  }
328  }
329 
330  template < typename GUM_SCALAR >
332  const PRMInstance< GUM_SCALAR >* i,
333  BucketSet& pool,
334  BucketSet& trash,
335  List< const PRMInstance< GUM_SCALAR >* >& elim_list,
336  Set< const PRMInstance< GUM_SCALAR >* >& ignore,
337  Set< const PRMInstance< GUM_SCALAR >* >& eliminated) {
338  // Downward elimination
339  ignore.insert(i);
340 
341  for (auto iter = i->beginInvRef(); iter != i->endInvRef(); ++iter) {
342  for (auto child = (*(iter.val())).begin(); child != (*(iter.val())).end();
343  ++child) {
344  if (!ignore.exists(child->first)) {
345  __eliminateNodesDownward(
346  i, child->first, pool, trash, elim_list, ignore, eliminated);
347  }
348  }
349  }
350 
351  // Eliminating all nodes in i instance
352  __variableElimination(i, pool, trash);
353  eliminated.insert(i);
354  // Eliminating instance in elim_list
356 
357  while (!elim_list.empty()) {
358  if (__checkElimOrder(i, elim_list.front())) {
359  if (!ignore.exists(elim_list.front())) {
360  __eliminateNodesDownward(
361  i, elim_list.front(), pool, trash, elim_list, ignore, eliminated);
362  }
363  } else {
364  tmp_list.insert(elim_list.front());
365  }
366 
367  elim_list.popFront();
368  }
369 
370  // Upward elimination
371  for (const auto chain : i->type().slotChains()) {
372  for (const auto parent : i->getInstances(chain->id())) {
373  if (!ignore.exists(parent)) {
374  __eliminateNodesUpward(
375  parent, pool, trash, tmp_list, ignore, eliminated);
376  }
377  }
378  }
379  }
380 
381  template < typename GUM_SCALAR >
383  const PRMInstance< GUM_SCALAR >* i,
384  BucketSet& pool,
385  BucketSet& trash,
386  Set< NodeId >* delayedVars) {
387  // First we check if evidences are on inner nodes
388  bool inner = false;
389 
390  for (const auto& elt : this->evidence(i)) {
391  inner = i->type().isInputNode(i->get(elt.first))
392  || i->type().isInnerNode(i->get(elt.first));
393 
394  if (inner) { break; }
395  }
396 
397  // Evidence on inner nodes
398  if (inner) {
399  BucketSet tmp_pool;
400  __insertEvidence(i, tmp_pool);
401 
402  // We need a local to not eliminate queried inner nodes of the same
403  // class
404  for (const auto& elt : *i) {
405  tmp_pool.insert(
406  &(const_cast< Potential< GUM_SCALAR >& >(elt.second->cpf())));
407  }
408 
410  DefaultTriangulation t(&(bn.moralGraph()), &(bn.modalities()));
411  const std::vector< NodeId >& full_elim_order = t.eliminationOrder();
412  // Removing Output nodes of elimination order
413  std::vector< const DiscreteVariable* > inner_elim_order;
414  std::vector< const DiscreteVariable* > output_elim_order;
415 
416  for (size_t idx = 0; idx < full_elim_order.size(); ++idx) {
417  auto var_id = full_elim_order[idx];
418  const auto& var = bn.variable(var_id);
419 
420  if (!i->type().isOutputNode(i->get(full_elim_order[idx]))) {
421  inner_elim_order.push_back(&var);
422  } else if (delayedVars != nullptr) {
423  if (!delayedVars->exists(full_elim_order[idx])) {
424  output_elim_order.push_back(&var);
425  }
426  } else {
427  output_elim_order.push_back(&var);
428  }
429  }
430 
431  eliminateNodes(inner_elim_order, tmp_pool, trash);
432 
433  // Now we add the new potentials in pool and eliminate output nodes
434  for (const auto pot : tmp_pool)
435  pool.insert(pot);
436 
437  if (!output_elim_order.empty())
438  eliminateNodes(output_elim_order, pool, trash);
439 
440  } else {
442  __insertEvidence(i, pool);
443  __insertLiftedNodes(i, pool, trash);
444 
445  for (const auto agg : i->type().aggregates())
446  pool.insert(__getAggPotential(i, agg));
447 
448  try {
449  std::vector< const DiscreteVariable* > elim;
450 
451  for (auto iter = __getElimOrder(i->type()).begin();
452  iter != __getElimOrder(i->type()).end();
453  ++iter) {
454  const auto& var = bn.variable(*iter);
455  if (delayedVars != nullptr) {
456  if (!delayedVars->exists(*iter)) { elim.push_back(&var); }
457  } else {
458  elim.push_back(&var);
459  }
460  }
461 
462  eliminateNodes(elim, pool, trash);
463  } catch (NotFound&) {
464  GUM_ERROR(FatalError, "there should be at least one node here.");
465  }
466  }
467  }
468 
469  template < typename GUM_SCALAR >
471  BucketSet& pool,
472  BucketSet& trash) {
473  SVE< GUM_SCALAR >::BucketSet* lifted_pool = 0;
474 
475  try {
476  lifted_pool = __lifted_pools[&(i->type())];
477  } catch (NotFound&) {
478  __initLiftedNodes(i->type());
479  lifted_pool = __lifted_pools[&(i->type())];
480  }
481 
482  for (const auto lifted_pot : *lifted_pool) {
483  Potential< GUM_SCALAR >* pot = copyPotential(i->bijection(), *lifted_pot);
484  pool.insert(pot);
485  trash.insert(pot);
486  }
487  }
488 
489  template < typename GUM_SCALAR >
491  BucketSet* lifted_pool = new BucketSet();
492  __lifted_pools.insert(&c, lifted_pool);
493  NodeSet inners, outers;
494 
495  for (const auto node : c.containerDag().nodes())
497  if (c.isOutputNode(c.get(node)))
498  outers.insert(node);
499  else if (!outers.exists(node))
500  inners.insert(node);
501 
502  lifted_pool->insert(
503  const_cast< Potential< GUM_SCALAR >* >(&(c.get(node).cpf())));
504  } else if (PRMClassElement< GUM_SCALAR >::isAggregate(c.get(node))) {
505  outers.insert(node);
506 
507  // We need to put in the output_elim_order aggregator's parents which
508  // are
509  // innner nodes
510  for (const auto par : c.containerDag().parents(node))
512  && c.isInnerNode(c.get(par))) {
513  inners.erase(par);
514  outers.insert(par);
515  }
516  }
517 
518  // Now we proceed with the elimination of inner attributes
520  List< NodeSet > partial_ordering;
521 
522  if (inners.size()) partial_ordering.push_back(inners);
523 
524  if (outers.size()) partial_ordering.push_back(outers);
525 
527  &(bn.moralGraph()), &(bn.modalities()), &partial_ordering);
528 
529  for (size_t idx = 0; idx < inners.size(); ++idx)
530  eliminateNode(&(c.get(t.eliminationOrder()[idx]).type().variable()),
531  *lifted_pool,
532  __lifted_trash);
533 
534  // If there is not only inner and input Attributes
535  if (outers.size()) {
536  __elim_orders.insert(
537  &c,
538  new std::vector< NodeId >(t.eliminationOrder().begin() + inners.size(),
539  t.eliminationOrder().end()));
540  }
541  }
542 
543  template < typename GUM_SCALAR >
545  ClassDependencyGraph< GUM_SCALAR > cdg(*(this->_prm));
547  std::list< NodeId > l;
548 
549  for (const auto node : cdg.dag().nodes()) {
550  if (cdg.dag().parents(node).empty()) { l.push_back(node); }
551  }
552 
553  Set< NodeId > visited_node;
554 
555  while (!l.empty()) {
556  visited_node.insert(l.front());
557 
558  if (!class_elim_order.exists(cdg.get(l.front()).first)) {
559  class_elim_order.insert(cdg.get(l.front()).first);
560  }
561 
562  for (const auto child : cdg.dag().children(l.front())) {
563  if (!visited_node.contains(child)) { l.push_back(child); }
564  }
565 
566  l.pop_front();
567  }
568 
569  __class_elim_order = new Sequence< std::string >();
570  for (auto c : class_elim_order) {
571  std::string name = c->name();
572  auto pos = name.find_first_of("<");
573  if (pos != std::string::npos) { name = name.substr(0, pos); }
574  try {
575  __class_elim_order->insert(name);
576  } catch (DuplicateElement&) {}
577  }
578  }
579 
580  template < typename GUM_SCALAR >
583  const PRMInstance< GUM_SCALAR >* i = chain.first;
584  const PRMAttribute< GUM_SCALAR >* elt = chain.second;
585  SVE< GUM_SCALAR >::BucketSet pool, trash;
586 
587  __eliminateNodes(i, elt->id(), pool, trash);
588 
589  std::vector< Potential< GUM_SCALAR >* > result;
590 
591  for (const auto pot : pool) {
592  if (pot->contains(elt->type().variable())) { result.push_back(pot); }
593  }
594 
595  while (result.size() > 1) {
596  auto& p1 = *(result.back());
597  result.pop_back();
598  auto& p2 = *(result.back());
599  result.pop_back();
600  auto mult = new Potential< GUM_SCALAR >(p1 * p2);
601  trash.insert(mult);
602  result.push_back(mult);
603  }
604 
605  m = *(result.back());
606  m.normalize();
607 
608  for (const auto pot : trash) {
609  delete pot;
610  }
611  }
612 
613  template < typename GUM_SCALAR >
614  void SVE< GUM_SCALAR >::_joint(const std::vector< Chain >& queries,
616  GUM_ERROR(FatalError, "Not implemented.");
617  }
618 
619  template < typename GUM_SCALAR >
621  const PRMSystem< GUM_SCALAR >& system) :
622  PRMInference< GUM_SCALAR >(prm, system),
623  __class_elim_order(0) {
624  GUM_CONSTRUCTOR(SVE);
625  }
626 
627  template < typename GUM_SCALAR >
628  INLINE void
630  BucketSet& pool) {
631  for (const auto& elt : this->evidence(i))
632  pool.insert(const_cast< Potential< GUM_SCALAR >* >(elt.second));
633  }
634 
635  template < typename GUM_SCALAR >
636  INLINE std::vector< NodeId >&
638  return *(__elim_orders[&c]);
639  }
640 
641  template < typename GUM_SCALAR >
642  INLINE std::string SVE< GUM_SCALAR >::__trim(const std::string& s) {
643  auto pos = s.find_first_of("<");
644  if (pos != std::string::npos) { return s.substr(0, pos); }
645  return s;
646  }
647 
648  template < typename GUM_SCALAR >
650  const PRMInstance< GUM_SCALAR >* first,
651  const PRMInstance< GUM_SCALAR >* second) {
652  if (__class_elim_order == 0) { __initElimOrder(); }
653 
654  auto first_name = __trim(first->type().name());
655  auto second_name = __trim(second->type().name());
656  return (__class_elim_order->pos(first_name)
657  <= __class_elim_order->pos(second_name));
658  }
659 
660  template < typename GUM_SCALAR >
663  return &(const_cast< Potential< GUM_SCALAR >& >(i->get(agg->id()).cpf()));
664  }
665 
666  template < typename GUM_SCALAR >
667  INLINE void SVE< GUM_SCALAR >::_evidenceAdded(const Chain& chain) {
668  // Do nothing
669  }
670 
671  template < typename GUM_SCALAR >
672  INLINE void SVE< GUM_SCALAR >::_evidenceRemoved(const Chain& chain) {
673  // Do nothing
674  }
675 
676  template < typename GUM_SCALAR >
677  INLINE void
679  const PRMInstance< GUM_SCALAR >* j,
680  NodeId id) {
681  try {
682  __delayedVariables[i]->insert(&(j->get(id).type().variable()));
683  } catch (NotFound&) {
685  __delayedVariables[i]->insert(&(j->get(id).type().variable()));
686  } catch (DuplicateElement&) {
687  // happends if j->get(id) is parent of more than one variable in i
688  }
689 
690  static std::string dot = ".";
691 
692  try {
693  __delayedVariablesCounters[j->name() + dot + j->get(id).safeName()] += 1;
694  } catch (NotFound&) {
695  __delayedVariablesCounters.insert(j->name() + dot + j->get(id).safeName(),
696  1);
697  }
698  }
699 
700  template < typename GUM_SCALAR >
701  INLINE std::string SVE< GUM_SCALAR >::name() const {
702  return "SVE";
703  }
704 
705  } /* namespace prm */
706 } /* namespace gum */
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:581
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:60
bool empty() const noexcept
Returns a boolean indicating whether the chained list is empty.
Definition: list_tpl.h:1970
Idx pos(const Key &key) const
Returns the position of the object passed in argument (if it exists).
Definition: sequence_tpl.h:518
Val & push_back(Args &&... args)
An alias for pushBack used for STL compliance.
void __addDelayedVariable(const PRMInstance< GUM_SCALAR > *i, const PRMInstance< GUM_SCALAR > *j, NodeId id)
When there is a loop in the references some variable elimination must be delayed, this methods add su...
Definition: SVE_tpl.h:678
~SVE()
Destructor.
Definition: SVE_tpl.h:108
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition: set_tpl.h:707
const DAG & dag() const
Returns a constant reference over the graph of the DAG representing the ClassDependencyGraph<GUM_SCAL...
DiscreteVariable & variable()
Return a reference on the DiscreteVariable contained in this.
Definition: PRMType_inl.h:45
virtual const DiscreteVariable & variable(NodeId id) const
See gum::IBaseBayesNet::variable().
std::string __print_pot__(const Potential< GUM_SCALAR > &pot)
Definition: SVE_tpl.h:86
HashTable< std::string, Size > __delayedVariablesCounters
Some variable must be delayed for more than one PRMInstance<GUM_SCALAR>, when the delayed variable co...
Definition: SVE.h:139
PRMAttribute< GUM_SCALAR > & get(NodeId id)
Getter on an PRMAttribute<GUM_SCALAR> of this PRMInstance<GUM_SCALAR>.
void __insertEvidence(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:629
const std::string & name() const
Returns the name of this object.
Definition: PRMObject_inl.h:35
std::vector< NodeId > & __getElimOrder(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:637
void __initLiftedNodes(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:490
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
virtual PRMType & type()=0
See gum::PRMClassElement::type().
An PRMInstance is a Bayesian Network fragment defined by a Class and used in a PRMSystem.
Definition: PRMInstance.h:63
const Potential< GUM_SCALAR > & normalize() const
normalisation of this do nothing if sum is 0
virtual bool isInnerNode(const PRMClassElement< GUM_SCALAR > &elt) const
Returns true if the node is an inner node.
std::string __print_list__(LIST l)
Definition: SVE_tpl.h:75
void __eliminateNodesUpward(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, List< const PRMInstance< GUM_SCALAR > * > &elim_list, Set< const PRMInstance< GUM_SCALAR > * > &ignore, Set< const PRMInstance< GUM_SCALAR > * > &eliminated)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:331
virtual void _marginal(const Chain &chain, Potential< GUM_SCALAR > &m)
See PRMInference<GUM_SCALAR>::_marginal().
Definition: SVE_tpl.h:581
The generic class for storing (ordered) sequences of objects.
Definition: sequence.h:1022
PRMClass< GUM_SCALAR > & type()
Returns the type of this instance.
bool __checkElimOrder(const PRMInstance< GUM_SCALAR > *first, const PRMInstance< GUM_SCALAR > *second)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:649
const NodeProperty< Size > & modalities() const
See gum::IBaseBayesNet::cpt().
Abstract class representing an element of PRM class.
const EltPair & get(NodeId id) const
Returns a constant reference over the element assiociated with the node id in the ClassDependencyGrap...
A multidim implementation for buckets.
Sequence< std::string > * __class_elim_order
Definition: SVE.h:128
void erase(const Key &k)
Erases an element from the set.
Definition: set_tpl.h:656
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 __eliminateNodes(const PRMInstance< GUM_SCALAR > *query, NodeId id, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:128
void popFront()
Removes the first element of a List, if any.
Definition: list_tpl.h:1964
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Potential< GUM_SCALAR > * __getAggPotential(const PRMInstance< GUM_SCALAR > *i, const PRMAggregate< GUM_SCALAR > *agg)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:661
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > *> __elim_orders
Definition: SVE.h:124
InvRefIterator beginInvRef()
Alias to iterate over the gum::prm::PRMAttribute<GUM_SCALAR> in this PRMInstance<GUM_SCALAR>.
Representation of a setA Set is a structure that contains arbitrary elements.
Definition: set.h:165
const std::string & safeName() const
Returns the safe name of this PRMClassElement, if any.
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:607
std::string __print_instance__(const PRMInstance< GUM_SCALAR > &i)
Definition: SVE_tpl.h:49
This class decorates a gum::prm::Class<GUM_SCALAR> has an IBaseBayesNet.
Definition: classBayesNet.h:60
virtual void _evidenceRemoved(const Chain &chain)
See PRMInference<GUM_SCALAR>::_evidenceRemoved().
Definition: SVE_tpl.h:672
class for graph triangulations for which we enforce a given partial ordering on the nodes elimination...
void __insertLiftedNodes(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:470
SVE(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default Constructor.
Definition: SVE_tpl.h:620
The default triangulation algorithm used by aGrUM.
const Bijection< const DiscreteVariable *, const DiscreteVariable *> & bijection() const
Returns a mapping between DiscreteVariable used in this and the ones used in this PRMInstance<GUM_SCA...
const std::vector< NodeId > & eliminationOrder()
returns an elimination ordering compatible with the triangulated graph
PRMClassElement< GUM_SCALAR > & get(NodeId id)
See gum::prm::PRMClassElementContainer<GUM_SCALAR>::get(NodeId).
void __variableElimination(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, Set< NodeId > *delayedVars=0)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:289
void __eliminateNodesDownward(const PRMInstance< GUM_SCALAR > *from, const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, List< const PRMInstance< GUM_SCALAR > * > &elim_list, Set< const PRMInstance< GUM_SCALAR > * > &ignore, Set< const PRMInstance< GUM_SCALAR > * > &eliminated)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:236
A PRMSystem is a container of PRMInstance and describe a relational skeleton.
Definition: PRMObject.h:229
bool exists(const Key &k) const
Check the existence of k in the sequence.
Definition: sequence_tpl.h:402
void __initElimOrder()
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:544
Potential< GUM_SCALAR > * copyPotential(const Bijection< const DiscreteVariable *, const DiscreteVariable * > &bij, const Potential< GUM_SCALAR > &source)
Returns a copy of a Potential after applying a bijection over the variables in source.
Definition: utils_prm_tpl.h:29
virtual void _evidenceAdded(const Chain &chain)
See PRMInference<GUM_SCALAR>::_evidenceAdded().
Definition: SVE_tpl.h:667
This class represent the dependencies of all classes in a PRM<GUM_SCALAR>.
std::string __print_attribute__(const PRMInstance< GUM_SCALAR > &i, const PRMAttribute< GUM_SCALAR > &a)
Definition: SVE_tpl.h:36
void eliminateNode(const DiscreteVariable *var, Set< Potential< GUM_SCALAR > * > &pool, Set< Potential< GUM_SCALAR > * > &trash)
Proceeds with the elimination of var in pool.
virtual bool isOutputNode(const PRMClassElement< GUM_SCALAR > &elt) const
Returns true if elt is an output node.
void eliminateNodes(const std::vector< const DiscreteVariable * > &elim_order, Set< Potential< GUM_SCALAR > * > &pool, Set< Potential< GUM_SCALAR > * > &trash)
std::pair< const PRMInstance< GUM_SCALAR > *, const PRMAttribute< GUM_SCALAR > *> Chain
Code alias.
Definition: PRMInference.h:57
virtual std::string name() const
Returns the name of the current inference algorithm.
Definition: SVE_tpl.h:701
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
void __eliminateNodesWithEvidence(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, Set< NodeId > *delayedVars=0)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:382
This abstract class is used as base class for all inference class on PRM<GUM_SCALAR>.
Definition: PRMInference.h:52
const UndiGraph & moralGraph(bool clear=true) const
The node&#39;s id are coherent with the variables and nodes of the topology.
Definition: DAGmodel.cpp:101
This class represents a Probabilistic Relational PRMSystem<GUM_SCALAR>.
Definition: PRM.h:66
NodeId id() const
Returns the NodeId of this element in it&#39;s class DAG.
const iterator & end()
Returns a reference over the iterator at the end of the list of gum::prm::PRMAttribute<GUM_SCALAR> in...
void __eliminateDelayedVariables(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:210
A PRMClass is an object of a PRM representing a fragment of a Bayesian Network which can be instantia...
Definition: PRMClass.h:66
This class decorates an PRMInstance<GUM_SCALAR> as an IBaseBayesNet.
std::string __trim(const std::string &s)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:642
PRMAttribute is a member of a Class in a PRM.
Definition: PRMAttribute.h:61
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const final
Returns a const ref to the sequence of DiscreteVariable*.
iterator begin()
Returns an iterator at the begining of the list of gum::prm::PRMAttribute<GUM_SCALAR> in this PRMInst...
std::string __print_system__(const PRMSystem< GUM_SCALAR > &s)
Definition: SVE_tpl.h:66
const Set< const DiscreteVariable *> & allVariables() const
Returns the sequence of all the variables contained in the bucket.
Size size() const noexcept
Returns the number of elements in the set.
Definition: set_tpl.h:701
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
std::string __print_set__(SET set)
Definition: SVE_tpl.h:97
const InvRefIterator & endInvRef()
Alias to iterate over the gum::prm::PRMAttribute<GUM_SCALAR> in this PRMInstance<GUM_SCALAR>.
virtual const DAG & containerDag() const
Returns the gum::DAG of this PRMClassElementContainer.
void add(const MultiDimContainer< GUM_SCALAR > &impl)
Add a MultiDimContainer in the bucket.
Size NodeId
Type for node ids.
Definition: graphElements.h:98
HashTable< const PRMInstance< GUM_SCALAR > *, Set< const DiscreteVariable *> *> __delayedVariables
Definition: SVE.h:132
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
const NodeProperty< Size > & modalities() const
See gum::IBaseBayesNet::modalities().
virtual void _joint(const std::vector< Chain > &queries, Potential< GUM_SCALAR > &j)
See PRMInference<GUM_SCALAR>::_joint().
Definition: SVE_tpl.h:614
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
This class is an implementation of the Structured Variable Elimination algorithm on PRM<GUM_SCALAR>...
Definition: SVE.h:66
const Set< PRMInstance< GUM_SCALAR > *> & getInstances(NodeId id) const
Returns the Set of PRMInstance<GUM_SCALAR> referenced by id.
void insert(const Key &k)
Insert an element at the end of the sequence.
Definition: sequence_tpl.h:408