use std::{convert::Infallible, error::Error};
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<Self::Out, Self::Err>;
}
pub struct BinaryFitnessWrapper<D, TFitness>
where
D: Dim,
DefaultAllocator: Allocator<D>
{
min: OVector<f64, D>,
max: OVector<f64, D>,
fitting_function: TFitness,
}
impl<D, TFitness> BinaryFitnessWrapper<D, TFitness>
where
D: Dim,
DefaultAllocator: Allocator<D>
{
pub fn new(fitting_function: TFitness, min: OVector<f64, D>, max: OVector<f64, D>) -> Self {
BinaryFitnessWrapper {
fitting_function,
min,
max
}
}
}
impl<D, TFitness> FitnessFunction for BinaryFitnessWrapper<D, TFitness>
where
D: Dim,
DefaultAllocator: Allocator<D>,
TFitness: FitnessFunction<In = OVector<f64, D>, 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.min, &self.max)?).unwrap())
}
}