Trait metaheuristics_nature::Fitness

source ·
pub trait Fitness: MaybeParallel + Clone + 'static {
    type Best<T: Fitness>: Best<Item = T>;
    type Eval: PartialOrd + 'static;

    // Required methods
    fn is_dominated(&self, rhs: &Self) -> bool;
    fn eval(&self) -> Self::Eval;
}
Expand description

Trait for dominance comparison.

By default, the trait is implemented for types that implement PartialOrd + Clone + 'static, which means a clonable non-lifetime type comparable with a < b is equivalent to a.is_dominated(b) for using single objective.

§Example

Single objective problems can simply use the f32/f64 number type.

Multi-objective problems can specify the Pareto container as Fitness::Best and implement Fitness::eval() to decide the final fitness value.

use metaheuristics_nature::{pareto::Pareto, Fitness};

#[derive(Clone)]
struct MyObject {
    cost: f64,
    weight: f64,
}

impl Fitness for MyObject {
    type Best<T: Fitness> = Pareto<T>;
    type Eval = f64;
    fn is_dominated(&self, rhs: &Self) -> bool {
        self.cost <= rhs.cost && self.weight <= rhs.weight
    }
    fn eval(&self) -> Self::Eval {
        self.cost.max(self.weight)
    }
}

Required Associated Types§

source

type Best<T: Fitness>: Best<Item = T>

The best element container.

source

type Eval: PartialOrd + 'static

A value to compare the final fitness value.

Required Methods§

source

fn is_dominated(&self, rhs: &Self) -> bool

Check if self dominates rhs.

source

fn eval(&self) -> Self::Eval

Evaluate the final fitness value.

Used in Best::as_result() and Best::update() when reaching the limit.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<T: MaybeParallel + PartialOrd + Clone + 'static> Fitness for T

§

type Best<A: Fitness> = SingleBest<A>

§

type Eval = T

source§

impl<Y: Fitness> Fitness for MakeSingle<Y>
where Y::Eval: Fitness,

§

type Best<T: Fitness> = SingleBest<T>

§

type Eval = <Y as Fitness>::Eval

source§

impl<Y: Fitness, P> Fitness for WithProduct<Y, P>
where P: MaybeParallel + Clone + 'static,

§

type Best<T: Fitness> = <Y as Fitness>::Best<T>

§

type Eval = <Y as Fitness>::Eval