particlefilter.cpp
Go to the documentation of this file.
00001 std::vector<unsigned int> sistematicResampler<State,Numeric>::resample(const vector<Particle>& particles) const{
00002         Numeric cweight=0;
00003 
00004         //compute the cumulative weights
00005         unsigned int n=0;
00006         for (vector<Particle>::const_iterator it=particles.begin(); it!=particles.end(); ++it){
00007                 cweight+=it->weight;
00008                 n++;
00009         }
00010 
00011         //compute the interval
00012         Numeric interval=cweight/n;
00013 
00014         //compute the initial target weight
00015         Numeric target=
00016         //compute the resampled indexes
00017 
00018         cweight=0;
00019         std::vector<int> indexes(n);
00020         n=0;
00021         unsigned int i=0;
00022         for (vector<Particle>::const_iterator it=particles.begin(); it!=particles.end(); ++it, ++i){
00023                 cweight+=it->weight;
00024                 while(cweight>target){
00025                         indexes[n++]=i;
00026                         target+=interval;
00027                 }
00028         }
00029         return indexes;
00030         }
00031 
00032 template <class Numeric>
00033 std::vector<unsigned int> indexResampler<Numeric>::resample(const vector<Numeric> >& weights) const{
00034         Numeric cweight=0;
00035 
00036         //compute the cumulative weights
00037         unsigned int n=0;
00038         for (vector<Numeric>::const_iterator it=weights.begin(); it!=weights.end(); ++it){
00039                 cweight+=*it;
00040                 n++;
00041         }
00042 
00043         //compute the interval
00044         Numeric interval=cweight/n;
00045 
00046         //compute the initial target weight
00047         Numeric target=
00048         //compute the resampled indexes
00049 
00050         cweight=0;
00051         std::vector<int> indexes(n);
00052         n=0;
00053         unsigned int i=0;
00054         for (vector<Numeric>::const_iterator it=weights.begin(); it!=weights.end(); ++it, ++i){
00055                 cweight+=it->weight;
00056                 while(cweight>target){
00057                         indexes[n++]=i;
00058                         target+=interval;
00059                 }
00060         }
00061         return indexes;
00062 }
00063 
00064 /*
00065 
00066 The following are patterns for the evolution and the observation classes
00067 The user should implement classes having the specified meaning
00068 
00069 template <class State, class Numeric, class Observation>
00070 struct observer{
00071         Observation& observation
00072         Numeric observe(const class State&) const;
00073 };
00074 
00075 template <class State, class Numeric, class Input>
00076 struct evolver{
00077         Input& input;
00078         State& evolve(const State& s);
00079 };
00080 */
00081 
00082 template <class State, class Numeric, class EvolutionModel>
00083 void evolver<State, Numeric, EvolutionModel>::evolve(std::vector<evolver::Particle>& particles) const{
00084         for (std::vector<Particle>::const_iterator it=particles.begin(); it!=particles.end(); ++it)
00085                 *it=evolutionModel.evolve(*it);
00086 }
00087 
00088 void evolver<State, Numeric, EvolutionModel>::evolve(std::vector<evolver::Particle>& dest, const std::vector<evolver::Particle>& src) const{
00089         dest.clear();
00090         for (std::vector<Particle>::const_iterator it=src.begin(); it!=src.end(); ++it)
00091                 dest.push_back(evolutionModel.evolve(*it));
00092 }
00093 
00094 template <class State, class Numeric, class QualificationModel, class EvolutionModel, class LikelyhoodModel>
00095 struct auxiliaryEvolver{
00096         typedef particle<State, Numeric> Particle;
00097 
00098         EvolutionModel evolutionModel;
00099         QualificationModel qualificationModel;
00100         LikelyhoodModel likelyhoodModel;
00101         indexResampler<Numeric> resampler;
00102 
00103 void auxiliaryEvolver<State, Numeric, QualificationModel, EvolutionModel, LikelyhoodModel>::evolve
00104   (std::vector<auxiliaryEvolver::Particle>&particles){
00105         std::vector<Numeric> observationWeights(particles.size());
00106         unsigned int i=0;
00107         for (std::vector<Particle>::const_iterator it=particles.begin(); it!=particles.end(); ++it, i++){
00108                 observationWeights[i]=likelyhoodModel.likelyhood(qualificationModel.evolve(*it));
00109         }
00110         std::vector<unsigned int> indexes(indexResampler.resample(observationWeights));
00111         for (std::vector<unsigned int>::const_iterator it=indexes.begin(); it!=indexes.end(); it++){
00112                 Particle & particle=particles[*it];
00113                 particle=evolutionModel.evolve(particle);
00114                 particle.weight*=lykelyhoodModel.lykelyhood(particle)/observationWeights[*it];
00115         }
00116 }
00117 
00118 void auxiliaryEvolver<State, Numeric, QualificationModel, EvolutionModel, LikelyhoodModel>::evolve
00119   (std::vector<Particle>& dest, const std::vector<Particle>& src){
00120         dest.clear();
00121         std::vector<Numeric> observationWeights(particles.size());
00122         unsigned int i=0;
00123         for (std::vector<Particle>::const_iterator it=src.begin(); it!=src.end(); ++it, i++){
00124                 observationWeights[i]=likelyhoodModel.likelyhood(qualificationModel.evolve(*it));
00125         }
00126         std::vector<unsigned int> indexes(indexResampler.resample(observationWeights));
00127         for (std::vector<unsigned int>::const_iterator it=indexes.begin(); it!=indexes.end(); it++){
00128                 Particle & particle=src[*it];
00129                 dest.push_back(evolutionModel.evolve(particle));
00130                 dest.back().weight*=likelyhoodModel.lykelyhood(particle)/observationWeights[*it];
00131         }
00132         return dest();
00133 }


openslam_gmapping
Author(s): Giorgio Grisetti, Cyrill Stachniss, Wolfram Burgard
autogenerated on Fri Aug 28 2015 11:56:21