File size: 10,710 Bytes
210d88d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# reoptimizer.py

"""

This module handles emergency re-optimization of an existing timetable.

It is used when a disruption occurs (e.g., Faculty Leave) and the schedule

must be adjusted with minimal changes to the rest of the institution.



Strategy:

1. Accept the original solution.

2. Identify which Sections are directly affected by the disruption.

3. "Freeze" (Fixed Constraint) all tasks belonging to unaffected sections.

4. "Relax" (Variable) tasks belonging to affected sections, allowing them to swap slots.

5. Add a penalty for every task that changes from its original slot to minimize disruption.

"""

from typing import List, Dict, Any, Tuple, Set
from ortools.sat.python import cp_model

# Import project components
from models import Task, Faculty, Section, Room
from constraint_engine import ConstraintEngine
import constants as const

class EmergencyReoptimizer:
    """

    Specialized solver for adjusting existing timetables under new constraints.

    """

    def __init__(

        self,

        tasks: List[Task],

        faculties: List[Faculty],

        sections: List[Section],

        rooms: List[Room]

    ):
        self.tasks = tasks
        self.faculties = faculties
        self.sections = sections
        self.rooms = rooms
        
        # Internal lookups
        self.tasks_by_id = {t.task_id: t for t in tasks}

    def reoptimize_for_faculty_leave(

        self,

        current_schedule: Dict[str, Dict[str, Any]],

        faculty_id: str,

        leave_day_index: int,

        time_limit_seconds: int = 30

    ) -> Tuple[str, Dict[str, Any]]:
        """

        Adjusts the schedule to accommodate a faculty member being unavailable on a specific day.

        

        Args:

            current_schedule: The output dictionary from the original Solver.

            faculty_id: The ID of the faculty on leave.

            leave_day_index: The day index (0=Mon, 4=Fri) of the leave.

            time_limit_seconds: Max time for re-solving.



        Returns:

            Tuple (status, new_schedule)

        """
        print(f"--- Starting Emergency Re-optimization: Faculty {faculty_id} on Day {leave_day_index} ---")

        # 1. Initialize a fresh model
        model = cp_model.CpModel()
        
        # 2. Initialize Constraint Engine (applies all HARD constraints: limits, rooms, etc.)
        # We assume the Faculty object's availability hasn't been permanently changed in the DB,
        # so we will add the leave constraint manually below.
        ce = ConstraintEngine(model, self.tasks, self.faculties, self.sections, self.rooms)
        ce.apply_all_constraints()

        # 3. Apply the Emergency Constraint: Faculty Unavailable on Leave Day
        self._apply_leave_constraint(model, ce, faculty_id, leave_day_index)

        # 4. Identify Affected Sections
        # We need to unfreeze any section that has a class with this faculty on that day,
        # so the solver can swap that class with another class from a different day.
        affected_section_ids = self._identify_affected_sections(current_schedule, faculty_id, leave_day_index)
        print(f"Affected Sections (Schedule will be relaxed): {affected_section_ids}")

        # 5. Apply Freezing and Change Penalties
        self._freeze_and_relax_variables(model, ce, current_schedule, affected_section_ids)

        # 6. Solve
        solver = cp_model.CpSolver()
        solver.parameters.max_time_in_seconds = time_limit_seconds
        solver.parameters.num_search_workers = 8
        
        status_val = solver.Solve(model)
        
        status_map = {
            cp_model.OPTIMAL: "OPTIMAL",
            cp_model.FEASIBLE: "FEASIBLE",
            cp_model.INFEASIBLE: "INFEASIBLE",
            cp_model.MODEL_INVALID: "MODEL_INVALID",
            cp_model.UNKNOWN: "UNKNOWN"
        }
        status_str = status_map.get(status_val, "UNKNOWN")
        print(f"Re-optimization finished: {status_str}")

        if status_val in [cp_model.OPTIMAL, cp_model.FEASIBLE]:
            return status_str, self._extract_solution(solver, ce)
        else:
            print("Could not find a valid solution for the emergency constraint.")
            return status_str, None

    def _apply_leave_constraint(self, model: cp_model.CpModel, ce: ConstraintEngine, faculty_id: str, day_index: int):
        """

        Hard constraint: The specific faculty cannot teach on the specific day.

        """
        day_start_slot = day_index * const.NUM_TEACHING_SLOTS_PER_DAY
        day_end_slot = (day_index + 1) * const.NUM_TEACHING_SLOTS_PER_DAY
        
        # Iterate over all tasks taught by this faculty
        for task in self.tasks:
            if task.faculty.id == faculty_id:
                start_var = ce.task_vars[task.task_id][0]
                
                # Logic: Task cannot start within the day's range.
                # Since tasks are interval vars, we technically check overlap, 
                # but start_time check is usually sufficient for single-day constraint 
                # if duration doesn't span days (which it doesn't).
                
                # We enforce: start_var < day_start OR start_var >= day_end
                # This effectively bans the task from that day.
                
                # Using boolean indicators to enforce the "OR" logic
                before_day = model.NewBoolVar(f"{task.task_id}_before_leave")
                after_day = model.NewBoolVar(f"{task.task_id}_after_leave")
                
                model.Add(start_var < day_start_slot).OnlyEnforceIf(before_day)
                model.Add(start_var >= day_end_slot).OnlyEnforceIf(after_day)
                
                # Must be either before OR after (cannot be during)
                model.AddBoolOr([before_day, after_day])

    def _identify_affected_sections(self, schedule: Dict, faculty_id: str, day_index: int) -> Set[str]:
        """

        Identifies which sections have a class with the faculty on the leave day.

        These are the sections that need their schedules relaxed to allow swapping.

        """
        affected_sections = set()
        
        for task_id, data in schedule.items():
            # Check if this task is taught by the faculty on the specific day
            if data['faculty_name'] == self.tasks_by_id[task_id].faculty.name: # Ideally match ID
                # We use the faculty object from the task list to be safe
                task_fac_id = self.tasks_by_id[task_id].faculty.id
                
                if task_fac_id == faculty_id and data['day_index'] == day_index:
                    affected_sections.add(data['section_id'])
                    
        return affected_sections

    def _freeze_and_relax_variables(

        self,

        model: cp_model.CpModel,

        ce: ConstraintEngine,

        current_schedule: Dict,

        affected_section_ids: Set[str]

    ):
        """

        Freezes tasks for unaffected sections.

        Adds penalties for changing tasks for affected sections.

        """
        change_vars = []

        for task in self.tasks:
            # Get the CP variables for this task
            start_var, _, _, room_var = ce.task_vars[task.task_id]
            
            # Get original values
            if task.task_id in current_schedule:
                original_start = current_schedule[task.task_id]['start_slot']
                original_room_id = current_schedule[task.task_id]['room_id']
                # Map room ID back to index
                original_room_idx = ce.room_map[original_room_id]
            else:
                # Should not happen in a valid existing schedule, but safe fallback
                continue

            if task.section.section_id not in affected_section_ids:
                # --- STRATEGY A: FREEZE UNAFFECTED ---
                # This task belongs to a section that is NOT affected by the leave.
                # Its schedule should remain exactly the same to preserve stability.
                model.Add(start_var == original_start)
                model.Add(room_var == original_room_idx)
            else:
                # --- STRATEGY B: RELAX AND PENALIZE AFFECTED ---
                # This task belongs to a section that needs to reshuffle.
                # We allow it to move, but we penalize it if it does.
                
                # Boolean: Is the new start time different from the old start time?
                is_changed = model.NewBoolVar(f"changed_{task.task_id}")
                model.Add(start_var != original_start).OnlyEnforceIf(is_changed)
                model.Add(start_var == original_start).OnlyEnforceIf(is_changed.Not())
                
                # Weight the penalty.
                # If this specific task is the one causing the conflict (taught by absent faculty),
                # it MUST change, so the penalty is inevitable (and ignored by solver logic essentially).
                # For other tasks (swapping candidates), the penalty discourages unnecessary moves.
                change_vars.append(is_changed)

        # Minimize the total number of tasks moved
        if change_vars:
            model.Minimize(sum(change_vars))

    def _extract_solution(self, solver: cp_model.CpSolver, ce: ConstraintEngine) -> Dict:
        """

        Extracts the new schedule similar to the main solver.

        """
        schedule = {}
        for task in self.tasks:
            start_var, _, _, room_var = ce.task_vars[task.task_id]
            
            start_slot = solver.Value(start_var)
            room_index = solver.Value(room_var)
            assigned_room = self.rooms[room_index]
            
            day_index = start_slot // const.NUM_TEACHING_SLOTS_PER_DAY
            daily_slot_index = start_slot % const.NUM_TEACHING_SLOTS_PER_DAY

            schedule[task.task_id] = {
                "task_obj": task,
                "start_slot": start_slot,
                "day_index": day_index,
                "day_name": const.DAYS[day_index],
                "period_index": daily_slot_index,
                "room_id": assigned_room.room_id,
                "room_name": f"{assigned_room.room_id} ({assigned_room.building})",
                "faculty_name": task.faculty.name,
                "subject_code": task.subject.subject_code,
                "section_id": task.section.section_id,
                "duration": task.duration
            }
        return schedule