MRPT  2.0.0
CParticleFilterCapable.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
12 #include <cstdint>
13 #include <cstdlib>
14 #include <vector>
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  */
42  virtual ~CParticleFilterCapable() = default;
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  PF_options,
71  [[maybe_unused]] const CParticleFilterCapable* obj, size_t index,
72  [[maybe_unused]] const void* action,
73  [[maybe_unused]] const void* observation)
74  {
75  return obj->getW(index);
76  }
77 
78  /** Prepares data structures for calling fastDrawSample method next.
79  * This method must be called once before using "fastDrawSample" (calling
80  *this more than once has no effect, but it takes time for nothing!)
81  * The behavior depends on the configuration of the PF (see
82  *CParticleFilter::TParticleFilterOptions):
83  * - <b>DYNAMIC SAMPLE SIZE=NO</b>: In this case this method fills out
84  *an
85  *internal array (m_fastDrawAuxiliary.alreadyDrawnIndexes) with
86  * the random indexes generated according to the selected resample
87  *scheme
88  *in TParticleFilterOptions. Those indexes are
89  * read sequentially by subsequent calls to fastDrawSample.
90  * - <b>DYNAMIC SAMPLE SIZE=YES</b>: Then:
91  * - If TParticleFilterOptions.resamplingMethod = prMultinomial,
92  *the
93  *internal buffers will be filled out (m_fastDrawAuxiliary.CDF, CDF_indexes
94  *& PDF) and
95  * then fastDrawSample can be called an arbitrary number of
96  *times
97  *to
98  *generate random indexes.
99  * - For the rest of resampling algorithms, an exception will be
100  *raised
101  *since they are not appropriate for a dynamic (unknown in advance) number
102  *of particles.
103  *
104  * The function pointed by "partEvaluator" should take into account the
105  *particle filter algorithm selected in "m_PFAlgorithm".
106  * If called without arguments (defaultEvaluator), the default behavior is
107  *to draw samples with a probability proportional to their current weights.
108  * The action and the observation are declared as "void*" for a greater
109  *flexibility.
110  * For a more detailed information see the <a
111  *href="http://www.mrpt.org/Particle_Filters" >Particle Filter
112  *tutorial</a>.
113  * Custom supplied "partEvaluator" functions must take into account the
114  *previous particle weight, i.e. multiplying the current observation
115  *likelihood by the weights.
116  * \sa fastDrawSample
117  */
121  const void* action = nullptr, const void* observation = nullptr) const;
122 
123  /** Draws a random sample from the particle filter, in such a way that each
124  *particle has a probability proportional to its weight (in the standard PF
125  *algorithm).
126  * This method can be used to generate a variable number of m_particles
127  *when resampling: to vary the number of m_particles in the filter.
128  * See prepareFastDrawSample for more information, or the <a
129  *href="http://www.mrpt.org/Particle_Filters" >Particle Filter
130  *tutorial</a>.
131  *
132  * NOTES:
133  * - You MUST call "prepareFastDrawSample" ONCE before calling this
134  *method. That method must be called after modifying the particle filter
135  *(executing one step, resampling, etc...)
136  * - This method returns ONE index for the selected ("drawn") particle,
137  *in
138  *the range [0,M-1]
139  * - You do not need to call "normalizeWeights" before calling this.
140  * \sa prepareFastDrawSample
141  */
142  size_t fastDrawSample(
143  const bayes::CParticleFilter::TParticleFilterOptions& PF_options) const;
144 
145  /** Access to i'th particle (logarithm) weight, where first one is index 0.
146  */
147  virtual double getW(size_t i) const = 0;
148 
149  /** Modifies i'th particle (logarithm) weight, where first one is index 0.
150  */
151  virtual void setW(size_t i, double w) = 0;
152 
153  /** Get the m_particles count.
154  */
155  virtual size_t particlesCount() const = 0;
156 
157  /** Performs the prediction stage of the Particle Filter.
158  * This method simply selects the appropiate protected method according to
159  * the particle filter algorithm to run.
160  * \sa
161  * prediction_and_update_pfStandardProposal,prediction_and_update_pfAuxiliaryPFStandard,prediction_and_update_pfOptimalProposal,prediction_and_update_pfAuxiliaryPFOptimal
162  */
164  const mrpt::obs::CActionCollection* action,
165  const mrpt::obs::CSensoryFrame* observation,
167 
168  /** Performs the substitution for internal use of resample in particle
169  * filter algorithm, don't call it directly.
170  * \param indx The indices of current m_particles to be saved as the new
171  * m_particles set.
172  */
173  virtual void performSubstitution(const std::vector<size_t>& indx) = 0;
174 
175  /** Normalize the (logarithmic) weights, such as the maximum weight is zero.
176  * \param out_max_log_w If provided, will return with the maximum log_w
177  * before normalizing, such as new_weights = old_weights - max_log_w.
178  * \return The max/min ratio of weights ("dynamic range")
179  */
180  virtual double normalizeWeights(double* out_max_log_w = nullptr) = 0;
181 
182  /** Returns the normalized ESS (Estimated Sample Size), in the range [0,1].
183  * Note that you do NOT need to normalize the weights before calling this.
184  */
185  virtual double ESS() const = 0;
186 
187  /** Performs a resample of the m_particles, using the method selected in the
188  * constructor.
189  * After computing the surviving samples, this method internally calls
190  * "performSubstitution" to actually perform the particle replacement.
191  * This method is called automatically by CParticleFilter::execute,
192  * andshould not be invoked manually normally.
193  * To just obtaining the sequence of resampled indexes from a sequence of
194  * weights, use "resample"
195  * \param[in] out_particle_count The desired number of output particles
196  * after resampling; 0 means don't modify the current number.
197  * \sa resample
198  */
199  void performResampling(
201  size_t out_particle_count = 0);
202 
203  /** A static method to perform the computation of the samples resulting from
204  * resampling a given set of particles, given their logarithmic weights, and
205  * a resampling method.
206  * It returns the sequence of indexes from the resampling. The number of
207  * output samples is the same than the input population.
208  * This generic method just computes these indexes, to actually perform a
209  * resampling in a particle filter object, call performResampling
210  * \param[in] out_particle_count The desired number of output particles
211  * after resampling; 0 means don't modify the current number.
212  * \sa performResampling
213  */
214  static void computeResampling(
216  const std::vector<double>& in_logWeights,
217  std::vector<size_t>& out_indexes, size_t out_particle_count = 0);
218 
219  /** A static method to compute the linear, normalized (the sum the unity)
220  * weights from log-weights.
221  * \sa performResampling
222  */
223  static void log2linearWeights(
224  const std::vector<double>& in_logWeights,
225  std::vector<double>& out_linWeights);
226 
227  protected:
228  /** Performs the particle filter prediction/update stages for the algorithm
229  * "pfStandardProposal" (if not implemented in heritated class, it will
230  * raise a 'non-implemented' exception).
231  * \sa prediction_and_update
232  */
234  const mrpt::obs::CActionCollection* action,
235  const mrpt::obs::CSensoryFrame* observation,
237  /** Performs the particle filter prediction/update stages for the algorithm
238  * "pfAuxiliaryPFStandard" (if not implemented in heritated class, it will
239  * raise a 'non-implemented' exception).
240  * \sa prediction_and_update
241  */
243  const mrpt::obs::CActionCollection* action,
244  const mrpt::obs::CSensoryFrame* observation,
246  /** Performs the particle filter prediction/update stages for the algorithm
247  * "pfOptimalProposal" (if not implemented in heritated class, it will raise
248  * a 'non-implemented' exception).
249  * \sa prediction_and_update
250  */
252  const mrpt::obs::CActionCollection* action,
253  const mrpt::obs::CSensoryFrame* observation,
255  /** Performs the particle filter prediction/update stages for the algorithm
256  * "pfAuxiliaryPFOptimal" (if not implemented in heritated class, it will
257  * raise a 'non-implemented' exception).
258  * \sa prediction_and_update
259  */
261  const mrpt::obs::CActionCollection* action,
262  const mrpt::obs::CSensoryFrame* observation,
264 
265  /** Auxiliary vectors, see CParticleFilterCapable::prepareFastDrawSample for
266  * more information
267  */
269  {
270  TFastDrawAuxVars() = default;
271 
272  std::vector<double> CDF;
273  std::vector<uint32_t> CDF_indexes;
274  std::vector<double> PDF;
275  std::vector<uint32_t> alreadyDrawnIndexes;
277  };
278 
279  /** Auxiliary vectors, see CParticleFilterCapable::prepareFastDrawSample for
280  * more information
281  */
283 
284 }; // End of class def.
285 
286 } // namespace mrpt::bayes
static double defaultEvaluator([[maybe_unused]] const bayes::CParticleFilter::TParticleFilterOptions &PF_options, [[maybe_unused]] const CParticleFilterCapable *obj, size_t index, [[maybe_unused]] const void *action, [[maybe_unused]] 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.
Declares a class for storing a collection of robot actions.
Auxiliary vectors, see CParticleFilterCapable::prepareFastDrawSample for more information.
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.
Declares a class for storing a "sensory frame", a set of "observations" taken by the robot approximat...
Definition: CSensoryFrame.h:51
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.
virtual ~CParticleFilterCapable()=default
Virtual destructor.
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...



Page generated by Doxygen 1.8.14 for MRPT 2.0.0 Git: b38439d21 Tue Mar 31 19:58:06 2020 +0200 at miƩ abr 1 00:50:30 CEST 2020