From ff86cab83b7a8afcf2cba73d12cb63b074544207 Mon Sep 17 00:00:00 2001 From: Rutherther Date: Fri, 5 Dec 2025 23:01:23 +0100 Subject: [PATCH] fix: problem g05 was missing sin calls --- codes/constr_hw02/src/problems.rs | 42 ++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/codes/constr_hw02/src/problems.rs b/codes/constr_hw02/src/problems.rs index 156c95795e7d35c85f69f72a79f9b8b1c4c48949..634801d0950a065506adb187e55a14986b1133bb 100644 --- a/codes/constr_hw02/src/problems.rs +++ b/codes/constr_hw02/src/problems.rs @@ -127,16 +127,16 @@ pub fn problem_g05() -> ConstrainedProblem<4, 5> { LowerThanConstraintFunction::new(Box::new(|vec| -vec[3] + vec[2] - 0.55)), LowerThanConstraintFunction::new(Box::new(|vec| -vec[2] + vec[3] - 0.55)), LowerThanConstraintFunction::new(Box::new(|vec| { - 1000.0 * (-vec[2] - 0.25).sin() + 1000.0 * (-vec[3] - 0.25) - vec[0] + 894.8 + 1000.0 * (-vec[2] - 0.25).sin() + 1000.0 * (-vec[3] - 0.25).sin() - vec[0] + 894.8 })), LowerThanConstraintFunction::new(Box::new(|vec| { 1000.0 * (vec[2] - 0.25).sin() - + 1000.0 * (vec[2] - vec[3] - 0.25) + + 1000.0 * (vec[2] - vec[3] - 0.25).sin() - vec[1] + 894.8 })), LowerThanConstraintFunction::new(Box::new(|vec| { - 1000.0 * (vec[2] - 0.25).sin() + 1000.0 * (vec[3] - vec[2] - 0.25) + 1294.8 + 1000.0 * (vec[3] - 0.25).sin() + 1000.0 * (vec[3] - vec[2] - 0.25).sin() + 1294.8 })), ], bounds: ( @@ -370,4 +370,38 @@ pub struct NsgaConfig { pub parents_count: usize, pub iterations: usize, pub mutation_std_dev: f64, -} \ No newline at end of file +} + +#[cfg(test)] +mod tests { + use super::*; + use nalgebra::SVector; + use eoa_lib::constraints::ConstraintFunction; + + #[test] + fn test_g05_optimal_feasibility() { + let problem = problem_g05(); + let optimal_solution = SVector::::from([ + 679.945148297028709, + 1026.06697600004691, + 0.118876369094410433, + -0.39623348521517826, + ]); + + let epsilon = 1e-4; // Tolerance for floating point comparisons + + for (i, constraint) in problem.constraints.iter().enumerate() { + let evaluation = constraint.evaluate(&optimal_solution).unwrap(); + // For inequality constraints g(x) <= 0, we check if evaluation <= epsilon + // For equality constraints h(x) = 0, which are transformed to |h(x)| - eps <= 0, + // we check if evaluation <= epsilon + assert!( + evaluation <= epsilon, + "Constraint {} (g05) failed: Expected evaluation <= {}, got {}", + i + 1, + epsilon, + evaluation + ); + } + } +}