⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 auxiliaryparticlesmootherharness.cpp

📁 dysii是一款非常出色的滤波函数库
💻 CPP
字号:
#include "indii/ml/filter/ParticleSmoother.hpp"#include "indii/ml/filter/AuxiliaryParticleResampler.hpp"#include "indii/ml/aux/vector.hpp"#include "indii/ml/aux/matrix.hpp"#include "indii/ml/aux/Random.hpp"#include "MobileRobotParticleFilterModel.hpp"#include "MobileRobot.hpp"#include <math.h>#include <iostream>#include <fstream>#include <vector>#include <stack>#define SYSTEM_SIZE 5#define SYSTEM_NOISE_SIZE 2#define MEAS_SIZE 1#define MEAS_NOISE_SIZE 1#define ACTUAL_SIZE 3#define STEPS 250#define NUM_PARTICLES 400using namespace std;using namespace indii::ml::filter;namespace aux = indii::ml::aux;/** * @file AuxiliaryParticleSmootherHarness.cpp * * Basic test of ParticleSmoother with AuxiliaryParticleResampler. * * Results are output into files as follows: * * @section actualPS results/AuxiliaryParticleSmootherHarness_actual.out * * Actual state of the robot at each time. Columns are as follows: * * @li time * @li x coordinate * @li y coordinate * @li orientation (radians) * * @section measPS results/AuxiliaryParticleSmootherHarness_meas.out *  * Measurement at each time step. Columns are as follows: * * @li time * @li measurement * * @section filterPS results/AuxiliaryParticleSmootherHarness_filter.out * * Predicted state at each time step. Columns are as follows: * * @li time * @li mean x coordinate * @li mean y coordinate * @li mean orientation * @li The remaining columns give the covariance matrix between the above * state variables. * * @section smoothPS results/AuxiliaryParticleSmootherHarness_smooth.out * * Predicted state (smoothed) at each time step. Columns are as follows: * * @li time * @li mean x coordinate * @li mean y coordinate * @li mean orientation * @li The remaining columns give the covariance matrix between the above * state variables. * * Note that as the smoothing is performed in a backwards pass, this file has * entries in reverse time order. * * @section resultsPS Results * * Results are as follows: * * \image html AuxiliaryParticleSmootherHarness.png "Results" * \image latex AuxiliaryParticleSmootherHarness.eps "Results" */void outputVector(ofstream& out, aux::vector vec);void outputMatrix(ofstream& out, aux::matrix mat);/** * Run tests */int main(int argc, char* argv[]) {  boost::mpi::environment env(argc, argv);  boost::mpi::communicator world;  const int rank = world.rank();  const int size = world.size();  int i;  /* set up robot simulator */  MobileRobot robot(0.1, 5e-3);  /* initial state */  aux::vector mu(SYSTEM_SIZE);  aux::symmetric_matrix sigma(SYSTEM_SIZE);  mu.clear();  mu(0) = -1.0;  mu(1) = 1.0;  mu(2) = 0.8;  mu(3) = 0.1;  mu(4) = 5e-3;  sigma.clear();  sigma(0,0) = 1.0;  sigma(1,1) = 1.0;  sigma(2,2) = 0.01;  sigma(3,3) = 1e-6;  sigma(4,4) = 1e-6;  aux::GaussianPdf x0_tmp(mu, sigma);  aux::DiracMixturePdf x0(SYSTEM_SIZE);  for (i = 0; i < NUM_PARTICLES / size; i++) {    x0.addComponent(x0_tmp.sample());  }  /* system noise */  mu.resize(SYSTEM_NOISE_SIZE, false);  sigma.resize(SYSTEM_NOISE_SIZE, false);  mu.clear();  sigma.clear();  sigma(0,0) = pow(0.01, 2.0);  sigma(1,1) = pow(0.01, 2.0);  aux::GaussianPdf w(mu, sigma);  /* resampling noise */  sigma(0,0) = pow(0.001, 2.0);  sigma(1,1) = pow(0.001, 2.0);  aux::GaussianPdf r(mu, sigma);  /* measurement noise */  mu.resize(MEAS_NOISE_SIZE, false);  sigma.resize(MEAS_NOISE_SIZE, false);  mu.clear();  sigma.clear();  sigma(0,0) = pow(0.05,2.0);  aux::GaussianPdf v(mu, sigma);  /* define model */  MobileRobotParticleFilterModel model(w, v, r);  /* create filter */  ParticleSmoother<unsigned int> smoother(&model, x0);  /* create resampler */  AuxiliaryParticleResampler<unsigned int> resampler(&smoother, NUM_PARTICLES);  /* estimate and output results */  stack<aux::DiracMixturePdf> filteredStates;  aux::vector meas(MEAS_SIZE);  aux::vector actual(ACTUAL_SIZE);  aux::DiracMixturePdf pred(SYSTEM_SIZE);  unsigned int t = 0;  ofstream fmeas("results/AuxiliaryParticleSmootherHarness_meas.out");  ofstream factual("results/AuxiliaryParticleSmootherHarness_actual.out");  ofstream ffilter("results/AuxiliaryParticleSmootherHarness_filter.out");  ofstream fsmooth("results/AuxiliaryParticleSmootherHarness_smooth.out");  mu.resize(SYSTEM_SIZE);  sigma.resize(SYSTEM_SIZE);  /* output initial state */  pred = smoother.getFilteredState();  actual = robot.getState();  mu = pred.getDistributedExpectation();  sigma = pred.getDistributedCovariance();  if (rank == 0) {    cerr << t << ' ';    factual << t << '\t';    outputVector(factual, actual);    factual << endl;    ffilter << t << '\t';    outputVector(ffilter, mu);    ffilter << '\t';    outputMatrix(ffilter, sigma);    ffilter << endl;  }  for (t = 1; t <= STEPS; t++) {    if (rank == 0) {      robot.move();      meas = robot.measure();    }    boost::mpi::broadcast(world, meas, 0);    resampler.setLookAhead(t, meas);    smoother.resample(&resampler);    smoother.filter(t, meas);    pred = smoother.getFilteredState();    actual = robot.getState();    mu = pred.getDistributedExpectation();    sigma = pred.getDistributedCovariance();    filteredStates.push(pred);    if (rank == 0) {      cerr << t << ' ';      /* output measurement */      fmeas << t << '\t';      outputVector(fmeas, meas);      fmeas << endl;      /* output actual state */      factual << t << '\t';      outputVector(factual, actual);      factual << endl;      /* output filtered state */      ffilter << t << '\t';      outputVector(ffilter, mu);      ffilter << '\t';      outputMatrix(ffilter, sigma);      ffilter << endl;    }  }  /* smooth */  t--;  pred = smoother.getSmoothedState();  mu = pred.getDistributedExpectation();  sigma = pred.getDistributedCovariance();  if (rank == 0) {    cerr << t << ' ';    fsmooth << t << '\t';    outputVector(fsmooth, mu);    fsmooth << '\t';    outputMatrix(fsmooth, sigma);    fsmooth << endl;  }  filteredStates.pop();  for (t = STEPS - 1; t >= 1; t--) {    smoother.smooth(t, filteredStates.top());    filteredStates.pop();    pred = smoother.getSmoothedState();    mu = pred.getDistributedExpectation();    sigma = pred.getDistributedCovariance();    if (rank == 0) {      cerr << t << ' ';      /* output smoothed state */      fsmooth << t << '\t';      outputVector(fsmooth, mu);      fsmooth << '\t';      outputMatrix(fsmooth, sigma);      fsmooth << endl;    }  }  fmeas.close();  factual.close();  ffilter.close();  fsmooth.close();  return 0;}void outputVector(ofstream& out, aux::vector vec) {  aux::vector::iterator iter, end;  iter = vec.begin();  end = vec.end();  while (iter != end) {    out << *iter;    iter++;    if (iter != end) {      out << '\t';    }  }}void outputMatrix(ofstream& out, aux::matrix mat) {  unsigned int i, j;  for (j = 0; j < mat.size2(); j++) {    for (i = 0; i < mat.size1(); i++) {      out << mat(i,j);      if (i != mat.size1() - 1 || j != mat.size2() - 1) {	out << '\t';      }    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -