The following snippet code shows how to run a Sequential Importance Sampling (SIS) particle filter using the implementation provided by the library.
23 #include <Eigen/Dense>
26 using namespace Eigen;
29 class SISSimulation :
public SIS
34 unsigned int num_particle,
35 std::size_t state_size,
36 unsigned int simulation_steps,
37 std::unique_ptr<ParticleSetInitialization> initialization,
38 std::unique_ptr<PFPrediction> prediction,
39 std::unique_ptr<PFCorrection> correction,
40 std::unique_ptr<Resampling> resampling
42 SIS(num_particle, state_size, std::move(initialization), std::move(prediction), std::move(correction), std::move(resampling)),
43 simulation_steps_(simulation_steps),
44 estimates_extraction_(state_size)
48 bool run_condition()
override
50 if (step_number() < simulation_steps_)
56 std::vector<std::string> log_file_names(
const std::string& folder_path,
const std::string& file_name_prefix)
override
58 std::vector<std::string> sis_filenames =
SIS::log_file_names(folder_path, file_name_prefix);
61 sis_filenames.push_back(folder_path +
"/" + file_name_prefix +
"_mean");
66 bool initialization_step()
override
79 std::tie(std::ignore, mean) = estimates_extraction_.extract(cor_particle_.state(), cor_particle_.weight());
81 logger(pred_particle_.state().transpose(), pred_particle_.weight().transpose(),
82 cor_particle_.state().transpose(), cor_particle_.weight().transpose(),
87 unsigned int simulation_steps_;
93 int main(
int argc,
char* argv[])
95 std::cout <<
"Running a SIS particle filter on a simulated target." << std::endl;
97 const bool write_to_file = (argc > 1 ? std::string(argv[1]) ==
"ON" :
false);
99 std::cout <<
"Data is logged in the test folder with prefix testSIS." << std::endl;
103 double surv_x = 1000.0;
104 double surv_y = 1000.0;
105 unsigned int num_particle_x = 30;
106 unsigned int num_particle_y = 30;
107 unsigned int num_particle = num_particle_x * num_particle_y;
108 Vector4d initial_state(10.0f, 0.0f, 10.0f, 0.0f);
109 unsigned int simulation_time = 100;
110 std::size_t state_size = 4;
114 std::unique_ptr<ParticleSetInitialization> grid_initialization = utils::make_unique<InitSurveillanceAreaGrid>(surv_x, surv_y, num_particle_x, num_particle_y);
121 double tilde_q = 10.0f;
127 std::unique_ptr<PFPrediction> pf_prediction = utils::make_unique<DrawParticles>(std::move(wna));
134 std::unique_ptr<SimulatedStateModel> simulated_state_model = utils::make_unique<SimulatedStateModel>(std::move(target_model), initial_state, simulation_time);
137 simulated_state_model->enable_log(
"./",
"testSIS");
140 double sigma_x = 10.0;
141 double sigma_y = 10.0;
142 Eigen::MatrixXd R(2, 2);
143 R << std::pow(sigma_x, 2.0), 0.0,
144 0.0, std::pow(sigma_y, 2.0);
146 std::unique_ptr<MeasurementModel> simulated_linear_sensor = utils::make_unique<SimulatedLinearSensor>(std::move(simulated_state_model),
SimulatedLinearSensor::LinearMatrixComponent{ 4, std::vector<std::size_t>{ 0, 2 } }, R);
149 simulated_linear_sensor->enable_log(
"./",
"testSIS");
154 std::unique_ptr<LikelihoodModel> exp_likelihood = utils::make_unique<GaussianLikelihood>();
158 std::unique_ptr<PFCorrection> pf_correction = utils::make_unique<BootstrapCorrection>(std::move(simulated_linear_sensor), std::move(exp_likelihood));
163 std::unique_ptr<Resampling> resampling = utils::make_unique<Resampling>();
167 std::cout <<
"Constructing SIS particle filter..." << std::flush;
168 SISSimulation sis_pf(num_particle, state_size, simulation_time, std::move(grid_initialization), std::move(pf_prediction), std::move(pf_correction), std::move(resampling));
171 sis_pf.enable_log(
"./",
"testSIS");
173 std::cout <<
"done!" << std::endl;
177 std::cout <<
"Booting SIS particle filter..." << std::flush;
179 std::cout <<
"completed!" << std::endl;
184 std::cout <<
"Running SIS particle filter..." << std::flush;
186 std::cout <<
"waiting..." << std::flush;
189 std::cout <<
"completed!" << std::endl;