use std::{convert::Infallible, marker::PhantomData}; use nalgebra::{allocator::Allocator, DefaultAllocator, Dim}; use crate::binary_string::BinaryString; use super::FitnessFunction; pub struct OneMax { _phantom: PhantomData } impl OneMax { pub fn new() -> Self { Self { _phantom: PhantomData } } } impl FitnessFunction for OneMax where D: Dim, DefaultAllocator: Allocator { type In = BinaryString; type Out = i32; type Err = Infallible; fn fit(self: &Self, chromosome: &BinaryString) -> Result { 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::("tests/onemax.txt"); for test in data { assert_eq!( OneMax::new().fit(&BinaryString::new_dyn(test.inp)).unwrap(), test.out ); } } }