The following snippet code shows how to run an Unscented Particle Filter (UPF) using the implementation provided by the library.
27 #include <Eigen/Dense>
30 using namespace Eigen;
33 class UPFSimulation :
public SIS
38 std::size_t num_particle,
39 std::size_t state_size,
40 std::size_t simulation_steps,
42 Ref<MatrixXd> initial_covariance,
43 std::unique_ptr<ParticleSetInitialization> initialization,
44 std::unique_ptr<PFPrediction> prediction,
45 std::unique_ptr<PFCorrection> correction,
46 std::unique_ptr<Resampling> resampling
48 SIS(num_particle, state_size, std::move(initialization), std::move(prediction), std::move(correction), std::move(resampling)),
49 simulation_steps_(simulation_steps),
50 initial_covariance_(initial_covariance),
51 estimates_extraction_(state_size)
55 bool run_condition()
override
57 if (step_number() < simulation_steps_)
63 std::vector<std::string> log_file_names(
const std::string& folder_path,
const std::string& file_name_prefix)
override
65 std::vector<std::string> sis_filenames =
SIS::log_file_names(folder_path, file_name_prefix);
68 sis_filenames.push_back(folder_path +
"/" + file_name_prefix +
"_mean");
73 bool initialization_step()
override
81 for (std::size_t i = 0; i < pred_particle_.components; i++)
84 pred_particle_.mean(i) = pred_particle_.state(i);
87 pred_particle_.covariance(i) = initial_covariance_;
96 std::tie(std::ignore, mean) = estimates_extraction_.extract(cor_particle_.state(), cor_particle_.weight());
98 logger(pred_particle_.state().transpose(), pred_particle_.weight().transpose(),
99 cor_particle_.state().transpose(), cor_particle_.weight().transpose(),
104 std::size_t simulation_steps_;
106 Eigen::MatrixXd initial_covariance_;
112 int main(
int argc,
char* argv[])
114 std::cout <<
"Running an unscented particle filter on a simulated target." << std::endl;
116 const bool write_to_file = (argc > 1 ? std::string(argv[1]) ==
"ON" :
false);
118 std::cout <<
"Data is logged in the test folder with prefix testUPF." << std::endl;
122 double surv_x = 1000.0;
123 double surv_y = 1000.0;
124 std::size_t num_particle_x = 7;
125 std::size_t num_particle_y = 7;
126 std::size_t num_particle = num_particle_x * num_particle_y;
127 Vector4d initial_state(10.0f, 0.0f, 10.0f, 0.0f);
128 std::size_t simulation_time = 100;
129 std::size_t state_size = 4;
138 Matrix4d initial_covariance;
139 initial_covariance << pow(0.05, 2), 0, 0, 0,
140 0, pow(0.05, 2), 0, 0,
141 0, 0, pow(0.01, 2), 0,
142 0, 0, 0, pow(0.01, 2);
145 std::unique_ptr<ParticleSetInitialization> grid_initialization = utils::make_unique<InitSurveillanceAreaGrid>(surv_x, surv_y, num_particle_x, num_particle_y);
154 double tilde_q = 10.0f;
162 std::unique_ptr<GaussianPrediction> upf_prediction = utils::make_unique<UKFPrediction>(std::move(wna), alpha, beta, kappa);
163 std::unique_ptr<PFPrediction> gpf_prediction = utils::make_unique<GPFPrediction>(std::move(upf_prediction));
172 std::unique_ptr<SimulatedStateModel> simulated_state_model = utils::make_unique<SimulatedStateModel>(std::move(target_model), initial_state, simulation_time);
175 simulated_state_model->enable_log(
"./",
"testUPF");
178 double sigma_x = 10.0;
179 double sigma_y = 10.0;
180 Eigen::MatrixXd R(2, 2);
181 R << std::pow(sigma_x, 2.0), 0.0,
182 0.0, std::pow(sigma_y, 2.0);
184 std::unique_ptr<AdditiveMeasurementModel> simulated_linear_sensor = utils::make_unique<SimulatedLinearSensor>(std::move(simulated_state_model),
SimulatedLinearSensor::LinearMatrixComponent{ 4, std::vector<std::size_t>{ 0, 2 } }, R);
187 simulated_linear_sensor->enable_log(
"./",
"testUPF");
193 std::unique_ptr<LikelihoodModel> exp_likelihood = utils::make_unique<GaussianLikelihood>();
203 std::unique_ptr<GaussianCorrection> upf_correction = utils::make_unique<UKFCorrection>(std::move(simulated_linear_sensor), alpha, beta, kappa);
204 std::unique_ptr<PFCorrection> gpf_correction = utils::make_unique<GPFCorrection>(std::move(exp_likelihood), std::move(upf_correction), std::move(transition_probability_model));
210 std::unique_ptr<Resampling> resampling = utils::make_unique<Resampling>();
214 std::cout <<
"Constructing unscented particle filter..." << std::flush;
216 UPFSimulation upf(num_particle, state_size, simulation_time, initial_covariance, std::move(grid_initialization), std::move(gpf_prediction), std::move(gpf_correction), std::move(resampling));
219 upf.enable_log(
"./",
"testUPF");
221 std::cout <<
"done!" << std::endl;
225 std::cout <<
"Booting unscented particle filter..." << std::flush;
229 std::cout <<
"completed!" << std::endl;
234 std::cout <<
"Running unscented particle filter..." << std::flush;
238 std::cout <<
"waiting..." << std::flush;
243 std::cout <<
"completed!" << std::endl;