Struct Solution

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

The Solution is an individual for using genetic algorithm to approximate functions. It contains the specific function values.

Implementations§

Source§

impl Solution

Source

pub fn new(function_values: Vec<f64>) -> Self

Create a new Solution based on function values x,y and z.

§Arguments
  • x - The value of x that this solution represents.
  • y - The value of y that this solution represents.
  • z - The value of z that this solution represents.
§Examples
use genetic_algorithm_fn::solution;
let my_solution = solution::Solution::new(vec![3.0, 4.0, 5.0]);
Source

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

Create a random Solution with with values between or equal min .. max.

§Arguments
  • min - The minimal value of the function arguments in solution.
  • max - The maximal value of the function arguments in solution.
§Examples
use genetic_algorithm_fn::solution;
let random_solution = solution::Solution::random(3.0..10.0, 3);
Source

pub fn get_arguments(&self) -> Vec<f64>

Return the function arguments stored in a solution.

§Examples
use genetic_algorithm_fn::solution;
let simple_solution = solution::Solution::new(vec![1.0, 2.0, 3.0]);
assert_eq!(simple_solution.get_arguments(), vec![1.0, 2.0, 3.0])

Trait Implementations§

Source§

impl Clone for Solution

Source§

fn clone(&self) -> Solution

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 Solution

Source§

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

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

impl Display for Solution

Represent the Solution by Displaying Solution(x-value, y-value, z-value).

Source§

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

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

impl Hash for Solution

To hash a solution, use the representation chosen designed in fmt::Display.

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a> Individual<'a> for Solution

Source§

fn mutate(self, prob: f32) -> Self

Mutate the solution by multiplying a random function argument with a factor between 0.8-1.2

§Arguments
  • prob - The probability with which on of the function values will mutated.
§Examples
use genetic_algorithm_fn::solution;
use genetic_algorithm_traits::Individual;

let my_solution = solution::Solution::new(vec![1.0, 2.0, 3.0]);
println!("Solution before mutation: {}, solution after mutation: {}", my_solution, my_solution.clone().mutate(1.0));
Source§

fn crossover(&self, other: &Solution) -> Self

Crossover one solution with another. For a lack of creativity, this is currently just taking the average of the two solutions.

§Arguments
  • other - The other Solution you would like to crossover with.
§Examples
use genetic_algorithm_traits::Individual;
use genetic_algorithm_fn::solution;

let solution_to_crossover = solution::Solution::new(vec![1.0, 2.0, 3.0]);
let solution_to_crossover_with = solution::Solution::new(vec![3.0, 2.0, 1.0]);
println!("{}", solution_to_crossover.crossover(&solution_to_crossover_with));
Source§

fn fitness(&self, function: &Function) -> f64

Compute the fitness of a Solution, that is the specific function value of the Function for the function arguments stored in Solution.

§Arguments
  • function - The function that is should be used to compute of the function value of the solution’s arguments.
§Examples
use genetic_algorithm_fn::solution;
use genetic_algorithm_fn::function;
use genetic_algorithm_traits::Individual;

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 this_solution = solution::Solution::new(vec![2.0, 3.0, 5.0]);
println!("{}", this_solution.fitness(&function_to_optimize));
Source§

type IndividualCost = Function

The Type of cost data this individual is compatible to compute its fitness on.
Source§

impl PartialEq for Solution

Compare Solutions by converting the floating points values to a 10 decimal places representation as string - then compare the strings.

Source§

fn eq(&self, other: &Self) -> 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 Eq for Solution

Does not need additional implementation, uses the eq function from PartialEq.

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