-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScheduleGenerator.java
553 lines (473 loc) · 22.5 KB
/
ScheduleGenerator.java
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
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.DayOfWeek;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.Collections;
import java.util.Comparator;
public class ScheduleGenerator {
public static List<Course> expandCoursesWithSections(List<Course> courses) {
List<Course> expandedCourses = new ArrayList<>();
for (Course course : courses) {
if (course.getName().endsWith("Lab") || course.getName().endsWith("Tutorial")) {
if (course.getTimeSlots().size() >= 1) {
for (int i = 0; i < course.getTimeSlots().size(); i++) {
String newCourseName = course.getName() + " Section " + (i + 1);
// System.out.println(course.getName());
expandedCourses.add(new Course(newCourseName, Collections.singletonList(course.getTimeSlots().get(i))));
}
} else {
expandedCourses.add(course);
}
} else {
expandedCourses.add(course);
}
}
return expandedCourses;
}
public static String properName(Course course, int sectionOrGroupNumber){
if(course.getName().contains("Lecture")){
return course.getName() + " Group " + sectionOrGroupNumber;
}
else{
return course.getName() + " Section " + sectionOrGroupNumber;
}
}
public static int timeSpentPerWeek(List<CourseSlot> schedule) {
HashMap<DayOfWeek, LocalTime[]> dayTimings = new HashMap<>();
for (DayOfWeek day : DayOfWeek.values()) {
dayTimings.put(day, new LocalTime[]{null, null});
}
for (CourseSlot slot : schedule) {
DayOfWeek day = slot.getDayOfWeek();
LocalTime[] times = dayTimings.get(day);
if (times[0] == null || slot.getStartTime().isBefore(times[0])) {
times[0] = slot.getStartTime();
}
if (times[1] == null || slot.getEndTime().isAfter(times[1])) {
times[1] = slot.getEndTime();
}
}
int totalTimeInSeconds = 0;
for (Map.Entry<DayOfWeek, LocalTime[]> entry : dayTimings.entrySet()) {
LocalTime[] times = entry.getValue();
if (times[0] != null && times[1] != null) {
totalTimeInSeconds += times[1].toSecondOfDay() - times[0].toSecondOfDay();
}
}
return totalTimeInSeconds;
}
public static List<List<Course>> sectionSpecificCombinations(List<Course> courses){
int k = courses.size();
List<List<Course>> possibleCombinations = new ArrayList<>();
if (k == 1){
for(int i = 0; i < courses.get(0).getTimeSlots().size(); i++){
Course currentCourse = new Course(properName(courses.get(0), i+1), //courses.get(0).getName(),
Collections.singletonList(courses.get(0).getTimeSlots().get(i)));
possibleCombinations.add(Collections.singletonList(currentCourse));
}
}
else{
List<List<Course>> possiblePathesAhead = sectionSpecificCombinations(courses.subList(0+1, k));
for(int ii = 0; ii < courses.get(0).getTimeSlots().size(); ii++){
Course currentCourse = new Course(properName(courses.get(0), ii+1),
Collections.singletonList(courses.get(0).getTimeSlots().get(ii)));
List<List<Course>> newPathesAhead = new ArrayList<>();
for (List<Course> path : possiblePathesAhead) {
newPathesAhead.add(new ArrayList<>(path));
newPathesAhead.get(newPathesAhead.size() - 1).add(currentCourse);
}
possibleCombinations.addAll(newPathesAhead);
}
}
return possibleCombinations;
}
public static String getBaseName(CourseSlot slot){
return slot.getCourseName().substring(0, " section 2".length());
}
public static String getBaseName(String courseName){
return courseName.substring(0, " section 2".length());
}
public static boolean filterForSameSectionPerCourse (List<CourseSlot> schedule)
{
boolean result = true;
Iterator<CourseSlot> tutorialIterator = schedule.stream().filter(course -> course.getCourseName().contains("Tutorial")).iterator();
while(tutorialIterator.hasNext()){
Iterator<CourseSlot> labIterator = schedule.stream().filter(course -> course.getCourseName().contains("Lab")).iterator();
String tutorialFullName = tutorialIterator.next().getCourseName();
String tutorialName = getBaseName(tutorialFullName);
String coreqCourseName1 = tutorialName.split(":")[0];
while(labIterator.hasNext()){
String labFullName = labIterator.next().getCourseName();
String labName = getBaseName(labFullName);
String coreqCourseName2 = labName.split(":")[0];
if(coreqCourseName1.equals(coreqCourseName2)){
result = result && (labFullName.charAt(labFullName.length()-1) == tutorialFullName.charAt(tutorialFullName.length()-1));
}
}
}
return result;
}
public static List<List<CourseSlot>> generateSchedules(List<Course> courses) {
List<Course> expandedCourses = expandCoursesWithSections(courses);
List<List<CourseSlot>> schedules = new ArrayList<>();
generateSchedulesHelper(schedules, expandedCourses, new ArrayList<>(), 0);
return schedules;
}
private static void generateSchedulesHelper(List<List<CourseSlot>> schedules,
List<Course> courses, List<CourseSlot> currentSchedule, int courseIndex) {
if (courseIndex == courses.size()) {
schedules.add(new ArrayList<>(currentSchedule));
return;
}
Course course = courses.get(courseIndex);
// Check if this course is a lab or tutorial
boolean isLabOrTutorial = course.getName().endsWith("Lab") || course.getName().endsWith("Tutorial");
for (String[] timeslot : course.getTimeSlots()) {
CourseSlot slot = new CourseSlot(course.getName(), DayOfWeek.valueOf(timeslot[0]),
LocalTime.parse(timeslot[1]), LocalTime.parse(timeslot[2]));
// Check for conflicts
if (!hasConflict(currentSchedule, slot)) {
currentSchedule.add(slot);
// If this is a lab or tutorial, we don't increment the courseIndex as we
// explore all sections
int nextCourseIndex = isLabOrTutorial ? courseIndex : courseIndex + 1;
generateSchedulesHelper(schedules, courses, currentSchedule, nextCourseIndex);
currentSchedule.remove(currentSchedule.size() - 1); // Backtrack
}
}
}
private static boolean hasConflict(List<CourseSlot> schedule, CourseSlot newSlot) {
for (CourseSlot slot : schedule) {
if (slot.overlaps(newSlot)) {
return true;
}
}
return false;
}
public static void printTimetable(List<CourseSlot> schedule) {
// Map days of week to empty lists for storing course slots
HashMap<DayOfWeek, List<CourseSlot>> dayMap = new HashMap<>();
for (DayOfWeek day : DayOfWeek.values()) {
dayMap.put(day, new ArrayList<>());
}
// Add each course slot to its corresponding day list
for (CourseSlot slot : schedule) {
dayMap.get(slot.getDayOfWeek()).add(slot);
}
// Calculate the maximum course name length and find longest day of week name
int maxCourseNameLength = 0;
int longestDayOfWeekLength = 0;
for (DayOfWeek day : DayOfWeek.values()) {
maxCourseNameLength = Math.max(maxCourseNameLength,
dayMap.get(day).stream().map(CourseSlot::getCourseName).mapToInt(String::length).max().orElse(0));
longestDayOfWeekLength = Math.max(longestDayOfWeekLength, day.toString().length());
}
// Calculate total cell width based on longest day of week and course name
int totalCellWidth = Math.max(longestDayOfWeekLength + 2, maxCourseNameLength + 2);
// Header row with days of the week
System.out.print(" ");
for (DayOfWeek day : dayMap.keySet()) {
System.out.printf("| %-" + totalCellWidth + "s", day.toString());
}
System.out.println(" |");
// Time slots
for (int hour = 9; hour <= 15; hour++) {
System.out.printf("%2d:00", hour);
for (DayOfWeek day : dayMap.keySet()) {
List<CourseSlot> daySlots = dayMap.get(day);
boolean foundSlot = false;
for (CourseSlot slot : daySlots) {
if (slot.getStartTime().getHour() == hour) {
// Format course name to fit max length
String formattedName = String.format("%-" + maxCourseNameLength + "s", slot.getCourseName());
System.out.printf("| %-" + totalCellWidth + "s", formattedName);
foundSlot = true;
// break;
}
}
if (!foundSlot) {
// Use same width for empty cells
System.out.printf("| %-" + totalCellWidth + "s", "");
}
}
System.out.println(" |");
}
}
public static void exportSchedulesToFile(String filename, List<List<CourseSlot>> schedules) throws IOException {
try (PrintWriter writer = new PrintWriter(new File(filename))) {
for (int i = 0; i < schedules.size(); i++) {
writer.println("Schedule " + (i + 1) + ": " + schedules.get(i).size() + " Slots");
writer.println("Time spent on campus per week: " + timeSpentPerWeek(schedules.get(i))/(60.0*60.0) + " hours");
printTimetableToFile(schedules.get(i), writer);
writer.println();
}
}
}
private static void printTimetableToFile(List<CourseSlot> schedule, PrintWriter writer) {
// Calculate maximum course name length and longest day of week name
int maxCourseNameLength = 0;
int longestDayOfWeekLength = 0;
for (DayOfWeek day : DayOfWeek.values()) {
maxCourseNameLength = Math.max(maxCourseNameLength, schedule.stream().filter(s -> s.getDayOfWeek() == day)
.map(CourseSlot::getCourseName).mapToInt(String::length).max().orElse(0));
longestDayOfWeekLength = Math.max(longestDayOfWeekLength, day.toString().length());
}
// Calculate total cell width based on longest day of week and course name
int totalCellWidth = Math.max(longestDayOfWeekLength + 2, maxCourseNameLength + 2);
// Header row with days of the week
writer.print(" ");
for (DayOfWeek day : DayOfWeek.values()) {
writer.printf("| %-" + totalCellWidth + "s", day.toString());
}
writer.println(" |");
// Time slots
for (int hour = 9; hour <= 15; hour++) {
writer.printf("%2d:00", hour);
for (DayOfWeek day : DayOfWeek.values()) {
boolean foundSlot = false;
for (CourseSlot slot : schedule) {
if (slot.getDayOfWeek() == day && slot.getStartTime().getHour() == hour) {
// Format course name to fit max length
String formattedName = String.format("%-" + maxCourseNameLength + "s", slot.getCourseName());
writer.printf("| %-" + totalCellWidth + "s", formattedName);
foundSlot = true;
// break;
}
}
if (!foundSlot) {
// Use same width for empty cells
writer.printf("| %-" + totalCellWidth + "s", "");
}
}
writer.println(" |");
}
}
public static void sortSchedulesByDayAndTime(List<List<CourseSlot>> schedules) {
schedules.forEach(schedule -> schedule.sort((slot1, slot2) -> {
if (slot1.getDayOfWeek().compareTo(slot2.getDayOfWeek()) != 0) {
return slot1.getDayOfWeek().compareTo(slot2.getDayOfWeek());
} else {
return slot1.getStartTime().compareTo(slot2.getStartTime());
}
}));
}
public static void main(String[] args) throws IOException {
// Example usage (modify time slots and day of week)
List<Course> courses = new ArrayList<>();
// All available time slots
/*
courses.add(new Course("Compilers: Lecture",
Collections.singletonList(new String[] { "TUESDAY", "09:00", "10:30" }
)));
courses.add(new Course("Compilers: Tutorial", List.of(
new String[] { "THURSDAY", "12:30", "13:15" },
new String[] { "THURSDAY", "14:15", "15:00" },
new String[] { "MONDAY", "12:30", "01:15" }
)));
courses.add(new Course("Compilers: Lab", List.of(
new String[] { "TUESDAY", "12:30", "14:00" },
new String[] { "THURSDAY", "09:00", "10:30" },
new String[] { "WEDNESDAY", "12:30", "14:00" }
)));
courses.add(new Course("Networks: Lecture", List.of(
new String[] { "TUESDAY", "09:00", "10:30" },
new String[] { "TUESDAY", "10:45", "12:15" }
)));
courses.add(new Course("Algorithms: Lecture", List.of(
new String[] { "WEDNESDAY", "10:45", "12:15" },
new String[] { "WEDNESDAY", "14:15","15:45" }
)));
courses.add(new Course("Embedded Systems: Lecture", List.of(
new String[] { "TUESDAY", "12:30", "14:00" },
new String[] { "TUESDAY", "14:15", "15:45" }
)));
courses.add(new Course("Netowrks: Lab", List.of(
new String[] { "MONDAY", "09:00", "10:30" },
new String[] { "MONDAY", "12:30", "14:00" },
new String[] { "WEDNESDAY", "09:00", "10:30" },
new String[] { "WEDNESDAY", "10:45", "12:15" },
new String[] { "MONDAY", "14:15", "15:45" }
)));
courses.add(new Course("Netowrks: Tutorial", List.of(
new String[] { "TUESDAY", "15:00", "15:45" },
new String[] { "WEDNESDAY", "09:00", "09:45" },
new String[] { "MONDAY", "10:45", "11:30" },
new String[] {"MONDAY", "11:30", "12:15"},
new String[] { "WEDNESDAY", "09:45", "10:30"}
)));
courses.add(new Course("Algorithms: Tutorial", List.of(
new String[] { "THURSDAY", "10:45", "12:15" },
new String[] { "THURSDAY", "14:15","15:45" },
new String[] { "MONDAY", "09:00", "10:30" },
new String[] { "MONDAY", "12:30", "14:00"},
new String[] { "TUESDAY", "12:30", "14:00"}
)));
courses.add(new Course("Embedded Systems: Lab", List.of(
new String[] { "MONDAY", "12:30", "14:00" },
new String[] { "MONDAY", "10:45", "12:15" },
new String[] { "TUESDAY", "10:45", "12:15" },
new String[] {"MONDAY", "14:15", "15:45"},
new String[] { "MONDAY", "09:00", "10:30" }
)));
courses.add(new Course("Embedded Systems: Tutorial", List.of(
new String[] { "WEDNESDAY", "15:00", "15:45" },
new String[] { "TUESDAY", "14:15", "15:00" },
new String[] { "WEDNESDAY", "14:15", "15:00" },
new String[] { "TUESDAY", "13:15", "14:00" },
new String[] { "TUESDAY", "09:45", "10:30"}
)));
courses.add(new Course("Public Policy", Collections.singletonList(
// new String[] { "WEDNESDAY","12:30", "14:00" }//,
new String[] { "WEDNESDAY", "14:15", "15:45" }
)));
*/
// ML
courses.add(new Course("ML: Lecture",
Collections.singletonList(new String[] { "MONDAY", "09:00", "10:30" })
));
courses.add(new Course("ML: Tutorial",
Arrays.asList(
new String[] { "THURSDAY", "13:15", "14:00" },
new String[] { "MONDAY", "14:15", "15:00" },
new String[] { "MONDAY", "15:00", "15:45" }
)
));
courses.add(new Course("ML: Lab",
Arrays.asList(
new String[] { "TUESDAY", "14:15", "15:45" },
new String[] { "TUESDAY", "14:15", "15:45" },
new String[] { "TUESDAY", "14:15", "15:45" },
new String[] { "TUESDAY", "12:30", "14:00" },
new String[] { "TUESDAY", "12:30", "14:00" }
)
));
// // CA
// courses.add(new Course("CA: Lecture",
// Collections.singletonList(new String[] { "MONDAY", "10:45", "12:15" })
// ));
// courses.add(new Course("CA: Tutorial",
// Collections.singletonList(new String[] { "WEDNESDAY", "15:00", "15:45" })
// ));
// courses.add(new Course("CA: Lab",
// Collections.singletonList(new String[] { "MONDAY", "12:30", "14:00" })
// ));
// Crypto
courses.add(new Course("Crypto: Lecture",
Arrays.asList(
new String[] { "MONDAY", "12:30", "14:00" },
new String[] { "MONDAY", "14:15", "15:45" }
)
));
courses.add(new Course("Crypto: Tutorial",
Arrays.asList(
new String[] { "WEDNESDAY", "14:15", "15:45" },
new String[] { "THURSDAY", "10:45", "12:15" },
new String[] { "THURSDAY", "12:30", "14:00" },
new String[] { "WEDNESDAY", "12:30", "14:00" },
new String[] { "THURSDAY", "09:00", "10:30" }
)
));
// OS
courses.add(new Course("OS: Lecture",
Arrays.asList(
new String[] { "THURSDAY", "09:00", "10:30" },
new String[] { "THURSDAY", "12:30", "14:00" }
)
));
courses.add(new Course("OS: Tutorial",
Arrays.asList(
new String[] { "MONDAY", "14:15", "15:00" },
new String[] { "MONDAY", "15:00", "15:45" },
new String[] { "TUESDAY", "09:45", "10:30" },
new String[] { "TUESDAY", "14:15", "15:00" },
new String[] { "TUESDAY", "09:00", "09:45" }
)
));
courses.add(new Course("OS: Lab",
Arrays.asList(
new String[] { "THURSDAY", "10:45", "12:15" },
new String[] { "WEDNESDAY", "14:15", "15:45" },
new String[] { "THURSDAY", "14:15", "15:45" },
new String[] { "THURSDAY", "09:00", "10:30" },
new String[] { "WEDNESDAY", "12:30", "14:00" }
)
));
// Embedded
// courses.add(new Course("Embedded: Lecture",
// Collections.singletonList(new String[] { "TUESDAY", "10:45", "12:15" })
// ));
// courses.add(new Course("Embedded: Tutorial",
// Arrays.asList(
// new String[] { "THURSDAY", "12:30", "13:15" },
// new String[] { "WEDNESDAY", "14:15", "15:00" }
// )
// ));
// courses.add(new Course("Embedded: Lab",
// Arrays.asList(
// new String[] { "TUESDAY", "09:00", "10:30" },
// new String[] { "THURSDAY", "10:45", "12:15" }
// )
// ));
// Emerging Topics
/*
courses.add(new Course("Emerging Topics: Lecture",
Collections.singletonList(new String[] { "MONDAY", "10:45", "12:15" })
));
courses.add(new Course("Emerging Topics: Tutorial",
Collections.singletonList(new String[] { "WEDNESDAY", "13:15", "14:00" })
));
courses.add(new Course("Emerging Topics: Lab",
Collections.singletonList(new String[] { "TUESDAY", "12:30", "14:00" })
));
*/
// Computer Graphics
// /*
courses.add(new Course("Computer Graphics: Lecture",
Collections.singletonList(new String[] { "WEDNESDAY", "10:45", "12:15" })
));
courses.add(new Course("Computer Graphics: Tutorial",
Collections.singletonList(new String[] { "WEDNESDAY", "12:30", "13:15" })
));
courses.add(new Course("Computer Graphics: Lab",
Collections.singletonList(new String[] { "WEDNESDAY", "09:00", "10:30" })
));
// */
// // Robotics
// courses.add(new Course("Robotics: Lecture",
// Collections.singletonList(new String[] { "WEDNESDAY", "09:00", "10:30" })
// ));
// courses.add(new Course("Robotics: Tutorial",
// Collections.singletonList(new String[] { "TUESDAY", "15:00", "15:45" })
// ));
// courses.add(new Course("Robotics: Lab",
// Collections.singletonList(new String[] { "WEDNESDAY", "10:45", "12:15" })
// ));
List<List<Course>> allPossiblePathes = sectionSpecificCombinations(courses);
List<List<CourseSlot>> schedules = new ArrayList<>();
int pathesCount = 0;
for(List<Course> path: allPossiblePathes){
pathesCount++;
System.out.println("Path " + pathesCount + " : " + path.size());
schedules.addAll(generateSchedules(path));
}
schedules = schedules.stream().filter(course -> filterForSameSectionPerCourse(course)).collect(Collectors.toList());
sortSchedulesByDayAndTime(schedules);
// sortByGaps(schedules);
Collections.sort(schedules, new Comparator<List<CourseSlot>>(){
public int compare(List<CourseSlot> schedule1, List<CourseSlot> schedule2){
return timeSpentPerWeek(schedule1) - timeSpentPerWeek(schedule2);
}
});
String scheduleTitle = "Reqs + ML + Graphics";
exportSchedulesToFile(scheduleTitle, schedules);
System.out.println("Number of possible schedules: " + schedules.size());
}
}