Bayes Filters Library
Resampling.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2019 Istituto Italiano di Tecnologia (IIT)
3  *
4  * This software may be modified and distributed under the terms of the
5  * BSD 3-Clause license. See the accompanying LICENSE file for details.
6  */
7 
9 
10 #include <utility>
11 
12 using namespace bfl;
13 using namespace Eigen;
14 
15 
16 Resampling::Resampling(unsigned int seed) noexcept :
17  generator_(std::mt19937_64(seed))
18 { }
19 
20 
22  Resampling(1)
23 { }
24 
25 
26 Resampling::Resampling(const Resampling& resampling) noexcept :
27  generator_(resampling.generator_)
28 { }
29 
30 
31 Resampling::Resampling(Resampling&& resampling) noexcept :
32  generator_(std::move(resampling.generator_))
33 { }
34 
35 
37 {
38  Resampling tmp(resampling);
39  *this = std::move(tmp);
40 
41  return *this;
42 }
43 
44 
46 {
47  if (this == &resampling)
48  return *this;
49 
50  generator_ = std::move(resampling.generator_);
51 
52  return *this;
53 }
54 
55 
56 Resampling& Resampling::operator=(const Resampling&& resampling) noexcept
57 {
58  if (this == &resampling)
59  return *this;
60 
61  generator_ = std::move(resampling.generator_);
62 
63  return *this;
64 }
65 
66 
67 void Resampling::resample(const ParticleSet& cor_particles, ParticleSet& res_particles,
68  Ref<VectorXi> res_parents)
69 {
70  int num_particles = static_cast<int>(cor_particles.weight().rows());
71  VectorXd csw(num_particles);
72 
73  csw(0) = exp(cor_particles.weight(0));
74  for (int i = 1; i < num_particles; ++i)
75  csw(i) = csw(i-1) + exp(cor_particles.weight(i));
76 
77  std::uniform_real_distribution<double> distribution_res(0.0, 1.0/num_particles);
78  double u_1 = distribution_res(generator_);
79 
80  int idx_csw = 0;
81  for (int j = 0; j < num_particles; ++j)
82  {
83  double u_j = u_1 + static_cast<double>(j)/num_particles;
84 
85  while (u_j > csw(idx_csw) && idx_csw < (num_particles - 1))
86  idx_csw += 1;
87 
88  res_particles.state(j) = cor_particles.state(idx_csw);
89  res_particles.mean(j) = cor_particles.mean(idx_csw);
90  res_particles.covariance(j) = cor_particles.covariance(idx_csw);
91  res_particles.weight(j) = -std::log(num_particles);
92  res_parents(j) = idx_csw;
93  }
94 }
95 
96 
97 double Resampling::neff(const Ref<const VectorXd>& cor_weights)
98 {
99  return 1.0/cor_weights.array().exp().square().sum();
100 }
bfl::Resampling::Resampling
Resampling() noexcept
Definition: Resampling.cpp:21
bfl::Resampling::resample
virtual void resample(const bfl::ParticleSet &cor_particles, bfl::ParticleSet &res_particles, Eigen::Ref< Eigen::VectorXi > res_parents)
Definition: Resampling.cpp:67
bfl
Port of boost::any for C++11 compilers.
Definition: AdditiveMeasurementModel.h:13
Resampling.h
bfl::GaussianMixture::mean
Eigen::Ref< Eigen::MatrixXd > mean()
Definition: GaussianMixture.cpp:94
bfl::GaussianMixture::covariance
Eigen::Ref< Eigen::MatrixXd > covariance()
Definition: GaussianMixture.cpp:130
bfl::Resampling
Definition: Resampling.h:22
bfl::ParticleSet::state
Eigen::Ref< Eigen::MatrixXd > state()
Definition: ParticleSet.cpp:84
bfl::Resampling::neff
virtual double neff(const Eigen::Ref< const Eigen::VectorXd > &cor_weights)
Definition: Resampling.cpp:97
bfl::ParticleSet
Definition: ParticleSet.h:20
bfl::GaussianMixture::weight
Eigen::Ref< Eigen::VectorXd > weight()
Definition: GaussianMixture.cpp:166
bfl::Resampling::operator=
Resampling & operator=(const Resampling &resampling)
Definition: Resampling.cpp:36