Main MRPT website > C++ reference for MRPT 1.4.0
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-2016, 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
14#include <mrpt/random.h>
15
16namespace mrpt
17{
18/// \ingroup mrpt_bayes_grp
19namespace 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;
54 typename std::vector<TParticle>::iterator it;
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
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
A base class for implementing rejection sampling in a generic state space.
void rejectionSampling(size_t desiredSamples, std::vector< TParticle > &outSamples, size_t timeoutTrials=1000)
Generates a set of N independent samples via rejection sampling.
virtual double RS_observationLikelihood(const TStateSpace &x)=0
Returns the NORMALIZED observation likelihood (linear, not exponential!!!) at a given point of the st...
virtual ~CRejectionSamplingCapable()
Virtual destructor.
virtual void RS_drawFromProposal(TStateSpace &outSample)=0
Generates one sample, drawing from some proposal distribution.
CProbabilityParticle< TStateSpace > TParticle
#define MRPT_START
Definition: mrpt_macros.h:349
#define ASSERT_(f)
Definition: mrpt_macros.h:261
#define MRPT_END
Definition: mrpt_macros.h:353
BASE_IMPEXP CRandomGenerator randomGenerator
A static instance of a CRandomGenerator class, for use in single-thread applications.
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.



Page generated by Doxygen 1.9.5 for MRPT 1.4.0 SVN: at Sun Nov 27 02:56:59 UTC 2022