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,
|
upper_bound: f64,
|
||||||
lower_bound: f64,
|
lower_bound: f64,
|
||||||
) {
|
) {
|
||||||
let mut different_food_source_index = rand::thread_rng().gen_range(0..food_sources.len());
|
send_bee(
|
||||||
while different_food_source_index == index {
|
food_sources,
|
||||||
different_food_source_index = rand::thread_rng().gen_range(0..food_sources.len());
|
index,
|
||||||
}
|
decision_variable_count,
|
||||||
let selected_coordinate_index = rand::thread_rng().gen_range(0..decision_variable_count);
|
upper_bound,
|
||||||
let randomness = rand::thread_rng().gen_range(-1.0..=1.0);
|
lower_bound,
|
||||||
|
);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn onlooker_bee(
|
pub fn onlooker_bee(
|
||||||
|
@ -49,7 +30,7 @@ impl Bee {
|
||||||
) {
|
) {
|
||||||
let fitness_for_index = food_sources[index].fitness_calculation;
|
let fitness_for_index = food_sources[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::employed_bee(
|
send_bee(
|
||||||
food_sources,
|
food_sources,
|
||||||
index,
|
index,
|
||||||
decision_variable_count,
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -57,6 +57,14 @@ impl FoodSource {
|
||||||
}
|
}
|
||||||
food_sources
|
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 {
|
impl fmt::Display for FoodSource {
|
||||||
|
|
|
@ -18,16 +18,8 @@ fn main() {
|
||||||
);
|
);
|
||||||
|
|
||||||
for run_counter in 0..input.run {
|
for run_counter in 0..input.run {
|
||||||
let mut best_food_source = food_sources
|
let mut best_food_source = FoodSource::find_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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _ in 0..input.iteration {
|
for _ in 0..input.iteration {
|
||||||
for index in 0..input.food_source_number as usize {
|
for index in 0..input.food_source_number as usize {
|
||||||
Bee::employed_bee(
|
Bee::employed_bee(
|
||||||
|
@ -37,10 +29,16 @@ fn main() {
|
||||||
input.upper_bound,
|
input.upper_bound,
|
||||||
input.lower_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(
|
Bee::onlooker_bee(
|
||||||
&mut food_sources,
|
&mut food_sources,
|
||||||
index,
|
index,
|
||||||
|
@ -49,15 +47,13 @@ fn main() {
|
||||||
input.upper_bound,
|
input.upper_bound,
|
||||||
input.lower_bound,
|
input.lower_bound,
|
||||||
);
|
);
|
||||||
|
|
||||||
for food_source in &food_sources {
|
|
||||||
if best_food_source.fitness_calculation < food_source.fitness_calculation {
|
|
||||||
best_food_source = food_source.clone();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// önce employed biticek sonra onlooker bakacak ve bakılandan tekrar başla
|
||||||
|
best_food_source = FoodSource::find_best_food_source(&food_sources);
|
||||||
|
|
||||||
for index in 0..food_sources.len() {
|
for index in 0..food_sources.len() {
|
||||||
if food_sources[index].try_counter >= input.food_source_try_limit {
|
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(
|
Bee::scout_bee(
|
||||||
&mut food_sources,
|
&mut food_sources,
|
||||||
index,
|
index,
|
||||||
|
@ -70,7 +66,6 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
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