diff --git a/.env.local.template b/.env.local.template index 3e58d9e5..a68bc96b 100644 --- a/.env.local.template +++ b/.env.local.template @@ -12,3 +12,6 @@ AUTH0_ISSUER=#issuer base url, example: example.us.auth0.com AWS_SECRET_ACCESS_KEY=#aws secret access key AWS_ACCESS_KEY_ID=#aws access key id + +TWILIO_ACCOUNT_SID=#twilio account sid +TWILIO_AUTH_TOKEN=#twilio auth token diff --git a/.github/workflows/algorithm.yml b/.github/workflows/algorithm.yml index 96ba20c3..ce22a1e0 100644 --- a/.github/workflows/algorithm.yml +++ b/.github/workflows/algorithm.yml @@ -1,10 +1,10 @@ on: push: paths: - - "algorithm/*" + - "algorithm/**" pull_request: paths: - - "algorithm/*" + - "algorithm/**" workflow_dispatch: jobs: diff --git a/algorithm/bridge/fetch_applicants_and_committees.py b/algorithm/bridge/fetch_applicants_and_committees.py index b01cf645..e50a4662 100644 --- a/algorithm/bridge/fetch_applicants_and_committees.py +++ b/algorithm/bridge/fetch_applicants_and_committees.py @@ -1,29 +1,61 @@ from pymongo import MongoClient from dotenv import load_dotenv -from datetime import datetime, timezone +from datetime import datetime, timedelta, timezone import os import certifi +from typing import List, Dict + +from mip_matching.Committee import Committee +from mip_matching.TimeInterval import TimeInterval +from mip_matching.Applicant import Applicant +from mip_matching.match_meetings import match_meetings, MeetingMatch def main(): periods = fetch_periods() - #Sjekker om perioden er etter søknadstiden og før intervjuslutt og hasSentInterviewtimes er false, og returnerer søkere og komitétider dersom det er tilfelle for period in periods: periodId = str(period["_id"]) - interview_end = datetime.fromisoformat(period["interviewPeriod"]["end"].replace("Z", "+00:00")) application_end = datetime.fromisoformat(period["applicationPeriod"]["end"].replace("Z", "+00:00")) - now = datetime.now(timezone.utc) + - if application_end > now and period["hasSentInterviewTimes"] == False and interview_end < now: + #or period["name"] == "Juli Opptak" + if (application_end < now and period["hasSentInterviewTimes"] == False): applicants = fetch_applicants(periodId) committee_times = fetch_committee_times(periodId) - print(applicants) - print(committee_times) - return applicants, committee_times + committee_objects = create_committee_objects(committee_times) + + all_committees = {committee.name: committee for committee in committee_objects} + + applicant_objects = create_applicant_objects(applicants, all_committees) + + print(applicant_objects) + print(committee_objects) + + match_result = match_meetings(applicant_objects, committee_objects) + + send_to_db(match_result, applicants, periodId) + return match_result +def send_to_db(match_result: MeetingMatch, applicants: List[dict], periodId): + load_dotenv() + formatted_results = format_match_results(match_result, applicants, periodId) + print("Sending to db") + print(formatted_results) + + mongo_uri = os.getenv("MONGODB_URI") + db_name = os.getenv("DB_NAME") + client = MongoClient(mongo_uri, tlsCAFile=certifi.where()) + + db = client[db_name] # type: ignore + + collection = db["interviews"] + + collection.insert_many(formatted_results) + + client.close() def connect_to_db(collection_name): load_dotenv() @@ -40,37 +72,95 @@ def connect_to_db(collection_name): return collection, client def fetch_periods(): - collection, client = connect_to_db("period") - - periods = collection.find() + collection, client = connect_to_db("periods") - periods = list(periods) + periods = list(collection.find()) client.close() return periods def fetch_applicants(periodId): - collection, client = connect_to_db("applicant") - - applicants = collection.find({"periodId": periodId}) + collection, client = connect_to_db("applications") - applicants = list(applicants) + applicants = list(collection.find({"periodId": periodId})) client.close() return applicants def fetch_committee_times(periodId): - collection, client = connect_to_db("committee") + collection, client = connect_to_db("committees") - committee_times = collection.find({"periodId": periodId}) - - committee_times = list(committee_times) + committee_times = list(collection.find({"periodId": periodId})) client.close() return committee_times +def format_match_results(match_results: MeetingMatch, applicants: List[dict], periodId) -> List[Dict]: + transformed_results = {} + + for result in match_results['matchings']: + applicant_id = str(result[0]) + + if applicant_id not in transformed_results: + transformed_results[applicant_id] = { + "periodId": periodId, + "applicantId": applicant_id, + "interviews": [] + } + + committee = result[1] + time_interval = result[2] + start = time_interval.start.isoformat() + end = time_interval.end.isoformat() + + transformed_results[applicant_id]["interviews"].append({ + "start": start, + "end": end, + "committeeName": committee.name + }) + + return list(transformed_results.values()) + +def create_applicant_objects(applicants_data: List[dict], all_committees: dict[str, Committee]) -> set[Applicant]: + applicants = set() + for data in applicants_data: + applicant = Applicant(name=str(data['_id'])) + + optional_committee_names = data.get('optionalCommittees', []) + optional_committees = {all_committees[name] for name in optional_committee_names if name in all_committees} + applicant.add_committees(optional_committees) + + preferences = data.get('preferences', {}) + preference_committees = {all_committees[committee_name] for committee_name in preferences.values() if committee_name in all_committees} + applicant.add_committees(preference_committees) + + for interval_data in data['selectedTimes']: + interval = TimeInterval( + start=datetime.fromisoformat(interval_data['start'].replace("Z", "+00:00")), + end=datetime.fromisoformat(interval_data['end'].replace("Z", "+00:00")) + ) + applicant.add_interval(interval) + + applicants.add(applicant) + return applicants + +def create_committee_objects(committee_data: List[dict]) -> set[Committee]: + committees = set() + for data in committee_data: + committee = Committee(name=data['committee'], interview_length=timedelta(minutes=int(data["timeslot"]))) + for interval_data in data['availabletimes']: + interval = TimeInterval( + start=datetime.fromisoformat(interval_data['start'].replace("Z", "+00:00")), + end=datetime.fromisoformat(interval_data['end'].replace("Z", "+00:00")) + ) + capacity = interval_data.get('capacity', 1) + committee.add_interval(interval, capacity) + committees.add(committee) + return committees + + if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/algorithm/src/Modellering.md b/algorithm/src/Modellering.md index 3fea01e0..30fc6439 100644 --- a/algorithm/src/Modellering.md +++ b/algorithm/src/Modellering.md @@ -10,43 +10,53 @@ ## Variabler `p` + - Person `k` + - Komité `t` + - Timeslot (Må gjøres til intervaller etter hvert) `m(p, k, t)` + - Binær variabel - Person `p` har møte med komité `k` i timeslot `t` ## Hjelpevariabler `c(p, t)` + - Binære variabler - Tidspunkt `t` passer for person `p` `c(k, t)` + - Heltallsvariabel - Kapasitet for komité `k` på tidspunkt `t` (hvor mange intervju de kan ha på det gitte tidspunktet) ## Begrensninger For alle `p`: - -- `m(p, k, t) <= 1` dersom - - `p` har søkt på komité `k` - - `c(p, t) => 1` - - `c(k, t) => 1` + +- `m(p, k, t_1) + m(p, k, t_2) < 2` for alle gyldige `k, t_1` og `k, t_2`, hvor t_1 og t_2 overlapper eller er innenfor et gitt buffer-intervall. +- `m(p, k, t) <= 1` dersom + - `p` har søkt på komité `k` + - `c(p, t) => 1` + - `c(k, t) => 1` - `m(p, k, t) <= 0` ellers For alle `k`: -- `sum(m(p, k, t)) <= c(k, t)` for alle personer `p` og tidspunkt `t` - +- `sum(m(p, k, t)) <= c(k, t)` for alle personer `p` og tidspunkt `t` ## Mål Maksimere `sum(m(p, k, t))` for alle `p`, `k` og `t` + +### Sekundærmål + +- [Ikke enda implementert] La det være færrest mulig og minst mulig mellomrom mellom intervjuene for komitéene. diff --git a/algorithm/src/mip_matching/Applicant.py b/algorithm/src/mip_matching/Applicant.py index d769ebc7..6172e5fb 100644 --- a/algorithm/src/mip_matching/Applicant.py +++ b/algorithm/src/mip_matching/Applicant.py @@ -83,4 +83,4 @@ def __str__(self) -> str: return self.name def __repr__(self) -> str: - return str(self) + return str(self) \ No newline at end of file diff --git a/algorithm/src/mip_matching/Committee.py b/algorithm/src/mip_matching/Committee.py index 115debc4..8f2c28f5 100644 --- a/algorithm/src/mip_matching/Committee.py +++ b/algorithm/src/mip_matching/Committee.py @@ -1,17 +1,10 @@ from __future__ import annotations from datetime import timedelta -import sys -print(sys.path) -print(__name__) -# sys.path.append("C:\\Users\\Jørgen Galdal\\Documents\\lokalSkoleprogrammering\\appkom\\OnlineOpptak\\algorithm\\mip_matching") from mip_matching.Applicant import Applicant +from mip_matching.TimeInterval import TimeInterval from typing import Iterator -# from typing import TYPE_CHECKING -# if TYPE_CHECKING: -# # Unngår cyclic import -from mip_matching.TimeInterval import TimeInterval class Committee: @@ -84,4 +77,4 @@ def __repr__(self): if __name__ == "__main__": - print("running") + print("running") \ No newline at end of file diff --git a/algorithm/src/mip_matching/TimeInterval.py b/algorithm/src/mip_matching/TimeInterval.py index f2156d5e..9228aa28 100644 --- a/algorithm/src/mip_matching/TimeInterval.py +++ b/algorithm/src/mip_matching/TimeInterval.py @@ -64,6 +64,9 @@ def get_contained_slots(self, slots: list[TimeInterval]): def divide(self, length: timedelta) -> list[TimeInterval]: return TimeInterval.divide_interval(self, length) + def is_within_distance(self, other: TimeInterval, distance: timedelta) -> bool: + return (self.end <= other.start and self.end + distance > other.start) or (other.end <= self.start and other.end + distance > self.start) + @staticmethod def divide_interval(interval: TimeInterval, length: timedelta) -> list[TimeInterval]: """ @@ -85,29 +88,3 @@ def divide_interval(interval: TimeInterval, length: timedelta) -> list[TimeInter local_end += length return result - - -""" -Dette er gammel kode som nå er flyttet til de passende komité-/søker-klassene. -Foreløpig beholdt for referanse. -""" -# class TimeIntervals: -# def __init__(self, initial_list: list[TimeInterval] = None): -# self.list: list[TimeInterval] = initial_list if initial_list else [] - -# def add(self, interval: TimeInterval): -# self.list.append(interval) - -# def recursive_intersection(self, other: TimeIntervals): -# """ -# Returnerer alle tidsintervallene i *other* som er inneholdt i et av *self* sine intervaller""" -# result = TimeIntervals() - -# for self_interval, other_interval in itertools.product(self.list, other.list): -# if self_interval.contains(other_interval): -# result.add(other_interval) - -# return result - -# def __iter__(self): -# return self.list.__iter__() diff --git a/algorithm/src/mip_matching/match_meetings.py b/algorithm/src/mip_matching/match_meetings.py index 9bbd512e..27f6d6bb 100644 --- a/algorithm/src/mip_matching/match_meetings.py +++ b/algorithm/src/mip_matching/match_meetings.py @@ -5,7 +5,21 @@ from mip_matching.Applicant import Applicant import mip -# from typing import TypedDict +from datetime import timedelta, time +from itertools import combinations + +from mip_matching.utils import subtract_time + + +# Hvor stort buffer man ønsker å ha mellom intervjuene +APPLICANT_BUFFER_LENGTH = timedelta(minutes=15) + +# Et mål på hvor viktig det er at intervjuer er i nærheten av hverandre +CLUSTERING_WEIGHT = 0.001 + +# Når på dagen man helst vil ha intervjuene rundt +CLUSTERING_TIME_BASELINE = time(12, 00) +MAX_SCALE_CLUSTERING_TIME = timedelta(seconds=43200) # TODO: Rename variable class MeetingMatch(TypedDict): @@ -20,7 +34,7 @@ def match_meetings(applicants: set[Applicant], committees: set[Committee]) -> Me """Matches meetings and returns a MeetingMatch-object""" model = mip.Model(sense=mip.MAXIMIZE) - m = {} + m: dict[tuple[Applicant, Committee, TimeInterval], mip.Var] = {} # Lager alle maksimeringsvariabler for applicant in applicants: @@ -44,22 +58,38 @@ def match_meetings(applicants: set[Applicant], committees: set[Committee]) -> Me # type: ignore for interval in applicant.get_fitting_committee_slots(committee)) <= 1 - # Legger inn begrensninger for at en person kun kan ha ett intervju på hvert tidspunkt + # Legger inn begrensninger for at en søker ikke kan ha overlappende intervjutider + # og minst har et buffer mellom hvert intervju som angitt for applicant in applicants: - potential_intervals = set() + potential_interviews: set[tuple[Committee, TimeInterval]] = set() for applicant_candidate, committee, interval in m: if applicant == applicant_candidate: - potential_intervals.add(interval) + potential_interviews.add((committee, interval)) - for interval in potential_intervals: + for interview_a, interview_b in combinations(potential_interviews, r=2): + if interview_a[1].intersects(interview_b[1]) or interview_a[1].is_within_distance(interview_b[1], APPLICANT_BUFFER_LENGTH): + model += m[(applicant, *interview_a)] + \ + m[(applicant, *interview_b)] <= 1 # type: ignore - model += mip.xsum(m[(applicant, committee, interval)] - for committee in applicant.get_committees() - # type: ignore - if (applicant, committee, interval) in m) <= 1 + # Legger til sekundærmål om at man ønsker å sentrere intervjuer rundt CLUSTERING_TIME_BASELINE + clustering_objectives = [] - # Setter mål til å være maksimering av antall møter - model.objective = mip.maximize(mip.xsum(m.values())) + for name, variable in m.items(): + applicant, committee, interval = name + if interval.start.time() < CLUSTERING_TIME_BASELINE: + relative_distance_from_baseline = subtract_time(CLUSTERING_TIME_BASELINE, + interval.end.time()) / MAX_SCALE_CLUSTERING_TIME + else: + relative_distance_from_baseline = subtract_time(interval.start.time(), + CLUSTERING_TIME_BASELINE) / MAX_SCALE_CLUSTERING_TIME + + clustering_objectives.append( + CLUSTERING_WEIGHT * relative_distance_from_baseline * variable) # type: ignore + + # Setter mål til å være maksimering av antall møter + # med sekundærmål om å samle intervjuene rundt CLUSTERING_TIME_BASELINE + model.objective = mip.maximize( + mip.xsum(m.values()) + mip.xsum(clustering_objectives)) # Kjør optimeringen solver_status = model.optimize() diff --git a/algorithm/src/mip_matching/utils.py b/algorithm/src/mip_matching/utils.py new file mode 100644 index 00000000..b248ae3e --- /dev/null +++ b/algorithm/src/mip_matching/utils.py @@ -0,0 +1,41 @@ +from mip_matching.Applicant import Applicant +from mip_matching.Committee import Committee +from mip_matching.TimeInterval import TimeInterval + +from datetime import time, date, datetime, timedelta + + +def group_by_committee(meetings: list[tuple[Applicant, Committee, TimeInterval]]) -> dict[Committee, list[tuple[Applicant, Committee, TimeInterval]]]: + result = {} + + for applicant, committee, interval in meetings: + if committee not in result: + result[committee] = [] + + result[committee].append((applicant, committee, interval)) + + return result + + +def measure_clustering(meetings: list[tuple[Applicant, Committee, TimeInterval]]) -> int: + grouped_meetings = group_by_committee(meetings) + + holes = 0 + + for _, committee_meetings in grouped_meetings.items(): + committee_meetings.sort(key=lambda meeting: meeting[2].end) + + previous_interval: TimeInterval = committee_meetings[0][2] + for _, _, interval in committee_meetings[1:]: + if not previous_interval.is_within_distance(interval, timedelta(minutes=1)): + holes += 1 + previous_interval = interval + + return holes + + +def subtract_time(minuend: time, subtrahend: time) -> timedelta: + minuend_date = datetime.combine(date.min, minuend) + subtrahend_date = datetime.combine(date.min, subtrahend) + + return minuend_date - subtrahend_date diff --git a/algorithm/tests/mip_test.py b/algorithm/tests/mip_test.py index a8b3a9f8..ec173ea2 100644 --- a/algorithm/tests/mip_test.py +++ b/algorithm/tests/mip_test.py @@ -1,6 +1,5 @@ from __future__ import annotations from datetime import datetime, timedelta, date, time -# import ..algorithm.mip_matching.core.Applicant.py as applicant from mip_matching.TimeInterval import TimeInterval from mip_matching.Committee import Committee @@ -12,6 +11,7 @@ import unittest import random +from itertools import combinations def print_matchings(committees: list[Committee], @@ -59,6 +59,20 @@ def check_constraints(self, matchings: list[tuple[Applicant, Committee, TimeInte self.assertGreaterEqual(committee.get_capacity(interval), load, f"Constraint \"Number of interviews per slot per committee cannot exceed capacity\" failed for Committee {committee} and interval {interval}") + # Overlapping interviews per applicant + interviews_per_applicant: dict[Applicant, + set[tuple[Committee, TimeInterval]]] = {} + for applicant, committee, interval in matchings: + if applicant not in interviews_per_applicant: + interviews_per_applicant[applicant] = set() + + interviews_per_applicant[applicant].add((committee, interval)) + + for applicant, interviews in interviews_per_applicant.items(): + for interview_a, interview_b in combinations(interviews, r=2): + self.assertFalse(interview_a[1].intersects(interview_b[1]), f"Constraint \"Applicant cannot have time-overlapping interviews\" failed for { + applicant}'s interviews with {interview_a[0]} ({interview_a[1]}) and {interview_b[0]} ({interview_b[1]})") + def test_fixed_small(self): """Small, fixed test with all capacities set to one""" diff --git a/components/CommitteeAboutCard.tsx b/components/CommitteeAboutCard.tsx index 502719b3..6699e2b7 100644 --- a/components/CommitteeAboutCard.tsx +++ b/components/CommitteeAboutCard.tsx @@ -15,19 +15,21 @@ const CommitteeAboutCard = ({ return (
{email}
{application_description || "Ingen opptaksbeskrivelse"} diff --git a/components/applicantoverview/ApplicantCard.tsx b/components/applicantoverview/ApplicantCard.tsx index b77f73c9..d19e3d0b 100644 --- a/components/applicantoverview/ApplicantCard.tsx +++ b/components/applicantoverview/ApplicantCard.tsx @@ -1,7 +1,7 @@ import { useState } from "react"; import { applicantType } from "../../lib/types/types"; import { ChevronDownIcon } from "@heroicons/react/24/outline"; -import { changeDisplayName, getBankomValue } from "../../lib/utils/toString"; +import { changeDisplayName } from "../../lib/utils/toString"; interface Props { applicant: applicantType | undefined; @@ -71,11 +71,9 @@ const ApplicantCard = ({ applicant, includePreferences }: Props) => {
- Ønsker å være økonomiansvarlig: {getBankomValue(applicant?.bankom)} -
-Ønsker å være økonomiansvarlig: {applicant?.bankom}
+{applicant?.about}
{`${interviewsPlanned} intervjuer planlagt`}
+{`${interviewsPlanned} / ${numberOfApplications} intervjuer planlagt`}
- For å sende en egendefinert melding må du først fylle ut intervju - tider for valgt komitee. -
- )} - - {committeeHasSubmitedTimes && !committeeHasSubmitedMessage && ( -{message}
-