~ruther/ctu-fee-eoa

ref: 2ae232625f0de81d7b05edb44448f32ad83fad61 ctu-fee-eoa/env/src/fitness/one_max.rs -rw-r--r-- 938 bytes
2ae23262 — Rutherther chore: add nalgebra 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
40
41
use std::convert::Infallible;
use crate::binary_string::BinaryString;
use super::FitnessFunction;

pub struct OneMax;
impl OneMax {
    pub fn new() -> Self {
        OneMax
    }
}

impl FitnessFunction for OneMax {
    type In = BinaryString;
    type Out = i32;
    type Err = Infallible;

    fn fit(self: &Self, chromosome: &BinaryString) -> Result<i32, Infallible> {
        Ok(chromosome.into_iter()
           .map(|x| *x as i32)
           .sum())
    }
}

#[cfg(test)]
pub mod tests {
    use crate::fitness::one_max::OneMax;
    use crate::{binary_string::BinaryString, test_infra::load_test_file};
    use crate::fitness::FitnessFunction;

    #[test]
    fn test_one_max() {
        let data = load_test_file::<i8, i32>("tests/onemax.txt");

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