From 4ac19a0016ffa310b39b5d08d2ad8fe7e7757f9e Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Fri, 11 Nov 2022 11:31:34 +0100 Subject: [PATCH] make ideal_alternatives a method of IdealAlternatives Signed-off-by: Wolfgang Bumiller --- src/topsis.rs | 49 ++++++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/src/topsis.rs b/src/topsis.rs index d86a2b9..437f41f 100644 --- a/src/topsis.rs +++ b/src/topsis.rs @@ -136,35 +136,34 @@ struct TopsisIdealAlternatives { worst: [f64; N_CRITERIA], } -/// Compute the idealized alternatives from the given `matrix`. The `criteria` are required to know -/// if a critierion should be maximized or minimized. -fn ideal_alternatives( - matrix: &TopsisMatrix, - criteria: &TopsisCriteria, -) -> TopsisIdealAlternatives { - let criteria = &criteria.0; +impl TopsisIdealAlternatives { + /// Compute the idealized alternatives from the given `matrix`. The `criteria` are required to know + /// if a critierion should be maximized or minimized. + fn compute(matrix: &TopsisMatrix, criteria: &TopsisCriteria) -> Self { + let criteria = &criteria.0; - let mut best = [0.0; N]; - let mut worst = [0.0; N]; + let mut best = [0.0; N]; + let mut worst = [0.0; N]; - for n in 0..N { - let fixed_criterion = matrix.fixed_criterion(n); - let min = fixed_criterion - .iter() - .min_by(|a, b| a.total_cmp(b)) - .unwrap(); - let max = fixed_criterion - .iter() - .max_by(|a, b| a.total_cmp(b)) - .unwrap(); + for n in 0..N { + let fixed_criterion = matrix.fixed_criterion(n); + let min = fixed_criterion + .iter() + .min_by(|a, b| a.total_cmp(b)) + .unwrap(); + let max = fixed_criterion + .iter() + .max_by(|a, b| a.total_cmp(b)) + .unwrap(); - (best[n], worst[n]) = match criteria[n].maximize { - true => (*max, *min), - false => (*min, *max), + (best[n], worst[n]) = match criteria[n].maximize { + true => (*max, *min), + false => (*min, *max), + } } - } - TopsisIdealAlternatives { best, worst } + Self { best, worst } + } } /// Scores the alternatives in `matrix` according to their similarity to the ideal worst @@ -174,7 +173,7 @@ pub fn score_alternatives( matrix: &TopsisMatrix, criteria: &TopsisCriteria, ) -> Result, Error> { - let ideal_alternatives = ideal_alternatives(matrix, criteria); + let ideal_alternatives = TopsisIdealAlternatives::compute(matrix, criteria); let ideal_best = &ideal_alternatives.best; let ideal_worst = &ideal_alternatives.worst;