pub struct Population<T: Individual> {
pub num_of_individuals: u32,
pub population: Vec<IndividualWrapper<T>>,
pub reset_limit: u32,
pub reset_limit_start: u32,
pub reset_limit_end: u32,
pub reset_limit_increment: u32,
pub reset_counter: u32,
pub id: u32,
pub fitness_counter: u64,
}Expand description
The Population type. Contains the actual individuals (through a wrapper) and informations
like the reset_limit. Use the PopulationBuilder in your main program to create populations.
Fields§
§num_of_individuals: u32The number of individuals for this population.
population: Vec<IndividualWrapper<T>>The actual population (vector of individuals).
reset_limit: u32The amount of iteration to wait until all individuals will be resetted.
This calls the reset method for each individual.
reset_limit_start: u32The start value of the reset limit.
reset_limit_end: u32The end value of the reset limit, if reset_limit >= reset_limit_end, then the reset_limit
will be resettet to the start value reset_limit_start.
If reset_limit_end == 0, this feature will be disabled.
reset_limit_increment: u32The increment for the reset_limit. After the reset_limit value is reached, it will be
increased by the value of reset_limit_increment.
reset_counter: u32The reset counter, if reset_counter >= reset_limit, all the individuals are discarded and
the simulation restarts anew with an increased reset_limit. This prevents local minima,
but also discards the current fittest individual.
id: u32The ID of the population, only used for statistics. For example: which population does have the most fittest individuals ? This may help you to set the correct parameters for your simulations.
fitness_counter: u64Count how often this population has created (found) the fittest individual. This may help you to fine tune the parameters for the population and the simulation in general.
Implementations§
Source§impl<T: Individual + Send + Sync + Clone> Population<T>
impl<T: Individual + Send + Sync + Clone> Population<T>
Sourcepub fn calculate_fitness(&mut self)
pub fn calculate_fitness(&mut self)
Just calculates the fitness for each individual.
Usually this is the most computational expensive operation, so optimize the
calculate_fitness method of your data structure ;-)
Sourcepub fn run_body(&mut self)
pub fn run_body(&mut self)
This is the body that gets called for every iteration. This function does the following:
-
Check if the reset limit is reached. If it is, this whole population is discarded and re-initialized from the start. All the information about the current fittest individual is lost. This is done to avoid local minima.
-
Clone the current population.
-
Mutate the current population using the
mutate_populationfunction. -
Merge the newly mutated population and the original cloned population into one big population twice the size.
-
Sort this new big population by fitness. So the fittest individual is at position 0.
-
Truncated the big population to its original size and thus gets rid of all the less fittest individuals (they “die”).
-
Check if the fittest individual (at index 0) in the current sorted population is better (= fitter) than the global fittest individual of the whole simulation. If yes, the global fittest individual is replaced.
-
Calculate the new improvement factor and prepare for the next iteration.
Trait Implementations§
Source§impl<T: Clone + Individual> Clone for Population<T>
impl<T: Clone + Individual> Clone for Population<T>
Source§fn clone(&self) -> Population<T>
fn clone(&self) -> Population<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more