GitHub repo: https://github.com/KajanM/TimeTableScheduler
- navigate to the
executabledirectory which hasscheduler.jar - ensure the input, output
csvfiles are in the same directory - open terminal from that directory
- run
java -jar scheduler.jar <input_file_name> <output_file_name>(do not include.csvextension)
[kajan@kajan executable]$ java -jar scheduler.jar test-input-2 test-output
Debug enabled: false
Found Linux OS, setting path separator to /
Input file contents
# 6 subjects, 6 available slots
S1, o, M1, M2, M3, T1
S2, o, M1, M2, M3, T2
S3, o, M1, M2, M3, T3
S4, o, M3
S5, c, M2
S6, o, M1
R1
Parsing input csv file completed
Assigning priority to subjects based on `Minimum Remaining Value` and `Degree` heuristics.
Recursive scheduling job started at Mon Sep 03 06:46:30 IST 2018
Scheduling job completed at Mon Sep 03 06:46:30 IST 2018
Scheduling process took 15 milli seconds
Results
S4 | compulsory: false | R1 | M3
S6 | compulsory: false | R1 | M1
S5 | compulsory: true | R1 | M2
S1 | compulsory: false | R1 | T1
S2 | compulsory: false | R1 | T2
S3 | compulsory: false | R1 | T3
Running test cases to verify all constraints are met...
Passed: No subjects are assigned to same room and time
Passed: No other subjects assigned at a compulsory subject time
Passed: Assigned time is one of given applicable times
[kajan@kajan executable]$
-
Subjectobject representing a subject that need to be scheduled with the following fields- name
- compulsory or not
- scheduled room and time (represented by a single object
RoomAndTime) - priority (to apply heuristics when selecting which subject to schedule next/first)
- set of applicable slots
-
RoomAndTimeobject to hold combination of room and time. Since both room and time need to be assigned for a subject they are combined to form a single object to applyOOPconcepts. -
subjectsToSchedule- aSet(no duplicates) ofSubjects to be scheduled. -
subjectsScheduled- OrderedsetofSubjects that are scheduled so far. -
availableRoomsAndTimes-SetofRoomAndTimeobjects that are available. -
assignedRoomsAndTimes-SetofRoomAndTimeobjects that are scheduled.
Domain for each variable is extracted from the input csv file.
Input file should be of the following format for correct processing.
#comments are ignored from parsing
Subject_1, c, M1, M3, Tu2
Subject_2, o, Tu1, W1, Th2
Subject_3, c, M1, M3, W1
.
.
.
Subject_n, o, M3, Th2
R1, R2, R3
- While parsing the input
csvfile, createRoomAndTimeandSubjectobjects. - add all
Subjects tosubjectsToSchedule - add all
RoomAndTimetoavailableRoomsAndTimes - initialize
subjectsScheduledas emptyset - initialize
assignedRoomsAndTimesas emptyset
This is a recursive operation, i.e this operation calls itself until there is no Subject to assign or infinite loop is detected.
- get a
SubjectfromsubjectsToSchedule - iterate through each of the applicable timeslot for the selected
Subject - check if the applicable slot is
consistentto current assignment - forward check to see if the assignment will fail any of the remaining
Subjectto schedule - once a
consistentslot is assigned to theSubjectmove it to thesubjectsScheduledset - update
availableRoomsAndTimes,assignedRoomsAndTimesaccordingly - if no consistent value is found then
backtrack
When selecting a RoomAndTime to assign to a Subject following constraints are checked.
- A given subjects can be assigned only to one of the possible time slots given for that subject.
- No other subject is assigned to a time that is already assigned to a compulsory subject regardless of room.
- Two subjects cannot be assigned to the same room if they are assigned to the same time slot.
Test cases are also written to ensure the end result satisfies above constraints. They are availabe at TestCases.java. Below is a snippet of the code.
public static void onlyApplicableTimeIsAssigned(Set<Subject> subjectsScheduled) {
for(Subject subject : subjectsScheduled) {
if(!subject.getApplicableSlots().contains(subject.getRoomAndTime())) {
System.out.println("Failed: Assigned time is not applicable for the subject " + subject);
return;
}
}
System.out.println("Passed: Assigned time is one of given applicable times");
}
Before assigning a consistent RoomAndTime value to a Subject, the logic ensures that remaining Subjects to be scheduled has atleast one applicable RoomAndTime which is also availabe.
When the logic finds there is no consistent RoomAndTime to assign to a Subject, it removes the cause of assignment from subjectsScheduled and put into subjectsToSchedule and update other relevant parameters(availableRoomsAndTimes, ``assignedRoomsAndTimes`)
Priority is assigned to each Subject based on following heuristics. Subject with smaller priority value is considered to have higer priority.
Subject with the fewest number of applicable slots is chosen for next assignment.
Priority values of compulsory Subjects are increased by one to give higer priority to optional Subjects.