fix: resolve compilation errors in optimiz-r

- Remove unused Uniform import in shade.rs
- Prefix unused variables with underscore in shade.rs and timeseries_utils.rs
- Make PyO3 bindings conditional with feature gates in rust_objectives.rs
- Simplify rust_objectives.rs with compact implementations
- All benchmarks (Sphere, Rosenbrock, Rastrigin, Ackley, Griewank) now compile without python-bindings feature
- Resolves: unused imports, unused variables, unresolved PyO3 crate errors
This commit is contained in:
Melvin Alvarez
2026-01-04 13:25:12 +01:00
parent 75660d0bf7
commit 5ec5dff6ab
3 changed files with 57 additions and 211 deletions
+55 -209
View File
@@ -1,276 +1,122 @@
///! Rust-native objective functions for GIL-free parallelization ///! Rust-native objective functions for GIL-free parallelization
///!
///! This module defines a RustObjective trait that enables parallel evaluation
///! of objective functions without Python GIL contention. Useful for:
///! - Benchmark functions (Sphere, Rosenbrock, Rastrigin, etc.)
///! - Pure mathematical functions
///! - High-throughput optimization scenarios
///!
///! Unlike Python callbacks, RustObjective functions can be parallelized
///! using Rayon for 10-100× speedup on multi-core systems.
#[cfg(feature = "python-bindings")]
use pyo3::prelude::*; use pyo3::prelude::*;
/// Trait for Rust-native objective functions
///
/// Implementing this trait allows objective functions to be evaluated
/// in parallel without Python GIL contention.
pub trait RustObjective: Send + Sync { pub trait RustObjective: Send + Sync {
/// Evaluate the objective function at point x
fn evaluate(&self, x: &[f64]) -> f64; fn evaluate(&self, x: &[f64]) -> f64;
fn dimension(&self) -> Option<usize> { None }
/// Optional: Get the dimensionality of the problem fn global_optimum(&self) -> Option<f64> { None }
fn dimension(&self) -> Option<usize> { fn optimal_solution(&self) -> Option<Vec<f64>> { None }
None
}
/// Optional: Get the known global optimum (for benchmarking)
fn global_optimum(&self) -> Option<f64> {
None
}
/// Optional: Get the known optimal solution (for benchmarking)
fn optimal_solution(&self) -> Option<Vec<f64>> {
None
}
} }
// ============================================================================ #[cfg_attr(feature = "python-bindings", pyo3::pyclass)]
// Benchmark Functions
// ============================================================================
/// Sphere function: f(x) = sum(x_i^2)
/// Global minimum: f(0, ..., 0) = 0
/// Convex, unimodal, separable
#[pyclass]
#[derive(Clone)] #[derive(Clone)]
pub struct Sphere { pub struct Sphere { pub dim: usize }
#[pyo3(get)]
pub dim: usize,
}
#[pymethods]
impl Sphere { impl Sphere {
#[new] pub fn new(dim: usize) -> Self { Sphere { dim } }
pub fn new(dim: usize) -> Self { #[cfg(feature = "python-bindings")]
Sphere { dim } pub fn __call__(&self, x: Vec<f64>) -> f64 { self.evaluate(&x) }
}
pub fn __call__(&self, x: Vec<f64>) -> f64 {
self.evaluate(&x)
}
} }
impl RustObjective for Sphere { impl RustObjective for Sphere {
fn evaluate(&self, x: &[f64]) -> f64 { fn evaluate(&self, x: &[f64]) -> f64 { x.iter().map(|xi| xi * xi).sum() }
x.iter().map(|xi| xi * xi).sum() fn dimension(&self) -> Option<usize> { Some(self.dim) }
} fn global_optimum(&self) -> Option<f64> { Some(0.0) }
fn optimal_solution(&self) -> Option<Vec<f64>> { Some(vec![0.0; self.dim]) }
fn dimension(&self) -> Option<usize> {
Some(self.dim)
}
fn global_optimum(&self) -> Option<f64> {
Some(0.0)
}
fn optimal_solution(&self) -> Option<Vec<f64>> {
Some(vec![0.0; self.dim])
}
} }
/// Rosenbrock function: f(x) = sum(100(x_{i+1} - x_i^2)^2 + (1 - x_i)^2) #[cfg_attr(feature = "python-bindings", pyo3::pyclass)]
/// Global minimum: f(1, ..., 1) = 0
/// Non-convex, unimodal, non-separable
#[pyclass]
#[derive(Clone)] #[derive(Clone)]
pub struct Rosenbrock { pub struct Rosenbrock { pub dim: usize }
#[pyo3(get)]
pub dim: usize,
}
#[pymethods]
impl Rosenbrock { impl Rosenbrock {
#[new] pub fn new(dim: usize) -> Self { Rosenbrock { dim } }
pub fn new(dim: usize) -> Self { #[cfg(feature = "python-bindings")]
Rosenbrock { dim } pub fn __call__(&self, x: Vec<f64>) -> f64 { self.evaluate(&x) }
}
pub fn __call__(&self, x: Vec<f64>) -> f64 {
self.evaluate(&x)
}
} }
impl RustObjective for Rosenbrock { impl RustObjective for Rosenbrock {
fn evaluate(&self, x: &[f64]) -> f64 { fn evaluate(&self, x: &[f64]) -> f64 {
(0..x.len() - 1) (0..x.len() - 1).map(|i| {
.map(|i| { let t1 = x[i + 1] - x[i] * x[i];
let term1 = x[i + 1] - x[i] * x[i]; let t2 = 1.0 - x[i];
let term2 = 1.0 - x[i]; 100.0 * t1 * t1 + t2 * t2
100.0 * term1 * term1 + term2 * term2 }).sum()
})
.sum()
}
fn dimension(&self) -> Option<usize> {
Some(self.dim)
}
fn global_optimum(&self) -> Option<f64> {
Some(0.0)
}
fn optimal_solution(&self) -> Option<Vec<f64>> {
Some(vec![1.0; self.dim])
} }
fn dimension(&self) -> Option<usize> { Some(self.dim) }
fn global_optimum(&self) -> Option<f64> { Some(0.0) }
fn optimal_solution(&self) -> Option<Vec<f64>> { Some(vec![1.0; self.dim]) }
} }
/// Rastrigin function: f(x) = 10n + sum(x_i^2 - 10cos(2πx_i)) #[cfg_attr(feature = "python-bindings", pyo3::pyclass)]
/// Global minimum: f(0, ..., 0) = 0
/// Highly multimodal, separable
#[pyclass]
#[derive(Clone)] #[derive(Clone)]
pub struct Rastrigin { pub struct Rastrigin { pub dim: usize }
#[pyo3(get)]
pub dim: usize,
}
#[pymethods]
impl Rastrigin { impl Rastrigin {
#[new] pub fn new(dim: usize) -> Self { Rastrigin { dim } }
pub fn new(dim: usize) -> Self { #[cfg(feature = "python-bindings")]
Rastrigin { dim } pub fn __call__(&self, x: Vec<f64>) -> f64 { self.evaluate(&x) }
}
pub fn __call__(&self, x: Vec<f64>) -> f64 {
self.evaluate(&x)
}
} }
impl RustObjective for Rastrigin { impl RustObjective for Rastrigin {
fn evaluate(&self, x: &[f64]) -> f64 { fn evaluate(&self, x: &[f64]) -> f64 {
let n = x.len() as f64; let n = x.len() as f64;
let pi = std::f64::consts::PI; let pi = std::f64::consts::PI;
10.0 * n + x.iter().map(|xi| xi * xi - 10.0 * (2.0 * pi * xi).cos()).sum::<f64>()
10.0 * n + x.iter()
.map(|xi| xi * xi - 10.0 * (2.0 * pi * xi).cos())
.sum::<f64>()
}
fn dimension(&self) -> Option<usize> {
Some(self.dim)
}
fn global_optimum(&self) -> Option<f64> {
Some(0.0)
}
fn optimal_solution(&self) -> Option<Vec<f64>> {
Some(vec![0.0; self.dim])
} }
fn dimension(&self) -> Option<usize> { Some(self.dim) }
fn global_optimum(&self) -> Option<f64> { Some(0.0) }
fn optimal_solution(&self) -> Option<Vec<f64>> { Some(vec![0.0; self.dim]) }
} }
/// Ackley function: f(x) = -20exp(-0.2√(1/n ∑x_i^2)) - exp(1/n ∑cos(2πx_i)) + 20 + e #[cfg_attr(feature = "python-bindings", pyo3::pyclass)]
/// Global minimum: f(0, ..., 0) = 0
/// Highly multimodal, non-separable
#[pyclass]
#[derive(Clone)] #[derive(Clone)]
pub struct Ackley { pub struct Ackley { pub dim: usize }
#[pyo3(get)]
pub dim: usize,
}
#[pymethods]
impl Ackley { impl Ackley {
#[new] pub fn new(dim: usize) -> Self { Ackley { dim } }
pub fn new(dim: usize) -> Self { #[cfg(feature = "python-bindings")]
Ackley { dim } pub fn __call__(&self, x: Vec<f64>) -> f64 { self.evaluate(&x) }
}
pub fn __call__(&self, x: Vec<f64>) -> f64 {
self.evaluate(&x)
}
} }
impl RustObjective for Ackley { impl RustObjective for Ackley {
fn evaluate(&self, x: &[f64]) -> f64 { fn evaluate(&self, x: &[f64]) -> f64 {
let n = x.len() as f64; let n = x.len() as f64;
let pi = std::f64::consts::PI; let pi = std::f64::consts::PI;
let e = std::f64::consts::E;
let sum_sq = x.iter().map(|xi| xi * xi).sum::<f64>(); let sum_sq = x.iter().map(|xi| xi * xi).sum::<f64>();
let sum_cos = x.iter().map(|xi| (2.0 * pi * xi).cos()).sum::<f64>(); let sum_cos = x.iter().map(|xi| (2.0 * pi * xi).cos()).sum::<f64>();
-20.0 * (-0.2 * (sum_sq / n).sqrt()).exp() - (sum_cos / n).exp() + 20.0 + std::f64::consts::E
-20.0 * (-0.2 * (sum_sq / n).sqrt()).exp()
- (sum_cos / n).exp()
+ 20.0
+ e
}
fn dimension(&self) -> Option<usize> {
Some(self.dim)
}
fn global_optimum(&self) -> Option<f64> {
Some(0.0)
}
fn optimal_solution(&self) -> Option<Vec<f64>> {
Some(vec![0.0; self.dim])
} }
fn dimension(&self) -> Option<usize> { Some(self.dim) }
fn global_optimum(&self) -> Option<f64> { Some(0.0) }
fn optimal_solution(&self) -> Option<Vec<f64>> { Some(vec![0.0; self.dim]) }
} }
/// Griewank function: f(x) = 1 + (1/4000)∑x_i^2 - ∏cos(x_i/√i) #[cfg_attr(feature = "python-bindings", pyo3::pyclass)]
/// Global minimum: f(0, ..., 0) = 0
/// Multimodal, non-separable
#[pyclass]
#[derive(Clone)] #[derive(Clone)]
pub struct Griewank { pub struct Griewank { pub dim: usize }
#[pyo3(get)]
pub dim: usize,
}
#[pymethods]
impl Griewank { impl Griewank {
#[new] pub fn new(dim: usize) -> Self { Griewank { dim } }
pub fn new(dim: usize) -> Self { #[cfg(feature = "python-bindings")]
Griewank { dim } pub fn __call__(&self, x: Vec<f64>) -> f64 { self.evaluate(&x) }
}
pub fn __call__(&self, x: Vec<f64>) -> f64 {
self.evaluate(&x)
}
} }
impl RustObjective for Griewank { impl RustObjective for Griewank {
fn evaluate(&self, x: &[f64]) -> f64 { fn evaluate(&self, x: &[f64]) -> f64 {
let sum_sq = x.iter().map(|xi| xi * xi).sum::<f64>(); let sum_sq = x.iter().map(|xi| xi * xi).sum::<f64>();
let prod_cos = x.iter() let prod_cos = x.iter().enumerate().map(|(i, xi)| (xi / ((i + 1) as f64).sqrt()).cos()).product::<f64>();
.enumerate()
.map(|(i, xi)| (xi / ((i + 1) as f64).sqrt()).cos())
.product::<f64>();
1.0 + sum_sq / 4000.0 - prod_cos 1.0 + sum_sq / 4000.0 - prod_cos
} }
fn dimension(&self) -> Option<usize> { Some(self.dim) }
fn dimension(&self) -> Option<usize> { fn global_optimum(&self) -> Option<f64> { Some(0.0) }
Some(self.dim) fn optimal_solution(&self) -> Option<Vec<f64>> { Some(vec![0.0; self.dim]) }
}
fn global_optimum(&self) -> Option<f64> {
Some(0.0)
}
fn optimal_solution(&self) -> Option<Vec<f64>> {
Some(vec![0.0; self.dim])
}
} }
// ============================================================================ #[cfg(feature = "python-bindings")]
// Python Bindings pub fn register_benchmark_functions(m: &pyo3::Bound<pyo3::types::PyModule>) -> pyo3::PyResult<()> {
// ============================================================================
pub fn register_benchmark_functions(m: &Bound<PyModule>) -> PyResult<()> {
m.add_class::<Sphere>()?; m.add_class::<Sphere>()?;
m.add_class::<Rosenbrock>()?; m.add_class::<Rosenbrock>()?;
m.add_class::<Rastrigin>()?; m.add_class::<Rastrigin>()?;
+1 -1
View File
@@ -31,7 +31,7 @@
///! - Multimodal functions ///! - Multimodal functions
///! - Convergence speed (fewer evaluations to target) ///! - Convergence speed (fewer evaluations to target)
use rand::distributions::{Distribution, Uniform}; use rand::distributions::Distribution;
use rand::prelude::*; use rand::prelude::*;
use rand_distr::{Cauchy, Normal}; use rand_distr::{Cauchy, Normal};
+1 -1
View File
@@ -403,7 +403,7 @@ mod tests {
#[test] #[test]
fn test_return_statistics() { fn test_return_statistics() {
let returns = vec![0.01, -0.02, 0.015, 0.005, -0.01]; let returns = vec![0.01, -0.02, 0.015, 0.005, -0.01];
let (mean, std, skew, kurt, sharpe) = return_statistics(&returns); let (mean, std, _skew, _kurt, _sharpe) = return_statistics(&returns);
assert!((mean).abs() < 0.1); // Small mean assert!((mean).abs() < 0.1); // Small mean
assert!(std > 0.0); // Non-zero volatility assert!(std > 0.0); // Non-zero volatility