Artiste : heure de passage ; vœux d'artistes pris en compte par le solveur
Artist gagne start_time/end_time (end <= start ⇒ finit le lendemain, même convention que Shift/DayPart/Availability). Migration 0008. Le solveur (app/solver/) résout maintenant les fenêtres de passage des artistes que chaque bénévole veut voir (VolunteerArtist) et pénalise toute suggestion qui chevaucherait un passage souhaité — le bénévole raterait l'artiste. Comme le reste du scoring, c'est un terme de l'objectif, pas un blocage : pour un créneau urgent sans autre candidat, le solveur propose quand même, quitte à ce que ce soit un pis-aller. - SolverVolunteer.wished_artist_windows (fenêtres ShiftInfo précalculées par le loader depuis Artist + VolunteerArtist). - Nouveau malus _ARTIST_CONFLICT_PENALTY dans engine.py. - Tests : moteur (conflit détecté/ignoré hors chevauchement/toujours suggéré en dernier recours) + intégration API bout en bout. 176 tests, ruff et alembic check au vert. CLAUDE.md mis à jour (§2 et §4). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017uUsRg7XzopkCPWimwg9ZM
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
e47c29d35b
commit
fea9d37019
@@ -7,9 +7,9 @@ contente de faire remonter ces candidats en tête de liste).
|
||||
|
||||
Contrainte dure : aucun chevauchement de créneaux pour un même bénévole (règle
|
||||
`SHIFT_OVERLAP`, cf. `app.validation`). Tout le reste (disponibilité, cumul de
|
||||
8 h, repos, vœu de poste, équité de charge) est un terme de l'objectif, pas
|
||||
une contrainte — comme le moteur de validation, qui reste seul juge au moment
|
||||
où la coordination confirme une affectation.
|
||||
8 h, repos, vœu de poste, vœu d'artiste, équité de charge) est un terme de
|
||||
l'objectif, pas une contrainte — comme le moteur de validation, qui reste seul
|
||||
juge au moment où la coordination confirme une affectation.
|
||||
"""
|
||||
|
||||
import datetime as dt
|
||||
@@ -41,6 +41,8 @@ _AVAIL_PENALTY_PER_HALF_HOUR = 5
|
||||
_DAILY_OVERAGE_PENALTY_PER_MIN = 2
|
||||
_REST_PENALTY = 60
|
||||
_FAIRNESS_PENALTY_PER_HOUR = 2
|
||||
# Le créneau lui ferait manquer le passage d'un artiste qu'il veut voir.
|
||||
_ARTIST_CONFLICT_PENALTY = 80
|
||||
|
||||
|
||||
def _overlaps(a: ShiftInfo, b: ShiftInfo) -> bool:
|
||||
@@ -114,6 +116,10 @@ def _score(
|
||||
)
|
||||
if _has_insufficient_rest(volunteer, shift):
|
||||
score -= _REST_PENALTY
|
||||
if any(
|
||||
_overlaps(shift.shift_info, passage) for passage in volunteer.wished_artist_windows
|
||||
):
|
||||
score -= _ARTIST_CONFLICT_PENALTY
|
||||
score -= (_FAIRNESS_PENALTY_PER_HOUR * volunteer.existing_minutes) // 60
|
||||
return score
|
||||
|
||||
|
||||
@@ -1,23 +1,39 @@
|
||||
"""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."""
|
||||
|
||||
import datetime as dt
|
||||
from collections import defaultdict
|
||||
|
||||
from sqlmodel import Session, select
|
||||
|
||||
from app.models import (
|
||||
Artist,
|
||||
Assignment,
|
||||
Availability,
|
||||
DayPart,
|
||||
Edition,
|
||||
Shift,
|
||||
Volunteer,
|
||||
VolunteerArtist,
|
||||
VolunteerPositionWish,
|
||||
)
|
||||
from app.solver.models import SolverInput, SolverShift, SolverVolunteer
|
||||
from app.validation.models import AvailabilityWindow, DayPartWindow, ShiftInfo
|
||||
|
||||
|
||||
def _artist_window(artist: Artist) -> ShiftInfo:
|
||||
"""Fenêtre datée du passage de l'artiste (même convention que les
|
||||
créneaux : `end_time <= start_time` ⇒ le passage finit le lendemain)."""
|
||||
starts_at = dt.datetime.combine(artist.performance_date, artist.start_time)
|
||||
end_date = (
|
||||
artist.performance_date
|
||||
if artist.end_time > artist.start_time
|
||||
else artist.performance_date + dt.timedelta(days=1)
|
||||
)
|
||||
ends_at = dt.datetime.combine(end_date, artist.end_time)
|
||||
return ShiftInfo(id=str(artist.id), starts_at=starts_at, ends_at=ends_at)
|
||||
|
||||
|
||||
def build_solver_input(*, session: Session, edition: Edition) -> SolverInput:
|
||||
day_parts = list(
|
||||
session.exec(select(DayPart).where(DayPart.edition_id == edition.id))
|
||||
@@ -67,6 +83,20 @@ def build_solver_input(*, session: Session, edition: Edition) -> SolverInput:
|
||||
):
|
||||
wishes[w.volunteer_id].add(str(w.position_id))
|
||||
|
||||
artists_by_id = {
|
||||
a.id: a
|
||||
for a in session.exec(select(Artist).where(Artist.edition_id == edition.id))
|
||||
}
|
||||
artist_wishes: dict = defaultdict(list)
|
||||
for w in session.exec(
|
||||
select(VolunteerArtist).join(
|
||||
Volunteer, Volunteer.id == VolunteerArtist.volunteer_id
|
||||
).where(Volunteer.edition_id == edition.id)
|
||||
):
|
||||
artist = artists_by_id.get(w.artist_id)
|
||||
if artist is not None:
|
||||
artist_wishes[w.volunteer_id].append(_artist_window(artist))
|
||||
|
||||
volunteers = list(
|
||||
session.exec(select(Volunteer).where(Volunteer.edition_id == edition.id))
|
||||
)
|
||||
@@ -75,6 +105,7 @@ def build_solver_input(*, session: Session, edition: Edition) -> SolverInput:
|
||||
id=str(v.id),
|
||||
availability=tuple(availabilities.get(v.id, ())),
|
||||
wished_position_ids=frozenset(wishes.get(v.id, ())),
|
||||
wished_artist_windows=tuple(artist_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, ())
|
||||
|
||||
@@ -33,6 +33,10 @@ class SolverVolunteer:
|
||||
id: str
|
||||
availability: tuple[AvailabilityWindow, ...] = ()
|
||||
wished_position_ids: frozenset[str] = frozenset()
|
||||
# Passages des artistes que le bénévole veut voir (heure de passage),
|
||||
# sous forme de fenêtres datées : un créneau qui chevauche l'une d'elles
|
||||
# lui ferait manquer le passage.
|
||||
wished_artist_windows: tuple[ShiftInfo, ...] = ()
|
||||
# Créneaux déjà occupés (affectations existantes, décisions figées) :
|
||||
# comptent pour chevauchement, cumul journalier, repos.
|
||||
existing_shifts: tuple[ShiftInfo, ...] = ()
|
||||
|
||||
Reference in New Issue
Block a user