mirror of
https://gitlab.crans.org/bde/nk20
synced 2025-08-22 13:17:28 +02:00
Compare commits
6 Commits
a3a9dfc812
...
8638c16b34
Author | SHA1 | Date | |
---|---|---|---|
8638c16b34
|
|||
9583cec3ff
|
|||
1ef25924a0
|
|||
e89383e3f4
|
|||
79a116d9c6
|
|||
aa75ce5c7a
|
@@ -50,15 +50,19 @@ class WEIBusInformation:
|
|||||||
self.bus.information = d
|
self.bus.information = d
|
||||||
self.bus.save()
|
self.bus.save()
|
||||||
|
|
||||||
def free_seats(self, surveys: List["WEISurvey"] = None):
|
def free_seats(self, surveys: List["WEISurvey"] = None, quotas=None):
|
||||||
size = self.bus.size
|
if not quotas:
|
||||||
already_occupied = WEIMembership.objects.filter(bus=self.bus).count()
|
size = self.bus.size
|
||||||
|
already_occupied = WEIMembership.objects.filter(bus=self.bus).count()
|
||||||
|
quotas = {self.bus: size - already_occupied}
|
||||||
|
|
||||||
|
quota = quotas[self.bus]
|
||||||
valid_surveys = sum(1 for survey in surveys if survey.information.valid
|
valid_surveys = sum(1 for survey in surveys if survey.information.valid
|
||||||
and survey.information.get_selected_bus() == self.bus) if surveys else 0
|
and survey.information.get_selected_bus() == self.bus) if surveys else 0
|
||||||
return size - already_occupied - valid_surveys
|
return quota - valid_surveys
|
||||||
|
|
||||||
def has_free_seats(self, surveys=None):
|
def has_free_seats(self, surveys=None, quotas=None):
|
||||||
return self.free_seats(surveys) > 0
|
return self.free_seats(surveys, quotas) > 0
|
||||||
|
|
||||||
|
|
||||||
class WEISurveyAlgorithm:
|
class WEISurveyAlgorithm:
|
||||||
@@ -86,14 +90,20 @@ class WEISurveyAlgorithm:
|
|||||||
"""
|
"""
|
||||||
Queryset of all first year registrations
|
Queryset of all first year registrations
|
||||||
"""
|
"""
|
||||||
return WEIRegistration.objects.filter(wei__year=cls.get_survey_class().get_year(), first_year=True)
|
if not hasattr(cls, '_registrations'):
|
||||||
|
cls._registrations = WEIRegistration.objects.filter(wei__year=cls.get_survey_class().get_year(),
|
||||||
|
first_year=True).all()
|
||||||
|
|
||||||
|
return cls._registrations
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_buses(cls) -> QuerySet:
|
def get_buses(cls) -> QuerySet:
|
||||||
"""
|
"""
|
||||||
Queryset of all buses of the associated wei.
|
Queryset of all buses of the associated wei.
|
||||||
"""
|
"""
|
||||||
return Bus.objects.filter(wei__year=cls.get_survey_class().get_year(), size__gt=0)
|
if not hasattr(cls, '_buses'):
|
||||||
|
cls._buses = Bus.objects.filter(wei__year=cls.get_survey_class().get_year(), size__gt=0).all()
|
||||||
|
return cls._buses
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_bus_information(cls, bus):
|
def get_bus_information(cls, bus):
|
||||||
@@ -135,7 +145,10 @@ class WEISurvey:
|
|||||||
"""
|
"""
|
||||||
The WEI associated to this kind of survey.
|
The WEI associated to this kind of survey.
|
||||||
"""
|
"""
|
||||||
return WEIClub.objects.get(year=cls.get_year())
|
if not hasattr(cls, '_wei'):
|
||||||
|
cls._wei = WEIClub.objects.get(year=cls.get_year())
|
||||||
|
|
||||||
|
return cls._wei
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_survey_information_class(cls):
|
def get_survey_information_class(cls):
|
||||||
|
@@ -1,13 +1,17 @@
|
|||||||
# Copyright (C) 2018-2021 by BDE ENS Paris-Saclay
|
# Copyright (C) 2018-2021 by BDE ENS Paris-Saclay
|
||||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
from functools import lru_cache
|
||||||
from random import Random
|
from random import Random
|
||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
|
from django.db.models import Q
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from .base import WEISurvey, WEISurveyInformation, WEISurveyAlgorithm, WEIBusInformation
|
from .base import WEISurvey, WEISurveyInformation, WEISurveyAlgorithm, WEIBusInformation
|
||||||
|
from ...models import WEIMembership
|
||||||
|
|
||||||
WORDS = [
|
WORDS = [
|
||||||
'13 organisé', '3ième mi temps', 'Années 2000', 'Apéro', 'BBQ', 'BP', 'Beauf', 'Binge drinking', 'Bon enfant',
|
'13 organisé', '3ième mi temps', 'Années 2000', 'Apéro', 'BBQ', 'BP', 'Beauf', 'Binge drinking', 'Bon enfant',
|
||||||
@@ -135,15 +139,31 @@ class WEISurvey2021(WEISurvey):
|
|||||||
"""
|
"""
|
||||||
return self.information.step == 20
|
return self.information.step == 20
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
@lru_cache()
|
||||||
|
def word_mean(cls, word):
|
||||||
|
"""
|
||||||
|
Calculate the mid-score given by all buses.
|
||||||
|
"""
|
||||||
|
buses = cls.get_algorithm_class().get_buses()
|
||||||
|
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(self, bus):
|
||||||
if not self.is_complete():
|
if not self.is_complete():
|
||||||
raise ValueError("Survey is not ended, can't calculate score")
|
raise ValueError("Survey is not ended, can't calculate score")
|
||||||
bus_info = self.get_algorithm_class().get_bus_information(bus)
|
|
||||||
return sum(bus_info.scores[getattr(self.information, 'word' + str(i))] for i in range(1, 21)) / 20
|
|
||||||
|
|
||||||
|
bus_info = self.get_algorithm_class().get_bus_information(bus)
|
||||||
|
# Score is the given score by the bus subtracted to the mid-score of the buses.
|
||||||
|
s = sum(bus_info.scores[getattr(self.information, 'word' + str(i))]
|
||||||
|
- self.word_mean(getattr(self.information, 'word' + str(i))) for i in range(1, 21)) / 20
|
||||||
|
return s
|
||||||
|
|
||||||
|
@lru_cache()
|
||||||
def scores_per_bus(self):
|
def scores_per_bus(self):
|
||||||
return {bus: self.score(bus) for bus in self.get_algorithm_class().get_buses()}
|
return {bus: self.score(bus) for bus in self.get_algorithm_class().get_buses()}
|
||||||
|
|
||||||
|
@lru_cache()
|
||||||
def ordered_buses(self):
|
def ordered_buses(self):
|
||||||
values = list(self.scores_per_bus().items())
|
values = list(self.scores_per_bus().items())
|
||||||
values.sort(key=lambda item: -item[1])
|
values.sort(key=lambda item: -item[1])
|
||||||
@@ -164,19 +184,63 @@ class WEISurveyAlgorithm2021(WEISurveyAlgorithm):
|
|||||||
def get_bus_information_class(cls):
|
def get_bus_information_class(cls):
|
||||||
return WEIBusInformation2021
|
return WEIBusInformation2021
|
||||||
|
|
||||||
def run_algorithm(self):
|
def run_algorithm(self, display_tqdm=False):
|
||||||
"""
|
"""
|
||||||
Gale-Shapley algorithm implementation.
|
Gale-Shapley algorithm implementation.
|
||||||
We modify it to allow buses to have multiple "weddings".
|
We modify it to allow buses to have multiple "weddings".
|
||||||
"""
|
"""
|
||||||
surveys = list(self.get_survey_class()(r) for r in self.get_registrations()) # All surveys
|
surveys = list(self.get_survey_class()(r) for r in self.get_registrations()) # All surveys
|
||||||
surveys = [s for s in surveys if s.is_complete()]
|
surveys = [s for s in surveys if s.is_complete()] # Don't consider invalid surveys
|
||||||
free_surveys = [s for s in surveys if not s.information.valid] # Remaining surveys
|
# Don't manage hardcoded people
|
||||||
|
surveys = [s for s in surveys if not hasattr(s.information, 'hardcoded') or not s.information.hardcoded]
|
||||||
|
|
||||||
|
# Reset previous algorithm run
|
||||||
|
for survey in surveys:
|
||||||
|
survey.free()
|
||||||
|
survey.save()
|
||||||
|
|
||||||
|
non_men = [s for s in surveys if s.registration.gender != 'male']
|
||||||
|
men = [s for s in surveys if s.registration.gender == 'male']
|
||||||
|
|
||||||
|
quotas = {}
|
||||||
|
registrations = self.get_registrations()
|
||||||
|
non_men_total = registrations.filter(~Q(gender='male')).count()
|
||||||
|
for bus in self.get_buses():
|
||||||
|
free_seats = bus.size - WEIMembership.objects.filter(bus=bus, registration__first_year=False).count()
|
||||||
|
quotas[bus] = 4 + int(non_men_total / registrations.count() * free_seats)
|
||||||
|
|
||||||
|
tqdm_obj = None
|
||||||
|
if display_tqdm:
|
||||||
|
from tqdm import tqdm
|
||||||
|
tqdm_obj = tqdm(total=len(non_men), desc="Non-hommes")
|
||||||
|
|
||||||
|
# Repartition for non men people first
|
||||||
|
self.make_repartition(non_men, quotas, tqdm_obj=tqdm_obj)
|
||||||
|
|
||||||
|
quotas = {}
|
||||||
|
for bus in self.get_buses():
|
||||||
|
free_seats = bus.size - WEIMembership.objects.filter(bus=bus, registration__first_year=False).count()
|
||||||
|
free_seats -= sum(1 for s in non_men if s.information.selected_bus_pk == bus.pk)
|
||||||
|
quotas[bus] = free_seats
|
||||||
|
|
||||||
|
if display_tqdm:
|
||||||
|
tqdm_obj.close()
|
||||||
|
|
||||||
|
from tqdm import tqdm
|
||||||
|
tqdm_obj = tqdm(total=len(men), desc="Hommes")
|
||||||
|
|
||||||
|
self.make_repartition(men, quotas, tqdm_obj=tqdm_obj)
|
||||||
|
|
||||||
|
if display_tqdm:
|
||||||
|
tqdm_obj.close()
|
||||||
|
|
||||||
|
def make_repartition(self, surveys, quotas=None, tqdm_obj=None):
|
||||||
|
free_surveys = surveys.copy() # Remaining surveys
|
||||||
while free_surveys: # Some students are not affected
|
while free_surveys: # Some students are not affected
|
||||||
survey = free_surveys[0]
|
survey = free_surveys[0]
|
||||||
buses = survey.ordered_buses() # Preferences of the student
|
buses = survey.ordered_buses() # Preferences of the student
|
||||||
for bus, _ignored in buses:
|
for bus, current_score in buses:
|
||||||
if self.get_bus_information(bus).has_free_seats(surveys):
|
if self.get_bus_information(bus).has_free_seats(surveys, quotas):
|
||||||
# Selected bus has free places. Put student in the bus
|
# Selected bus has free places. Put student in the bus
|
||||||
survey.select_bus(bus)
|
survey.select_bus(bus)
|
||||||
survey.save()
|
survey.save()
|
||||||
@@ -184,7 +248,6 @@ class WEISurveyAlgorithm2021(WEISurveyAlgorithm):
|
|||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
# Current bus has not enough places. Remove the least preferred student from the bus if existing
|
# Current bus has not enough places. Remove the least preferred student from the bus if existing
|
||||||
current_score = survey.score(bus)
|
|
||||||
least_preferred_survey = None
|
least_preferred_survey = None
|
||||||
least_score = -1
|
least_score = -1
|
||||||
# Find the least student in the bus that has a lower score than the current student
|
# Find the least student in the bus that has a lower score than the current student
|
||||||
@@ -206,6 +269,11 @@ class WEISurveyAlgorithm2021(WEISurveyAlgorithm):
|
|||||||
free_surveys.append(least_preferred_survey)
|
free_surveys.append(least_preferred_survey)
|
||||||
survey.select_bus(bus)
|
survey.select_bus(bus)
|
||||||
survey.save()
|
survey.save()
|
||||||
|
free_surveys.remove(survey)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"User {survey.registration.user} has no free seat")
|
raise ValueError(f"User {survey.registration.user} has no free seat")
|
||||||
|
|
||||||
|
if tqdm_obj is not None:
|
||||||
|
tqdm_obj.n = len(surveys) - len(free_surveys)
|
||||||
|
tqdm_obj.refresh()
|
@@ -24,7 +24,14 @@ class Command(BaseCommand):
|
|||||||
sid = transaction.savepoint()
|
sid = transaction.savepoint()
|
||||||
|
|
||||||
algorithm = CurrentSurvey.get_algorithm_class()()
|
algorithm = CurrentSurvey.get_algorithm_class()()
|
||||||
algorithm.run_algorithm()
|
|
||||||
|
try:
|
||||||
|
from tqdm import tqdm
|
||||||
|
display_tqdm = True
|
||||||
|
except ImportError:
|
||||||
|
display_tqdm = False
|
||||||
|
|
||||||
|
algorithm.run_algorithm(display_tqdm=display_tqdm)
|
||||||
|
|
||||||
output = options['output']
|
output = options['output']
|
||||||
registrations = algorithm.get_registrations()
|
registrations = algorithm.get_registrations()
|
||||||
@@ -34,8 +41,13 @@ class Command(BaseCommand):
|
|||||||
for bus, members in per_bus.items():
|
for bus, members in per_bus.items():
|
||||||
output.write(bus.name + "\n")
|
output.write(bus.name + "\n")
|
||||||
output.write("=" * len(bus.name) + "\n")
|
output.write("=" * len(bus.name) + "\n")
|
||||||
|
order = -1
|
||||||
for r in members:
|
for r in members:
|
||||||
output.write(r.user.username + "\n")
|
survey = CurrentSurvey(r)
|
||||||
|
for order, (b, _score) in enumerate(survey.ordered_buses()):
|
||||||
|
if b == bus:
|
||||||
|
break
|
||||||
|
output.write(f"{r.user.username} ({order + 1})\n")
|
||||||
output.write("\n")
|
output.write("\n")
|
||||||
|
|
||||||
if not options['doit']:
|
if not options['doit']:
|
||||||
|
Reference in New Issue
Block a user