pub struct Population<F, R>{ /* private fields */ }Expand description
Holds the population for the differential evolution based on the given settings.
Implementations§
Source§impl<F, R> Population<F, R>
impl<F, R> Population<F, R>
Sourcepub fn new(s: Settings<F, R>) -> Population<F, R>
pub fn new(s: Settings<F, R>) -> Population<F, R>
Creates a new population based on the given settings.
Sourcepub fn best(&self) -> Option<(f32, &[f32])>
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
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}Sourcepub fn num_cost_evaluations(&self) -> usize
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}Sourcepub fn eval(&mut self) -> Option<f32>
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.
Sourcepub fn iter(&mut self) -> PopIter<'_, F, R> ⓘ
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
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>
impl<F, R> RefUnwindSafe for Population<F, R>where
R: RefUnwindSafe,
F: RefUnwindSafe,
impl<F, R> Send for Population<F, R>
impl<F, R> Sync for Population<F, R>
impl<F, R> Unpin for Population<F, R>
impl<F, R> UnwindSafe for Population<F, R>where
R: UnwindSafe,
F: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more