-
Notifications
You must be signed in to change notification settings - Fork 207
/
particlefilter.h
328 lines (281 loc) · 9.24 KB
/
particlefilter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#ifndef PARTICLEFILTER_H
#define PARTICLEFILTER_H
#include <stdlib.h>
#include <sys/types.h>
#include <vector>
#include <utility>
#include <cmath>
#include <limits>
#include <gmapping/utils/gvalues.h>
/**
the particle class has to be convertible into numeric data type;
That means that a particle must define the Numeric conversion operator;
operator Numeric() const.
that returns the weight, and the method
setWeight(Numeric)
that sets the weight.
*/
typedef std::pair<uint,uint> UIntPair;
template <class OutputIterator, class Iterator>
double toNormalForm(OutputIterator& out, const Iterator & begin, const Iterator & end){
//determine the maximum
double lmax = -std::numeric_limits<double>::max();
for (Iterator it=begin; it!=end; it++){
lmax=lmax>((double)(*it))? lmax: (double)(*it);
}
//convert to raw form
for (Iterator it=begin; it!=end; it++){
*out=exp((double)(*it)-lmax);
out++;
}
return lmax;
}
template <class OutputIterator, class Iterator, class Numeric>
void toLogForm(OutputIterator& out, const Iterator & begin, const Iterator & end, Numeric lmax){
//determine the maximum
for (Iterator it=begin; it!=end; it++){
*out=log((Numeric)(*it))-lmax;
out++;
}
return lmax;
}
template <class WeightVector>
void resample(std::vector<int>& indexes, const WeightVector& weights, unsigned int nparticles=0){
double cweight=0;
//compute the cumulative weights
unsigned int n=0;
for (typename WeightVector::const_iterator it=weights.begin(); it!=weights.end(); ++it){
cweight+=(double)*it;
n++;
}
if (nparticles>0)
n=nparticles;
//compute the interval
double interval=cweight/n;
//compute the initial target weight
double target=interval*::drand48();
//compute the resampled indexes
cweight=0;
indexes.resize(n);
n=0;
unsigned int i=0;
for (typename WeightVector::const_iterator it=weights.begin(); it!=weights.end(); ++it, ++i){
cweight+=(double)* it;
while(cweight>target){
indexes[n++]=i;
target+=interval;
}
}
}
template <typename Vector>
void repeatIndexes(Vector& dest, const std::vector<int>& indexes, const Vector& particles){
assert(indexes.size()==particles.size());
dest.resize(particles.size());
unsigned int i=0;
for (std::vector<int>::const_iterator it=indexes.begin(); it!=indexes.end(); ++it){
dest[i]=particles[*it];
i++;
}
}
template <class Iterator>
double neff(const Iterator& begin, const Iterator& end){
double sum=0;
for (Iterator it=begin; it!=end; ++it){
sum+=*it;
}
double cum=0;
for (Iterator it=begin; it!=end; ++it){
double w=*it/sum;
cum+=w*w;
}
return 1./cum;
}
template <class Iterator>
void normalize(const Iterator& begin, const Iterator& end){
double sum=0;
for (Iterator it=begin; it!=end; ++it){
sum+=*it;
}
for (Iterator it=begin; it!=end; ++it){
*it=*it/sum;
}
}
template <class OutputIterator, class Iterator>
void rle(OutputIterator& out, const Iterator & begin, const Iterator & end){
unsigned int current=0;
unsigned int count=0;
for (Iterator it=begin; it!=end; it++){
if (it==begin){
current=*it;
count=1;
continue;
}
if (((uint)*it) ==current)
count++;
if (((uint)*it)!=current){
*out=std::make_pair(current,count);
out++;
current=*it;
count=1;
}
}
if (count>0)
*out=std::make_pair(current,count);
out++;
}
//BEGIN legacy
template <class Particle, class Numeric>
struct uniform_resampler{
std::vector<unsigned int> resampleIndexes(const std::vector<Particle> & particles, int nparticles=0) const;
std::vector<Particle> resample(const std::vector<Particle> & particles, int nparticles=0) const;
Numeric neff(const std::vector<Particle> & particles) const;
};
/*Implementation of the above stuff*/
template <class Particle, class Numeric>
std::vector<unsigned int> uniform_resampler<Particle, Numeric>:: resampleIndexes(const std::vector<Particle>& particles, int nparticles) const{
Numeric cweight=0;
//compute the cumulative weights
unsigned int n=0;
for (typename std::vector<Particle>::const_iterator it=particles.begin(); it!=particles.end(); ++it){
cweight+=(Numeric)*it;
n++;
}
if (nparticles>0)
n=nparticles;
//compute the interval
Numeric interval=cweight/n;
//compute the initial target weight
Numeric target=interval*::drand48();
//compute the resampled indexes
cweight=0;
std::vector<unsigned int> indexes(n);
n=0;
unsigned int i=0;
for (typename std::vector<Particle>::const_iterator it=particles.begin(); it!=particles.end(); ++it, ++i){
cweight+=(Numeric)* it;
while(cweight>target){
indexes[n++]=i;
target+=interval;
}
}
return indexes;
}
template <class Particle, class Numeric>
std::vector<Particle> uniform_resampler<Particle,Numeric>::resample
(const typename std::vector<Particle>& particles, int nparticles) const{
Numeric cweight=0;
//compute the cumulative weights
unsigned int n=0;
for (typename std::vector<Particle>::const_iterator it=particles.begin(); it!=particles.end(); ++it){
cweight+=(Numeric)*it;
n++;
}
if (nparticles>0)
n=nparticles;
//weight of the particles after resampling
double uw=1./n;
//compute the interval
Numeric interval=cweight/n;
//compute the initial target weight
Numeric target=interval*::drand48();
//compute the resampled indexes
cweight=0;
std::vector<Particle> resampled;
n=0;
unsigned int i=0;
for (typename std::vector<Particle>::const_iterator it=particles.begin(); it!=particles.end(); ++it, ++i){
cweight+=(Numeric)*it;
while(cweight>target){
resampled.push_back(*it);
resampled.back().setWeight(uw);
target+=interval;
}
}
return resampled;
}
template <class Particle, class Numeric>
Numeric uniform_resampler<Particle,Numeric>::neff(const std::vector<Particle> & particles) const{
double cum=0;
double sum=0;
for (typename std::vector<Particle>::const_iterator it=particles.begin(); it!=particles.end(); ++it){
Numeric w=(Numeric)*it;
cum+=w*w;
sum+=w;
}
return sum*sum/cum;
}
/*
The following are patterns for the evolution and the observation classes
The user should implement classes having the specified meaning
template <class State, class Numeric, class Observation>
struct observer{
Observation& observation
Numeric observe(const class State&) const;
};
template <class State, class Numeric, class Input>
struct evolver{
Input& input;
State& evolve(const State& s);
};
*/
template <class Particle, class EvolutionModel>
struct evolver{
EvolutionModel evolutionModel;
void evolve(std::vector<Particle>& particles);
void evolve(std::vector<Particle>& dest, const std::vector<Particle>& src);
};
template <class Particle, class EvolutionModel>
void evolver<Particle, EvolutionModel>::evolve(std::vector<Particle>& particles){
for (typename std::vector<Particle>::iterator it=particles.begin(); it!=particles.end(); ++it){
*it=evolutionModel.evolve(*it);
}
}
template <class Particle, class EvolutionModel>
void evolver<Particle, EvolutionModel>::evolve(std::vector<Particle>& dest, const std::vector<Particle>& src){
dest.clear();
for (typename std::vector<Particle>::const_iterator it=src.begin(); it!=src.end(); ++it)
dest.push_back(evolutionModel.evolve(*it));
}
template <class Particle, class Numeric, class QualificationModel, class EvolutionModel, class LikelyhoodModel>
struct auxiliary_evolver{
EvolutionModel evolutionModel;
QualificationModel qualificationModel;
LikelyhoodModel likelyhoodModel;
void evolve(std::vector<Particle>& particles);
void evolve(std::vector<Particle>& dest, const std::vector<Particle>& src);
};
template <class Particle, class Numeric, class QualificationModel, class EvolutionModel, class LikelyhoodModel>
void auxiliary_evolver<Particle, Numeric, QualificationModel, EvolutionModel, LikelyhoodModel>::evolve
(std::vector<Particle>&particles){
std::vector<Numeric> observationWeights(particles.size());
unsigned int i=0;
for (typename std::vector<Particle>::const_iterator it=particles.begin(); it!=particles.end(); ++it, i++){
observationWeights[i]=likelyhoodModel.likelyhood(qualificationModel.evolve(*it));
}
uniform_resampler<Numeric, Numeric> resampler;
std::vector<unsigned int> indexes(resampler.resampleIndexes(observationWeights));
for (typename std::vector<unsigned int>::const_iterator it=indexes.begin(); it!=indexes.end(); ++it){
Particle & particle=particles[*it];
particle=evolutionModel.evolve(particle);
particle.setWeight(likelyhoodModel.likelyhood(particle)/observationWeights[*it]);
}
}
template <class Particle, class Numeric, class QualificationModel, class EvolutionModel, class LikelyhoodModel>
void auxiliary_evolver<Particle, Numeric, QualificationModel, EvolutionModel, LikelyhoodModel>::evolve
(std::vector<Particle>& dest, const std::vector<Particle>& src){
dest.clear();
std::vector<Numeric> observationWeights(src.size());
unsigned int i=0;
for (typename std::vector<Particle>::const_iterator it=src.begin(); it!=src.end(); ++it, i++){
observationWeights[i]=likelyhoodModel.likelyhood(qualificationModel.evolve(*it));
}
uniform_resampler<Numeric, Numeric> resampler;
std::vector<unsigned int> indexes(resampler.resampleIndexes(observationWeights));
for (typename std::vector<unsigned int>::const_iterator it=indexes.begin(); it!=indexes.end(); ++it){
Particle & particle=src[*it];
dest.push_back(evolutionModel.evolve(particle));
dest.back().weight*=likelyhoodModel.likelyhood(particle)/observationWeights[*it];
}
}
//END legacy
#endif