Trait Population

Source
pub trait Population<'a> {
    type Individual: Individual<'a> + 'a;
    type IndividualCollection: Iterator<Item = &'a <Self as Population<'a>>::Individual>;

    // Required methods
    fn get_fittest_population(
        &'a self,
        n: usize,
        cost_data: &'a <<Self as Population<'a>>::Individual as Individual<'a>>::IndividualCost,
    ) -> Self;
    fn evolve(&self, mutate_prob: f32) -> Self;
    fn iter(&'a self) -> Self::IndividualCollection;

    // Provided methods
    fn fitnesses(
        &'a self,
        cost_data: &'a <<Self as Population<'a>>::Individual as Individual<'a>>::IndividualCost,
    ) -> Vec<(f64, &Self::Individual)> { ... }
    fn get_n_fittest(
        &'a self,
        n: usize,
        cost_data: &'a <<Self as Population<'a>>::Individual as Individual<'a>>::IndividualCost,
    ) -> Vec<Self::Individual> { ... }
    fn evolve_individuals(&'a self, mutate_prob: f32) -> Vec<Self::Individual> { ... }
}
Expand description

The container for your individuals in a genetic algorithm.

Required Associated Types§

Source

type Individual: Individual<'a> + 'a

The Type of individuals your population should consist of.

Source

type IndividualCollection: Iterator<Item = &'a <Self as Population<'a>>::Individual>

The iteratore type if you iterate over your individuals. It depends on the data container you use to store individuals in your implementation of Population.

Required Methods§

Source

fn get_fittest_population( &'a self, n: usize, cost_data: &'a <<Self as Population<'a>>::Individual as Individual<'a>>::IndividualCost, ) -> Self

Get the n fittest individuals in your population as population routes object. This is typically used to select the top n inidividuals, before continuing to evolve the routes further.

§Arguments
  • n - The number of individuals you would like to have.
  • cost_data - The cost data structure your indivudals need to compute their fitness.
Source

fn evolve(&self, mutate_prob: f32) -> Self

Evolve your population.

The evolution consists of the following stages:

  1. crossover between all 1,…,n indivials. Each individual will not be crossovered with itself, but as the crossover is not neccessarily symmetric indivdual_a.crossover(indivual_b) as well as individual_b.crossover(individual_a) will be computed.
  2. mutate is applied to all individuals.

The difference to the evolve_individuals function is that this needs to be implemented in the struct that implements this trait because how the population is constructed depends on the representation you choose to use. Please use the evolve_individuals-function to get the updated inviduals. You could use an iterator or implement the From<Vec<Individuals>>-trait.

§Arguments
  • mutate_prob - The probabilty of an inviduals beeing mutated. Is applied via individuals.mutate.
Source

fn iter(&'a self) -> Self::IndividualCollection

Iterate over the individuals in your population.

Provided Methods§

Source

fn fitnesses( &'a self, cost_data: &'a <<Self as Population<'a>>::Individual as Individual<'a>>::IndividualCost, ) -> Vec<(f64, &Self::Individual)>

Given the pool of current individuals, compute the fitness of your individuals to solve the problem at hand.

§Arguments
  • cost_data - The data neccessary to assess the fitness of an individual.
Source

fn get_n_fittest( &'a self, n: usize, cost_data: &'a <<Self as Population<'a>>::Individual as Individual<'a>>::IndividualCost, ) -> Vec<Self::Individual>

Get the n fittest individuals in your population.

§Arguments
  • n - The number of individuals you would like to get
  • cost_data - The cost data structure your individuals need to compute their fitness.
Source

fn evolve_individuals(&'a self, mutate_prob: f32) -> Vec<Self::Individual>

Evolve your population.

The evolution consists of the following stages:

  1. crossover between all 1,…,n indivials. Each individual will not be crossovered with itself, but as the crossover is not neccessarily symmetric indivdual_a.crossover(indivual_b) as well as individual_b.crossover(individual_a) will be computed.
  2. mutate is applied to all individuals.
§Arguments
  • mutate_prob - The probabilty of an inviduals beeing mutated. Is applied via individuals.mutate.

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§