Struct Solutions

Source
pub struct Solutions { /* private fields */ }
Expand description

The Solution is the container for your current pool of solution’s.

Implementations§

Source§

impl Solutions

Source

pub fn random<R>(n_solutions: usize, range: R, length: usize) -> Self
where R: SampleRange<f64> + Clone,

Create a pool of random solutions.

§Arguments
  • n_solutions - The number of solutions your population should contain.
§Examples
use genetic_algorithm_fn::solutions;
println!("{}", solutions::Solutions::random(5, 1.0..10.0, 3));

Trait Implementations§

Source§

impl Clone for Solutions

Source§

fn clone(&self) -> Solutions

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Solutions

Source§

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

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

impl Display for Solutions

Represent the Solution by displaying `Solutions([solution-1, solution-2]).

Source§

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

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

impl From<Vec<Solution>> for Solutions

Source§

fn from(solution: Vec<Solution>) -> Self

Create a new Population from a vector of solutions.

§Arguments
  • solutions - The solutions you collected so far and would like to put into your Solutions.
§Examples
use genetic_algorithm_fn::solutions;
use genetic_algorithm_fn::solution;

let my_solutions = solutions::Solutions::from(vec![
    solution::Solution::new(vec![1.0, 2.0, 3.0]),
    solution::Solution::new(vec![1.0, 2.0, 4.0])
]);
println!("Current solutions: {}", my_solutions);
Source§

impl PartialEq for Solutions

Source§

fn eq(&self, other: &Solutions) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> Population<'a> for Solutions

Source§

fn get_fittest_population(&self, n: usize, function: &Function) -> Solutions

Given your pool, compute the fitness of your individuals to solve the problem at hand.

§Arguments
  • n - How many individuals to keep?
  • function - The distances between nodes that is neccessary to computes how well the route work in terms of the Function to maximize.
§Examples
use genetic_algorithm_fn::solutions;
use genetic_algorithm_fn::function;
use genetic_algorithm_traits::Population;

let function_to_optimize = function::Function::new(
    |x| match x.len() {
        3 => Ok(x[0] * x[1] * x[2]),
        _ => Err(function::FunctionError::WrongNumberOfEntries {
            actual_number_of_entries: x.len(),
            expected_number_of_entries: 3,
        }),
    }
);
let all_solutions = solutions::Solutions::random(30, 1.0..10.0, 3);
println!("Best 5 solutions: {}", all_solutions.get_fittest_population(5, &function_to_optimize));
Source§

fn evolve(&self, mutate_prob: f32) -> Solutions

Evolve your population.

The evolution process consists of the following stages:

  1. crossover between all 1,…,n solutions excluding the solution itself.
  2. mutate is applied to all individuals.
§Arguments
  • mutate_prob - The probabilty of an inviduals beeing mutated. Is applied via individuals.mutate.
§Examples
use genetic_algorithm_fn::solutions;
use genetic_algorithm_traits::Population;

let all_solutions = solutions::Solutions::random(2, 1.0..10.0, 3);
println!("The evolved invdividuals are {}", all_solutions.evolve(0.5));
Source§

fn iter(&'a self) -> Iter<'_, Solution>

Iterate over the individuals of your population.

§Examples
use genetic_algorithm_fn::solutions;
use genetic_algorithm_traits::Population;

let all_solutions = solutions::Solutions::random(5, 1.0..10.0, 3);
all_solutions.iter().map(|solution| println!("{}", solution));
Source§

type Individual = Solution

The Type of individuals your population should consist of.
Source§

type IndividualCollection = Iter<'a, Solution>

The iteratore type if you iterate over your individuals. It depends on the data container you use to store individuals in your implementation of Population.
Source§

fn fitnesses( &'a self, cost_data: &'a <Self::Individual as Individual<'a>>::IndividualCost, ) -> Vec<(f64, &'a Self::Individual)>

Given the pool of current individuals, compute the fitness of your individuals to solve the problem at hand. Read more
Source§

fn get_n_fittest( &'a self, n: usize, cost_data: &'a <Self::Individual as Individual<'a>>::IndividualCost, ) -> Vec<Self::Individual>

Get the n fittest individuals in your population. Read more
Source§

fn evolve_individuals(&'a self, mutate_prob: f32) -> Vec<Self::Individual>

Evolve your population. Read more
Source§

impl StructuralPartialEq for Solutions

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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