pre commit
This commit is contained in:
parent
48a0497ead
commit
52c4a859e4
2 changed files with 31 additions and 31 deletions
|
@ -4,10 +4,7 @@ use crate::{food::FoodSource, utils::Input};
|
||||||
|
|
||||||
pub struct Bee {}
|
pub struct Bee {}
|
||||||
impl Bee {
|
impl Bee {
|
||||||
pub fn send_all_employed_bees(
|
pub fn send_all_employed_bees(food_sources: &mut [FoodSource], input: &Input) {
|
||||||
food_sources: &mut [FoodSource],
|
|
||||||
input:&Input,
|
|
||||||
) {
|
|
||||||
for food_source_index in 0..input.food_source_number {
|
for food_source_index in 0..input.food_source_number {
|
||||||
Bee::employed_bee(
|
Bee::employed_bee(
|
||||||
food_sources,
|
food_sources,
|
||||||
|
@ -19,32 +16,32 @@ impl Bee {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send_all_onlooker_bees(food_sources: &mut [FoodSource],
|
pub fn send_all_onlooker_bees(food_sources: &mut [FoodSource], input: &Input) {
|
||||||
input:&Input,) {
|
|
||||||
let total_fitness = food_sources
|
let total_fitness = food_sources
|
||||||
.iter()
|
.iter()
|
||||||
.map(|food_source| food_source.fitness_calculation)
|
.map(|food_source| food_source.fitness_calculation)
|
||||||
.sum();
|
.sum();
|
||||||
let onlooker_bee_count = input.food_source_number;
|
|
||||||
let mut last_looked = 0;
|
let onlooker_bee_count = input.food_source_number;
|
||||||
for _ in 0..onlooker_bee_count {
|
let mut where_to_look = 0;
|
||||||
loop {
|
for _ in 0..onlooker_bee_count {
|
||||||
if last_looked >= input.food_source_number {
|
loop {
|
||||||
last_looked = 0;
|
if where_to_look >= input.food_source_number {
|
||||||
}
|
where_to_look = 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;
|
|
||||||
}
|
}
|
||||||
|
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(
|
fn employed_bee(
|
||||||
|
@ -72,7 +69,7 @@ impl Bee {
|
||||||
lower_bound: f64,
|
lower_bound: f64,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
let fitness_for_index = food_sources[food_source_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) {
|
||||||
Self::send_bee(
|
Self::send_bee(
|
||||||
food_sources,
|
food_sources,
|
||||||
food_source_index,
|
food_source_index,
|
||||||
|
|
|
@ -19,13 +19,16 @@ fn main() {
|
||||||
);
|
);
|
||||||
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 {
|
||||||
Bee::send_all_employed_bees(&mut food_sources, &input);
|
Bee::send_all_employed_bees(&mut food_sources, &input);
|
||||||
|
|
||||||
Bee::send_all_onlooker_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 =
|
let most_tried_food_source_index =
|
||||||
FoodSource::find_most_tried_food_source_index(&food_sources);
|
FoodSource::find_most_tried_food_source_index(&food_sources);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue