Main MRPT website > C++ reference
MRPT logo
CRejectionSamplingCapable.h
Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                       http://www.mrpt.org/                                |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef CRejectionSamplingCapable_H
00029 #define CRejectionSamplingCapable_H
00030 
00031 #include <mrpt/utils/utils_defs.h>
00032 #include <mrpt/bayes/CProbabilityParticle.h>
00033 #include <mrpt/random.h>
00034 
00035 namespace mrpt
00036 {
00037 /// \ingroup mrpt_bayes_grp
00038 namespace bayes
00039 {
00040         /** A base class for implementing rejection sampling in a generic state space.
00041          *   See the main method CRejectionSamplingCapable::rejectionSampling
00042          *  To use this class, create your own class as a child of this one and implement the desired
00043          *   virtual methods, and add any required internal data.
00044          * \ingroup mrpt_bayes_grp
00045          */
00046         template <class TStateSpace>
00047         class CRejectionSamplingCapable
00048         {
00049         public:
00050                 typedef CProbabilityParticle<TStateSpace> TParticle;
00051 
00052         /** Virtual destructor
00053           */
00054         virtual ~CRejectionSamplingCapable()
00055                 {
00056                 }
00057 
00058                 /** Generates a set of N independent samples via rejection sampling.
00059                   * \param desiredSamples The number of desired samples to generate
00060                   * \param outSamples The output samples.
00061                   * \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.
00062                   *  All will have equal importance weights (a property of rejection sampling), although those samples
00063                   *   generated at timeout will have a different importance weights.
00064                   */
00065                 void rejectionSampling(
00066                         size_t                                                  desiredSamples,
00067                         std::vector<TParticle>                  &outSamples,
00068                         size_t                                                  timeoutTrials = 1000)
00069                 {
00070                         MRPT_START
00071 
00072                         TStateSpace                                                     x;
00073                         typename std::vector<TParticle>::iterator       it;
00074 
00075                         // Set output size:
00076                         if ( outSamples.size() != desiredSamples )
00077                         {
00078                                 // Free old memory:
00079                                 for (it = outSamples.begin();it!=outSamples.end();it++)
00080                                         delete (it->d);
00081                                 outSamples.clear();
00082 
00083                                 // Reserve new memory:
00084                                 outSamples.resize( desiredSamples );
00085                                 for (it = outSamples.begin();it!=outSamples.end();it++)
00086                                         it->d = new TStateSpace;
00087                         }
00088 
00089                         // Rejection sampling loop:
00090                         double  acceptanceProb;
00091                         for (it = outSamples.begin();it!=outSamples.end();it++)
00092                         {
00093                                 size_t  timeoutCount = 0;
00094                                 double          bestLik = -1e250;
00095                                 TStateSpace     bestVal;
00096                                 do
00097                                 {
00098                                         RS_drawFromProposal( *it->d );
00099                                         acceptanceProb = RS_observationLikelihood( *it->d );
00100                                         ASSERT_(acceptanceProb>=0 && acceptanceProb<=1);
00101                                         if (acceptanceProb>bestLik)
00102                                         {
00103                                                 bestLik = acceptanceProb;
00104                                                 bestVal = *it->d;
00105                                         }
00106                                 } while (       acceptanceProb < mrpt::random::randomGenerator.drawUniform(0.0,0.999) &&
00107                                                         (++timeoutCount)<timeoutTrials );
00108 
00109                                 // Save weights:
00110                                 if (timeoutCount>=timeoutTrials)
00111                                 {
00112                                         it->log_w = log(bestLik);
00113                                         *it->d    = bestVal;
00114                                 }
00115                                 else
00116                                 {
00117                                         it->log_w = 0; // log(1.0);
00118                                 }
00119                         } // end for it
00120 
00121                         MRPT_END
00122                 }
00123 
00124         protected:
00125                 /** Generates one sample, drawing from some proposal distribution.
00126                   */
00127                 virtual void RS_drawFromProposal( TStateSpace &outSample ) = 0;
00128 
00129                 /** Returns the NORMALIZED observation likelihood (linear, not exponential!!!) at a given point of the state space (values in the range [0,1]).
00130                   */
00131                 virtual double RS_observationLikelihood( const TStateSpace &x) = 0;
00132 
00133         }; // End of class def.
00134 
00135 } // End of namespace
00136 } // End of namespace
00137 
00138 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011