Spaces:
Runtime error

File size: 7,817 Bytes
c4738d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
198
199
200
201
202
203
204
205
206
207
208
import streamlit as st
import pandas as pd
from ortools.sat.python import cp_model

# Initialize session state if not already done
if "timetable" not in st.session_state:
    st.session_state.timetable = pd.DataFrame(columns=["Day", "Time", "Subject", "Teacher", "Room"])

if "subjects" not in st.session_state:
    st.session_state.subjects = {}

if "teachers" not in st.session_state:
    st.session_state.teachers = []

if "rooms" not in st.session_state:
    st.session_state.rooms = []

if "teacher_constraints" not in st.session_state:
    st.session_state.teacher_constraints = {}

# App title
st.title("Subject Timetable Generator")

# Sidebar inputs to add data
st.sidebar.header("Add Data")

# Add subjects with weekly slots
subject = st.sidebar.text_input("Enter Subject Name")
weekly_slots = st.sidebar.number_input("Number of Weekly Slots", min_value=1, max_value=10, step=1)
if st.sidebar.button("Add Subject"):
    if subject and weekly_slots > 0:
        st.session_state.subjects[subject] = weekly_slots
        st.success(f"Added Subject: {subject} with {weekly_slots} slots per week")
    else:
        st.error("Please provide a valid subject and weekly slots.")

# Add teachers
teacher = st.sidebar.text_input("Enter Teacher Name")
if st.sidebar.button("Add Teacher"):
    if teacher:
        st.session_state.teachers.append(teacher)
        st.success(f"Added Teacher: {teacher}")
    else:
        st.error("Teacher name cannot be empty!")

# Add rooms
room = st.sidebar.text_input("Enter Room Name")
if st.sidebar.button("Add Room"):
    if room:
        st.session_state.rooms.append(room)
        st.success(f"Added Room: {room}")
    else:
        st.error("Room name cannot be empty!")

# Add teacher constraints
teacher_name = st.sidebar.selectbox("Select Teacher for Constraints", st.session_state.teachers)
no_class_after = st.sidebar.time_input("No Class After (HH:MM)", value=pd.Timestamp("15:30"))
no_class_on_days = st.sidebar.multiselect("No Class on Days", ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"])
if st.sidebar.button("Add Teacher Constraints"):
    if teacher_name:
        st.session_state.teacher_constraints[teacher_name] = {
            "no_class_after": no_class_after,
            "no_class_on_days": no_class_on_days
        }
        st.success(f"Added Constraints for Teacher: {teacher_name}")
    else:
        st.error("Teacher name cannot be empty!")

# Show current data
st.write("### Current Data")
st.write("**Subjects:**", st.session_state.subjects)
st.write("**Teachers:**", st.session_state.teachers)
st.write("**Rooms:**", st.session_state.rooms)
st.write("**Teacher Constraints:**", st.session_state.teacher_constraints)

# Timetable generation logic with debugging
def generate_timetable(subjects, teachers, rooms, time_slots, days, teacher_constraints):
    model = cp_model.CpModel()
    schedule = {}

    # Create variables for each subject-teacher-room-time-slot combination
    for subject, slots in subjects.items():
        for teacher in teachers:
            for room in rooms:
                for day in days:
                    for time_slot in time_slots:
                        schedule[(subject, teacher, room, day, time_slot)] = model.NewBoolVar(
                            f"{subject}_{teacher}_{room}_{day}_{time_slot}"
                        )

    # Constraints: Each subject should have the required number of weekly slots
    for subject, slots in subjects.items():
        model.Add(
            sum(
                schedule[(subject, teacher, room, day, time_slot)]
                for teacher in teachers
                for room in rooms
                for day in days
                for time_slot in time_slots
            ) == slots
        )

    # Constraints: No teacher or room should be double-booked
    for teacher in teachers:
        for day in days:
            for time_slot in time_slots:
                model.Add(
                    sum(
                        schedule[(subject, teacher, room, day, time_slot)]
                        for subject in subjects
                        for room in rooms
                    ) <= 1  # A teacher can only be in one class at a time
                )

    for room in rooms:
        for day in days:
            for time_slot in time_slots:
                model.Add(
                    sum(
                        schedule[(subject, teacher, room, day, time_slot)]
                        for subject in subjects
                        for teacher in teachers
                    ) <= 1  # A room can only be used for one subject at a time
                )

    # Constraints: Lunch break
    for day in days:
        for subject in subjects:
            for teacher in teachers:
                for room in rooms:
                    model.Add(
                        schedule[(subject, teacher, room, day, "12:00 - 12:30")] == 0
                    )

    # Constraints: Teacher-specific constraints
    for teacher, constraints in teacher_constraints.items():
        no_class_after = constraints["no_class_after"].strftime("%H:%M")
        no_class_on_days = constraints["no_class_on_days"]
        for day in days:
            if day in no_class_on_days:
                for time_slot in time_slots:
                    model.Add(
                        sum(
                            schedule[(subject, teacher, room, day, time_slot)]
                            for subject in subjects
                            for room in rooms
                        ) == 0
                    )
            for time_slot in time_slots:
                if time_slot.split(" - ")[1] > no_class_after:
                    model.Add(
                        sum(
                            schedule[(subject, teacher, room, day, time_slot)]
                            for subject in subjects
                            for room in rooms
                        ) == 0
                    )

    # Solve the model
    solver = cp_model.CpSolver()
    status = solver.Solve(model)

    # Debugging: Print the solver status and results
    if status != cp_model.OPTIMAL:
        st.error(f"Solver failed to find a solution. Solver status: {status}")
        return []

    result = []
    for key, var in schedule.items():
        if solver.Value(var) == 1:
            result.append(key)

    return result

# Time slots and days for timetable
time_slots = [f"{hour:02d}:00 - {hour:02d}:30" for hour in range(9, 16)]  # 9 AM to 3:30 PM
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]

# Generate timetable button
if st.button("Generate Timetable"):
    timetable = generate_timetable(
        st.session_state.subjects, st.session_state.teachers, st.session_state.rooms, time_slots, days, st.session_state.teacher_constraints
    )
    if timetable:
        # Store the generated timetable
        for entry in timetable:
            subject, teacher, room, day, time_slot = entry
            st.session_state.timetable = pd.concat(
                [st.session_state.timetable, pd.DataFrame([[day, time_slot, subject, teacher, room]],
                                                          columns=["Day", "Time", "Subject", "Teacher", "Room"])],
                ignore_index=True
            )
        st.success("Timetable generated successfully!")
    else:
        st.error("Failed to generate timetable. Check constraints!")

# Display the generated timetable
st.write("### Generated Timetable")
if not st.session_state.timetable.empty:
    st.dataframe(st.session_state.timetable)
    st.download_button(
        "Download Timetable (CSV)",
        data=st.session_state.timetable.to_csv(index=False),
        file_name="timetable.csv",
        mime="text/csv"
    )
else:
    st.write("No timetable generated yet.")