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§
Sourcetype Individual: Individual<'a> + 'a
type Individual: Individual<'a> + 'a
The Type of individuals your population should consist of.
Sourcetype IndividualCollection: Iterator<Item = &'a <Self as Population<'a>>::Individual>
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§
Sourcefn get_fittest_population(
&'a self,
n: usize,
cost_data: &'a <<Self as Population<'a>>::Individual as Individual<'a>>::IndividualCost,
) -> Self
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.
Sourcefn evolve(&self, mutate_prob: f32) -> Self
fn evolve(&self, mutate_prob: f32) -> Self
Evolve your population.
The evolution consists of the following stages:
crossover
between all 1,…,n indivials. Each individual will not becrossover
ed with itself, but as the crossover is not neccessarily symmetricindivdual_a.crossover(indivual_b)
as well asindividual_b.crossover(individual_a)
will be computed.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 viaindividuals.mutate
.
Sourcefn iter(&'a self) -> Self::IndividualCollection
fn iter(&'a self) -> Self::IndividualCollection
Iterate over the individuals in your population.
Provided Methods§
Sourcefn fitnesses(
&'a self,
cost_data: &'a <<Self as Population<'a>>::Individual as Individual<'a>>::IndividualCost,
) -> Vec<(f64, &Self::Individual)>
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.
Sourcefn get_n_fittest(
&'a self,
n: usize,
cost_data: &'a <<Self as Population<'a>>::Individual as Individual<'a>>::IndividualCost,
) -> Vec<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>
Get the n fittest individuals in your population.
§Arguments
n
- The number of individuals you would like to getcost_data
- The cost data structure your individuals need to compute their fitness.
Sourcefn evolve_individuals(&'a self, mutate_prob: f32) -> Vec<Self::Individual>
fn evolve_individuals(&'a self, mutate_prob: f32) -> Vec<Self::Individual>
Evolve your population.
The evolution consists of the following stages:
crossover
between all 1,…,n indivials. Each individual will not becrossover
ed with itself, but as the crossover is not neccessarily symmetricindivdual_a.crossover(indivual_b)
as well asindividual_b.crossover(individual_a)
will be computed.mutate
is applied to all individuals.
§Arguments
mutate_prob
- The probabilty of an inviduals beeing mutated. Is applied viaindividuals.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.