From cbabe083cf8aee853528e3f440315a3601a53510 Mon Sep 17 00:00:00 2001 From: FiveMovesAhead Date: Wed, 18 Dec 2024 11:30:26 +0800 Subject: [PATCH] Fix fraud issue in knapsack. --- tig-challenges/src/knapsack.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tig-challenges/src/knapsack.rs b/tig-challenges/src/knapsack.rs index e65e238..c6e8245 100644 --- a/tig-challenges/src/knapsack.rs +++ b/tig-challenges/src/knapsack.rs @@ -112,9 +112,8 @@ impl crate::ChallengeTrait for Challenge { .collect(); // Sort the list of tuples by value-to-weight ratio in descending order - value_weight_ratios.sort_unstable_by(|&(_, ratio_a, _), &(_, ratio_b, _)| { - ratio_b.partial_cmp(&ratio_a).unwrap() - }); + value_weight_ratios + .sort_by(|&(_, ratio_a, _), &(_, ratio_b, _)| ratio_b.partial_cmp(&ratio_a).unwrap()); let mut total_weight = 0; let mut selected_indices = Vec::new(); @@ -124,7 +123,7 @@ impl crate::ChallengeTrait for Challenge { total_weight += weight; } } - selected_indices.sort_unstable(); + selected_indices.sort(); let mut min_value = calculate_total_value(&selected_indices, &values, &interaction_values); min_value = (min_value as f32 * (1.0 + difficulty.better_than_baseline as f32 / 1000.0)) @@ -187,7 +186,7 @@ pub fn calculate_total_value( interaction_values: &Vec>, ) -> u32 { let mut indices = indices.clone(); - indices.sort_unstable(); + indices.sort(); let mut total_value = 0i32;