use std::convert::Infallible;
use crate::binary_string::{BinaryString, BinaryStringConversionError, Bounds};
pub mod labs;
pub mod one_max;
pub mod rosenbrock;
pub mod sphere;
pub trait FitnessFunction {
type In;
type Out;
type Err;
fn fit(self: &Self, inp: &Self::In) -> Result<Self::Out, Self::Err>;
}
pub struct BinaryFitnessWrapper<TFitness> {
bounds: Vec<Bounds>,
fitting_function: TFitness,
}
impl<TFitness> BinaryFitnessWrapper<TFitness> {
pub fn new(fitting_function: TFitness, bounds: Vec<Bounds>) -> Self {
BinaryFitnessWrapper {
fitting_function,
bounds,
}
}
}
impl<TFitness> FitnessFunction for BinaryFitnessWrapper<TFitness>
where
TFitness: FitnessFunction<In = Vec<f64>, Out = f64, Err = Infallible>
{
type In = BinaryString;
type Out = f64;
type Err = BinaryStringConversionError;
fn fit(self: &Self, inp: &BinaryString) -> Result<f64, BinaryStringConversionError> {
Ok(self.fitting_function.fit(&inp.to_real(&self.bounds)?).unwrap())
}
}