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-2018, 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 
13 #include <mrpt/random.h>
14 
15 namespace mrpt
16 {
17 /// \ingroup mrpt_bayes_grp
18 namespace bayes
19 {
20 /** A base class for implementing rejection sampling in a generic state space.
21  * See the main method CRejectionSamplingCapable::rejectionSampling
22  * To use this class, create your own class as a child of this one and
23  * implement the desired
24  * virtual methods, and add any required internal data.
25  * \ingroup mrpt_bayes_grp
26  */
27 template <
28  class TStateSpace, mrpt::bayes::particle_storage_mode STORAGE =
31 {
32  public:
34 
35  /** Virtual destructor
36  */
38  /** Generates a set of N independent samples via rejection sampling.
39  * \param desiredSamples The number of desired samples to generate
40  * \param outSamples The output samples.
41  * \param timeoutTrials The maximum number of rejection trials for each
42  * generated sample (i.e. the maximum number of iterations). This can be
43  * used to set a limit to the time complexity of the algorithm for difficult
44  * probability densities.
45  * All will have equal importance weights (a property of rejection
46  * sampling), although those samples
47  * generated at timeout will have a different importance weights.
48  */
50  size_t desiredSamples, std::vector<TParticle>& outSamples,
51  size_t timeoutTrials = 1000)
52  {
54 
55  TStateSpace x;
57 
58  // Set output size:
59  if (outSamples.size() != desiredSamples)
60  {
61  // Free old memory:
62  outSamples.clear();
63 
64  // Reserve new memory:
65  outSamples.resize(desiredSamples);
66  for (it = outSamples.begin(); it != outSamples.end(); ++it)
67  it->d.reset(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 <
89  0.0, 0.999) &&
90  (++timeoutCount) < timeoutTrials);
91 
92  // Save weights:
93  if (timeoutCount >= timeoutTrials)
94  {
95  it->log_w = log(bestLik);
96  *it->d = bestVal;
97  }
98  else
99  {
100  it->log_w = 0; // log(1.0);
101  }
102  } // end for it
103 
104  MRPT_END
105  }
106 
107  protected:
108  /** Generates one sample, drawing from some proposal distribution.
109  */
110  virtual void RS_drawFromProposal(TStateSpace& outSample) = 0;
111 
112  /** Returns the NORMALIZED observation likelihood (linear, not
113  * exponential!!!) at a given point of the state space (values in the range
114  * [0,1]).
115  */
116  virtual double RS_observationLikelihood(const TStateSpace& x) = 0;
117 
118 }; // End of class def.
119 
120 } // End of namespace
121 } // End of namespace
122 
123 #endif
CProbabilityParticle.h
mrpt::bayes::CRejectionSamplingCapable
A base class for implementing rejection sampling in a generic state space.
Definition: CRejectionSamplingCapable.h:30
mrpt::bayes::particle_storage_mode
particle_storage_mode
use for CProbabilityParticle
Definition: CProbabilityParticle.h:20
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
random.h
mrpt::bayes::CProbabilityParticle
A template class for holding a the data and the weight of a particle.
Definition: CProbabilityParticle.h:38
mrpt::bayes::CRejectionSamplingCapable::~CRejectionSamplingCapable
virtual ~CRejectionSamplingCapable()
Virtual destructor.
Definition: CRejectionSamplingCapable.h:37
mrpt::bayes::CRejectionSamplingCapable::RS_drawFromProposal
virtual void RS_drawFromProposal(TStateSpace &outSample)=0
Generates one sample, drawing from some proposal distribution.
MRPT_START
#define MRPT_START
Definition: exceptions.h:262
mrpt::random::getRandomGenerator
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
Definition: RandomGenerator.cpp:19
MRPT_END
#define MRPT_END
Definition: exceptions.h:266
mrpt::bayes::particle_storage_mode::POINTER
@ POINTER
mrpt::bayes::CRejectionSamplingCapable::rejectionSampling
void rejectionSampling(size_t desiredSamples, std::vector< TParticle > &outSamples, size_t timeoutTrials=1000)
Generates a set of N independent samples via rejection sampling.
Definition: CRejectionSamplingCapable.h:49
iterator
Scalar * iterator
Definition: eigen_plugins.h:26
mrpt::bayes::CRejectionSamplingCapable::RS_observationLikelihood
virtual double RS_observationLikelihood(const TStateSpace &x)=0
Returns the NORMALIZED observation likelihood (linear, not exponential!!!) at a given point of the st...
x
GLenum GLint x
Definition: glext.h:3538



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST