feat: ✨ verbose output
fix: 🚑 forgot to square root at the end
This commit is contained in:
parent
f12b1a9547
commit
cca9a7a253
3 changed files with 20 additions and 17 deletions
|
@ -5,6 +5,7 @@ use rand::Rng;
|
|||
#[derive(Debug, Clone)]
|
||||
pub struct FoodSource {
|
||||
pub fitness: f64,
|
||||
pub function_calculation: f64,
|
||||
pub coordinates: Vec<f64>,
|
||||
pub try_counter: u128,
|
||||
}
|
||||
|
@ -13,6 +14,7 @@ impl FoodSource {
|
|||
pub fn get(coordinates: Vec<f64>) -> Self {
|
||||
let mut food_source = FoodSource {
|
||||
fitness: 0.0,
|
||||
function_calculation: 0.0,
|
||||
coordinates,
|
||||
try_counter: 0,
|
||||
};
|
||||
|
@ -23,6 +25,7 @@ impl FoodSource {
|
|||
}
|
||||
fn fitness_function(&mut self) {
|
||||
let calculation = Self::calculate(self.coordinates.clone());
|
||||
self.function_calculation = calculation;
|
||||
if calculation >= 0.0 {
|
||||
self.fitness = 1.0 / (1.0 + calculation);
|
||||
} else {
|
||||
|
@ -62,8 +65,8 @@ impl fmt::Display for FoodSource {
|
|||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"fitness = {}\ncoordinates = {:#?}\ntry_counter = {}\n",
|
||||
self.fitness, self.coordinates, self.try_counter
|
||||
"fitness = {}\nfunction_calculation = {}\ncoordinates = {:#?}\ntry_counter = {}\n",
|
||||
self.fitness, self.function_calculation, self.coordinates, self.try_counter
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -120,7 +120,7 @@ impl Input {
|
|||
|
||||
pub fn give_output(
|
||||
best_food_source: FoodSource,
|
||||
fitness_results: &[f64],
|
||||
function_results: &[f64],
|
||||
input_run: usize,
|
||||
run_counter: usize,
|
||||
) {
|
||||
|
@ -135,8 +135,8 @@ 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(fitness_results);
|
||||
let standard_deviation = standard_deviation(fitness_results, arithmetic_mean);
|
||||
let arithmetic_mean = arithmetic_mean(function_results);
|
||||
let standard_deviation = standard_deviation(function_results, arithmetic_mean);
|
||||
write!(
|
||||
print_buffer,
|
||||
"[evaluation]\narithmetic_mean = {}\nstandard_deviation = {}",
|
||||
|
@ -185,18 +185,18 @@ fn write_output_file(print_buffer: &[u8], run_counter: usize) {
|
|||
file.flush().unwrap();
|
||||
}
|
||||
|
||||
fn arithmetic_mean(fitness_results: &[f64]) -> f64 {
|
||||
let mut total_fitness_results = 0.0;
|
||||
for fitness_result in fitness_results {
|
||||
total_fitness_results += fitness_result
|
||||
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
|
||||
}
|
||||
total_fitness_results / fitness_results.len() as f64
|
||||
total_function_results / function_results.len() as f64
|
||||
}
|
||||
|
||||
fn standard_deviation(fitness_results: &[f64], arithmetic_mean: f64) -> f64 {
|
||||
fn standard_deviation(function_results: &[f64], arithmetic_mean: f64) -> f64 {
|
||||
let mut total_difference_square = 0.0;
|
||||
for fitness_result in fitness_results {
|
||||
total_difference_square += (fitness_result - arithmetic_mean).powi(2)
|
||||
for function_result in function_results {
|
||||
total_difference_square += (function_result - arithmetic_mean).powi(2)
|
||||
}
|
||||
total_difference_square / (fitness_results.len() - 1) as f64
|
||||
f64::sqrt(total_difference_square / (function_results.len() - 1) as f64)
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ fn main() {
|
|||
println!("Hello, world!");
|
||||
|
||||
let input = Input::get();
|
||||
let mut fitness_results = vec![];
|
||||
let mut function_results = vec![];
|
||||
|
||||
let mut food_sources = FoodSource::create_food_sources(
|
||||
input.food_source_number,
|
||||
|
@ -60,10 +60,10 @@ fn main() {
|
|||
);
|
||||
}
|
||||
}
|
||||
fitness_results.push(best_food_source.fitness);
|
||||
function_results.push(best_food_source.function_calculation);
|
||||
give_output(
|
||||
best_food_source,
|
||||
&fitness_results[..],
|
||||
&function_results[..],
|
||||
input.run,
|
||||
run_counter,
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue