simple/simple.rs
1// Copyright 2016 Martin Ankerl.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9extern crate differential_evolution;
10
11use differential_evolution::self_adaptive_de;
12
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}