28 #ifndef CParticleFilterData_H
29 #define CParticleFilterData_H
76 if (it->d)
delete it->d;
96 uint32_t n =
static_cast<uint32_t
>(
m_particles.size());
100 out << it->log_w << (*it->d);
149 if (ret==NULL || it->log_w > ret->log_w)
168 #define IMPLEMENT_PARTICLE_FILTER_CAPABLE(T) \
170 virtual double getW(size_t i) const \
173 if (i>=m_particles.size()) THROW_EXCEPTION_CUSTOM_MSG1("Index %i is out of range!",(int)i); \
174 return m_particles[i].log_w; \
177 virtual void setW(size_t i, double w) \
180 if (i>=m_particles.size()) THROW_EXCEPTION_CUSTOM_MSG1("Index %i is out of range!",(int)i); \
181 m_particles[i].log_w = w; \
184 virtual size_t particlesCount() const { return m_particles.size(); } \
185 virtual double normalizeWeights( double *out_max_log_w = NULL ) \
188 CParticleList::iterator it;\
190 if (!m_particles.size()) return 0; \
192 minW = maxW = m_particles[0].log_w; \
194 for (it=m_particles.begin();it!=m_particles.end();it++) \
196 maxW = std::max<double>( maxW, it->log_w ); \
197 minW = std::min<double>( minW, it->log_w ); \
200 for (it=m_particles.begin();it!=m_particles.end();it++) \
203 *out_max_log_w = maxW; \
205 return exp(maxW-minW); \
208 virtual double ESS() \
211 CParticleList::iterator it; \
215 double sumLinearWeights = 0; \
216 for (it=m_particles.begin();it!=m_particles.end();it++) \
217 sumLinearWeights += exp( it->log_w ); \
219 for (it=m_particles.begin();it!=m_particles.end();it++) \
220 cum+= utils::square( exp( it->log_w ) / sumLinearWeights ); \
224 else return 1.0/(m_particles.size()*cum); \
228 virtual void performSubstitution( const std::vector<size_t> &indx) \
231 CParticleList parts; \
232 CParticleList::iterator itDest,itSrc; \
233 size_t M_old = m_particles.size(); \
234 size_t i,j,lastIndxOld = 0; \
235 std::vector<bool> oldParticlesReused(M_old,false); \
236 std::vector<bool>::const_iterator oldPartIt; \
237 std::vector<size_t> sorted_indx(indx); \
238 std::vector<size_t>::iterator sort_idx_it; \
240 std::sort( sorted_indx.begin(), sorted_indx.end() ); \
242 parts.resize( sorted_indx.size() ); \
243 for (i=0,itDest=parts.begin();itDest!=parts.end();i++,itDest++) \
245 const size_t sorted_idx = sorted_indx[i]; \
246 itDest->log_w = m_particles[ sorted_idx ].log_w; \
248 for (j=lastIndxOld;j<sorted_idx;j++) \
250 if (!oldParticlesReused[j]) \
252 delete m_particles[j].d; \
253 m_particles[j].d = NULL; \
257 lastIndxOld = sorted_idx; \
260 if (!oldParticlesReused[sorted_idx]) \
263 parts[i].d = m_particles[ sorted_idx ].d; \
264 oldParticlesReused[sorted_idx]=true; \
269 ASSERT_( m_particles[ sorted_idx ].d != NULL); \
270 parts[i].d = new T( *m_particles[ sorted_idx ].d ); \
274 for (itSrc=m_particles.begin(),oldPartIt=oldParticlesReused.begin();itSrc!=m_particles.end();itSrc++,oldPartIt++) \
281 m_particles.resize( parts.size() ); \
282 for (itSrc=parts.begin(),itDest=m_particles.begin(); itSrc!=parts.end(); itSrc++, itDest++ ) \
284 itDest->log_w = itSrc->log_w; \
285 itDest->d = itSrc->d; \