2024-11-20 00:20:40 +03:00
|
|
|
use core::fmt;
|
|
|
|
|
2024-11-20 00:20:40 +03:00
|
|
|
use rand::Rng;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct FoodSource {
|
2024-11-20 00:20:40 +03:00
|
|
|
pub function_calculation: f64,
|
2024-11-20 00:20:40 +03:00
|
|
|
pub fitness_calculation: f64,
|
2024-11-20 00:20:40 +03:00
|
|
|
pub coordinates: Vec<f64>,
|
|
|
|
pub try_counter: u128,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FoodSource {
|
2024-11-20 00:20:40 +03:00
|
|
|
pub fn new(coordinates: Vec<f64>) -> Self {
|
2024-11-20 00:20:40 +03:00
|
|
|
let mut food_source = FoodSource {
|
2024-11-20 00:20:40 +03:00
|
|
|
fitness_calculation: 0.0,
|
2024-11-20 00:20:40 +03:00
|
|
|
function_calculation: 1.0,
|
2024-11-20 00:20:40 +03:00
|
|
|
coordinates,
|
|
|
|
try_counter: 0,
|
|
|
|
};
|
2024-11-20 00:20:40 +03:00
|
|
|
food_source.fitness_function();
|
2024-11-20 00:20:40 +03:00
|
|
|
food_source
|
|
|
|
}
|
|
|
|
fn fitness_function(&mut self) {
|
|
|
|
let calculation = Self::calculate(self.coordinates.clone());
|
2024-11-20 00:20:40 +03:00
|
|
|
self.function_calculation = calculation;
|
2024-11-20 00:20:40 +03:00
|
|
|
if calculation >= 0.0 {
|
2024-11-20 00:20:40 +03:00
|
|
|
self.fitness_calculation = 1.0 / (1.0 + calculation);
|
2024-11-20 00:20:40 +03:00
|
|
|
} else {
|
2024-11-20 00:20:40 +03:00
|
|
|
self.fitness_calculation = 1.0 + calculation.abs();
|
2024-11-20 00:20:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn calculate(decision_variables: Vec<f64>) -> f64 {
|
|
|
|
let mut result = 0.0;
|
|
|
|
for element in decision_variables {
|
|
|
|
result += element * element;
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_food_sources(
|
2024-11-20 00:20:40 +03:00
|
|
|
food_source_number: usize,
|
2024-11-20 00:20:40 +03:00
|
|
|
decision_variable_count: usize,
|
|
|
|
upper_bound: f64,
|
|
|
|
lower_bound: f64,
|
|
|
|
) -> Vec<FoodSource> {
|
|
|
|
let mut food_sources = vec![];
|
|
|
|
|
|
|
|
for _ in 0..food_source_number {
|
|
|
|
let mut coordinates = vec![];
|
|
|
|
for _ in 0..decision_variable_count {
|
|
|
|
let random = rand::thread_rng().gen_range(lower_bound..=upper_bound);
|
|
|
|
coordinates.push(random);
|
|
|
|
}
|
2024-11-20 00:20:40 +03:00
|
|
|
food_sources.push(FoodSource::new(coordinates));
|
2024-11-20 00:20:40 +03:00
|
|
|
}
|
|
|
|
food_sources
|
|
|
|
}
|
2024-11-20 00:20:40 +03:00
|
|
|
|
2024-11-20 00:20:40 +03:00
|
|
|
pub fn find_best_food_source(food_sources: &[FoodSource]) -> FoodSource {
|
2024-11-20 00:20:40 +03:00
|
|
|
food_sources
|
|
|
|
.iter()
|
|
|
|
.max_by(|x, y| x.fitness_calculation.total_cmp(&y.fitness_calculation))
|
|
|
|
.unwrap()
|
|
|
|
.clone()
|
|
|
|
}
|
2024-11-20 00:20:40 +03:00
|
|
|
|
|
|
|
pub fn find_most_tried_food_source_index(food_sources: &[FoodSource]) -> usize {
|
|
|
|
let (most_tried_food_source_index, _) = food_sources
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.max_by(|(_, x), (_, y)| x.try_counter.cmp(&y.try_counter))
|
|
|
|
.unwrap();
|
|
|
|
most_tried_food_source_index
|
|
|
|
}
|
2024-11-20 00:20:40 +03:00
|
|
|
}
|
2024-11-20 00:20:40 +03:00
|
|
|
|
|
|
|
impl fmt::Display for FoodSource {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-11-20 00:20:40 +03:00
|
|
|
writeln!(
|
2024-11-20 00:20:40 +03:00
|
|
|
f,
|
2024-11-20 00:20:40 +03:00
|
|
|
"function_calculation = {:e}\nfitness_calculation = {:e}\ncoordinates = [",
|
|
|
|
self.function_calculation, self.fitness_calculation
|
|
|
|
)?;
|
|
|
|
for coordinate in &self.coordinates {
|
|
|
|
writeln!(f, " {:e},", coordinate)?;
|
|
|
|
}
|
|
|
|
write!(f, "]\ntry_counter = {}\n", self.try_counter)
|
2024-11-20 00:20:40 +03:00
|
|
|
}
|
|
|
|
}
|