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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019