From 7d54ed4795617e9199047b1d100f6752fcbaa301 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] feat: :sparkles: now able to run abc multiple times --- artificial_bee_colony/src/lib.rs | 4 ++ artificial_bee_colony/src/main.rs | 95 ++++++++++++++++--------------- 2 files changed, 53 insertions(+), 46 deletions(-) diff --git a/artificial_bee_colony/src/lib.rs b/artificial_bee_colony/src/lib.rs index 498436f..5374b75 100644 --- a/artificial_bee_colony/src/lib.rs +++ b/artificial_bee_colony/src/lib.rs @@ -10,6 +10,7 @@ pub struct Input { pub upper_bound: f64, pub lower_bound: f64, pub iteration: usize, + pub run: usize, } impl Input { pub fn get() -> Self { @@ -31,6 +32,8 @@ impl Input { println!("Iteration"); let iteration = Self::get_input().parse().unwrap(); + println!("Run"); + let run = Self::get_input().parse().unwrap(); Input { decision_variable_count, food_source_number, @@ -38,6 +41,7 @@ impl Input { upper_bound, lower_bound, iteration, + run, } } fn get_input() -> String { diff --git a/artificial_bee_colony/src/main.rs b/artificial_bee_colony/src/main.rs index 87191df..d553b41 100644 --- a/artificial_bee_colony/src/main.rs +++ b/artificial_bee_colony/src/main.rs @@ -10,51 +10,54 @@ fn main() { input.upper_bound, input.lower_bound, ); - let mut best = FoodSource::get(vec![]); - for food_source in &food_sources { - if best.fitness < food_source.fitness { - best.coordinates = food_source.coordinates.clone(); - best.fitness = food_source.fitness; - } - } - for _ in 0..input.iteration { - for index in 0..input.food_source_number as usize { - Bee::worker_bee( - &mut food_sources, - index, - input.decision_variable_count, - input.upper_bound, - input.lower_bound, - ); - let mut total_fitness = 0.0; - for food_source in &food_sources { - total_fitness += food_source.fitness; - } - Bee::onlooker_bee( - &mut food_sources, - index, - total_fitness, - input.decision_variable_count, - input.upper_bound, - input.lower_bound, - ); - let mut most_tried_index = 0; - for i in 0..food_sources.len() { - if food_sources[most_tried_index].try_counter < food_sources[i].try_counter { - most_tried_index = i; - } - } - best = food_sources[most_tried_index].clone(); - Bee::scout_bee( - &mut food_sources, - most_tried_index, - input.food_source_try_limit, - input.lower_bound, - input.upper_bound, - input.decision_variable_count, - ); - } - } - println!("{:#?}", best); + for _ in 0..input.run { + let mut best = FoodSource::get(vec![]); + for food_source in &food_sources { + if best.fitness < food_source.fitness { + best.coordinates = food_source.coordinates.clone(); + best.fitness = food_source.fitness; + } + } + for _ in 0..input.iteration { + for index in 0..input.food_source_number as usize { + Bee::worker_bee( + &mut food_sources, + index, + input.decision_variable_count, + input.upper_bound, + input.lower_bound, + ); + let mut total_fitness = 0.0; + for food_source in &food_sources { + total_fitness += food_source.fitness; + } + Bee::onlooker_bee( + &mut food_sources, + index, + total_fitness, + input.decision_variable_count, + input.upper_bound, + input.lower_bound, + ); + let mut most_tried_index = 0; + for i in 0..food_sources.len() { + if food_sources[most_tried_index].try_counter < food_sources[i].try_counter { + most_tried_index = i; + } + } + best = food_sources[most_tried_index].clone(); + Bee::scout_bee( + &mut food_sources, + most_tried_index, + input.food_source_try_limit, + input.lower_bound, + input.upper_bound, + input.decision_variable_count, + ); + } + } + + println!("{:#?}", best); + } }