1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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)
}
}
}