aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
CNMonteCarloSampling.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 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,
60  class BNInferenceEngine = LazyPropagation< GUM_SCALAR > >
63  private:
64  /** To easily acces MultipleInferenceEngine< GUM_SCALAR, BNInferenceEngine
65  * >
66  * methods. */
68 
69  /// @name Private initialization methods
70  /// @{
71  /** Initialize approximation Scheme. */
73  /** Initialize threads data. */
74  void mcThreadDataCopy__();
75  /// @}
76 
77  /// @name Private algorithm methods
78  /// @{
79  /** Thread samples a IBayesNet from the CredalNet. */
80  inline void verticesSampling__();
81 
82  /** Insert CredalNet evidence into a thread BNInferenceEngine. */
83  inline void insertEvidence__();
84 
85  /** Thread performs an inference using BNInferenceEngine. Calls
86  * verticesSampling__ and insertEvidence__. */
87  inline void threadInference__();
88 
89  /** Update thread data after a IBayesNet inference. */
90  inline void threadUpdate__();
91 
92  /**
93  * Get the binary representation of a given value.
94  * @param toFill A reference to the bits to fill. Size must be correct
95  * before
96  * passing argument (i.e. big enough to represent \c value)
97  * @param value The constant integer we want to binarize.
98  */
99  inline void binaryRep__(std::vector< bool >& toFill, const Idx value) const;
100 
101  /// @}
102 
103  protected:
104  public:
105  /// @name Constructors / Destructors
106  /// @{
107  /**
108  * Constructor.
109  * @param credalNet The CredalNet to be used by the algorithm.
110  */
111  explicit CNMonteCarloSampling(const CredalNet< GUM_SCALAR >& credalNet);
112  /** Destructor. */
113  virtual ~CNMonteCarloSampling();
114  /// @}
115 
116  /// @name Public algorithm methods
117  /// @{
118 
119  /** Starts the inference. */
120  void makeInference();
121 
122  /// @}
123 
124  /// unsigned int notOptDelete;
125 
126  virtual void insertEvidenceFile(const std::string& path) {
127  InferenceEngine< GUM_SCALAR >::insertEvidenceFile(path);
128  };
129 
130  protected:
131  bool repetitiveInd_;
132  };
133 
134 
135 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
136  extern template class CNMonteCarloSampling< float, LazyPropagation< float > >;
137 
138  extern template class CNMonteCarloSampling< double,
139  LazyPropagation< double > >;
140 #endif
141 
142 
143  } // namespace credal
144 } // namespace gum
145 
146 #include <agrum/CN/inference/CNMonteCarloSampling_tpl.h>
147 
148 #endif
void threadUpdate__()
Update thread data after a IBayesNet inference.
void verticesSampling__()
Thread samples a IBayesNet from the CredalNet.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
void mcThreadDataCopy__()
Initialize threads data.
void threadInference__()
Thread performs an inference using BNInferenceEngine.
void mcInitApproximationScheme__()
Initialize approximation Scheme.
void binaryRep__(std::vector< bool > &toFill, const Idx value) const
Get the binary representation of a given value.
CNMonteCarloSampling(const CredalNet< GUM_SCALAR > &credalNet)
Constructor.
void makeInference()
Starts the inference.
<agrum/CN/CNMonteCarloSampling.h>
virtual void insertEvidenceFile(const std::string &path)
unsigned int notOptDelete;
void insertEvidence__()
Insert CredalNet evidence into a thread BNInferenceEngine.
namespace for all credal networks entities
Definition: LpInterface.cpp:37