31 size_t out_particle_count)
36 const size_t in_particle_count = particlesCount();
40 vector<double> log_ws;
41 log_ws.assign(in_particle_count, .0);
42 for (
size_t i = 0; i < in_particle_count; i++) log_ws[i] = getW(i);
49 performSubstitution(indxs);
52 for (
size_t i = 0; i < out_particle_count; i++)
63 const vector<double>& in_logWeights, vector<size_t>& out_indexes,
64 size_t out_particle_count)
71 size_t i, j, M = in_logWeights.size();
74 if (!out_particle_count) out_particle_count = M;
76 vector<double> linW(M, 0);
83 for (i = 0, inIt = in_logWeights.begin(), outIt = linW.begin(); i < M;
85 linW_SUM += ((*outIt) = exp((*inIt) - max_log_w));
89 linW *= 1.0 / linW_SUM;
99 mrpt::math::cumsum_tmpl<vector<double>, vector<double>,
double>(
109 sort(T.begin(), T.end());
111 out_indexes.resize(out_particle_count);
114 while (i < out_particle_count)
118 out_indexes[i++] = (
unsigned int)j;
123 if (j >= M) j = M - 1;
135 std::vector<uint32_t> N(M);
137 for (i = 0; i < M; i++)
139 N[i] = int(M * linW[i]);
142 size_t N_rnd = out_particle_count >=
R ? (out_particle_count -
R)
148 out_indexes.resize(out_particle_count);
149 for (i = 0, j = 0; i < out_particle_count; i++)
150 for (
size_t k = 0; k < N[i]; k++) out_indexes[j++] = i;
161 vector<double> linW_mod(M);
162 const double M_R_1 = 1.0 / N_rnd;
163 for (i = 0; i < M; i++)
164 linW_mod[i] = M_R_1 * (M * linW[i] - N[i]);
168 mrpt::math::cumsum_tmpl<vector<double>, vector<double>,
double>(
177 sort(T.begin(), T.end());
186 out_indexes[M_fixed + i++] = (
unsigned int)j;
191 if (j >= M) j = M - 1;
203 mrpt::math::cumsum_tmpl<vector<double>, vector<double>,
double>(
208 vector<double> T(M + 1);
209 const double _1_M = 1.0 / M;
210 const double _1_M_eps = _1_M - 0.000001;
212 for (i = 0; i < M; i++)
220 out_indexes.resize(out_particle_count);
222 while (i < out_particle_count)
225 out_indexes[i++] = (
unsigned int)j;
229 if (j >= M) j = M - 1;
240 mrpt::math::cumsum_tmpl<vector<double>, vector<double>,
double>(
245 vector<double> T(M + 1);
246 double _1_M = 1.0 / M;
248 for (i = 1; i < M; i++) T[i] = T[i - 1] + _1_M;
251 out_indexes.resize(out_particle_count);
253 while (i < out_particle_count)
256 out_indexes[i++] = (
unsigned int)j;
260 if (j >= M) j = M - 1;
268 "ERROR: Unknown resampling method selected: %i", method));
285 prediction_and_update_pfStandardProposal(
286 action, observation, PF_options);
289 prediction_and_update_pfAuxiliaryPFStandard(
290 action, observation, PF_options);
293 prediction_and_update_pfOptimalProposal(
294 action, observation, PF_options);
297 prediction_and_update_pfAuxiliaryPFOptimal(
298 action, observation, PF_options);
320 "Algorithm 'pfStandardProposal' is not implemented in inherited "
335 "Algorithm 'pfAuxiliaryPFStandard' is not implemented in inherited "
350 "Algorithm 'pfOptimalProposal' is not implemented in inherited class!");
364 "Algorithm 'pfAuxiliaryPFOptimal' is not implemented in inherited "
374 const void* observation)
const
386 "resamplingMethod must be 'prMultinomial' for a dynamic number "
389 size_t i, j = 666666, M = particlesCount();
394 m_fastDrawAuxiliary.CDF.resize(
395 1 + PARTICLE_FILTER_CAPABLE_FAST_DRAW_BINS, 0);
396 m_fastDrawAuxiliary.CDF_indexes.resize(
397 PARTICLE_FILTER_CAPABLE_FAST_DRAW_BINS, 0);
401 m_fastDrawAuxiliary.PDF.resize(M, 0);
407 for (i = 0; i < M; i++)
408 m_fastDrawAuxiliary.PDF[i] =
409 partEvaluator(PF_options,
this, i, action, observation);
411 m_fastDrawAuxiliary.PDF += -
math::maximum(m_fastDrawAuxiliary.PDF);
412 for (i = 0; i < M; i++)
413 SUM += m_fastDrawAuxiliary.PDF[i] = exp(m_fastDrawAuxiliary.PDF[i]);
416 m_fastDrawAuxiliary.PDF *= 1.0 / SUM;
419 for (i = 0; i < PARTICLE_FILTER_CAPABLE_FAST_DRAW_BINS; i++)
420 m_fastDrawAuxiliary.CDF[i] =
421 ((
double)i) / ((double)PARTICLE_FILTER_CAPABLE_FAST_DRAW_BINS);
422 m_fastDrawAuxiliary.CDF[PARTICLE_FILTER_CAPABLE_FAST_DRAW_BINS] = 1.0;
426 for (i = 0, j = 0; i < M && j < PARTICLE_FILTER_CAPABLE_FAST_DRAW_BINS;
429 double CDF_next = CDF + m_fastDrawAuxiliary.PDF[i];
430 if (i == (M - 1)) CDF_next = 1.0;
431 if (CDF_next > 1.0) CDF_next = 1.0;
433 while (m_fastDrawAuxiliary.CDF[j] < CDF_next)
434 m_fastDrawAuxiliary.CDF_indexes[j++] = (
unsigned int)i;
439 ASSERT_(j == PARTICLE_FILTER_CAPABLE_FAST_DRAW_BINS);
442 #if !defined(_MSC_VER) || (_MSC_VER > 1400) // <=VC2005 doesn't work with this!
445 cout <<
"j=" << j <<
"\nm_fastDrawAuxiliary.CDF_indexes:" << m_fastDrawAuxiliary.CDF_indexes << endl; \
446 cout <<
"m_fastDrawAuxiliary.CDF:" << m_fastDrawAuxiliary.CDF << endl; \
460 size_t i, M = particlesCount();
461 vector<double> PDF(M, 0);
462 for (i = 0; i < M; i++)
463 PDF[i] = partEvaluator(
464 PF_options,
this, i, action,
474 m_fastDrawAuxiliary.alreadyDrawnIndexes.resize(idxs.size());
475 for (it = idxs.begin(),
476 it2 = m_fastDrawAuxiliary.alreadyDrawnIndexes.begin();
477 it != idxs.end(); ++it, ++it2)
478 *it2 = (
unsigned int)(*it);
480 m_fastDrawAuxiliary.alreadyDrawnNextOne = 0;
502 "resamplingMethod must be 'prMultinomial' for a dynamic number "
506 double CDF_next = -1.;
513 size_t j = (size_t)floor(
514 draw * ((
double)PARTICLE_FILTER_CAPABLE_FAST_DRAW_BINS - 0.05));
515 CDF = m_fastDrawAuxiliary.CDF[j];
516 size_t i = m_fastDrawAuxiliary.CDF_indexes[j];
519 while (draw > (CDF_next = CDF + m_fastDrawAuxiliary.PDF[i]))
529 "\n[CParticleFilterCapable::fastDrawSample] DEBUG: draw=%f, "
530 "CDF=%f CDF_next=%f\n",
531 draw, CDF, CDF_next););
539 if (m_fastDrawAuxiliary.alreadyDrawnNextOne >=
540 m_fastDrawAuxiliary.alreadyDrawnIndexes.size())
542 "Have you called 'fastDrawSample' more times than the sample "
543 "size? Did you forget calling 'prepareFastCall' before?");
545 return m_fastDrawAuxiliary
546 .alreadyDrawnIndexes[m_fastDrawAuxiliary.alreadyDrawnNextOne++];
556 const vector<double>& in_logWeights, vector<double>& out_linWeights)
560 size_t N = in_logWeights.size();
562 out_linWeights.resize(N);
568 for (i = 0; i < N; i++) sumW += out_linWeights[i] = exp(in_logWeights[i]);
572 for (i = 0; i < N; i++) out_linWeights[i] /= sumW;