aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
multipleInferenceEngine.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 #ifndef __MULTIPLE_INFERENCE_ENGINES__H__
23 #define __MULTIPLE_INFERENCE_ENGINES__H__
24 
25 /**
26  * @file
27  * @brief Abstract class representing CredalNet inference engines
28  * @author Matthieu HOURBRACQ and Pierre-Henri WUILLEMIN(@LIP6)
29  */
30 
31 /// @todo virtual for all functions that MAY be one day redefined in any derived
32 /// class
33 
34 #include <agrum/BN/inference/lazyPropagation.h>
35 #include <agrum/CN/inference/inferenceEngine.h>
36 
37 namespace gum {
38  namespace credal {
39 
40  /**
41  * @class MultipleInferenceEngine
42  * @headerfile <agrum/CN/multipleInferenceEngine.h>
43  * @brief Class template representing a CredalNet inference engine using
44  * one or more IBayesNet inference engines such as LazyPropagation. Extends
45  * InferenceEngine< GUM_SCALAR >. Used for outer multi-threading such as
46  * CNMonteCarloSampling.
47  * @ingroup cn_group
48  * @tparam GUM_SCALAR A floating type ( float, double, long double ... ).
49  * @tparam BNInferenceEngine A IBayesNet inference engine such as
50  * LazyPropagation.
51  * @author Matthieu HOURBRACQ and Pierre-Henri WUILLEMIN(@LIP6)
52  */
53  template < typename GUM_SCALAR, class BNInferenceEngine >
55  private:
56  /** To easily access InferenceEngine< GUM_SCALAR > methods. */
58 
63 
65  typedef std::vector< margi__ > margis__;
66  typedef std::vector< expe__ > expes__;
69 
70  typedef typename std::vector<
73 
74  /**
75  * @brief Ask for redundancy elimination of a node credal set of a calling
76  *thread.
77  *
78  * Called by updateThread_ if vertices are stored.
79  *
80  * @param id A constant reference to the node id whose credal set is to be
81  *checked for redundancy.
82  * @param vertex The vertex to add to the credal set.
83  * @param elimRedund \c true if redundancy elimination is to be performed,
84  *\c
85  *false otherwise and by default.
86  */
87  inline void updateThreadCredalSets__(const NodeId& id,
88  const std::vector< GUM_SCALAR >& vertex,
89  const bool& elimRedund);
90 
91  protected:
92  /** Threads lower marginals, one per thread. */
94  /** Threads upper marginals, one per thread. */
96  /** Threads lower expectations, one per thread. */
98  /** Threads upper expectations, one per thread. */
100  /** Threads modalities. */
102  /** Threads vertices. */
104  /** Threads evidence. */
106  /** Threads clusters. */
108 
109  /** Threads IBayesNet. */
110  typename std::vector< bnet__* > workingSet_;
111  /** Threads evidence. */
112  typename std::vector< List< const Potential< GUM_SCALAR >* >* > workingSetE_;
113 
114  /** Threads BNInferenceEngine. */
116  /** Threads optimal IBayesNet. */
118  /** Fusion of threads optimal IBayesNet. */
119  // OptBN< GUM_SCALAR > threadFusion_; // we should use this OptBN if omp
120  // is
121  // disabled (avoid creating 2 objects when only one is necessary)
122  // it should also avoid calling thread fusion operations
123 
124  /// @name Protected initialization methods
125  /// @{
126 
127  /**
128  * @brief Initialize threads data.
129  *
130  * @param num_threads The number of threads.
131  * @param storeVertices__ \c True if vertices should be stored, \c False
132  *otherwise.
133  * @param storeBNOpt__ \c True if optimal IBayesNet should be stored, \c
134  *false
135  *otherwise.
136  */
137  void initThreadsData_(const Size& num_threads,
138  const bool storeVertices__,
139  const bool storeBNOpt__);
140 
141  /// @}
142 
143  /// @name Protected algorithms methods
144  /// @{
145 
146  /**
147  * @brief Update thread information (marginals, expectations, IBayesNet,
148  *vertices) for a given node id.
149  *
150  * @param id The id of the node to be updated.
151  * @param vertex The vertex.
152  * @param elimRedund \c true if redundancy elimination is to be performed,
153  *\c
154  *false otherwise and by default.
155  * @return \c True if the IBayesNet is kept (for now), \c False otherwise.
156  */
157  inline bool updateThread_(const NodeId& id,
158  const std::vector< GUM_SCALAR >& vertex,
159  const bool& elimRedund = false);
160 
161  /**
162  * @brief Fusion of threads marginals.
163  */
164  inline void updateMarginals_();
165 
166  /**
167  * @brief Compute epsilon and update old marginals.
168  *
169  * @return Epsilon.
170  */
171  inline const GUM_SCALAR computeEpsilon_();
172 
173  /**
174  * Update old marginals (from current marginals). Call this once to
175  * initialize
176  * old marginals (after burn-in for example) and then use computeEpsilon_
177  * which
178  * does the same job but compute epsilon too.
179  */
180  void updateOldMarginals_();
181 
182  /// @}
183 
184  /// @name Proptected post-inference methods
185  /// @{
186 
187  /** Fusion of threads optimal IBayesNet. */
188  void optFusion_();
189  /** Fusion of threads expectations. */
190  void expFusion_();
191  /** @deprecated Fusion of threads vertices. */
192  void verticesFusion_(); // called ?? not done yet
193 
194  /// @}
195 
196  public:
197  /// @name Constructors / Destructors
198  /// @{
199 
200  /**
201  * Constructor.
202  * @param credalNet The CredalNet to be used.
203  */
204  explicit MultipleInferenceEngine(const CredalNet< GUM_SCALAR >& credalNet);
205 
206  /** Destructor. */
207  virtual ~MultipleInferenceEngine();
208 
209  /// @}
210 
211  /// @name Post-inference methods
212  /// @{
213  /**
214  * Erase all inference related data to perform another one. You need to
215  * insert
216  * evidence again if needed but modalities are kept. You can insert new
217  * ones by
218  * using the appropriate method which will delete the old ones.
219  */
220  virtual void eraseAllEvidence();
221  /// @}
222 
223  /// @name Pure virtual methods
224  /// @{
225  /** To be redefined by each credal net algorithm. Starts the inference. */
226  virtual void makeInference() = 0;
227  /// @}
228  };
229 
230 
231 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
232  extern template class MultipleInferenceEngine<
233  double,
234  gum::LazyPropagation< double > >;
235 
236  extern template class MultipleInferenceEngine< float,
237  gum::LazyPropagation< float > >;
238 #endif
239 
240  } // namespace credal
241 } // namespace gum
242 
243 #include <agrum/CN/inference/multipleInferenceEngine_tpl.h>
244 
245 #endif
expes__ l_expectationMax_
Threads upper expectations, one per thread.
credalSets__ l_marginalSets_
Threads vertices.
clusters__ l_clusters_
Threads clusters.
expes__ l_expectationMin_
Threads lower expectations, one per thread.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
virtual void makeInference()=0
To be redefined by each credal net algorithm.
void initThreadsData_(const Size &num_threads, const bool storeVertices__, const bool storeBNOpt__)
Initialize threads data.
margis__ l_marginalMin_
Threads lower marginals, one per thread.
MultipleInferenceEngine(const CredalNet< GUM_SCALAR > &credalNet)
Constructor.
virtual void eraseAllEvidence()
Erase all inference related data to perform another one.
void updateThreadCredalSets__(const NodeId &id, const std::vector< GUM_SCALAR > &vertex, const bool &elimRedund)
Ask for redundancy elimination of a node credal set of a calling thread.
bool updateThread_(const NodeId &id, const std::vector< GUM_SCALAR > &vertex, const bool &elimRedund=false)
Update thread information (marginals, expectations, IBayesNet, vertices) for a given node id...
void expFusion_()
Fusion of threads expectations.
std::vector< VarMod2BNsMap< GUM_SCALAR > *> l_optimalNet_
Threads optimal IBayesNet.
void updateMarginals_()
Fusion of threads marginals.
void optFusion_()
Fusion of threads optimal IBayesNet.
const GUM_SCALAR computeEpsilon_()
Compute epsilon and update old marginals.
void updateOldMarginals_()
Update old marginals (from current marginals).
InferenceEngine< GUM_SCALAR > infE__
To easily access InferenceEngine< GUM_SCALAR > methods.
margis__ l_marginalMax_
Threads upper marginals, one per thread.
namespace for all credal networks entities
Definition: LpInterface.cpp:37