~ruther/ctu-fee-eoa

ref: 79c83af97c81934e0a00b46126f6cea215dc77dc ctu-fee-eoa/env/src/fitness/rosenbrock.rs -rw-r--r-- 880 bytes
79c83af9 — Rutherther feat: add real fitness functions 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::convert::Infallible;
use super::FitnessFunction;

pub struct Rosenbrock;

impl Rosenbrock {
    pub fn new() -> Self {
        Rosenbrock
    }
}

impl FitnessFunction for Rosenbrock {
    type In = Vec<f64>;
    type Out = f64;
    type Err = Infallible;

    fn fit(self: &Self, inp: &Vec<f64>) -> Result<f64, Infallible> {
        Ok(inp.windows(2)
           .map(|xs| 100.0 * (xs[1] - xs[0].powi(2)).powi(2) + (1.0 - xs[0]).powi(2))
           .sum())
    }
}

#[cfg(test)]
pub mod tests {
    use crate::{fitness::{rosenbrock::Rosenbrock, FitnessFunction}, test_infra::load_test_file};

    #[test]
    fn test_rosenbrock() {
        let data = load_test_file::<f64, f64>("tests/rosenbrock.txt");

        for test in data {
            assert_eq!(
                Rosenbrock::new().fit(&test.inp).unwrap(),
                test.out
            )
        }
    }
}