aGrUM  0.16.0
utils_prm_tpl.h
Go to the documentation of this file.
1 
23 #include <agrum/PRM/utils_prm.h>
24 
25 namespace gum {
26  namespace prm {
27 
28  template < typename GUM_SCALAR >
31  const Potential< GUM_SCALAR >& source) {
32  const MultiDimImplementation< GUM_SCALAR >* impl = source.content();
34 
35  try {
36  if (dynamic_cast< const MultiDimReadOnly< GUM_SCALAR >* >(impl)) {
37  if (dynamic_cast< const MultiDimNoisyORCompound< GUM_SCALAR >* >(impl)) {
40  bij,
41  static_cast< const MultiDimNoisyORCompound< GUM_SCALAR >& >(
42  *impl)));
43  } else if (dynamic_cast< const MultiDimNoisyORNet< GUM_SCALAR >* >(
44  impl)) {
46  bij,
47  static_cast< const MultiDimNoisyORNet< GUM_SCALAR >& >(*impl)));
48  } else if (dynamic_cast<
50  impl)) {
53  impl->newFactory()));
54 
55  for (auto var : impl->variablesSequence())
56  p->add(*(bij.second(var)));
57  } else if (dynamic_cast< const MultiDimBucket< GUM_SCALAR >* >(impl)) {
58  // This is necessary just to prevent non initialized arrays
59  const_cast< MultiDimBucket< GUM_SCALAR >* >(
60  static_cast< const MultiDimBucket< GUM_SCALAR >* >(impl))
61  ->compute();
62 
63  try {
65  bij,
66  static_cast< const MultiDimBucket< GUM_SCALAR >* >(impl)
67  ->bucket()));
68  } catch (OperationNotAllowed&) {
69  // This is an empty bucket, it happens if all variables were
70  // eliminated
71  return new Potential< GUM_SCALAR >();
72  }
73  } else {
75  "encountered an unexpected MultiDim implementation");
76  }
77  } else {
78  if (dynamic_cast< const MultiDimArray< GUM_SCALAR >* >(impl)) {
80  bij, static_cast< const MultiDimArray< GUM_SCALAR >& >(*impl)));
81  } else if (dynamic_cast< const MultiDimBijArray< GUM_SCALAR >* >(impl)) {
83  bij, static_cast< const MultiDimBijArray< GUM_SCALAR >& >(*impl)));
84  } else if (dynamic_cast< const MultiDimSparse< GUM_SCALAR >* >(impl)) {
86  "There is no MultiDimSparse in PRMs, normally...");
87  } else {
88  // Just need to make the copy using the bijection but we only use
89  // multidim array
91  "encountered an unexpected MultiDim implementation");
92  }
93  }
94 
95  return p;
96  } catch (Exception&) {
97  if (p) delete p;
98 
99  throw;
100  }
101  }
102 
103  // the function used to combine two tables
104  template < typename GUM_SCALAR >
106  const Potential< GUM_SCALAR >& t2) {
107  return new Potential< GUM_SCALAR >(t1 * t2);
108  }
109 
110  template < typename GUM_SCALAR >
112  Set< Potential< GUM_SCALAR >* >& pool,
113  Set< Potential< GUM_SCALAR >* >& trash) {
114  Potential< GUM_SCALAR >* pot = nullptr;
115  Potential< GUM_SCALAR >* tmp = nullptr;
116 
118  var_set.insert(var);
120 
121  for (const auto p : pool)
122  if (p->contains(*var)) pots.insert(p);
123 
124  if (pots.size() == 0) {
125  return;
126  } else if (pots.size() == 1) {
127  tmp = const_cast< Potential< GUM_SCALAR >* >(*pots.begin());
128  pot = new Potential< GUM_SCALAR >(tmp->margSumOut(var_set));
129  } else {
131  tmp = Comb.combine(pots);
132  pot = new Potential< GUM_SCALAR >(tmp->margSumOut(var_set));
133  delete tmp;
134  }
135 
136  for (const auto p : pots) {
137  pool.erase(const_cast< Potential< GUM_SCALAR >* >(p));
138 
139  if (trash.exists(const_cast< Potential< GUM_SCALAR >* >(p))) {
140  trash.erase(const_cast< Potential< GUM_SCALAR >* >(p));
141  delete const_cast< Potential< GUM_SCALAR >* >(p);
142  }
143  }
144 
145  pool.insert(pot);
146  trash.insert(pot);
147  }
148 
149  template < typename GUM_SCALAR >
150  void eliminateNodes(const std::vector< const DiscreteVariable* >& elim_order,
151  Set< Potential< GUM_SCALAR >* >& pool,
152  Set< Potential< GUM_SCALAR >* >& trash) {
153  for (auto var : elim_order) {
154  eliminateNode(var, pool, trash);
155  }
156  }
157 
158  } /* namespace prm */
159 } // namespace gum
Noisy OR representation.
A class to combine efficiently several MultiDim tablesMultiDimCombinationDefault is a class designed ...
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:60
const T2 & second(const T1 &first) const
Returns the second value of a pair given its first value.
Decorator of a MultiDimArray, using a bijection over the variables.
Definition: multiDimArray.h:39
virtual void erase(const DiscreteVariable &var) final
Removes a var from the variables of the multidimensional matrix.
A multidim implementation for buckets.
Base class for discrete random variable.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
iterator begin() const
The usual unsafe begin iterator to parse the set.
Definition: set_tpl.h:517
Abstract base class for all multi dimensionnal read only structure.
<agrum/multidim/aggregators/multiDimAggregator.h>
virtual const MultiDimImplementation< GUM_SCALAR > * content() const final
Returns the implementation for this object (may be *this).
Representation of a setA Set is a structure that contains arbitrary elements.
Definition: set.h:165
void compute(bool force=false) const
This method computes the final table of this bucket.
Base class for all aGrUM&#39;s exceptions.
Definition: exceptions.h:106
Multidimensional matrix stored as an array in memory.
Definition: multiDimArray.h:54
Set of pairs of elements with fast search for both elements.
Definition: bijection.h:1805
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
void eliminateNode(const DiscreteVariable *var, Set< Potential< GUM_SCALAR > * > &pool, Set< Potential< GUM_SCALAR > * > &trash)
Proceeds with the elimination of var in pool.
Potential< GUM_SCALAR > margSumOut(const Set< const DiscreteVariable * > &del_vars) const
Projection using sum as operation (and implementation-optimized operations)
void eliminateNodes(const std::vector< const DiscreteVariable * > &elim_order, Set< Potential< GUM_SCALAR > * > &pool, Set< Potential< GUM_SCALAR > * > &trash)
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const override
Returns a const ref to the sequence of DiscreteVariable*.
virtual void add(const DiscreteVariable &v) final
Adds a new var to the variables of the multidimensional matrix.
<agrum/multidim/multiDimImplementation.h>
Size size() const noexcept
Returns the number of elements in the set.
Definition: set_tpl.h:701
virtual MultiDimContainer< GUM_SCALAR > * newFactory() const override=0
Creates an empty clone of this MultiDimContainer.
virtual TABLE< GUM_SCALAR > * combine(const Set< const TABLE< GUM_SCALAR > * > &set)
Creates and returns the result of the combination of the tables within set.
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
Multidimensional matrix stored as a sparse array in memory.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
Potential< GUM_SCALAR > * multPotential(const Potential< GUM_SCALAR > &t1, const Potential< GUM_SCALAR > &t2)