aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
SVE.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Headers of SVE (Structured Variable Elimination).
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 #ifndef GUM_SVE_H
30 #define GUM_SVE_H
31 
32 #include <vector>
33 
34 #include <agrum/tools/core/set.h>
35 
36 #include <agrum/tools/graphs/algorithms/triangulations/defaultTriangulation.h>
37 #include <agrum/tools/graphs/algorithms/triangulations/partialOrderedTriangulation.h>
38 
39 #include <agrum/BN/inference/variableElimination.h>
40 
41 #include <agrum/tools/multidim/implementations/multiDimArray.h>
42 #include <agrum/tools/multidim/implementations/multiDimBucket.h>
43 #include <agrum/tools/multidim/implementations/multiDimSparse.h>
44 #include <agrum/tools/multidim/potential.h>
45 
46 #include <agrum/PRM/utils_prm.h>
47 
48 #include <agrum/PRM/inference/PRMInference.h>
49 
50 #include <agrum/PRM/classBayesNet.h>
51 #include <agrum/PRM/instanceBayesNet.h>
52 
53 namespace gum {
54  namespace prm {
55 
56  /**
57  * @class SVE
58  * @headerfile SVE.h <agrum/PRM/SVE.h>
59  * @brief This class is an implementation of the Structured Variable
60  *Elimination
61  * algorithm on PRM<GUM_SCALAR>.
62  *
63  */
64  template < typename GUM_SCALAR >
65  class SVE: public PRMInference< GUM_SCALAR > {
66  public:
67  // ========================================================================
68  /// @name Constructors & destructor.
69  // ========================================================================
70  /// @{
71 
72  /// Default Constructor.
73  SVE(const PRM< GUM_SCALAR >& prm, const PRMSystem< GUM_SCALAR >& system);
74 
75  /// Destructor.
76  ~SVE();
77 
78  /// @}
79  // ========================================================================
80  /// @name Getters & setters.
81  // ========================================================================
82  /// @{
83 
84  /// Returns the name of the current inference algorithm
85  virtual std::string name() const;
86 
87  /// @}
88  protected:
89  // ========================================================================
90  /// @name Query methods.
91  // ========================================================================
92  /// @{
93 
94  /// Code alias.
95  typedef typename PRMInference< GUM_SCALAR >::Chain Chain;
96 
97  /// See PRMInference<GUM_SCALAR>::evidenceAdded_().
98  virtual void evidenceAdded_(const Chain& chain);
99 
100  /// See PRMInference<GUM_SCALAR>::evidenceRemoved_().
101  virtual void evidenceRemoved_(const Chain& chain);
102 
103  /// See PRMInference<GUM_SCALAR>::posterior_().
104  virtual void posterior_(const Chain& chain, Potential< GUM_SCALAR >& m);
105 
106  /// See PRMInference<GUM_SCALAR>::joint_().
107  virtual void joint_(const std::vector< Chain >& queries,
108  Potential< GUM_SCALAR >& j);
109 
110  /// @}
111  private:
112  /// Code alias
114  /// Code alias
115  typedef
117 
118  /// Code alias
119  typedef typename Set< MultiDimArray< GUM_SCALAR >* >::iterator_safe
121 
122  HashTable< const PRMClass< GUM_SCALAR >*, std::vector< NodeId >* >
124 
126 
128 
129  HashTable< const PRMInstance< GUM_SCALAR >*,
130  Set< const DiscreteVariable* >* >
132 
133  /// Some variable must be delayed for more than one
134  /// PRMInstance<GUM_SCALAR>,
135  /// when
136  /// the delayed
137  /// variable counter reach 0 it can be eliminated.
139 
141 
142  /// @name Inference sub methods.
143 
144  /// @{
145 
146  void eliminateNodes__(const PRMInstance< GUM_SCALAR >* query,
147  NodeId id,
148  BucketSet& pool,
149  BucketSet& trash);
150 
152  const PRMInstance< GUM_SCALAR >* from,
153  const PRMInstance< GUM_SCALAR >* i,
154  BucketSet& pool,
155  BucketSet& trash,
156  List< const PRMInstance< GUM_SCALAR >* >& elim_list,
157  Set< const PRMInstance< GUM_SCALAR >* >& ignore,
158  Set< const PRMInstance< GUM_SCALAR >* >& eliminated);
159 
161  const PRMInstance< GUM_SCALAR >* i,
162  BucketSet& pool,
163  BucketSet& trash,
164  List< const PRMInstance< GUM_SCALAR >* >& elim_list,
165  Set< const PRMInstance< GUM_SCALAR >* >& ignore,
166  Set< const PRMInstance< GUM_SCALAR >* >& eliminated);
167 
168  void eliminateNodesWithEvidence__(const PRMInstance< GUM_SCALAR >* i,
169  BucketSet& pool,
170  BucketSet& trash,
171  Set< NodeId >* delayedVars = 0);
172 
173  void eliminateDelayedVariables__(const PRMInstance< GUM_SCALAR >* i,
174  BucketSet& pool,
175  BucketSet& trash);
176 
177  void insertLiftedNodes__(const PRMInstance< GUM_SCALAR >* i,
178  BucketSet& pool,
179  BucketSet& trash);
180 
181  void variableElimination__(const PRMInstance< GUM_SCALAR >* i,
182  BucketSet& pool,
183  BucketSet& trash,
184  Set< NodeId >* delayedVars = 0);
185 
186  /// Returns true if second can be eliminated before first.
187  bool checkElimOrder__(const PRMInstance< GUM_SCALAR >* first,
188  const PRMInstance< GUM_SCALAR >* second);
189 
190  void initElimOrder__();
191 
192  void insertEvidence__(const PRMInstance< GUM_SCALAR >* i, BucketSet& pool);
193 
194  /// When there is a loop in the references some variable elimination
195  /// must be delayed, this methods add such variable to delayedVariables__
196  /// to keep track of them.
197  /// @param i An PRMInstance<GUM_SCALAR> with a child of j->get(id).
198  /// @param j The PRMInstance<GUM_SCALAR> with the delayed variable.
199  /// @param id The NodeId of the delayed variable.
200  void addDelayedVariable__(const PRMInstance< GUM_SCALAR >* i,
201  const PRMInstance< GUM_SCALAR >* j,
202  NodeId id);
203 
205 
208  const PRMAggregate< GUM_SCALAR >* agg);
209 
210  void initLiftedNodes__(const PRMClass< GUM_SCALAR >& c);
211  std::string trim__(const std::string& s);
212 
213  /// @}
214  };
215 
216 
217 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
218  extern template class SVE< double >;
219 #endif
220 
221 
222  } /* namespace prm */
223 } /* namespace gum */
224 
225 #include <agrum/PRM/inference/SVE_tpl.h>
226 
227 #endif /* GUM_SVE_H */
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:246
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:418
~SVE()
Destructor.
Definition: SVE_tpl.h:107
Sequence< std::string > * class_elim_order__
Definition: SVE.h:127
void eliminateDelayedVariables__(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:218
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
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:138
HashTable< const PRMClass< GUM_SCALAR > *, BucketSet *> lifted_pools__
Definition: SVE.h:125
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:686
void initElimOrder__()
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:581
void insertLiftedNodes__(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:506
virtual void evidenceRemoved_(const Chain &chain)
See PRMInference<GUM_SCALAR>::evidenceRemoved_().
Definition: SVE_tpl.h:710
void insertEvidence__(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:666
virtual void posterior_(const Chain &chain, Potential< GUM_SCALAR > &m)
See PRMInference<GUM_SCALAR>::posterior_().
Definition: SVE_tpl.h:618
virtual void evidenceAdded_(const Chain &chain)
See PRMInference<GUM_SCALAR>::evidenceAdded_().
Definition: SVE_tpl.h:705
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:311
SVE(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default Constructor.
Definition: SVE_tpl.h:657
virtual void joint_(const std::vector< Chain > &queries, Potential< GUM_SCALAR > &j)
See PRMInference<GUM_SCALAR>::joint_().
Definition: SVE_tpl.h:651
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)
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:127
std::string trim__(const std::string &s)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:679
virtual std::string name() const
Returns the name of the current inference algorithm.
Definition: SVE_tpl.h:739
std::vector< NodeId > & getElimOrder__(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:674
void initLiftedNodes__(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:526
HashTable< const PRMInstance< GUM_SCALAR > *, Set< const DiscreteVariable *> *> delayedVariables__
Definition: SVE.h:131
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:698
BucketSet lifted_trash__
Definition: SVE.h:140
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:716
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:353