From 3f32c049c9ba29ca59828d02a473c16c000859ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=9CM=C3=9C=C5=9E?= <96421894+Tahinli@users.noreply.github.com> Date: Wed, 20 Nov 2024 00:20:40 +0300 Subject: [PATCH] fix: :bug: negative values cause problem --- .gitignore | 1 + simulated_annealing/src/main.rs | 69 ++++++++++++++++++++++++++------- 2 files changed, 55 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 6985cf1..0d389bf 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ # will have compiled files and executables debug/ target/ +.vscode/ # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html diff --git a/simulated_annealing/src/main.rs b/simulated_annealing/src/main.rs index 7d255b9..94786f9 100644 --- a/simulated_annealing/src/main.rs +++ b/simulated_annealing/src/main.rs @@ -30,23 +30,54 @@ fn main() { for i in 0..max_iteration { x1 = bound_detective(x1, upper_bound, lower_bound); x2 = bound_detective(x2, upper_bound, lower_bound); - let new_x1: f64; let new_x2: f64; let where_to_go = rand::thread_rng().gen_range(0.0..=1.0); if where_to_go > 0.7 { - new_x1 = rand::thread_rng().gen_range(x1..=(x1 + ((5.0 * x1) / 100.0))); - new_x2 = rand::thread_rng().gen_range(x2..=(x2 + ((5.0 * x2) / 100.0))); + if x1 >= 0.0 { + new_x1 = rand::thread_rng().gen_range(x1..=(x1 + ((5.0 * x1) / 100.0))); + } else { + new_x1 = rand::thread_rng().gen_range(x1..=(x1 - ((5.0 * x1) / 100.0))); + } + if x2 >= 0.0 { + new_x2 = rand::thread_rng().gen_range(x2..=(x2 + ((5.0 * x2) / 100.0))); + } else { + new_x2 = rand::thread_rng().gen_range(x2..=(x2 - ((5.0 * x2) / 100.0))); + } } else if where_to_go > 0.5 { - new_x1 = rand::thread_rng().gen_range(x1..=(x1 + ((5.0 * x1) / 100.0))); - new_x2 = rand::thread_rng().gen_range((x2 - ((5.0 * x2) / 100.0))..=x2); + if x1 >= 0.0 { + new_x1 = rand::thread_rng().gen_range(x1..=(x1 + ((5.0 * x1) / 100.0))); + } else { + new_x1 = rand::thread_rng().gen_range(x1..=(x1 - ((5.0 * x1) / 100.0))); + } + if x2 >= 0.0 { + new_x2 = rand::thread_rng().gen_range((x2 - ((5.0 * x2) / 100.0))..=x2); + } else { + new_x2 = rand::thread_rng().gen_range((x2 + ((5.0 * x2) / 100.0))..=x2); + } } else if where_to_go > 0.3 { - new_x1 = rand::thread_rng().gen_range((x1 - ((5.0 * x1) / 100.0))..=x1); - new_x2 = rand::thread_rng().gen_range(x2..=(x2 + ((5.0 * x2) / 100.0))); + if x1 >= 0.0 { + new_x1 = rand::thread_rng().gen_range((x1 - ((5.0 * x1) / 100.0))..=x1); + } else { + new_x1 = rand::thread_rng().gen_range((x1 + ((5.0 * x1) / 100.0))..=x1); + } + if x2 >= 0.0 { + new_x2 = rand::thread_rng().gen_range(x2..=(x2 + ((5.0 * x2) / 100.0))); + } else { + new_x2 = rand::thread_rng().gen_range(x2..=(x2 - ((5.0 * x2) / 100.0))); + } } else { - new_x1 = rand::thread_rng().gen_range((x1 - ((5.0 * x1) / 100.0))..=x1); - new_x2 = rand::thread_rng().gen_range((x2 - ((5.0 * x2) / 100.0))..=x2); + if x1 >= 0.0 { + new_x1 = rand::thread_rng().gen_range((x1 - ((5.0 * x1) / 100.0))..=x1); + } else { + new_x1 = rand::thread_rng().gen_range((x1 + ((5.0 * x1) / 100.0))..=x1); + } + if x2 >= 0.0 { + new_x2 = rand::thread_rng().gen_range((x2 - ((5.0 * x2) / 100.0))..=x2); + } else { + new_x2 = rand::thread_rng().gen_range((x2 + ((5.0 * x2) / 100.0))..=x2); + } } let give_up_return = give_up(x1, x2, new_x1, new_x2, give_up_counter); @@ -84,9 +115,9 @@ fn calculate(x1: f64, x2: f64) -> f64 { } fn bound_detective(x: f64, upper_bound: f64, lower_bound: f64) -> f64 { - if x > upper_bound { + if x >= upper_bound { magic_trick(upper_bound, Bound::Upper) - } else if x < lower_bound { + } else if x <= lower_bound { magic_trick(lower_bound, Bound::Lower) } else { magic_trick(x, Bound::Nothing) @@ -94,10 +125,18 @@ fn bound_detective(x: f64, upper_bound: f64, lower_bound: f64) -> f64 { } fn magic_trick(x: f64, what_happened: Bound) -> f64 { - match what_happened { - Bound::Upper => x + (-0.000000001 * x / 100.0), - Bound::Lower => x + (0.000000001 * x / 100.0), - Bound::Nothing => x, + if x >= 0.0 { + match what_happened { + Bound::Upper => return x - (0.000000001 * x / 100.0), + Bound::Lower => return x + (0.000000001 * x / 100.0), + Bound::Nothing => return x, + } + } else { + match what_happened { + Bound::Upper => return x + (0.000000001 * x / 100.0), + Bound::Lower => return x - (0.000000001 * x / 100.0), + Bound::Nothing => return x, + } } }