aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
SVED.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 SVED (Structured Value Elimination with d-seperation).
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 #ifndef GUM_SVED_H
29 #define GUM_SVED_H
30 
31 #include <vector>
32 
33 #include <agrum/tools/core/set.h>
34 
35 #include <agrum/tools/graphs/algorithms/triangulations/defaultTriangulation.h>
36 #include <agrum/tools/graphs/algorithms/triangulations/partialOrderedTriangulation.h>
37 
38 #include <agrum/BN/inference/variableElimination.h>
39 
40 #include <agrum/tools/multidim/implementations/multiDimArray.h>
41 #include <agrum/tools/multidim/implementations/multiDimBucket.h>
42 #include <agrum/tools/multidim/potential.h>
43 
44 #include <agrum/PRM/inference/PRMInference.h>
45 #include <agrum/PRM/inference/structuredBayesBall.h>
46 
47 #include <agrum/PRM/classBayesNet.h>
48 #include <agrum/PRM/classDependencyGraph.h>
49 #include <agrum/PRM/instanceBayesNet.h>
50 namespace gum {
51  namespace prm {
52 
53  /**
54  * @class SVED
55  * @headerfile SVED.h <agrum/PRM/SVED.h>
56  * @brief This class is an implementation of the Structured Value
57  *Elimination
58  * algorithm on PRM<GUM_SCALAR>.
59  *
60  */
61  template < typename GUM_SCALAR >
62  class SVED: public PRMInference< GUM_SCALAR > {
63  public:
64  // ========================================================================
65  /// @name Constructors & destructor.
66  // ========================================================================
67  /// @{
68 
69  /// Default Constructor.
70  SVED(const PRM< GUM_SCALAR >& prm, const PRMSystem< GUM_SCALAR >& model);
71 
72  /// Destructor.
73  ~SVED();
74 
75  /// @}
76  // ========================================================================
77  /// @name Getters & setters.
78  // ========================================================================
79  /// @{
80 
81  /// Returns the name of the current inference algorithm
82  virtual std::string name() const;
83 
84  /// @}
85  protected:
86  // ========================================================================
87  /// @name Query methods.
88  // ========================================================================
89  /// @{
90 
91  /// Code alias.
92  typedef typename PRMInference< GUM_SCALAR >::Chain Chain;
93 
94  /// See PRMInference::evidenceAdded_().
95  virtual void evidenceAdded_(const Chain& chain);
96 
97  /// See PRMInference::evidenceRemoved_().
98  virtual void evidenceRemoved_(const Chain& chain);
99 
100  /// See PRMInference::posterior_().
101  virtual void posterior_(const Chain& chain, Potential< GUM_SCALAR >& m);
102 
103  /// See PRMInference::joint_().
104  virtual void joint_(const std::vector< Chain >& queries,
105  Potential< GUM_SCALAR >& j);
106 
107  /// @}
108  private:
109  /// Code alias
111  /// Code alias
112  typedef
114 
115  /// Code alias
116  typedef typename Set< MultiDimArray< GUM_SCALAR >* >::iterator_safe
118 
119  HashTable< const PRMClass< GUM_SCALAR >*, std::vector< NodeId >* >
121 
122  /// The Set<NodeId> returned by StructuredBayesBall<GUM_SCALAR> is unique
123  /// for
124  /// each
125  /// family of instances with the same requisite set (thus the same lifted
126  /// potentials).
128 
130 
132 
133  /// First pair -> requisite Attributes
134  /// Second pair -> requisite SlotChains
135  HashTable< const Set< NodeId >*,
136  std::pair< Set< NodeId >*, Set< NodeId >* > >
138 
139  /// @name Inference sub methods.
140 
141  /// @{
142 
143  void eliminateNodes__(const PRMInstance< GUM_SCALAR >* query,
144  NodeId id,
145  BucketSet& pool,
146  BucketSet& trash);
147 
149  const PRMInstance< GUM_SCALAR >* from,
150  const PRMInstance< GUM_SCALAR >* i,
151  BucketSet& pool,
152  BucketSet& trash,
153  List< const PRMInstance< GUM_SCALAR >* >& elim_list,
154  Set< const PRMInstance< GUM_SCALAR >* >& ignore);
155 
157  const PRMInstance< GUM_SCALAR >* i,
158  BucketSet& pool,
159  BucketSet& trash,
160  List< const PRMInstance< GUM_SCALAR >* >& elim_list,
161  Set< const PRMInstance< GUM_SCALAR >* >& ignore);
162 
163  void eliminateNodesWithEvidence__(const PRMInstance< GUM_SCALAR >* i,
164  BucketSet& pool,
165  BucketSet& trash);
166 
167  void insertLiftedNodes__(const PRMInstance< GUM_SCALAR >* i,
168  BucketSet& pool,
169  BucketSet& trash);
170 
171  /// Returns true if second can be eliminated before first.
172  bool checkElimOrder__(const PRMInstance< GUM_SCALAR >* first,
173  const PRMInstance< GUM_SCALAR >* second);
174 
175  void initElimOrder__();
176 
177  void insertEvidence__(const PRMInstance< GUM_SCALAR >* i, BucketSet& pool);
178 
180 
183  const PRMAggregate< GUM_SCALAR >* agg);
184 
185  void initLiftedNodes__(const PRMInstance< GUM_SCALAR >* i, BucketSet& trash);
186 
187  void initReqSets__(const PRMInstance< GUM_SCALAR >* i);
188 
190 
191  Set< NodeId >& getSCSet__(const PRMInstance< GUM_SCALAR >* i);
192 
193  void reduceElimList__(const PRMInstance< GUM_SCALAR >* i,
194  List< const PRMInstance< GUM_SCALAR >* >& elim_list,
195  List< const PRMInstance< GUM_SCALAR >* >& reduced_list,
196  Set< const PRMInstance< GUM_SCALAR >* >& ignore,
197  BucketSet& pool,
198  BucketSet& trash);
199 
200  std::string trim__(const std::string& s);
201  /// @}
202  };
203 
204 
205 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
206  extern template class SVED< double >;
207 #endif
208 
209 
210  } /* namespace prm */
211 } /* namespace gum */
212 
213 #include <agrum/PRM/inference/SVED_tpl.h>
214 
215 #endif /* GUM_SVED_H */
virtual std::string name() const
Returns the name of the current inference algorithm.
Definition: SVED_tpl.h:636
void initElimOrder__()
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:399
virtual void evidenceAdded_(const Chain &chain)
See PRMInference::evidenceAdded_().
Definition: SVED_tpl.h:575
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:123
std::string trim__(const std::string &s)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:548
SVED(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &model)
Default Constructor.
Definition: SVED_tpl.h:525
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:609
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
void initLiftedNodes__(const PRMInstance< GUM_SCALAR > *i, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:329
virtual void posterior_(const Chain &chain, Potential< GUM_SCALAR > &m)
See PRMInference::posterior_().
Definition: SVED_tpl.h:436
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:567
Set< NodeId > & getSCSet__(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:599
void insertLiftedNodes__(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:309
void insertEvidence__(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:535
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:45
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:555
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)
void eliminateNodesWithEvidence__(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:285
std::vector< NodeId > & getElimOrder__(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:543
virtual void evidenceRemoved_(const Chain &chain)
See PRMInference::evidenceRemoved_().
Definition: SVED_tpl.h:581
StructuredBayesBall< GUM_SCALAR > bb__
Definition: SVED.h:131
virtual void joint_(const std::vector< Chain > &queries, Potential< GUM_SCALAR > &j)
See PRMInference::joint_().
Definition: SVED_tpl.h:488
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:204
void initReqSets__(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:494
Sequence< std::string > * class_elim_order__
Definition: SVED.h:129
~SVED()
Destructor.
Definition: SVED_tpl.h:34