1 #ifndef PARTICLEFILTER_H
2 #define PARTICLEFILTER_H
24 template <
class OutputIterator,
class Iterator>
25 double toNormalForm(OutputIterator& out,
const Iterator & begin,
const Iterator & end){
29 for (Iterator it=begin; it!=end; it++){
30 lmax=lmax>((double)(*it))? lmax: (
double)(*it);
33 for (Iterator it=begin; it!=end; it++){
34 *out=exp((
double)(*it)-lmax);
40 template <
class OutputIterator,
class Iterator,
class Numeric>
41 void toLogForm(OutputIterator& out,
const Iterator & begin,
const Iterator & end, Numeric lmax){
43 for (Iterator it=begin; it!=end; it++){
44 *out=log((Numeric)(*it))-lmax;
50 template <
class WeightVector>
51 void resample(std::vector<int>& indexes,
const WeightVector& weights,
unsigned int nparticles=0){
56 for (
typename WeightVector::const_iterator it=weights.begin(); it!=weights.end(); ++it){
65 double interval=cweight/
n;
68 double target=interval*::drand48();
76 for (
typename WeightVector::const_iterator it=weights.begin(); it!=weights.end(); ++it, ++i){
77 cweight+=(double)* it;
78 while(cweight>target){
85 template <
typename Vector>
86 void repeatIndexes(Vector& dest,
const std::vector<int>& indexes,
const Vector& particles){
87 assert(indexes.size()==particles.size());
88 dest.resize(particles.size());
90 for (std::vector<int>::const_iterator it=indexes.begin(); it!=indexes.end(); ++it){
91 dest[i]=particles[*it];
97 template <
class Iterator>
98 double neff(
const Iterator& begin,
const Iterator& end){
100 for (Iterator it=begin; it!=end; ++it){
104 for (Iterator it=begin; it!=end; ++it){
111 template <
class Iterator>
112 void normalize(
const Iterator& begin,
const Iterator& end){
114 for (Iterator it=begin; it!=end; ++it){
117 for (Iterator it=begin; it!=end; ++it){
122 template <
class OutputIterator,
class Iterator>
123 void rle(OutputIterator& out,
const Iterator & begin,
const Iterator & end){
124 unsigned int current=0;
125 unsigned int count=0;
126 for (Iterator it=begin; it!=end; it++){
132 if (((uint)*it) ==current)
134 if (((uint)*it)!=current){
135 *out=std::make_pair(current,count);
142 *out=std::make_pair(current,count);
147 template <
class Particle,
class Numeric>
149 std::vector<unsigned int>
resampleIndexes(
const std::vector<Particle> & particles,
int nparticles=0)
const;
150 std::vector<Particle>
resample(
const std::vector<Particle> & particles,
int nparticles=0)
const;
151 Numeric
neff(
const std::vector<Particle> & particles)
const;
155 template <
class Particle,
class Numeric>
161 for (
typename std::vector<Particle>::const_iterator it=particles.begin(); it!=particles.end(); ++it){
162 cweight+=(Numeric)*it;
170 Numeric interval=cweight/
n;
173 Numeric target=interval*::drand48();
177 std::vector<unsigned int> indexes(
n);
180 for (
typename std::vector<Particle>::const_iterator it=particles.begin(); it!=particles.end(); ++it, ++i){
181 cweight+=(Numeric)* it;
182 while(cweight>target){
190 template <
class Particle,
class Numeric>
192 (
const typename std::vector<Particle>& particles,
int nparticles)
const{
197 for (
typename std::vector<Particle>::const_iterator it=particles.begin(); it!=particles.end(); ++it){
198 cweight+=(Numeric)*it;
209 Numeric interval=cweight/
n;
212 Numeric target=interval*::drand48();
216 std::vector<Particle> resampled;
219 for (
typename std::vector<Particle>::const_iterator it=particles.begin(); it!=particles.end(); ++it, ++i){
220 cweight+=(Numeric)*it;
221 while(cweight>target){
222 resampled.push_back(*it);
223 resampled.back().setWeight(uw);
230 template <
class Particle,
class Numeric>
234 for (
typename std::vector<Particle>::const_iterator it=particles.begin(); it!=particles.end(); ++it){
235 Numeric w=(Numeric)*it;
262 template <
class Particle,
class EvolutionModel>
265 void evolve(std::vector<Particle>& particles);
266 void evolve(std::vector<Particle>& dest,
const std::vector<Particle>& src);
269 template <
class Particle,
class EvolutionModel>
271 for (
typename std::vector<Particle>::iterator it=particles.begin(); it!=particles.end(); ++it){
272 *it=evolutionModel.evolve(*it);
276 template <
class Particle,
class EvolutionModel>
279 for (
typename std::vector<Particle>::const_iterator it=src.begin(); it!=src.end(); ++it)
280 dest.push_back(evolutionModel.evolve(*it));
284 template <
class Particle,
class Numeric,
class QualificationModel,
class EvolutionModel,
class LikelyhoodModel>
289 void evolve(std::vector<Particle>& particles);
290 void evolve(std::vector<Particle>& dest,
const std::vector<Particle>& src);
293 template <
class Particle,
class Numeric,
class QualificationModel,
class EvolutionModel,
class LikelyhoodModel>
295 (std::vector<Particle>&particles){
296 std::vector<Numeric> observationWeights(particles.size());
298 for (
typename std::vector<Particle>::const_iterator it=particles.begin(); it!=particles.end(); ++it, i++){
299 observationWeights[i]=likelyhoodModel.likelyhood(qualificationModel.evolve(*it));
302 std::vector<unsigned int> indexes(resampler.
resampleIndexes(observationWeights));
303 for (
typename std::vector<unsigned int>::const_iterator it=indexes.begin(); it!=indexes.end(); it++){
305 particle=evolutionModel.evolve(particle);
306 particle.
setWeight(likelyhoodModel.likelyhood(particle)/observationWeights[*it]);
310 template <
class Particle,
class Numeric,
class QualificationModel,
class EvolutionModel,
class LikelyhoodModel>
312 (std::vector<Particle>& dest,
const std::vector<Particle>& src){
314 std::vector<Numeric> observationWeights(src.size());
316 for (
typename std::vector<Particle>::const_iterator it=src.begin(); it!=src.end(); ++it, i++){
317 observationWeights[i]=likelyhoodModel.likelyhood(qualificationModel.evolve(*it));
320 std::vector<unsigned int> indexes(resampler.
resampleIndexes(observationWeights));
321 for (
typename std::vector<unsigned int>::const_iterator it=indexes.begin(); it!=indexes.end(); it++){
323 dest.push_back(evolutionModel.evolve(particle));
324 dest.back().weight*=likelyhoodModel.likelyhood(particle)/observationWeights[*it];