pre commit
This commit is contained in:
parent
dc5c855dd7
commit
d694cea4a5
3 changed files with 76 additions and 58 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue