aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
SVED.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 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, Potential< GUM_SCALAR >& j);
105 
106  /// @}
107  private:
108  /// Code alias
110  /// Code alias
111  typedef typename Set< Potential< GUM_SCALAR >* >::iterator_safe BucketSetIterator;
112 
113  /// Code alias
115 
117 
118  /// The Set<NodeId> returned by StructuredBayesBall<GUM_SCALAR> is unique
119  /// for
120  /// each
121  /// family of instances with the same requisite set (thus the same lifted
122  /// potentials).
124 
126 
128 
129  /// First pair -> requisite Attributes
130  /// Second pair -> requisite SlotChains
131  HashTable< const Set< NodeId >*, std::pair< Set< NodeId >*, Set< NodeId >* > > _req_set_;
132 
133  /// @name Inference sub methods.
134 
135  /// @{
136 
137  void _eliminateNodes_(const PRMInstance< GUM_SCALAR >* query,
138  NodeId id,
139  BucketSet& pool,
140  BucketSet& trash);
141 
142  void _eliminateNodesDownward_(const PRMInstance< GUM_SCALAR >* from,
143  const PRMInstance< GUM_SCALAR >* i,
144  BucketSet& pool,
145  BucketSet& trash,
146  List< const PRMInstance< GUM_SCALAR >* >& elim_list,
147  Set< const PRMInstance< GUM_SCALAR >* >& ignore);
148 
149  void _eliminateNodesUpward_(const PRMInstance< GUM_SCALAR >* i,
150  BucketSet& pool,
151  BucketSet& trash,
152  List< const PRMInstance< GUM_SCALAR >* >& elim_list,
153  Set< const PRMInstance< GUM_SCALAR >* >& ignore);
154 
155  void _eliminateNodesWithEvidence_(const PRMInstance< GUM_SCALAR >* i,
156  BucketSet& pool,
157  BucketSet& trash);
158 
159  void
160  _insertLiftedNodes_(const PRMInstance< GUM_SCALAR >* i, BucketSet& pool, BucketSet& trash);
161 
162  /// Returns true if second can be eliminated before first.
163  bool _checkElimOrder_(const PRMInstance< GUM_SCALAR >* first,
164  const PRMInstance< GUM_SCALAR >* second);
165 
166  void _initElimOrder_();
167 
168  void _insertEvidence_(const PRMInstance< GUM_SCALAR >* i, BucketSet& pool);
169 
171 
173  const PRMAggregate< GUM_SCALAR >* agg);
174 
175  void _initLiftedNodes_(const PRMInstance< GUM_SCALAR >* i, BucketSet& trash);
176 
177  void _initReqSets_(const PRMInstance< GUM_SCALAR >* i);
178 
180 
181  Set< NodeId >& _getSCSet_(const PRMInstance< GUM_SCALAR >* i);
182 
183  void _reduceElimList_(const PRMInstance< GUM_SCALAR >* i,
184  List< const PRMInstance< GUM_SCALAR >* >& elim_list,
185  List< const PRMInstance< GUM_SCALAR >* >& reduced_list,
186  Set< const PRMInstance< GUM_SCALAR >* >& ignore,
187  BucketSet& pool,
188  BucketSet& trash);
189 
190  std::string _trim_(const std::string& s);
191  /// @}
192  };
193 
194 
195 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
196  extern template class SVED< double >;
197 #endif
198 
199 
200  } /* namespace prm */
201 } /* namespace gum */
202 
203 #include <agrum/PRM/inference/SVED_tpl.h>
204 
205 #endif /* GUM_SVED_H */
virtual std::string name() const
Returns the name of the current inference algorithm.
Definition: SVED_tpl.h:571
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:44
virtual void evidenceAdded_(const Chain &chain)
See PRMInference::evidenceAdded_().
Definition: SVED_tpl.h:519
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:109
void _initReqSets_(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:442
SVED(const PRM< GUM_SCALAR > &prm, const PRMSystem< GUM_SCALAR > &model)
Default Constructor.
Definition: SVED_tpl.h:472
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
virtual void posterior_(const Chain &chain, Potential< GUM_SCALAR > &m)
See PRMInference::posterior_().
Definition: SVED_tpl.h:386
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:551
Sequence< std::string > * _class_elim_order_
Definition: SVED.h:125
void _initLiftedNodes_(const PRMInstance< GUM_SCALAR > *i, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:286
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:177
Set< NodeId > & _getSCSet_(const PRMInstance< GUM_SCALAR > *i)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:540
void _initElimOrder_()
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:349
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:501
void _eliminateNodesWithEvidence_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:244
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:512
StructuredBayesBall< GUM_SCALAR > _bb_
Definition: SVED.h:127
void _insertEvidence_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:481
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: SVED_tpl.h:494
virtual void evidenceRemoved_(const Chain &chain)
See PRMInference::evidenceRemoved_().
Definition: SVED_tpl.h:525
virtual void joint_(const std::vector< Chain > &queries, Potential< GUM_SCALAR > &j)
See PRMInference::joint_().
Definition: SVED_tpl.h:436
void _insertLiftedNodes_(const PRMInstance< GUM_SCALAR > *i, BucketSet &pool, BucketSet &trash)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:266
~SVED()
Destructor.
Definition: SVED_tpl.h:34
std::vector< NodeId > & _getElimOrder_(const PRMClass< GUM_SCALAR > &c)
Returns true if second can be eliminated before first.
Definition: SVED_tpl.h:489