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§
sourcetype Best<T: Fitness>: Best<Item = T>
type Best<T: Fitness>: Best<Item = T>
The best element container.
- Use
SingleBest
for single objective. - Use
Pareto
for multi-objective.
sourcetype Eval: PartialOrd + 'static
type Eval: PartialOrd + 'static
A value to compare the final fitness value.
Required Methods§
sourcefn is_dominated(&self, rhs: &Self) -> bool
fn is_dominated(&self, rhs: &Self) -> bool
Check if self
dominates rhs
.
sourcefn eval(&self) -> Self::Eval
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.