21 class CParticleFilterCapable;
30 template <
class Derived,
class particle_list_t>
36 return *
dynamic_cast<const Derived*
>(
this);
39 inline Derived&
derived() {
return *
dynamic_cast<Derived*
>(
this); }
40 double getW(
size_t i)
const override 42 if (i >=
derived().m_particles.size())
44 return derived().m_particles[i].log_w;
47 void setW(
size_t i,
double w)
override 49 if (i >=
derived().m_particles.size())
56 return derived().m_particles.size();
62 if (
derived().m_particles.empty())
return 0;
63 double minW =
derived().m_particles[0].log_w;
69 it !=
derived().m_particles.end(); ++it)
71 maxW = std::max<double>(maxW, it->log_w);
72 minW = std::min<double>(minW, it->log_w);
77 it !=
derived().m_particles.end(); ++it)
79 if (out_max_log_w) *out_max_log_w = maxW;
82 return std::exp(maxW - minW);
86 double ESS()
const override 92 double sumLinearWeights = 0;
95 it !=
derived().m_particles.end(); ++it)
96 sumLinearWeights += std::exp(it->log_w);
100 it !=
derived().m_particles.end(); ++it)
101 cum +=
mrpt::square(std::exp(it->log_w) / sumLinearWeights);
106 return 1.0 / (
derived().m_particles.size() * cum);
117 std::vector<size_t> sorted_indx(indx);
118 std::sort(sorted_indx.begin(), sorted_indx.end());
121 particle_list_t parts;
122 parts.resize(sorted_indx.size());
127 const size_t M_old =
derived().m_particles.size();
128 std::vector<bool> oldParticlesReused(M_old,
false);
131 size_t i, lastIndxOld = 0;
133 for (i = 0, itDest = parts.begin(); itDest != parts.end();
136 const size_t sorted_idx = sorted_indx[i];
137 itDest->log_w =
derived().m_particles[sorted_idx].log_w;
140 for (
size_t j = lastIndxOld; j < sorted_idx; j++)
142 if (!oldParticlesReused
144 derived().m_particles[j].d.reset();
148 lastIndxOld = sorted_idx;
152 if (!oldParticlesReused[sorted_idx])
155 parts[i].d.reset(
derived().m_particles[sorted_idx].d.get());
156 oldParticlesReused[sorted_idx] =
true;
163 new typename Derived::CParticleDataContent(
164 *
derived().m_particles[sorted_idx].d));
168 for (itSrc =
derived().m_particles.begin(),
169 oldPartIt = oldParticlesReused.begin();
170 itSrc !=
derived().m_particles.end(); itSrc++, oldPartIt++)
171 if (!*oldPartIt) itSrc->d.reset();
176 auto it_idx = sorted_indx.begin();
177 auto itDest = parts.begin();
178 for (; itDest != parts.end(); ++it_idx, ++itDest)
179 *itDest =
derived().m_particles[*it_idx];
182 derived().m_particles = std::move(parts);
227 template <
class STREAM>
248 template <
class STREAM>
262 it->d.reset(
new T());
282 for (it = out_logWeights.begin(), it2 =
m_particles.begin();
297 if (ret ==
nullptr ||
p.log_w > ret->log_w) ret = &
p;
void clearParticles()
Free the memory of all the particles and reset the array "m_particles" to length zero.
void writeParticlesToStream(STREAM &out) const
Dumps the sequence of particles and their weights to a stream (requires T implementing CSerializable)...
void getWeights(std::vector< double > &out_logWeights) const
Returns a vector with the sequence of the logaritmic weights of all the samples.
Derived & derived()
CRTP helper method.
CParticleList m_particles
The array of particles.
double normalizeWeights(double *out_max_log_w=nullptr) override
Normalize the (logarithmic) weights, such as the maximum weight is zero.
static const particle_storage_mode PARTICLE_STORAGE
The namespace for Bayesian filtering algorithm: different particle filters and Kalman filter algorith...
double ESS() const override
Returns the normalized ESS (Estimated Sample Size), in the range [0,1].
GLubyte GLubyte GLubyte GLubyte w
size_t particlesCount() const override
Get the m_particles count.
T square(const T x)
Inline function for the square of a number.
CParticleFilterData()
Default constructor.
#define ASSERT_(f)
Defines an assertion mechanism.
void readParticlesFromStream(STREAM &in)
Reads the sequence of particles and their weights from a stream (requires T implementing CSerializabl...
This virtual class defines the interface that any particles based PDF class must implement in order t...
std::deque< CParticleData > CParticleList
Use this type to refer to the list of particles m_particles.
const CParticleData * getMostLikelyParticle() const
Returns the particle with the highest weight.
This template class declares the array of particles and its internal data, managing some memory-relat...
void performSubstitution(const std::vector< size_t > &indx) override
Replaces the old particles by copies determined by the indexes in "indx", performing an efficient cop...
double getW(size_t i) const override
Access to i'th particle (logarithm) weight, where first one is index 0.
A template class for holding a the data and the weight of a particle.
const Derived & derived() const
CRTP helper method.
particle_storage_mode
use for CProbabilityParticle
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
unsigned __int32 uint32_t
const Scalar * const_iterator
A curiously recurring template pattern (CRTP) approach to providing the basic functionality of any CP...
CRBPFParticleData CParticleDataContent
This is the type inside the corresponding CParticleData class.
void setW(size_t i, double w) override
Modifies i'th particle (logarithm) weight, where first one is index 0.