feat: ✨ fitness evaluation is added to output
This commit is contained in:
parent
8eda015039
commit
812c34051d
4 changed files with 31 additions and 23 deletions
|
@ -33,7 +33,7 @@ impl Bee {
|
|||
candidate_coordinates[selected_coordinate_index] = candidate_one_index;
|
||||
let candidate = FoodSource::new(candidate_coordinates);
|
||||
food_sources[index].try_counter += 1;
|
||||
if candidate.fitness > food_sources[index].fitness {
|
||||
if candidate.fitness_calculation > food_sources[index].fitness_calculation {
|
||||
food_sources[index] = candidate;
|
||||
food_sources[index].try_counter = 0;
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ impl Bee {
|
|||
upper_bound: f64,
|
||||
lower_bound: f64,
|
||||
) {
|
||||
let fitness_for_index = food_sources[index].fitness;
|
||||
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(
|
||||
food_sources,
|
||||
|
|
|
@ -4,8 +4,8 @@ use rand::Rng;
|
|||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FoodSource {
|
||||
pub fitness: f64,
|
||||
pub function_calculation: f64,
|
||||
pub fitness_calculation: f64,
|
||||
pub coordinates: Vec<f64>,
|
||||
pub try_counter: u128,
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ pub struct FoodSource {
|
|||
impl FoodSource {
|
||||
pub fn new(coordinates: Vec<f64>) -> Self {
|
||||
let mut food_source = FoodSource {
|
||||
fitness: 0.0,
|
||||
fitness_calculation: 0.0,
|
||||
function_calculation: 0.0,
|
||||
coordinates,
|
||||
try_counter: 0,
|
||||
|
@ -27,9 +27,9 @@ impl FoodSource {
|
|||
let calculation = Self::calculate(self.coordinates.clone());
|
||||
self.function_calculation = calculation;
|
||||
if calculation >= 0.0 {
|
||||
self.fitness = 1.0 / (1.0 + calculation);
|
||||
self.fitness_calculation = 1.0 / (1.0 + calculation);
|
||||
} else {
|
||||
self.fitness = 1.0 + calculation.abs();
|
||||
self.fitness_calculation = 1.0 + calculation.abs();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -65,8 +65,8 @@ impl fmt::Display for FoodSource {
|
|||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"fitness = {}\nfunction_calculation = {}\ncoordinates = {:#?}\ntry_counter = {}\n",
|
||||
self.fitness, self.function_calculation, self.coordinates, self.try_counter
|
||||
"function_calculation = {}\nfitness_calculation = {}\ncoordinates = {:#?}\ntry_counter = {}\n",
|
||||
self.function_calculation, self.fitness_calculation, self.coordinates, self.try_counter
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -121,6 +121,7 @@ impl Input {
|
|||
pub fn give_output(
|
||||
best_food_source: FoodSource,
|
||||
function_results: &[f64],
|
||||
fitness_results: &[f64],
|
||||
input_run: usize,
|
||||
run_counter: usize,
|
||||
) {
|
||||
|
@ -135,12 +136,16 @@ pub fn give_output(
|
|||
let mut print_buffer = vec![];
|
||||
write!(print_buffer, "[{}]\n{}\n", run_counter, best_food_source).unwrap();
|
||||
if run_counter == input_run - 1 {
|
||||
let arithmetic_mean = arithmetic_mean(function_results);
|
||||
let standard_deviation = standard_deviation(function_results, arithmetic_mean);
|
||||
let function_results_arithmetic_mean = arithmetic_mean(function_results);
|
||||
let function_results_standard_deviation =
|
||||
standard_deviation(function_results, function_results_arithmetic_mean);
|
||||
let fitness_results_arithmetic_mean = arithmetic_mean(fitness_results);
|
||||
let fitness_results_standard_deviation =
|
||||
standard_deviation(fitness_results, fitness_results_arithmetic_mean);
|
||||
write!(
|
||||
print_buffer,
|
||||
"[evaluation]\narithmetic_mean = {}\nstandard_deviation = {}",
|
||||
arithmetic_mean, standard_deviation
|
||||
"[evaluation_function_results]\nfunction_results_arithmetic_mean = {}\nfunction_results_standard_deviation = {}\n\n[evaluation_fitness_results]\nfitness_results_arithmetic_mean = {}\nfitness_results_standard_deviation = {}",
|
||||
function_results_arithmetic_mean, function_results_standard_deviation, fitness_results_arithmetic_mean, fitness_results_standard_deviation
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
@ -185,18 +190,18 @@ fn write_output_file(print_buffer: &[u8], run_counter: usize) {
|
|||
file.flush().unwrap();
|
||||
}
|
||||
|
||||
fn arithmetic_mean(function_results: &[f64]) -> f64 {
|
||||
let mut total_function_results = 0.0;
|
||||
for function_result in function_results {
|
||||
total_function_results += function_result
|
||||
fn arithmetic_mean(results: &[f64]) -> f64 {
|
||||
let mut total_results = 0.0;
|
||||
for function_result in results {
|
||||
total_results += function_result
|
||||
}
|
||||
total_function_results / function_results.len() as f64
|
||||
total_results / results.len() as f64
|
||||
}
|
||||
|
||||
fn standard_deviation(function_results: &[f64], arithmetic_mean: f64) -> f64 {
|
||||
fn standard_deviation(results: &[f64], arithmetic_mean: f64) -> f64 {
|
||||
let mut total_difference_square = 0.0;
|
||||
for function_result in function_results {
|
||||
for function_result in results {
|
||||
total_difference_square += (function_result - arithmetic_mean).powi(2)
|
||||
}
|
||||
f64::sqrt(total_difference_square / (function_results.len() - 1) as f64)
|
||||
f64::sqrt(total_difference_square / (results.len() - 1) as f64)
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ fn main() {
|
|||
|
||||
let input = Input::get();
|
||||
let mut function_results = vec![];
|
||||
let mut fitness_results = vec![];
|
||||
|
||||
let mut food_sources = FoodSource::create_food_sources(
|
||||
input.food_source_number,
|
||||
|
@ -15,11 +16,11 @@ fn main() {
|
|||
for run_counter in 0..input.run {
|
||||
let mut best_food_source = FoodSource::new(vec![]);
|
||||
for food_source in &food_sources {
|
||||
if best_food_source.fitness < food_source.fitness {
|
||||
if best_food_source.fitness_calculation < food_source.fitness_calculation {
|
||||
best_food_source
|
||||
.coordinates
|
||||
.clone_from(&food_source.coordinates);
|
||||
best_food_source.fitness = food_source.fitness;
|
||||
best_food_source.fitness_calculation = food_source.fitness_calculation;
|
||||
}
|
||||
}
|
||||
for _ in 0..input.iteration {
|
||||
|
@ -33,7 +34,7 @@ fn main() {
|
|||
);
|
||||
let mut total_fitness = 0.0;
|
||||
for food_source in &food_sources {
|
||||
total_fitness += food_source.fitness;
|
||||
total_fitness += food_source.fitness_calculation;
|
||||
}
|
||||
Bee::onlooker_bee(
|
||||
&mut food_sources,
|
||||
|
@ -61,9 +62,11 @@ fn main() {
|
|||
}
|
||||
}
|
||||
function_results.push(best_food_source.function_calculation);
|
||||
fitness_results.push(best_food_source.fitness_calculation);
|
||||
give_output(
|
||||
best_food_source,
|
||||
&function_results[..],
|
||||
&fitness_results[..],
|
||||
input.run,
|
||||
run_counter,
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue