pre commit

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-11-20 00:20:40 +03:00
parent dc5c855dd7
commit d694cea4a5
3 changed files with 76 additions and 58 deletions

View file

@ -11,32 +11,13 @@ impl Bee {
upper_bound: f64,
lower_bound: f64,
) {
let mut different_food_source_index = rand::thread_rng().gen_range(0..food_sources.len());
while different_food_source_index == index {
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 randomness = rand::thread_rng().gen_range(-1.0..=1.0);
let mut candidate_one_index = food_sources[index].coordinates[selected_coordinate_index]
+ randomness
* (food_sources[index].coordinates[selected_coordinate_index]
- food_sources[different_food_source_index].coordinates
[selected_coordinate_index]);
if candidate_one_index > upper_bound {
candidate_one_index = upper_bound;
}
if candidate_one_index < lower_bound {
candidate_one_index = lower_bound;
}
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;
}
send_bee(
food_sources,
index,
decision_variable_count,
upper_bound,
lower_bound,
);
}
pub fn onlooker_bee(
@ -49,7 +30,7 @@ impl Bee {
) {
let fitness_for_index = food_sources[index].fitness_calculation;
if fitness_for_index / total_fitness <= rand::thread_rng().gen_range(0.0..=1.0) {
Self::employed_bee(
send_bee(
food_sources,
index,
decision_variable_count,
@ -79,3 +60,37 @@ impl Bee {
}
}
}
fn send_bee(
food_sources: &mut [FoodSource],
index: usize,
decision_variable_count: usize,
upper_bound: f64,
lower_bound: f64,
) {
let mut different_food_source_index = rand::thread_rng().gen_range(0..food_sources.len());
while different_food_source_index == index {
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 randomness = rand::thread_rng().gen_range(-1.0..=1.0);
let mut candidate_one_index = food_sources[index].coordinates[selected_coordinate_index]
+ randomness
* (food_sources[index].coordinates[selected_coordinate_index]
- food_sources[different_food_source_index].coordinates[selected_coordinate_index]);
if candidate_one_index > upper_bound {
candidate_one_index = upper_bound;
}
if candidate_one_index < lower_bound {
candidate_one_index = lower_bound;
}
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

@ -57,6 +57,14 @@ impl FoodSource {
}
food_sources
}
pub fn find_best_food_source(food_sources: &Vec<FoodSource>) -> FoodSource {
food_sources
.iter()
.max_by(|x, y| x.fitness_calculation.total_cmp(&y.fitness_calculation))
.unwrap()
.clone()
}
}
impl fmt::Display for FoodSource {

View file

@ -18,16 +18,8 @@ fn main() {
);
for run_counter in 0..input.run {
let mut best_food_source = food_sources
.iter()
.max_by(|x, y| x.fitness_calculation.total_cmp(&y.fitness_calculation))
.unwrap()
.clone();
for food_source in &food_sources {
if best_food_source.fitness_calculation < food_source.fitness_calculation {
best_food_source = food_source.clone();
}
}
let mut best_food_source = FoodSource::find_best_food_source(&food_sources);
for _ in 0..input.iteration {
for index in 0..input.food_source_number as usize {
Bee::employed_bee(
@ -37,10 +29,16 @@ fn main() {
input.upper_bound,
input.lower_bound,
);
let mut total_fitness = 0.0;
for food_source in &food_sources {
total_fitness += food_source.fitness_calculation;
}
let total_fitness = food_sources.iter().map(|food_source|food_source.fitness_calculation).sum();
let mut last_looked = 0;
for _ in 0..input.food_source_number {
if last_looked >= input.food_source_number {
last_looked = 0;
}
}
for index in 0..input.food_source_number as usize {
Bee::onlooker_bee(
&mut food_sources,
index,
@ -49,25 +47,22 @@ fn main() {
input.upper_bound,
input.lower_bound,
);
}
// önce employed biticek sonra onlooker bakacak ve bakılandan tekrar başla
best_food_source = FoodSource::find_best_food_source(&food_sources);
for food_source in &food_sources {
if best_food_source.fitness_calculation < food_source.fitness_calculation {
best_food_source = food_source.clone();
}
}
for index in 0..food_sources.len() {
if food_sources[index].try_counter >= input.food_source_try_limit {
Bee::scout_bee(
&mut food_sources,
index,
input.food_source_try_limit,
input.lower_bound,
input.upper_bound,
input.decision_variable_count,
);
break;
}
for index in 0..food_sources.len() {
if food_sources[index].try_counter >= input.food_source_try_limit {
// en büyüğü bul sonra limiti geçiyosa kaşif gider
Bee::scout_bee(
&mut food_sources,
index,
input.food_source_try_limit,
input.lower_bound,
input.upper_bound,
input.decision_variable_count,
);
break;
}
}
}