Main MRPT website > C++ reference
MRPT logo
CRejectionSamplingCapable.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef CRejectionSamplingCapable_H
29 #define CRejectionSamplingCapable_H
30 
31 #include <mrpt/utils/utils_defs.h>
33 #include <mrpt/random.h>
34 
35 namespace mrpt
36 {
37 /// \ingroup mrpt_bayes_grp
38 namespace bayes
39 {
40  /** A base class for implementing rejection sampling in a generic state space.
41  * See the main method CRejectionSamplingCapable::rejectionSampling
42  * To use this class, create your own class as a child of this one and implement the desired
43  * virtual methods, and add any required internal data.
44  * \ingroup mrpt_bayes_grp
45  */
46  template <class TStateSpace>
48  {
49  public:
51 
52  /** Virtual destructor
53  */
55  {
56  }
57 
58  /** Generates a set of N independent samples via rejection sampling.
59  * \param desiredSamples The number of desired samples to generate
60  * \param outSamples The output samples.
61  * \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.
62  * All will have equal importance weights (a property of rejection sampling), although those samples
63  * generated at timeout will have a different importance weights.
64  */
66  size_t desiredSamples,
67  std::vector<TParticle> &outSamples,
68  size_t timeoutTrials = 1000)
69  {
71 
72  TStateSpace x;
74 
75  // Set output size:
76  if ( outSamples.size() != desiredSamples )
77  {
78  // Free old memory:
79  for (it = outSamples.begin();it!=outSamples.end();it++)
80  delete (it->d);
81  outSamples.clear();
82 
83  // Reserve new memory:
84  outSamples.resize( desiredSamples );
85  for (it = outSamples.begin();it!=outSamples.end();it++)
86  it->d = new TStateSpace;
87  }
88 
89  // Rejection sampling loop:
90  double acceptanceProb;
91  for (it = outSamples.begin();it!=outSamples.end();it++)
92  {
93  size_t timeoutCount = 0;
94  double bestLik = -1e250;
95  TStateSpace bestVal;
96  do
97  {
98  RS_drawFromProposal( *it->d );
99  acceptanceProb = RS_observationLikelihood( *it->d );
100  ASSERT_(acceptanceProb>=0 && acceptanceProb<=1);
101  if (acceptanceProb>bestLik)
102  {
103  bestLik = acceptanceProb;
104  bestVal = *it->d;
105  }
106  } while ( acceptanceProb < mrpt::random::randomGenerator.drawUniform(0.0,0.999) &&
107  (++timeoutCount)<timeoutTrials );
108 
109  // Save weights:
110  if (timeoutCount>=timeoutTrials)
111  {
112  it->log_w = log(bestLik);
113  *it->d = bestVal;
114  }
115  else
116  {
117  it->log_w = 0; // log(1.0);
118  }
119  } // end for it
120 
121  MRPT_END
122  }
123 
124  protected:
125  /** Generates one sample, drawing from some proposal distribution.
126  */
127  virtual void RS_drawFromProposal( TStateSpace &outSample ) = 0;
128 
129  /** Returns the NORMALIZED observation likelihood (linear, not exponential!!!) at a given point of the state space (values in the range [0,1]).
130  */
131  virtual double RS_observationLikelihood( const TStateSpace &x) = 0;
132 
133  }; // End of class def.
134 
135 } // End of namespace
136 } // End of namespace
137 
138 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013