pub struct Gas<const N: usize, const NSYMS: usize> {
pub fitness: FitnessConfig<N, NSYMS>,
pub constraints: ConstraintConfig<N, NSYMS>,
pub crossovers: CrossoverConfig<N, NSYMS>,
pub mutations: MutationConfig<N, NSYMS>,
pub cycle_tournament: Box<dyn Tournament<N, NSYMS> + Send + Sync>,
pub final_tournament: Box<dyn Tournament<N, NSYMS> + Send + Sync>,
pub taboo_distance: usize,
pub population_size: usize,
}
Expand description
see module documentation
Fields
fitness: FitnessConfig<N, NSYMS>
the set of fitness functions that turn a chromosone into a set of fitness scores
constraints: ConstraintConfig<N, NSYMS>
constraints determine whether chromosones are valid or invalid
crossovers: CrossoverConfig<N, NSYMS>
crossovers and constraints are the heart of a genetic algorithm.
mutations: MutationConfig<N, NSYMS>
crossovers and constraints are the heart of a genetic algorithm.
cycle_tournament: Box<dyn Tournament<N, NSYMS> + Send + Sync>
this is the tournament used in the algorithm, so is typically called millions of times. faster, less accurate tournaments may therefore provide better results due to their speedup.
final_tournament: Box<dyn Tournament<N, NSYMS> + Send + Sync>
used at the end of a cycle, a comprehensive tournament is best
taboo_distance: usize
to ensure genetic diversity, the hamming distance between any two chromosones in the population must be at least this value
population_size: usize
Implementations
sourceimpl<const N: usize, const NSYMS: usize> Gas<N, NSYMS>
impl<const N: usize, const NSYMS: usize> Gas<N, NSYMS>
- Given a population, run multiple [Gas.generation]’s of the algorithm until it stagnates and then return the winner.
- This function is designed to be run in a thread, it keeps the CycleProgress
- structure updated which allows the function to be monitored on the fly.
- There are four phases to the algorithm.
-
- Seeding: Starting with a set of random candidates, iterate [Gas.generation] until the top
- candidate either has zero constraint violations or the number of constraint
- violations has stabilized. Add the candidate to the seed and then restart
- with another set of random candidates. Repeat until you’ve accumulated
- population_size number of seeds.
-
- Running: Iterate [Gas.generation] until both the score and number of violations has stagnated.
-
- Stagnated: Keep running the GA, doing a biased sampling of the winners
- until we’ve got enough samples or we’ve been doing this step for too long.
-
- Do a final tournament of the winners do get the grand winner, which we return.
pub fn cycle(
&self,
progress: &mut CycleProgress<N, NSYMS>
) -> Candidate<N, NSYMS>
sourceimpl<const N: usize, const NSYMS: usize> Gas<N, NSYMS>
impl<const N: usize, const NSYMS: usize> Gas<N, NSYMS>
Given one generation of candidates, create the next generation. The heart of the GA.
Each generation consists of the following steps:
- Run a Tournament to order the candidates.
- Loop for each new child: a. Select two parents. Parent selection is biased by Tournament score and prefers selecting dissimilar parents. b. Choose a Crossover algorithm to run on the two parents to create a child. c. Choose a Mutation algorithm to run on the child
These arguments could be calculated inside this function rather than outside, but are taken as parameters so they don’t have to be recalculated every generation:
rng
: passRando::new()
score_weights
: passFitnessConfig::weights()
fromGas::fitness
Auto Trait Implementations
impl<const N: usize, const NSYMS: usize> !RefUnwindSafe for Gas<N, NSYMS>
impl<const N: usize, const NSYMS: usize> Send for Gas<N, NSYMS>
impl<const N: usize, const NSYMS: usize> Sync for Gas<N, NSYMS>
impl<const N: usize, const NSYMS: usize> Unpin for Gas<N, NSYMS>
impl<const N: usize, const NSYMS: usize> !UnwindSafe for Gas<N, NSYMS>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more