Skip to content

Commit

Permalink
[TEST] Made the vpUniRand static to be able to have the same seed use…
Browse files Browse the repository at this point in the history
…d every time the tests are run
  • Loading branch information
rlagneau committed Jun 19, 2024
1 parent 60dd61e commit 083bf92
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
4 changes: 2 additions & 2 deletions example/particle-filter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ foreach(cpp ${example_cpp})
endif()
endforeach()

visp_add_test(pf-nonlinear-example-monothread pf-nonlinear-example --nb-particles 500 --ampli-max-X 0.02 --ampli-max-Y 0.02 --ampli-max-Z 0.01 --nb-steps-main 300 --max-distance-likelihood 10 --nb-threads 1 ${OPTION_TO_DESACTIVE_DISPLAY})
visp_add_test(pf-nonlinear-example-multithread pf-nonlinear-example --nb-particles 500 --ampli-max-X 0.02 --ampli-max-Y 0.02 --ampli-max-Z 0.01 --nb-steps-main 300 --max-distance-likelihood 10 --nb-threads -1 ${OPTION_TO_DESACTIVE_DISPLAY})
visp_add_test(pf-nonlinear-example-monothread pf-nonlinear-example --nb-particles 500 --ampli-max-X 0.02 --ampli-max-Y 0.02 --ampli-max-Z 0.01 --nb-steps-main 300 --max-distance-likelihood 10 --nb-threads 1 --seed 4224 ${OPTION_TO_DESACTIVE_DISPLAY})
visp_add_test(pf-nonlinear-example-multithread pf-nonlinear-example --nb-particles 500 --ampli-max-X 0.02 --ampli-max-Y 0.02 --ampli-max-Z 0.01 --nb-steps-main 300 --max-distance-likelihood 10 --nb-threads -1 --seed 4224 ${OPTION_TO_DESACTIVE_DISPLAY})
3 changes: 3 additions & 0 deletions modules/core/include/visp3/core/vpParticleFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ class VISP_EXPORT vpParticleFilter
void predictMonothread(const double &dt, const vpColVector &u);
void updateMonothread(const vpColVector &z);

static vpUniRand sampler;
static vpUniRand samplerRandomIdx;

unsigned int m_N; /*!< Number of particles.*/
unsigned int m_nbMaxThreads; /*!< Maximum number of threads to use.*/
std::vector<std::vector<vpGaussRand>> m_noiseGenerators; /*!< The noise generator adding noise to the particles at each time step.*/
Expand Down
10 changes: 8 additions & 2 deletions modules/core/src/math/misc/vpParticleFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@

BEGIN_VISP_NAMESPACE

vpUniRand vpParticleFilter::sampler;
vpUniRand vpParticleFilter::samplerRandomIdx;

vpParticleFilter::vpParticleFilter(const unsigned int &N, const std::vector<double> &stdev, const long &seed, const int &nbThreads)
: m_N(N)
, m_particles(N)
Expand Down Expand Up @@ -83,6 +86,11 @@ vpParticleFilter::vpParticleFilter(const unsigned int &N, const std::vector<doub
else {
seedForGenerator = vpTime::measureTimeMicros();
}

// Sampler for the simpleImportanceResampling method
sampler.setSeed(seed, 0x123465789ULL);
samplerRandomIdx.setSeed(seed + 4224, 0x123465789ULL);

vpUniRand seedGenerator(seedForGenerator);
for (unsigned int threadId = 0; threadId < m_nbMaxThreads; ++threadId) {
for (unsigned int stateId = 0; stateId < sizeState; ++stateId) {
Expand Down Expand Up @@ -193,8 +201,6 @@ bool vpParticleFilter::simpleResamplingCheck(const unsigned int &N, const std::v

vpParticleFilter::vpParticlesWithWeights vpParticleFilter::simpleImportanceResampling(const std::vector<vpColVector> &particles, const std::vector<double> &weights)
{
static vpUniRand sampler(vpTime::measureTimeMicros());
static vpUniRand samplerRandomIdx(vpTime::measureTimeMicros() + 4224);
unsigned int nbParticles = particles.size();
double x = 0.;
double sumWeights = 0.;
Expand Down

0 comments on commit 083bf92

Please sign in to comment.