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