Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve dejongf3 with simulated annealing. #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions examples/dejongf2_simulated_annealing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ fn main() {
let mut x: Vec2<f64> = 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,
Expand Down
2 changes: 1 addition & 1 deletion examples/dejongf3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn bench(name: &str, thresh: f64, ga: &mut GeneticAlgorithm<f64>, fit_fn: &fn(co

#[main]
fn main() {
let mut ga: GeneticAlgorithm<f64> = GeneticAlgorithm::new(3000, 2, 0.8, 60.0, 100);
let mut ga: GeneticAlgorithm<f64> = 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
Expand Down
57 changes: 57 additions & 0 deletions examples/dejongf3_simulated_annealing.rs
Original file line number Diff line number Diff line change
@@ -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>) -> 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<f64>, accept: &fn(&DVec<f64>) -> bool) {
let mut direction: DVec<f64> = 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;
}
}