From fc2c9918e0e54f8cccf5e8ed7cbeb7e9af4e4303 Mon Sep 17 00:00:00 2001 From: Markus <66058642+mhovd@users.noreply.github.com> Date: Tue, 3 Jun 2025 13:33:21 +0200 Subject: [PATCH 1/3] chore: Add validation of parameters Ensures that the parameter ranges are valid prior to initialization --- src/routines/initialization/mod.rs | 48 ++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/src/routines/initialization/mod.rs b/src/routines/initialization/mod.rs index 216171e4..3e09df28 100644 --- a/src/routines/initialization/mod.rs +++ b/src/routines/initialization/mod.rs @@ -31,7 +31,8 @@ impl Prior { Prior::Sobol(points, seed) } - pub fn get_points(&self) -> usize { + /// Get the number of initial support points + pub fn points(&self) -> usize { match self { Prior::Sobol(points, _) => *points, Prior::Latin(points, _) => *points, @@ -42,7 +43,8 @@ impl Prior { } } - pub fn get_seed(&self) -> usize { + /// Get the seed used for the random number generator + pub fn seed(&self) -> usize { match self { Prior::Sobol(_, seed) => *seed, Prior::Latin(_, seed) => *seed, @@ -62,6 +64,48 @@ impl Default for Prior { /// This function generates the grid of support points according to the sampler specified in the [Settings] pub fn sample_space(settings: &Settings) -> Result { + // Ensure that the parameter ranges are not infinite + for param in settings.parameters().iter() { + if param.lower.is_infinite() || param.upper.is_infinite() { + bail!( + "Parameter '{}' has infinite bounds: [{}, {}]", + param.name, + param.lower, + param.upper + ); + } + + // Ensure that the lower bound is less than the upper bound + if param.lower >= param.upper { + bail!( + "Parameter '{}' has invalid bounds: [{}, {}]. Lower bound must be less than upper bound.", + param.name, + param.lower, + param.upper + ); + } + + // Ensure that the bounds are finite + if !param.lower.is_finite() || !param.upper.is_finite() { + bail!( + "Parameter '{}' has non-finite bounds: [{}, {}]", + param.name, + param.lower, + param.upper + ); + } + + // Ensure that the parameter has a non-zero range + if param.lower == param.upper { + bail!( + "Parameter '{}' has zero range: [{}, {}]. Lower and upper bounds must not be equal.", + param.name, + param.lower, + param.upper + ); + } + } + // Otherwise, parse the sampler type and generate the grid let prior = match settings.prior() { Prior::Sobol(points, seed) => sobol::generate(settings.parameters(), *points, *seed)?, From fce7882752e1df4c4b062624d030b2c46ce40b1c Mon Sep 17 00:00:00 2001 From: Markus Hovd Date: Tue, 3 Jun 2025 13:39:42 +0200 Subject: [PATCH 2/3] Update src/routines/initialization/mod.rs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/routines/initialization/mod.rs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/routines/initialization/mod.rs b/src/routines/initialization/mod.rs index 3e09df28..970dd54c 100644 --- a/src/routines/initialization/mod.rs +++ b/src/routines/initialization/mod.rs @@ -85,16 +85,6 @@ pub fn sample_space(settings: &Settings) -> Result { ); } - // Ensure that the bounds are finite - if !param.lower.is_finite() || !param.upper.is_finite() { - bail!( - "Parameter '{}' has non-finite bounds: [{}, {}]", - param.name, - param.lower, - param.upper - ); - } - // Ensure that the parameter has a non-zero range if param.lower == param.upper { bail!( From 0db954d30645f162994634192a8f66b610189dd1 Mon Sep 17 00:00:00 2001 From: Markus <66058642+mhovd@users.noreply.github.com> Date: Tue, 3 Jun 2025 13:41:18 +0200 Subject: [PATCH 3/3] Remove unnecessary check --- src/routines/initialization/mod.rs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/routines/initialization/mod.rs b/src/routines/initialization/mod.rs index 970dd54c..279fee46 100644 --- a/src/routines/initialization/mod.rs +++ b/src/routines/initialization/mod.rs @@ -84,16 +84,6 @@ pub fn sample_space(settings: &Settings) -> Result { param.upper ); } - - // Ensure that the parameter has a non-zero range - if param.lower == param.upper { - bail!( - "Parameter '{}' has zero range: [{}, {}]. Lower and upper bounds must not be equal.", - param.name, - param.lower, - param.upper - ); - } } // Otherwise, parse the sampler type and generate the grid