From 358eee7897a9fe3371ebba101f20baad6214609d Mon Sep 17 00:00:00 2001 From: lenarddome Date: Thu, 11 Aug 2022 18:08:26 +0100 Subject: [PATCH] :ambulance: fixed random sampling error R's global seed interfered with the sampling procedure in pspGlobal --- DESCRIPTION | 2 +- src/pspGlobal.cpp | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 9ef8882..9254c48 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: psp Title: Parameter Space Partitioning MCMC for Global Model Evaluation -Version: 0.5.3 +Version: 0.5.4 Date: 2022-06-12 Authors@R: c(person("Lenard", "Dome", email = "lenarddome@gmail.com", diff --git a/src/pspGlobal.cpp b/src/pspGlobal.cpp index fff31c2..2debf3c 100644 --- a/src/pspGlobal.cpp +++ b/src/pspGlobal.cpp @@ -12,6 +12,13 @@ using namespace arma; // https://mathworld.wolfram.com/HyperspherePointPicking.html // pick new jumping distributions from the unit hypersphere scaled by the radius mat HyperPoints(int counts, int dimensions, double radius) { + // set a random seed in R for better sampling + // see Documentation about the sampling + //std::srand(std::time_t(nullptr)); + int pool = std::rand(); + Rcpp::Environment base_env("package:base"); + Rcpp::Function set_seed_r = base_env["set.seed"]; + set_seed_r(pool); // create a uniform distribution mat hypersphere = randn(counts, dimensions, distr_param(0, 1) ); colvec denominator = sum(square(hypersphere), 1); @@ -212,7 +219,7 @@ List pspGlobal(Function model, List control, bool save = false, iteration += 1; // generate new jumping distributions from ordinal patterns with counts < population mat jumping_distribution = HyperPoints(last_eval.n_rows, dimensions, radius); - jumping_distribution += last_eval; + jumping_distribution = jumping_distribution + last_eval; jumping_distribution = ClampParameters(jumping_distribution, lower, upper); jumping_distribution.shed_rows(find(counts > population));