From d0b6ff6a1b9619241d4c24595f3b3e4383e4ffc6 Mon Sep 17 00:00:00 2001 From: Rutherther Date: Fri, 17 Oct 2025 21:46:24 +0200 Subject: [PATCH] tests: Add test for sphere real representation --- env/src/local_search/mod.rs | 39 ++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/env/src/local_search/mod.rs b/env/src/local_search/mod.rs index 4205b83aabee50ce7f1bad05532180348747f144..53e5f4f23db7a360fe002f867ba0b421181bdac2 100644 --- a/env/src/local_search/mod.rs +++ b/env/src/local_search/mod.rs @@ -277,10 +277,10 @@ pub fn plot_fitness_evolution(file: &str, stats: LocalSearchStats::from_element(0.0); @@ -315,7 +315,40 @@ pub mod tests { optimum ); - plot_fitness_evolution("test_results/sphere.png", result.stats.to_real(&min, &max).unwrap()).unwrap(); + plot_fitness_evolution("test_results/sphere_binary.png", result.stats.to_real(&min, &max).unwrap()).unwrap(); + } + + #[test] + fn test_local_search_sphere() { + let optimum = SVector::::repeat(4.0); + let sphere = Sphere::new(optimum); + + let result = local_search_first_improving_evolving( + &sphere, + &mut + AndTerminatingConditions::new( + vec![ + &mut CloserThanTerminatingCondition::new_remembered(optimum.clone(), 0.1), + &mut NoBetterForCyclesTerminatingCondition::new(100) + ] + ), + &mut RandomDistributionPerturbation::normal(0.3).unwrap(), + &MinimizingOperator::new(), + &mut IdentityStrategy, + &SVector::::repeat(-5.0), + ).unwrap(); + + println!("{:?}", result); + + assert!( + result.best_candidate.fit <= 0.1 + ); + + assert!( + (result.best_candidate.pos - optimum).magnitude() <= 0.1 + ); + + plot_fitness_evolution("test_results/sphere.png", result.stats).unwrap(); } #[test]