Main MRPT website > C++ reference
MRPT logo
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-2014, 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  for (it = outSamples.begin();it!=outSamples.end();it++)
61  delete (it->d);
62  outSamples.clear();
63 
64  // Reserve new memory:
65  outSamples.resize( desiredSamples );
66  for (it = outSamples.begin();it!=outSamples.end();it++)
67  it->d = new TStateSpace;
68  }
69 
70  // Rejection sampling loop:
71  double acceptanceProb;
72  for (it = outSamples.begin();it!=outSamples.end();it++)
73  {
74  size_t timeoutCount = 0;
75  double bestLik = -1e250;
76  TStateSpace bestVal;
77  do
78  {
79  RS_drawFromProposal( *it->d );
80  acceptanceProb = RS_observationLikelihood( *it->d );
81  ASSERT_(acceptanceProb>=0 && acceptanceProb<=1);
82  if (acceptanceProb>bestLik)
83  {
84  bestLik = acceptanceProb;
85  bestVal = *it->d;
86  }
87  } while ( acceptanceProb < mrpt::random::randomGenerator.drawUniform(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 exponential!!!) at a given point of the state space (values in the range [0,1]).
111  */
112  virtual double RS_observationLikelihood( const TStateSpace &x) = 0;
113 
114  }; // End of class def.
115 
116 } // End of namespace
117 } // End of namespace
118 
119 #endif
BASE_IMPEXP CRandomGenerator randomGenerator
A static instance of a CRandomGenerator class, for use in single-thread applications.
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: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.
CProbabilityParticle< TStateSpace > TParticle



Page generated by Doxygen 1.8.14 for MRPT 1.1.0 SVN: at lun oct 28 00:54:49 CET 2019 Hosted on:
SourceForge.net Logo