Go to the documentation of this file.00001 #include <stdlib.h>
00002 #include <iostream>
00003 #include <fstream>
00004 #include "particlefilter.h"
00005
00006 using namespace std;
00007
00008 #define test(s) {cout << s << " " << flush;}
00009 #define testOk() {cout << "OK" << endl;}
00010
00011 struct Particle{
00012 double p;
00013 double w;
00014 inline operator double() const {return w;}
00015 inline void setWeight(double _w) {w=_w;}
00016 };
00017
00018 ostream& printParticles(ostream& os, const vector<Particle>& p)
00019 {
00020 for (vector<Particle>::const_iterator it=p.begin(); it!=p.end(); ++it) {
00021 os << it->p<< " " << (double)*it << endl;
00022 }
00023 return os;
00024 }
00025
00026 struct EvolutionModel{
00027 Particle evolve(const Particle& p){
00028 Particle pn(p);
00029 pn.p+=.5*(drand48()-.5);
00030 return pn;
00031 }
00032 };
00033
00034 struct QualificationModel{
00035 Particle evolve(const Particle& p){
00036 return p;
00037 }
00038 };
00039
00040 struct LikelyhoodModel{
00041 double likelyhood(const Particle& p) const{
00042 double v = 1./(0.1+10*(p.p-2)*(p.p-2))+0.5/(0.1+10*(p.p-8)*(p.p-8));
00043 return v;
00044 }
00045 };
00046
00047 int main (unsigned int argc, const char * const * argv){
00048 int nparticles=100;
00049 if (argc>1)
00050 nparticles=atoi(argv[1]);
00051 vector<Particle> particles(nparticles);
00052 LikelyhoodModel likelyhoodModel;
00053 uniform_resampler<Particle, double> resampler;
00054 auxiliary_evolver <Particle, double, QualificationModel, EvolutionModel, LikelyhoodModel> auxevolver;
00055 evolver <Particle, EvolutionModel> evolver;
00056
00057 for (vector<Particle>::iterator it=particles.begin(); it!=particles.end(); it++){
00058 it->w=1;
00059 it->p=10*(drand48());
00060 }
00061
00062 vector<Particle> sirparticles(particles);
00063 vector<Particle> auxparticles(particles);
00064
00065
00066 while (1){
00067 char buf[2];
00068 cin.getline(buf,2);
00069 vector<Particle> newgeneration;
00070
00071 cout << "# SIR step" << endl;
00072 evolver.evolve(sirparticles);
00073 for (vector<Particle>::iterator it=sirparticles.begin(); it!=sirparticles.end(); it++){
00074 it->setWeight(likelyhoodModel.likelyhood(*it));
00075 }
00076 ofstream os("sir.dat");
00077 printParticles(os, sirparticles);
00078 os.close();
00079 newgeneration=resampler.resample(sirparticles);
00080 sirparticles=newgeneration;
00081
00082 cout << "# AUX step" << endl;
00083 auxevolver.evolve(auxparticles);
00084 for (vector<Particle>::iterator it=auxparticles.begin(); it!=auxparticles.end(); it++){
00085 it->setWeight(likelyhoodModel.likelyhood(*it));
00086 }
00087 os.open("aux.dat");
00088 printParticles(os, auxparticles);
00089 os.close();
00090 newgeneration=resampler.resample(auxparticles);
00091 auxparticles=newgeneration;
00092 cout << "plot [0:10][0:10]\"sir.dat\" w impulses" << endl;
00093 cout << "replot 1./(0.1+10*(x-2)*(x-2))+0.5/(0.1+10*(x-8)*(x-8))" << endl;
00094
00095
00096 }
00097 }
00098