aGrUM  0.14.2
utils_prm_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it udnder 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  ***************************************************************************/
20 
21 #include <agrum/PRM/utils_prm.h>
22 
23 namespace gum {
24  namespace prm {
25 
26  template < typename GUM_SCALAR >
29  const Potential< GUM_SCALAR >& source) {
30  const MultiDimImplementation< GUM_SCALAR >* impl = source.content();
32 
33  try {
34  if (dynamic_cast< const MultiDimReadOnly< GUM_SCALAR >* >(impl)) {
35  if (dynamic_cast< const MultiDimNoisyORCompound< GUM_SCALAR >* >(impl)) {
38  bij,
39  static_cast< const MultiDimNoisyORCompound< GUM_SCALAR >& >(
40  *impl)));
41  } else if (dynamic_cast< const MultiDimNoisyORNet< GUM_SCALAR >* >(
42  impl)) {
44  bij,
45  static_cast< const MultiDimNoisyORNet< GUM_SCALAR >& >(*impl)));
46  } else if (dynamic_cast<
48  impl)) {
51  impl->newFactory()));
52 
53  for (auto var : impl->variablesSequence())
54  p->add(*(bij.second(var)));
55  } else if (dynamic_cast< const MultiDimBucket< GUM_SCALAR >* >(impl)) {
56  // This is necessary just to prevent non initialized arrays
57  const_cast< MultiDimBucket< GUM_SCALAR >* >(
58  static_cast< const MultiDimBucket< GUM_SCALAR >* >(impl))
59  ->compute();
60 
61  try {
63  bij,
64  static_cast< const MultiDimBucket< GUM_SCALAR >* >(impl)
65  ->bucket()));
66  } catch (OperationNotAllowed&) {
67  // This is an empty bucket, it happens if all variables were
68  // eliminated
69  return new Potential< GUM_SCALAR >();
70  }
71  } else {
73  "encountered an unexpected MultiDim implementation");
74  }
75  } else {
76  if (dynamic_cast< const MultiDimArray< GUM_SCALAR >* >(impl)) {
78  bij, static_cast< const MultiDimArray< GUM_SCALAR >& >(*impl)));
79  } else if (dynamic_cast< const MultiDimBijArray< GUM_SCALAR >* >(impl)) {
81  bij, static_cast< const MultiDimBijArray< GUM_SCALAR >& >(*impl)));
82  } else if (dynamic_cast< const MultiDimSparse< GUM_SCALAR >* >(impl)) {
84  "There is no MultiDimSparse in PRMs, normally...");
85  } else {
86  // Just need to make the copy using the bijection but we only use
87  // multidim array
89  "encountered an unexpected MultiDim implementation");
90  }
91  }
92 
93  return p;
94  } catch (Exception&) {
95  if (p) delete p;
96 
97  throw;
98  }
99  }
100 
101  // the function used to combine two tables
102  template < typename GUM_SCALAR >
104  const Potential< GUM_SCALAR >& t2) {
105  return new Potential< GUM_SCALAR >(t1 * t2);
106  }
107 
108  template < typename GUM_SCALAR >
110  Set< Potential< GUM_SCALAR >* >& pool,
111  Set< Potential< GUM_SCALAR >* >& trash) {
112  Potential< GUM_SCALAR >* pot = nullptr;
113  Potential< GUM_SCALAR >* tmp = nullptr;
114 
116  var_set.insert(var);
118 
119  for (const auto p : pool)
120  if (p->contains(*var)) pots.insert(p);
121 
122  if (pots.size() == 0) {
123  return;
124  } else if (pots.size() == 1) {
125  tmp = const_cast< Potential< GUM_SCALAR >* >(*pots.begin());
126  pot = new Potential< GUM_SCALAR >(tmp->margSumOut(var_set));
127  } else {
129  tmp = Comb.combine(pots);
130  pot = new Potential< GUM_SCALAR >(tmp->margSumOut(var_set));
131  delete tmp;
132  }
133 
134  for (const auto p : pots) {
135  pool.erase(const_cast< Potential< GUM_SCALAR >* >(p));
136 
137  if (trash.exists(const_cast< Potential< GUM_SCALAR >* >(p))) {
138  trash.erase(const_cast< Potential< GUM_SCALAR >* >(p));
139  delete const_cast< Potential< GUM_SCALAR >* >(p);
140  }
141  }
142 
143  pool.insert(pot);
144  trash.insert(pot);
145  }
146 
147  template < typename GUM_SCALAR >
148  void eliminateNodes(const std::vector< const DiscreteVariable* >& elim_order,
149  Set< Potential< GUM_SCALAR >* >& pool,
150  Set< Potential< GUM_SCALAR >* >& trash) {
151  for (auto var : elim_order) {
152  eliminateNode(var, pool, trash);
153  }
154  }
155 
156  } /* namespace prm */
157 } // 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:57
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:36
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.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
iterator begin() const
The usual unsafe begin iterator to parse the set.
Definition: set_tpl.h:514
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:162
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:103
Multidimensional matrix stored as an array in memory.
Definition: multiDimArray.h:51
Set of pairs of elements with fast search for both elements.
Definition: bijection.h:1803
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
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:698
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:610
Multidimensional matrix stored as a sparse array in memory.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
Potential< GUM_SCALAR > * multPotential(const Potential< GUM_SCALAR > &t1, const Potential< GUM_SCALAR > &t2)