pre commit
This commit is contained in:
parent
52c4a859e4
commit
ed7407743f
3 changed files with 49 additions and 53 deletions
|
@ -20,25 +20,28 @@ impl Bee {
|
||||||
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::<f64>();
|
||||||
|
|
||||||
let onlooker_bee_count = input.food_source_number;
|
let onlooker_bee_count = input.food_source_number;
|
||||||
let mut where_to_look = 0;
|
let mut where_to_look = 0;
|
||||||
for _ in 0..onlooker_bee_count {
|
for _ in 0..onlooker_bee_count {
|
||||||
loop {
|
'decide_what_dance_to_follow: loop {
|
||||||
if where_to_look >= input.food_source_number {
|
if where_to_look >= input.food_source_number {
|
||||||
where_to_look = 0;
|
where_to_look = 0;
|
||||||
}
|
}
|
||||||
if Bee::onlooker_bee(
|
|
||||||
|
let fitness_for_index = food_sources[where_to_look].fitness_calculation;
|
||||||
|
if rand::thread_rng().gen_range(0.0..=1.0) < fitness_for_index / total_fitness {
|
||||||
|
Bee::onlooker_bee(
|
||||||
food_sources,
|
food_sources,
|
||||||
where_to_look,
|
where_to_look,
|
||||||
total_fitness,
|
|
||||||
input.decision_variable_count,
|
input.decision_variable_count,
|
||||||
input.upper_bound,
|
input.upper_bound,
|
||||||
input.lower_bound,
|
input.lower_bound,
|
||||||
) {
|
);
|
||||||
break;
|
break 'decide_what_dance_to_follow;
|
||||||
}
|
}
|
||||||
|
|
||||||
where_to_look += 1;
|
where_to_look += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -63,13 +66,10 @@ impl Bee {
|
||||||
fn onlooker_bee(
|
fn onlooker_bee(
|
||||||
food_sources: &mut [FoodSource],
|
food_sources: &mut [FoodSource],
|
||||||
food_source_index: usize,
|
food_source_index: usize,
|
||||||
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[food_source_index].fitness_calculation;
|
|
||||||
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,
|
||||||
|
@ -77,30 +77,18 @@ impl Bee {
|
||||||
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_food_source_index: usize,
|
most_tried_food_source_index: usize,
|
||||||
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_food_source_index].try_counter > limit {
|
let new_food_source = FoodSource::new(decision_variable_count, lower_bound, upper_bound);
|
||||||
let mut coordinates_for_new = vec![];
|
|
||||||
for _ in 0..decision_variable_count {
|
|
||||||
let random = lower_bound
|
|
||||||
+ rand::thread_rng().gen_range(0.0..=1.0) * (upper_bound - lower_bound);
|
|
||||||
coordinates_for_new.push(random);
|
|
||||||
}
|
|
||||||
let new_food_source = FoodSource::new(coordinates_for_new);
|
|
||||||
food_sources[most_tried_food_source_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],
|
||||||
|
@ -134,7 +122,7 @@ impl Bee {
|
||||||
food_sources[food_source_index].coordinates.clone();
|
food_sources[food_source_index].coordinates.clone();
|
||||||
original_decision_variables[decision_variable_index] = candidate_decision_variable;
|
original_decision_variables[decision_variable_index] = candidate_decision_variable;
|
||||||
let candidate_decision_variables = original_decision_variables;
|
let candidate_decision_variables = original_decision_variables;
|
||||||
FoodSource::new(candidate_decision_variables)
|
FoodSource::from_coordinates(candidate_decision_variables)
|
||||||
};
|
};
|
||||||
|
|
||||||
food_sources[food_source_index].try_counter += 1;
|
food_sources[food_source_index].try_counter += 1;
|
||||||
|
@ -142,7 +130,6 @@ impl Bee {
|
||||||
> food_sources[food_source_index].fitness_calculation
|
> food_sources[food_source_index].fitness_calculation
|
||||||
{
|
{
|
||||||
food_sources[food_source_index] = candidate_food_source;
|
food_sources[food_source_index] = candidate_food_source;
|
||||||
food_sources[food_source_index].try_counter = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ pub struct FoodSource {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FoodSource {
|
impl FoodSource {
|
||||||
pub fn new(coordinates: Vec<f64>) -> Self {
|
pub fn from_coordinates(coordinates: Vec<f64>) -> Self {
|
||||||
let mut food_source = FoodSource {
|
let mut food_source = FoodSource {
|
||||||
fitness_calculation: 0.0,
|
fitness_calculation: 0.0,
|
||||||
function_calculation: 1.0,
|
function_calculation: 1.0,
|
||||||
|
@ -21,6 +21,16 @@ impl FoodSource {
|
||||||
food_source.fitness_function();
|
food_source.fitness_function();
|
||||||
food_source
|
food_source
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new(decision_variable_count: usize, lower_bound: f64, upper_bound: f64) -> Self {
|
||||||
|
let mut coordinates = vec![];
|
||||||
|
for _ in 0..decision_variable_count {
|
||||||
|
let random = rand::thread_rng().gen_range(lower_bound..=upper_bound);
|
||||||
|
coordinates.push(random);
|
||||||
|
}
|
||||||
|
FoodSource::from_coordinates(coordinates)
|
||||||
|
}
|
||||||
|
|
||||||
fn fitness_function(&mut self) {
|
fn fitness_function(&mut self) {
|
||||||
let calculation = Self::calculate(self.coordinates.clone());
|
let calculation = Self::calculate(self.coordinates.clone());
|
||||||
self.function_calculation = calculation;
|
self.function_calculation = calculation;
|
||||||
|
@ -48,12 +58,9 @@ impl FoodSource {
|
||||||
let mut food_sources = vec![];
|
let mut food_sources = vec![];
|
||||||
|
|
||||||
for _ in 0..food_source_number {
|
for _ in 0..food_source_number {
|
||||||
let mut coordinates = vec![];
|
let new_food_source =
|
||||||
for _ in 0..decision_variable_count {
|
FoodSource::new(decision_variable_count, lower_bound, upper_bound);
|
||||||
let random = rand::thread_rng().gen_range(lower_bound..=upper_bound);
|
food_sources.push(new_food_source);
|
||||||
coordinates.push(random);
|
|
||||||
}
|
|
||||||
food_sources.push(FoodSource::new(coordinates));
|
|
||||||
}
|
}
|
||||||
food_sources
|
food_sources
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,15 +32,17 @@ fn main() {
|
||||||
|
|
||||||
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);
|
||||||
|
if food_sources[most_tried_food_source_index].try_counter > input.food_source_try_limit
|
||||||
|
{
|
||||||
Bee::scout_bee(
|
Bee::scout_bee(
|
||||||
&mut food_sources,
|
&mut food_sources,
|
||||||
most_tried_food_source_index,
|
most_tried_food_source_index,
|
||||||
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,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
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);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue