use std::convert::Infallible; use nalgebra::{allocator::Allocator, DefaultAllocator, Dim}; use crate::binary_string::BinaryString; use super::FitnessFunction; use std::marker::PhantomData; pub struct LABS { _phantom: PhantomData } impl LABS { pub fn new() -> Self { Self { _phantom: PhantomData } } } impl FitnessFunction for LABS where D: Dim, DefaultAllocator: Allocator { type In = BinaryString; type Out = i32; type Err = Infallible; fn fit(self: &Self, chromosome: &BinaryString) -> Result { fn ck(k: usize, s: &Vec) -> i32 { let d = s.len(); s.iter() .take(d - k) .zip(s.iter().skip(k)) .map(|(x, y)| x * y) .sum() } let s: Vec = chromosome .into_iter() .map(|c| (*c as i32) * 2 - 1) .collect(); let d = s.len(); Ok((1..=d-1) .map(|k| ck(k, &s).pow(2)) .sum()) } } #[cfg(test)] pub mod tests { use nalgebra::Dyn; use crate::{binary_string::BinaryString, fitness::{labs::LABS, FitnessFunction}, test_infra::load_test_file}; #[test] fn test_labs() { let data = load_test_file::("tests/labs.txt"); for test in data { println!("Test vector {}", test.inp.iter() .map(|x| x.to_string()) .collect::>() .join(", ")); assert_eq!( LABS::::new().fit(&BinaryString::new_dyn(test.inp)).unwrap(), test.out ) } } }