Struct Population

Source
pub struct Population<F, R>
where F: Fn(&[f32]) -> f32, R: Rng,
{ /* private fields */ }
Expand description

Holds the population for the differential evolution based on the given settings.

Implementations§

Source§

impl<F, R> Population<F, R>
where F: Fn(&[f32]) -> f32, R: Rng,

Source

pub fn new(s: Settings<F, R>) -> Population<F, R>

Creates a new population based on the given settings.

Source

pub fn best(&self) -> Option<(f32, &[f32])>

Gets a tuple of the best cost and best position found so far.

Examples found in repository?
examples/simple.rs (line 25)
13fn main() {
14    // create a self adaptive DE with an inital search area
15    // from -10 to 10 in 5 dimensions.
16    let mut de = self_adaptive_de(vec![(-10.0, 10.0); 5], |pos| {
17        // cost function to minimize: sum of squares
18        pos.iter().fold(0.0, |sum, x| sum + x*x)
19    });
20
21    // perform 10000 cost evaluations
22    de.iter().nth(10000);
23    
24    // show the result
25    let (cost, pos) = de.best().unwrap();
26    println!("cost: {}", cost);
27    println!("pos: {:?}", pos);
28}
More examples
Hide additional examples
examples/rastrigin.rs (line 41)
23fn main() {
24    // command line args: dimension, number of evaluations
25    let args: Vec<String> = env::args().collect();
26    let dim = args[1].parse::<usize>().unwrap();
27
28    // initial search space for each dimension
29    let initial_min_max = vec![(-5.12, 5.12); dim];
30
31    // initialize differential evolution
32    let mut de = self_adaptive_de(initial_min_max, rastrigin);
33
34    // perform optimization for a maximum of 100000 cost evaluations,
35    // or until best cost is below 0.1.
36    de.iter().take(100000).find(|&cost| cost < 0.1);
37
38    // see what we've found
39    println!("{} evaluations done", de.num_cost_evaluations());
40    
41    let (cost, pos) = de.best().unwrap();
42    println!("{} best cost", cost);
43    println!("{:?} best position", pos);
44}
Source

pub fn num_cost_evaluations(&self) -> usize

Gets the total number of times the cost function has been evaluated.

Examples found in repository?
examples/rastrigin.rs (line 39)
23fn main() {
24    // command line args: dimension, number of evaluations
25    let args: Vec<String> = env::args().collect();
26    let dim = args[1].parse::<usize>().unwrap();
27
28    // initial search space for each dimension
29    let initial_min_max = vec![(-5.12, 5.12); dim];
30
31    // initialize differential evolution
32    let mut de = self_adaptive_de(initial_min_max, rastrigin);
33
34    // perform optimization for a maximum of 100000 cost evaluations,
35    // or until best cost is below 0.1.
36    de.iter().take(100000).find(|&cost| cost < 0.1);
37
38    // see what we've found
39    println!("{} evaluations done", de.num_cost_evaluations());
40    
41    let (cost, pos) = de.best().unwrap();
42    println!("{} best cost", cost);
43    println!("{:?} best position", pos);
44}
Source

pub fn eval(&mut self) -> Option<f32>

Performs a single cost evaluation, and updates best positions and evolves the population if the whole population has been evaluated. Returns the cost value of the current best solution found.

Source

pub fn iter(&mut self) -> PopIter<'_, F, R>

Gets an iterator for this population. Each call to next() performs one cost evaluation.

Examples found in repository?
examples/simple.rs (line 22)
13fn main() {
14    // create a self adaptive DE with an inital search area
15    // from -10 to 10 in 5 dimensions.
16    let mut de = self_adaptive_de(vec![(-10.0, 10.0); 5], |pos| {
17        // cost function to minimize: sum of squares
18        pos.iter().fold(0.0, |sum, x| sum + x*x)
19    });
20
21    // perform 10000 cost evaluations
22    de.iter().nth(10000);
23    
24    // show the result
25    let (cost, pos) = de.best().unwrap();
26    println!("cost: {}", cost);
27    println!("pos: {:?}", pos);
28}
More examples
Hide additional examples
examples/rastrigin.rs (line 36)
23fn main() {
24    // command line args: dimension, number of evaluations
25    let args: Vec<String> = env::args().collect();
26    let dim = args[1].parse::<usize>().unwrap();
27
28    // initial search space for each dimension
29    let initial_min_max = vec![(-5.12, 5.12); dim];
30
31    // initialize differential evolution
32    let mut de = self_adaptive_de(initial_min_max, rastrigin);
33
34    // perform optimization for a maximum of 100000 cost evaluations,
35    // or until best cost is below 0.1.
36    de.iter().take(100000).find(|&cost| cost < 0.1);
37
38    // see what we've found
39    println!("{} evaluations done", de.num_cost_evaluations());
40    
41    let (cost, pos) = de.best().unwrap();
42    println!("{} best cost", cost);
43    println!("{:?} best position", pos);
44}

Auto Trait Implementations§

§

impl<F, R> Freeze for Population<F, R>
where R: Freeze, F: Freeze,

§

impl<F, R> RefUnwindSafe for Population<F, R>

§

impl<F, R> Send for Population<F, R>
where R: Send, F: Send,

§

impl<F, R> Sync for Population<F, R>
where R: Sync, F: Sync,

§

impl<F, R> Unpin for Population<F, R>
where R: Unpin, F: Unpin,

§

impl<F, R> UnwindSafe for Population<F, R>
where R: UnwindSafe, F: UnwindSafe,

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, 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.