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
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 void outputVector(ofstream& out, aux::vector vec);
00069
00070 void outputMatrix(ofstream& out, aux::matrix mat);
00071
00072
00073
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
00082 MobileRobot robot(0.1, 5e-3);
00083
00084
00085 MobileRobotParticleFilterModel model(0.1, 5.0e-3);
00086 aux::GaussianPdf prior(model.suggestPrior());
00087 aux::DiracMixturePdf x0(prior, NUM_PARTICLES / size);
00088
00089
00090 ParticleFilter<unsigned int> filter(&model, x0);
00091
00092
00093 StratifiedParticleResampler resampler(NUM_PARTICLES);
00094
00095
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
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
00146 fmeas << t << '\t';
00147 outputVector(fmeas, meas);
00148 fmeas << endl;
00149
00150
00151 factual << t << '\t';
00152 outputVector(factual, actual);
00153 factual << endl;
00154
00155
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 }