43 template <
typename FLT_TYPE =
float>
47 virtual FLT_TYPE&
operator[](
const size_t i) = 0;
48 virtual size_t size()
const = 0;
55 for (
size_t i = 0; i <
size(); i++)
57 ret[i] = (*this)[i] + in[i];
63 const T& e,
const size_t& j,
const size_t& k)
66 return ((*
this)[k] - exp[k]) * ((*this)[j] - exp[j]);
70 std::default_random_engine& engine_,
74 for (
size_t i = 0; i < noise.size(); i++)
76 std::normal_distribution<FLT_TYPE> nd(mean[i], sigma[i]);
77 noise[i] = nd(engine_);
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>>
159 : engine_(seed_gen_())
161 particles_.resize(num_particles);
165 for (
auto& p : particles_)
167 p.state_ = T::generateNoise(engine_, mean, sigma);
168 p.probability_ = 1.0 / particles_.size();
174 for (
auto& p : particles_)
176 accum += p.probability_;
177 p.accum_probability_ = accum;
180 particles_dup_ = particles_;
181 std::sort(particles_dup_.begin(), particles_dup_.end());
183 const FLT_TYPE pstep = accum / particles_.size();
184 auto it = particles_dup_.begin();
185 auto it_prev = particles_dup_.begin();
186 const FLT_TYPE prob = 1.0 / particles_.size();
187 for (
size_t i = 0; i < particles_.size(); ++i)
189 auto &p = particles_[i];
190 const FLT_TYPE pscan = std::nextafter(pstep * (i + 1), static_cast<FLT_TYPE>(0.0));
192 p.probability_ = prob;
193 if (it == particles_dup_.end())
195 p.state_ = it_prev->state_;
198 else if (it == it_prev)
200 p.state_ = it->state_ + T::generateNoise(engine_, T(), sigma);
201 p.state_.normalize();
205 p.state_ = it->state_;
212 for (
auto& p : particles_)
214 p.state_ = p.state_ + T::generateNoise(engine_, T(), sigma);
219 for (
auto& p : particles_)
224 void bias(std::function<
void(
const T&,
float& p_bias)> prob)
226 for (
auto& p : particles_)
228 prob(p.state_, p.probability_bias_);
231 void measure(std::function<FLT_TYPE(
const T&)> likelihood)
233 auto particles_prev = particles_;
235 for (
auto& p : particles_)
237 p.probability_ *= likelihood(p.state_);
238 sum += p.probability_;
242 for (
auto& p : particles_)
244 p.probability_ /= sum;
249 particles_ = particles_prev;
257 if (pass_ratio < 1.0)
258 std::sort(particles_.rbegin(), particles_.rend());
259 for (
auto& p : particles_)
261 mean.add(p.state_, p.probability_);
262 if (mean.getTotalProbability() > pass_ratio)
265 return mean.getMean();
271 for (
auto& p : particles_)
273 mean.add(p.state_, p.probability_ * p.probability_bias_);
275 return mean.getMean();
279 T e = expectation(pass_ratio);
283 cov.resize(e.size());
285 for (
auto& p : particles_)
288 p_sum += p.probability_;
289 if (p_sum > pass_ratio)
293 for (
auto& p : particles_)
295 for (
size_t j = 0; j < ie_.size(); j++)
297 for (
size_t k = j; k < ie_.size(); k++)
299 cov[k][j] = cov[j][k] += p.state_.covElement(e, j, k) * p.probability_;
303 p_sum += p.probability_;
304 if (p_sum > pass_ratio)
307 for (
size_t j = 0; j < ie_.size(); j++)
309 for (
size_t k = j; k < ie_.size(); k++)
320 T* m = &particles_[0].state_;
321 FLT_TYPE max_probability = particles_[0].probability_;
322 for (
auto& p : particles_)
324 if (max_probability < p.probability_)
326 max_probability = p.probability_;
334 T* m = &particles_[0].state_;
335 FLT_TYPE max_probability =
336 particles_[0].probability_ * particles_[0].probability_bias_;
337 for (
auto& p : particles_)
339 const FLT_TYPE prob = p.probability_ * p.probability_bias_;
340 if (max_probability < prob)
342 max_probability = prob;
350 return particles_[i].state_;
354 return particles_.size();
359 for (
auto& p : particles_)
361 accum += p.probability_;
362 p.accum_probability_ = accum;
365 particles_dup_ = particles_;
366 std::sort(particles_dup_.begin(), particles_dup_.end());
368 FLT_TYPE pstep = accum / num;
370 auto it = particles_dup_.begin();
371 auto it_prev = particles_dup_.begin();
373 particles_.resize(num);
375 FLT_TYPE prob = 1.0 / num;
376 for (
auto& p : particles_)
379 it = std::lower_bound(it, particles_dup_.end(),
381 p.probability_ = prob;
382 if (it == particles_dup_.end())
384 p.state_ = it_prev->state_;
389 p.state_ = it->state_;
394 typename std::vector<Particle<T, FLT_TYPE>>::iterator
appendParticle(
const size_t num)
396 const size_t size_orig = particles_.size();
397 particles_.resize(size_orig + num);
398 return begin() + size_orig;
400 typename std::vector<Particle<T, FLT_TYPE>>::iterator
begin()
402 return particles_.begin();
404 typename std::vector<Particle<T, FLT_TYPE>>::iterator
end()
406 return particles_.end();
420 #endif // MCL_3DL_PF_H virtual FLT_TYPE & operator[](const size_t i)=0
T expectation(const FLT_TYPE pass_ratio=1.0)
std::vector< Particle< T, FLT_TYPE > > particles_
void measure(std::function< FLT_TYPE(const T &)> likelihood)
std::vector< Particle< T, FLT_TYPE > >::iterator appendParticle(const size_t num)
FLT_TYPE covElement(const T &e, const size_t &j, const size_t &k)
std::vector< T > covariance(const FLT_TYPE pass_ratio=1.0)
void predict(std::function< void(T &)> model)
size_t getParticleSize() const
FLT_TYPE probability_bias_
void init(T mean, T sigma)
void add(const T &s, const FLT_TYPE &prob)
std::vector< Particle< T, FLT_TYPE > >::iterator end()
void bias(std::function< void(const T &, float &p_bias)> prob)
T getParticle(const size_t i) const
bool operator<(const Particle &p2) const
INLINE Rall1d< T, V, S > exp(const Rall1d< T, V, S > &arg)
void resizeParticle(const size_t num)
virtual size_t size() const =0
virtual void normalize()=0
FLT_TYPE getTotalProbability()
std::vector< Particle< T, FLT_TYPE > > particles_dup_
ParticleFilter(const int num_particles)
std::vector< Particle< T, FLT_TYPE > >::iterator begin()
std::default_random_engine engine_
FLT_TYPE accum_probability_
std::random_device seed_gen_
static T generateNoise(std::default_random_engine &engine_, T mean, T sigma)