Spaces:
Sleeping
Sleeping
Update timetable.py
Browse files- timetable.py +52 -54
timetable.py
CHANGED
|
@@ -1,64 +1,62 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
-
import numpy as np
|
| 3 |
import random
|
|
|
|
| 4 |
|
| 5 |
TIME_SLOTS = ["9:00-10:30", "10:30-12:00", "12:00-1:30", "1:30-3:00"]
|
| 6 |
DAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
|
| 7 |
|
| 8 |
def generate_timetable(courses_file, rooms_file):
|
| 9 |
-
courses_df = pd.read_csv(courses_file)
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
timetable_dict = {}
|
| 13 |
-
|
| 14 |
for cls in courses_df["Class"].unique():
|
| 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 |
-
print(f"Could not assign {block['Course']} for {cls}")
|
| 59 |
-
|
| 60 |
-
# Convert timetable to DataFrame
|
| 61 |
-
df = pd.DataFrame(timetable, index=TIME_SLOTS)
|
| 62 |
-
timetable_dict[cls] = df
|
| 63 |
-
|
| 64 |
return timetable_dict
|
|
|
|
| 1 |
import pandas as pd
|
|
|
|
| 2 |
import random
|
| 3 |
+
from collections import defaultdict
|
| 4 |
|
| 5 |
TIME_SLOTS = ["9:00-10:30", "10:30-12:00", "12:00-1:30", "1:30-3:00"]
|
| 6 |
DAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
|
| 7 |
|
| 8 |
def generate_timetable(courses_file, rooms_file):
|
| 9 |
+
courses_df = pd.read_csv(courses_file, skipinitialspace=True)
|
| 10 |
+
courses_df.columns = courses_df.columns.str.strip()
|
| 11 |
+
|
| 12 |
+
rooms_df = pd.read_csv(rooms_file, skipinitialspace=True)
|
| 13 |
+
rooms_df.columns = rooms_df.columns.str.strip()
|
| 14 |
+
rooms_list = rooms_df["Room"].tolist()
|
| 15 |
+
|
| 16 |
timetable_dict = {}
|
| 17 |
+
|
| 18 |
for cls in courses_df["Class"].unique():
|
| 19 |
+
cls_courses = courses_df[courses_df["Class"] == cls].copy()
|
| 20 |
+
|
| 21 |
+
# Prepare empty timetable
|
| 22 |
+
timetable = {day: {slot: None for slot in TIME_SLOTS} for day in DAYS}
|
| 23 |
+
|
| 24 |
+
# Keep track of teacher and room occupancy to avoid conflicts
|
| 25 |
+
teacher_schedule = {day: [] for day in DAYS}
|
| 26 |
+
room_schedule = {day: [] for day in DAYS}
|
| 27 |
+
|
| 28 |
+
for idx, row in cls_courses.iterrows():
|
| 29 |
+
course = row["Course"]
|
| 30 |
+
hours = int(row["Hours"])
|
| 31 |
+
teacher = row["Teacher"]
|
| 32 |
+
|
| 33 |
+
# How many 1.5-hour slots required
|
| 34 |
+
slots_needed = hours // 1.5
|
| 35 |
+
if hours % 1.5 != 0:
|
| 36 |
+
slots_needed += 1
|
| 37 |
+
|
| 38 |
+
slots_assigned = 0
|
| 39 |
+
|
| 40 |
+
attempts = 0
|
| 41 |
+
while slots_assigned < slots_needed and attempts < 100:
|
| 42 |
+
day = random.choice(DAYS)
|
| 43 |
+
slot = random.choice(TIME_SLOTS)
|
| 44 |
+
|
| 45 |
+
# Check conflicts
|
| 46 |
+
if timetable[day][slot] is None and teacher not in teacher_schedule[day] and len(room_schedule[day]) < len(rooms_list):
|
| 47 |
+
room = random.choice([r for r in rooms_list if r not in room_schedule[day]])
|
| 48 |
+
|
| 49 |
+
timetable[day][slot] = {
|
| 50 |
+
"Course": course,
|
| 51 |
+
"Teacher": teacher,
|
| 52 |
+
"Room": room
|
| 53 |
+
}
|
| 54 |
+
teacher_schedule[day].append(teacher)
|
| 55 |
+
room_schedule[day].append(room)
|
| 56 |
+
slots_assigned += 1
|
| 57 |
+
|
| 58 |
+
attempts += 1
|
| 59 |
+
|
| 60 |
+
timetable_dict[cls] = timetable
|
| 61 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
return timetable_dict
|