~ruther/ctu-fee-eoa

ref: cab8e17120da81286ea20f990eec468130938541 ctu-fee-eoa/env/src/fitness/mod.rs -rw-r--r-- 1.0 KiB
cab8e171 — Rutherther chore: guard test imports by #[cfg(test)] a day ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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())
    }
}