use crate::device::WgpuDevice;
use crate::error::{BarracudaError, Result};
use crate::optimize::eval_record::EvaluationCache;
use crate::sample::latin_hypercube;
use crate::surrogate::{RBFKernel, RBFSurrogate, loo_cv_optimal_smoothing};
use std::sync::Arc;
#[derive(Debug, Clone)]
pub struct DirectSamplerConfig {
pub n_rounds: usize,
pub n_solvers: usize,
pub max_eval_per_solver: usize,
pub tol: f64,
pub patience: usize,
pub improvement_threshold: f64,
pub seed: u64,
pub warm_start_seeds: Vec<Vec<f64>>,
pub kernel: RBFKernel,
pub auto_smoothing: bool,
}
impl DirectSamplerConfig {
#[must_use]
pub fn new(seed: u64) -> Self {
Self {
n_rounds: 5,
n_solvers: 8,
max_eval_per_solver: 200,
tol: 1e-8,
patience: 2,
improvement_threshold: 1e-6,
seed,
warm_start_seeds: Vec::new(),
kernel: RBFKernel::ThinPlateSpline,
auto_smoothing: true,
}
}
#[must_use]
pub fn with_rounds(mut self, n: usize) -> Self {
self.n_rounds = n;
self
}
#[must_use]
pub fn with_solvers(mut self, n: usize) -> Self {
self.n_solvers = n;
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_tolerance(mut self, tol: f64) -> Self {
self.tol = tol;
self
}
#[must_use]
pub fn with_patience(mut self, p: usize) -> Self {
self.patience = p;
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_rounds * self.n_solvers * self.max_eval_per_solver
}
}
#[derive(Debug, Clone)]
pub struct RoundResult {
pub round: usize,
pub best_f: f64,
pub n_evals: usize,
pub surrogate_rmse: Option<f64>,
pub improvement: f64,
}
#[derive(Debug)]
pub struct DirectSamplerResult {
pub x_best: Vec<f64>,
pub f_best: f64,
pub cache: EvaluationCache,
pub rounds: Vec<RoundResult>,
pub surrogate: Option<RBFSurrogate>,
pub early_stopped: bool,
}
impl DirectSamplerResult {
#[must_use]
pub fn top_k_seeds(&self, k: usize) -> Vec<Vec<f64>> {
let mut records: Vec<_> = self.cache.records().to_vec();
records.sort_by(|a, b| a.f.partial_cmp(&b.f).unwrap_or(std::cmp::Ordering::Equal));
records.into_iter().take(k).map(|r| r.x).collect()
}
#[must_use]
pub fn total_evals(&self) -> usize {
self.cache.len()
}
}
pub fn direct_sampler<F>(
device: Arc<WgpuDevice>,
f: F,
bounds: &[(f64, f64)],
config: &DirectSamplerConfig,
) -> Result<DirectSamplerResult>
where
F: Fn(&[f64]) -> f64,
{
let n_dims = bounds.len();
if n_dims == 0 {
return Err(BarracudaError::InvalidInput {
message: "bounds cannot be empty".to_string(),
});
}
let mut cache = EvaluationCache::with_capacity(config.total_budget());
let mut rounds = Vec::with_capacity(config.n_rounds);
let mut last_surrogate = None;
let mut best_f = f64::INFINITY;
let mut no_improvement_count = 0;
let mut early_stopped = false;
for seed in &config.warm_start_seeds {
if seed.len() == n_dims {
let val = f(seed);
cache.record(seed.clone(), val);
if val < best_f {
best_f = val;
}
}
}
for round in 0..config.n_rounds {
let round_start_evals = cache.len();
let prev_best = best_f;
let round_seed = config.seed.wrapping_add((round as u64 + 1) * 10007);
let starts = latin_hypercube(config.n_solvers, bounds, round_seed)?;
for x0 in &starts {
let (x_opt, f_opt, _) = crate::optimize::nelder_mead(
&f,
x0,
bounds,
config.max_eval_per_solver,
config.tol,
)?;
cache.record(x_opt, f_opt);
if f_opt < best_f {
best_f = f_opt;
}
}
let (x_data, y_data) = cache.training_data();
let surrogate_rmse = if x_data.len() >= 2 {
let smoothing = if config.auto_smoothing {
loo_cv_optimal_smoothing(device.clone(), &x_data, &y_data, config.kernel, None)
.map_or(1e-6, |r| r.smoothing)
} else {
1e-6
};
match RBFSurrogate::train(device.clone(), &x_data, &y_data, config.kernel, smoothing) {
Ok(surr) => {
let rmse = surr.loo_cv_rmse().ok();
last_surrogate = Some(surr);
rmse
}
Err(_) => None,
}
} else {
None
};
let improvement = prev_best - best_f;
if improvement < config.improvement_threshold {
no_improvement_count += 1;
} else {
no_improvement_count = 0;
}
rounds.push(RoundResult {
round,
best_f,
n_evals: cache.len() - round_start_evals,
surrogate_rmse,
improvement,
});
if no_improvement_count >= config.patience {
early_stopped = true;
break;
}
}
let record = cache
.best()
.ok_or_else(|| BarracudaError::Internal("No evaluations recorded".to_string()))?;
let (x_best, f_best) = (record.x.clone(), record.f);
Ok(DirectSamplerResult {
x_best,
f_best,
cache,
rounds,
surrogate: last_surrogate,
early_stopped,
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::device::test_pool::get_test_device_if_f64_gpu_available_sync;
#[test]
fn test_direct_sampler_sphere() {
let Some(device) = get_test_device_if_f64_gpu_available_sync() else {
return;
};
let sphere = |x: &[f64]| x.iter().map(|v| v * v).sum();
let bounds = vec![(-5.0, 5.0), (-5.0, 5.0)];
let config = DirectSamplerConfig::new(42)
.with_rounds(3)
.with_solvers(4)
.with_eval_budget(100);
let result = direct_sampler(device, sphere, &bounds, &config).unwrap();
assert!(
result.f_best < 0.01,
"Failed to minimize: {}",
result.f_best
);
assert_eq!(result.x_best.len(), 2);
}
#[test]
fn test_direct_sampler_with_warm_start() {
let Some(device) = get_test_device_if_f64_gpu_available_sync() else {
return;
};
let sphere = |x: &[f64]| x.iter().map(|v| v * v).sum();
let bounds = vec![(-5.0, 5.0), (-5.0, 5.0)];
let config = DirectSamplerConfig::new(42)
.with_rounds(2)
.with_solvers(2)
.with_warm_start(vec![vec![0.1, 0.1]]);
let result = direct_sampler(device, sphere, &bounds, &config).unwrap();
assert!(result.f_best < 0.001);
}
#[test]
fn test_direct_sampler_early_stop() {
let Some(device) = get_test_device_if_f64_gpu_available_sync() else {
return;
};
let constant = |_: &[f64]| 1.0;
let bounds = vec![(-1.0, 1.0)];
let config = DirectSamplerConfig::new(42)
.with_rounds(10)
.with_patience(2);
let result = direct_sampler(device, constant, &bounds, &config).unwrap();
assert!(result.early_stopped);
assert!(result.rounds.len() < 10);
}
#[test]
fn test_direct_sampler_diagnostics() {
let Some(device) = get_test_device_if_f64_gpu_available_sync() else {
return;
};
let sphere = |x: &[f64]| x.iter().map(|v| v * v).sum();
let bounds = vec![(-5.0, 5.0)];
let config = DirectSamplerConfig::new(42).with_rounds(3).with_solvers(2);
let result = direct_sampler(device, sphere, &bounds, &config).unwrap();
assert!(!result.rounds.is_empty());
for (i, round) in result.rounds.iter().enumerate() {
assert_eq!(round.round, i);
assert!(round.n_evals > 0);
}
}
}