-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparticle_filter.rs
357 lines (332 loc) · 11.5 KB
/
particle_filter.rs
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#![allow(dead_code)] // TODO: remove this
use nalgebra::{allocator::Allocator, Const, DefaultAllocator, Dim, OMatrix, OVector, RealField};
use rand::distributions::Distribution;
use rand::Rng;
use rand_distr::{Standard, StandardNormal};
use rustc_hash::FxHashMap;
use crate::localization::bayesian_filter::{BayesianFilter, BayesianFilterKnownCorrespondences};
use crate::models::measurement::MeasurementModel;
use crate::models::motion::MotionModel;
use crate::utils::mvn::MultiVariateNormal;
use crate::utils::state::GaussianState;
pub enum ResamplingScheme {
IID,
Stratified,
Systematic,
}
/// S : State Size, Z: Observation Size, U: Input Size
pub struct ParticleFilter<T: RealField, S: Dim, Z: Dim, U: Dim>
where
DefaultAllocator: Allocator<T, S> + Allocator<T, S, S> + Allocator<T, Z, Z>,
{
r: OMatrix<T, S, S>,
q: OMatrix<T, Z, Z>,
measurement_model: Box<dyn MeasurementModel<T, S, Z> + Send>,
motion_model: Box<dyn MotionModel<T, S, Z, U> + Send>,
pub particules: Vec<OVector<T, S>>,
resampling_scheme: ResamplingScheme,
}
impl<T: RealField + Copy, S: Dim, Z: Dim, U: Dim> ParticleFilter<T, S, Z, U>
where
StandardNormal: Distribution<T>,
Standard: Distribution<T>,
DefaultAllocator: Allocator<T, S>
+ Allocator<T, S, S>
+ Allocator<T, Z, Z>
+ Allocator<T, Const<1>, S>
+ Allocator<T, Const<1>, Z>,
{
pub fn new(
r: OMatrix<T, S, S>,
q: OMatrix<T, Z, Z>,
measurement_model: Box<dyn MeasurementModel<T, S, Z> + Send>,
motion_model: Box<dyn MotionModel<T, S, Z, U> + Send>,
initial_state: GaussianState<T, S>,
num_particules: usize,
resampling_scheme: ResamplingScheme,
) -> ParticleFilter<T, S, Z, U> {
let mvn = MultiVariateNormal::new(&initial_state.x, &r).unwrap();
let mut particules = Vec::with_capacity(num_particules);
for _ in 0..num_particules {
particules.push(mvn.sample());
}
ParticleFilter {
r,
q,
measurement_model,
motion_model,
particules,
resampling_scheme,
}
}
}
impl<T: RealField + Copy, S: Dim, Z: Dim, U: Dim> BayesianFilter<T, S, Z, U>
for ParticleFilter<T, S, Z, U>
where
DefaultAllocator: Allocator<T, S>
+ Allocator<T, U>
+ Allocator<T, Z>
+ Allocator<T, S, S>
+ Allocator<T, Z, Z>
+ Allocator<T, Z, S>
+ Allocator<T, S, U>
+ Allocator<T, U, U>
+ Allocator<T, Const<1>, S>
+ Allocator<T, Const<1>, Z>,
Standard: Distribution<T>,
StandardNormal: Distribution<T>,
{
fn update_estimate(&mut self, u: &OVector<T, U>, z: &OVector<T, Z>, dt: T) {
let shape = self.particules[0].shape_generic();
let mvn =
MultiVariateNormal::new(&OMatrix::zeros_generic(shape.0, shape.1), &self.r).unwrap();
self.particules = self
.particules
.iter()
.map(|p| self.motion_model.prediction(p, u, dt) + mvn.sample())
.collect();
let mut weights = vec![T::one(); self.particules.len()];
let shape = z.shape_generic();
let mvn =
MultiVariateNormal::new(&OMatrix::zeros_generic(shape.0, shape.1), &self.q).unwrap();
for (i, particule) in self.particules.iter().enumerate() {
let z_pred = self.measurement_model.prediction(particule, None);
let error = z - z_pred;
let pdf = mvn.pdf(&error);
weights[i] *= pdf;
}
self.particules = match self.resampling_scheme {
ResamplingScheme::IID => resampling_sort(&self.particules, &weights),
ResamplingScheme::Stratified => resampling_stratified(&self.particules, &weights),
ResamplingScheme::Systematic => resampling_systematic(&self.particules, &weights),
};
}
fn gaussian_estimate(&self) -> GaussianState<T, S> {
gaussian_estimate(&self.particules)
}
}
/// S : State Size, Z: Observation Size, U: Input Size
pub struct ParticleFilterKnownCorrespondences<T: RealField, S: Dim, Z: Dim, U: Dim>
where
DefaultAllocator: Allocator<T, S> + Allocator<T, S, S> + Allocator<T, Z, Z>,
{
q: OMatrix<T, Z, Z>,
landmarks: FxHashMap<u32, OVector<T, S>>,
measurement_model: Box<dyn MeasurementModel<T, S, Z> + Send>,
motion_model: Box<dyn MotionModel<T, S, Z, U> + Send>,
pub particules: Vec<OVector<T, S>>,
}
impl<T: RealField + Copy, S: Dim, Z: Dim, U: Dim> ParticleFilterKnownCorrespondences<T, S, Z, U>
where
StandardNormal: Distribution<T>,
Standard: Distribution<T>,
DefaultAllocator:
Allocator<T, S> + Allocator<T, S, S> + Allocator<T, Z, Z> + Allocator<T, Const<1>, S>,
{
pub fn new(
initial_noise: OMatrix<T, S, S>,
q: OMatrix<T, Z, Z>,
landmarks: FxHashMap<u32, OVector<T, S>>,
measurement_model: Box<dyn MeasurementModel<T, S, Z> + Send>,
motion_model: Box<dyn MotionModel<T, S, Z, U> + Send>,
initial_state: GaussianState<T, S>,
num_particules: usize,
) -> ParticleFilterKnownCorrespondences<T, S, Z, U> {
let mvn = MultiVariateNormal::new(&initial_state.x, &initial_noise).unwrap();
let mut particules = Vec::with_capacity(num_particules);
for _ in 0..num_particules {
particules.push(mvn.sample());
}
ParticleFilterKnownCorrespondences {
q,
landmarks,
measurement_model,
motion_model,
particules,
}
}
}
impl<T: RealField + Copy, S: Dim, Z: Dim, U: Dim> BayesianFilterKnownCorrespondences<T, S, Z, U>
for ParticleFilterKnownCorrespondences<T, S, Z, U>
where
StandardNormal: Distribution<T>,
Standard: Distribution<T>,
DefaultAllocator: Allocator<T, S>
+ Allocator<T, U>
+ Allocator<T, Z>
+ Allocator<T, S, S>
+ Allocator<T, Z, Z>
+ Allocator<T, Z, S>
+ Allocator<T, S, U>
+ Allocator<T, U, U>
+ Allocator<T, Const<1>, S>
+ Allocator<T, Const<1>, Z>,
{
fn update_estimate(
&mut self,
control: Option<OVector<T, U>>,
measurements: Option<Vec<(u32, OVector<T, Z>)>>,
dt: T,
) {
if let Some(u) = control {
self.particules = self
.particules
.iter()
.map(|p| self.motion_model.sample(p, &u, dt))
.collect();
}
if let Some(measurements) = measurements {
let mut weights = vec![T::one(); self.particules.len()];
let shape = measurements[0].1.shape_generic();
let mvn = MultiVariateNormal::new(&OMatrix::zeros_generic(shape.0, shape.1), &self.q)
.unwrap();
for (id, z) in measurements
.iter()
.filter(|(id, _)| self.landmarks.contains_key(id))
{
let landmark = self.landmarks.get(id);
for (i, particule) in self.particules.iter().enumerate() {
let z_pred = self.measurement_model.prediction(particule, landmark);
let error = z - z_pred;
let pdf = mvn.pdf(&error);
weights[i] *= pdf;
}
}
self.particules = resampling(&self.particules, &weights);
// self.particules = resampling_sort(&self.particules, weights);
}
}
fn gaussian_estimate(&self) -> GaussianState<T, S> {
gaussian_estimate(&self.particules)
}
}
fn gaussian_estimate<T: RealField + Copy, S: Dim>(
particules: &[OVector<T, S>],
) -> GaussianState<T, S>
where
DefaultAllocator: Allocator<T, S> + Allocator<T, S, S> + Allocator<T, Const<1>, S>,
{
let shape = particules[0].shape_generic();
let x = particules
.iter()
.fold(OMatrix::zeros_generic(shape.0, shape.1), |a, b| a + b)
/ T::from_usize(particules.len()).unwrap();
let cov = particules
.iter()
.map(|p| p - &x)
.map(|dx| &dx * dx.transpose())
.fold(OMatrix::zeros_generic(shape.0, shape.0), |a, b| a + b)
/ T::from_usize(particules.len()).unwrap();
GaussianState { x, cov }
}
fn resampling<T: RealField + Copy, S: Dim>(
particules: &Vec<OVector<T, S>>,
weights: &[T],
) -> Vec<OVector<T, S>>
where
DefaultAllocator: Allocator<T, S>,
Standard: Distribution<T>,
{
let cum_weight: Vec<T> = weights
.iter()
.scan(T::zero(), |state, &x| {
*state += x;
Some(*state)
})
.collect();
let weight_tot = *cum_weight.last().unwrap();
// sampling
let mut rng = rand::thread_rng();
(0..particules.len())
.map(|_| {
let rng_nb = rng.gen::<T>() * weight_tot;
for (i, p) in particules.iter().enumerate() {
if (&cum_weight)[i] > rng_nb {
return p.clone();
}
}
unreachable!()
})
.collect()
}
fn resampling_sort<T: RealField + Copy, S: Dim>(
particules: &Vec<OVector<T, S>>,
weights: &[T],
) -> Vec<OVector<T, S>>
where
DefaultAllocator: Allocator<T, S>,
Standard: Distribution<T>,
{
let total_weight: T = weights.iter().fold(T::zero(), |a, b| a + *b);
let mut rng = rand::thread_rng();
let mut draws: Vec<T> = (0..particules.len())
.map(|_| rng.gen::<T>() * total_weight)
.collect();
resample(&mut draws, total_weight, particules, weights)
}
fn resampling_stratified<T: RealField + Copy, S: Dim>(
particules: &Vec<OVector<T, S>>,
weights: &[T],
) -> Vec<OVector<T, S>>
where
DefaultAllocator: Allocator<T, S>,
Standard: Distribution<T>,
{
let total_weight: T = weights.iter().fold(T::zero(), |a, b| a + *b);
let mut rng = rand::thread_rng();
let mut draws: Vec<T> = (0..particules.len())
.map(|i| {
(T::from_usize(i).unwrap() + rng.gen::<T>()) / T::from_usize(particules.len()).unwrap()
* total_weight
})
.collect();
resample(&mut draws, total_weight, particules, weights)
}
fn resampling_systematic<T: RealField + Copy, S: Dim>(
particules: &Vec<OVector<T, S>>,
weights: &[T],
) -> Vec<OVector<T, S>>
where
DefaultAllocator: Allocator<T, S>,
Standard: Distribution<T>,
{
let total_weight: T = weights.iter().fold(T::zero(), |a, b| a + *b);
let mut rng = rand::thread_rng();
let draw = rng.gen::<T>();
let mut draws: Vec<T> = (0..particules.len())
.map(|i| {
(T::from_usize(i).unwrap() + draw) / T::from_usize(particules.len()).unwrap()
* total_weight
})
.collect();
resample(&mut draws, total_weight, particules, weights)
}
fn resample<T: RealField + Copy, S: Dim>(
draws: &mut [T],
total_weight: T,
particules: &Vec<OVector<T, S>>,
weights: &[T],
) -> Vec<OVector<T, S>>
where
DefaultAllocator: Allocator<T, S>,
Standard: Distribution<T>,
{
draws.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap());
let mut index = 0;
let mut cum_weight = draws[0];
(0..particules.len())
.map(|i| {
while cum_weight < draws[i] {
if index == particules.len() - 1 {
// weird precision edge case
cum_weight = total_weight;
break;
} else {
cum_weight += weights[index];
index += 1;
}
}
particules[index].clone()
})
.collect()
}