use crate::surrogate::RBFKernel;
use std::sync::Arc;
use crate::device::WgpuDevice;
#[derive(Debug, Clone, Copy, Default)]
pub enum PenaltyFilter {
#[default]
None,
Threshold(f64),
Quantile(f64),
AdaptiveMAD(f64),
}
#[derive(Clone)]
pub struct SparsitySamplerConfig {
pub n_initial: usize,
pub n_solvers: usize,
pub n_direct_solvers: usize,
pub max_eval_per_solver: usize,
pub n_iterations: usize,
pub tol: f64,
pub kernel: RBFKernel,
pub smoothing: f64,
pub auto_smoothing: bool,
pub penalty_filter: PenaltyFilter,
pub warm_start_seeds: Vec<Vec<f64>>,
pub seed: u64,
pub gpu_device: Option<Arc<WgpuDevice>>,
pub gpu_threshold: usize,
}
impl std::fmt::Debug for SparsitySamplerConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SparsitySamplerConfig")
.field("n_initial", &self.n_initial)
.field("n_solvers", &self.n_solvers)
.field("n_direct_solvers", &self.n_direct_solvers)
.field("max_eval_per_solver", &self.max_eval_per_solver)
.field("n_iterations", &self.n_iterations)
.field("tol", &self.tol)
.field("kernel", &self.kernel)
.field("smoothing", &self.smoothing)
.field("auto_smoothing", &self.auto_smoothing)
.field("penalty_filter", &self.penalty_filter)
.field("warm_start_seeds", &self.warm_start_seeds.len())
.field("seed", &self.seed)
.field(
"gpu_device",
&self.gpu_device.as_ref().map(|_| "Some(WgpuDevice)"),
)
.field("gpu_threshold", &self.gpu_threshold)
.finish()
}
}
impl SparsitySamplerConfig {
#[must_use]
pub fn new(n_dims: usize, seed: u64) -> Self {
Self {
n_initial: 10 * n_dims,
n_solvers: 8,
n_direct_solvers: 2, max_eval_per_solver: 50,
n_iterations: 5,
tol: 1e-6,
kernel: RBFKernel::ThinPlateSpline,
smoothing: 1e-3, auto_smoothing: true, penalty_filter: PenaltyFilter::None,
warm_start_seeds: Vec::new(),
seed,
gpu_device: None,
gpu_threshold: 100,
}
}
#[must_use]
pub fn with_initial_samples(mut self, n: usize) -> Self {
self.n_initial = n;
self
}
#[must_use]
pub fn with_solvers(mut self, n: usize) -> Self {
self.n_solvers = n;
self
}
#[must_use]
pub fn with_direct_solvers(mut self, n: usize) -> Self {
self.n_direct_solvers = n.min(self.n_solvers);
self
}
#[must_use]
pub fn with_eval_budget(mut self, n: usize) -> Self {
self.max_eval_per_solver = n;
self
}
#[must_use]
pub fn with_iterations(mut self, n: usize) -> Self {
self.n_iterations = n;
self
}
#[must_use]
pub fn with_kernel(mut self, kernel: RBFKernel) -> Self {
self.kernel = kernel;
self
}
#[must_use]
pub fn with_gpu(mut self, device: Arc<WgpuDevice>) -> Self {
self.gpu_device = Some(device);
self
}
#[must_use]
pub fn with_gpu_threshold(mut self, n: usize) -> Self {
self.gpu_threshold = n;
self
}
#[must_use]
pub fn with_smoothing(mut self, smoothing: f64) -> Self {
self.smoothing = smoothing;
self
}
#[must_use]
pub fn with_auto_smoothing(mut self, enabled: bool) -> Self {
self.auto_smoothing = enabled;
self
}
#[must_use]
pub fn with_penalty_filter(mut self, filter: PenaltyFilter) -> Self {
self.penalty_filter = filter;
self
}
#[must_use]
pub fn with_warm_start(mut self, seeds: Vec<Vec<f64>>) -> Self {
self.warm_start_seeds = seeds;
self
}
#[must_use]
pub fn total_budget(&self) -> usize {
self.n_initial + self.n_iterations * self.n_solvers * self.max_eval_per_solver
}
#[must_use]
pub fn should_use_gpu(&self, dataset_size: usize) -> bool {
self.gpu_device.is_some() && dataset_size >= self.gpu_threshold
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::surrogate::RBFKernel;
#[test]
fn config_new_scales_initial_by_dims() {
let config = SparsitySamplerConfig::new(5, 123);
assert_eq!(config.n_initial, 50);
assert_eq!(config.seed, 123);
assert_eq!(config.n_solvers, 8);
assert_eq!(config.n_direct_solvers, 2);
}
#[test]
fn config_total_budget() {
let config = SparsitySamplerConfig::new(2, 0)
.with_initial_samples(20)
.with_solvers(4)
.with_eval_budget(30)
.with_iterations(3);
assert_eq!(config.total_budget(), 20 + 3 * 4 * 30);
}
#[test]
fn config_should_use_gpu_no_device() {
let config = SparsitySamplerConfig::new(1, 0);
assert!(!config.should_use_gpu(1000));
}
#[test]
fn config_with_direct_solvers_clamps_to_n_solvers() {
let config = SparsitySamplerConfig::new(2, 0)
.with_solvers(4)
.with_direct_solvers(10);
assert_eq!(config.n_direct_solvers, 4);
}
#[test]
fn config_builder_chain() {
let config = SparsitySamplerConfig::new(3, 42)
.with_initial_samples(50)
.with_solvers(6)
.with_direct_solvers(3)
.with_kernel(RBFKernel::Gaussian { epsilon: 1.0 })
.with_smoothing(1e-5)
.with_auto_smoothing(false)
.with_penalty_filter(PenaltyFilter::Threshold(100.0));
assert_eq!(config.n_initial, 50);
assert_eq!(config.n_solvers, 6);
assert_eq!(config.n_direct_solvers, 3);
assert!(matches!(
config.kernel,
RBFKernel::Gaussian { epsilon: 1.0 }
));
assert!((config.smoothing - 1e-5).abs() < 1e-10);
assert!(!config.auto_smoothing);
assert!(matches!(
config.penalty_filter,
PenaltyFilter::Threshold(100.0)
));
}
#[test]
fn penalty_filter_default_is_none() {
let filter = PenaltyFilter::default();
assert!(matches!(filter, PenaltyFilter::None));
}
}