Spaces:
Sleeping
Sleeping
File size: 6,854 Bytes
c398d0b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | import re
from datetime import datetime
def get_day(timeslot):
"""
assume timeslot is Day starttime to endtime
"""
if type(timeslot) != str:
# assume it is na
return None
else:
return timeslot.split(" ")[0]
def refine_time_parsing(df, time_column):
"""
Improves the parsing of time ranges by handling edge cases and filling in missing values.
"""
def time_to_24h(t):
"""Convert 12-hour time format to 24-hour time."""
return (
datetime.strptime(t, "%I%p").time()
if "." not in t
else datetime.strptime(t, "%I.%M%p").time()
)
starts, ends = [], []
for entry in df[time_column]:
match = re.search(r"(\d+\.?\d*\w+)\s*to\s*(\d+\.?\d*\w+)", str(entry))
if match:
starts.append(time_to_24h(match.group(1)))
ends.append(time_to_24h(match.group(2)))
else:
starts.append(None)
ends.append(None)
df[f"{time_column}_Start"] = starts
df[f"{time_column}_End"] = ends
return df
def check_overlap(first_class, second_class, restrict_back_to_back=False):
"""
class: (day, start_time, end_time)
"""
# require both classes to have a valid day string
if type(first_class[0]) != str or type(second_class[0]) != str:
return False
# require both classes to have start and end times
if (
first_class[1] is None
or first_class[2] is None
or second_class[1] is None
or second_class[2] is None
):
return False
# same day check
if first_class[0] != second_class[0]:
return False
# If restricting back-to-back, treat end == start as overlap
if restrict_back_to_back:
# treat touching endpoints as overlap as well
return not (first_class[2] < second_class[1] or second_class[2] < first_class[1])
# Default: strict overlap (interval intersection)
return first_class[1] < second_class[2] and second_class[1] < first_class[2]
def check_conflict(current_schedule, new_class, restrict_back_to_back=False):
def is_touching(a, b):
# a and b are (day, start, end)
if a[0] != b[0]:
return False
if a[1] is None or a[2] is None or b[1] is None or b[2] is None:
return False
return a[2] == b[1] or b[2] == a[1]
def count_touching_pairs(schedule_rows, day):
# build list of intervals for the given day from schedule rows
intervals = []
for r in schedule_rows:
# lecture
if r.get("lecture_day") == day and r.get("Lecture_Start") is not None:
intervals.append((r.get("Lecture_Start"), r.get("Lecture_End")))
# tutorial
if r.get("tutorial_day") == day and r.get("Tutorial_Start") is not None:
intervals.append((r.get("Tutorial_Start"), r.get("Tutorial_End")))
# count touching pairs
cnt = 0
for i in range(len(intervals)):
for j in range(i + 1, len(intervals)):
if intervals[i][1] == intervals[j][0] or intervals[j][1] == intervals[i][0]:
cnt += 1
return cnt
for s in current_schedule:
# check lecture with lecture
class_1 = (s["lecture_day"], s["Lecture_Start"], s["Lecture_End"])
class_2 = (
new_class["lecture_day"],
new_class["Lecture_Start"],
new_class["Lecture_End"],
)
# Check overlap normally
if check_overlap(class_1, class_2, restrict_back_to_back):
# If overlap is a touching endpoint and we are restricting back-to-back,
# allow up to 2 touching pairs per day.
if restrict_back_to_back and is_touching(class_1, class_2):
day = class_1[0]
existing_touches = count_touching_pairs(current_schedule, day)
# if adding this touch would make more than 2 touches, reject
if existing_touches >= 2:
return True
# otherwise allow this touching pair (do not treat as conflict)
else:
return True
# check lecture with tutorial
class_1 = (s["lecture_day"], s["Lecture_Start"], s["Lecture_End"])
class_2 = (
new_class["tutorial_day"],
new_class["Tutorial_Start"],
new_class["Tutorial_End"],
)
if check_overlap(class_1, class_2, restrict_back_to_back):
if restrict_back_to_back and is_touching(class_1, class_2):
day = class_1[0]
existing_touches = count_touching_pairs(current_schedule, day)
if existing_touches >= 2:
return True
else:
return True
# check tutorial with lecture
class_1 = (s["tutorial_day"], s["Tutorial_Start"], s["Tutorial_End"])
class_2 = (
new_class["lecture_day"],
new_class["Lecture_Start"],
new_class["Lecture_End"],
)
if check_overlap(class_1, class_2, restrict_back_to_back):
if restrict_back_to_back and is_touching(class_1, class_2):
day = class_1[0]
existing_touches = count_touching_pairs(current_schedule, day)
if existing_touches >= 2:
return True
else:
return True
# check tutorial with tutorial
class_1 = (s["tutorial_day"], s["Tutorial_Start"], s["Tutorial_End"])
class_2 = (
new_class["tutorial_day"],
new_class["Tutorial_Start"],
new_class["Tutorial_End"],
)
if check_overlap(class_1, class_2, restrict_back_to_back):
if restrict_back_to_back and is_touching(class_1, class_2):
day = class_1[0]
existing_touches = count_touching_pairs(current_schedule, day)
if existing_touches >= 2:
return True
else:
return True
return False
def check_solution_is_valid(solution, num_combi, num_units):
if len(solution) != num_combi:
return False
for s in solution:
if len(s) != num_units:
return False
return True
def print_solution(solution, show_selected_days=True):
for s in solution:
selected_days = []
for c in s:
print(
f"{c['Module']} | {c['Class']} | {c['Lecture']} | {c['Tutorial']}"
)
selected_days.append(c["lecture_day"])
selected_days.append(c["tutorial_day"])
if show_selected_days:
selected_days = list(set(selected_days))
print("selected days: ", selected_days)
print("-" * 88)
|