Nouveau module `backend/app/solver/` (mêmes conventions que
`validation/` : models purs, engine pur testé, loader seul lien avec la
base). Pour chaque créneau encore sous son effectif idéal, calcule une
liste ordonnée de bénévoles candidats — c'est la coordination qui
affecte, dans l'écran habituel ; le solveur fait juste remonter ces
candidats en tête de liste (voir le câblage frontend à suivre).
- Seule contrainte dure : aucun chevauchement de créneaux pour un même
bénévole (règle SHIFT_OVERLAP), à la fois contre l'existant et entre
deux suggestions du même calcul. L'effectif idéal est un plafond dur.
- Tout le reste est un terme de l'objectif, pas un blocage : priorité
forte aux créneaux sous l'effectif minimal, bonus vœu de poste, malus
disponibilité manquante (réutilise `validation.engine.uncovered_minutes`),
cumul de 8 h et repos (contre les affectations déjà décidées),
répartition de la charge.
- `POST /editions/{id}/solver/suggestions` (COORDINATION/référent) :
recalcule à la demande, ne persiste jamais d'affectation.
Dépendance `ortools`. 18 nouveaux tests (moteur + API), 172 tests au
vert, `ruff` et `alembic check` verts. Testé sur le jeu de démo (120
bénévoles, 100 créneaux) : ~0,3 s.
CLAUDE.md : nouvelle section 4 (solveur), renumérotation 5-10,
compétences retirées de « hors périmètre », journal étape 15.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017uUsRg7XzopkCPWimwg9ZM
98 lines
3.2 KiB
Python
98 lines
3.2 KiB
Python
"""Construit un `SolverInput` depuis la base pour une édition. Couche de
|
|
liaison : c'est le seul endroit du module `solver` qui connaît la base."""
|
|
|
|
from collections import defaultdict
|
|
|
|
from sqlmodel import Session, select
|
|
|
|
from app.models import (
|
|
Assignment,
|
|
Availability,
|
|
DayPart,
|
|
Edition,
|
|
Shift,
|
|
Volunteer,
|
|
VolunteerPositionWish,
|
|
)
|
|
from app.solver.models import SolverInput, SolverShift, SolverVolunteer
|
|
from app.validation.models import AvailabilityWindow, DayPartWindow, ShiftInfo
|
|
|
|
|
|
def build_solver_input(*, session: Session, edition: Edition) -> SolverInput:
|
|
day_parts = list(
|
|
session.exec(select(DayPart).where(DayPart.edition_id == edition.id))
|
|
)
|
|
|
|
shifts = list(session.exec(select(Shift).where(Shift.edition_id == edition.id)))
|
|
assignments = list(
|
|
session.exec(select(Assignment).where(Assignment.edition_id == edition.id))
|
|
)
|
|
shifts_by_id = {s.id: s for s in shifts}
|
|
|
|
assigned_count: dict = defaultdict(int)
|
|
shifts_by_volunteer: dict = defaultdict(list)
|
|
for a in assignments:
|
|
assigned_count[a.shift_id] += 1
|
|
shift = shifts_by_id.get(a.shift_id)
|
|
if shift is not None:
|
|
shifts_by_volunteer[a.volunteer_id].append(shift)
|
|
|
|
solver_shifts = tuple(
|
|
SolverShift(
|
|
id=str(s.id),
|
|
position_id=str(s.position_id),
|
|
starts_at=s.starts_at,
|
|
ends_at=s.ends_at,
|
|
min_staff=s.min_staff,
|
|
ideal_staff=s.ideal_staff,
|
|
already_assigned=assigned_count.get(s.id, 0),
|
|
)
|
|
for s in shifts
|
|
if assigned_count.get(s.id, 0) < s.ideal_staff
|
|
)
|
|
|
|
availabilities: dict = defaultdict(list)
|
|
for a in session.exec(
|
|
select(Availability).where(Availability.edition_id == edition.id)
|
|
):
|
|
availabilities[a.volunteer_id].append(
|
|
AvailabilityWindow(date=a.date, start_time=a.start_time, end_time=a.end_time)
|
|
)
|
|
|
|
wishes: dict = defaultdict(set)
|
|
for w in session.exec(
|
|
select(VolunteerPositionWish).join(
|
|
Volunteer, Volunteer.id == VolunteerPositionWish.volunteer_id
|
|
).where(Volunteer.edition_id == edition.id)
|
|
):
|
|
wishes[w.volunteer_id].add(str(w.position_id))
|
|
|
|
volunteers = list(
|
|
session.exec(select(Volunteer).where(Volunteer.edition_id == edition.id))
|
|
)
|
|
solver_volunteers = tuple(
|
|
SolverVolunteer(
|
|
id=str(v.id),
|
|
availability=tuple(availabilities.get(v.id, ())),
|
|
wished_position_ids=frozenset(wishes.get(v.id, ())),
|
|
existing_shifts=tuple(
|
|
ShiftInfo(id=str(s.id), starts_at=s.starts_at, ends_at=s.ends_at)
|
|
for s in shifts_by_volunteer.get(v.id, ())
|
|
),
|
|
existing_minutes=sum(
|
|
int((s.ends_at - s.starts_at).total_seconds() // 60)
|
|
for s in shifts_by_volunteer.get(v.id, ())
|
|
),
|
|
)
|
|
for v in volunteers
|
|
)
|
|
|
|
return SolverInput(
|
|
shifts=solver_shifts,
|
|
volunteers=solver_volunteers,
|
|
day_parts=tuple(
|
|
DayPartWindow(dp.code, dp.start_time, dp.end_time, dp.order)
|
|
for dp in day_parts
|
|
),
|
|
)
|