|
| 1 | +#ifndef IMPORTANCERESAMPLING_H |
| 2 | +#define IMPORTANCERESAMPLING_H |
| 3 | + |
| 4 | +#include "libPF/CRandomNumberGenerator.h" |
| 5 | + |
| 6 | +namespace libPF |
| 7 | +{ |
| 8 | + |
| 9 | +/** |
| 10 | + * @class ImportanceResampling |
| 11 | + * |
| 12 | + * @brief A resampling strategy that performs importance resampling |
| 13 | + * |
| 14 | + * The resampling strategy defines how the resampling is performed in the resample step |
| 15 | + * of a particle filter. |
| 16 | + * |
| 17 | + * @author Stephan Wirth |
| 18 | + * |
| 19 | + * @see ResamplingStrategy |
| 20 | + */ |
| 21 | + |
| 22 | +template <class StateType> |
| 23 | +class ImportanceResampling : public ResamplingStrategy<StateType>{ |
| 24 | + |
| 25 | + /** |
| 26 | + * A ParticleList is an array of pointers to Particles. |
| 27 | + */ |
| 28 | + typedef std::vector< Particle<StateType>* > ParticleList; |
| 29 | + |
| 30 | + public: |
| 31 | + /** |
| 32 | + * The constructor of this base class inits some members. |
| 33 | + */ |
| 34 | + ImportanceResampling<StateType>(); |
| 35 | + |
| 36 | + /** |
| 37 | + * The destructor is empty. |
| 38 | + */ |
| 39 | + virtual ~ImportanceResampling(); |
| 40 | + |
| 41 | + /** |
| 42 | + * This is the main method of ImportanceResampling. It takes two references to |
| 43 | + * particle lists. The first reference refers to the old particle list, the |
| 44 | + * second to the new one. |
| 45 | + * @param source the source list to draw new particles from. |
| 46 | + * @param destination the destination list where to put the copies. |
| 47 | + */ |
| 48 | + void resample(const ParticleList& source, const ParticleList& destination) const; |
| 49 | + |
| 50 | + /** |
| 51 | + * Sets the Random Number Generator to use in resample() to generate uniformly distributed random numbers. |
| 52 | + */ |
| 53 | + void setRNG(RandomNumberGenerationStrategy* rng); |
| 54 | + |
| 55 | + private: |
| 56 | + |
| 57 | + // Stores a pointer to the random number generator. |
| 58 | + const RandomNumberGenerationStrategy* m_RNG; |
| 59 | + |
| 60 | + // The default random number generator |
| 61 | + CRandomNumberGenerator m_DefaultRNG; |
| 62 | + |
| 63 | +}; |
| 64 | + |
| 65 | + |
| 66 | +template <class StateType> |
| 67 | +ImportanceResampling<StateType>::ImportanceResampling() : |
| 68 | + m_RNG(&m_DefaultRNG) { |
| 69 | +} |
| 70 | + |
| 71 | +template <class StateType> |
| 72 | +ImportanceResampling<StateType>::~ImportanceResampling() { |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +// resampling based on the cumulative distribution function (CDF) |
| 77 | +template <class StateType> |
| 78 | +void ImportanceResampling<StateType>::resample(const ParticleList& sourceList, const ParticleList& destinationList) const { |
| 79 | + |
| 80 | + double inverseNum = 1.0f / sourceList.size(); |
| 81 | + double start = m_RNG->getUniform() * inverseNum; // random start in CDF |
| 82 | + double cumulativeWeight = 0.0f; |
| 83 | + unsigned int sourceIndex = 0; // index to draw from |
| 84 | + cumulativeWeight += sourceList[sourceIndex]->getWeight(); |
| 85 | + for (unsigned int destIndex = 0; destIndex < destinationList.size(); destIndex++) { |
| 86 | + double probSum = start + inverseNum * destIndex; // amount of cumulative weight to reach |
| 87 | + while (probSum > cumulativeWeight) { // sum weights until |
| 88 | + sourceIndex++; |
| 89 | + if (sourceIndex >= sourceList.size()) { |
| 90 | + sourceIndex = sourceList.size() - 1; |
| 91 | + break; |
| 92 | + } |
| 93 | + cumulativeWeight += sourceList[sourceIndex]->getWeight(); // target sum reached |
| 94 | + } |
| 95 | + *(destinationList[destIndex]) = *(sourceList[sourceIndex]); // copy particle (via assignment operator) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | +template <class StateType> |
| 101 | +void ImportanceResampling<StateType>::setRNG(RandomNumberGenerationStrategy* rng) |
| 102 | +{ |
| 103 | + m_RNG = rng; |
| 104 | +} |
| 105 | + |
| 106 | +} // end of namespace |
| 107 | +#endif // IMPORTANCERESAMPLING_H |
| 108 | + |
0 commit comments