aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
CNMonteCarloSampling.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 Inference by basic sampling algorithm (pure random) of bnet in credal
25  * networks.
26  * @author Matthieu HOURBRACQ and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 #ifndef __CN_MC_SAMPLING__H__
30 #define __CN_MC_SAMPLING__H__
31 
32 #include <agrum/CN/inference/multipleInferenceEngine.h>
33 #include <limits>
34 
35 #include <agrum/BN/inference/lazyPropagation.h>
36 
37 namespace gum {
38  namespace credal {
39 
40  /**
41  * @class CNMonteCarloSampling CNMonteCarloSampling.h
42  *<agrum/CN/CNMonteCarloSampling.h>
43  * @brief Inference by basic sampling algorithm (pure random) of bnet in
44  *credal
45  *networks.
46  * @ingroup cn_group
47  * @tparam GUM_SCALAR A floating type ( float, double, long double ... ).
48  * @tparam BNInferenceEngine A IBayesNet inference engine such as
49  *LazyPropagation
50  *( recommanded ).
51  * @author Matthieu HOURBRACQ and Pierre-Henri WUILLEMIN(@LIP6)
52  *
53  * @warning p(e) must be available ( by a call to
54  *my_BNInferenceEngine.evidenceMarginal() ) !! the vertices are correct if
55  *p(e) >
56  *0 for a sample
57  * the test is made once
58  */
59  template < typename GUM_SCALAR, class BNInferenceEngine = LazyPropagation< GUM_SCALAR > >
61  private:
62  /** To easily acces MultipleInferenceEngine< GUM_SCALAR, BNInferenceEngine
63  * >
64  * methods. */
66 
67  /// @name Private initialization methods
68  /// @{
69  /** Initialize approximation Scheme. */
71  /** Initialize threads data. */
72  void _mcThreadDataCopy_();
73  /// @}
74 
75  /// @name Private algorithm methods
76  /// @{
77  /** Thread samples a IBayesNet from the CredalNet. */
78  inline void _verticesSampling_();
79 
80  /** Insert CredalNet evidence into a thread BNInferenceEngine. */
81  inline void _insertEvidence_();
82 
83  /** Thread performs an inference using BNInferenceEngine. Calls
84  * _verticesSampling_ and _insertEvidence_. */
85  inline void _threadInference_();
86 
87  /** Update thread data after a IBayesNet inference. */
88  inline void _threadUpdate_();
89 
90  /**
91  * Get the binary representation of a given value.
92  * @param toFill A reference to the bits to fill. Size must be correct
93  * before
94  * passing argument (i.e. big enough to represent \c value)
95  * @param value The constant integer we want to binarize.
96  */
97  inline void _binaryRep_(std::vector< bool >& toFill, const Idx value) const;
98 
99  /// @}
100 
101  protected:
102  public:
103  /// @name Constructors / Destructors
104  /// @{
105  /**
106  * Constructor.
107  * @param credalNet The CredalNet to be used by the algorithm.
108  */
109  explicit CNMonteCarloSampling(const CredalNet< GUM_SCALAR >& credalNet);
110  /** Destructor. */
111  virtual ~CNMonteCarloSampling();
112  /// @}
113 
114  /// @name Public algorithm methods
115  /// @{
116 
117  /** Starts the inference. */
118  void makeInference();
119 
120  /// @}
121 
122  /// unsigned int notOptDelete;
123 
124  virtual void insertEvidenceFile(const std::string& path) {
125  InferenceEngine< GUM_SCALAR >::insertEvidenceFile(path);
126  };
127 
128  protected:
129  bool repetitiveInd_;
130  };
131 
132 
133 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
134  extern template class CNMonteCarloSampling< float, LazyPropagation< float > >;
135 
136  extern template class CNMonteCarloSampling< double, LazyPropagation< double > >;
137 #endif
138 
139 
140  } // namespace credal
141 } // namespace gum
142 
143 #include <agrum/CN/inference/CNMonteCarloSampling_tpl.h>
144 
145 #endif
void _threadInference_()
Thread performs an inference using BNInferenceEngine.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
void _mcThreadDataCopy_()
Initialize threads data.
void _mcInitApproximationScheme_()
Initialize approximation Scheme.
CNMonteCarloSampling(const CredalNet< GUM_SCALAR > &credalNet)
Constructor.
void makeInference()
Starts the inference.
<agrum/CN/CNMonteCarloSampling.h>
void _binaryRep_(std::vector< bool > &toFill, const Idx value) const
Get the binary representation of a given value.
virtual void insertEvidenceFile(const std::string &path)
unsigned int notOptDelete;
void _verticesSampling_()
Thread samples a IBayesNet from the CredalNet.
void _insertEvidence_()
Insert CredalNet evidence into a thread BNInferenceEngine.
void _threadUpdate_()
Update thread data after a IBayesNet inference.
namespace for all credal networks entities
Definition: LpInterface.cpp:37