From 013321a16fb46c5eae5a2b3156d60bc3501a873f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Mon, 14 Oct 2013 16:57:17 +0200 Subject: [PATCH] Solve dejongf3 with simulated annealing. --- Makefile | 1 + examples/dejongf2_simulated_annealing.rs | 3 -- examples/dejongf3.rs | 2 +- examples/dejongf3_simulated_annealing.rs | 57 ++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 examples/dejongf3_simulated_annealing.rs diff --git a/Makefile b/Makefile index 975802f..a06e81f 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ all: rustc --lib --opt-level=3 src/lib.rs -L$(nalgebra_lib_path) --out-dir lib $(build_ex_cmd) examples/dejong_simulated_annealing.rs $(build_ex_cmd) examples/dejongf2_simulated_annealing.rs + $(build_ex_cmd) examples/dejongf3_simulated_annealing.rs $(build_ex_cmd) examples/dejong.rs $(build_ex_cmd) examples/michalewicz.rs $(build_ex_cmd) examples/dejongf2.rs diff --git a/examples/dejongf2_simulated_annealing.rs b/examples/dejongf2_simulated_annealing.rs index 4d361c7..9f1a633 100644 --- a/examples/dejongf2_simulated_annealing.rs +++ b/examples/dejongf2_simulated_annealing.rs @@ -10,9 +10,6 @@ fn main() { let mut x: Vec2 = rand::random(); x = x * 5.0; - println!("Initial energy: {}", dejongf2(&Vec2::new(1.0, 1.0))); - println!("Initial energy: {}", dejongf2(&Vec2::new(0.0, 0.0))); - simulated_annealing::solve( 200000000000, 1.0, diff --git a/examples/dejongf3.rs b/examples/dejongf3.rs index ce53ed0..4f9ce58 100644 --- a/examples/dejongf3.rs +++ b/examples/dejongf3.rs @@ -31,7 +31,7 @@ fn bench(name: &str, thresh: f64, ga: &mut GeneticAlgorithm, fit_fn: &fn(co #[main] fn main() { - let mut ga: GeneticAlgorithm = GeneticAlgorithm::new(3000, 2, 0.8, 60.0, 100); + let mut ga: GeneticAlgorithm = GeneticAlgorithm::new(3000, 20, 0.8, 60.0, 100); let bmax = 5.12 * 2.0; do bench("De Jong f3", 9.0, &mut ga) |c| { // Restrict to hypercube -5.12 <= x <= 5.12 diff --git a/examples/dejongf3_simulated_annealing.rs b/examples/dejongf3_simulated_annealing.rs new file mode 100644 index 0000000..6688ffc --- /dev/null +++ b/examples/dejongf3_simulated_annealing.rs @@ -0,0 +1,57 @@ +extern mod nalgebra; +extern mod extra; +extern mod frog; + +use std::rand; +use nalgebra::na::{DVec, Norm, Iterable}; +use frog::simulated_annealing; + +fn main() { + let n = 20; // number of dimensions + + let mut x = (DVec::new_random(n) - 0.5) * 10.0; + + println!("Initial energy: {}", dejong(&x)); + + simulated_annealing::solve( + 2000000, + 25.0, + 100, + 25, + 3, + &mut x, + dejong, + neighbor); + + println!("Solution: {:?}", x); + println!("Final energy: {}", dejong(&x)); +} + +fn dejong(x: &DVec) -> f64 { + let res = + do x.iter().fold(0i) |a, e| { + // return a ridiculously high value when we are outside of the domain. + if *e < -5.12 || *e > 5.12 { + 66666 + } + else { + a + *e as int + } + }; + + res as f64 +} + +fn neighbor(curr: &mut DVec, accept: &fn(&DVec) -> bool) { + let mut direction: DVec = DVec::new_random(curr.len()) - 0.5; + + direction.normalize(); + + let step: f64 = 1.0; + let ammount = step * rand::random(); + let next = (direction * ammount) + *curr; + + if accept(&next) { + *curr = next; + } +}