aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
SVE.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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, Potential< GUM_SCALAR >& j);
108 
109  /// @}
110  private:
111  /// Code alias
113  /// Code alias
114  typedef typename Set< Potential< GUM_SCALAR >* >::iterator_safe BucketSetIterator;
115 
116  /// Code alias
118 
120 
122 
124 
125  HashTable< const PRMInstance< GUM_SCALAR >*, Set< const DiscreteVariable* >* >
127 
128  /// Some variable must be delayed for more than one
129  /// PRMInstance<GUM_SCALAR>,
130  /// when
131  /// the delayed
132  /// variable counter reach 0 it can be eliminated.
134 
136 
137  /// @name Inference sub methods.
138 
139  /// @{
140 
141  void _eliminateNodes_(const PRMInstance< GUM_SCALAR >* query,
142  NodeId id,
143  BucketSet& pool,
144  BucketSet& trash);
145 
146  void _eliminateNodesDownward_(const PRMInstance< GUM_SCALAR >* from,
147  const PRMInstance< GUM_SCALAR >* i,
148  BucketSet& pool,
149  BucketSet& trash,
150  List< const PRMInstance< GUM_SCALAR >* >& elim_list,
151  Set< const PRMInstance< GUM_SCALAR >* >& ignore,
152  Set< const PRMInstance< GUM_SCALAR >* >& eliminated);
153 
154  void _eliminateNodesUpward_(const PRMInstance< GUM_SCALAR >* i,
155  BucketSet& pool,
156  BucketSet& trash,
157  List< const PRMInstance< GUM_SCALAR >* >& elim_list,
158  Set< const PRMInstance< GUM_SCALAR >* >& ignore,
159  Set< const PRMInstance< GUM_SCALAR >* >& eliminated);
160 
161  void _eliminateNodesWithEvidence_(const PRMInstance< GUM_SCALAR >* i,
162  BucketSet& pool,
163  BucketSet& trash,
164  Set< NodeId >* delayedVars = 0);
165 
166  void _eliminateDelayedVariables_(const PRMInstance< GUM_SCALAR >* i,
167  BucketSet& pool,
168  BucketSet& trash);
169 
170  void
171  _insertLiftedNodes_(const PRMInstance< GUM_SCALAR >* i, BucketSet& pool, BucketSet& trash);
172 
173  void _variableElimination_(const PRMInstance< GUM_SCALAR >* i,
174  BucketSet& pool,
175  BucketSet& trash,
176  Set< NodeId >* delayedVars = 0);
177 
178  /// Returns true if second can be eliminated before first.
179  bool _checkElimOrder_(const PRMInstance< GUM_SCALAR >* first,
180  const PRMInstance< GUM_SCALAR >* second);
181 
182  void _initElimOrder_();
183 
184  void _insertEvidence_(const PRMInstance< GUM_SCALAR >* i, BucketSet& pool);
185 
186  /// When there is a loop in the references some variable elimination
187  /// must be delayed, this methods add such variable to _delayedVariables_
188  /// to keep track of them.
189  /// @param i An PRMInstance<GUM_SCALAR> with a child of j->get(id).
190  /// @param j The PRMInstance<GUM_SCALAR> with the delayed variable.
191  /// @param id The NodeId of the delayed variable.
192  void _addDelayedVariable_(const PRMInstance< GUM_SCALAR >* i,
193  const PRMInstance< GUM_SCALAR >* j,
194  NodeId id);
195 
197 
199  const PRMAggregate< GUM_SCALAR >* agg);
200 
201  void _initLiftedNodes_(const PRMClass< GUM_SCALAR >& c);
202  std::string _trim_(const std::string& s);
203 
204  /// @}
205  };
206 
207 
208 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
209  extern template class SVE< double >;
210 #endif
211 
212 
213  } /* namespace prm */
214 } /* namespace gum */
215 
216 #include <agrum/PRM/inference/SVE_tpl.h>
217 
218 #endif /* GUM_SVE_H */
BucketSet _lifted_trash_
Definition: SVE.h:135
HashTable< const PRMInstance< GUM_SCALAR > *, Set< const DiscreteVariable *> *> _delayedVariables_
Definition: SVE.h:126
void _insertLiftedNodes_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:456
~SVE()
Destructor.
Definition: SVE_tpl.h:106
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:630
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
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:375
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:133
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:282
void _insertEvidence_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:610
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:657
HashTable< const PRMClass< GUM_SCALAR > *, BucketSet *> _lifted_pools_
Definition: SVE.h:121
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:322
virtual void evidenceRemoved_(const Chain &chain)
See PRMInference<GUM_SCALAR>::evidenceRemoved_().
Definition: SVE_tpl.h:652
virtual void posterior_(const Chain &chain, Potential< GUM_SCALAR > &m)
See PRMInference<GUM_SCALAR>::posterior_().
Definition: SVE_tpl.h:564
virtual void evidenceAdded_(const Chain &chain)
See PRMInference<GUM_SCALAR>::evidenceAdded_().
Definition: SVE_tpl.h:647
SVE(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &system)
Default Constructor.
Definition: SVE_tpl.h:602
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:234
virtual void joint_(const std::vector< Chain > &queries, Potential< GUM_SCALAR > &j)
See PRMInference<GUM_SCALAR>::joint_().
Definition: SVE_tpl.h:596
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)
std::string _trim_(const std::string &s)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:623
std::vector< NodeId > & _getElimOrder_(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:618
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
virtual std::string name() const
Returns the name of the current inference algorithm.
Definition: SVE_tpl.h:679
Sequence< std::string > * _class_elim_order_
Definition: SVE.h:123
void _initElimOrder_()
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:527
void _initLiftedNodes_(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition: SVE_tpl.h:476
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
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:641