use bevy::prelude::*;
use crate::prelude::Score;
#[reflect_trait]
pub trait Measure: std::fmt::Debug + Sync + Send {
fn calculate(&self, inputs: Vec<(&Score, f32)>) -> f32;
}
#[derive(Debug, Clone, Reflect)]
pub struct WeightedSum;
impl Measure for WeightedSum {
fn calculate(&self, scores: Vec<(&Score, f32)>) -> f32 {
scores
.iter()
.fold(0f32, |acc, (score, weight)| acc + score.0 * weight)
}
}
#[derive(Debug, Clone, Reflect)]
pub struct WeightedProduct;
impl Measure for WeightedProduct {
fn calculate(&self, scores: Vec<(&Score, f32)>) -> f32 {
scores
.iter()
.fold(0f32, |acc, (score, weight)| acc * score.0 * weight)
}
}
#[derive(Debug, Clone, Reflect)]
pub struct ChebyshevDistance;
impl Measure for ChebyshevDistance {
fn calculate(&self, scores: Vec<(&Score, f32)>) -> f32 {
scores
.iter()
.fold(0f32, |best, (score, weight)| (score.0 * weight).max(best))
}
}
#[derive(Debug, Clone, Default, Reflect)]
pub struct WeightedMeasure;
impl Measure for WeightedMeasure {
fn calculate(&self, scores: Vec<(&Score, f32)>) -> f32 {
let wsum: f32 = scores.iter().map(|(_score, weight)| weight).sum();
if wsum == 0.0 {
0.0
} else {
scores
.iter()
.map(|(score, weight)| weight / wsum * score.get().powf(2.0))
.sum::<f32>()
.powf(1.0 / 2.0)
}
}
}