[][src]Struct bbte_optim_tzim1773_genetic::Genetic

pub struct Genetic<'a, T> {
    pub population: usize,
    pub max_generation: usize,
    pub pc: f64,
    pub pm: f64,
    pub get_random_agent: &'a dyn Fn() -> T,
    pub f_fitness: &'a dyn Fn(&T) -> f64,
    pub f_mutate: &'a dyn Fn(&T) -> T,
    pub f_offspring: &'a dyn Fn(&T, &T) -> T,
}

Fields

population: usize

Population size: with increased size comes increased accuracy but decreased speed Suggested value: 100

max_generation: usize

Max generation: with increased generation comes increased accuracy but decreased speed Suggested value: 1000 Depends on the complexity of the task. Bigger tasks require more generations.

pc: f64

Probability of crossover ((never) 0.0 <= pc <= 1.0 (always))

pm: f64

Probability of mutation ((never) 0.0 <= pm <= 1.0 (always))

get_random_agent: &'a dyn Fn() -> T

Function that returns one agent which is used in the 0th generation You can start from a given point, or use a random generator like the rand crate

f_fitness: &'a dyn Fn(&T) -> f64

Function that evaluates an agent and returns it's fitness (this algorithm maximises this function)

f_mutate: &'a dyn Fn(&T) -> T

Function that mutates an agent and returns the mutated version of it

f_offspring: &'a dyn Fn(&T, &T) -> T

Function that crossovers two agents and creates an offspring

Methods

impl<'a, T> Genetic<'a, T> where
    T: Clone
[src]

pub fn get_best(&self, u: &Vec<T>) -> usize[src]

Returns the index of the best agent from a vector of agents

Arguments:

  • u a vector of agents

Examples:

See at run()

pub fn run(&self) -> Vec<T>[src]

Returns agents from the given generation.

Arguments:

  • u a vector of agents

Examples:

use rand::prelude::*;
use bbte_optim_tzim1773_genetic::Genetic;

fn main() {
   let agent = || 123;
   let fit = |_a: &usize| 1.0;
   let muta = |a: &usize| *a + 1;
   let off = |a: &usize, b: &usize| (*a + *b) / 2;
   let test: Genetic<usize> = Genetic {
       population: 10,
       max_generation: 1,
       pc: 0.5,
       pm: 1.0,
       get_random_agent: &agent,
       f_fitness: &fit,
       f_mutate: &muta,
       f_offspring: &off,
   };
    
   let pop = test.run();
   println!("{}", pop[0]); // since all agents are mutated (pm = 1.0)
                            // all agents should hold the value 124
}

Maximising the -x^2 + 5 function:

use rand::prelude::*;
use bbte_optim_tzim1773_genetic::Genetic;

fn main() {
    let agent = || {
        let mut rng = thread_rng();
        rng.gen_range(-5.0, 5.0)
    };
    let fit = |a: &f64| 5.0 - a * a;
    let muta = |a: &f64| {
        let mut rng = thread_rng();
        *a + rng.gen_range(-0.01, 0.01)
    };
    let off = |a: &f64, b: &f64| (*a + *b) / 2.0;
    let test: Genetic<f64> = Genetic {
        population: 100,
        max_generation: 20,
        pc: 0.5,
        pm: 0.4,
        get_random_agent: &agent,
        f_fitness: &fit,
        f_mutate: &muta,
        f_offspring: &off,
    };

    let simul = test.run();
    let best = test.get_best(&simul);

    println!("{}", simul[best]); // should be a number close to 0
}

Auto Trait Implementations

impl<'a, T> Unpin for Genetic<'a, T>

impl<'a, T> !Sync for Genetic<'a, T>

impl<'a, T> !Send for Genetic<'a, T>

impl<'a, T> !UnwindSafe for Genetic<'a, T>

impl<'a, T> !RefUnwindSafe for Genetic<'a, T>

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

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