For the input lesson: CS3343L01 WEB 9-11 CS3343T01 WEB 11-12 CS3343L02 THU 9-11 CS2222L01 WEB 10-11 CS2222T01 WEB 12-13 CS2222L02 THU 12-13 The expect result should return two possible schedule as follow: Possible Schedule 1 : 3 10 - 11 ---- CS2222L01 3 11 - 12 ---- CS3343T01 3 12 - 13 ---- CS2222T01 4 9 - 11 ---- CS3343L02 Total Distance: 3 Possible Schedule 2 : 3 9 - 11 ---- CS3343L01 3 11 - 12 ---- CS3343T01 3 12 - 13 ---- CS2222T01 4 12 - 13 ---- CS2222L02 Total Distance: 4 However, the function only return one schedule: Possible Schedule 1 : 3 9 - 11 ---- CS3343L01 3 11 - 12 ---- CS3343T01 3 12 - 13 ---- CS2222T01 4 12 - 13 ---- CS2222L02 Total Distance: 4 I suspect a deep copy problem cause this bug. As the function will clone a new studentSchedule from last schedule if the lesson is overleap (In Line175: StudentSchedule TempNewStudentSchedule = (StudentSchedule)tempNewStudentSchedule.clone();) But when generate the new schedule, it changing the object in the old schedule(ArrayList<Lesson> lessonSelected). The clone of StudentSchedule maybe is only a shallow copy rather than a deep copy, which lead only return one schedule while there are multiple Possible Schedule.
Edit the function findOptimalScheule(StudentSchedule tempNewStudentSchedule,int pointer) in Class InteralScheduling. Change: StudentSchedule TempNewStudentSchedule = (StudentSchedule)tempNewStudentSchedule.clone(); --- In Line175 To: Gson gson = new Gson(); StudentSchedule TempNewStudentSchedule = gson.fromJson(gson.toJson(tempNewStudentSchedule), StudentSchedule.class); The reason of bug is because the second schedule is the shallow copy of the first schedule object. When running the function findOptimalScheule() for the second schedule, it changing the object in the first schedule rather than clone a new obj for the second schedule. Thus, a deep copy is need. We using the library of Gson to transfer the StudentSchedule obj to JSON format for easier deep copy.