From 573f2d8a225863ead07994e82cbfc5092f6eefe2 Mon Sep 17 00:00:00 2001 From: Ehouarn Date: Sat, 2 Aug 2025 17:18:51 +0200 Subject: [PATCH] More robust algorithm --- apps/wei/forms/surveys/wei2025.py | 15 +++++++-------- apps/wei/tests/test_wei_algorithm_2025.py | 2 +- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/apps/wei/forms/surveys/wei2025.py b/apps/wei/forms/surveys/wei2025.py index 648a88c6..db64487a 100644 --- a/apps/wei/forms/surveys/wei2025.py +++ b/apps/wei/forms/surveys/wei2025.py @@ -365,7 +365,7 @@ class WEISurvey2025(WEISurvey): return sum([cls.get_algorithm_class().get_bus_information(bus).scores[word] for bus in buses]) / buses.count() @lru_cache() - def score(self, bus): + def score_questions(self, bus): """ The score given by the answers to the questions """ @@ -391,7 +391,7 @@ class WEISurvey2025(WEISurvey): @lru_cache() def scores_per_bus(self): - return {bus: (self.score(bus), self.score_words(bus)) for bus in self.get_algorithm_class().get_buses()} + return {bus: (self.score_questions(bus), self.score_words(bus)) for bus in self.get_algorithm_class().get_buses()} @lru_cache() def ordered_buses(self): @@ -400,7 +400,6 @@ class WEISurvey2025(WEISurvey): """ values = list(self.scores_per_bus().items()) values.sort(key=lambda item: -item[1][1]) - values = values[:3] return values @classmethod @@ -509,17 +508,17 @@ class WEISurveyAlgorithm2025(WEISurveyAlgorithm): else: # Current bus has not enough places. Remove the least preferred student from the bus if existing least_preferred_survey = None - least_scores = (-1, -1) + least_score = -1 # Find the least student in the bus that has a lower score than the current student for survey2 in surveys: if not survey2.information.valid or survey2.information.get_selected_bus() != bus: continue - scores2 = survey2.score(bus), survey2.score_words(bus) - if current_scores <= scores2: # Ignore better students + score2 = survey2.score_questions(bus) + if current_scores[0] <= score2: # Ignore better students continue - if least_preferred_survey is None or scores2 < least_scores: + if least_preferred_survey is None or score2 < least_score: least_preferred_survey = survey2 - least_scores = scores2 + least_score = score2 if least_preferred_survey is not None: # Remove the least student from the bus and put the current student in. diff --git a/apps/wei/tests/test_wei_algorithm_2025.py b/apps/wei/tests/test_wei_algorithm_2025.py index 571ba40d..268c5b03 100644 --- a/apps/wei/tests/test_wei_algorithm_2025.py +++ b/apps/wei/tests/test_wei_algorithm_2025.py @@ -110,6 +110,6 @@ class TestWEIAlgorithm(TestCase): max_score = buses[0][1][0] penalty += (max_score - score) ** 2 - self.assertLessEqual(max_score - score, 1) # Always less than 25 % of tolerance + self.assertLessEqual(max_score - score, 1) self.assertLessEqual(penalty / 100, 25) # Tolerance of 5 %