use std::{convert::Infallible, error::Error, marker::PhantomData}; use nalgebra::{allocator::Allocator, DefaultAllocator, Dim, OVector}; use crate::binary_string::{BinaryString, BinaryStringConversionError}; pub mod labs; pub mod one_max; pub mod rosenbrock; pub mod sphere; pub mod real; pub trait FitnessFunction { type In; type Out; type Err: Error + 'static; fn fit(self: &Self, inp: &Self::In) -> Result; } pub struct BinaryFitnessWrapper where D: Dim, DefaultAllocator: Allocator { min: OVector, max: OVector, fitting_function: TFitness, _phantom: PhantomData } impl BinaryFitnessWrapper where DString: Dim, DefaultAllocator: Allocator, D: Dim, DefaultAllocator: Allocator { pub fn new(fitting_function: TFitness, min: OVector, max: OVector) -> Self { Self { fitting_function, min, max, _phantom: PhantomData } } } impl FitnessFunction for BinaryFitnessWrapper where DString: Dim, DefaultAllocator: Allocator, D: Dim, DefaultAllocator: Allocator, TFitness: FitnessFunction, Out = f64, Err = Infallible> { type In = BinaryString; type Out = f64; type Err = BinaryStringConversionError; fn fit(self: &Self, inp: &BinaryString) -> Result { Ok(self.fitting_function.fit(&inp.to_real(&self.min, &self.max)?).unwrap()) } }