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-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 #pragma once
10 
12 #include <vector>
13 #include <cstdlib>
14 #include <cstdint>
15 
16 namespace mrpt::bayes
17 {
18 #define INVALID_LIKELIHOOD_VALUE \
19  (-1e300) // An invalid log-likelihood value, used to signal non-initialized
20 // likelihood variables.
21 
22 /** This virtual class defines the interface that any particles based PDF class
23  * must implement in order to be executed by a mrpt::bayes::CParticleFilter.
24  *
25  * See the <a href="http://www.mrpt.org/Particle_Filter_Tutorial" >Particle
26  * Filter tutorial</a> explaining how to use the particle filter-related
27  * classes.
28  * \sa CParticleFilter, CParticleFilterData
29  * \ingroup mrpt_bayes_grp
30  */
32 {
33  friend class CParticleFilter;
34 
35  private:
37 
38  public:
40  /** Virtual destructor
41  */
43  /** A callback function type for evaluating the probability of m_particles
44  * of being selected, used in "fastDrawSample".
45  * The default evaluator function "defaultEvaluator" simply returns the
46  * particle weight.
47  * \param index This is the index of the particle its probability is being
48  * computed.
49  * \param action The value of this is the parameter passed to
50  * "prepareFastDrawSample"
51  * \param observation The value of this is the parameter passed to
52  * "prepareFastDrawSample"
53  * The action and the observation are declared as "void*" for a greater
54  * flexibility.
55  * \sa prepareFastDrawSample
56  */
57  using TParticleProbabilityEvaluator = double (*)(
59  const CParticleFilterCapable* obj, size_t index, const void* action,
60  const void* observation);
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  */
68  static double defaultEvaluator(
70  const CParticleFilterCapable* obj, size_t index, const void* action,
71  const void* observation)
72  {
73  MRPT_UNUSED_PARAM(PF_options);
74  MRPT_UNUSED_PARAM(action);
75  MRPT_UNUSED_PARAM(observation);
76  return obj->getW(index);
77  }
78 
79  /** Prepares data structures for calling fastDrawSample method next.
80  * This method must be called once before using "fastDrawSample" (calling
81  *this more than once has no effect, but it takes time for nothing!)
82  * The behavior depends on the configuration of the PF (see
83  *CParticleFilter::TParticleFilterOptions):
84  * - <b>DYNAMIC SAMPLE SIZE=NO</b>: In this case this method fills out
85  *an
86  *internal array (m_fastDrawAuxiliary.alreadyDrawnIndexes) with
87  * the random indexes generated according to the selected resample
88  *scheme
89  *in TParticleFilterOptions. Those indexes are
90  * read sequentially by subsequent calls to fastDrawSample.
91  * - <b>DYNAMIC SAMPLE SIZE=YES</b>: Then:
92  * - If TParticleFilterOptions.resamplingMethod = prMultinomial,
93  *the
94  *internal buffers will be filled out (m_fastDrawAuxiliary.CDF, CDF_indexes
95  *& PDF) and
96  * then fastDrawSample can be called an arbitrary number of
97  *times
98  *to
99  *generate random indexes.
100  * - For the rest of resampling algorithms, an exception will be
101  *raised
102  *since they are not appropriate for a dynamic (unknown in advance) number
103  *of particles.
104  *
105  * The function pointed by "partEvaluator" should take into account the
106  *particle filter algorithm selected in "m_PFAlgorithm".
107  * If called without arguments (defaultEvaluator), the default behavior is
108  *to draw samples with a probability proportional to their current weights.
109  * The action and the observation are declared as "void*" for a greater
110  *flexibility.
111  * For a more detailed information see the <a
112  *href="http://www.mrpt.org/Particle_Filters" >Particle Filter
113  *tutorial</a>.
114  * Custom supplied "partEvaluator" functions must take into account the
115  *previous particle weight, i.e. multiplying the current observation
116  *likelihood by the weights.
117  * \sa fastDrawSample
118  */
122  const void* action = nullptr, const void* observation = nullptr) const;
123 
124  /** Draws a random sample from the particle filter, in such a way that each
125  *particle has a probability proportional to its weight (in the standard PF
126  *algorithm).
127  * This method can be used to generate a variable number of m_particles
128  *when resampling: to vary the number of m_particles in the filter.
129  * See prepareFastDrawSample for more information, or the <a
130  *href="http://www.mrpt.org/Particle_Filters" >Particle Filter
131  *tutorial</a>.
132  *
133  * NOTES:
134  * - You MUST call "prepareFastDrawSample" ONCE before calling this
135  *method. That method must be called after modifying the particle filter
136  *(executing one step, resampling, etc...)
137  * - This method returns ONE index for the selected ("drawn") particle,
138  *in
139  *the range [0,M-1]
140  * - You do not need to call "normalizeWeights" before calling this.
141  * \sa prepareFastDrawSample
142  */
143  size_t fastDrawSample(
144  const bayes::CParticleFilter::TParticleFilterOptions& PF_options) const;
145 
146  /** Access to i'th particle (logarithm) weight, where first one is index 0.
147  */
148  virtual double getW(size_t i) const = 0;
149 
150  /** Modifies i'th particle (logarithm) weight, where first one is index 0.
151  */
152  virtual void setW(size_t i, double w) = 0;
153 
154  /** Get the m_particles count.
155  */
156  virtual size_t particlesCount() const = 0;
157 
158  /** Performs the prediction stage of the Particle Filter.
159  * This method simply selects the appropiate protected method according to
160  * the particle filter algorithm to run.
161  * \sa
162  * prediction_and_update_pfStandardProposal,prediction_and_update_pfAuxiliaryPFStandard,prediction_and_update_pfOptimalProposal,prediction_and_update_pfAuxiliaryPFOptimal
163  */
165  const mrpt::obs::CActionCollection* action,
166  const mrpt::obs::CSensoryFrame* observation,
168 
169  /** Performs the substitution for internal use of resample in particle
170  * filter algorithm, don't call it directly.
171  * \param indx The indices of current m_particles to be saved as the new
172  * m_particles set.
173  */
174  virtual void performSubstitution(const std::vector<size_t>& indx) = 0;
175 
176  /** Normalize the (logarithmic) weights, such as the maximum weight is zero.
177  * \param out_max_log_w If provided, will return with the maximum log_w
178  * before normalizing, such as new_weights = old_weights - max_log_w.
179  * \return The max/min ratio of weights ("dynamic range")
180  */
181  virtual double normalizeWeights(double* out_max_log_w = nullptr) = 0;
182 
183  /** Returns the normalized ESS (Estimated Sample Size), in the range [0,1].
184  * Note that you do NOT need to normalize the weights before calling this.
185  */
186  virtual double ESS() const = 0;
187 
188  /** Performs a resample of the m_particles, using the method selected in the
189  * constructor.
190  * After computing the surviving samples, this method internally calls
191  * "performSubstitution" to actually perform the particle replacement.
192  * This method is called automatically by CParticleFilter::execute,
193  * andshould not be invoked manually normally.
194  * To just obtaining the sequence of resampled indexes from a sequence of
195  * weights, use "resample"
196  * \param[in] out_particle_count The desired number of output particles
197  * after resampling; 0 means don't modify the current number.
198  * \sa resample
199  */
200  void performResampling(
202  size_t out_particle_count = 0);
203 
204  /** A static method to perform the computation of the samples resulting from
205  * resampling a given set of particles, given their logarithmic weights, and
206  * a resampling method.
207  * It returns the sequence of indexes from the resampling. The number of
208  * output samples is the same than the input population.
209  * This generic method just computes these indexes, to actually perform a
210  * resampling in a particle filter object, call performResampling
211  * \param[in] out_particle_count The desired number of output particles
212  * after resampling; 0 means don't modify the current number.
213  * \sa performResampling
214  */
215  static void computeResampling(
217  const std::vector<double>& in_logWeights,
218  std::vector<size_t>& out_indexes, size_t out_particle_count = 0);
219 
220  /** A static method to compute the linear, normalized (the sum the unity)
221  * weights from log-weights.
222  * \sa performResampling
223  */
224  static void log2linearWeights(
225  const std::vector<double>& in_logWeights,
226  std::vector<double>& out_linWeights);
227 
228  protected:
229  /** Performs the particle filter prediction/update stages for the algorithm
230  * "pfStandardProposal" (if not implemented in heritated class, it will
231  * raise a 'non-implemented' exception).
232  * \sa prediction_and_update
233  */
235  const mrpt::obs::CActionCollection* action,
236  const mrpt::obs::CSensoryFrame* observation,
238  /** Performs the particle filter prediction/update stages for the algorithm
239  * "pfAuxiliaryPFStandard" (if not implemented in heritated class, it will
240  * raise a 'non-implemented' exception).
241  * \sa prediction_and_update
242  */
244  const mrpt::obs::CActionCollection* action,
245  const mrpt::obs::CSensoryFrame* observation,
247  /** Performs the particle filter prediction/update stages for the algorithm
248  * "pfOptimalProposal" (if not implemented in heritated class, it will raise
249  * a 'non-implemented' exception).
250  * \sa prediction_and_update
251  */
253  const mrpt::obs::CActionCollection* action,
254  const mrpt::obs::CSensoryFrame* observation,
256  /** Performs the particle filter prediction/update stages for the algorithm
257  * "pfAuxiliaryPFOptimal" (if not implemented in heritated class, it will
258  * raise a 'non-implemented' exception).
259  * \sa prediction_and_update
260  */
262  const mrpt::obs::CActionCollection* action,
263  const mrpt::obs::CSensoryFrame* observation,
265 
266  /** Auxiliary vectors, see CParticleFilterCapable::prepareFastDrawSample for
267  * more information
268  */
270  {
272  : CDF(),
273  CDF_indexes(),
274  PDF(),
277  {
278  }
279 
280  std::vector<double> CDF;
281  std::vector<uint32_t> CDF_indexes;
282  std::vector<double> PDF;
283 
284  std::vector<uint32_t> alreadyDrawnIndexes;
286  };
287 
288  /** Auxiliary vectors, see CParticleFilterCapable::prepareFastDrawSample for
289  * more information
290  */
292 
293 }; // End of class def.
294 
295 }
296 
static double defaultEvaluator(const bayes::CParticleFilter::TParticleFilterOptions &PF_options, const CParticleFilterCapable *obj, size_t index, const void *action, const void *observation)
The default evaluator function, which simply returns the particle weight.
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...
The namespace for Bayesian filtering algorithm: different particle filters and Kalman filter algorith...
virtual void prediction_and_update_pfAuxiliaryPFStandard(const mrpt::obs::CActionCollection *action, const mrpt::obs::CSensoryFrame *observation, const bayes::CParticleFilter::TParticleFilterOptions &PF_options)
Performs the particle filter prediction/update stages for the algorithm "pfAuxiliaryPFStandard" (if n...
void prepareFastDrawSample(const bayes::CParticleFilter::TParticleFilterOptions &PF_options, TParticleProbabilityEvaluator partEvaluator=defaultEvaluator, const void *action=nullptr, const void *observation=nullptr) const
Prepares data structures for calling fastDrawSample method next.
double(*)(const bayes::CParticleFilter::TParticleFilterOptions &PF_options, const CParticleFilterCapable *obj, size_t index, const void *action, const void *observation) TParticleProbabilityEvaluator
A callback function type for evaluating the probability of m_particles of being selected, used in "fastDrawSample".
virtual void setW(size_t i, double w)=0
Modifies i&#39;th particle (logarithm) weight, where first one is index 0.
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
Declares a class for storing a collection of robot actions.
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.
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.
GLuint index
Definition: glext.h:4054
Declares a class for storing a "sensory frame", a set of "observations" taken by the robot approximat...
Definition: CSensoryFrame.h:52
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.
virtual void prediction_and_update_pfStandardProposal(const mrpt::obs::CActionCollection *action, const mrpt::obs::CSensoryFrame *observation, const bayes::CParticleFilter::TParticleFilterOptions &PF_options)
Performs the particle filter prediction/update stages for the algorithm "pfStandardProposal" (if not ...
virtual void prediction_and_update_pfAuxiliaryPFOptimal(const mrpt::obs::CActionCollection *action, const mrpt::obs::CSensoryFrame *observation, const bayes::CParticleFilter::TParticleFilterOptions &PF_options)
Performs the particle filter prediction/update stages for the algorithm "pfAuxiliaryPFOptimal" (if no...
void prediction_and_update(const mrpt::obs::CActionCollection *action, const mrpt::obs::CSensoryFrame *observation, const bayes::CParticleFilter::TParticleFilterOptions &PF_options)
Performs the prediction stage of the Particle Filter.
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...
virtual void prediction_and_update_pfOptimalProposal(const mrpt::obs::CActionCollection *action, const mrpt::obs::CSensoryFrame *observation, const bayes::CParticleFilter::TParticleFilterOptions &PF_options)
Performs the particle filter prediction/update stages for the algorithm "pfOptimalProposal" (if not i...
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...
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020