ParticleFilterHarness.cpp

Go to the documentation of this file.
00001 #include "indii/ml/filter/ParticleFilter.hpp"
00002 #include "indii/ml/filter/StratifiedParticleResampler.hpp"
00003 #include "indii/ml/aux/DiracMixturePdf.hpp"
00004 #include "indii/ml/aux/vector.hpp"
00005 #include "indii/ml/aux/matrix.hpp"
00006 
00007 #include "MobileRobotParticleFilterModel.hpp"
00008 #include "MobileRobot.hpp"
00009 
00010 #include <math.h>
00011 #include <iostream>
00012 #include <fstream>
00013 #include <vector>
00014 
00015 #define SYSTEM_SIZE 2
00016 #define MEAS_SIZE 1
00017 #define ACTUAL_SIZE 3
00018 #define STEPS 250
00019 #define NUM_PARTICLES 1000
00020 
00021 using namespace std;
00022 using namespace indii::ml::filter;
00023 
00024 namespace aux = indii::ml::aux;
00025 
00026 /**
00027  * @file ParticleFilterHarness.cpp
00028  *
00029  * Basic test of ParticleFilter.
00030  *
00031  * Results are output into files as follows:
00032  *
00033  * @section actualPF results/ParticleFilterHarness_actual.out
00034  *
00035  * Actual state of the robot at each time. Columns are as follows:
00036  *
00037  * @li time
00038  * @li x coordinate
00039  * @li y coordinate
00040  * @li orientation (radians)
00041  *
00042  * @section measPF results/ParticleFilterHarness_meas.out
00043  * 
00044  * Measurement at each time step. Columns are as follows:
00045  *
00046  * @li time
00047  * @li measurement
00048  *
00049  * @section predPF results/ParticleFilterHarness_filter.out
00050  *
00051  * Filtered state at each time step. Columns are as follows:
00052  *
00053  * @li time
00054  * @li mean x coordinate
00055  * @li mean y coordinate
00056  * @li mean orientation
00057  * @li The remaining columns give the covariance matrix between the above
00058  * state variables.
00059  *
00060  * @section resultsPF Results
00061  *
00062  * Results are as follows:
00063  *
00064  * \image html ParticleFilterHarness.png "Results"
00065  * \image latex ParticleFilterHarness.eps "Results"
00066  */
00067 
00068 void outputVector(ofstream& out, aux::vector vec);
00069 
00070 void outputMatrix(ofstream& out, aux::matrix mat);
00071 
00072 /**
00073  * Run tests
00074  */
00075 int main(int argc, char* argv[]) {
00076   boost::mpi::environment env(argc, argv);
00077   boost::mpi::communicator world;
00078   const unsigned int rank = world.rank();
00079   const unsigned int size = world.size();
00080 
00081   /* set up robot simulator */
00082   MobileRobot robot(0.1, 5e-3);
00083   
00084   /* define model */
00085   MobileRobotParticleFilterModel model(0.1, 5.0e-3);
00086   aux::GaussianPdf prior(model.suggestPrior());
00087   aux::DiracMixturePdf x0(prior, NUM_PARTICLES / size);
00088 
00089   /* create filter */
00090   ParticleFilter<unsigned int> filter(&model, x0);
00091 
00092   /* create resamplers */
00093   StratifiedParticleResampler resampler(NUM_PARTICLES);
00094 
00095   /* estimate and output results */
00096   aux::vector meas(MEAS_SIZE);
00097   aux::vector actual(ACTUAL_SIZE);
00098   aux::DiracMixturePdf pred(SYSTEM_SIZE);
00099   unsigned int t = 0;
00100 
00101   ofstream fmeas("results/ParticleFilterHarness_meas.out");
00102   ofstream factual("results/ParticleFilterHarness_actual.out");
00103   ofstream fpred("results/ParticleFilterHarness_filter.out");
00104 
00105   aux::vector mu(SYSTEM_SIZE);
00106   aux::symmetric_matrix sigma(SYSTEM_SIZE);
00107 
00108   /* output initial state */
00109   pred = filter.getFilteredState();
00110   actual = robot.getState();
00111   mu = pred.getDistributedExpectation();
00112   sigma = pred.getDistributedCovariance();
00113 
00114   if (rank == 0) {
00115     cerr << t << ' ';
00116 
00117     factual << t << '\t';
00118     outputVector(factual, actual);
00119     factual << endl;
00120 
00121     fpred << t << '\t';
00122     outputVector(fpred, mu);
00123     fpred << '\t';
00124     outputMatrix(fpred, sigma);
00125     fpred << endl;
00126   }
00127 
00128   for (t = 1; t <= STEPS; t++) {
00129     if (rank == 0) {
00130       robot.move();
00131       meas = robot.measure();
00132     }
00133     boost::mpi::broadcast(world, meas, 0);
00134 
00135     filter.resample(&resampler);
00136     filter.filter(t, meas);
00137     pred = filter.getFilteredState();
00138     actual = robot.getState();
00139     mu = pred.getDistributedExpectation();
00140     sigma = pred.getDistributedCovariance();
00141 
00142     if (rank == 0) {
00143       cerr << t << ' ';
00144 
00145       /* output measurement */
00146       fmeas << t << '\t';
00147       outputVector(fmeas, meas);
00148       fmeas << endl;
00149 
00150       /* output actual state */
00151       factual << t << '\t';
00152       outputVector(factual, actual);
00153       factual << endl;
00154 
00155       /* output filtered state */
00156       fpred << t << '\t';
00157       outputVector(fpred, mu);
00158       fpred << '\t';
00159       outputMatrix(fpred, sigma);
00160       fpred << endl;
00161     }
00162   }
00163 
00164   fmeas.close();
00165   factual.close();
00166   fpred.close();
00167 
00168   return 0;
00169 }
00170 
00171 void outputVector(ofstream& out, aux::vector vec) {
00172   aux::vector::iterator iter, end;
00173   iter = vec.begin();
00174   end = vec.end();
00175   while (iter != end) {
00176     out << *iter;
00177     iter++;
00178     if (iter != end) {
00179       out << '\t';
00180     }
00181   }
00182 }
00183 
00184 void outputMatrix(ofstream& out, aux::matrix mat) {
00185   unsigned int i, j;
00186   for (j = 0; j < mat.size2(); j++) {
00187     for (i = 0; i < mat.size1(); i++) {
00188       out << mat(i,j);
00189       if (i != mat.size1() - 1 || j != mat.size2() - 1) {
00190   out << '\t';
00191       }
00192     }
00193   }
00194 }

Generated on Wed Dec 17 15:05:50 2008 for dysii Filtering Test Suite by  doxygen 1.5.3