use std::convert::Infallible;
use crate::{binary_string::BinaryString, test_infra::load_test_file};
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())
}
}
#[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
);
}
}