Main MRPT website > C++ reference
MRPT logo
CParticleFilterCapable.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 CPARTICLEFILTERCAPABLE_H
29 #define CPARTICLEFILTERCAPABLE_H
30 
31 #include <mrpt/utils/utils_defs.h>
33 
34 namespace mrpt
35 {
36 namespace bayes
37 {
38  #define INVALID_LIKELIHOOD_VALUE (-1e300) // An invalid log-likelihood value, used to signal non-initialized likelihood variables.
39 
40  /** This virtual class defines the interface that any particles based PDF class must implement in order to be executed by a mrpt::bayes::CParticleFilter.
41  *
42  * See the <a href="http://www.mrpt.org/Particle_Filter_Tutorial" >Particle Filter tutorial</a> explaining how to use the particle filter-related classes.
43  * \sa CParticleFilter, CParticleFilterData
44  * \ingroup mrpt_base_grp
45  */
47  {
48  friend class CParticleFilter;
49 
50  private:
52 
53  public:
54 
55  CParticleFilterCapable() : m_fastDrawAuxiliary()
56  { }
57 
58 
59  /** Virtual destructor
60  */
62  {
63  }
64 
65  /** A callback function type for evaluating the probability of m_particles of being selected, used in "fastDrawSample".
66  * The default evaluator function "defaultEvaluator" simply returns the particle weight.
67  * \param index This is the index of the particle its probability is being computed.
68  * \param action The value of this is the parameter passed to "prepareFastDrawSample"
69  * \param observation The value of this is the parameter passed to "prepareFastDrawSample"
70  * The action and the observation are declared as "void*" for a greater flexibility.
71  * \sa prepareFastDrawSample
72  */
73  typedef double ( *TParticleProbabilityEvaluator) (
75  const CParticleFilterCapable *obj,
76  size_t index,
77  const void * action,
78  const void * observation );
79 
80  /** The default evaluator function, which simply returns the particle weight.
81  * The action and the observation are declared as "void*" for a greater flexibility.
82  * \sa prepareFastDrawSample
83  */
84  static double defaultEvaluator(
86  const CParticleFilterCapable *obj,
87  size_t index,
88  const void * action,
89  const void * observation )
90  {
91  MRPT_UNUSED_PARAM(action); MRPT_UNUSED_PARAM(observation);
92  return obj->getW(index);
93  }
94 
95  /** Prepares data structures for calling fastDrawSample method next.
96  * This method must be called once before using "fastDrawSample" (calling this more than once has no effect, but it takes time for nothing!)
97  * The behavior depends on the configuration of the PF (see CParticleFilter::TParticleFilterOptions):
98  * - <b>DYNAMIC SAMPLE SIZE=NO</b>: In this case this method fills out an internal array (m_fastDrawAuxiliary.alreadyDrawnIndexes) with
99  * the random indexes generated according to the selected resample scheme in TParticleFilterOptions. Those indexes are
100  * read sequentially by subsequent calls to fastDrawSample.
101  * - <b>DYNAMIC SAMPLE SIZE=YES</b>: Then:
102  * - If TParticleFilterOptions.resamplingMethod = prMultinomial, the internal buffers will be filled out (m_fastDrawAuxiliary.CDF, CDF_indexes & PDF) and
103  * then fastDrawSample can be called an arbitrary number of times to generate random indexes.
104  * - For the rest of resampling algorithms, an exception will be raised since they are not appropriate for a dynamic (unknown in advance) number of particles.
105  *
106  * The function pointed by "partEvaluator" should take into account the particle filter algorithm selected in "m_PFAlgorithm".
107  * If called without arguments (defaultEvaluator), the default behavior is to draw samples with a probability proportional to their current weights.
108  * The action and the observation are declared as "void*" for a greater flexibility.
109  * For a more detailed information see the <a href="http://www.mrpt.org/Particle_Filters" >Particle Filter tutorial</a>.
110  * Custom supplied "partEvaluator" functions must take into account the previous particle weight, i.e. multiplying the current observation likelihood by the weights.
111  * \sa fastDrawSample
112  */
113  void prepareFastDrawSample(
115  TParticleProbabilityEvaluator partEvaluator = defaultEvaluator,
116  const void * action = NULL,
117  const void * observation = NULL
118  ) const;
119 
120  /** Draws a random sample from the particle filter, in such a way that each particle has a probability proportional to its weight (in the standard PF algorithm).
121  * This method can be used to generate a variable number of m_particles when resampling: to vary the number of m_particles in the filter.
122  * See prepareFastDrawSample for more information, or the <a href="http://www.mrpt.org/Particle_Filters" >Particle Filter tutorial</a>.
123  *
124  * NOTES:
125  * - You MUST call "prepareFastDrawSample" ONCE before calling this method. That method must be called after modifying the particle filter (executing one step, resampling, etc...)
126  * - This method returns ONE index for the selected ("drawn") particle, in the range [0,M-1]
127  * - You do not need to call "normalizeWeights" before calling this.
128  * \sa prepareFastDrawSample
129  */
130  size_t fastDrawSample( const bayes::CParticleFilter::TParticleFilterOptions &PF_options ) const;
131 
132  /** Access to i'th particle (logarithm) weight, where first one is index 0.
133  */
134  virtual double getW(size_t i) const = 0;
135 
136  /** Modifies i'th particle (logarithm) weight, where first one is index 0.
137  */
138  virtual void setW(size_t i, double w) = 0;
139 
140  /** Get the m_particles count.
141  */
142  virtual size_t particlesCount() const = 0;
143 
144  /** Performs the prediction stage of the Particle Filter.
145  * This method simply selects the appropiate protected method according to the particle filter algorithm to run.
146  * \sa prediction_and_update_pfStandardProposal,prediction_and_update_pfAuxiliaryPFStandard,prediction_and_update_pfOptimalProposal,prediction_and_update_pfAuxiliaryPFOptimal
147  */
148  void prediction_and_update(
149  const mrpt::slam::CActionCollection * action,
150  const mrpt::slam::CSensoryFrame * observation,
152  );
153 
154  /** Performs the substitution for internal use of resample in particle filter algorithm, don't call it directly.
155  * \param indx The indices of current m_particles to be saved as the new m_particles set.
156  */
157  virtual void performSubstitution( const std::vector<size_t> &indx) = 0;
158 
159  /** Normalize the (logarithmic) weights, such as the maximum weight is zero.
160  * \param out_max_log_w If provided, will return with the maximum log_w before normalizing, such as new_weights = old_weights - max_log_w.
161  * \return The max/min ratio of weights ("dynamic range")
162  */
163  virtual double normalizeWeights( double *out_max_log_w = NULL ) =0;
164 
165  /** Returns the normalized ESS (Estimated Sample Size), in the range [0,1].
166  * Note that you do NOT need to normalize the weights before calling this.
167  */
168  virtual double ESS() = 0;
169 
170  /** Performs a resample of the m_particles, using the method selected in the constructor.
171  * After computing the surviving samples, this method internally calls "performSubstitution" to actually perform the particle replacement.
172  * This method is called automatically by CParticleFilter::execute, andshould not be invoked manually normally.
173  * To just obtaining the sequence of resampled indexes from a sequence of weights, use "resample"
174  * \sa resample
175  */
176  void performResampling( const bayes::CParticleFilter::TParticleFilterOptions &PF_options );
177 
178  /** A static method to perform the computation of the samples resulting from resampling a given set of particles, given their logarithmic weights, and a resampling method.
179  * It returns the sequence of indexes from the resampling. The number of output samples is the same than the input population.
180  * This generic method just computes these indexes, to actually perform a resampling in a particle filter object, call performResampling
181  * \sa performResampling
182  */
183  static void computeResampling(
185  const vector_double &in_logWeights,
186  std::vector<size_t> &out_indexes
187  );
188 
189  /** A static method to compute the linear, normalized (the sum the unity) weights from log-weights.
190  * \sa performResampling
191  */
192  static void log2linearWeights(
193  const vector_double &in_logWeights,
194  vector_double &out_linWeights );
195 
196 
197  protected:
198  /** Performs the particle filter prediction/update stages for the algorithm "pfStandardProposal" (if not implemented in heritated class, it will raise a 'non-implemented' exception).
199  * \sa prediction_and_update
200  */
201  virtual void prediction_and_update_pfStandardProposal(
202  const mrpt::slam::CActionCollection * action,
203  const mrpt::slam::CSensoryFrame * observation,
205  /** Performs the particle filter prediction/update stages for the algorithm "pfAuxiliaryPFStandard" (if not implemented in heritated class, it will raise a 'non-implemented' exception).
206  * \sa prediction_and_update
207  */
208  virtual void prediction_and_update_pfAuxiliaryPFStandard(
209  const mrpt::slam::CActionCollection * action,
210  const mrpt::slam::CSensoryFrame * observation,
212  /** Performs the particle filter prediction/update stages for the algorithm "pfOptimalProposal" (if not implemented in heritated class, it will raise a 'non-implemented' exception).
213  * \sa prediction_and_update
214  */
215  virtual void prediction_and_update_pfOptimalProposal(
216  const mrpt::slam::CActionCollection * action,
217  const mrpt::slam::CSensoryFrame * observation,
219  /** Performs the particle filter prediction/update stages for the algorithm "pfAuxiliaryPFOptimal" (if not implemented in heritated class, it will raise a 'non-implemented' exception).
220  * \sa prediction_and_update
221  */
222  virtual void prediction_and_update_pfAuxiliaryPFOptimal(
223  const mrpt::slam::CActionCollection * action,
224  const mrpt::slam::CSensoryFrame * observation,
226 
227  /** Auxiliary vectors, see CParticleFilterCapable::prepareFastDrawSample for more information
228  */
230  {
232  CDF(),
233  CDF_indexes(),
234  PDF(),
235  alreadyDrawnIndexes(),
236  alreadyDrawnNextOne(0)
237  { }
238 
242 
245  };
246 
247  /** Auxiliary vectors, see CParticleFilterCapable::prepareFastDrawSample for more information
248  */
250 
251  }; // End of class def.
252 
253  } // end namespace
254 } // end namespace
255 #endif



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