aGrUM  0.14.2
SVED_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  ***************************************************************************/
27 
28 namespace gum {
29  namespace prm {
30 
31  template < typename GUM_SCALAR >
33  GUM_DESTRUCTOR(SVED);
34 
35  for (const auto& elt : __elim_orders)
36  delete elt.second;
37 
38  if (__class_elim_order != nullptr) delete __class_elim_order;
39  }
40 
41  template < typename GUM_SCALAR >
42  void
44  NodeId node,
45  BucketSet& pool,
46  BucketSet& trash) {
48  ignore.insert(query);
49  // Extracting required attributes and slotchains
50  Set< NodeId >& attr_set = __getAttrSet(query);
51  Set< NodeId >& sc_set = __getSCSet(query);
52  // Downward elimination
54 
55  for (const auto attr : attr_set) {
56  try {
57  for (auto iter = query->getRefAttr(attr).begin();
58  iter != query->getRefAttr(attr).end();
59  ++iter)
60  if ((!ignore.exists(iter->first)) && (__bb.exists(iter->first)))
61  __eliminateNodesDownward(
62  query, iter->first, pool, trash, elim_list, ignore);
63  } catch (NotFound&) {
64  // Ok
65  }
66  }
67 
68  // Eliminating all nodes in query instance, except query
70  DefaultTriangulation t(&(bn.moralGraph()), &(bn.modalities()));
71  std::vector< const DiscreteVariable* > elim_order;
72 
73  if (this->hasEvidence(query)) __insertEvidence(query, pool);
74 
75  for (const auto attr : attr_set)
76  pool.insert(
77  &(const_cast< Potential< GUM_SCALAR >& >(query->get(attr).cpf())));
78 
79  for (size_t idx = 0; idx < t.eliminationOrder().size(); ++idx) {
80  if (t.eliminationOrder()[idx] != node) {
81  auto var_id = t.eliminationOrder()[idx];
82  const auto& var = bn.variable(var_id);
83  elim_order.push_back(&var);
84  }
85  }
86 
87  eliminateNodes(elim_order, pool, trash);
88  // Eliminating instance in elim_list
90  __reduceElimList(query, elim_list, tmp_list, ignore, pool, trash);
91 
92  while (!elim_list.empty()) {
93  if (__checkElimOrder(query, elim_list.front())) {
94  if ((!ignore.exists(elim_list.front()))
95  && (__bb.exists(elim_list.front())))
96  __eliminateNodesDownward(
97  query, elim_list.front(), pool, trash, elim_list, ignore);
98  } else if (__bb.exists(elim_list.front())) {
99  tmp_list.insert(elim_list.front());
100  }
101 
102  elim_list.popFront();
103  }
104 
105  // Upward elimination
106  for (const auto chain : sc_set)
107  for (const auto parent : query->getInstances(chain))
108  if ((!ignore.exists(parent)) && (__bb.exists(*parent)))
109  __eliminateNodesUpward(parent, pool, trash, tmp_list, ignore);
110  }
111 
112  template < typename GUM_SCALAR >
114  const PRMInstance< GUM_SCALAR >* from,
115  const PRMInstance< GUM_SCALAR >* i,
116  BucketSet& pool,
117  BucketSet& trash,
118  List< const PRMInstance< GUM_SCALAR >* >& elim_list,
119  Set< const PRMInstance< GUM_SCALAR >* >& ignore) {
120  ignore.insert(i);
121  // Extracting required attributes and slotchains
122  Set< NodeId >& attr_set = __getAttrSet(i);
123  Set< NodeId >& sc_set = __getSCSet(i);
124  // Calling elimination over child instance
126 
127  for (const auto attr : attr_set) {
128  try {
129  for (auto iter = i->getRefAttr(attr).begin();
130  iter != i->getRefAttr(attr).end();
131  ++iter)
132  if ((!ignore.exists(iter->first)) && (__bb.exists(iter->first)))
133  __eliminateNodesDownward(
134  i, iter->first, pool, trash, my_list, ignore);
135  } catch (NotFound&) {
136  // Ok
137  }
138  }
139 
140  // Eliminating all nodes in current instance
141  if (this->hasEvidence(i)) {
142  __eliminateNodesWithEvidence(i, pool, trash);
143  } else {
144  __insertLiftedNodes(i, pool, trash);
145 
146  for (const auto agg : i->type().aggregates())
147  if (__bb.requisiteNodes(i).exists(agg->id()))
148  pool.insert(__getAggPotential(i, agg));
149 
150  try {
152  std::vector< const DiscreteVariable* > elim_order;
153 
154  for (auto node : __getElimOrder(i->type())) {
155  const auto& var = bn.variable(node);
156  elim_order.push_back(&var);
157  }
158 
159  eliminateNodes(elim_order, pool, trash);
160  } catch (NotFound&) {
161  // Raised if there is no inner nodes to eliminate
162  }
163  }
164 
165  // Calling elimination over child's parents
166  while (!my_list.empty()) {
167  if (__checkElimOrder(i, my_list.front())) {
168  if ((!ignore.exists(my_list.front())) && (__bb.exists(my_list.front())))
169  __eliminateNodesDownward(
170  i, my_list.front(), pool, trash, my_list, ignore);
171  } else if (__bb.exists(my_list.front())) {
172  elim_list.insert(my_list.front());
173  }
174 
175  my_list.popFront();
176  }
177 
178  // Adding parents instance to elim_list
179  for (const auto chain : sc_set)
180  for (const auto parent : i->getInstances(chain))
181  if ((!ignore.exists(parent)) && __bb.exists(parent) && (parent != from))
182  elim_list.insert(parent);
183  }
184 
185  template < typename GUM_SCALAR >
187  const PRMInstance< GUM_SCALAR >* i,
188  BucketSet& pool,
189  BucketSet& trash,
190  List< const PRMInstance< GUM_SCALAR >* >& elim_list,
191  Set< const PRMInstance< GUM_SCALAR >* >& ignore) {
192  ignore.insert(i);
193  // Extracting required attributes and slotchains
194  Set< NodeId >& attr_set = __getAttrSet(i);
195  Set< NodeId >& sc_set = __getSCSet(i);
196 
197  // Downward elimination
198  for (const auto attr : attr_set) {
199  try {
200  for (auto iter = i->getRefAttr(attr).begin();
201  iter != i->getRefAttr(attr).end();
202  ++iter)
203  if ((!ignore.exists(iter->first)) && (__bb.exists(iter->first)))
204  __eliminateNodesDownward(
205  i, iter->first, pool, trash, elim_list, ignore);
206  } catch (NotFound&) {
207  // Ok
208  }
209  }
210 
211  // Eliminating all nodes in i instance
212  if (this->hasEvidence(i)) {
213  __eliminateNodesWithEvidence(i, pool, trash);
214  } else {
215  __insertLiftedNodes(i, pool, trash);
216 
217  for (const auto agg : i->type().aggregates())
218  if (__bb.requisiteNodes(i).exists(agg->id()))
219  pool.insert(__getAggPotential(i, agg));
220 
221  try {
223  std::vector< const DiscreteVariable* > elim_order;
224 
225  for (auto node : __getElimOrder(i->type())) {
226  const auto& var = bn.variable(node);
227  elim_order.push_back(&var);
228  }
229  eliminateNodes(elim_order, pool, trash);
230  } catch (NotFound&) {
231  // Raised if there is no inner nodes to eliminate
232  }
233  }
234 
235  // Eliminating instance in elim_list
237 
238  while (!elim_list.empty()) {
239  if (__checkElimOrder(i, elim_list.front())) {
240  if ((!ignore.exists(elim_list.front()))
241  && (__bb.exists(elim_list.front())))
242  __eliminateNodesDownward(
243  i, elim_list.front(), pool, trash, elim_list, ignore);
244  } else if (__bb.exists(elim_list.front())) {
245  ignore.insert(elim_list.front());
246  }
247 
248  elim_list.popFront();
249  }
250 
251  // Upward elimination
252  for (const auto chain : sc_set)
253  for (const auto parent : i->getInstances(chain))
254  if ((!ignore.exists(parent)) && (__bb.exists(parent)))
255  __eliminateNodesUpward(parent, pool, trash, tmp_list, ignore);
256  }
257 
258  template < typename GUM_SCALAR >
260  const PRMInstance< GUM_SCALAR >* i, BucketSet& pool, BucketSet& trash) {
261  // Adding required evidences
262  for (const auto& elt : this->evidence(i))
263  if (__bb.requisiteNodes(i).exists(elt.first))
264  pool.insert(const_cast< Potential< GUM_SCALAR >* >(elt.second));
265 
266  // Adding potentials and eliminating the remaining nodes
267  for (const auto& a : *i)
268  if (__bb.requisiteNodes(i).exists(a.first))
269  pool.insert(&(const_cast< Potential< GUM_SCALAR >& >(a.second->cpf())));
270 
272  DefaultTriangulation t(&(bn.moralGraph()), &(bn.modalities()));
273  const std::vector< NodeId >& full_elim_order = t.eliminationOrder();
274 
275  for (auto var = full_elim_order.begin(); var != full_elim_order.end(); ++var)
276  eliminateNode(&(i->get(*var).type().variable()), pool, trash);
277  }
278 
279  template < typename GUM_SCALAR >
281  const PRMInstance< GUM_SCALAR >* i, BucketSet& pool, BucketSet& trash) {
282  BucketSet* lifted_pool = nullptr;
283 
284  try {
285  lifted_pool = __lifted_pools[&(__bb.requisiteNodes(i))];
286  } catch (NotFound&) {
287  __initLiftedNodes(i, trash);
288  lifted_pool = __lifted_pools[&(__bb.requisiteNodes(i))];
289  }
290 
291  for (const auto lifted_pot : *lifted_pool) {
292  Potential< GUM_SCALAR >* pot = copyPotential(i->bijection(), *lifted_pot);
293  pool.insert(pot);
294  trash.insert(pot);
295  }
296  }
297 
298  template < typename GUM_SCALAR >
300  BucketSet& trash) {
301  PRMClass< GUM_SCALAR >& c = const_cast< PRMClass< GUM_SCALAR >& >(i->type());
302  BucketSet* lifted_pool = new BucketSet();
303  __lifted_pools.insert(&(__bb.requisiteNodes(i)), lifted_pool);
304 
305  for (const auto node : __bb.requisiteNodes(i))
307  lifted_pool->insert(
308  const_cast< Potential< GUM_SCALAR >* >(&(c.get(node).cpf())));
309 
310  NodeSet inners, outers, ignore;
311 
312  for (const auto& elt : *i) {
313  if (__bb.requisiteNodes(*i).exists(elt.first)) {
315  if (c.isOutputNode(c.get(elt.first)))
316  outers.insert(elt.first);
317  else if (!outers.exists(elt.first))
318  inners.insert(elt.first);
320  c.get(elt.first))) {
321  outers.insert(elt.first);
322 
323  // We need to put in the output_elim_order aggregator's parents
324  // which are
325  // innner nodes
326  for (const auto par : c.containerDag().parents(elt.first))
327  if (PRMClassElement< GUM_SCALAR >::isAttribute(i->type().get(par))
328  && i->type().isInnerNode(i->type().get(par))
329  && __bb.requisiteNodes(i).exists(par)) {
330  inners.erase(par);
331  outers.insert(par);
332  }
333  }
334  } else {
335  ignore.insert(elt.first);
336  }
337  }
338 
339  // Now we proceed with the elimination of inner attributes
341  List< NodeSet > partial_ordering;
342 
343  if (inners.size()) partial_ordering.pushBack(inners);
344 
345  if (outers.size()) partial_ordering.pushBack(outers);
346 
347  if (ignore.size()) partial_ordering.pushBack(ignore);
348 
349  GUM_ASSERT(inners.size() || outers.size());
351  &(bn.moralGraph()), &(bn.modalities()), &partial_ordering);
352 
353  for (size_t idx = 0; idx < inners.size(); ++idx)
354  eliminateNode(&(c.get(t.eliminationOrder()[idx]).type().variable()),
355  *lifted_pool,
356  trash);
357 
358  // If there is not only inner and input Attributes
359  if (outers.size()) {
360  __elim_orders.insert(
361  &c,
362  new std::vector< NodeId >(t.eliminationOrder().begin() + inners.size(),
363  t.eliminationOrder().end()));
364  }
365  }
366 
367  template < typename GUM_SCALAR >
369  ClassDependencyGraph< GUM_SCALAR > cdg(*(this->_prm));
371  std::list< NodeId > l;
372 
373  for (const auto node : cdg.dag().nodes()) {
374  if (cdg.dag().parents(node).empty()) { l.push_back(node); }
375  }
376 
377  Set< NodeId > visited_node;
378 
379  while (!l.empty()) {
380  visited_node.insert(l.front());
381 
382  if (!class_elim_order.exists(cdg.get(l.front()).first)) {
383  class_elim_order.insert(cdg.get(l.front()).first);
384  }
385 
386  for (const auto child : cdg.dag().children(l.front())) {
387  if (!visited_node.contains(child)) { l.push_back(child); }
388  }
389 
390  l.pop_front();
391  }
392 
393  __class_elim_order = new Sequence< std::string >();
394  for (auto c : class_elim_order) {
395  std::string name = c->name();
396  auto pos = name.find_first_of("<");
397  if (pos != std::string::npos) { name = name.substr(0, pos); }
398  try {
399  __class_elim_order->insert(name);
400  } catch (DuplicateElement&) {}
401  }
402  }
403 
404  template < typename GUM_SCALAR >
407  const PRMInstance< GUM_SCALAR >* i = chain.first;
408  const PRMAttribute< GUM_SCALAR >* elt = chain.second;
409  SVED< GUM_SCALAR >::BucketSet pool, trash;
410  __bb.compute(i, elt->id());
411  __eliminateNodes(i, elt->id(), pool, trash);
412 
413  std::vector< const Potential< GUM_SCALAR >* > result;
414  for (auto pot : pool) {
415  if (pot->contains(*(m.variablesSequence().atPos(0))))
416  result.push_back(pot);
417  }
418 
419  while (result.size() > 1) {
420  const auto& p1 = *(result.back());
421  result.pop_back();
422  const auto& p2 = *(result.back());
423  result.pop_back();
424  auto mult = new Potential< GUM_SCALAR >(p1 * p2);
425  result.push_back(mult);
426  trash.insert(mult);
427  }
428 
429  m = *(result.back());
430  m.normalize();
431 
432  GUM_ASSERT(m.nbrDim() == (Size)1);
433 
434  // cleaning up the mess
435  for (const auto pot : trash)
436  delete pot;
437 
438  for (const auto& elt : __lifted_pools)
439  delete elt.second;
440 
441  __lifted_pools.clear();
442 
443  for (const auto& elt : __req_set) {
444  delete elt.second.first;
445  delete elt.second.second;
446  }
447 
448  __req_set.clear();
449 
450  for (const auto elt : __elim_orders)
451  delete elt.second;
452 
453  __elim_orders.clear();
454  }
455 
456  template < typename GUM_SCALAR >
457  void SVED< GUM_SCALAR >::_joint(const std::vector< Chain >& queries,
459  GUM_ERROR(FatalError, "Not implemented.");
460  }
461 
462  template < typename GUM_SCALAR >
464  Set< NodeId >* attr_set = new Set< NodeId >();
465  Set< NodeId >* sc_set = new Set< NodeId >();
466 
467  for (const auto node : __bb.requisiteNodes(i)) {
468  switch (i->type().get(node).elt_type()) {
471  attr_set->insert(node);
472  break;
473  }
474 
476  sc_set->insert(node);
477  break;
478  }
479 
480  default: {
482  "There should not be elements other"
483  " than PRMAttribute<GUM_SCALAR> and SlotChain.");
484  }
485  }
486  }
487 
488  __req_set.insert(
489  &(__bb.requisiteNodes(i)),
490  std::pair< Set< NodeId >*, Set< NodeId >* >(attr_set, sc_set));
491  }
492 
493  template < typename GUM_SCALAR >
495  const PRMSystem< GUM_SCALAR >& model) :
496  PRMInference< GUM_SCALAR >(prm, model),
497  __class_elim_order(0), __bb(*this) {
498  GUM_CONSTRUCTOR(SVED);
499  }
500 
501 
502  template < typename GUM_SCALAR >
503  INLINE void
505  BucketSet& pool) {
506  for (const auto& elt : this->evidence(i))
507  pool.insert(const_cast< Potential< GUM_SCALAR >* >(elt.second));
508  }
509 
510  template < typename GUM_SCALAR >
511  INLINE std::vector< NodeId >&
513  return *(__elim_orders[&c]);
514  }
515 
516  template < typename GUM_SCALAR >
517  INLINE std::string SVED< GUM_SCALAR >::__trim(const std::string& s) {
518  auto pos = s.find_first_of("<");
519  if (pos != std::string::npos) { return s.substr(0, pos); }
520  return s;
521  }
522 
523  template < typename GUM_SCALAR >
525  const PRMInstance< GUM_SCALAR >* first,
526  const PRMInstance< GUM_SCALAR >* second) {
527  if (__class_elim_order == 0) { __initElimOrder(); }
528 
529  auto first_name = __trim(first->type().name());
530  auto second_name = __trim(second->type().name());
531  return (__class_elim_order->pos(first_name)
532  <= __class_elim_order->pos(second_name));
533  }
534 
535  template < typename GUM_SCALAR >
538  return &(
539  const_cast< Potential< GUM_SCALAR >& >(i->get(agg->safeName()).cpf()));
540  }
541 
542  template < typename GUM_SCALAR >
544  const typename SVED< GUM_SCALAR >::Chain& chain) {
545  // Do nothing
546  }
547 
548  template < typename GUM_SCALAR >
550  const typename SVED< GUM_SCALAR >::Chain& chain) {
551  // Do nothing
552  }
553 
554  template < typename GUM_SCALAR >
555  INLINE Set< NodeId >&
557  try {
558  return *(__req_set[&(__bb.requisiteNodes(i))].first);
559  } catch (NotFound&) {
560  __initReqSets(i);
561  return *(__req_set[&(__bb.requisiteNodes(i))].first);
562  }
563  }
564 
565  template < typename GUM_SCALAR >
566  INLINE Set< NodeId >&
568  try {
569  return *(__req_set[&(__bb.requisiteNodes(i))].second);
570  } catch (NotFound&) {
571  __initReqSets(i);
572  return *(__req_set[&(__bb.requisiteNodes(i))].second);
573  }
574  }
575 
576  template < typename GUM_SCALAR >
578  const PRMInstance< GUM_SCALAR >* i,
579  List< const PRMInstance< GUM_SCALAR >* >& elim_list,
580  List< const PRMInstance< GUM_SCALAR >* >& reduced_list,
581  Set< const PRMInstance< GUM_SCALAR >* >& ignore,
582  BucketSet& pool,
583  BucketSet& trash) {
584  while (!elim_list.empty()) {
585  if (__checkElimOrder(i, elim_list.front())) {
586  if ((!ignore.exists(elim_list.front()))
587  && (__bb.exists(elim_list.front()))) {
589  i, elim_list.front(), pool, trash, elim_list, ignore);
590  }
591  } else if (__bb.exists(elim_list.front())) {
592  reduced_list.insert(elim_list.front());
593  }
594 
595  elim_list.popFront();
596  }
597  }
598 
599  template < typename GUM_SCALAR >
600  INLINE std::string SVED< GUM_SCALAR >::name() const {
601  return "SVED";
602  }
603 
604  } /* namespace prm */
605 } /* namespace gum */
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:578
HashTable< const Set< NodeId > *, std::pair< Set< NodeId > *, Set< NodeId > *> > __req_set
First pair -> requisite Attributes Second pair -> requisite SlotChains.
Definition: SVED.h:135
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
virtual std::string name() const
Returns the name of the current inference algorithm.
Definition: SVED_tpl.h:600
virtual Idx nbrDim() const final
Returns the number of vars in the multidimensional container.
Idx pos(const Key &key) const
Returns the position of the object passed in argument (if it exists).
Definition: sequence_tpl.h:515
void __insertEvidence(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:504
virtual void _marginal(const Chain &chain, Potential< GUM_SCALAR > &m)
See PRMInference::_marginal().
Definition: SVED_tpl.h:405
void __eliminateNodes(const PRMInstance< GUM_SCALAR > *query, NodeId id, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:43
const DAG & dag() const
Returns a constant reference over the graph of the DAG representing the ClassDependencyGraph<GUM_SCAL...
virtual const DiscreteVariable & variable(NodeId id) const
See gum::IBaseBayesNet::variable().
PRMAttribute< GUM_SCALAR > & get(NodeId id)
Getter on an PRMAttribute<GUM_SCALAR> of this PRMInstance<GUM_SCALAR>.
virtual void _evidenceRemoved(const Chain &chain)
See PRMInference::_evidenceRemoved().
Definition: SVED_tpl.h:549
SVED(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &model)
Default Constructor.
Definition: SVED_tpl.h:494
Set< NodeId > & __getSCSet(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:567
EMap & evidence(const PRMInstance< GUM_SCALAR > &i)
Returns EMap of evidences over i.
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
The generic class for storing (ordered) sequences of objects.
Definition: sequence.h:1019
PRMClass< GUM_SCALAR > & type()
Returns the type of this instance.
void __initElimOrder()
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:368
const NodeProperty< Size > & modalities() const
See gum::IBaseBayesNet::cpt().
Abstract class representing an element of PRM class.
Potential< GUM_SCALAR > * __getAggPotential(const PRMInstance< GUM_SCALAR > *i, const PRMAggregate< GUM_SCALAR > *agg)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:536
void __initReqSets(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:463
const EltPair & get(NodeId id) const
Returns a constant reference over the element assiociated with the node id in the ClassDependencyGrap...
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 popFront()
Removes the first element of a List, if any.
Definition: list_tpl.h:1961
void __initLiftedNodes(const PRMInstance< GUM_SCALAR > *i, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:299
std::vector< std::pair< PRMInstance< GUM_SCALAR > *, std::string > > & getRefAttr(NodeId id)
Returns a vector of pairs of refering attributes of id.
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.
Headers of SVED (Structured Value Elimination with d-seperation).
PRMInference< GUM_SCALAR >::Chain Chain
Code alias.
Definition: SVED.h:90
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:604
std::vector< NodeId > & __getElimOrder(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:512
This class decorates a gum::prm::Class<GUM_SCALAR> has an IBaseBayesNet.
Definition: classBayesNet.h:57
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: SVED_tpl.h:280
This class is an implementation of the Structured Value Elimination algorithm on PRM<GUM_SCALAR>.
Definition: SVED.h:60
The default triangulation algorithm used by aGrUM.
Val & pushBack(const Val &val)
Inserts a new element (a copy) at the end of the chained list.
Definition: list_tpl.h:1589
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).
Sequence< std::string > * __class_elim_order
Definition: SVED.h:127
A PRMSystem is a container of PRMInstance and describe a relational skeleton.
Definition: PRMObject.h:226
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)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:113
bool exists(const Key &k) const
Check the existence of k in the sequence.
Definition: sequence_tpl.h:399
StructuredBayesBall< GUM_SCALAR > __bb
Definition: SVED.h:129
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
std::string __trim(const std::string &s)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:517
This class represent the dependencies of all classes in a PRM<GUM_SCALAR>.
void eliminateNode(const DiscreteVariable *var, Set< Potential< GUM_SCALAR > * > &pool, Set< Potential< GUM_SCALAR > * > &trash)
Proceeds with the elimination of var in pool.
bool __checkElimOrder(const PRMInstance< GUM_SCALAR > *first, const PRMInstance< GUM_SCALAR > *second)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:524
virtual PRMType & type()=0
Return a reference over the gum::PRMType of this class element.
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
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
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.
A PRMClass is an object of a PRM representing a fragment of a Bayesian Network which can be instantia...
Definition: PRMClass.h:63
virtual void _joint(const std::vector< Chain > &queries, Potential< GUM_SCALAR > &j)
See PRMInference::_joint().
Definition: SVED_tpl.h:457
void __eliminateNodesWithEvidence(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:259
This class decorates an PRMInstance<GUM_SCALAR> as an IBaseBayesNet.
HashTable< const PRMClass< GUM_SCALAR > *, std::vector< NodeId > *> __elim_orders
Definition: SVED.h:118
void __eliminateNodesUpward(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash, List< const PRMInstance< GUM_SCALAR > * > &elim_list, Set< const PRMInstance< GUM_SCALAR > * > &ignore)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:186
virtual void _evidenceAdded(const Chain &chain)
See PRMInference::_evidenceAdded().
Definition: SVED_tpl.h:543
PRMAttribute is a member of a Class in a PRM.
Definition: PRMAttribute.h:58
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const final
Returns a const ref to the sequence of DiscreteVariable*.
Set< NodeId > & __getAttrSet(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:556
Size size() const noexcept
Returns the number of elements in the set.
Definition: set_tpl.h:698
~SVED()
Destructor.
Definition: SVED_tpl.h:32
virtual const DAG & containerDag() const
Returns the gum::DAG of this PRMClassElementContainer.
Size NodeId
Type for node ids.
Definition: graphElements.h:97
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().
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
const Set< PRMInstance< GUM_SCALAR > *> & getInstances(NodeId id) const
Returns the Set of PRMInstance<GUM_SCALAR> referenced by id.
void __reduceElimList(const PRMInstance< GUM_SCALAR > *i, List< const PRMInstance< GUM_SCALAR > * > &elim_list, List< const PRMInstance< GUM_SCALAR > * > &reduced_list, Set< const PRMInstance< GUM_SCALAR > * > &ignore, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:577
void insert(const Key &k)
Insert an element at the end of the sequence.
Definition: sequence_tpl.h:405