pub fn self_adaptive_de<F>(
min_max_pos: Vec<(f32, f32)>,
cost_function: F,
) -> Population<F, XorShiftRng>Expand description
Convenience function to create a fully configured self adaptive differential evolution population.
Examples found in repository?
examples/simple.rs (lines 16-19)
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 32)
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}