Main MRPT website > C++ reference for MRPT 1.5.6
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 implement the desired
24  * virtual methods, and add any required internal data.
25  * \ingroup mrpt_bayes_grp
26  */
27  template <class TStateSpace>
29  {
30  public:
32 
33  /** Virtual destructor
34  */
36  {
37  }
38 
39  /** Generates a set of N independent samples via rejection sampling.
40  * \param desiredSamples The number of desired samples to generate
41  * \param outSamples The output samples.
42  * \param timeoutTrials The maximum number of rejection trials for each generated sample (i.e. the maximum number of iterations). This can be used to set a limit to the time complexity of the algorithm for difficult probability densities.
43  * All will have equal importance weights (a property of rejection sampling), although those samples
44  * generated at timeout will have a different importance weights.
45  */
47  size_t desiredSamples,
48  std::vector<TParticle> &outSamples,
49  size_t timeoutTrials = 1000)
50  {
52 
53  TStateSpace x;
55 
56  // Set output size:
57  if ( outSamples.size() != desiredSamples )
58  {
59  // Free old memory:
60  outSamples.clear();
61 
62  // Reserve new memory:
63  outSamples.resize( desiredSamples );
64  for (it = outSamples.begin(); it != outSamples.end(); ++it)
65  it->d.reset(new TStateSpace);
66  }
67 
68  // Rejection sampling loop:
69  double acceptanceProb;
70  for (it = outSamples.begin();it!=outSamples.end();++it)
71  {
72  size_t timeoutCount = 0;
73  double bestLik = -1e250;
74  TStateSpace bestVal;
75  do
76  {
77  RS_drawFromProposal( *it->d );
78  acceptanceProb = RS_observationLikelihood( *it->d );
79  ASSERT_(acceptanceProb>=0 && acceptanceProb<=1);
80  if (acceptanceProb>bestLik)
81  {
82  bestLik = acceptanceProb;
83  bestVal = *it->d;
84  }
85  } while ( acceptanceProb < mrpt::random::randomGenerator.drawUniform(0.0,0.999) &&
86  (++timeoutCount)<timeoutTrials );
87 
88  // Save weights:
89  if (timeoutCount>=timeoutTrials)
90  {
91  it->log_w = log(bestLik);
92  *it->d = bestVal;
93  }
94  else
95  {
96  it->log_w = 0; // log(1.0);
97  }
98  } // end for it
99 
100  MRPT_END
101  }
102 
103  protected:
104  /** Generates one sample, drawing from some proposal distribution.
105  */
106  virtual void RS_drawFromProposal( TStateSpace &outSample ) = 0;
107 
108  /** Returns the NORMALIZED observation likelihood (linear, not exponential!!!) at a given point of the state space (values in the range [0,1]).
109  */
110  virtual double RS_observationLikelihood( const TStateSpace &x) = 0;
111 
112  }; // End of class def.
113 
114 } // End of namespace
115 } // End of namespace
116 
117 #endif
void rejectionSampling(size_t desiredSamples, std::vector< TParticle > &outSamples, size_t timeoutTrials=1000)
Generates a set of N independent samples via rejection sampling.
BASE_IMPEXP CRandomGenerator randomGenerator
A static instance of a CRandomGenerator class, for use in single-thread applications.
Scalar * iterator
Definition: eigen_plugins.h:23
#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:3516
CProbabilityParticle< TStateSpace > TParticle



Page generated by Doxygen 1.8.14 for MRPT 1.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019