20 #include "../pdf/mcpdf.h" 24 #define MeasModel MeasurementModel 26 #define STATE_AND_MEAS_VAR_DIFFERENT 28 template <
typename SV,
typename MV>
29 ParticleFilter<SV,MV>::ParticleFilter(MCPdf<SV> * prior,
30 ConditionalPdf<SV,SV> * proposal,
32 double resamplethreshold,
34 : Filter<
SV,
MV>(prior)
36 , _sample(WeightedSample<
SV>(prior->DimensionGet()))
37 , _resampleScheme(resamplescheme)
44 this->
_post =
new MCPdf<SV>(prior->NumSamplesGet(),prior->DimensionGet());
49 bool ret = (
dynamic_cast<MCPdf<SV> *
>(this->
_post))->ListOfSamplesSet(prior->ListOfSamplesGet());
58 assert(!(resampleperiod == 0 && resamplethreshold == 0));
59 assert(!(resampleperiod != 0 && resamplethreshold != 0));
62 if (resampleperiod == 0)
73 template <
typename SV,
typename MV>
76 ConditionalPdf<SV,SV> * proposal,
78 double resamplethreshold,
90 bool ret = (
dynamic_cast<MCPdf<SV> *
>(this->
_post))->ListOfSamplesSet(prior->ListOfSamplesGet());
98 assert(!(resampleperiod == 0 && resamplethreshold == 0));
99 assert(!(resampleperiod != 0 && resamplethreshold != 0));
102 if (resampleperiod == 0)
115 template <
typename SV,
typename MV>
122 template <
typename SV,
typename MV>
132 template <
typename SV,
typename MV>
void 144 template <
typename SV,
typename MV>
bool 157 const SV& x_old =
_os_it->ValueGet();
165 #ifndef STATE_AND_MEAS_VAR_DIFFERENT 177 #ifndef STATE_AND_MEAS_VAR_DIFFERENT 200 template <
typename SV,
typename MV>
bool 216 const SV& x_new =
_ns_it->ValueGet();
217 const SV& x_old =
_os_it->ValueGet();
219 if (sysmodel == NULL)
236 #ifndef STATE_AND_MEAS_VAR_DIFFERENT 243 else weightfactor = 0;
248 #ifndef STATE_AND_MEAS_VAR_DIFFERENT 254 else weightfactor = 0;
271 template <
typename SV,
typename MV>
bool 275 bool resampling =
false;
276 double sum_sq_weigths = 0.0;
288 sum_sq_weigths += pow(
_ns_it->WeightGet(),2);
293 #ifdef __RESAMPLE_DEBUG__ 294 cout <<
"resampling now: " << this->
_timestep 295 <<
"\tN_eff: " << (1.0 / sum_sq_weigths) << endl;
296 #endif // __RESAMPLE_DEBUG__ 300 if (resampling ==
true)
307 template <
typename SV,
typename MV>
bool 317 template <
typename SV,
typename MV>
bool 328 if (sysmodel != NULL)
335 if (measmodel != NULL)
344 template <
typename SV,
typename MV>
bool 347 int NumSamples = (
dynamic_cast<MCPdf<SV> *
>(this->
_post))->NumSamplesGet();
349 #ifdef __PARTICLEFILTER_DEBUG__ 350 cout <<
"PARTICLEFILTER: resampling now" << endl;
354 cout <<
"PF: Old samples:\n";
355 cout <<
_ns_it->ValueGet() <<
_ns_it->WeightGet() << endl;
357 #endif // __PARTICLEFILTER_DEBUG 370 cerr <<
"Sampling method not supported" << endl;
375 #ifdef __PARTICLEFILTER_DEBUG__ 376 cout <<
"PARTICLEFILTER: after resampling" << endl;
380 cout <<
"PF: New samples:\n";
381 cout <<
_ns_it->ValueGet() <<
_ns_it->WeightGet() << endl;
383 #endif // __PARTICLEFILTER_DEBUG 389 template<
typename SV,
typename MV>
MCPdf<SV> *
Abstract class representing an interface for Bayesian Filters.
bool SystemWithoutInputs() const
Has the system inputs or not.
virtual MCPdf< StateVar > * PostGet()
Get Posterior density.
ParticleFilter(MCPdf< StateVar > *prior, ConditionalPdf< StateVar, StateVar > *proposal, int resampleperiod=0, double resamplethreshold=0, int resamplescheme=DEFAULT_RS)
Constructor.
double _resampleThreshold
Threshold used when dynamic resampling.
Filter(Pdf< StateVar > *prior)
Constructor.
Pdf< StateVar > * _post
Pointer to the Posterior Pdf.
Virtual Class representing all particle filters.
ConditionalPdf< StateVar, StateVar > * ProposalGet()
Get a pointer to the proposal density.
virtual bool DynamicResampleStep()
Resample if necessary.
virtual void ConditionalArgumentSet(unsigned int n_argument, const CondArg &argument)
Set the n-th argument of the list.
WeightedSample< StateVar > _sample
While updating use sample<StateVar>
bool _proposal_depends_on_meas
Proposal depends on last measurement?
vector< WeightedSample< StateVar > >::iterator _os_it
Iterator for old list of samples.
virtual Pdf< StateVar > * PostGet()
Get Posterior density.
Probability ProbabilityGet(const MeasVar &z, const StateVar &x, const StateVar &s)
Get the probability of a certain measurement.
virtual bool Resample()
Actual Resampling happens here;.
int _resampleScheme
Which resample algorithm (see top of particle.h for #defines)
Abstract Class representing conditional Pdfs P(x | ...)
vector< WeightedSample< StateVar > > _old_samples
While updating store list of old samples.
int _timestep
Represents the current timestep of the filter.
vector< WeightedSample< StateVar > > _new_samples
While updating store list of new samples.
virtual bool UpdateInternal(SystemModel< StateVar > *const sysmodel, const StateVar &u, MeasurementModel< MeasVar, StateVar > *const measmodel, const MeasVar &z, const StateVar &s)
Actual implementation of Update, varies along filters.
virtual Probability ProbabilityGet(const T &input) const
Get the probability of a certain argument.
Monte Carlo Pdf: Sample based implementation of Pdf.
virtual void ProposalSet(ConditionalPdf< StateVar, StateVar > *const cpdf)
Set the proposal density.
virtual bool ProposalStepInternal(SystemModel< StateVar > *const sysmodel, const StateVar &u, MeasurementModel< MeasVar, StateVar > *const measmodel, const MeasVar &z, const StateVar &s)
Proposal step.
virtual bool StaticResampleStep()
Resample if wanted.
virtual ~ParticleFilter()
Destructor.
bool _created_post
created own post
Probability ProbabilityGet(const T &x_k, const T &x_kminusone, const T &u)
Get the probability of arriving in a next state.
vector< WeightedSample< StateVar > >::iterator _ns_it
Iterator for new list of samples.
bool _dynamicResampling
Dynamic resampling or fixed period resampling?
virtual bool UpdateWeightsInternal(SystemModel< StateVar > *const sysmodel, const StateVar &u, MeasurementModel< MeasVar, StateVar > *const measmodel, const MeasVar &z, const StateVar &s)
Update Weights.
vector< Sample< StateVar > > _new_samples_unweighted
While resampling.
int _resamplePeriod
Number of timestep between resampling from the Posterior Pdf.
ConditionalPdf< StateVar, StateVar > * _proposal
Pointer to the Proposal Density.
Class representing a probability (a double between 0 and 1)
virtual bool SampleFrom(vector< Sample< T > > &list_samples, const unsigned int num_samples, int method=DEFAULT, void *args=NULL) const
Draw multiple samples from the Pdf (overloaded)
T & ValueGet()
Get the value of the Sample.
bool SystemWithoutSensorParams() const
Number of Conditional Arguments.