Main MRPT website > C++ reference for MRPT 1.9.9
CRejectionSamplingCapable.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #ifndef CRejectionSamplingCapable_H
10 #define CRejectionSamplingCapable_H
11 
12 #include <mrpt/utils/utils_defs.h>
14 #include <mrpt/random.h>
15 
16 namespace mrpt
17 {
18 /// \ingroup mrpt_bayes_grp
19 namespace bayes
20 {
21 /** A base class for implementing rejection sampling in a generic state space.
22  * See the main method CRejectionSamplingCapable::rejectionSampling
23  * To use this class, create your own class as a child of this one and
24  * implement the desired
25  * virtual methods, and add any required internal data.
26  * \ingroup mrpt_bayes_grp
27  */
28 template <class TStateSpace>
30 {
31  public:
33 
34  /** Virtual destructor
35  */
37  /** Generates a set of N independent samples via rejection sampling.
38  * \param desiredSamples The number of desired samples to generate
39  * \param outSamples The output samples.
40  * \param timeoutTrials The maximum number of rejection trials for each
41  * generated sample (i.e. the maximum number of iterations). This can be
42  * used to set a limit to the time complexity of the algorithm for difficult
43  * probability densities.
44  * All will have equal importance weights (a property of rejection
45  * sampling), although those samples
46  * generated at timeout will have a different importance weights.
47  */
49  size_t desiredSamples, std::vector<TParticle>& outSamples,
50  size_t timeoutTrials = 1000)
51  {
53 
54  TStateSpace x;
56 
57  // Set output size:
58  if (outSamples.size() != desiredSamples)
59  {
60  // Free old memory:
61  outSamples.clear();
62 
63  // Reserve new memory:
64  outSamples.resize(desiredSamples);
65  for (it = outSamples.begin(); it != outSamples.end(); ++it)
66  it->d.reset(new TStateSpace);
67  }
68 
69  // Rejection sampling loop:
70  double acceptanceProb;
71  for (it = outSamples.begin(); it != outSamples.end(); ++it)
72  {
73  size_t timeoutCount = 0;
74  double bestLik = -1e250;
75  TStateSpace bestVal;
76  do
77  {
78  RS_drawFromProposal(*it->d);
79  acceptanceProb = RS_observationLikelihood(*it->d);
80  ASSERT_(acceptanceProb >= 0 && acceptanceProb <= 1);
81  if (acceptanceProb > bestLik)
82  {
83  bestLik = acceptanceProb;
84  bestVal = *it->d;
85  }
86  } while (acceptanceProb < mrpt::random::getRandomGenerator().drawUniform(
87  0.0, 0.999) &&
88  (++timeoutCount) < timeoutTrials);
89 
90  // Save weights:
91  if (timeoutCount >= timeoutTrials)
92  {
93  it->log_w = log(bestLik);
94  *it->d = bestVal;
95  }
96  else
97  {
98  it->log_w = 0; // log(1.0);
99  }
100  } // end for it
101 
102  MRPT_END
103  }
104 
105  protected:
106  /** Generates one sample, drawing from some proposal distribution.
107  */
108  virtual void RS_drawFromProposal(TStateSpace& outSample) = 0;
109 
110  /** Returns the NORMALIZED observation likelihood (linear, not
111  * exponential!!!) at a given point of the state space (values in the range
112  * [0,1]).
113  */
114  virtual double RS_observationLikelihood(const TStateSpace& x) = 0;
115 
116 }; // End of class def.
117 
118 } // End of namespace
119 } // End of namespace
120 
121 #endif
void rejectionSampling(size_t desiredSamples, std::vector< TParticle > &outSamples, size_t timeoutTrials=1000)
Generates a set of N independent samples via rejection sampling.
Scalar * iterator
Definition: eigen_plugins.h:26
#define MRPT_END
virtual double RS_observationLikelihood(const TStateSpace &x)=0
Returns the NORMALIZED observation likelihood (linear, not exponential!!!) at a given point of the st...
#define MRPT_START
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
A template class for holding a the data and the weight of a particle.
virtual void RS_drawFromProposal(TStateSpace &outSample)=0
Generates one sample, drawing from some proposal distribution.
virtual ~CRejectionSamplingCapable()
Virtual destructor.
#define ASSERT_(f)
A base class for implementing rejection sampling in a generic state space.
GLenum GLint x
Definition: glext.h:3538
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
CProbabilityParticle< TStateSpace > TParticle



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019