MRPT  1.9.9
PF_implementations_data.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 #ifndef PF_implementations_data_H
10 #define PF_implementations_data_H
11 
16 #include <mrpt/poses/CPose3D.h>
19 #include <mrpt/slam/TKLDParams.h>
21 
22 namespace mrpt::slam
23 {
24 // Frwd decl:
25 template <class PARTICLETYPE, class BINTYPE>
27  BINTYPE& outBin, const TKLDParams& opts,
28  const PARTICLETYPE* currentParticleValue = nullptr,
29  const mrpt::math::TPose3D* newPoseToBeInserted = nullptr);
30 
31 /** A set of common data shared by PF implementations for both SLAM and
32  * localization
33  * \ingroup mrpt_slam_grp
34  */
35 template <
36  class PARTICLE_TYPE, class MYSELF,
39 {
40  public:
42  : mrpt::system::COutputLogger("PF_implementation"),
45  {
46  }
47 
48  protected:
49  /** \name Data members and methods used by generic PF implementations
50  @{ */
51 
56 
57  /** Used in al PF implementations. \sa
58  * PF_SLAM_implementation_gatherActionsCheckBothActObs */
60  /** Auxiliary variable used in the "pfAuxiliaryPFOptimal" algorithm. */
62  /** Auxiliary variable used in the "pfAuxiliaryPFStandard" algorithm. */
64  /** Auxiliary variable used in the "pfAuxiliaryPFOptimal" algorithm. */
66  /** Auxiliary variable used in the "pfAuxiliaryPFOptimal" algorithm. */
67  mutable std::vector<mrpt::math::TPose3D>
70 
71  /** Compute w[i]*p(z_t | mu_t^i), with mu_t^i being
72  * the mean of the new robot pose
73  *
74  * \param action MUST be a "const CPose3D*"
75  * \param observation MUST be a "const CSensoryFrame*"
76  */
77  template <class BINTYPE> // Template arg. actually not used, just to allow
78  // giving the definition in another file later on
82  const void* action, const void* observation);
83 
84  template <class BINTYPE> // Template arg. actually not used, just to allow
85  // giving the definition in another file later on
89  const void* action, const void* observation);
90 
91  /** @} */
92 
93  /** \name The generic PF implementations for localization & SLAM.
94  @{ */
95 
96  /** A generic implementation of the PF method
97  * "prediction_and_update_pfAuxiliaryPFOptimal" (optimal sampling with
98  * rejection sampling approximation),
99  * common to both localization and mapping.
100  *
101  * - BINTYPE: TPoseBin or whatever to discretize the sample space for
102  * KLD-sampling.
103  *
104  * This method implements optimal sampling with a rejection sampling-based
105  * approximation of the true posterior.
106  * For details, see the papers:
107  *
108  * J.L. Blanco, J. Gonzalez, and J.-A. Fernandez-Madrigal,
109  * "An Optimal Filtering Algorithm for Non-Parametric Observation Models
110  * in
111  * Robot Localization," in Proc. IEEE International Conference on
112  * Robotics
113  * and Automation (ICRA'08), 2008, pp. 461-466.
114  */
115  template <class BINTYPE>
117  const mrpt::obs::CActionCollection* actions,
118  const mrpt::obs::CSensoryFrame* sf,
120  const TKLDParams& KLD_options);
121 
122  /** A generic implementation of the PF method
123  * "prediction_and_update_pfAuxiliaryPFStandard" (Auxiliary particle filter
124  * with the standard proposal),
125  * common to both localization and mapping.
126  *
127  * - BINTYPE: TPoseBin or whatever to discretize the sample space for
128  * KLD-sampling.
129  *
130  * This method is described in the paper:
131  * Pitt, M.K.; Shephard, N. (1999). "Filtering Via Simulation: Auxiliary
132  * Particle Filters".
133  * Journal of the American Statistical Association 94 (446): 590-591.
134  * doi:10.2307/2670179.
135  *
136  */
137  template <class BINTYPE>
139  const mrpt::obs::CActionCollection* actions,
140  const mrpt::obs::CSensoryFrame* sf,
142  const TKLDParams& KLD_options);
143 
144  /** A generic implementation of the PF method "pfStandardProposal" (standard
145  * proposal distribution, that is, a simple SIS particle filter),
146  * common to both localization and mapping.
147  *
148  * - BINTYPE: TPoseBin or whatever to discretize the sample space for
149  * KLD-sampling.
150  */
151  template <class BINTYPE>
153  const mrpt::obs::CActionCollection* actions,
154  const mrpt::obs::CSensoryFrame* sf,
156  const TKLDParams& KLD_options);
157 
158  /** @} */
159 
160  public:
161  /** \name Virtual methods that the PF_implementations assume exist.
162  @{ */
163 
164  /** Return the last robot pose in the i'th particle; is_valid_pose will be
165  * false if there is no such last pose.
166  * \exception std::exception on out-of-range particle index */
168  const size_t i, bool& is_valid_pose) const = 0;
169 
171  PARTICLE_TYPE* particleData,
172  const mrpt::math::TPose3D& newPose) const = 0;
173 
174  /** This is the default algorithm to efficiently replace one old set of
175  * samples by another new set.
176  * The method uses pointers to make fast copies the first time each
177  * particle is duplicated, then
178  * makes real copies for the next ones.
179  *
180  * Note that more efficient specializations might exist for specific
181  * particle data structs.
182  */
185  PARTICLE_TYPE, STORAGE>::CParticleList& old_particles,
186  const std::vector<mrpt::math::TPose3D>& newParticles,
187  const std::vector<double>& newParticlesWeight,
188  const std::vector<size_t>& newParticlesDerivedFromIdx) const
189  {
190  // ---------------------------------------------------------------------------------
191  // Substitute old by new particle set:
192  // Old are in "m_particles"
193  // New are in "newParticles",
194  // "newParticlesWeight","newParticlesDerivedFromIdx"
195  // ---------------------------------------------------------------------------------
196  const size_t N = newParticles.size();
197  std::remove_reference_t<decltype(old_particles)> newParticlesArray(N);
198  if constexpr(STORAGE == mrpt::bayes::particle_storage_mode::POINTER)
199  {
200  // For efficiency, just copy the "CParticleData" from the old particle
201  // into the new one, but this can be done only once:
202  const auto N_old = old_particles.size();
203  std::vector<bool> oldParticleAlreadyCopied(N_old, false);
204  std::vector<PARTICLE_TYPE*> oldParticleFirstCopies(N_old, nullptr);
205 
206  auto newPartIt = newParticlesArray.begin();
207  for (size_t i = 0; newPartIt != newParticlesArray.end();
208  ++newPartIt, ++i)
209  {
210  // The weight:
211  newPartIt->log_w = newParticlesWeight[i];
212 
213  // The data (CParticleData):
214  PARTICLE_TYPE* newPartData;
215  const size_t i_in_old = newParticlesDerivedFromIdx[i];
216  if (!oldParticleAlreadyCopied[i_in_old])
217  {
218  // The first copy of this old particle:
219  newPartData = old_particles[i_in_old].d.release();
220  oldParticleAlreadyCopied[i_in_old] = true;
221  oldParticleFirstCopies[i_in_old] = newPartData;
222  }
223  else
224  {
225  // Make a copy:
226  ASSERT_(oldParticleFirstCopies[i_in_old]);
227  newPartData =
228  new PARTICLE_TYPE(*oldParticleFirstCopies[i_in_old]);
229  }
230 
231  newPartIt->d.reset(newPartData);
232  } // end for "newPartIt"
233 
234  // Now add the new robot pose to the paths:
235  // (this MUST be done after the above loop, separately):
236  // Update the particle with the new pose: this part is caller-dependant
237  // and must be implemented there:
238  newPartIt = newParticlesArray.begin();
239  for (size_t i = 0; i < N;
240  ++newPartIt, ++i)
242  newPartIt->d.get(), newParticles[i]);
243  }
244  else
245  {
246  // Particles as values:
247 
248  auto newPartIt = newParticlesArray.begin();
249  for (size_t i = 0; newPartIt != newParticlesArray.end();
250  ++newPartIt, ++i)
251  {
252  newPartIt->log_w = newParticlesWeight[i];
253  const size_t i_in_old = newParticlesDerivedFromIdx[i];
254  newPartIt->d = old_particles[i_in_old].d;
255  }
256 
257  // Now add the new robot pose to the paths:
258  newPartIt = newParticlesArray.begin();
259  for (size_t i = 0; i < N; ++newPartIt, ++i)
261  &newPartIt->d, newParticles[i]);
262  }
263  // Move to "m_particles"
264  old_particles = std::move(newParticlesArray);
265  } // end of PF_SLAM_implementation_replaceByNewParticleSet
266 
268  const typename mrpt::bayes::CParticleFilterData<
269  PARTICLE_TYPE,STORAGE>::CParticleList& particles,
270  const mrpt::obs::CSensoryFrame* sf) const
271  {
272  MRPT_UNUSED_PARAM(particles);
273  MRPT_UNUSED_PARAM(sf);
274  return true; // By default, always process the SFs.
275  }
276 
277  /** Make a specialization if needed, eg. in the first step in SLAM. */
279  {
280  return false; // By default, always allow the robot to move!
281  }
282 
283  /** Evaluate the observation likelihood for one particle at a given location
284  */
287  const size_t particleIndexForMap,
288  const mrpt::obs::CSensoryFrame& observation,
289  const mrpt::poses::CPose3D& x) const = 0;
290 
291  /** @} */
292 
293  /** Auxiliary method called by PF implementations: return true if we have
294  * both action & observation,
295  * otherwise, return false AND accumulate the odometry so when we have an
296  * observation we didn't lose a thing.
297  * On return=true, the "m_movementDrawer" member is loaded and ready to
298  * draw samples of the increment of pose since last step.
299  * This method is smart enough to accumulate CActionRobotMovement2D or
300  * CActionRobotMovement3D, whatever comes in.
301  */
302  template <class BINTYPE> // Template arg. actually not used, just to allow
303  // giving the definition in another file later on
305  const mrpt::obs::CActionCollection* actions,
306  const mrpt::obs::CSensoryFrame* sf);
307 
308  private:
309  /** The shared implementation body of two PF methods: APF and Optimal-APF,
310  * depending on USE_OPTIMAL_SAMPLING */
311  template <class BINTYPE>
313  const mrpt::obs::CActionCollection* actions,
314  const mrpt::obs::CSensoryFrame* sf,
316  const TKLDParams& KLD_options, const bool USE_OPTIMAL_SAMPLING);
317 
318  template <class BINTYPE>
320  const bool USE_OPTIMAL_SAMPLING, const bool doResample,
321  const double maxMeanLik,
322  size_t k, // The particle from the old set "m_particles[]"
323  const mrpt::obs::CSensoryFrame* sf,
325  mrpt::poses::CPose3D& out_newPose, double& out_newParticleLogWeight);
326 
327 }; // end PF_implementation
328 }
329 #endif
330 
331 
mrpt::math::CVectorDouble m_pfAuxiliaryPFStandard_estimatedProb
Auxiliary variable used in the "pfAuxiliaryPFStandard" algorithm.
mrpt::math::CVectorDouble m_pfAuxiliaryPFOptimal_estimatedProb
Auxiliary variable used in the "pfAuxiliaryPFOptimal" algorithm.
void KLF_loadBinFromParticle(BINTYPE &outBin, const TKLDParams &opts, const PARTICLETYPE *currentParticleValue=nullptr, const mrpt::math::TPose3D *newPoseToBeInserted=nullptr)
void PF_SLAM_implementation_pfAuxiliaryPFStandard(const mrpt::obs::CActionCollection *actions, const mrpt::obs::CSensoryFrame *sf, const mrpt::bayes::CParticleFilter::TParticleFilterOptions &PF_options, const TKLDParams &KLD_options)
A generic implementation of the PF method "prediction_and_update_pfAuxiliaryPFStandard" (Auxiliary pa...
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:44
Option set for KLD algorithm.
Definition: TKLDParams.h:18
virtual mrpt::math::TPose3D getLastPose(const size_t i, bool &is_valid_pose) const =0
Return the last robot pose in the i&#39;th particle; is_valid_pose will be false if there is no such last...
virtual bool PF_SLAM_implementation_doWeHaveValidObservations(const typename mrpt::bayes::CParticleFilterData< PARTICLE_TYPE, STORAGE >::CParticleList &particles, const mrpt::obs::CSensoryFrame *sf) const
virtual void PF_SLAM_implementation_custom_update_particle_with_new_pose(PARTICLE_TYPE *particleData, const mrpt::math::TPose3D &newPose) const =0
void PF_SLAM_implementation_pfAuxiliaryPFOptimal(const mrpt::obs::CActionCollection *actions, const mrpt::obs::CSensoryFrame *sf, const mrpt::bayes::CParticleFilter::TParticleFilterOptions &PF_options, const TKLDParams &KLD_options)
A generic implementation of the PF method "prediction_and_update_pfAuxiliaryPFOptimal" (optimal sampl...
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
Declares a class for storing a collection of robot actions.
std::vector< bool > m_pfAuxiliaryPFOptimal_maxLikMovementDrawHasBeenUsed
A set of common data shared by PF implementations for both SLAM and localization. ...
mrpt::math::CVectorDouble m_pfAuxiliaryPFOptimal_maxLikelihood
Auxiliary variable used in the "pfAuxiliaryPFOptimal" algorithm.
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
Represents a probabilistic 2D movement of the robot mobile base.
void PF_SLAM_aux_perform_one_rejection_sampling_step(const bool USE_OPTIMAL_SAMPLING, const bool doResample, const double maxMeanLik, size_t k, const mrpt::obs::CSensoryFrame *sf, const mrpt::bayes::CParticleFilter::TParticleFilterOptions &PF_options, mrpt::poses::CPose3D &out_newPose, double &out_newParticleLogWeight)
GLuint index
Definition: glext.h:4054
Versatile class for consistent logging and management of output messages.
mrpt::poses::CPoseRandomSampler m_movementDrawer
Used in al PF implementations.
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 template class declares the array of particles and its internal data, managing some memory-relat...
static double PF_SLAM_particlesEvaluator_AuxPFOptimal(const mrpt::bayes::CParticleFilter::TParticleFilterOptions &PF_options, const mrpt::bayes::CParticleFilterCapable *obj, size_t index, const void *action, const void *observation)
void PF_SLAM_implementation_pfAuxiliaryPFStandardAndOptimal(const mrpt::obs::CActionCollection *actions, const mrpt::obs::CSensoryFrame *sf, const mrpt::bayes::CParticleFilter::TParticleFilterOptions &PF_options, const TKLDParams &KLD_options, const bool USE_OPTIMAL_SAMPLING)
The shared implementation body of two PF methods: APF and Optimal-APF, depending on USE_OPTIMAL_SAMPL...
virtual double PF_SLAM_computeObservationLikelihoodForParticle(const mrpt::bayes::CParticleFilter::TParticleFilterOptions &PF_options, const size_t particleIndexForMap, const mrpt::obs::CSensoryFrame &observation, const mrpt::poses::CPose3D &x) const =0
Evaluate the observation likelihood for one particle at a given location.
mrpt::poses::CPose3DPDFGaussian m_accumRobotMovement3D
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void PF_SLAM_implementation_pfStandardProposal(const mrpt::obs::CActionCollection *actions, const mrpt::obs::CSensoryFrame *sf, const mrpt::bayes::CParticleFilter::TParticleFilterOptions &PF_options, const TKLDParams &KLD_options)
A generic implementation of the PF method "pfStandardProposal" (standard proposal distribution...
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:86
static double PF_SLAM_particlesEvaluator_AuxPFStandard(const mrpt::bayes::CParticleFilter::TParticleFilterOptions &PF_options, const mrpt::bayes::CParticleFilterCapable *obj, size_t index, const void *action, const void *observation)
Compute w[i]*p(z_t | mu_t^i), with mu_t^i being the mean of the new robot pose.
The configuration of a particle filter.
COutputLogger()
Default class constructor.
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
Declares a class that represents a Probability Density function (PDF) of a 3D pose ...
std::vector< mrpt::math::TPose3D > m_pfAuxiliaryPFOptimal_maxLikDrawnMovement
Auxiliary variable used in the "pfAuxiliaryPFOptimal" algorithm.
mrpt::obs::CActionRobotMovement2D m_accumRobotMovement2D
bool PF_SLAM_implementation_gatherActionsCheckBothActObs(const mrpt::obs::CActionCollection *actions, const mrpt::obs::CSensoryFrame *sf)
Auxiliary method called by PF implementations: return true if we have both action & observation...
GLenum GLint x
Definition: glext.h:3538
An efficient generator of random samples drawn from a given 2D (CPosePDF) or 3D (CPose3DPDF) pose pro...
particle_storage_mode
use for CProbabilityParticle
virtual bool PF_SLAM_implementation_skipRobotMovement() const
Make a specialization if needed, eg.
virtual void PF_SLAM_implementation_replaceByNewParticleSet(typename mrpt::bayes::CParticleFilterData< PARTICLE_TYPE, STORAGE >::CParticleList &old_particles, const std::vector< mrpt::math::TPose3D > &newParticles, const std::vector< double > &newParticlesWeight, const std::vector< size_t > &newParticlesDerivedFromIdx) const
This is the default algorithm to efficiently replace one old set of samples by another new set...
#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