46 template <
typename FLT_TYPE =
float>
50 virtual FLT_TYPE&
operator[](
const size_t i) = 0;
51 virtual size_t size()
const = 0;
58 for (
size_t i = 0; i <
size(); i++)
60 ret[i] = (*this)[i] + in[i];
66 const T& e,
const size_t& j,
const size_t& k)
69 return ((*
this)[k] - exp[k]) * ((*this)[j] - exp[j]);
72 template <
typename T,
typename RANDOM_ENGINE,
typename NOISE_GEN>
75 const auto org_noise = gen(engine);
77 for (
size_t i = 0; i < noise.size(); i++)
79 noise[i] = org_noise[i];
85 template <
typename T,
typename FLT_TYPE =
float>
92 probability_bias_ = 0.0;
96 accum_probability_ = prob;
108 template <
typename T,
typename FLT_TYPE =
float>
122 void add(
const T& s,
const FLT_TYPE& prob)
127 for (
size_t i = 0; i < e1.size(); i++)
129 e1[i] = e1[i] * prob;
136 assert(p_sum_ > 0.0);
140 for (
size_t i = 0; i < s.size(); i++)
142 s[i] = s[i] / p_sum_;
154 template <
typename T,
typename FLT_TYPE =
float,
typename MEAN = ParticleWeightedMean<T, FLT_TYPE>,
155 typename RANDOM_ENGINE = std::default_random_engine>
160 explicit ParticleFilter(
const int num_particles,
const unsigned int random_seed = std::random_device()())
161 : engine_(random_seed)
163 particles_.resize(num_particles);
169 template <
typename GEN>
172 for (
auto& p : particles_)
174 p.state_ = T::template generateNoise<T>(engine_, generator);
175 p.probability_ = 1.0 / particles_.size();
182 template <
typename GEN>
186 for (
auto& p : particles_)
188 accum += p.probability_;
189 p.accum_probability_ = accum;
192 particles_dup_ = particles_;
193 std::sort(particles_dup_.begin(), particles_dup_.end());
194 const FLT_TYPE pstep = accum / particles_.size();
195 const FLT_TYPE initial_p = std::uniform_real_distribution<FLT_TYPE>(0.0, pstep)(engine_);
196 auto it = particles_dup_.begin();
197 auto it_prev = particles_dup_.begin();
198 const FLT_TYPE prob = 1.0 / particles_.size();
199 for (
size_t i = 0; i < particles_.size(); ++i)
201 auto& p = particles_[i];
202 const FLT_TYPE pscan = pstep * i + initial_p;
204 p.probability_ = prob;
205 if (it == particles_dup_.end())
207 p.state_ = it_prev->state_;
210 else if (it == it_prev)
212 p.state_ = it->state_ + T::template generateNoise<T>(engine_, generator);
213 p.state_.normalize();
217 p.state_ = it->state_;
226 template <
typename GEN>
229 for (
auto& p : particles_)
231 p.state_ = p.state_ + T::template generateNoise<T>(engine_, generator);
236 for (
auto& p : particles_)
241 void bias(std::function<
void(
const T&,
float& p_bias)> prob)
243 for (
auto& p : particles_)
245 prob(p.state_, p.probability_bias_);
248 void measure(std::function<FLT_TYPE(
const T&)> likelihood)
250 auto particles_prev = particles_;
252 for (
auto& p : particles_)
254 p.probability_ *= likelihood(p.state_);
255 sum += p.probability_;
259 for (
auto& p : particles_)
261 p.probability_ /= sum;
266 particles_ = particles_prev;
274 if (pass_ratio < 1.0)
275 std::sort(particles_.rbegin(), particles_.rend());
276 for (
auto& p : particles_)
278 mean.add(p.state_, p.probability_);
279 if (mean.getTotalProbability() > pass_ratio)
282 return mean.getMean();
288 for (
auto& p : particles_)
290 mean.add(p.state_, p.probability_ * p.probability_bias_);
292 return mean.getMean();
295 const FLT_TYPE pass_ratio = 1.0,
296 const FLT_TYPE random_sample_ratio = 1.0)
298 T e = expectation(pass_ratio);
301 cov.resize(e.size());
304 for (
auto& p : particles_)
307 p_sum += p.probability_;
308 if (p_sum > pass_ratio)
312 std::vector<size_t> indices(p_num);
313 std::iota(indices.begin(), indices.end(), 0);
314 if (random_sample_ratio < 1.0)
316 std::shuffle(indices.begin(), indices.end(), engine_);
318 const size_t sample_num =
323 static_cast<size_t>(p_num * random_sample_ratio)));
324 indices.resize(sample_num);
328 for (
size_t i : indices)
330 auto& p = particles_[i];
331 p_sum += p.probability_;
332 for (
size_t j = 0; j < ie_.size(); j++)
334 for (
size_t k = j; k < ie_.size(); k++)
336 cov[k][j] = cov[j][k] += p.state_.covElement(e, j, k) * p.probability_;
340 for (
size_t j = 0; j < ie_.size(); j++)
342 for (
size_t k = 0; k < ie_.size(); k++)
352 T* m = &particles_[0].state_;
353 FLT_TYPE max_probability = particles_[0].probability_;
354 for (
auto& p : particles_)
356 if (max_probability < p.probability_)
358 max_probability = p.probability_;
366 T* m = &particles_[0].state_;
367 FLT_TYPE max_probability =
368 particles_[0].probability_ * particles_[0].probability_bias_;
369 for (
auto& p : particles_)
371 const FLT_TYPE prob = p.probability_ * p.probability_bias_;
372 if (max_probability < prob)
374 max_probability = prob;
382 return particles_[i].state_;
386 return particles_.size();
391 for (
auto& p : particles_)
393 accum += p.probability_;
394 p.accum_probability_ = accum;
397 particles_dup_ = particles_;
398 std::sort(particles_dup_.begin(), particles_dup_.end());
400 FLT_TYPE pstep = accum / num;
402 auto it = particles_dup_.begin();
403 auto it_prev = particles_dup_.begin();
405 particles_.resize(num);
407 FLT_TYPE prob = 1.0 / num;
408 for (
auto& p : particles_)
411 it = std::lower_bound(it, particles_dup_.end(),
413 p.probability_ = prob;
414 if (it == particles_dup_.end())
416 p.state_ = it_prev->state_;
421 p.state_ = it->state_;
426 typename std::vector<Particle<T, FLT_TYPE>>::iterator
appendParticle(
const size_t num)
428 const size_t size_orig = particles_.size();
429 particles_.resize(size_orig + num);
430 return begin() + size_orig;
432 typename std::vector<Particle<T, FLT_TYPE>>::iterator
begin()
434 return particles_.begin();
436 typename std::vector<Particle<T, FLT_TYPE>>::iterator
end()
438 return particles_.end();
451 #endif // MCL_3DL_PF_H virtual FLT_TYPE & operator[](const size_t i)=0
T getParticle(const size_t i) const
size_t getParticleSize() const
T expectation(const FLT_TYPE pass_ratio=1.0)
void resizeParticle(const size_t num)
void init(T mean, T sigma)
static T generateNoise(RANDOM_ENGINE &engine, const NOISE_GEN &gen)
std::vector< T > covariance(const FLT_TYPE pass_ratio=1.0, const FLT_TYPE random_sample_ratio=1.0)
FLT_TYPE covElement(const T &e, const size_t &j, const size_t &k)
void initUsingNoiseGenerator(const GEN &generator)
FLT_TYPE probability_bias_
std::vector< Particle< T, FLT_TYPE > > particles_dup_
std::vector< Particle< T, FLT_TYPE > >::iterator appendParticle(const size_t num)
void add(const T &s, const FLT_TYPE &prob)
std::vector< Particle< T, FLT_TYPE > >::iterator begin()
std::vector< Particle< T, FLT_TYPE > > particles_
bool operator<(const Particle &p2) const
void resampleUsingNoiseGenerator(const GEN &generator)
INLINE Rall1d< T, V, S > exp(const Rall1d< T, V, S > &arg)
std::vector< Particle< T, FLT_TYPE > >::iterator end()
virtual size_t size() const =0
virtual void normalize()=0
FLT_TYPE getTotalProbability()
ParticleFilter(const int num_particles, const unsigned int random_seed=std::random_device()())
void addNoiseUsingNoiseGenerator(const GEN &generator)
FLT_TYPE accum_probability_
void measure(std::function< FLT_TYPE(const T &)> likelihood)
void predict(std::function< void(T &)> model)
void bias(std::function< void(const T &, float &p_bias)> prob)