fix: 🐛 non-sticking to bounds
This commit is contained in:
parent
f0675e4bdf
commit
6cbba58e63
2 changed files with 41 additions and 6 deletions
|
@ -8,6 +8,8 @@ impl 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 {
|
||||
|
@ -16,11 +18,17 @@ impl Bee {
|
|||
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 candidate_one_index = food_sources[index].coordinates[selected_coordinate_index]
|
||||
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::get(candidate_coordinates);
|
||||
|
@ -36,10 +44,18 @@ impl Bee {
|
|||
index: usize,
|
||||
total_fitness: f64,
|
||||
decision_variable_count: usize,
|
||||
upper_bound: f64,
|
||||
lower_bound: f64,
|
||||
) {
|
||||
let fitness_for_index = food_sources[index].fitness;
|
||||
if fitness_for_index / total_fitness <= rand::thread_rng().gen_range(0.0..=1.0) {
|
||||
Self::worker_bee(food_sources, index, decision_variable_count);
|
||||
Self::worker_bee(
|
||||
food_sources,
|
||||
index,
|
||||
decision_variable_count,
|
||||
upper_bound,
|
||||
lower_bound,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,9 +70,20 @@ impl Bee {
|
|||
if food_sources[most_tried_index].try_counter > limit {
|
||||
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 random = if upper_bound * lower_bound > 0.0 {
|
||||
lower_bound
|
||||
+ rand::thread_rng().gen_range(0.0..=1.0) * (upper_bound - lower_bound)
|
||||
} else {
|
||||
lower_bound
|
||||
+ rand::thread_rng().gen_range(0.0..=1.0) * (upper_bound + lower_bound)
|
||||
};
|
||||
if random > upper_bound {
|
||||
coordinates_for_new.push(upper_bound);
|
||||
} else if random < lower_bound {
|
||||
coordinates_for_new.push(lower_bound);
|
||||
} else {
|
||||
coordinates_for_new.push(random);
|
||||
}
|
||||
}
|
||||
let new_food_source = FoodSource::get(coordinates_for_new);
|
||||
food_sources[most_tried_index] = new_food_source;
|
||||
|
|
|
@ -19,7 +19,13 @@ fn main() {
|
|||
}
|
||||
for _ in 0..input.iteration {
|
||||
for index in 0..input.food_source_number as usize {
|
||||
Bee::worker_bee(&mut food_sources, index, input.decision_variable_count);
|
||||
Bee::worker_bee(
|
||||
&mut food_sources,
|
||||
index,
|
||||
input.decision_variable_count,
|
||||
input.upper_bound,
|
||||
input.lower_bound,
|
||||
);
|
||||
let mut total_fitness = 0.0;
|
||||
for food_source in &food_sources {
|
||||
total_fitness += food_source.fitness;
|
||||
|
@ -29,6 +35,8 @@ fn main() {
|
|||
index,
|
||||
total_fitness,
|
||||
input.decision_variable_count,
|
||||
input.upper_bound,
|
||||
input.lower_bound,
|
||||
);
|
||||
let mut most_tried_index = 0;
|
||||
for i in 0..food_sources.len() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue