public bool removeLessonsAndFindNewPosition3(Lesson lesson, ref List <Lesson> allLesson, int level = 1) { //Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>removeLessonsAndFindNewPosition"); //Console.WriteLine("start " + lesson.ToString() + " level " + level); bool result = false; Timetable groupTT = lesson.getGroup().getTimetable(); //List<TimeSlot> groupFreeSlots = groupTT.getFreeSlotsToLesson(lesson); Timetable teacherTT = lesson.getTeacher().getTimetable(); List <TimeSlot> teacherFreeSlots = teacherTT.getFreeSlotsToLesson(lesson); // sloty w salach w które można wstawic daną lekcje List <FreeSlotsInRoomToLesson> freeSlotsInRoomToLesson = new List <FreeSlotsInRoomToLesson>(); foreach (Timetable tt in roomsTimetables) { if (tt.getRoom().getRoomType().Equals(lesson.getRoomType()) && tt.getRoom().getAmount() >= lesson.getGroup().getAmount()) { freeSlotsInRoomToLesson.Add(new FreeSlotsInRoomToLesson(tt.getFreeSlotsToLesson(lesson), tt.getRoom())); } } // sloty w sali i w planie nauczyciela w ktorych mozna wstawic dana lekcje FreeSlotsToLesson freeSlotsToLesson = new FreeSlotsToLesson(teacherFreeSlots, lesson, freeSlotsInRoomToLesson); if (freeSlotsToLesson.slots.Count == 0) { TimeSlot slotToChange = null; result = false; foreach (TimeSlot rts in freeSlotsToLesson.getSlotsFromRoomLists()) { if (teacherTT.getLessons(rts.day, rts.hour).Count == 1) { Lesson lessonToChange = teacherTT.getLessons(rts.day, rts.hour)[0]; if (level < 5) { result = removeLessonsAndFindNewPosition3(lessonToChange, ref allLesson, 4); } if (result) { slotToChange = rts; break; } } } if (!result) { //Console.WriteLine("error : freeSlotsToLesson.slots.Count = 0"); return(result); } freeSlotsToLesson.slots.Add(slotToChange); } // lista slotów skąd może być ściągnięta lekcja w planie dla grupy List <TimeSlot> slots = new List <TimeSlot>(); foreach (TimeSlot ts in freeSlotsToLesson.slots) { if (groupTT.getLessons(ts.day, ts.hour).Count == 1) { if (groupTT.getLessons(ts.day, ts.hour)[0].getSize() == 1) { slots.Add(ts); } } } if (slots.Count == 0) { //slots.Add() return(false); } //znajdujemy zastępczy slot dla losowej lekcji while (slots.Count > 0) { TimeSlot slot = slots[rand.Next(0, slots.Count - 1)]; slots.Remove(slot); //szukam zastepczego slotu Lesson lessonToChange = groupTT.getLessons(slot.day, slot.hour)[0]; List <FreeSlotsInRoomToLesson> freeSlotsInRoomToLessonTMP = new List <FreeSlotsInRoomToLesson>(); foreach (Timetable tt in roomsTimetables) { if (tt.getRoom().getRoomType().Equals(lessonToChange.getRoomType()) && tt.getRoom().getAmount() >= lessonToChange.getGroup().getAmount()) { freeSlotsInRoomToLesson.Add(new FreeSlotsInRoomToLesson(tt.getFreeSlotsToLesson(lessonToChange), tt.getRoom())); } } List <TimeSlot> groupSlots = groupTT.getFreeSlotsToLesson(lessonToChange); List <TimeSlot> teacherSlots = lessonToChange.getTeacher().getTimetable().getFreeSlotsToLesson(lessonToChange); List <TimeSlot> theSameSlots = getTheSameSlots(groupSlots, teacherSlots); FreeSlotsToLesson freeSlotsToLessonTMP = new FreeSlotsToLesson(theSameSlots, lessonToChange, freeSlotsInRoomToLesson); if (freeSlotsToLessonTMP.slots.Count > 0) { Room bestRoom = null; TimeSlot bestSlot = this.getBestTimeSlot(freeSlotsToLessonTMP.slots, freeSlotsToLessonTMP.roomSlots, ref bestRoom); //Console.WriteLine("found : " + lessonToChange.ToString()); lessonToChange.setRoom(bestRoom); lessonToChange.addLessonToTimetableAndBlockSlot(bestSlot.day, bestSlot.hour); lessonToChange.removeLessonFromTimetable(slot.day, slot.hour); bestRoom = freeSlotsToLesson.getRoomsFromSlot(slot)[0]; lesson.setRoom(bestRoom); lesson.addLessonToTimetableAndBlockSlot(slot.day, slot.hour); result = true; break; } } int i = 0;; while (!result && slots.Count > i) { TimeSlot slot = slots[i]; Lesson lessonToChange = groupTT.getLessons(slot.day, slot.hour)[0]; if (level < 5) { result = removeLessonsAndFindNewPosition3(lessonToChange, ref allLesson, 3); } } //Console.WriteLine("<<<<<<<<<<<<<<<<<<<<<<<<<<<removeLessonsAndFindNewPosition"); return(result); }
private void findAndSetLessonsInBestPosition(List <Lesson> choosenLesson, List <Lesson> allLesson) { List <FreeSlotsToLesson> freeSlotsToLesson = new List <FreeSlotsToLesson>(); Timetable currentTimeTable = new Timetable(); List <TimeSlot> freeSlots = new List <TimeSlot>(); List <TimeSlot> groupSlots = new List <TimeSlot>(); List <TimeSlot> teacherSlots = new List <TimeSlot>(); List <TimeSlot> theSameSlots = new List <TimeSlot>(); Group group; Teacher teacher; foreach (Lesson l in choosenLesson) { group = l.getGroup(); teacher = l.getTeacher(); // przygotowanie listy wolnych slotów dla odpowiednich sal List <FreeSlotsInRoomToLesson> freeSlotsInRoomToLesson = new List <FreeSlotsInRoomToLesson>(); foreach (Timetable tt in roomsTimetables) { if (tt.getRoom().getRoomType().getName() == l.getRoomType().getName() && tt.getRoom().getAmount() >= group.getAmount()) { freeSlotsInRoomToLesson.Add(new FreeSlotsInRoomToLesson(tt.getFreeSlotsToLesson(l), tt.getRoom())); } } groupSlots = group.getTimetable().getFreeSlotsToLesson(l); /* * if (group.getTimetable().getCountSubGroupLesson(l) > 0) { * groupSlots.Clear(); * groupSlots = group.getTimetable().getFreeSlotsWithSubGroupLessons(l); * } */ teacherSlots = teacher.getTimetable().getFreeSlotsToLesson(l); theSameSlots = getTheSameSlots(groupSlots, teacherSlots); freeSlotsToLesson.Add(new FreeSlotsToLesson(theSameSlots, l, freeSlotsInRoomToLesson)); // przygotowanie planu lekcji z wszystkimi wolnymi slotami dla wybranych lekcji //foreach(TimeSlot ts in freeSlotsToLesson.Last().getSlots()) foreach (TimeSlot ts in theSameSlots) { currentTimeTable.addLesson(l, ts.day, ts.hour); if (!freeSlots.Contains(ts)) { freeSlots.Add(ts); } } } // sortowanie lekcji pod wzgledem wolnych slotów freeSlotsToLesson.Sort(new Comparison <FreeSlotsToLesson>(BFSComparator)); foreach (FreeSlotsToLesson fstl in freeSlotsToLesson) { //Console.WriteLine(fstl.lesson.ToString()); if (fstl.slots.Count > 0) { List <int> indexOfSlotsWithMaxCount = new List <int>(); int max = 0; // znajdywanie maksymalnej wartości foreach (TimeSlot ts in fstl.slots) { int currentTiteTableSlotCount = currentTimeTable.getLessons(ts.day, ts.hour).Count; if (max > currentTiteTableSlotCount) { max = currentTiteTableSlotCount; } } // znajdywanie wszystki maksymalnych foreach (TimeSlot ts in fstl.slots) { int currentTiteTableSlotCount = currentTimeTable.getLessons(ts.day, ts.hour).Count; if (max == currentTiteTableSlotCount) { indexOfSlotsWithMaxCount.Add(fstl.slots.IndexOf(ts)); } } //////////////////////// Room bestRoom = null; TimeSlot bestSlot = getBestTimeSlot(fstl.slots, fstl.roomSlots, ref bestRoom, indexOfSlotsWithMaxCount); // znajdywanie najlepszego slotu for (int i = 0; i < fstl.lesson.getSize(); ++i) { if (fstl.lesson.getGroup().getTimetable().getLessons(bestSlot.day, bestSlot.hour + i).Count == 0) // czy oby na pewno jest wolny slot { fstl.lesson.setRoom(bestRoom); fstl.lesson.addLessonToTimetable(bestSlot.day, bestSlot.hour + i); //fstl.lesson.getGroup().addLesson(fstl.lesson, bestSlot.day, bestSlot.hour + i); //fstl.lesson.getTeacher().getTimetable().addLesson(fstl.lesson, bestSlot.day, bestSlot.hour + i); //fstl.lesson.getRoom().getTimetable().addLesson(fstl.lesson, bestSlot.day, bestSlot.hour + i); } else { allLesson.Add(fstl.lesson); //errorValue += 1000; //Console.WriteLine("ERROR!!!!!!!!!!!!!!!! "+ fstl.lesson.ToString()); ////////////////////////wystąpił???! } } // usuwanie wolnych slotów z listy ustawionych lekcji foreach (FreeSlotsToLesson tmp in freeSlotsToLesson) { for (int i = 0; i < fstl.lesson.getSize(); ++i) { TimeSlot tmpSlot = new TimeSlot(bestSlot.day, bestSlot.hour + i); if (tmp.slots.Contains(tmpSlot)) { tmp.slots.Remove(tmpSlot); } } } } else { //Console.WriteLine("REMOVE " + fstl.lesson); if (!removeLessonsAndFindNewPosition3(fstl.lesson, ref allLesson)) { errorValue += 1000; //Console.WriteLine(" nie znaleziono "); } } } }