pub trait Objective<G: ?Sized> {
fn evaluate(&mut self, genome: &G) -> f64;
}
impl<G: ?Sized, F: FnMut(&G) -> f64> Objective<G> for F {
fn evaluate(&mut self, genome: &G) -> f64 {
self(genome)
}
}
pub trait MultiObjective<G: ?Sized> {
fn objectives(&self) -> usize;
fn evaluate(&mut self, genome: &G, out: &mut [f64]) -> f64;
}
impl<G: ?Sized, M: MultiObjective<G> + ?Sized> MultiObjective<G> for &mut M {
fn objectives(&self) -> usize {
(**self).objectives()
}
fn evaluate(&mut self, genome: &G, out: &mut [f64]) -> f64 {
(**self).evaluate(genome, out)
}
}
pub fn multi_objective_fn<G: ?Sized, F: FnMut(&G, &mut [f64]) -> f64>(
objectives: usize,
f: F,
) -> MultiObjectiveFn<F> {
MultiObjectiveFn { objectives, f }
}
#[derive(Debug, Clone, Copy)]
pub struct MultiObjectiveFn<F> {
objectives: usize,
f: F,
}
impl<G: ?Sized, F: FnMut(&G, &mut [f64]) -> f64> MultiObjective<G> for MultiObjectiveFn<F> {
fn objectives(&self) -> usize {
self.objectives
}
fn evaluate(&mut self, genome: &G, out: &mut [f64]) -> f64 {
(self.f)(genome, out)
}
}