-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #287 from appKom/269-favoriser-få-hull-i-intervjup…
…lanen-til-komitéene Legg til clustering
- Loading branch information
Showing
2 changed files
with
72 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |