pub struct ContinuousTrainer<'scope, G> {
pub gene_pool: Arc<RwLock<Vec<(G, f32)>>>,
pub children_created: usize,
pub mutation_rate: f32,
pub reproduction_type_proportion: f32,
/* private fields */
}Expand description
This is one of two basic genetic algorithms in this crate, the “continuous” strategy.
This is an alternative genetic algorithm implementation compared to the standard stochastic, generation-based strategy. This trainer runs its training continuously, nonstop, with no definable “break” in between generations. New genes are constantly being reproduced, crossbred, mutated, evaluated, and ranked while the trainer runs, using a multithreaded pool of workers.
As it’s a more nonstandard training strategy, the allowed criteria for
determining when to print population reports and when to end training are more flexible than in the
typical evolutionary trainer, allowing the user to define exactly what criteria they want to
pay attention to during the training process. You can access these more detailed and complex
controls by calling the ContinuousTrainer::train_custom function. If you would like a simpler
interface with simple, default training criteria and reporting, just call ContinuousTrainer::train.
Fields§
§gene_pool: Arc<RwLock<Vec<(G, f32)>>>A collection of all the genes in the population and their fitness score, sorted descending by fitness.
Because of the continuous nature of the trainer,
the collection is behind an Arc<RwLock<_>> combo.
children_created: usizeCount of the total number of children reproduced.
mutation_rate: f32The mutation rate of newly reproduced children.
reproduction_type_proportion: f32The proportion of newly reproduced children that are created as a result of crossbreeding vs mutations.
Higher = more crossbreeding, lower = more mutations. Set to 1 to only create new children via crossbreeding, and 0 to only create new children via mutation.
Implementations§
Source§impl<'scope, G> ContinuousTrainer<'scope, G>
impl<'scope, G> ContinuousTrainer<'scope, G>
Sourcepub fn new(
population_size: usize,
mutation_rate: f32,
reproduction_type_proportion: f32,
scope: &'scope Scope<'scope, '_>,
) -> Self
pub fn new( population_size: usize, mutation_rate: f32, reproduction_type_proportion: f32, scope: &'scope Scope<'scope, '_>, ) -> Self
Construct a new trainer with a given population size, mutation rate, and reproduction type proportion.
A reference to a thread::Scope must be passed in order
to spawn the child worker threads for the lifetime of the trainer.
Sourcepub fn submit_job(&mut self, gene: G)
pub fn submit_job(&mut self, gene: G)
Submit a new genome to the worker pool to be evaluated for its fitness and ranked among the population.
Used internally by the training process, should typically not be called directly unless the user knows what they’re doing.
Sourcepub fn seed<R>(&mut self, rng: &mut R)where
R: RandomSource,
G: Genome,
pub fn seed<R>(&mut self, rng: &mut R)where
R: RandomSource,
G: Genome,
Seed the population with new genes up to the current population cap.
This is called automatically at the start of training, so should typically not need to be called directly.
A RandomSource must be passed as a source of randomness
for generating the initial population.
Sourcepub fn train<R>(&mut self, num_children: usize, rng: &mut R) -> G
pub fn train<R>(&mut self, num_children: usize, rng: &mut R) -> G
Begin training, finishing once num_children children have been
reproduced, ranked for fitness, and introduced into the population.
A RandomSource must be passed as a source of randomness
for mutating genes to produce new offspring.
Sourcepub fn train_custom<R>(
&mut self,
train_criteria: impl FnMut(TrainingCriteriaMetrics) -> bool,
reporting_strategy: Option<TrainingReportStrategy<impl FnMut(TrainingCriteriaMetrics) -> bool, impl FnMut(TrainingStats)>>,
rng: &mut R,
) -> G
pub fn train_custom<R>( &mut self, train_criteria: impl FnMut(TrainingCriteriaMetrics) -> bool, reporting_strategy: Option<TrainingReportStrategy<impl FnMut(TrainingCriteriaMetrics) -> bool, impl FnMut(TrainingStats)>>, rng: &mut R, ) -> G
Begin training with detailed custom parameters.
Instead of a specific child
count cutoff point, a function train_criteria is passed in, which takes in an
instance of TrainingCriteriaMetrics and outputs a bool. This allows greater
control over exactly what criteria to finish training under.
Additionally, the user may pass a reporting_strategy, which determines the conditions
and method under which periodic statistical reporting of the population is performed.
Pass None to disable reporting entirely, otherwise pass Some with an instance of a
TrainingReportStrategy to define the two methods necessary to manage reporting.
To mimic the default reporting strategy, pass the result of default_reporting_strategy() wrapped
in Some().
A RandomSource must be passed as a source of randomness
for mutating genes to produce new offspring.
Sourcepub fn metrics(&self) -> TrainingCriteriaMetrics
pub fn metrics(&self) -> TrainingCriteriaMetrics
Generate training criteria metrics for the current state of this trainer.
This is a strict subset of the data available in an instance of TrainingStats
returned from calling ContinuousTrainer::stats. However, these
metrics were chosen specifically for their computation efficiency, and thus can be
re-evaluated frequently with minimal cost. These metrics are used both to determine
whether or not to continue training, and whether or not to display a report about
training progress.
Sourcepub fn stats(&self) -> TrainingStats
pub fn stats(&self) -> TrainingStats
Generate population stats for the current state of this trainer.
This function is called whenever the reporting strategy is asked to produce a report about the current population, but it may also be called manually here.