fix: 🚑 first all employed bees then all onlooker bees

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-11-20 00:20:40 +03:00
parent d694cea4a5
commit b0fb8f40d8
5 changed files with 117 additions and 95 deletions

View file

@ -1,8 +1,8 @@
[start_parameters] [start_parameters]
decision_variable_count = 3 decision_variable_count = 50
food_source_number = 100 food_source_number = 20
food_source_try_limit = 100 food_source_try_limit = 1000
upper_bound = 5 upper_bound = 100
lower_bound = -5 lower_bound = -100
iteration = 1000 iteration = 100
run = 5 run = 30

View file

@ -2,18 +2,18 @@ use rand::Rng;
use crate::food::FoodSource; use crate::food::FoodSource;
pub enum Bee {} pub struct Bee {}
impl Bee { impl Bee {
pub fn employed_bee( pub fn employed_bee(
food_sources: &mut [FoodSource], food_sources: &mut [FoodSource],
index: usize, food_source_index: usize,
decision_variable_count: usize, decision_variable_count: usize,
upper_bound: f64, upper_bound: f64,
lower_bound: f64, lower_bound: f64,
) { ) {
send_bee( Self::send_bee(
food_sources, food_sources,
index, food_source_index,
decision_variable_count, decision_variable_count,
upper_bound, upper_bound,
lower_bound, lower_bound,
@ -22,33 +22,35 @@ impl Bee {
pub fn onlooker_bee( pub fn onlooker_bee(
food_sources: &mut [FoodSource], food_sources: &mut [FoodSource],
index: usize, food_source_index: usize,
total_fitness: f64, total_fitness: f64,
decision_variable_count: usize, decision_variable_count: usize,
upper_bound: f64, upper_bound: f64,
lower_bound: f64, lower_bound: f64,
) { ) -> bool {
let fitness_for_index = food_sources[index].fitness_calculation; 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) {
send_bee( Self::send_bee(
food_sources, food_sources,
index, food_source_index,
decision_variable_count, decision_variable_count,
upper_bound, upper_bound,
lower_bound, lower_bound,
); );
return true;
} }
false
} }
pub fn scout_bee( pub fn scout_bee(
food_sources: &mut [FoodSource], food_sources: &mut [FoodSource],
most_tried_index: usize, most_tried_food_source_index: usize,
limit: u128, limit: u128,
lower_bound: f64, lower_bound: f64,
upper_bound: f64, upper_bound: f64,
decision_variable_count: usize, decision_variable_count: usize,
) { ) {
if food_sources[most_tried_index].try_counter > limit { if food_sources[most_tried_food_source_index].try_counter > limit {
let mut coordinates_for_new = vec![]; let mut coordinates_for_new = vec![];
for _ in 0..decision_variable_count { for _ in 0..decision_variable_count {
let random = lower_bound let random = lower_bound
@ -56,41 +58,51 @@ impl Bee {
coordinates_for_new.push(random); coordinates_for_new.push(random);
} }
let new_food_source = FoodSource::new(coordinates_for_new); let new_food_source = FoodSource::new(coordinates_for_new);
food_sources[most_tried_index] = new_food_source; food_sources[most_tried_food_source_index] = new_food_source;
}
} }
} }
fn send_bee( fn send_bee(
food_sources: &mut [FoodSource], food_sources: &mut [FoodSource],
index: usize, food_source_index: usize,
decision_variable_count: usize, decision_variable_count: usize,
upper_bound: f64, upper_bound: f64,
lower_bound: f64, lower_bound: f64,
) { ) {
let mut different_food_source_index = rand::thread_rng().gen_range(0..food_sources.len()); let mut different_food_source_index = rand::thread_rng().gen_range(0..food_sources.len());
while different_food_source_index == index { while different_food_source_index == food_source_index {
different_food_source_index = rand::thread_rng().gen_range(0..food_sources.len()); different_food_source_index = rand::thread_rng().gen_range(0..food_sources.len());
} }
let selected_coordinate_index = rand::thread_rng().gen_range(0..decision_variable_count); let decision_variable_index = rand::thread_rng().gen_range(0..decision_variable_count);
let randomness = rand::thread_rng().gen_range(-1.0..=1.0); let randomness = rand::thread_rng().gen_range(-1.0..=1.0);
let mut candidate_one_index = food_sources[index].coordinates[selected_coordinate_index] let mut candidate_decision_variable = food_sources[food_source_index].coordinates
[decision_variable_index]
+ randomness + randomness
* (food_sources[index].coordinates[selected_coordinate_index] * (food_sources[food_source_index].coordinates[decision_variable_index]
- food_sources[different_food_source_index].coordinates[selected_coordinate_index]); - food_sources[different_food_source_index].coordinates
if candidate_one_index > upper_bound { [decision_variable_index]);
candidate_one_index = upper_bound; if candidate_decision_variable > upper_bound {
candidate_decision_variable = upper_bound;
} }
if candidate_one_index < lower_bound { if candidate_decision_variable < lower_bound {
candidate_one_index = lower_bound; candidate_decision_variable = lower_bound;
}
let candidate_food_source = {
let mut original_decision_variables =
food_sources[food_source_index].coordinates.clone();
original_decision_variables[decision_variable_index] = candidate_decision_variable;
let candidate_decision_variables = original_decision_variables;
FoodSource::new(candidate_decision_variables)
};
food_sources[food_source_index].try_counter += 1;
if candidate_food_source.fitness_calculation
> food_sources[food_source_index].fitness_calculation
{
food_sources[food_source_index] = candidate_food_source;
food_sources[food_source_index].try_counter = 0;
} }
let mut candidate_coordinates = food_sources[index].coordinates.clone();
candidate_coordinates[selected_coordinate_index] = candidate_one_index;
let candidate = FoodSource::new(candidate_coordinates);
food_sources[index].try_counter += 1;
if candidate.fitness_calculation > food_sources[index].fitness_calculation {
food_sources[index] = candidate;
food_sources[index].try_counter = 0;
} }
} }

View file

@ -40,7 +40,7 @@ impl FoodSource {
} }
pub fn create_food_sources( pub fn create_food_sources(
food_source_number: u128, food_source_number: usize,
decision_variable_count: usize, decision_variable_count: usize,
upper_bound: f64, upper_bound: f64,
lower_bound: f64, lower_bound: f64,
@ -58,13 +58,22 @@ impl FoodSource {
food_sources food_sources
} }
pub fn find_best_food_source(food_sources: &Vec<FoodSource>) -> FoodSource { pub fn find_best_food_source(food_sources: &[FoodSource]) -> FoodSource {
food_sources food_sources
.iter() .iter()
.max_by(|x, y| x.fitness_calculation.total_cmp(&y.fitness_calculation)) .max_by(|x, y| x.fitness_calculation.total_cmp(&y.fitness_calculation))
.unwrap() .unwrap()
.clone() .clone()
} }
pub fn find_most_tried_food_source_index(food_sources: &[FoodSource]) -> usize {
let (most_tried_food_source_index, _) = food_sources
.iter()
.enumerate()
.max_by(|(_, x), (_, y)| x.try_counter.cmp(&y.try_counter))
.unwrap();
most_tried_food_source_index
}
} }
impl fmt::Display for FoodSource { impl fmt::Display for FoodSource {

View file

@ -10,61 +10,62 @@ fn main() {
let mut function_results = vec![]; let mut function_results = vec![];
let mut fitness_results = vec![]; let mut fitness_results = vec![];
for run_counter in 0..input.run {
let mut food_sources = FoodSource::create_food_sources( let mut food_sources = FoodSource::create_food_sources(
input.food_source_number, input.food_source_number,
input.decision_variable_count, input.decision_variable_count,
input.upper_bound, input.upper_bound,
input.lower_bound, input.lower_bound,
); );
for run_counter in 0..input.run {
let mut best_food_source = FoodSource::find_best_food_source(&food_sources); let mut best_food_source = FoodSource::find_best_food_source(&food_sources);
for _ in 0..input.iteration { for _ in 0..input.iteration {
for index in 0..input.food_source_number as usize { for food_source_index in 0..input.food_source_number {
Bee::employed_bee( Bee::employed_bee(
&mut food_sources, &mut food_sources,
index, food_source_index,
input.decision_variable_count, input.decision_variable_count,
input.upper_bound, input.upper_bound,
input.lower_bound, input.lower_bound,
); );
} }
let total_fitness = food_sources.iter().map(|food_source|food_source.fitness_calculation).sum();
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; let mut last_looked = 0;
for _ in 0..input.food_source_number { for _ in 0..onlooker_bee_count {
loop {
if last_looked >= input.food_source_number { if last_looked >= input.food_source_number {
last_looked = 0; last_looked = 0;
} }
if Bee::onlooker_bee(
}
for index in 0..input.food_source_number as usize {
Bee::onlooker_bee(
&mut food_sources, &mut food_sources,
index, last_looked,
total_fitness, total_fitness,
input.decision_variable_count, input.decision_variable_count,
input.upper_bound, input.upper_bound,
input.lower_bound, input.lower_bound,
); ) {
break;
}
last_looked += 1;
}
} }
// önce employed biticek sonra onlooker bakacak ve bakılandan tekrar başla
best_food_source = FoodSource::find_best_food_source(&food_sources); best_food_source = FoodSource::find_best_food_source(&food_sources);
for index in 0..food_sources.len() { let most_tried_food_source_index =
if food_sources[index].try_counter >= input.food_source_try_limit { FoodSource::find_most_tried_food_source_index(&food_sources);
// en büyüğü bul sonra limiti geçiyosa kaşif gider
Bee::scout_bee( Bee::scout_bee(
&mut food_sources, &mut food_sources,
index, most_tried_food_source_index,
input.food_source_try_limit, input.food_source_try_limit,
input.lower_bound, input.lower_bound,
input.upper_bound, input.upper_bound,
input.decision_variable_count, input.decision_variable_count,
); );
break;
}
}
} }
function_results.push(best_food_source.function_calculation); function_results.push(best_food_source.function_calculation);
fitness_results.push(best_food_source.fitness_calculation); fitness_results.push(best_food_source.fitness_calculation);

View file

@ -10,7 +10,7 @@ use crate::food::FoodSource;
pub struct Input { pub struct Input {
pub decision_variable_count: usize, pub decision_variable_count: usize,
pub food_source_number: u128, pub food_source_number: usize,
pub food_source_try_limit: u128, pub food_source_try_limit: u128,
pub upper_bound: f64, pub upper_bound: f64,
pub lower_bound: f64, pub lower_bound: f64,