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
impl Solution
Sourcepub fn new(function_values: Vec<f64>) -> Self
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]);
Sourcepub fn random<R>(range: R, length: usize) -> Self
pub fn random<R>(range: R, length: usize) -> Self
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);
Sourcepub fn get_arguments(&self) -> Vec<f64>
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 Display for Solution
Represent the Solution by Displaying Solution(x-value, y-value, z-value)
.
impl Display for Solution
Represent the Solution by Displaying Solution(x-value, y-value, z-value)
.
Source§impl Hash for Solution
To hash a solution, use the representation chosen designed in fmt::Display
.
impl Hash for Solution
To hash a solution, use the representation chosen designed in fmt::Display
.
Source§impl<'a> Individual<'a> for Solution
impl<'a> Individual<'a> for Solution
Source§fn mutate(self, prob: f32) -> Self
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
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
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
type IndividualCost = Function
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.
impl PartialEq for Solution
Compare Solutions by converting the floating points values to a 10 decimal places representation as string - then compare the strings.
impl Eq for Solution
Does not need additional implementation, uses the eq
function from
PartialEq
.