From 52c4a859e4a3c89ce4734e4d7094cdae7690d507 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] pre commit --- artificial_bee_colony/src/bee.rs | 53 +++++++++++++++---------------- artificial_bee_colony/src/main.rs | 9 ++++-- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/artificial_bee_colony/src/bee.rs b/artificial_bee_colony/src/bee.rs index 81e17cd..494705f 100644 --- a/artificial_bee_colony/src/bee.rs +++ b/artificial_bee_colony/src/bee.rs @@ -4,10 +4,7 @@ use crate::{food::FoodSource, utils::Input}; pub struct Bee {} impl Bee { - pub fn send_all_employed_bees( - food_sources: &mut [FoodSource], - input:&Input, - ) { + pub fn send_all_employed_bees(food_sources: &mut [FoodSource], input: &Input) { for food_source_index in 0..input.food_source_number { Bee::employed_bee( food_sources, @@ -19,32 +16,32 @@ impl Bee { } } - pub fn send_all_onlooker_bees(food_sources: &mut [FoodSource], - input:&Input,) { + pub fn send_all_onlooker_bees(food_sources: &mut [FoodSource], input: &Input) { let total_fitness = food_sources - .iter() - .map(|food_source| food_source.fitness_calculation) - .sum(); - let onlooker_bee_count = input.food_source_number; - let mut last_looked = 0; - for _ in 0..onlooker_bee_count { - loop { - if last_looked >= input.food_source_number { - last_looked = 0; - } - if Bee::onlooker_bee( - food_sources, - last_looked, - total_fitness, - input.decision_variable_count, - input.upper_bound, - input.lower_bound, - ) { - break; - } - last_looked += 1; + .iter() + .map(|food_source| food_source.fitness_calculation) + .sum(); + + let onlooker_bee_count = input.food_source_number; + let mut where_to_look = 0; + for _ in 0..onlooker_bee_count { + loop { + if where_to_look >= input.food_source_number { + where_to_look = 0; } + if Bee::onlooker_bee( + food_sources, + where_to_look, + total_fitness, + input.decision_variable_count, + input.upper_bound, + input.lower_bound, + ) { + break; + } + where_to_look += 1; } + } } fn employed_bee( @@ -72,7 +69,7 @@ impl Bee { lower_bound: f64, ) -> bool { let fitness_for_index = food_sources[food_source_index].fitness_calculation; - if fitness_for_index / total_fitness <= rand::thread_rng().gen_range(0.0..=1.0) { + if fitness_for_index / total_fitness > rand::thread_rng().gen_range(0.0..=1.0) { Self::send_bee( food_sources, food_source_index, diff --git a/artificial_bee_colony/src/main.rs b/artificial_bee_colony/src/main.rs index 00b58c4..7aea4cc 100644 --- a/artificial_bee_colony/src/main.rs +++ b/artificial_bee_colony/src/main.rs @@ -19,13 +19,16 @@ fn main() { ); let mut best_food_source = FoodSource::find_best_food_source(&food_sources); - for _ in 0..input.iteration { Bee::send_all_employed_bees(&mut food_sources, &input); Bee::send_all_onlooker_bees(&mut food_sources, &input); - - best_food_source = FoodSource::find_best_food_source(&food_sources); + + let candidate_best_food_source = FoodSource::find_best_food_source(&food_sources); + if candidate_best_food_source.fitness_calculation > best_food_source.fitness_calculation + { + best_food_source = candidate_best_food_source; + } let most_tried_food_source_index = FoodSource::find_most_tried_food_source_index(&food_sources);