<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Timetable Optimization Lab</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;500;700&family=Space+Grotesk:wght@300;400;500;600;700&display=swap" rel="stylesheet">
<style>
:root {
--bg: #0a0e14;
--bg-secondary: #0f1419;
--fg: #e6e6e6;
--muted: #5c6773;
--accent: #00e5c7;
--accent-dim: rgba(0, 229, 199, 0.15);
--warning: #ff9940;
--error: #ff6b6b;
--success: #7dd87d;
--card: #141a22;
--border: #1f2937;
--cs: #00b4d8;
--math: #9b5de5;
--physics: #f72585;
--engineering: #48bb78;
}
* { box-sizing: border-box; }
body {
font-family: 'Space Grotesk', sans-serif;
background: var(--bg);
color: var(--fg);
min-height: 100vh;
}
.font-mono { font-family: 'JetBrains Mono', monospace; }
.bg-grid {
background-image:
linear-gradient(rgba(0, 229, 199, 0.03) 1px, transparent 1px),
linear-gradient(90deg, rgba(0, 229, 199, 0.03) 1px, transparent 1px);
background-size: 40px 40px;
}
.glow-accent {
box-shadow: 0 0 30px rgba(0, 229, 199, 0.2), inset 0 1px 0 rgba(255,255,255,0.05);
}
.slot {
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
cursor: pointer;
}
.slot:hover {
transform: scale(1.02);
z-index: 10;
}
.slot.conflict {
animation: pulse-conflict 1.5s ease-in-out infinite;
}
@keyframes pulse-conflict {
0%, 100% { box-shadow: 0 0 0 0 rgba(255, 107, 107, 0.4); }
50% { box-shadow: 0 0 0 8px rgba(255, 107, 107, 0); }
}
.metric-bar {
transition: width 0.8s cubic-bezier(0.4, 0, 0.2, 1);
}
.pathway-line {
stroke-dasharray: 1000;
stroke-dashoffset: 1000;
animation: draw-line 2s ease forwards;
}
@keyframes draw-line {
to { stroke-dashoffset: 0; }
}
@keyframes fade-up {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
.animate-fade-up {
animation: fade-up 0.6s ease forwards;
}
.stagger-1 { animation-delay: 0.1s; }
.stagger-2 { animation-delay: 0.2s; }
.stagger-3 { animation-delay: 0.3s; }
.stagger-4 { animation-delay: 0.4s; }
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
animation-duration: 0.01ms !important;
transition-duration: 0.01ms !important;
}
}
.scrollbar-thin::-webkit-scrollbar {
width: 6px;
height: 6px;
}
.scrollbar-thin::-webkit-scrollbar-track {
background: var(--bg-secondary);
}
.scrollbar-thin::-webkit-scrollbar-thumb {
background: var(--border);
border-radius: 3px;
}
.optical-highlight {
background: linear-gradient(135deg, rgba(0, 229, 199, 0.1) 0%, transparent 50%);
}
</style>
</head>
<body class="bg-grid">
<!-- Header -->
<header class="border-b border-[--border] bg-[--bg]/90 backdrop-blur-sm sticky top-0 z-50">
<div class="max-w-7xl mx-auto px-4 py-4 flex items-center justify-between">
<div class="flex items-center gap-3">
<div class="w-10 h-10 rounded-lg bg-gradient-to-br from-[--accent] to-[--cs] flex items-center justify-center">
<svg class="w-6 h-6 text-[--bg]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"/>
</svg>
</div>
<div>
<h1 class="text-lg font-semibold tracking-tight">Timetable Optimization Lab</h1>
<p class="text-xs text-[--muted]">Constraint Satisfaction & Fairness Metrics</p>
</div>
</div>
<div class="flex items-center gap-4">
<div class="flex items-center gap-2 px-3 py-1.5 rounded-full bg-[--card] border border-[--border]">
<div class="w-2 h-2 rounded-full bg-[--success] animate-pulse"></div>
<span class="text-xs font-mono text-[--muted]" id="statusText">Ready</span>
</div>
<button
id="optimizeBtn"
class="px-4 py-2 rounded-lg bg-[--accent] text-[--bg] font-medium text-sm hover:brightness-110 transition-all active:scale-95 focus:outline-none focus:ring-2 focus:ring-[--accent] focus:ring-offset-2 focus:ring-offset-[--bg]"
>
Run Optimization
</button>
</div>
</div>
</header>
<main class="max-w-7xl mx-auto px-4 py-6">
<!-- Stats Overview -->
<section class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-6 opacity-0 animate-fade-up" style="animation-delay: 0.1s; animation-fill-mode: forwards;">
<div class="bg-[--card] border border-[--border] rounded-xl p-4 optical-highlight">
<div class="text-xs text-[--muted] uppercase tracking-wider mb-1">Hard Constraints</div>
<div class="text-2xl font-bold text-[--error]" id="hardViolations">3</div>
<div class="text-xs text-[--muted]">conflicts detected</div>
</div>
<div class="bg-[--card] border border-[--border] rounded-xl p-4 optical-highlight">
<div class="text-xs text-[--muted] uppercase tracking-wider mb-1">Soft Constraints</div>
<div class="text-2xl font-bold text-[--warning]" id="softViolations">7</div>
<div class="text-xs text-[--muted]">preferences unmet</div>
</div>
<div class="bg-[--card] border border-[--border] rounded-xl p-4 optical-highlight">
<div class="text-xs text-[--muted] uppercase tracking-wider mb-1">Fairness Score</div>
<div class="text-2xl font-bold text-[--accent]" id="fairnessScore">0.72</div>
<div class="text-xs text-[--muted]">equity index</div>
</div>
<div class="bg-[--card] border border-[--border] rounded-xl p-4 optical-highlight">
<div class="text-xs text-[--muted] uppercase tracking-wider mb-1">Optimization</div>
<div class="text-2xl font-bold text-[--success]" id="optScore">58%</div>
<div class="text-xs text-[--muted]">objective reached</div>
</div>
</section>
<div class="grid lg:grid-cols-3 gap-6">
<!-- Main Timetable -->
<div class="lg:col-span-2 space-y-6">
<!-- Timetable Grid -->
<section class="bg-[--card] border border-[--border] rounded-xl overflow-hidden opacity-0 animate-fade-up" style="animation-delay: 0.2s; animation-fill-mode: forwards;">
<div class="px-4 py-3 border-b border-[--border] flex items-center justify-between">
<div class="flex items-center gap-3">
<h2 class="font-semibold">Weekly Schedule</h2>
<div class="flex gap-1">
<button class="dept-filter px-2 py-0.5 text-xs rounded bg-[--cs]/20 text-[--cs] border border-[--cs]/30 hover:bg-[--cs]/30 transition-colors" data-dept="cs">CS</button>
<button class="dept-filter px-2 py-0.5 text-xs rounded bg-[--math]/20 text-[--math] border border-[--math]/30 hover:bg-[--math]/30 transition-colors" data-dept="math">MATH</button>
<button class="dept-filter px-2 py-0.5 text-xs rounded bg-[--physics]/20 text-[--physics] border border-[--physics]/30 hover:bg-[--physics]/30 transition-colors" data-dept="physics">PHYS</button>
<button class="dept-filter px-2 py-0.5 text-xs rounded bg-[--engineering]/20 text-[--engineering] border border-[--engineering]/30 hover:bg-[--engineering]/30 transition-colors" data-dept="engineering">ENG</button>
</div>
</div>
<div class="text-xs text-[--muted]">Click slot to inspect</div>
</div>
<div class="p-4 overflow-x-auto scrollbar-thin">
<div id="timetableGrid" class="min-w-[600px]"></div>
</div>
</section>
<!-- Student Pathways -->
<section class="bg-[--card] border border-[--border] rounded-xl overflow-hidden opacity-0 animate-fade-up" style="animation-delay: 0.3s; animation-fill-mode: forwards;">
<div class="px-4 py-3 border-b border-[--border] flex items-center justify-between">
<h2 class="font-semibold">Student Pathways</h2>
<select id="pathwaySelect" class="bg-[--bg] border border-[--border] rounded-lg px-3 py-1.5 text-sm focus:outline-none focus:ring-2 focus:ring-[--accent]">
<option value="cs-ai">CS: AI/ML Track</option>
<option value="cs-systems">CS: Systems Track</option>
<option value="math-applied">Math: Applied</option>
<option value="physics-quantum">Physics: Quantum</option>
</select>
</div>
<div class="p-4">
<div id="pathwayVis" class="relative h-48"></div>
</div>
</section>
</div>
<!-- Right Sidebar -->
<div class="space-y-6">
<!-- Constraints Panel -->
<section class="bg-[--card] border border-[--border] rounded-xl overflow-hidden opacity-0 animate-fade-up" style="animation-delay: 0.25s; animation-fill-mode: forwards;">
<div class="px-4 py-3 border-b border-[--border]">
<h2 class="font-semibold">Constraints</h2>
</div>
<div class="p-4 space-y-3 max-h-64 overflow-y-auto scrollbar-thin">
<div id="constraintsList"></div>
</div>
</section>
<!-- Faculty Preferences -->
<section class="bg-[--card] border border-[--border] rounded-xl overflow-hidden opacity-0 animate-fade-up" style="animation-delay: 0.35s; animation-fill-mode: forwards;">
<div class="px-4 py-3 border-b border-[--border] flex items-center justify-between">
<h2 class="font-semibold">Faculty Preferences</h2>
<span class="text-xs text-[--muted]" id="prefSatisfaction">65% satisfied</span>
</div>
<div class="p-4 space-y-3 max-h-72 overflow-y-auto scrollbar-thin" id="facultyList"></div>
</section>
<!-- Fairness Metrics -->
<section class="bg-[--card] border border-[--border] rounded-xl overflow-hidden opacity-0 animate-fade-up" style="animation-delay: 0.45s; animation-fill-mode: forwards;">
<div class="px-4 py-3 border-b border-[--border]">
<h2 class="font-semibold">Fairness Metrics</h2>
</div>
<div class="p-4 space-y-4" id="fairnessMetrics"></div>
</section>
</div>
</div>
<!-- Optimization Log -->
<section class="mt-6 bg-[--card] border border-[--border] rounded-xl overflow-hidden opacity-0 animate-fade-up" style="animation-delay: 0.5s; animation-fill-mode: forwards;">
<div class="px-4 py-3 border-b border-[--border] flex items-center justify-between">
<h2 class="font-semibold">Optimization Log</h2>
<button id="clearLog" class="text-xs text-[--muted] hover:text-[--fg] transition-colors">Clear</button>
</div>
<div id="optimizationLog" class="p-4 h-32 overflow-y-auto scrollbar-thin font-mono text-xs space-y-1"></div>
</section>
</main>
<!-- Slot Detail Modal -->
<div id="slotModal" class="fixed inset-0 bg-black/60 backdrop-blur-sm z-50 hidden items-center justify-center p-4">
<div class="bg-[--card] border border-[--border] rounded-xl max-w-md w-full p-6 glow-accent">
<div class="flex items-start justify-between mb-4">
<div>
<h3 class="font-semibold text-lg" id="modalTitle">Slot Details</h3>
<p class="text-sm text-[--muted]" id="modalSubtitle"></p>
</div>
<button id="closeModal" class="p-1 rounded hover:bg-[--border] transition-colors">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
</svg>
</button>
</div>
<div id="modalContent" class="space-y-3"></div>
</div>
</div>
<script>
// ============ DATA MODELS ============
const timeslots = ['08:00', '09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00'];
const days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
const departments = {
cs: { name: 'Computer Science', color: '#00b4d8', abbr: 'CS' },
math: { name: 'Mathematics', color: '#9b5de5', abbr: 'MATH' },
physics: { name: 'Physics', color: '#f72585', abbr: 'PHYS' },
engineering: { name: 'Engineering', color: '#48bb78', abbr: 'ENG' }
};
const courses = [
{ id: 'cs101', name: 'Intro to Programming', dept: 'cs', credits: 3, enrollment: 120 },
{ id: 'cs201', name: 'Data Structures', dept: 'cs', credits: 3, enrollment: 85 },
{ id: 'cs301', name: 'Machine Learning', dept: 'cs', credits: 4, enrollment: 60 },
{ id: 'cs302', name: 'Computer Networks', dept: 'cs', credits: 3, enrollment: 45 },
{ id: 'cs401', name: 'Deep Learning', dept: 'cs', credits: 4, enrollment: 40 },
{ id: 'math101', name: 'Calculus I', dept: 'math', credits: 4, enrollment: 150 },
{ id: 'math201', name: 'Linear Algebra', dept: 'math', credits: 3, enrollment: 90 },
{ id: 'math301', name: 'Probability Theory', dept: 'math', credits: 3, enrollment: 55 },
{ id: 'phys101', name: 'Physics I', dept: 'physics', credits: 4, enrollment: 130 },
{ id: 'phys201', name: 'Quantum Mechanics', dept: 'physics', credits: 4, enrollment: 35 },
{ id: 'eng101', name: 'Engineering Fundamentals', dept: 'engineering', credits: 3, enrollment: 100 },
{ id: 'eng201', name: 'Signal Processing', dept: 'engineering', credits: 3, enrollment: 50 }
];
const faculty = [
{ id: 'f1', name: 'Dr. Sarah Chen', dept: 'cs', prefTimes: ['10:00', '11:00', '14:00'], maxHours: 12, currentHours: 10, satisfaction: 0.8 },
{ id: 'f2', name: 'Prof. James Wilson', dept: 'cs', prefTimes: ['09:00', '13:00', '15:00'], maxHours: 10, currentHours: 9, satisfaction: 0.6 },
{ id: 'f3', name: 'Dr. Maria Garcia', dept: 'math', prefTimes: ['08:00', '10:00', '11:00'], maxHours: 14, currentHours: 12, satisfaction: 0.9 },
{ id: 'f4', name: 'Prof. David Kim', dept: 'physics', prefTimes: ['13:00', '14:00', '15:00'], maxHours: 12, currentHours: 8, satisfaction: 0.5 },
{ id: 'f5', name: 'Dr. Emily Brown', dept: 'engineering', prefTimes: ['09:00', '10:00', '16:00'], maxHours: 10, currentHours: 11, satisfaction: 0.4 },
{ id: 'f6', name: 'Prof. Alex Patel', dept: 'cs', prefTimes: ['11:00', '14:00', '16:00'], maxHours: 8, currentHours: 6, satisfaction: 0.85 }
];
const rooms = [
{ id: 'r1', name: 'Hall A', capacity: 150 },
{ id: 'r2', name: 'Hall B', capacity: 100 },
{ id: 'r3', name: 'Room 301', capacity: 60 },
{ id: 'r4', name: 'Lab 1', capacity: 40 },
{ id: 'r5', name: 'Room 205', capacity: 50 }
];
const pathways = {
'cs-ai': {
name: 'CS: AI/ML Track',
semesters: [
['cs101', 'math101'],
['cs201', 'math201'],
['cs301', 'math301'],
['cs401']
]
},
'cs-systems': {
name: 'CS: Systems Track',
semesters: [
['cs101', 'math101'],
['cs201', 'eng101'],
['cs302'],
['eng201']
]
},
'math-applied': {
name: 'Math: Applied',
semesters: [
['math101', 'phys101'],
['math201', 'cs101'],
['math301', 'phys201']
]
},
'physics-quantum': {
name: 'Physics: Quantum',
semesters: [
['phys101', 'math101'],
['math201', 'phys201'],
['cs301', 'math301']
]
}
};
const constraints = [
{ id: 'c1', type: 'hard', name: 'No room double-booking', description: 'A room cannot be assigned to two courses at the same time', satisfied: false },
{ id: 'c2', type: 'hard', name: 'No faculty conflict', description: 'A faculty member cannot teach two classes simultaneously', satisfied: false },
{ id: 'c3', type: 'hard', name: 'Room capacity', description: 'Room capacity must accommodate enrollment', satisfied: true },
{ id: 'c4', type: 'soft', name: 'Faculty time preferences', description: 'Respect faculty preferred teaching times', satisfied: false },
{ id: 'c5', type: 'soft', name: 'Lunch break', description: 'Avoid scheduling classes during 12:00-13:00', satisfied: false },
{ id: 'c6', type: 'soft', name: 'Course spacing', description: 'Multi-session courses should be on different days', satisfied: true },
{ id: 'c7', type: 'fairness', name: 'Load balancing', description: 'Teaching load should be balanced across faculty', satisfied: false },
{ id: 'c8', type: 'fairness', name: 'Department equity', description: 'Prime time slots distributed fairly among departments', satisfied: false }
];
// ============ STATE ============
let schedule = [];
let activeFilter = null;
let isOptimizing = false;
// ============ INITIALIZATION ============
function generateInitialSchedule() {
const newSchedule = [];
const coursePairs = [
['cs101', 'cs201', 'f1', 'r1'],
['math101', 'math201', 'f3', 'r2'],
['cs301', 'f2', 'r3'],
['phys101', 'phys201', 'f4', 'r1'],
['eng101', 'f5', 'r2'],
['cs401', 'f1', 'r4'],
['math301', 'f3', 'r5'],
['cs302', 'f6', 'r3'],
['eng201', 'f5', 'r4']
];
let slotIndex = 0;
coursePairs.forEach((pair, idx) => {
const courseIds = pair.slice(0, pair.length - 2);
const facultyId = pair[pair.length - 2];
const roomId = pair[pair.length - 1];
courseIds.forEach(courseId => {
const dayIdx = Math.floor(slotIndex / 8) % 5;
const timeIdx = slotIndex % 8;
// Create intentional conflicts for demo
let actualTimeIdx = timeIdx;
if (courseId === 'cs301') actualTimeIdx = 2; // Will conflict with phys101 on Monday
if (courseId === 'phys101' && dayIdx === 0) actualTimeIdx = 2;
newSchedule.push({
courseId,
facultyId,
roomId,
day: days[dayIdx],
time: timeslots[actualTimeIdx],
hasConflict: false
});
slotIndex++;
});
});
return newSchedule;
}
function detectConflicts() {
schedule.forEach(s => s.hasConflict = false);
schedule.forEach((s1, i) => {
schedule.forEach((s2, j) => {
if (i >= j) return;
if (s1.day === s2.day && s1.time === s2.time) {
if (s1.roomId === s2.roomId || s1.facultyId === s2.facultyId) {
s1.hasConflict = true;
s2.hasConflict = true;
}
}
});
});
}
// ============ RENDERING ============
function renderTimetable() {
const grid = document.getElementById('timetableGrid');
grid.innerHTML = '';
// Header row
const headerRow = document.createElement('div');
headerRow.className = 'grid gap-1 mb-2';
headerRow.style.gridTemplateColumns = '80px repeat(5, 1fr)';
const cornerCell = document.createElement('div');
headerRow.appendChild(cornerCell);
days.forEach(day => {
const dayCell = document.createElement('div');
dayCell.className = 'text-center text-xs font-medium text-[--muted] py-2';
dayCell.textContent = day.slice(0, 3);
headerRow.appendChild(dayCell);
});
grid.appendChild(headerRow);
// Time rows
timeslots.forEach(time => {
const row = document.createElement('div');
row.className = 'grid gap-1 mb-1';
row.style.gridTemplateColumns = '80px repeat(5, 1fr)';
const timeCell = document.createElement('div');
timeCell.className = 'text-xs text-[--muted] flex items-center justify-end pr-2 font-mono';
timeCell.textContent = time;
row.appendChild(timeCell);
days.forEach(day => {
const cell = document.createElement('div');
cell.className = 'min-h-[60px] bg-[--bg-secondary] rounded-lg p-1 relative';
const slotsInCell = schedule.filter(s => s.day === day && s.time === time);
slotsInCell.forEach(slot => {
const course = courses.find(c => c.id === slot.courseId);
const dept = departments[course.dept];
if (activeFilter && activeFilter !== course.dept) return;
const slotEl = document.createElement('div');
slotEl.className = `slot text-xs p-2 rounded mb-1 border-l-2 ${slot.hasConflict ? 'conflict' : ''}`;
slotEl.style.backgroundColor = dept.color + '20';
slotEl.style.borderColor = dept.color;
if (slot.hasConflict) {
slotEl.classList.add('ring-1', 'ring-[--error]');
}
slotEl.innerHTML = `
<div class="font-medium truncate" style="color: ${dept.color}">${course.name}</div>
<div class="text-[10px] text-[--muted] truncate">${course.enrollment} students</div>
`;
slotEl.addEventListener('click', () => showSlotDetails(slot, course));
cell.appendChild(slotEl);
});
row.appendChild(cell);
});
grid.appendChild(row);
});
}
function renderConstraints() {
const list = document.getElementById('constraintsList');
list.innerHTML = '';
const grouped = {
hard: constraints.filter(c => c.type === 'hard'),
soft: constraints.filter(c => c.type === 'soft'),
fairness: constraints.filter(c => c.type === 'fairness')
};
Object.entries(grouped).forEach(([type, items]) => {
const typeColors = {
hard: 'text-[--error] bg-[--error]/10 border-[--error]/30',
soft: 'text-[--warning] bg-[--warning]/10 border-[--warning]/30',
fairness: 'text-[--accent] bg-[--accent]/10 border-[--accent]/30'
};
const section = document.createElement('div');
section.className = 'mb-4 last:mb-0';
const label = document.createElement('div');
label.className = `text-[10px] uppercase tracking-wider mb-2 px-2 py-0.5 rounded inline-block border ${typeColors[type]}`;
label.textContent = type;
section.appendChild(label);
items.forEach(c => {
const item = document.createElement('div');
item.className = 'flex items-start gap-2 py-2 border-b border-[--border] last:border-0';
const icon = document.createElement('div');
icon.className = `w-4 h-4 rounded-full flex items-center justify-center flex-shrink-0 mt-0.5 ${c.satisfied ? 'bg-[--success]/20 text-[--success]' : 'bg-[--error]/20 text-[--error]'}`;
icon.innerHTML = c.satisfied
? '<svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/></svg>'
: '<svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/></svg>';
const content = document.createElement('div');
content.innerHTML = `
<div class="text-sm font-medium">${c.name}</div>
<div class="text-xs text-[--muted]">${c.description}</div>
`;
item.appendChild(icon);
item.appendChild(content);
section.appendChild(item);
});
list.appendChild(section);
});
}
function renderFaculty() {
const list = document.getElementById('facultyList');
list.innerHTML = '';
faculty.forEach(f => {
const dept = departments[f.dept];
const loadPercent = Math.min((f.currentHours / f.maxHours) * 100, 100);
const satPercent = Math.round(f.satisfaction * 100);
const card = document.createElement('div');
card.className = 'p-3 rounded-lg bg-[--bg-secondary] border border-[--border]';
card.innerHTML = `
<div class="flex items-center justify-between mb-2">
<div class="flex items-center gap-2">
<div class="w-8 h-8 rounded-full flex items-center justify-center text-xs font-bold" style="background: ${dept.color}20; color: ${dept.color}">
${f.name.split(' ').map(n => n[0]).join('')}
</div>
<div>
<div class="text-sm font-medium">${f.name}</div>
<div class="text-[10px] text-[--muted]">${dept.abbr}</div>
</div>
</div>
<div class="text-right">
<div class="text-xs font-mono">${f.currentHours}/${f.maxHours}h</div>
<div class="text-[10px] ${satPercent >= 70 ? 'text-[--success]' : satPercent >= 50 ? 'text-[--warning]' : 'text-[--error]'}">${satPercent}% sat</div>
</div>
</div>
<div class="h-1.5 bg-[--bg] rounded-full overflow-hidden">
<div class="h-full rounded-full transition-all duration-500" style="width: ${loadPercent}%; background: ${dept.color}"></div>
</div>
<div class="mt-2 flex gap-1">
${f.prefTimes.map(t => `<span class="px-1.5 py-0.5 text-[10px] rounded bg-[--bg] text-[--muted]">${t}</span>`).join('')}
</div>
`;
list.appendChild(card);
});
}
function renderFairnessMetrics() {
const container = document.getElementById('fairnessMetrics');
const metrics = [
{ name: 'Load Distribution', value: 0.78, description: 'Teaching hours variance across faculty' },
{ name: 'Prime Time Equity', value: 0.65, description: '10:00-14:00 slot distribution by department' },
{ name: 'Preference Satisfaction', value: 0.72, description: 'Faculty preference fulfillment rate' },
{ name: 'Room Utilization', value: 0.85, description: 'Balanced room usage across campus' }
];
container.innerHTML = metrics.map(m => `
<div>
<div class="flex justify-between text-sm mb-1">
<span>${m.name}</span>
<span class="font-mono ${m.value >= 0.8 ? 'text-[--success]' : m.value >= 0.6 ? 'text-[--warning]' : 'text-[--error]'}">${Math.round(m.value * 100)}%</span>
</div>
<div class="h-2 bg-[--bg-secondary] rounded-full overflow-hidden">
<div class="metric-bar h-full rounded-full ${m.value >= 0.8 ? 'bg-[--success]' : m.value >= 0.6 ? 'bg-[--warning]' : 'bg-[--error]'}" style="width: ${m.value * 100}%"></div>
</div>
<div class="text-[10px] text-[--muted] mt-1">${m.description}</div>
</div>
`).join('');
}
function renderPathway() {
const select = document.getElementById('pathwaySelect');
const pathwayId = select.value;
const pathway = pathways[pathwayId];
const container = document.getElementById('pathwayVis');
container.innerHTML = '';
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svg.setAttribute('width', '100%');
svg.setAttribute('height', '100%');
svg.setAttribute('viewBox', '0 0 600 180');
const semesterWidth = 140;
const startX = 20;
pathway.semesters.forEach((semCourses, semIdx) => {
const x = startX + semIdx * semesterWidth;
// Semester label
const label = document.createElementNS('http://www.w3.org/2000/svg', 'text');
label.setAttribute('x', x + 50);
label.setAttribute('y', 20);
label.setAttribute('fill', '#5c6773');
label.setAttribute('font-size', '12');
label.setAttribute('text-anchor', 'middle');
label.textContent = `Sem ${semIdx + 1}`;
svg.appendChild(label);
semCourses.forEach((courseId, courseIdx) => {
const course = courses.find(c => c.id === courseId);
const dept = departments[course.dept];
const y = 45 + courseIdx * 55;
// Connection line to next semester
if (semIdx < pathway.semesters.length - 1) {
const line = document.createElementNS('http://www.w3.org/2000/svg', 'line');
line.setAttribute('x1', x + 100);
line.setAttribute('y1', y + 20);
line.setAttribute('x2', x + semesterWidth + 10);
line.setAttribute('y2', y + 20);
line.setAttribute('stroke', dept.color);
line.setAttribute('stroke-width', '2');
line.setAttribute('stroke-dasharray', '4');
line.setAttribute('opacity', '0.5');
svg.appendChild(line);
}
// Course box
const rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
rect.setAttribute('x', x);
rect.setAttribute('y', y);
rect.setAttribute('width', '110');
rect.setAttribute('height', '40');
rect.setAttribute('rx', '6');
rect.setAttribute('fill', dept.color + '20');
rect.setAttribute('stroke', dept.color);
rect.setAttribute('stroke-width', '1');
svg.appendChild(rect);
// Course name
const text = document.createElementNS('http://www.w3.org/2000/svg', 'text');
text.setAttribute('x', x + 55);
text.setAttribute('y', y + 18);
text.setAttribute('fill', '#e6e6e6');
text.setAttribute('font-size', '11');
text.setAttribute('font-weight', '500');
text.setAttribute('text-anchor', 'middle');
text.textContent = course.name.length > 15 ? course.name.slice(0, 14) + '...' : course.name;
svg.appendChild(text);
// Credits
const credits = document.createElementNS('http://www.w3.org/2000/svg', 'text');
credits.setAttribute('x', x + 55);
credits.setAttribute('y', y + 32);
credits.setAttribute('fill', '#5c6773');
credits.setAttribute('font-size', '10');
credits.setAttribute('text-anchor', 'middle');
credits.textContent = `${course.credits} credits`;
svg.appendChild(credits);
});
});
container.appendChild(svg);
}
// ============ INTERACTIONS ============
function showSlotDetails(slot, course) {
const modal = document.getElementById('slotModal');
const dept = departments[course.dept];
const facultyMember = faculty.find(f => f.id === slot.facultyId);
const room = rooms.find(r => r.id === slot.roomId);
document.getElementById('modalTitle').textContent = course.name;
document.getElementById('modalSubtitle').textContent = `${slot.day} at ${slot.time}`;
const content = document.getElementById('modalContent');
content.innerHTML = `
<div class="grid grid-cols-2 gap-3">
<div class="p-3 rounded-lg bg-[--bg-secondary]">
<div class="text-[10px] text-[--muted] uppercase mb-1">Department</div>
<div class="text-sm font-medium" style="color: ${dept.color}">${dept.name}</div>
</div>
<div class="p-3 rounded-lg bg-[--bg-secondary]">
<div class="text-[10px] text-[--muted] uppercase mb-1">Enrollment</div>
<div class="text-sm font-medium">${course.enrollment} students</div>
</div>
<div class="p-3 rounded-lg bg-[--bg-secondary]">
<div class="text-[10px] text-[--muted] uppercase mb-1">Instructor</div>
<div class="text-sm font-medium">${facultyMember.name}</div>
</div>
<div class="p-3 rounded-lg bg-[--bg-secondary]">
<div class="text-[10px] text-[--muted] uppercase mb-1">Room</div>
<div class="text-sm font-medium">${room.name} (${room.capacity})</div>
</div>
</div>
${slot.hasConflict ? `
<div class="mt-3 p-3 rounded-lg bg-[--error]/10 border border-[--error]/30">
<div class="flex items-center gap-2 text-[--error]">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"/>
</svg>
<span class="text-sm font-medium">Scheduling Conflict Detected</span>
</div>
<p class="text-xs text-[--muted] mt-1">This slot overlaps with another course in the same room or with the same instructor.</p>
</div>
` : ''}
<div class="mt-3 p-3 rounded-lg bg-[--bg-secondary]">
<div class="text-[10px] text-[--muted] uppercase mb-2">Constraints Affecting This Slot</div>
<div class="space-y-1">
${constraints.slice(0, 3).map(c => `
<div class="flex items-center gap-2 text-xs">
<div class="w-2 h-2 rounded-full ${c.satisfied ? 'bg-[--success]' : 'bg-[--error]'}"></div>
<span class="text-[--muted]">${c.name}</span>
</div>
`).join('')}
</div>
</div>
`;
modal.classList.remove('hidden');
modal.classList.add('flex');
}
function logMessage(message, type = 'info') {
const log = document.getElementById('optimizationLog');
const colors = {
info: 'text-[--muted]',
success: 'text-[--success]',
warning: 'text-[--warning]',
error: 'text-[--error]'
};
const timestamp = new Date().toLocaleTimeString('en-US', { hour12: false });
const entry = document.createElement('div');
entry.className = colors[type];
entry.innerHTML = `<span class="text-[--accent]">[${timestamp}]</span> ${message}`;
log.appendChild(entry);
log.scrollTop = log.scrollHeight;
}
async function runOptimization() {
if (isOptimizing) return;
isOptimizing = true;
const btn = document.getElementById('optimizeBtn');
btn.disabled = true;
btn.textContent = 'Optimizing...';
btn.classList.add('opacity-50');
document.getElementById('statusText').textContent = 'Running...';
logMessage('Starting optimization algorithm...', 'info');
logMessage('Analyzing current schedule configuration', 'info');
await sleep(500);
// Simulate optimization steps
const steps = [
{ msg: 'Detecting hard constraint violations...', delay: 400 },
{ msg: 'Found 3 room conflicts, 2 faculty overlaps', type: 'warning', delay: 600 },
{ msg: 'Applying constraint propagation...', delay: 500 },
{ msg: 'Resolving conflict: CS301 moved to Tuesday 10:00', type: 'success', delay: 400 },
{ msg: 'Resolving conflict: PHYS201 moved to Wednesday 14:00', type: 'success', delay: 400 },
{ msg: 'Optimizing faculty preferences...', delay: 500 },
{ msg: 'Dr. Emily Brown: Assigned preferred slot 16:00', type: 'success', delay: 300 },
{ msg: 'Prof. David Kim: Satisfaction improved to 85%', type: 'success', delay: 300 },
{ msg: 'Balancing department prime-time allocation...', delay: 500 },
{ msg: 'Computing fairness metrics...', delay: 400 },
{ msg: 'Optimization complete!', type: 'success', delay: 300 }
];
for (const step of steps) {
logMessage(step.msg, step.type || 'info');
await sleep(step.delay);
}
// Update schedule
schedule = schedule.map(s => {
if (s.courseId === 'cs301') {
return { ...s, day: 'Tuesday', time: '10:00', hasConflict: false };
}
if (s.courseId === 'phys201') {
return { ...s, day: 'Wednesday', time: '14:00', hasConflict: false };
}
return { ...s, hasConflict: false };
});
// Update faculty satisfaction
faculty.forEach(f => {
f.satisfaction = Math.min(f.satisfaction + 0.15, 1);
if (f.id === 'f4') f.satisfaction = 0.85;
if (f.id === 'f5') f.satisfaction = 0.75;
});
// Update constraints
constraints.forEach(c => {
if (c.type === 'hard') c.satisfied = true;
if (c.id === 'c4') c.satisfied = true;
if (c.id === 'c7') c.satisfied = true;
});
detectConflicts();
renderTimetable();
renderConstraints();
renderFaculty();
renderFairnessMetrics();
// Update stats
document.getElementById('hardViolations').textContent = '0';
document.getElementById('hardViolations').classList.remove('text-[--error]');
document.getElementById('hardViolations').classList.add('text-[--success]');
document.getElementById('softViolations').textContent = '3';
document.getElementById('softViolations').classList.remove('text-[--warning]');
document.getElementById('softViolations').classList.add('text-[--accent]');
document.getElementById('fairnessScore').textContent = '0.89';
document.getElementById('optScore').textContent = '92%';
document.getElementById('optScore').classList.add('text-[--success]');
document.getElementById('prefSatisfaction').textContent = '85% satisfied';
document.getElementById('statusText').textContent = 'Optimized';
btn.disabled = false;
btn.textContent = 'Run Optimization';
btn.classList.remove('opacity-50');
isOptimizing = false;
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// ============ EVENT LISTENERS ============
document.getElementById('optimizeBtn').addEventListener('click', runOptimization);
document.getElementById('closeModal').addEventListener('click', () => {
document.getElementById('slotModal').classList.add('hidden');
document.getElementById('slotModal').classList.remove('flex');
});
document.getElementById('slotModal').addEventListener('click', (e) => {
if (e.target === e.currentTarget) {
e.currentTarget.classList.add('hidden');
e.currentTarget.classList.remove('flex');
}
});
document.querySelectorAll('.dept-filter').forEach(btn => {
btn.addEventListener('click', () => {
const dept = btn.dataset.dept;
document.querySelectorAll('.dept-filter').forEach(b => {
b.classList.remove('ring-2', 'ring-offset-1', 'ring-offset-[--card]');
});
if (activeFilter === dept) {
activeFilter = null;
} else {
activeFilter = dept;
btn.classList.add('ring-2', 'ring-offset-1', 'ring-offset-[--card]');
}
renderTimetable();
});
});
document.getElementById('pathwaySelect').addEventListener('change', renderPathway);
document.getElementById('clearLog').addEventListener('click', () => {
document.getElementById('optimizationLog').innerHTML = '';
logMessage('Log cleared', 'info');
});
// Keyboard accessibility
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
document.getElementById('slotModal').classList.add('hidden');
document.getElementById('slotModal').classList.remove('flex');
}
});
// ============ INIT ============
schedule = generateInitialSchedule();
detectConflicts();
renderTimetable();
renderConstraints();
renderFaculty();
renderFairnessMetrics();
renderPathway();
logMessage('Timetable Optimization Lab initialized', 'success');
logMessage('Schedule loaded with 12 courses, 6 faculty, 5 rooms', 'info');
logMessage('Click "Run Optimization" to begin constraint solving', 'info');
</script>
</body>
</html>