Struct Permutate

Source
pub struct Permutate<G: PermutateGenotype, F: Fitness<Genotype = G>, SR: StrategyReporter<Genotype = G>> {
    pub genotype: G,
    pub fitness: F,
    pub config: PermutateConfig,
    pub state: PermutateState<G>,
    pub reporter: SR,
}
Expand description

All possible combinations of genes are iterated over as chromosomes (unless the genotype seeds specific genes, in which case only those genes are iterated over). The fitness is calculated for each chromosome and the best is taken. For efficiency reasons the full population is never instantiated as a whole.

The chromosome_permutations_size is subject to combinatorial explosion, so check the genotype for practical values before using the Permutate strategy. This will not pose any memory issues, as the permutations are not instantiated at the same time, just iterated over. But it will take forever…

There are reporting hooks in the loop receiving the PermutateState, which can by handled by an StrategyReporter (e.g. PermutateReporterDuration, PermutateReporterSimple). But you are encouraged to roll your own, see StrategyReporter.

See PermutateBuilder for initialization options.

All multithreading mechanisms are implemented using rayon::iter and std::sync::mpsc.

Example:

use genetic_algorithm::strategy::permutate::prelude::*;
use genetic_algorithm::fitness::placeholders::CountTrue;

// the search space
let genotype = BinaryGenotype::builder() // boolean alleles
    .with_genes_size(12)                 // 12 genes per chromosome
    .build()
    .unwrap();

// the search strategy
let permutate = Permutate::builder()
    .with_genotype(genotype)
    .with_fitness(CountTrue)                          // count the number of true values in the chromosomes
    .with_fitness_ordering(FitnessOrdering::Minimize) // aim for the least true values
    .with_par_fitness(true)                           // optional, defaults to false, use parallel fitness calculation
    .with_reporter(PermutateReporterSimple::new(100)) // optional builder step, report every 100 generations
    .call()
    .unwrap();

// it's all about the best genes after all
let (best_genes, best_fitness_score) = permutate.best_genes_and_fitness_score().unwrap();
assert_eq!(best_genes, vec![false; 12]);
assert_eq!(best_fitness_score, 0);

Fields§

§genotype: G§fitness: F§config: PermutateConfig§state: PermutateState<G>§reporter: SR

Implementations§

Source§

impl<G: PermutateGenotype, F: Fitness<Genotype = G>, SR: StrategyReporter<Genotype = G>> Permutate<G, F, SR>
where G::Chromosome: GenesOwner<Genes = G::Genes>,

Source§

impl<G: PermutateGenotype, F: Fitness<Genotype = G>> Permutate<G, F, StrategyReporterNoop<G>>

Source§

impl<G: PermutateGenotype, F: Fitness<Genotype = G>, SR: StrategyReporter<Genotype = G>> Permutate<G, F, SR>

Source

pub fn setup(&mut self)

Source

pub fn cleanup(&mut self)

Trait Implementations§

Source§

impl<G: PermutateGenotype, F: Fitness<Genotype = G>, SR: StrategyReporter<Genotype = G>> Display for Permutate<G, F, SR>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<G: PermutateGenotype, F: Fitness<Genotype = G>, SR: StrategyReporter<Genotype = G>> Strategy<G> for Permutate<G, F, SR>

Source§

fn call(&mut self)

Source§

fn best_generation(&self) -> usize

Source§

fn best_fitness_score(&self) -> Option<FitnessValue>

Source§

fn best_genes(&self) -> Option<G::Genes>

Source§

fn flush_reporter(&mut self, output: &mut Vec<u8>)

strategy can be boxed, need a way to get to the reporter
Source§

fn best_genes_and_fitness_score(&self) -> Option<(G::Genes, FitnessValue)>

Source§

impl<G: PermutateGenotype, F: Fitness<Genotype = G>, SR: StrategyReporter<Genotype = G>> TryFrom<Builder<G, F, SR>> for Permutate<G, F, SR>

Source§

type Error = TryFromStrategyBuilderError

The type returned in the event of a conversion error.
Source§

fn try_from(builder: PermutateBuilder<G, F, SR>) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<G, F, SR> Freeze for Permutate<G, F, SR>
where G: Freeze, F: Freeze, SR: Freeze, <G as Genotype>::Chromosome: Freeze,

§

impl<G, F, SR> RefUnwindSafe for Permutate<G, F, SR>

§

impl<G, F, SR> Send for Permutate<G, F, SR>

§

impl<G, F, SR> Sync for Permutate<G, F, SR>
where <G as Genotype>::Chromosome: Sync,

§

impl<G, F, SR> Unpin for Permutate<G, F, SR>
where G: Unpin, F: Unpin, SR: Unpin, <G as Genotype>::Chromosome: Unpin,

§

impl<G, F, SR> UnwindSafe for Permutate<G, F, SR>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V