Spaces:
Sleeping
Sleeping
File size: 54,540 Bytes
56f6f21 | 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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 | import random
import itertools
from datetime import time
from .models import (
TycoInput,
TycoTimetable,
TycoBInput,
TycoBTimetable,
SycoInput,
SycoTimetable,
SycoBInput,
SycoBTimetable,
FycoInput,
FycoTimetable,
FycoBInput,
FycoBTimetable,
DAYS,
)
# === CONFIGURATION ===
ACADEMIC_SLOTS = [
(time(10, 0), time(11, 0)), # Slot 0
(time(11, 0), time(12, 0)), # Slot 1
(time(12, 45), time(13, 45)), # Slot 2 (after lunch)
(time(13, 45), time(14, 45)), # Slot 3
(time(15, 0), time(16, 0)), # Slot 4 (after tea)
(time(16, 0), time(17, 0)), # Slot 5
]
# Adjacent slot pairs that can host a 2-hour practical block
ADJACENT_PAIRS = [(0, 1), (2, 3), (4, 5)]
CLASS_CONFIG = {
"tyco": {"input": TycoInput, "timetable": TycoTimetable, "name": "TYCO A"},
"tyco_b": {"input": TycoBInput, "timetable": TycoBTimetable, "name": "TYCO B"},
"syco": {"input": SycoInput, "timetable": SycoTimetable, "name": "SYCO A"},
"syco_b": {"input": SycoBInput, "timetable": SycoBTimetable, "name": "SYCO B"},
"fyco": {"input": FycoInput, "timetable": FycoTimetable, "name": "FYCO A"},
"fyco_b": {"input": FycoBInput, "timetable": FycoBTimetable, "name": "FYCO B"},
}
SUBJECT_ABBR = {
# TYCO A (Sem V)
"OPERATING SYSTEM": "OSY",
"SOFTWARE ENGINEERING": "STE",
"ENTREPRENEURSHIP DEVELOPMENT AND STARTUPS": "ENDS",
"SEMINAR AND PROJECT INITIATION COURSE": "SPI",
"CLOUD COMPUTING": "CLC",
# TYCO A (Sem VI)
"Management": "MAN",
"Mobile Application Development": "MAD",
"Emerging Trends In Computer & Information Tech.": "ETI",
"Cilent-Side Scripting": "CSS",
"Software Testing": "SFT",
"Capstone Project": "CPE",
"Network And Information Security": "NIS",
# SYCO A (Sem III)
"Data Structure Using C": "DSU",
"Database Management System": "DMS",
"Digital Techniques": "DTE",
"Object Oriented Programming Using C++": "OOP",
"Computer Graphics": "CGR",
"Essence Of Indian Constitution": "EIC",
# SYCO A (Sem IV)
"Environmental Education And Sustainability": "EES",
"Java Programming": "JPR",
"Data Communication And Computer Network": "DCN",
"Microprocessor": "MIC",
"Python Programming": "PWP",
"User Interface Design": "UID",
# FYCO (Sem I)
"Basic Mathematics": "BMS",
"Basic Science (Physics)": "PHY",
"Basic Science (Chemistry)": "CHY",
"Communication Skills": "ENG",
"Engineering Graphics": "EGP",
"Professional Communication": "POC",
"Engineering Workshop Practice": "WPC",
"Fundamentals of ICT": "ICT",
# FYCO (Sem II)
"Basic Electrical And Electronics Engineering": "BEE",
"Programming In 'C'": "PIC",
"Linux Basics": "BLP",
"Web Page Designing": "WPD",
"Applied Mathematics": "AMS",
}
def get_abbr(name):
if name in SUBJECT_ABBR:
return SUBJECT_ABBR[name]
try:
from .models import MasterSubject
ms = MasterSubject.objects.filter(subject_name__iexact=name).first()
if ms and ms.abbreviation:
return ms.abbreviation.strip().upper()
except Exception:
pass
return name[:3].upper()
def normalize_teacher_name(name):
return " ".join(str(name).strip().split())
# ──────────────────────────────────────────────────────────────────────────────
# Teacher-conflict helpers
# ──────────────────────────────────────────────────────────────────────────────
def _teachers_in_grid_at(grid, day, start_time):
busy = set()
for (g_day, g_slot), g_data in grid.items():
if g_day != day:
continue
if ACADEMIC_SLOTS[g_slot][0] != start_time:
continue
t = g_data.get("type", "")
if t in ("TH", "EXTRA"):
busy.add(normalize_teacher_name(g_data["subject"].teacher_name))
elif t == "PR":
for lab in g_data.get("trio", []):
busy.add(normalize_teacher_name(lab.teacher_name))
return busy
def check_teacher_conflict_bulk(
teacher_list, day, start_time, exclude_class_key, current_grid=None
):
from django.db.models.functions import Trim
normalized = [normalize_teacher_name(n) for n in teacher_list]
normalized_set = set(normalized)
for key, cfg in CLASS_CONFIG.items():
if key == exclude_class_key:
continue
busy = (
cfg["timetable"]
.objects.annotate(trimmed=Trim("teacher_name"))
.filter(trimmed__in=normalized, day=day, start_time=start_time)
.exists()
)
if busy:
return True
if current_grid is not None:
grid_teachers = _teachers_in_grid_at(current_grid, day, start_time)
if normalized_set & grid_teachers:
return True
return False
def check_single_conflict(
teacher, day, start_time, exclude_class_key, current_grid=None
):
return check_teacher_conflict_bulk(
[teacher], day, start_time, exclude_class_key, current_grid
)
# ──────────────────────────────────────────────────────────────────────────────
# MAIN GENERATOR
# ──────────────────────────────────────────────────────────────────────────────
def generate_timetable_for_class(class_key, allow_extra=True):
"""
Human-teacher style timetable generator.
How a human teacher thinks:
- "I have lab subjects S0, S1, S2 needing N0, N1, N2 sessions each."
- "Each lab slot: all 3 batches run simultaneously. A1=Sa, A2=Sb, A3=Sc (unique subjects)."
- "I rotate: next slot A1=Sb, A2=Sc, A3=Sa. This way no batch ever does the same subject."
- "I count how many total lab windows I need, build that many rotated trios, then place them."
Phase 1 — Build all required practical sessions as (A1-subj, A2-subj, A3-subj) trios
using cyclic rotation of practical subjects.
Phase 2 — Place every session into a free adjacent-pair slot, checking teacher conflicts.
Phase 3 — Fill remaining free slots with theory.
Phase 4 — Fill any remaining gaps with Extra lectures or Library.
"""
if class_key not in CLASS_CONFIG:
return False, "Invalid Class"
InputModel = CLASS_CONFIG[class_key]["input"]
TimetableModel = CLASS_CONFIG[class_key]["timetable"]
TimetableModel.objects.all().delete()
all_inputs = list(InputModel.objects.all())
for inp in all_inputs:
inp.teacher_name = normalize_teacher_name(inp.teacher_name)
days_list = [d[0] for d in DAYS]
grid = {} # (day, slot_index) → data dict
# ──────────────────────────────────────────────────────────────────────────
# PHASE 1 — BUILD PRACTICAL SESSION LIST (rotation-based)
# ──────────────────────────────────────────────────────────────────────────
# Gather subjects that have practical work
lab_subjects = [inp for inp in all_inputs if inp.practical_credits > 0]
# sessions_queue: list of (subj_A1, subj_A2, subj_A3) tuples
# Each entry = one 2-hr block that needs a slot in the timetable.
# We build this by iterating over *all required blocks across all subjects*
# and rotating which subject goes to which batch.
sessions_queue = []
subj_by_id = {s.id: s for s in lab_subjects}
if lab_subjects:
class _Lib:
subject_name = "Library"
teacher_name = "-"
id = -1
lib_obj = _Lib()
needs = {
"A1": {s.id: s.practical_credits // 2 for s in lab_subjects},
"A2": {s.id: s.practical_credits // 2 for s in lab_subjects},
"A3": {s.id: s.practical_credits // 2 for s in lab_subjects},
}
# Safety breakout
for _ in range(100):
# Check if all batches have 0 remaining needs
if all(sum(needs[b].values()) == 0 for b in ["A1", "A2", "A3"]):
break
batch_order = ["A1", "A2", "A3"]
random.shuffle(batch_order)
this_slot = {"A1": lib_obj, "A2": lib_obj, "A3": lib_obj}
used_teachers = set()
for b in batch_order:
best_s_id = None
best_count = -1
# To add organic distribution without getting stuck, we can shuffle
# equal-count candidates, but sorting first ensures deterministic base
candidates = list(needs[b].keys())
random.shuffle(candidates)
for s_id in candidates:
count = needs[b][s_id]
if count > 0:
t_name = subj_by_id[s_id].teacher_name
if t_name not in used_teachers:
if count > best_count:
best_count = count
best_s_id = s_id
if best_s_id is not None:
needs[b][best_s_id] -= 1
used_teachers.add(subj_by_id[best_s_id].teacher_name)
this_slot[b] = subj_by_id[best_s_id]
# Append the trio exactly in A1, A2, A3 order
sessions_queue.append((this_slot["A1"], this_slot["A2"], this_slot["A3"]))
# ──────────────────────────────────────────────────────────────────────────
# PHASE 2 — PLACE PRACTICAL SESSIONS INTO FREE ADJACENT PAIRS
# ──────────────────────────────────────────────────────────────────────────
# Build shuffled list of all (day, s1, s2) candidates
all_pairs = [(day, s1, s2) for day in days_list for s1, s2 in ADJACENT_PAIRS]
random.shuffle(all_pairs)
remaining_sessions = list(sessions_queue)
for day, s1, s2 in all_pairs:
if not remaining_sessions:
break
if (day, s1) in grid or (day, s2) in grid:
continue
st1 = ACADEMIC_SLOTS[s1][0]
st2 = ACADEMIC_SLOTS[s2][0]
# Try to place the next session that works here (teacher conflict check)
placed = False
for idx, session in enumerate(remaining_sessions):
subj_A1, subj_A2, subj_A3 = session
teachers = list(
dict.fromkeys(
t
for t in [
subj_A1.teacher_name,
subj_A2.teacher_name,
subj_A3.teacher_name,
]
if t != "-"
)
)
if check_teacher_conflict_bulk(teachers, day, st1, class_key, grid):
continue
if check_teacher_conflict_bulk(teachers, day, st2, class_key, grid):
continue
# Valid — commit this session
trio = [subj_A1, subj_A2, subj_A3]
grid[(day, s1)] = {"type": "PR", "trio": trio}
grid[(day, s2)] = {"type": "PR", "trio": trio}
remaining_sessions.pop(idx)
placed = True
break
# If first choice failed, try any remaining session
if not placed:
for idx, session in enumerate(remaining_sessions):
subj_A1, subj_A2, subj_A3 = session
teachers = list(
dict.fromkeys(
t
for t in [
subj_A1.teacher_name,
subj_A2.teacher_name,
subj_A3.teacher_name,
]
if t != "-"
)
)
if check_teacher_conflict_bulk(teachers, day, st1, class_key, grid):
continue
if check_teacher_conflict_bulk(teachers, day, st2, class_key, grid):
continue
trio = [subj_A1, subj_A2, subj_A3]
grid[(day, s1)] = {"type": "PR", "trio": trio}
grid[(day, s2)] = {"type": "PR", "trio": trio}
remaining_sessions.pop(idx)
break
# ──────────────────────────────────────────────────────────────────────────
# SLOT RESERVATION — protect free adjacent pairs for unplaced sessions
# We know exactly how many sessions are left. Reserve that many pairs NOW
# so Phase 3 (theory) cannot consume them.
# ──────────────────────────────────────────────────────────────────────────
reserved_pairs = set() # (day, s1) keys — first slot of each reserved pair
if remaining_sessions:
free_adj = [
(day, s1, s2)
for day in days_list
for s1, s2 in ADJACENT_PAIRS
if (day, s1) not in grid and (day, s2) not in grid
]
random.shuffle(free_adj)
for day, s1, s2 in free_adj:
if len(reserved_pairs) >= len(remaining_sessions):
break
reserved_pairs.add((day, s1))
def _in_reserved(day, slot_idx):
"""True if this slot belongs to a reserved practical pair."""
for s1, s2 in ADJACENT_PAIRS:
if slot_idx in (s1, s2) and (day, s1) in reserved_pairs:
return True
return False
# ──────────────────────────────────────────────────────────────────────────
# PHASE 3 — THEORY (skips slots in reserved pairs)
# ──────────────────────────────────────────────────────────────────────────
theory_pool = []
for inp in all_inputs:
for _ in range(inp.theory_credits):
theory_pool.append(inp)
random.shuffle(theory_pool)
subject_daily_counts = {}
# First pass: max 2 same-subject per day
for day in days_list:
free_slots = [
i
for i in range(len(ACADEMIC_SLOTS))
if (day, i) not in grid and not _in_reserved(day, i)
]
random.shuffle(free_slots)
for slot_idx in free_slots:
if not theory_pool:
break
start_time = ACADEMIC_SLOTS[slot_idx][0]
idxs = list(range(len(theory_pool)))
random.shuffle(idxs)
for i in idxs:
cand = theory_pool[i]
if check_single_conflict(
cand.teacher_name, day, start_time, class_key, grid
):
continue
s_key = (day, cand.id)
if subject_daily_counts.get(s_key, 0) >= 2:
continue
grid[(day, slot_idx)] = {"type": "TH", "subject": cand, "batch": "ALL"}
subject_daily_counts[s_key] = subject_daily_counts.get(s_key, 0) + 1
theory_pool.pop(i)
break
# Theory backfill: relax daily cap to 3, still skip reserved pairs
if theory_pool:
all_free = [
(d, s)
for d in days_list
for s in range(len(ACADEMIC_SLOTS))
if (d, s) not in grid and not _in_reserved(d, s)
]
random.shuffle(all_free)
for d, s in all_free:
if not theory_pool:
break
start_time = ACADEMIC_SLOTS[s][0]
for i, cand in enumerate(theory_pool):
s_key = (d, cand.id)
if subject_daily_counts.get(s_key, 0) >= 3:
continue
if check_single_conflict(
cand.teacher_name, d, start_time, class_key, grid
):
continue
grid[(d, s)] = {"type": "TH", "subject": cand, "batch": "ALL"}
subject_daily_counts[s_key] = subject_daily_counts.get(s_key, 0) + 1
theory_pool.pop(i)
break
# ──────────────────────────────────────────────────────────────────────────
# PRACTICAL BACKFILL — fill reserved pairs with remaining sessions
# ──────────────────────────────────────────────────────────────────────────
if remaining_sessions and reserved_pairs:
reserved_slot_list = [
(day, s1, s2)
for day in days_list
for s1, s2 in ADJACENT_PAIRS
if (day, s1) in reserved_pairs
]
random.shuffle(reserved_slot_list)
for day, s1, s2 in reserved_slot_list:
if not remaining_sessions:
break
if (day, s1) in grid or (day, s2) in grid:
continue
st1 = ACADEMIC_SLOTS[s1][0]
st2 = ACADEMIC_SLOTS[s2][0]
placed = False
for idx, session in enumerate(remaining_sessions):
subj_A1, subj_A2, subj_A3 = session
teachers = list(
dict.fromkeys(
t
for t in [
subj_A1.teacher_name,
subj_A2.teacher_name,
subj_A3.teacher_name,
]
if t != "-"
)
)
if check_teacher_conflict_bulk(teachers, day, st1, class_key, grid):
continue
if check_teacher_conflict_bulk(teachers, day, st2, class_key, grid):
continue
trio = [subj_A1, subj_A2, subj_A3]
grid[(day, s1)] = {"type": "PR", "trio": trio}
grid[(day, s2)] = {"type": "PR", "trio": trio}
remaining_sessions.pop(idx)
placed = True
break
# If teacher conflict prevents EVERY session here, put Library
# so the slot is not left empty in the output
if not placed:
grid[(day, s1)] = {
"type": "FILLER",
"subject_name": "Library",
"batch": "ALL",
}
grid[(day, s2)] = {
"type": "FILLER",
"subject_name": "Library",
"batch": "ALL",
}
# ──────────────────────────────────────────────────────────────────────────
# PHASE 4 — EXTRA / FILLER (only if allow_extra=True)
# ──────────────────────────────────────────────────────────────────────────
if allow_extra:
extra_counts = {inp.id: 0 for inp in all_inputs}
final_gaps = [
(d, s)
for d in days_list
for s in range(len(ACADEMIC_SLOTS))
if (d, s) not in grid
]
random.shuffle(final_gaps)
for day, slot_idx in final_gaps:
if (day, slot_idx) in grid:
continue
start_time = ACADEMIC_SLOTS[slot_idx][0]
candidates = sorted(all_inputs, key=lambda x: extra_counts[x.id])
placed_extra = None
for cand in candidates:
if not check_single_conflict(
cand.teacher_name, day, start_time, class_key, grid
):
placed_extra = cand
break
if placed_extra:
grid[(day, slot_idx)] = {
"type": "EXTRA",
"subject": placed_extra,
"batch": "ALL",
}
extra_counts[placed_extra.id] += 1
else:
grid[(day, slot_idx)] = {
"type": "FILLER",
"subject_name": "Library",
"batch": "ALL",
}
# ──────────────────────────────────────────────────────────────────────────
# PERSIST TO DATABASE
# ──────────────────────────────────────────────────────────────────────────
for (day, slot_idx), data in grid.items():
start, end = ACADEMIC_SLOTS[slot_idx]
dtype = data["type"]
if dtype in ("TH", "EXTRA"):
subj = data["subject"]
base_name = get_abbr(subj.subject_name)
s_name = f"{base_name} - E" if dtype == "EXTRA" else base_name
TimetableModel.objects.create(
day=day,
start_time=start,
end_time=end,
subject_name=s_name,
teacher_name=normalize_teacher_name(subj.teacher_name),
batch="ALL",
)
elif dtype == "PR":
trio_labs = data["trio"]
for idx, batch_code in enumerate(("A1", "A2", "A3")):
if idx < len(trio_labs):
lab_obj = trio_labs[idx]
s_name = get_abbr(lab_obj.subject_name)
t_name = normalize_teacher_name(lab_obj.teacher_name)
else:
s_name, t_name = "Free", "-"
TimetableModel.objects.create(
day=day,
start_time=start,
end_time=end,
subject_name=s_name,
teacher_name=t_name,
batch=batch_code,
)
elif dtype == "FILLER":
TimetableModel.objects.create(
day=day,
start_time=start,
end_time=end,
subject_name="Library",
teacher_name="-",
batch="ALL",
)
return True, "Generated"
if class_key not in CLASS_CONFIG:
return False, "Invalid Class"
InputModel = CLASS_CONFIG[class_key]["input"]
TimetableModel = CLASS_CONFIG[class_key]["timetable"]
TimetableModel.objects.all().delete()
all_inputs = list(InputModel.objects.all())
for inp in all_inputs:
inp.teacher_name = normalize_teacher_name(inp.teacher_name)
days_list = [d[0] for d in DAYS]
grid = {} # (day, slot_index) -> data dict
# ──────────────────────────────────────────────────────────────────────────
# PHASE 1 — PRACTICALS
# ──────────────────────────────────────────────────────────────────────────
# Build per-batch pools with UNIQUE subject assignment per batch.
# Subjects are distributed round-robin so each batch gets a different subject,
# guaranteeing no two batches share the same teacher in a practical block.
# e.g. A1 → STE, A2 → OSY, A3 → CLC (for a 3-subject practical slot)
lab_pools = {"A1": [], "A2": [], "A3": []}
batches = ["A1", "A2", "A3"]
practical_subjects = [inp for inp in all_inputs if inp.practical_credits > 0]
# Assign each practical subject exclusively to one batch (round-robin by subject index)
for subj_idx, inp in enumerate(practical_subjects):
assigned_batch = batches[subj_idx % 3]
blocks_needed = inp.practical_credits // 2
for _ in range(blocks_needed):
lab_pools[assigned_batch].append(inp)
for b in lab_pools:
random.shuffle(lab_pools[b])
# Build list of all (day, adjacent_pair) candidates — no 1-per-day limit.
all_pr_candidates = []
for day in days_list:
for s1, s2 in ADJACENT_PAIRS:
all_pr_candidates.append((day, s1, s2))
random.shuffle(all_pr_candidates)
def _try_place_trio(day, s1, s2, p1, p2, p3):
"""Try every combination of subjects from three lists; return winning trio or None."""
st1 = ACADEMIC_SLOTS[s1][0]
st2 = ACADEMIC_SLOTS[s2][0]
cands = list(itertools.product(p1, p2, p3))
random.shuffle(cands)
for c1, c2, c3 in cands:
# Subjects must differ across batches (no two batches doing same subject)
if len({c1.id, c2.id, c3.id}) < 3:
continue
teachers = list(
dict.fromkeys([c1.teacher_name, c2.teacher_name, c3.teacher_name])
)
if check_teacher_conflict_bulk(teachers, day, st1, class_key, grid):
continue
if check_teacher_conflict_bulk(teachers, day, st2, class_key, grid):
continue
return [c1, c2, c3]
return None
def _place_pr_blocks():
"""Consume lab_pools until all three are empty (not just when ALL three have items)."""
for day, s1, s2 in all_pr_candidates:
# Stop when every pool is empty
if not any(lab_pools[b] for b in ("A1", "A2", "A3")):
break
if (day, s1) in grid or (day, s2) in grid:
continue
# We can skip this slot if no pool has items
non_empty = [b for b in ("A1", "A2", "A3") if lab_pools[b]]
if not non_empty:
break
# Attempt a full trio (all three batches have pending work)
if lab_pools["A1"] and lab_pools["A2"] and lab_pools["A3"]:
trio = _try_place_trio(
day, s1, s2, lab_pools["A1"], lab_pools["A2"], lab_pools["A3"]
)
if trio:
_commit_trio(day, s1, s2, trio)
continue
# Partial trio — at least one batch pool has leftover items; pad others with
# a Library placeholder so the slot is still useful for non-empty batches.
class _Lib:
subject_name = "Library"
teacher_name = "-"
id = -1
padded = [
lab_pools[b][0] if lab_pools[b] else _Lib() for b in ("A1", "A2", "A3")
]
st1 = ACADEMIC_SLOTS[s1][0]
st2 = ACADEMIC_SLOTS[s2][0]
real = [x for x in padded if x.id != -1]
if not real:
continue
teachers = list(
dict.fromkeys(x.teacher_name for x in real if x.teacher_name != "-")
)
if check_teacher_conflict_bulk(teachers, day, st1, class_key, grid):
continue
if check_teacher_conflict_bulk(teachers, day, st2, class_key, grid):
continue
_commit_trio(day, s1, s2, padded)
def _commit_trio(day, s1, s2, trio):
grid[(day, s1)] = {"type": "PR", "trio": trio, "batches": ["A1", "A2", "A3"]}
grid[(day, s2)] = {"type": "PR", "trio": trio, "batches": ["A1", "A2", "A3"]}
for idx, b in enumerate(("A1", "A2", "A3")):
lab = trio[idx]
if lab.id != -1 and lab in lab_pools[b]:
lab_pools[b].remove(lab)
_place_pr_blocks()
# ──────────────────────────────────────────────────────────────────────────
# PHASE 2 — THEORY (fills all remaining free slots)
# ──────────────────────────────────────────────────────────────────────────
theory_pool = []
for inp in all_inputs:
for _ in range(inp.theory_credits):
theory_pool.append(inp)
random.shuffle(theory_pool)
subject_daily_counts = {}
# First pass: max 2 same-subject per day
for day in days_list:
free_slots = [i for i in range(len(ACADEMIC_SLOTS)) if (day, i) not in grid]
random.shuffle(free_slots)
for slot_idx in free_slots:
if not theory_pool:
break
start_time = ACADEMIC_SLOTS[slot_idx][0]
idxs = list(range(len(theory_pool)))
random.shuffle(idxs)
for i in idxs:
cand = theory_pool[i]
if check_single_conflict(
cand.teacher_name, day, start_time, class_key, grid
):
continue
s_key = (day, cand.id)
if subject_daily_counts.get(s_key, 0) >= 2:
continue
grid[(day, slot_idx)] = {"type": "TH", "subject": cand, "batch": "ALL"}
subject_daily_counts[s_key] = subject_daily_counts.get(s_key, 0) + 1
theory_pool.pop(i)
break
# Backfill: relax daily cap to 3 for any remaining theory tasks
if theory_pool:
all_free = [
(d, s)
for d in days_list
for s in range(len(ACADEMIC_SLOTS))
if (d, s) not in grid
]
random.shuffle(all_free)
for d, s in all_free:
if not theory_pool:
break
start_time = ACADEMIC_SLOTS[s][0]
for i, cand in enumerate(theory_pool):
s_key = (d, cand.id)
if subject_daily_counts.get(s_key, 0) >= 3:
continue
if check_single_conflict(
cand.teacher_name, d, start_time, class_key, grid
):
continue
grid[(d, s)] = {"type": "TH", "subject": cand, "batch": "ALL"}
subject_daily_counts[s_key] = subject_daily_counts.get(s_key, 0) + 1
theory_pool.pop(i)
break
# Phase 2.5 backfill removed — practical deficit is handled post-persist
# by fill_practical_deficit_for_class() called at the end.
# ──────────────────────────────────────────────────────────────────────────
# PHASE 3 — EXTRA / FILLER (only if allow_extra=True)
# ──────────────────────────────────────────────────────────────────────────
if allow_extra:
extra_counts = {inp.id: 0 for inp in all_inputs}
final_gaps = [
(d, s)
for d in days_list
for s in range(len(ACADEMIC_SLOTS))
if (d, s) not in grid
]
random.shuffle(final_gaps)
for day, slot_idx in final_gaps:
if (day, slot_idx) in grid:
continue
start_time = ACADEMIC_SLOTS[slot_idx][0]
# Pick the least-used subject that doesn't conflict
candidates = sorted(all_inputs, key=lambda x: extra_counts[x.id])
placed_extra = None
for cand in candidates:
if not check_single_conflict(
cand.teacher_name, day, start_time, class_key, grid
):
placed_extra = cand
break
if placed_extra:
grid[(day, slot_idx)] = {
"type": "EXTRA",
"subject": placed_extra,
"batch": "ALL",
}
extra_counts[placed_extra.id] += 1
else:
grid[(day, slot_idx)] = {
"type": "FILLER",
"subject_name": "Library",
"batch": "ALL",
}
# ──────────────────────────────────────────────────────────────────────────
# PERSIST TO DATABASE
# ──────────────────────────────────────────────────────────────────────────
for (day, slot_idx), data in grid.items():
start, end = ACADEMIC_SLOTS[slot_idx]
dtype = data["type"]
if dtype in ("TH", "EXTRA"):
subj = data["subject"]
base_name = get_abbr(subj.subject_name)
s_name = f"{base_name} - E" if dtype == "EXTRA" else base_name
TimetableModel.objects.create(
day=day,
start_time=start,
end_time=end,
subject_name=s_name,
teacher_name=normalize_teacher_name(subj.teacher_name),
batch="ALL",
)
elif dtype == "PR":
trio_labs = data["trio"]
for idx, batch_code in enumerate(("A1", "A2", "A3")):
if idx < len(trio_labs):
lab_obj = trio_labs[idx]
s_name = get_abbr(lab_obj.subject_name)
t_name = normalize_teacher_name(lab_obj.teacher_name)
else:
s_name, t_name = "Free", "-"
TimetableModel.objects.create(
day=day,
start_time=start,
end_time=end,
subject_name=s_name,
teacher_name=t_name,
batch=batch_code,
)
elif dtype == "FILLER":
TimetableModel.objects.create(
day=day,
start_time=start,
end_time=end,
subject_name="Library",
teacher_name="-",
batch="ALL",
)
# ──────────────────────────────────────────────────────────────────────────
# PHASE 4 — DB-LEVEL PRACTICAL DEFICIT FILL
# Now that everything is persisted, run the dedicated practical filler.
# It works on actual DB rows and can displace Library/Filler placeholders.
# ──────────────────────────────────────────────────────────────────────────
fill_practical_deficit_for_class(class_key)
return True, "Generated"
# ──────────────────────────────────────────────────────────────────────────────
# DEDICATED PRACTICAL DEFICIT FILLER
# Works on the persisted timetable (DB rows). Called at the end of generation
# and can also be triggered standalone. Displaces Library/Filler slots to place
# practical blocks wherever there is a deficit.
# ──────────────────────────────────────────────────────────────────────────────
def fill_practical_deficit_for_class(class_key):
"""
Scans the generated timetable for practical deficits, finds available
adjacent-pair slots (free OR Library/Filler), and fills them with the
missing practical workload.
Strategy per batch:
1. Compute how many 2-hr practical blocks are still needed per (subject, batch).
2. Collect candidate adjacent slot pairs that are free or displace-able.
3. For each candidate pair, build a valid trio (A1, A2, A3) where each
batch's subject is unique. Batches with no deficit get Library.
4. Write the practical rows into DB, removing any displaced Library rows.
"""
if class_key not in CLASS_CONFIG:
return
cfg = CLASS_CONFIG[class_key]
InputModel = cfg["input"]
TimetableModel = cfg["timetable"]
all_inputs = list(InputModel.objects.all())
for inp in all_inputs:
inp.teacher_name = normalize_teacher_name(inp.teacher_name)
days_list = [d[0] for d in DAYS]
# ── Step 1: compute how many practical blocks are placed per (subject, batch) ──
def _placed_blocks(inp, batch):
"""
Count 2-hr practical blocks already in DB for this subject+batch.
We count unique (day, start_time) pairs where start_time is a pair-start.
"""
pair_starts = {ACADEMIC_SLOTS[s1][0] for s1, _ in ADJACENT_PAIRS}
rows = (
TimetableModel.objects.filter(
subject_name=get_abbr(inp.subject_name),
batch=batch,
start_time__in=pair_starts,
)
.values_list("day", "start_time")
.distinct()
)
return len(set(rows))
# Build deficit map: {batch: [(subject, blocks_needed), ...]}
deficit_map = {"A1": [], "A2": [], "A3": []}
for inp in all_inputs:
if inp.practical_credits <= 0:
continue
expected = inp.practical_credits // 2
for batch in ("A1", "A2", "A3"):
placed = _placed_blocks(inp, batch)
if placed < expected:
deficit_map[batch].append((inp, expected - placed))
if not any(deficit_map[b] for b in ("A1", "A2", "A3")):
return # Nothing to do
# ── Step 2: find candidate adjacent pairs ──
# Priority: fully free pairs first, then pairs that only have Library/Filler.
def _is_displaceable(day, start_time, end_time):
"""True if the slot only has Library or Filler records (safe to replace)."""
rows = TimetableModel.objects.filter(day=day, start_time=start_time)
if not rows.exists():
return True # completely free
return all(r.subject_name in ("Library", "Free") for r in rows)
candidate_pairs = []
for day in days_list:
for s1, s2 in ADJACENT_PAIRS:
st1, et1 = ACADEMIC_SLOTS[s1]
st2, et2 = ACADEMIC_SLOTS[s2]
if _is_displaceable(day, st1, et1) and _is_displaceable(day, st2, et2):
# Check no practical already occupies this pair for ANY batch
if (
not TimetableModel.objects.filter(
day=day, start_time=st1, batch__in=("A1", "A2", "A3")
)
.exclude(subject_name__in=("Library", "Free"))
.exists()
):
candidate_pairs.append((day, s1, s2))
random.shuffle(candidate_pairs)
# ── Step 3 & 4: fill deficit practical blocks ──
class _Lib:
subject_name = "Library"
teacher_name = "-"
id = -1
batch_order = ("A1", "A2", "A3")
for day, s1, s2 in candidate_pairs:
# Rebuild deficit lists each iteration (counts may have changed)
remaining = {b: [inp for inp, _ in deficit_map[b]] for b in batch_order}
if not any(remaining[b] for b in batch_order):
break
st1, et1 = ACADEMIC_SLOTS[s1]
st2, et2 = ACADEMIC_SLOTS[s2]
# Pick one subject per batch (the first available in deficit list)
trio_subjs = []
for batch in batch_order:
if remaining[batch]:
trio_subjs.append(remaining[batch][0])
else:
trio_subjs.append(_Lib())
# Enforce uniqueness: all real (non-Library) subjects must differ
real = [x for x in trio_subjs if x.id != -1]
real_ids = [x.id for x in real]
if len(real_ids) != len(set(real_ids)):
continue # duplicate subjects — skip this pair
# Teacher conflict check against other classes
teachers = list(
dict.fromkeys(x.teacher_name for x in real if x.teacher_name != "-")
)
if check_teacher_conflict_bulk(teachers, day, st1, class_key, None):
continue
if check_teacher_conflict_bulk(teachers, day, st2, class_key, None):
continue
# Displace any existing Library/Filler rows in both slots
TimetableModel.objects.filter(day=day, start_time__in=[st1, st2]).delete()
# Write PR rows for each slot in the pair
for slot_start, slot_end in [(st1, et1), (st2, et2)]:
for idx, batch in enumerate(batch_order):
subj = trio_subjs[idx]
TimetableModel.objects.create(
day=day,
start_time=slot_start,
end_time=slot_end,
subject_name=get_abbr(subj.subject_name),
teacher_name=normalize_teacher_name(subj.teacher_name),
batch=batch,
)
# Decrement deficit counts
for idx, batch in enumerate(batch_order):
subj = trio_subjs[idx]
if subj.id != -1:
deficit_map[batch] = [
(inp, cnt - 1) if inp.id == subj.id else (inp, cnt)
for inp, cnt in deficit_map[batch]
]
deficit_map[batch] = [
(inp, cnt) for inp, cnt in deficit_map[batch] if cnt > 0
]
# ──────────────────────────────────────────────────────────────────────────────
# VALIDATION & ANALYTICS (unchanged logic, kept for views.py compatibility)
# ──────────────────────────────────────────────────────────────────────────────
def validate_workload_distribution(class_key):
"""Comprehensive validation of practical and theory workload distribution."""
if class_key not in CLASS_CONFIG:
return {"error": "Invalid Class"}
from datetime import datetime
cfg = CLASS_CONFIG[class_key]
TimetableModel = cfg["timetable"]
InputModel = cfg["input"]
validation = {
"theory_distribution": [],
"practical_distribution": [],
"theory_balanced": True,
"practical_balanced": True,
"total_theory_deficit": 0,
"total_practical_deficit": 0,
"recommendations": [],
}
inputs = InputModel.objects.all()
for inp in inputs:
exp_theory = inp.theory_credits
abbr = get_abbr(inp.subject_name)
actual_theory = (
TimetableModel.objects.filter(subject_name=abbr, batch="ALL")
.exclude(subject_name__contains=" - E")
.count()
)
extra_theory = TimetableModel.objects.filter(
subject_name__contains=f"{abbr} - E", batch="ALL"
).count()
theory_status = "OK"
if actual_theory < exp_theory:
deficit = exp_theory - actual_theory
theory_status = f"DEFICIT: {deficit} sessions"
validation["theory_balanced"] = False
validation["total_theory_deficit"] += deficit
validation["recommendations"].append(
f"Add {deficit} more theory session(s) for {inp.subject_name}"
)
elif actual_theory > exp_theory:
excess = actual_theory - exp_theory
theory_status = f"EXCESS: {excess} sessions"
validation["theory_distribution"].append(
{
"subject": inp.subject_name,
"teacher": inp.teacher_name,
"expected": exp_theory,
"actual": actual_theory,
"extra": extra_theory,
"status": theory_status,
}
)
if inp.practical_credits > 0:
exp_practical_blocks = inp.practical_credits // 2
for batch in ("A1", "A2", "A3"):
practical_entries = TimetableModel.objects.filter(
subject_name=abbr, batch=batch
).order_by("day", "start_time")
# Count 2-hour blocks (consecutive pairs)
actual_blocks = 0
prev_entry = None
for entry in practical_entries:
if prev_entry:
time_diff = (
datetime.combine(datetime.today(), entry.start_time)
- datetime.combine(datetime.today(), prev_entry.end_time)
).seconds
if time_diff <= 900:
actual_blocks += 1
prev_entry = None
continue
prev_entry = entry
if len(practical_entries) % 2 == 1:
actual_blocks += 0.5
practical_status = "OK"
if actual_blocks < exp_practical_blocks:
deficit = exp_practical_blocks - actual_blocks
practical_status = f"DEFICIT: {deficit} blocks"
validation["practical_balanced"] = False
validation["total_practical_deficit"] += deficit
validation["recommendations"].append(
f"Add {deficit} practical block(s) for {inp.subject_name} - Batch {batch}"
)
elif actual_blocks > exp_practical_blocks:
excess = actual_blocks - exp_practical_blocks
practical_status = f"EXCESS: {excess} blocks"
validation["practical_distribution"].append(
{
"subject": inp.subject_name,
"teacher": inp.teacher_name,
"batch": batch,
"expected_blocks": exp_practical_blocks,
"actual_blocks": actual_blocks,
"status": practical_status,
}
)
if validation["theory_balanced"] and validation["practical_balanced"]:
validation["overall_status"] = "BALANCED"
elif not validation["theory_balanced"] and not validation["practical_balanced"]:
validation["overall_status"] = (
"CRITICAL: Both theory and practical workload unbalanced"
)
elif not validation["theory_balanced"]:
validation["overall_status"] = "WARNING: Theory workload unbalanced"
else:
validation["overall_status"] = "WARNING: Practical workload unbalanced"
return validation
def analyze_timetable(class_key):
"""Analyzes the generated timetable for conflicts and workload distribution."""
if class_key not in CLASS_CONFIG:
return {"error": "Invalid Class"}
cfg = CLASS_CONFIG[class_key]
TimetableModel = cfg["timetable"]
InputModel = cfg["input"]
analysis = {
"conflicts": [],
"distribution": [],
"is_balanced": True,
"has_conflicts": False,
}
my_entries = TimetableModel.objects.all()
for entry in my_entries:
if entry.teacher_name in ("-", "Free"):
continue
for other_key, other_cfg in CLASS_CONFIG.items():
if other_key == class_key:
continue
OtherTimetable = other_cfg["timetable"]
overlaps = OtherTimetable.objects.filter(
teacher_name=entry.teacher_name,
day=entry.day,
start_time=entry.start_time,
)
for overlap in overlaps:
analysis["conflicts"].append(
{
"teacher": entry.teacher_name,
"day": entry.day,
"time": f"{entry.start_time} - {entry.end_time}",
"other_class": other_cfg["name"],
"other_subject": overlap.subject_name,
}
)
analysis["has_conflicts"] = True
inputs = InputModel.objects.all()
for inp in inputs:
exp_th = inp.theory_credits
exp_pr = inp.practical_credits
abbr = get_abbr(inp.subject_name)
act_th = TimetableModel.objects.filter(
subject_name__startswith=abbr, batch="ALL"
).count()
status = "Balanced"
if act_th < exp_th:
status = "Underloaded (Theory)"
analysis["is_balanced"] = False
elif act_th > (exp_th + 2):
status = "Overloaded (Theory)"
batches = ("A1", "A2", "A3")
pr_status = []
for b in batches:
b_act = TimetableModel.objects.filter(subject_name=abbr, batch=b).count()
exp_per_batch = exp_pr # expected total rows per batch
if b_act < exp_per_batch:
pr_status.append(f"{b}: Low ({b_act}/{exp_per_batch})")
analysis["is_balanced"] = False
elif b_act > exp_per_batch:
pr_status.append(f"{b}: High ({b_act}/{exp_per_batch})")
analysis["distribution"].append(
{
"subject": inp.subject_name,
"teacher": inp.teacher_name,
"expected_th": exp_th,
"actual_th": act_th,
"expected_pr": exp_pr,
"practical_status": ", ".join(pr_status) if pr_status else "OK",
"status": status,
}
)
return analysis
# ──────────────────────────────────────────────────────────────────────────────
# STANDALONE helpers called by add_extra_lectures_view
# ──────────────────────────────────────────────────────────────────────────────
def fill_extra_lectures_for_class(class_key):
"""
Fill any remaining empty (non-break) slots in an already-generated
timetable with extra lectures. Does NOT clear or regenerate the timetable.
"""
if class_key not in CLASS_CONFIG:
return
cfg = CLASS_CONFIG[class_key]
InputModel = cfg["input"]
TimetableModel = cfg["timetable"]
all_inputs = list(InputModel.objects.all())
days_list = [d[0] for d in DAYS]
# Build a set of already-occupied (day, start_time) keys
occupied = set(TimetableModel.objects.values_list("day", "start_time"))
extra_counts = {inp.id: 0 for inp in all_inputs}
candidates = [
(day, start, end)
for day in days_list
for start, end in ACADEMIC_SLOTS
if (day, start) not in occupied
]
random.shuffle(candidates)
for day, start, end in candidates:
sorted_inputs = sorted(all_inputs, key=lambda x: extra_counts[x.id])
placed = None
for cand in sorted_inputs:
# Simple conflict check against other classes only; within-class is excluded
conflict = any(
cfg2["timetable"]
.objects.filter(
teacher_name=normalize_teacher_name(cand.teacher_name),
day=day,
start_time=start,
)
.exists()
for key2, cfg2 in CLASS_CONFIG.items()
if key2 != class_key
)
if not conflict:
placed = cand
break
if placed:
TimetableModel.objects.create(
day=day,
start_time=start,
end_time=end,
subject_name=f"{get_abbr(placed.subject_name)} - E",
teacher_name=normalize_teacher_name(placed.teacher_name),
batch="ALL",
)
extra_counts[placed.id] += 1
else:
TimetableModel.objects.create(
day=day,
start_time=start,
end_time=end,
subject_name="Library",
teacher_name="-",
batch="ALL",
)
|