26 #include "Condensation.hh"
28 #include <Base/Common/W32Compat.hh>
32 #include <Base/Debug/TimeMeasure.hh>
41 defaultInitFraction_=0.1;
42 importanceFraction_=0.0;
43 _doSecondOrderARP=
true;
45 NewDebugLevel(
"D_COND_INIT");
46 NewDebugLevel(
"D_COND_PROCESS");
47 NewDebugLevel(
"D_COND_WEIGHTS");
48 NewDebugLevel(
"D_COND_DEBUG");
49 NewDebugLevel(
"D_COND_ERROR");
50 NewDebugLevel(
"D_COND_MEANVAR");
55 if (_randomArray)
delete[] _randomArray;
64 _randomArray=
new double[N];
66 samplePosNew_.resize(N);
67 samplePosOld_.resize(N);
68 samplePosTminus2_.resize(N);
69 indexTminus2_.resize(N);
70 oldIndices_.resize(N);
71 sampleWeights_.resize(N);
72 sampleWeights_.assign(N,1);
73 cumulProbArray_.resize(N);
74 sampleImportanceWeights_.resize(N);
75 cumulProbArrayImportance_.resize(N);
76 correctionOfImportanceWeights_.resize(N);
77 correctionOfImportanceWeights_.assign(N,1);
80 BIASERR(
"Error at Init: stateDim_==0, set this at constructor");
85 diffusionSigma_.newsize(stateDim_);
86 diffusionSigma_.Set(0);
87 processFirstOrderScale_.newsize(stateDim_);
88 processFirstOrderScale_.Set(0);
89 processSecondOrderScale_.newsize(stateDim_);
90 processSecondOrderScale_.Set(0);
91 processMean_.newsize(stateDim_);
93 offSet_.newsize(stateDim_);
95 mean_.newsize(stateDim_);
97 meanTminus1_.newsize(stateDim_);
99 BCDOUT(D_COND_INIT,
"allocating memory for sample vectors\n");
102 BCDOUT(D_COND_INIT,
"InitModelDefaults() passed.\n");
105 for (
unsigned int n=0;n<Nsamples_;n++) {
106 samplePosNew_[n].newsize(stateDim_);
107 samplePosOld_[n].newsize(stateDim_);
108 samplePosTminus2_[n].newsize(stateDim_);
119 BCDOUT(D_COND_INIT,
"Initial Positions set.\n");
120 InitPriorPositions(Nsamples_);
121 samplePosOld_ = samplePosNew_;
122 samplePosTminus2_ = samplePosOld_;
124 for (
unsigned int n=0; n<Nsamples_; n++) {
125 BCDOUT(D_COND_INIT,
"prior position of sample "<<n<<
": "
126 <<samplePosOld_[n]<<endl);
127 sumOfWeights_ += sampleWeights_[n];
128 cumulProbArray_[n] = sumOfWeights_;
129 indexTminus2_[n] = n;
131 BCDOUT(D_COND_INIT,
"init done\n");
134 cerr <<
"Condensation::InitPrior() took "<<timer.
GetRealTime()<<
" us"<<endl;
149 unsigned int sampleIndex=0;
152 unsigned int initSamples=(
unsigned int)((
double)Nsamples_*
153 (defaultInitFraction_));
156 unsigned int importanceSamples=(
unsigned int)((
double)Nsamples_*
160 InitPriorPositions(initSamples);
164 cerr <<
"Condensation::PredictNewSamplePositions() pick prior samples took "<<timer.
GetRealTime()<<
" us"<<endl;
169 if (importanceSamples!=initSamples && importanceSamples!=0) {
171 EvaluateImportanceWeights();
173 sumOfImportanceWeights_ = 0;
174 for (
unsigned int n=0; n<Nsamples_; n++) {
175 BCDOUT(D_COND_WEIGHTS,
"importance weight for sample "<<n<<
": "
176 <<sampleImportanceWeights_[n]<<endl);
177 sumOfImportanceWeights_ += sampleImportanceWeights_[n];
178 cumulProbArrayImportance_[n] = sumOfImportanceWeights_;
180 for (
unsigned int i=initSamples; i<importanceSamples; i++) {
181 BCDOUT(D_COND_PROCESS,
"importance sample with index:"
182 <<sampleIndex<<endl);
183 sampleIndex = PickImportanceSample_();
185 oldIndices_[i]=sampleIndex;
186 SecondOrderARP_(sampleIndex, samplePosNew_[i]);
187 correctionOfImportanceWeights_[i]=
188 sampleWeights_[sampleIndex] /
189 (sampleImportanceWeights_[sampleIndex]+1E-9);
190 BCDOUT(D_COND_PROCESS,
"adding diffusion for sampleIndex:"
191 <<sampleIndex<<endl);
197 cerr <<
"Condensation::PredictNewSamplePositions() pick importance samples took "<<timer.
GetRealTime()<<
" us"<<endl;
228 for (
unsigned int n=importanceSamples; n<Nsamples_; n++) {
229 sampleIndex = PickOneSample_();
230 BCDOUT(D_COND_PROCESS,
"predicting sample with index:"<<sampleIndex<<endl);
231 oldIndices_[n]=sampleIndex;
233 BCDOUT(D_COND_DEBUG,
"Nsamples in SecondOrderARP: "<<n<<
" SampleIndex: "
234 <<sampleIndex<<endl);
235 SecondOrderARP_(sampleIndex, samplePosNew_[n]);
237 indexTminus2_=oldIndices_;
241 cerr <<
"Condensation::PredictNewSamplePositions() pick standard samples took "<<timer.
GetRealTime()<<
" us"<<endl;
253 cerr <<
"Condensation::PredictNewSamplePositions() took "<<timer.
GetRealTime()<<
" us"<<endl;
262 if (fraction<0) defaultInitFraction_=0;
263 else if (fraction>1) defaultInitFraction_=1;
264 else defaultInitFraction_=fraction;
270 if (fraction<0) importanceFraction_=0;
271 else if (fraction>1) importanceFraction_=1;
272 else importanceFraction_=fraction;
278 const unsigned StartIndex=
279 (
unsigned int)((
double)Nsamples_*defaultInitFraction_);
281 std::vector<double> r;
282 r.reserve(Nsamples_-StartIndex);
283 for (
unsigned int d=0; d<stateDim_; d++) {
284 random_.GetNormalDistributed(0.0, diffusionSigma_[d],
285 Nsamples_-StartIndex, r);
286 for (
unsigned i=StartIndex; i<Nsamples_; i++){
287 samplePosNew_[i][d] += r[i-StartIndex];
294 if (offSet_.size()>0)
295 for (
unsigned int n=0;n<Nsamples_;n++) {
296 samplePosNew_[n] += offSet_;
297 samplePosOld_[n] += offSet_;
310 EvaluateObservationDensities();
314 std::cout<<
"Condensation::CalculateBaseWeights_() Eavl took "<<timer.
GetRealTime()<<
" us"<<std::endl;
319 for (
unsigned int n=0; n<Nsamples_; n++) {
320 BCDOUT(D_COND_WEIGHTS,
"weight for sample "<<n<<
": "<<sampleWeights_[n]<<endl);
321 cumul_total += sampleWeights_[n];
322 cumulProbArray_[n] = cumul_total;
324 sumOfWeights_ = cumul_total;
328 std::cerr <<
"Condensation::CalculateBaseWeights_() took "<<timer.
GetRealTime()<<
" us"<<std::endl;
340 BCDOUT(D_COND_PROCESS,
"Calculating Weights\n");
341 CalculateBaseWeights_();
345 std::cerr <<
"Condensation::Process() CalculateBaseWeights_() took "<<timer.
GetRealTime()<<
" us"<<std::endl;
350 samplePosTminus2_=samplePosOld_;
351 samplePosOld_=samplePosNew_;
356 std::cerr <<
"Condensation::Process() copy of vector took "<<timer.
GetRealTime()<<
" us"<<std::endl;
369 std::cerr <<
"Condensation::Process() calculate mean took "<<timer.
GetRealTime()<<
" us"<<std::endl;
371 std::cerr <<
"Condensation::Process() took "<<ot.
GetRealTime()<<
" us"<<std::endl;
377 BIASASSERT(!isnan(sumOfWeights_) && !isinf(sumOfWeights_));
378 if (sumOfWeights_<1E-20||sumOfWeights_!=sumOfWeights_)
return 0;
381 for (
unsigned int n=0;n<Nsamples_;n++) {
382 diff = samplePosOld_[n] - mean_;
383 var += (diff*diff) * sampleWeights_[n];
385 var /= sumOfWeights_;
386 if (var!=var)
return 0;
394 unsigned start=(unsigned)rint(Nsamples_*(defaultInitFraction_+
395 importanceFraction_));
396 double SumOfWeights=cumulProbArray_[Nsamples_-1]-cumulProbArray_[start];
397 if (SumOfWeights<DBL_EPSILON) {
398 BCDOUT(D_COND_MEANVAR,
"sum of weights top small for variance "
399 <<
"calculation "<<SumOfWeights);
403 for (
unsigned int n=start;n<Nsamples_;n++) {
404 diff = samplePosOld_[n] - mean_;
416 unsigned start=(unsigned)rint(Nsamples_*(defaultInitFraction_+
417 importanceFraction_));
418 if (Nsamples_-start<=1) {
419 BCDOUT(D_COND_MEANVAR,
"not enough samples "<<Nsamples_-start<<endl);
424 for (
unsigned int n=start;n<Nsamples_;n++) {
425 diff = samplePosOld_[n] - mean_;
428 var /= (double)(Nsamples_-start-1);
435 unsigned start=(unsigned)rint(Nsamples_*(defaultInitFraction_+
436 importanceFraction_));
437 if (Nsamples_-start<=0){
438 BCDOUT(D_COND_MEANVAR,
"not enough samples "<<Nsamples_-start<<endl);
443 for (
unsigned n=start;n<Nsamples_;n++) {
445 mean += sampleWeights_[n];
447 mean /= (double)(Nsamples_-start);
bool GetWeightedVariance(Vector< double > &var) const
Returns the "weighted variance" of the sample positions in var.
int InitPrior()
sets the sample positions for first time step
int Init(unsigned int nrSmaples)
Init the Condensation directly after constructor, with the desired amount of Samples, there more the better the approximation of the real densities.
virtual void AddOffsets_()
adds offSet_ to the samplePosNew_, can be overwritten if some other offset mechanism is needed ...
void SetDefaultInitFraction(double fraction)
Set the amount of samples, whose position is always taken by the prior distribution.
void Process()
This really does one iteration of Condensation.
double GetMeanWeight() const
Returns the mean weight of all samples.
virtual void AddDiffusions_()
Adds gaussian noise to the position, can be overwritten if some other (i.e.
void ElementwiseProduct(const Vector< T > &arg, Vector< T > &dest) const
multiply this with arg elementwise and store the result in dest
void SetImportanceSampleFraction(double fraction)
Only specifiy a value unlike zero, if EvaluateImportanceWeights() is overloaded.
void SetZero()
equivalent to matrix call
Vector< T > & newsize(Subscript N)
double GetRealTime() const
return real time (=wall time clock) in usec JW For Win32: real-time is measured differently from user...
virtual void CalculateBaseWeights_()
Once all the unweighted sample positions have been computed using predict_new_bases, this routine computes the weights by evaluating the observation density at each of the positions.
double GetVariance() const
returns the weighted variance as a scalar
class TimeMeasure contains functions for timing real time and cpu time.
int PredictNewSamplePositions()