Mathematical Optimization (MILP) × On-Device AI Guide MILP

Behind "University Course Timetabling AI Copilot":
4D 0-1 MILP Formulation (UCTP) & Coupled Multi-Board Scheduling

TL;DR — This article explains why university course timetabling cannot be solved on a single grid—requiring simultaneous coordination across 4 student cohort boards, 10 professor schedules, and 7 specialized rooms—and how Chrome Built-in AI (Gemini Nano) + WaSHI (HiGHS) solve and diagnose it in the browser.

01Why University Timetabling Requires Multiple Coupled Boards (UCTP)

In Operations Research (OR), the University Course Timetabling Problem (UCTP)—standardized in benchmarks such as the International Timetabling Competition (ITC-2007 Curriculum-Based Track)—is one of the most heavily studied NP-hard scheduling problems alongside the Nurse Scheduling Problem (NSP).

Unlike an elementary school homeroom where one class stays together in one room, a university department timetable couples three distinct dimensions across 25 weekly periods (Mon–Fri × Periods 1–5): 4 student cohorts (Years 1–4), 10 faculty members (8 full-time + 2 adjuncts), and 7 heterogeneous rooms (large lecture halls, medium rooms, PC labs, and seminar rooms). Moving a single lecture immediately ripples across other cohorts taught by the same professor and other courses competing for the same lecture hall or PC lab.

App Element Real-World Academic Scheduling Meaning Mathematical Model Representation
Courses c ∈ C (25 courses, 30 weekly sessions) Lectures, PC labs, and seminars requiring 1 or 2 sessions/week Required weekly sessions S_c ∈ {1, 2}, enrollment Enr_c
Day d ∈ D, Period p ∈ P, Room r ∈ R 5 days × 5 periods (25 slots) × 7 rooms 4D binary decision variables x_{c,d,p,r} ∈ {0, 1} (4,375 vars)
4 Cohort Timetables (Year 1 – Year 4) Compulsory & core elective curriculum per student year Non-overlapping core curriculum constraints for each cohort Core_k
Adjunct Day Limits & Faculty Meeting NG Slots "Adjunct Prof. Kobayashi only on Tue/Thu", "Faculty meeting Wed P5" Hard zero-fixing constraints on unavailable (prof, d, p) slots
PC Lab Equipment & Hall Capacity Limits Programming labs require PC rooms; 130-student classes need Large Hall Forbid incompatible rooms r ∉ R_c (x_{c,d,p,r} = 0)
Student Hollow Gap ("Idle Bridge Period") Penalty Avoiding isolated empty periods between compulsory classes on the same day Continuous slack variable gap_{k,d,p} >= 0 in the objective
Infeasible Conflict Diagnosis (IIS) Pinpointing exact contradictory rules when no valid timetable exists Two-pass Elastic Constraint Relaxation via HiGHS

024D Binary Decision Variables and Core Structural Constraints

Let $C$ be the set of 25 courses, $D = \{\text{Mon}, \text{Tue}, \text{Wed}, \text{Thu}, \text{Fri}\}$ be the days, $P = \{1, 2, 3, 4, 5\}$ be the periods, and $R$ be the 7 rooms. We define a 4D binary variable $x_{c,d,p,r} \in \{0, 1\}$ equal to $1$ if and only if course $c$ is assigned to day $d$, period $p$, and room $r$.

Defining the slot projection $u_{c,d,p} = \sum_{r \in R} x_{c,d,p,r} \in \{0, 1\}$, the core structural constraints are:

Core Structural Hard Constraints for UCTP $$ \begin{aligned} &\text{(1) Weekly Required Sessions:} && \sum_{d \in D} \sum_{p \in P} \sum_{r \in R} x_{c,d,p,r} = S_c && (\forall c \in C) \\ &\text{(2) Single Room per Slot:} && u_{c,d,p} = \sum_{r \in R} x_{c,d,p,r} \le 1 && (\forall c \in C, d \in D, p \in P) \\ &\text{(3) Room Capacity \& Equipment:} && \sum_{d \in D} \sum_{p \in P} \sum_{r \notin R_c} x_{c,d,p,r} = 0 && (\forall c \in C) \\ &\text{(4) Room Exclusivity:} && \sum_{c \in C} x_{c,d,p,r} \le 1 && (\forall r \in R, d \in D, p \in P) \\ &\text{(5) Professor No-Overlap:} && \sum_{c \in C_{\text{prof}}} u_{c,d,p} \le 1 && (\forall \text{prof}, d \in D, p \in P) \\ &\text{(6) Cohort Curriculum Overlap:} && \sum_{c \in \text{Core}_k} u_{c,d,p} \le 1 && (\forall k \in \{Y1..Y4\}, d \in D, p \in P) \\ &\text{(7) Professor Unavailability:} && \sum_{c \in C_{\text{prof}}} u_{c,d,p} = 0 && (\forall (\text{prof}, d, p) \in \text{Unavail}) \end{aligned} $$
💡 Three Views of a Single 4D Tensor
Slicing $x_{c,d,p,r}$ by student cohort $k$ produces the 4 Cohort Boards; slicing by professor $\text{prof}$ produces the 10 Professors × 25 Slots Matrix (guaranteed at most 1 course per cell by Constraint 5); slicing by room $r$ produces the 7 Rooms × 25 Slots Matrix (guaranteed at most 1 course per cell by Constraint 4).

03Linearizing Pedagogical & Operational Rules

(A) Consecutive Double-Period Lab Sessions (course_consecutive)

While 2-session lecture courses are normally spread across different days ($\sum_{p \in P} u_{c,d,p} \le 1$), hands-on courses like "Software Design Lab (C12)" often require two consecutive periods on the same day (e.g., Tue Periods 2–3). We introduce binary indicator variables $z_{d,p} \in \{0, 1\}$ for $p \in \{1, 2, 3, 4\}$:

Linearization of Same-Day Consecutive Sessions (p ∈ {1, 2, 3, 4}) $$ \begin{aligned} \text{(a)} \quad & z_{d,p} - u_{c,d,p} \le 0 && (\forall d \in D, p \in \{1, 2, 3, 4\}) \\ \text{(b)} \quad & z_{d,p} - u_{c,d,p+1} \le 0 && (\forall d \in D, p \in \{1, 2, 3, 4\}) \\ \text{(c)} \quad & \sum_{d \in D} \sum_{p=1}^{4} z_{d,p} \ge 1 \end{aligned} $$

(B) Penalizing Student Hollow Gaps ("Idle Bridge Periods")

To prevent students from having a compulsory class in Period 1, an empty Period 2, and another compulsory class in Period 3, we introduce a non-negative continuous slack variable $\text{gap}_{k,d,p} \ge 0$ for each intermediate period $p \in \{2, 3, 4\}$:

Hollow Gap Detection Inequality and Objective Penalty $$ \begin{aligned} &\sum_{c \in \text{Core}_k} u_{c,d,p-1} + \sum_{c \in \text{Core}_k} u_{c,d,p+1} - \sum_{c \in \text{Core}_k} u_{c,d,p} - \text{gap}_{k,d,p} \le 1 \\ &\min \quad \sum_{k,d,p} W_{\text{gap}} \cdot \text{gap}_{k,d,p} \end{aligned} $$

(C) Minimal Perturbation Mode (Preserving Existing Assignments)

When an academic affairs officer adds a single rule mid-planning, reshuffling the entire university timetable is disruptive. When Minimal Perturbation Mode is enabled, each previously assigned session $(c, d^*, p^*, r^*)$ receives a negative objective reward ($-6 \cdot u_{c,d^*,p^*} - 2 \cdot x_{c,d^*,p^*,r^*}$), keeping unchanged courses in place unless mathematically required to move.

04Neuro-Symbolic Architecture: Gemini Nano + WaSHI (HiGHS)

Instead of asking an LLM to guess a timetable (which inevitably violates room capacities or double-books faculty), the copilot separates natural-language understanding from combinatorial optimization:

  1. Chrome Built-in AI (Gemini Nano / LanguageModel API): Parses unstructured faculty requests into schema-validated tool calls via responseConstraint.
  2. WaSHI + HiGHS (WASM MILP Solver): Solves the 4,420-variable, 1,601-constraint MILP to optimality in ~250 ms and verifies zero constraint violations via model.validateSolution().
  3. Two-Pass Elastic Conflict Diagnosis: When user rules conflict (Infeasible), WaSHI relaxes every hard constraint with non-negative slacks under two complementary weight profiles (prefer_structural and prefer_user) to isolate the exact colliding constraints.
💡 Two-Stage Decomposition for Large-Scale Timetabling
While our 30-course demo solves in ~250 ms using full 4D variables, university-wide timetables with hundreds of courses benefit from hierarchical problem decomposition:
Stage 1 (Timeslot Timetabling): Solve slot assignments using 3D variables $u_{c,d,p}$ under aggregate room capacity constraints ($\le$ total available rooms per capacity tier).
Stage 2 (Room Assignment): Fix timeslots and solve independent bipartite matching / transportation problems for each time slot $(d, p)$ to allocate specific rooms $r$ satisfying equipment requirements.
Exploiting hierarchical problem structures to project out high-dimensional variables is a key modeling strategy in large-scale practical optimization.