Trait FitnessIndividualTrait

Source
pub trait FitnessIndividualTrait: EachCrateIndividual {
    // Required method
    fn fitness(&self, other: &Self) -> usize;

    // Provided method
    fn fitness_group<'a, G>(group: G) -> HashMap<usize, usize>
       where G: IntoIterator<Item = &'a Self>,
             Self: 'a { ... }
}
Expand description

A trait for individual defined by fitness crate.

  • T - A type of value.

§Example

use std::{collections::HashMap, rc::Rc};

use scarlet_queen_core::each_individual::{Individual, EachCrateIndividual, FitnessIndividualTrait};

struct FITraitSample {
    individual: Rc<Individual<u8>>
}

impl EachCrateIndividual for FITraitSample {
    type Item = u8;

    fn new(individual: &std::rc::Rc<Individual<u8>>) -> Self {
        FITraitSample {
            individual: Rc::clone(individual)
        }
    }

    fn get_individual(&self) -> &Individual<u8> {
        &self.individual
    }
}

impl FitnessIndividualTrait for FITraitSample {
    fn fitness(&self, other: &Self) -> usize {
        if self.get_value() >= other.get_value() {
            1
        } else {
            0
        }
    }
}

let r_1: Rc<Individual<u8>> = Rc::new(Individual::new_with_id(0, 13));
let sample_1: FITraitSample = FITraitSample::new(&r_1);

assert_eq!(sample_1.get_individual(), r_1.as_ref());
assert_eq!(sample_1.get_id(), 0usize);
assert_eq!(sample_1.get_value(), &13u8);

let sample_2: FITraitSample = FITraitSample::new(&Rc::new(Individual::new_with_id(1, 5)));

assert_eq!(sample_1.fitness(&sample_2), 1);

let sample: Vec<FITraitSample> = vec![
    sample_1,
    sample_2,
    FITraitSample::new(&Rc::new(Individual::new_with_id(2, 15)))
];

assert_eq!(FITraitSample::fitness_group(&sample), vec![(0, 1), (1, 0), (2, 2)].into_iter().collect::<HashMap<usize, usize>>());

Required Methods§

Source

fn fitness(&self, other: &Self) -> usize

Calculate a fitness to an other individual.

  • other - A target of fitness.

Provided Methods§

Source

fn fitness_group<'a, G>(group: G) -> HashMap<usize, usize>
where G: IntoIterator<Item = &'a Self>, Self: 'a,

Calculate a fitness to a group.

A fitness to a group is the sum of fitnesses to other individuals.

  • 'a - A Lifetime of group.
  • G - A type of group.
  • group - A value which you are able to get Self from.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§