20 #include "../pdf/mixture.h" 24 #define MeasModel MeasurementModel 26 #define STATE_AND_MEAS_VAR_DIFFERENT 28 template <
typename SV,
typename MV>
29 MixtureParticleFilter<SV,MV>::MixtureParticleFilter(Mixture<SV> * prior,
30 ConditionalPdf<SV,SV> * proposal,
32 double resamplethreshold,
34 int maintainMixturePeriod)
35 : Filter<
SV,
MV>(prior)
37 , _sample(WeightedSample<
SV>(prior->DimensionGet()))
38 , _resampleScheme(resamplescheme)
40 , _newMixtureWeights(prior->NumComponentsGet())
41 , _sumWeights(prior->NumComponentsGet())
42 , _old_samplesVec(prior->NumComponentsGet())
43 , _new_samplesVec(prior->NumComponentsGet())
44 , _new_samples_unweightedVec(prior->NumComponentsGet())
45 , _maintainMixturePeriod(maintainMixturePeriod)
55 for(
int i = 0 ; i< dynamic_cast<Mixture<SV> *>(this->
_post)->NumComponentsGet(); i++)
57 _old_samplesVec[i] = (
dynamic_cast<MCPdf<SV> *
>(prior->ComponentGet(i))->ListOfSamplesGet());
64 assert(!(resampleperiod == 0 && resamplethreshold == 0));
65 assert(!(resampleperiod != 0 && resamplethreshold != 0));
68 if (resampleperiod == 0)
79 template <
typename SV,
typename MV>
82 ConditionalPdf<SV,SV> * proposal,
84 double resamplethreshold,
86 int maintainMixturePeriod)
103 bool ret = (
dynamic_cast<MCPdf<SV> *
>(this->
_post))->ListOfSamplesSet(prior->ListOfSamplesGet());
105 for(
int i =0 ; i < prior.NumComponentsGet() ; i++)
107 bool ret = (
dynamic_cast<MCPdf<SV> *
>(this->
_post->ComponentGet(i)))->ListOfSamplesSet(prior->ComponentGet(i)->ListOfSamplesGet());
111 for(
int i =0 ; i < prior.NumComponentsGet() ; i++)
118 assert(!(resampleperiod == 0 && resamplethreshold == 0));
119 assert(!(resampleperiod != 0 && resamplethreshold != 0));
122 if (resampleperiod == 0)
135 template <
typename SV,
typename MV>
142 template <
typename SV,
typename MV>
153 template <
typename SV,
typename MV>
void 163 template <
typename SV,
typename MV>
void 176 template <
typename SV,
typename MV>
bool 184 for(
int i = 0 ; i< dynamic_cast<Mixture<SV> *>(this->
_post)->NumComponentsGet(); i++)
192 template <
typename SV,
typename MV>
bool 205 const SV& x_old =
_os_it->ValueGet();
213 #ifndef STATE_AND_MEAS_VAR_DIFFERENT 225 #ifndef STATE_AND_MEAS_VAR_DIFFERENT 247 template <
typename SV,
typename MV>
bool 256 for(
int i = 0 ; i< dynamic_cast<Mixture<SV> *>(this->
_post)->NumComponentsGet(); i++)
260 for(
int i = 0 ; i< dynamic_cast<Mixture<SV> *>(this->
_post)->NumComponentsGet(); i++)
274 template <
typename SV,
typename MV>
bool 281 if(component < 0 || component >
dynamic_cast<Mixture<SV> *
>(this->
_post)->NumComponentsGet())
283 cerr<<
"MixtureParticleFilter::UpdateWeightsInternalOne called with invalid component number " << endl;
295 const SV& x_new =
_ns_it->ValueGet();
296 const SV& x_old =
_os_it->ValueGet();
298 if (sysmodel == NULL)
317 #ifndef STATE_AND_MEAS_VAR_DIFFERENT 324 else weightfactor = 0;
329 #ifndef STATE_AND_MEAS_VAR_DIFFERENT 335 else weightfactor = 0;
343 double new_weight =
_ns_it->WeightGet() * weightfactor;
344 _ns_it->WeightSet(new_weight);
355 template <
typename SV,
typename MV>
bool 360 for(
int i = 0 ; i< dynamic_cast<Mixture<SV> *>(this->
_post)->NumComponentsGet(); i++)
367 template <
typename SV,
typename MV>
bool 371 bool resampling =
false;
372 double sum_sq_weigths = 0.0;
384 sum_sq_weigths += pow(
_ns_it->WeightGet(),2);
389 #ifdef __RESAMPLE_DEBUG__ 390 cout <<
"resampling now: " << this->
_timestep 391 <<
"\tN_eff: " << (1.0 / sum_sq_weigths) << endl;
392 #endif // __RESAMPLE_DEBUG__ 396 if (resampling ==
true)
403 template <
typename SV,
typename MV>
bool 413 template <
typename SV,
typename MV>
bool 424 if (sysmodel != NULL)
431 if (measmodel != NULL)
444 template <
typename SV,
typename MV>
bool 449 for(
int i = 0 ; i< dynamic_cast<Mixture<SV> *>(this->
_post)->NumComponentsGet(); i++)
456 template <
typename SV,
typename MV>
bool 459 int NumSamples = (
dynamic_cast<MCPdf<SV> *
>(
dynamic_cast<Mixture<SV> *
>(this->
_post)->ComponentGet(component)))->NumSamplesGet();
461 #ifdef __MixtureParticleFilter_DEBUG__ 462 cout <<
"MixtureParticleFilter: resampling now" << endl;
466 cout <<
"PF: Old samples:\n";
467 cout <<
_ns_it->ValueGet() <<
_ns_it->WeightGet() << endl;
469 #endif // __MixtureParticleFilter_DEBUG 482 cerr <<
"Sampling method not supported" << endl;
487 #ifdef __MixtureParticleFilter_DEBUG__ 488 cout <<
"MixtureParticleFilter: after resampling" << endl;
492 cout <<
"PF: New samples:\n";
493 cout <<
_ns_it->ValueGet() <<
_ns_it->WeightGet() << endl;
495 #endif // __MixtureParticleFilter_DEBUG 507 template <
typename SV,
typename MV>
bool 516 template <
typename SV,
typename MV>
bool virtual ~MixtureParticleFilter()
Destructor.
Abstract class representing an interface for Bayesian Filters.
bool SystemWithoutInputs() const
Has the system inputs or not.
vector< vector< WeightedSample< StateVar > > > _new_samplesVec
While updating store list of new samples.
Virtual Class representing all Mixture particle filters.
double _resampleThreshold
Threshold used when dynamic resampling.
virtual bool ProposalStepInternal(SystemModel< StateVar > *const sysmodel, const StateVar &u, MeasurementModel< MeasVar, StateVar > *const measmodel, const MeasVar &z, const StateVar &s)
Proposal step.
vector< WeightedSample< StateVar > >::iterator _os_it
Iterator for old list of samples.
ConditionalPdf< StateVar, StateVar > * _proposal
Pointer to the Proposal Density.
virtual void Reset(Mixture< StateVar > *prior)
Reset Filter.
int _resampleScheme
Which resample algorithm (see top of particle.h for #defines)
int _maintainMixturePeriod
Number of timestep between mixture maintainance of the Posterior.
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.
WeightedSample< StateVar > _sample
While updating use sample<StateVar>
vector< vector< Sample< StateVar > > > _new_samples_unweightedVec
While resampling.
virtual Mixture< StateVar > * PostGet()
Get Posterior density.
Filter(Pdf< StateVar > *prior)
Constructor.
ConditionalPdf< StateVar, StateVar > * ProposalGet()
Get a pointer to the proposal density.
Pdf< StateVar > * _post
Pointer to the Posterior Pdf.
MixtureParticleFilter(Mixture< StateVar > *prior, ConditionalPdf< StateVar, StateVar > *proposal, int resampleperiod=0, double resamplethreshold=0, int resamplescheme=DEFAULT_RS, int maintainMixturePeriod=1)
Constructor.
virtual bool UpdateWeightsInternal(SystemModel< StateVar > *const sysmodel, const StateVar &u, MeasurementModel< MeasVar, StateVar > *const measmodel, const MeasVar &z, const StateVar &s)
Update Weights.
virtual void ConditionalArgumentSet(unsigned int n_argument, const CondArg &argument)
Set the n-th argument of the list.
vector< WeightedSample< StateVar > >::iterator _ns_it
Iterator for new list of samples.
virtual bool Resample()
Actual Resampling happens here;.
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 MaintainMixture()
Actual mixture maintainance happens here;.
virtual void ProposalSet(ConditionalPdf< StateVar, StateVar > *const cpdf)
Set the proposal density.
virtual bool ResampleOne(int component)
Actual Resampling for one component;.
Abstract Class representing conditional Pdfs P(x | ...)
Class representing a mixture of PDFs, the mixture can contain different.
int _timestep
Represents the current timestep of the filter.
virtual Probability ProbabilityGet(const T &input) const
Get the probability of a certain argument.
Monte Carlo Pdf: Sample based implementation of Pdf.
virtual bool UpdateWeightsInternalOne(int component, SystemModel< StateVar > *const sysmodel, const StateVar &u, MeasurementModel< MeasVar, StateVar > *const measmodel, const MeasVar &z, const StateVar &s)
Update Weights for one component.
virtual bool MaintainMixtureStep()
Maintain Mixture if wanted.
virtual bool ProposalStepInternalOne(int component, SystemModel< StateVar > *const sysmodel, const StateVar &u, MeasurementModel< MeasVar, StateVar > *const measmodel, const MeasVar &z, const StateVar &s)
Proposal step for one component.
virtual bool DynamicResampleStepOne(int component)
Resampling for one component.
Probability ProbabilityGet(const T &x_k, const T &x_kminusone, const T &u)
Get the probability of arriving in a next state.
virtual bool DynamicResampleStep()
Resample if necessary.
virtual bool StaticResampleStep()
Resample if wanted.
virtual Mixture * Clone() const
Clone function.
bool _proposal_depends_on_meas
Proposal depends on last measurement?
vector< Probability > _newMixtureWeights
Vector containing the new mixture weights during update step.
Class representing a probability (a double between 0 and 1)
vector< Probability > _sumWeights
Vector containing the sum of weights during update step.
Pdf< T > * ComponentGet(unsigned int componentNumber) const
Get the pointer to the component pdf of component "componentNumber".
Pdf< StateVar > * _prior
prior Pdf
vector< vector< WeightedSample< StateVar > > > _old_samplesVec
While updating store list of old samples.
virtual Pdf< T > * Clone() const =0
Pure virtual clone function.
int _resamplePeriod
Number of timestep between resampling from the Posterior Pdf.
bool _created_post
created own post
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.
bool _dynamicResampling
Dynamic resampling or fixed period resampling?