OpenKalman
ParticleFilter.hpp
1 /* This file is part of OpenKalman, a header-only C++ library for
2  * Kalman filters and other recursive filters.
3  *
4  * Copyright (c) 2017-2021 Christopher Lee Ogden <ogden@gatech.edu>
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
9  */
10 
11 #ifndef OPENKALMAN_PARTICLEFILTER_HPP
12 #define OPENKALMAN_PARTICLEFILTER_HPP
13 
14 #include <algorithm>
15 #include <random>
16 #include <type_traits>
17 #include <Eigen/Dense>
18 
19 #include "distributions/ParticleDistribution.hpp"
20 #include "transformations/Transformation.h"
21 
22 
23 namespace OpenKalman {
24 
25  template<typename _StateTransitionModel,
26  typename _MeasurementModel>
28  {
29  using StateTransitionModel = _StateTransitionModel;
30  using MeasurementModel = _MeasurementModel;
31 
32  using State = typename StateTransitionModel::Input_t;
33  using Measurement = typename MeasurementModel::Output_t;
34  static_assert(std::is_convertible<typename MeasurementModel::Input_t, State>);
35 
36 
37  protected:
38  const StateTransitionModel& state_transition_model;
39  const MeasurementModel& measurement_model;
40 
41  public:
43  const StateTransitionModel& state_transition_model,
44  const MeasurementModel& measurement_model
45  ):
46  state_transition_model{state_transition_model},
47  measurement_model{measurement_model}
48  {}
49 
50 
51  template<typename _Distribution>
52  _Distribution&
53  predict(_Distribution& x) const
54  {
55  std::generate(x.begin(), x.end(), state_transition_model);
56  return x;
57  }
58 
59 
60  template<typename _Distribution, typename _Noise>
61  _Distribution &
62  predict(_Distribution& x,
63  const _Noise& process_noise) const
64  {
65  x = predict(x);
66 
67  std::random_device rd;
68  std::mt19937 gen = std::mt19937(rd());
69 
70  _Noise::covariance_t L = process_noise.get_sqrt_covariance();
71 
72  const auto add_noise = [&](const State& v) -> const State
73  {
74  // Create uncorrelated, normalized random variable (mean 0, variance 1)
75  State Xnorm;
76  for (int i=0; i<Samples_t::dim; i++) {
77  Xnorm(i) = std::normal_distribution<>(0,1)(gen);
78  }
79  return v + L * Xnorm;
80  };
81 
82  std::generate(x.begin(), x.end(), add_noise);
83  return x;
84  }
85 
86 
95  template<typename ... Args>
96  auto update(
97  const typename measurement_transform_t::Input_variable_t& x,
98  const measurement_t& z,
99  const Args& ... args) const
100  {
101  const auto& result = measurement_transform.template transform<true>(x, args...);
102  const auto& y = std::get<0>(result);
103  const auto& P_xy = std::get<1>(result);
104  return x.unaugment().kalmanUpdate(y, P_xy, z);
105  }
106 
107 
115  auto update(
116  typename measurement_transform_t::Input_variable_t::unaugmented_t& x,
117  const measurement_t& z,
118  const typename measurement_transform_t::Input_variable_t::augmentation_t& aug) const
119  {
120  const auto& result = measurement_transform.template transform<true>(typename measurement_transform_t::Input_variable_t(x, aug));
121  const auto& y = std::get<0>(result);
122  const auto& P_xy = std::get<1>(result);
123  return x.kalmanUpdate(y, P_xy, z);
124  }
125 
126  };
127 
128 }
129 
130 
131 #endif //OPENKALMAN_PARTICLEFILTER_HPP
The root namespace for OpenKalman.
Definition: basics.hpp:34
constexpr detail::generate_adaptor generate
a collection_view generator associated with generate_view.
Definition: generate.hpp:416
auto update(typename measurement_transform_t::Input_variable_t::unaugmented_t &x, const measurement_t &z, const typename measurement_transform_t::Input_variable_t::augmentation_t &aug) const
Update the state, using prior state possibly augmented with measurement noise, propagating variable a...
Definition: ParticleFilter.hpp:115
Definition: ParticleFilter.hpp:27
auto update(const typename measurement_transform_t::Input_variable_t &x, const measurement_t &z, const Args &... args) const
Update the state, using prior state possibly augmented with measurement noise, propagating variable a...
Definition: ParticleFilter.hpp:96