make ideal_alternatives a method of IdealAlternatives

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2022-11-11 11:31:34 +01:00
parent c83c8887c8
commit 4ac19a0016

View File

@ -136,35 +136,34 @@ struct TopsisIdealAlternatives<const N_CRITERIA: usize> {
worst: [f64; N_CRITERIA], worst: [f64; N_CRITERIA],
} }
/// Compute the idealized alternatives from the given `matrix`. The `criteria` are required to know impl<const N: usize> TopsisIdealAlternatives<N> {
/// if a critierion should be maximized or minimized. /// Compute the idealized alternatives from the given `matrix`. The `criteria` are required to know
fn ideal_alternatives<const N: usize>( /// if a critierion should be maximized or minimized.
matrix: &TopsisMatrix<N>, fn compute(matrix: &TopsisMatrix<N>, criteria: &TopsisCriteria<N>) -> Self {
criteria: &TopsisCriteria<N>, let criteria = &criteria.0;
) -> TopsisIdealAlternatives<N> {
let criteria = &criteria.0;
let mut best = [0.0; N]; let mut best = [0.0; N];
let mut worst = [0.0; N]; let mut worst = [0.0; N];
for n in 0..N { for n in 0..N {
let fixed_criterion = matrix.fixed_criterion(n); let fixed_criterion = matrix.fixed_criterion(n);
let min = fixed_criterion let min = fixed_criterion
.iter() .iter()
.min_by(|a, b| a.total_cmp(b)) .min_by(|a, b| a.total_cmp(b))
.unwrap(); .unwrap();
let max = fixed_criterion let max = fixed_criterion
.iter() .iter()
.max_by(|a, b| a.total_cmp(b)) .max_by(|a, b| a.total_cmp(b))
.unwrap(); .unwrap();
(best[n], worst[n]) = match criteria[n].maximize { (best[n], worst[n]) = match criteria[n].maximize {
true => (*max, *min), true => (*max, *min),
false => (*min, *max), false => (*min, *max),
}
} }
}
TopsisIdealAlternatives { best, worst } Self { best, worst }
}
} }
/// Scores the alternatives in `matrix` according to their similarity to the ideal worst /// Scores the alternatives in `matrix` according to their similarity to the ideal worst
@ -174,7 +173,7 @@ pub fn score_alternatives<const N: usize>(
matrix: &TopsisMatrix<N>, matrix: &TopsisMatrix<N>,
criteria: &TopsisCriteria<N>, criteria: &TopsisCriteria<N>,
) -> Result<Vec<f64>, Error> { ) -> Result<Vec<f64>, Error> {
let ideal_alternatives = ideal_alternatives(matrix, criteria); let ideal_alternatives = TopsisIdealAlternatives::compute(matrix, criteria);
let ideal_best = &ideal_alternatives.best; let ideal_best = &ideal_alternatives.best;
let ideal_worst = &ideal_alternatives.worst; let ideal_worst = &ideal_alternatives.worst;