77 | Released under BSD License. See: https://www.mrpt.org/License |
88 +------------------------------------------------------------------------+ */
99
10- #include " bayes-precomp.h" // Precompiled headers
11- //
1210#include < mrpt/bayes/CParticleFilterCapable.h>
1311#include < mrpt/math/ops_vectors.h>
1412#include < mrpt/random.h>
@@ -47,7 +45,10 @@ void CParticleFilterCapable::performResampling(
4745 performSubstitution (indxs);
4846
4947 // Finally, equal weights:
50- for (size_t i = 0 ; i < out_particle_count; i++) setW (i, 0 /* Logarithmic weight */ );
48+ for (size_t i = 0 ; i < out_particle_count; i++)
49+ {
50+ setW (i, 0 /* Logarithmic weight */ );
51+ }
5152
5253 MRPT_END
5354}
@@ -69,7 +70,10 @@ void CParticleFilterCapable::computeResampling(
6970 size_t i, j, M = in_logWeights.size ();
7071 ASSERT_ (M > 0 );
7172
72- if (!out_particle_count) out_particle_count = M;
73+ if (!out_particle_count)
74+ {
75+ out_particle_count = M;
76+ }
7377
7478 vector<double > linW (M, 0 );
7579 vector<double >::const_iterator inIt;
@@ -79,7 +83,9 @@ void CParticleFilterCapable::computeResampling(
7983 // This is to avoid float point range problems:
8084 double max_log_w = math::maximum (in_logWeights);
8185 for (i = 0 , inIt = in_logWeights.begin (), outIt = linW.begin (); i < M; i++, inIt++, outIt++)
86+ {
8287 linW_SUM += ((*outIt) = exp ((*inIt) - max_log_w));
88+ }
8389
8490 // Normalize weights:
8591 ASSERT_ (linW_SUM > 0 );
@@ -132,7 +138,7 @@ void CParticleFilterCapable::computeResampling(
132138 size_t R = 0 ; // Remainder or residual count
133139 for (i = 0 ; i < M; i++)
134140 {
135- N[i] = int (M * linW[i]);
141+ N[i] = static_cast < uint32_t > (M * linW[i]);
136142 R += N[i];
137143 }
138144 size_t N_rnd =
@@ -143,7 +149,12 @@ void CParticleFilterCapable::computeResampling(
143149 // Fillout the deterministic part of the resampling:
144150 out_indexes.resize (out_particle_count);
145151 for (i = 0 , j = 0 ; i < out_particle_count; i++)
146- for (size_t k = 0 ; k < N[i]; k++) out_indexes[j++] = i;
152+ {
153+ for (size_t k = 0 ; k < N[i]; k++)
154+ {
155+ out_indexes[j++] = i;
156+ }
157+ }
147158
148159 size_t M_fixed = j;
149160
@@ -156,7 +167,10 @@ void CParticleFilterCapable::computeResampling(
156167 // Compute modified weights:
157168 vector<double > linW_mod (M);
158169 const double M_R_1 = 1.0 / N_rnd;
159- for (i = 0 ; i < M; i++) linW_mod[i] = M_R_1 * (M * linW[i] - N[i]);
170+ for (i = 0 ; i < M; i++)
171+ {
172+ linW_mod[i] = M_R_1 * (M * linW[i] - N[i]);
173+ }
160174
161175 // perform resampling:
162176 vector<double > Q;
@@ -214,7 +228,9 @@ void CParticleFilterCapable::computeResampling(
214228 while (i < out_particle_count)
215229 {
216230 if (T[i] < Q[j])
231+ {
217232 out_indexes[i++] = (unsigned int )j;
233+ }
218234 else
219235 {
220236 j++;
@@ -236,15 +252,20 @@ void CParticleFilterCapable::computeResampling(
236252 vector<double > T (M + 1 );
237253 double _1_M = 1.0 / M;
238254 T[0 ] = getRandomGenerator ().drawUniform (0.0 , _1_M);
239- for (i = 1 ; i < M; i++) T[i] = T[i - 1 ] + _1_M;
255+ for (i = 1 ; i < M; i++)
256+ {
257+ T[i] = T[i - 1 ] + _1_M;
258+ }
240259 T[M] = 1 ;
241260
242261 out_indexes.resize (out_particle_count);
243262 i = j = 0 ;
244263 while (i < out_particle_count)
245264 {
246265 if (T[i] < Q[j])
266+ {
247267 out_indexes[i++] = (unsigned int )j;
268+ }
248269 else
249270 {
250271 j++;
@@ -355,9 +376,11 @@ void CParticleFilterCapable::prepareFastDrawSample(
355376 // -> Use m_fastDrawAuxiliary.CDF, PDF, CDF_indexes
356377 // --------------------------------------------------------
357378 if (PF_options.resamplingMethod != CParticleFilter::prMultinomial)
379+ {
358380 THROW_EXCEPTION (
359381 " resamplingMethod must be 'prMultinomial' for a dynamic number "
360382 " of particles!" );
383+ }
361384
362385 size_t i, j = 666666 , M = particlesCount ();
363386
@@ -376,7 +399,9 @@ void CParticleFilterCapable::prepareFastDrawSample(
376399 double SUM = 0 ;
377400 // Save the log likelihoods:
378401 for (i = 0 ; i < M; i++)
402+ {
379403 m_fastDrawAuxiliary.PDF [i] = partEvaluator (PF_options, this , i, action, observation);
404+ }
380405 // "Normalize":
381406 m_fastDrawAuxiliary.PDF += -math::maximum (m_fastDrawAuxiliary.PDF );
382407 for (i = 0 ; i < M; i++) SUM += m_fastDrawAuxiliary.PDF [i] = exp (m_fastDrawAuxiliary.PDF [i]);
@@ -386,34 +411,37 @@ void CParticleFilterCapable::prepareFastDrawSample(
386411
387412 // Compute the CDF thresholds:
388413 for (i = 0 ; i < PARTICLE_FILTER_CAPABLE_FAST_DRAW_BINS; i++)
414+ {
389415 m_fastDrawAuxiliary.CDF [i] = ((double )i) / ((double )PARTICLE_FILTER_CAPABLE_FAST_DRAW_BINS);
416+ }
390417 m_fastDrawAuxiliary.CDF [PARTICLE_FILTER_CAPABLE_FAST_DRAW_BINS] = 1.0 ;
391418
392419 // Compute the CDF and save threshold indexes:
393420 double CDF = 0 ; // Cumulative density func.
394421 for (i = 0 , j = 0 ; i < M && j < PARTICLE_FILTER_CAPABLE_FAST_DRAW_BINS; i++)
395422 {
396423 double CDF_next = CDF + m_fastDrawAuxiliary.PDF [i];
397- if (i == (M - 1 )) CDF_next = 1.0 ; // rounds fix...
398- if (CDF_next > 1.0 ) CDF_next = 1.0 ;
424+ if (i == (M - 1 ))
425+ {
426+ CDF_next = 1.0 ; // rounds fix...
427+ }
428+ if (CDF_next > 1.0 )
429+ {
430+ CDF_next = 1.0 ;
431+ }
399432
400433 while (m_fastDrawAuxiliary.CDF [j] < CDF_next)
434+ {
401435 m_fastDrawAuxiliary.CDF_indexes [j++] = (unsigned int )i;
436+ }
402437
403438 CDF = CDF_next;
404439 }
405440
406441 ASSERT_ (j == PARTICLE_FILTER_CAPABLE_FAST_DRAW_BINS);
407442
408- // Done!
409- #if !defined(_MSC_VER) || (_MSC_VER > 1400) // <=VC2005 doesn't work with this!
410- MRPT_END_WITH_CLEAN_UP ( /* Debug: */
411- cout << " j=" << j << " \n m_fastDrawAuxiliary.CDF_indexes:"
412- << m_fastDrawAuxiliary.CDF_indexes << endl;
413- cout << " m_fastDrawAuxiliary.CDF:" << m_fastDrawAuxiliary.CDF << endl;);
414- #else
443+ // Done!
415444 MRPT_END
416- #endif
417445 }
418446 else
419447 {
@@ -426,9 +454,10 @@ void CParticleFilterCapable::prepareFastDrawSample(
426454 size_t i, M = particlesCount ();
427455 vector<double > PDF (M, 0 );
428456 for (i = 0 ; i < M; i++)
429- PDF[i] = partEvaluator (
430- PF_options, this , i, action,
431- observation); // Default evaluator: takes current weight.
457+ {
458+ // Default evaluator: takes current weight.
459+ PDF[i] = partEvaluator (PF_options, this , i, action, observation);
460+ }
432461
433462 vector<size_t > idxs;
434463
@@ -440,7 +469,9 @@ void CParticleFilterCapable::prepareFastDrawSample(
440469 m_fastDrawAuxiliary.alreadyDrawnIndexes .resize (idxs.size ());
441470 for (it = idxs.begin (), it2 = m_fastDrawAuxiliary.alreadyDrawnIndexes .begin (); it != idxs.end ();
442471 ++it, ++it2)
472+ {
443473 *it2 = (unsigned int )(*it);
474+ }
444475
445476 m_fastDrawAuxiliary.alreadyDrawnNextOne = 0 ;
446477 }
@@ -463,9 +494,11 @@ size_t CParticleFilterCapable::fastDrawSample(
463494 // -> Use m_fastDrawAuxiliary.CDF, PDF, CDF_indexes
464495 // --------------------------------------------------------
465496 if (PF_options.resamplingMethod != CParticleFilter::prMultinomial)
497+ {
466498 THROW_EXCEPTION (
467499 " resamplingMethod must be 'prMultinomial' for a dynamic number "
468500 " of particles!" );
501+ }
469502
470503 double draw = getRandomGenerator ().drawUniform (0.0 , 0.999999 );
471504 double CDF_next = -1 .;
@@ -500,9 +533,11 @@ size_t CParticleFilterCapable::fastDrawSample(
500533 // -> Use m_fastDrawAuxiliary.alreadyDrawnIndexes & alreadyDrawnNextOne
501534 // --------------------------------------------------------
502535 if (m_fastDrawAuxiliary.alreadyDrawnNextOne >= m_fastDrawAuxiliary.alreadyDrawnIndexes .size ())
536+ {
503537 THROW_EXCEPTION (
504538 " Have you called 'fastDrawSample' more times than the sample "
505539 " size? Did you forget calling 'prepareFastCall' before?" );
540+ }
506541
507542 return m_fastDrawAuxiliary.alreadyDrawnIndexes [m_fastDrawAuxiliary.alreadyDrawnNextOne ++];
508543 }
@@ -522,15 +557,24 @@ void CParticleFilterCapable::log2linearWeights(
522557
523558 out_linWeights.resize (N);
524559
525- if (!N) return ;
560+ if (!N)
561+ {
562+ return ;
563+ }
526564
527565 double sumW = 0 ;
528566 size_t i;
529- for (i = 0 ; i < N; i++) sumW += out_linWeights[i] = exp (in_logWeights[i]);
567+ for (i = 0 ; i < N; i++)
568+ {
569+ sumW += out_linWeights[i] = exp (in_logWeights[i]);
570+ }
530571
531572 ASSERT_ (sumW > 0 );
532573
533- for (i = 0 ; i < N; i++) out_linWeights[i] /= sumW;
574+ for (i = 0 ; i < N; i++)
575+ {
576+ out_linWeights[i] /= sumW;
577+ }
534578
535579 MRPT_END
536580}
0 commit comments