From e024536ed1af68510afba8d79775f64cba8acaca Mon Sep 17 00:00:00 2001 From: Rutherther Date: Sun, 30 Nov 2025 16:42:10 +0100 Subject: [PATCH] feat: declare problems for hw02 --- codes/constr_hw02/src/main.rs | 132 ++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 codes/constr_hw02/src/main.rs diff --git a/codes/constr_hw02/src/main.rs b/codes/constr_hw02/src/main.rs new file mode 100644 index 0000000000000000000000000000000000000000..54558aea2dda2e8e85a4ff9d33e1608b7f765e8a --- /dev/null +++ b/codes/constr_hw02/src/main.rs @@ -0,0 +1,132 @@ +use std::convert::Infallible; + +use eoa_lib::{constraints::LowerThanConstraintFunction, fitness::FitnessFunction}; +use nalgebra::{OVector, SVector}; + +pub struct ArbitraryFitness { + fun: Box) -> f64> +} + +impl ArbitraryFitness { + pub fn new(fun: Box) -> f64>) -> Self { + Self { + fun + } + } +} + +impl FitnessFunction for ArbitraryFitness { + type In = SVector; + type Out = f64; + type Err = Infallible; + + fn fit(&self, inp: &Self::In) -> Result { + Ok((self.fun)(*inp)) + } +} + +fn problem_g06() -> (ArbitraryFitness<2>, [LowerThanConstraintFunction, f64>; 2], f64) { + ( + ArbitraryFitness::new( + Box::new(|vec| (vec[0] - 10.0).powi(3) + (vec[1] - 20.0).powi(3)) + ), + [ + LowerThanConstraintFunction::new( + Box::new(|vec| -(vec[0] - 5.0).powi(2) - (vec[1] - 5.0).powi(2) + 100.0) + ), + LowerThanConstraintFunction::new( + Box::new(|vec| (vec[0] - 6.0).powi(2) + (vec[1] - 5.0).powi(2) - 82.81) + ), + ], + -6961.8137558015 + ) +} + +fn problem_g08(eps: f64) -> (ArbitraryFitness<2>, [LowerThanConstraintFunction, f64>; 2], f64) { + ( + ArbitraryFitness::new( + Box::new(|vec| { + let num = (2.0 * std::f64::consts::PI * vec[0]).sin().powi(3) + * (2.0 * std::f64::consts::PI * vec[1]).sin(); + let den = vec[0].powi(3) * (vec[0] + vec[1]); + -num / den + }) + ), + [ + LowerThanConstraintFunction::new( + Box::new(move |vec| { + let x1 = vec[0]; + let x2 = vec[1]; + x1.powi(2) - x2 + 1.0 + }) + ), + LowerThanConstraintFunction::new( + Box::new(move |vec| { + let x1 = vec[0]; + let x2 = vec[1]; + 1.0 - x1 + (x2 - 4.0).powi(2) + }) + ), + ], + -0.0958250414180359 + ) +} + +pub fn problem_g11(eps: f64) -> (ArbitraryFitness<2>, [LowerThanConstraintFunction, f64>; 2], f64) { + ( + ArbitraryFitness::new( + Box::new(|vec| { + // Minimize f(x) = x1^2 + (x2 - 1)^2 + vec[0].powi(2) + (vec[1] - 1.0).powi(2) + }) + ), + [ + // Equality h(x) = x2 - x1^2 = 0 + // Transformed 1: h - eps >= 0 => -(h - eps) <= 0 + LowerThanConstraintFunction::new( + Box::new(move |vec| { + let h = vec[1] - vec[0].powi(2); + h - eps + }) + ), + // Transformed 2: eps - h >= 0 => -(eps - h) <= 0 + LowerThanConstraintFunction::new( + Box::new(move |vec| { + let h = vec[1] - vec[0].powi(2); + eps - h + }) + ), + ], + 0.7499 // Best known optimum + ) +} + +pub fn problem_g24() -> (ArbitraryFitness<2>, [LowerThanConstraintFunction, f64>; 2], f64) { + ( + ArbitraryFitness::new( + Box::new(|vec| { + // Minimize f(x) = -x1 - x2 + -vec[0] - vec[1] + }) + ), + [ + // g1(x) = -2x1^4 + 8x1^3 - 8x1^2 + x2 - 2 <= 0 + LowerThanConstraintFunction::new( + Box::new(|vec| { + -2.0 * vec[0].powi(4) + 8.0 * vec[0].powi(3) - 8.0 * vec[0].powi(2) + vec[1] - 2.0 + }) + ), + // g2(x) = -4x1^4 + 32x1^3 - 88x1^2 + 96x1 + x2 - 36 <= 0 + LowerThanConstraintFunction::new( + Box::new(|vec| { + -4.0 * vec[0].powi(4) + 32.0 * vec[0].powi(3) - 88.0 * vec[0].powi(2) + 96.0 * vec[0] + vec[1] - 36.0 + }) + ), + ], + -5.50801327159536 // Best known optimum + ) +} + +fn main() { + println!("Hello, world!"); +}