aGrUM  0.13.2
samplingInference.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
28 #ifndef GUM_SAMPLING_INFERENCE_H
29 #define GUM_SAMPLING_INFERENCE_H
30 
32 #include <agrum/BN/IBayesNet.h>
38 
39 namespace gum {
56  template < typename GUM_SCALAR >
57  class SamplingInference : public ApproximateInference< GUM_SCALAR > {
58  public:
59  // ############################################################################
61  // ############################################################################
63 
65 
69  explicit SamplingInference(const IBayesNet< GUM_SCALAR >* bn);
70 
72  ~SamplingInference() override;
73 
75 
87 
90 
103  const Potential< GUM_SCALAR >& currentPosterior(const std::string& name);
106 
107 
108  // ############################################################################
110  // ############################################################################
114 
116 
127  const Potential< GUM_SCALAR >& _posterior(NodeId id) override;
128 
130 
131 
134 
143  virtual void contextualize();
144 
145  // ############################################################################
147  // ############################################################################
149 
150 
152 
158  virtual void _setEstimatorFromBN();
159 
161 
171  GUM_SCALAR virtualLBPSize);
173 
174  protected:
177 
179  bool isSetEstimator = false;
180 
182  bool isContextualized = false;
183 
185  virtual Instantiation _burnIn() = 0;
186 
188 
193  virtual Instantiation _draw(float* w, Instantiation prev) = 0;
194 
196  void _makeInference() override;
197  void _loopApproxInference();
198 
200 
207  virtual void _addVarSample(NodeId nod, Instantiation* I);
208 
209 
211 
219 
220  void _onEvidenceAdded(NodeId id, bool isHardEvidence) override;
221 
222  void _onEvidenceErased(NodeId id, bool isHardEvidence) override;
223 
224  void _onAllEvidenceErased(bool contains_hard_evidence) override;
225 
226  void _onEvidenceChanged(NodeId id, bool hasChangedSoftHard) override;
227 
228  void _onBayesNetChanged(const IBayesNet< GUM_SCALAR >* bn) override;
229 
230  void _updateOutdatedBNStructure() override;
231 
232  void _updateOutdatedBNPotentials() override;
233 
234  void _onMarginalTargetAdded(NodeId id) override;
235 
236  void _onMarginalTargetErased(NodeId id) override;
237 
238  void _onAllMarginalTargetsAdded() override;
239 
240  void _onAllMarginalTargetsErased() override;
241 
242  void _onStateChanged() override;
243 
244  private:
246  };
247 
248 
249  extern template class SamplingInference< float >;
250  extern template class SamplingInference< double >;
251 } // namespace gum
252 
254 #endif
void _onAllEvidenceErased(bool contains_hard_evidence) override
fired before all the evidence are erased
void _onEvidenceAdded(NodeId id, bool isHardEvidence) override
fired after a new evidence is inserted
aGrUM&#39;s Potential is a multi-dimensional array with tensor operators.
Definition: potential.h:57
This file contains Gibbs sampling (for BNs) class definitions.
virtual void contextualize()
Simplifying the bayesian network with relevance reasonning to lighten the computational charge...
This file contains general scheme for iteratively convergent algorithms.
unsigned int NodeId
Type for node ids.
Definition: graphElements.h:97
SamplingInference(const IBayesNet< GUM_SCALAR > *bn)
default constructor
void _onEvidenceChanged(NodeId id, bool hasChangedSoftHard) override
fired after an evidence is changed, in particular when its status (soft/hard) changes ...
void _updateOutdatedBNStructure() override
prepares inference when the latter is in OutdatedBNStructure state
void _onMarginalTargetAdded(NodeId id) override
fired after a new marginal target is inserted
Class representing Bayesian networks.
~SamplingInference() override
destructor
virtual void _setEstimatorFromBN()
Initializes the estimators object linked to the simulation.
virtual Instantiation _draw(float *w, Instantiation prev)=0
draws a sample in the bayesian network given a previous one
Class representing the minimal interface for Bayesian Network.
Definition: IBayesNet.h:59
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
Implementation of the non pure virtual methods of class ApproximateInference.
This file contains the abstract inference class definition for computing (incrementally) marginal pos...
void _onStateChanged() override
fired when the stage is changed
void _onAllMarginalTargetsAdded() override
fired after all the nodes of the BN are added as marginal targets
const Potential< GUM_SCALAR > & currentPosterior(NodeId id)
Computes and returns the actual estimation of the posterior of a node.
virtual Instantiation _burnIn()=0
draws samples without updating the estimators
Estimator< GUM_SCALAR > __estimator
Estimator object designed to approximate target posteriors.
Header files of gum::Instantiation.
<agrum/BN/inference/loopyBeliefPropagation.h>
Portion of a BN identified by the list of nodes and a BayesNet.
BayesNetFragment< GUM_SCALAR > * __samplingBN
const Potential< GUM_SCALAR > & _posterior(NodeId id) override
Computes and returns the posterior of a node.
void _onMarginalTargetErased(NodeId id) override
fired before a marginal target is removed
void _makeInference() override
makes the inference by generating samples
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:80
virtual void _setEstimatorFromLBP(LoopyBeliefPropagation< GUM_SCALAR > *lbp, GUM_SCALAR virtualLBPSize)
Initializes the estimators object linked to the simulation.
bool isContextualized
whether the referenced Bayesian Network has been "contextualized"
void _onEvidenceErased(NodeId id, bool isHardEvidence) override
fired before an evidence is removed
void _onAllMarginalTargetsErased() override
fired before a all marginal targets are removed
This file contains estimating tools for approximate inference.
virtual void _onContextualize(BayesNetFragment< GUM_SCALAR > *bn)
fired when Bayesian network is contextualized
void _updateOutdatedBNPotentials() override
prepares inference when the latter is in OutdatedBNPotentials state
bool isSetEstimator
whether the Estimator object has been initialized
virtual void _addVarSample(NodeId nod, Instantiation *I)
adds a node to current instantiation
void _onBayesNetChanged(const IBayesNet< GUM_SCALAR > *bn) override
fired after a new Bayes net has been assigned to the engine
const IBayesNet< GUM_SCALAR > & samplingBN()
get the BayesNet which is used to really perform the sampling
Class representing Fragment of Bayesian networks.