Esempio n. 1
0
 public bool Equals(TimeTable obj)
 {
     if (group != null)
     {
         return(obj.group.Equals(this.group));
     }
     else if (teacher != null)
     {
         return(obj.teacher.Equals(this.teacher));
     }
     else
     {
         return(base.Equals(obj));
     }
 }
        //
        public SchoolTimeTable(List <Lesson> lessons, List <Teacher> teachers, List <Group> groups, SchoolTimeTable stt) : this(lessons, teachers, groups)
        {
            for (int i = 0; i < stt.groupTimeTables.Count; ++i)
            {
                TimeTable gtt = stt.groupTimeTables[i];

                for (int d = 0; d < ConstVariable.NUMBER_OF_DAYS; ++d)
                {
                    for (int h = 0; h < ConstVariable.NUMBER_OF_SLOTS_IN_DAY; ++h)
                    {
                        if (gtt.getLesson(d, h).Count > 0)
                        {
                            Lesson tmp   = gtt.getLesson(d, h)[0];
                            int    index = this.lessons.IndexOf(tmp);
                            Lesson tmpT  = this.lessons[index];
                            this.groupTimeTables[i].addLesson(tmpT, d, h);
                            this.teacherTimeTables[getIndexOfTeacher(tmpT.getTeacher())].addLesson(tmpT, d, h);
                        }
                    }
                }
            }
        }
        public List <TimeSlot> findFreeSlots(TimeTable groupTimeTable, TimeTable teacherTimeTable)
        {
            List <TimeSlot> slots = new List <TimeSlot>();

            int day  = 0;
            int hour = 0;

            foreach (Day d in groupTimeTable.getDays())
            {
                hour = 0;
                foreach (Slot h in d.getSlots())
                {
                    if (groupTimeTable.isEmpty(day, hour) && teacherTimeTable.isEmpty(day, hour))
                    {
                        slots.Add(new TimeSlot(day, hour));
                    }
                    hour++;
                }
                day++;
            }

            return(slots);
        }
 public void setTimeTable(TimeTable timeTable)
 {
     this.timeTable = timeTable;
 }
        public void crossSchoolTimeTables(SchoolTimeTable stt1, SchoolTimeTable stt2)
        {
            foreach (TimeTable tt1 in stt1.groupTimeTables)
            {
                TimeTable tt2 = null;

                foreach (TimeTable tt2_tmp in stt2.groupTimeTables)
                {
                    if (tt1.group.Equals(tt2_tmp.group))
                    {
                        tt2 = tt2_tmp;
                        break;
                    }
                }

                if (tt2 != null)
                {
                    //tt1.printTimeTable();
                    //tt2.printTimeTable();

                    List <TimeSlot> slots = getTheSameTimeSlotInTheSameLesson(tt1, tt2);

                    foreach (TimeSlot slot in slots)
                    {
                        Lesson lesson = tt1.getLesson(slot.day, slot.hour)[0];

                        if (lesson.getSize() == 1)
                        {
                            TimeTable teacherTimeTable = null;

                            foreach (TimeTable teacherTT in this.teacherTimeTables)
                            {
                                if (teacherTT.Equals(lesson.getTeacher()))
                                {
                                    teacherTimeTable = teacherTT;
                                    break;
                                }
                            }

                            TimeTable groupTimeTable = null;

                            foreach (TimeTable groupTT in this.groupTimeTables)
                            {
                                if (groupTT.Equals(lesson.getGroup()))
                                {
                                    groupTimeTable = groupTT;
                                    break;
                                }
                            }

                            teacherTimeTable.addLesson(lesson, slot.day, slot.hour);
                            groupTimeTable.addLesson(lesson, slot.day, slot.hour);
                        }
                    }
                }
            }

            lessons = getListOfLessonToInsert();
            this.generateSchoolTimeTable();

            //this.printTimeTable();
        }
        public void findAndSetBestPositionToLessons(List <Lesson> positionedLessons)
        {
            TimeTable       currentTimeTable = new TimeTable();
            List <TimeSlot> freeSlots        = new List <TimeSlot>();
            List <TimeSlot> groupSlots;
            List <TimeSlot> teacherSlots;
            //List<TimeSlot> roomSlots;
            List <TimeSlot>          theSameSlots;
            Group                    group;
            List <FreeSlotsToLesson> freeSlotsToLesson = new List <FreeSlotsToLesson>();

            if (positionedLessons.Count > 0)                                                                        //
            {
                group = positionedLessons[0].getGroup();                                                            //grupa jest taka sama w kazdjej lekcji

                foreach (Lesson lesson in positionedLessons)                                                        // idziemy po kazdej lekcji
                {
                    //>>>>>>>>>>>>>>>>>>>>>>>>
                    //Console.WriteLine("============PORÓWNANIE=============");
                    //group.timeTable.printTimeTable();
                    //lesson.getTeacher().getTimeTable().printTimeTable();
                    //<<<<<<<<<<<<<<<<<<<<<<<<

                    List <FreeSlotsInRoomToLesson> freeSlotsInRoomToLesson = new List <FreeSlotsInRoomToLesson>();
                    foreach (TimeTable tt in roomTimeTables)
                    {
                        //Console.WriteLine(tt.room.ToString());
                        if (tt.room.type.Equals(lesson.getSubject().getRoomType()) && tt.room.amount >= group.amount)
                        {
                            freeSlotsInRoomToLesson.Add(new FreeSlotsInRoomToLesson(tt.getFreeTimeSlot(), tt.room));
                            //>>>>>>>>>>>>>>>>>>>
                            //tt.printTimeTable();
                            //<<<<<<<<<<<<<<<<<<<
                        }
                    }
                    //>>>>>>>>>>>>>>>>>>>>>>>>
                    //Console.WriteLine("====================================");
                    //<<<<<<<<<<<<<<<<<<<<<<<<

                    groupSlots   = group.getTimeTable().getFreeTimeSlot(lesson.getSize());                         //pobiera liste wolnych slotów
                    teacherSlots = lesson.getTeacher().getTimeTable().getFreeTimeSlot(lesson.getSize());           //pobiera liste wolnych slotów
                    theSameSlots = getTheSameSlots(groupSlots, teacherSlots);                                      //generuje liste wolnych slotów dla grupt i nauczyciela i sprawdza wolne sale

                    freeSlotsToLesson.Add(new FreeSlotsToLesson(theSameSlots, lesson, freeSlotsInRoomToLesson));   //generuje lekcje wraz z listą wolnych slotów

                    //dodanie list sal do

                    foreach (TimeSlot ts in theSameSlots)                                                           //
                    {
                        currentTimeTable.addLesson(lesson, ts.day, ts.hour);                                        //dodaje lekcje do globalnego planu w okreslonym slocie (cały plan z wszyskimi wolnymi slotami wszystkich lekcji)
                        if (!freeSlots.Contains(ts))                                                                //dodaje wolne sloty o ile jescze nie ma ich na liście (do szybszego wyszukiwania)
                        {
                            freeSlots.Add(ts);                                                                      //
                        }
                        //>>>>>>>>>>>>>>>>>>>>
                        //Console.WriteLine(ts.ToString());
                        //<<<<<<<<<<<<<<<<<<<<
                    }
                }

                freeSlotsToLesson.Sort(new Comparison <FreeSlotsToLesson>(BFSComparator));                           //sortuje wolne sloty pod wzgledem ilości lekcji w tym slocie
                //>>>>>>>>>>>>>>>>>>>>>
                //Console.WriteLine("----------wolne sloty dla lekcji----------");
                //foreach (FreeSlotsToLesson fstl in freeSlotsToLesson) { Console.WriteLine(fstl.ToString());}
                //Console.WriteLine("------------------------------------------");
                //<<<<<<<<<<<<<<<<<<<<<
                //-------------------------------------------------
                // posortowane lekcje

                foreach (FreeSlotsToLesson fstl in freeSlotsToLesson)
                {
                    if (fstl.slots.Count > 0)                                   //sprawdza czy oby na pewno jest w slocie jakaś lekcja
                    {
                        List <int> indexOfSlotsWithMaxCount = new List <int>(); //lista indeksów lekcji o największej ilości wolnych slotów
                        int        max = 0;
                        // find max
                        foreach (TimeSlot ts in fstl.slots)
                        {
                            int currentTiteTableSlotCount = currentTimeTable.getDays()[ts.day].getSlots()[ts.hour].getLessons().Count;
                            if (max < currentTiteTableSlotCount)
                            {
                                max = currentTiteTableSlotCount;
                            }
                        }
                        // wyszukanie wszystkich max
                        foreach (TimeSlot ts in fstl.slots)
                        {
                            int currentTiteTableSlotCount = currentTimeTable.getDays()[ts.day].getSlots()[ts.hour].getLessons().Count;
                            if (max == currentTiteTableSlotCount)
                            {
                                indexOfSlotsWithMaxCount.Add(fstl.slots.IndexOf(ts));
                            }
                        }

                        int      bestRoomId = 0;                                                           //najlepsza sala
                        TimeSlot bestSlot   = getBestTimeSlot(fstl.slots, fstl.roomSlots, ref bestRoomId); //najlepszy slot

                        for (int i = 0; i < fstl.lesson.getSize(); ++i)
                        {
                            if (fstl.lesson.getGroup().getTimeTable().getDays()[bestSlot.day].getSlots()[bestSlot.hour + i].isEmpty())
                            {
                                fstl.lesson.addTimeSlot(new TimeSlot(bestSlot.day, bestSlot.hour + i));
                                fstl.lesson.setRoom(roomTimeTables[bestRoomId].room);
                                fstl.lesson.getGroup().getTimeTable().addLesson(fstl.lesson, bestSlot.day, bestSlot.hour + i);
                                fstl.lesson.getTeacher().getTimeTable().addLesson(fstl.lesson, bestSlot.day, bestSlot.hour + i);
                                roomTimeTables[bestRoomId].addLesson(fstl.lesson, bestSlot.day, bestSlot.hour + i);

                                //>>>>>>>>>>>>>>>>>>>>>>>>
                                fstl.lesson.getGroup().getTimeTable().printTimeTable();
                                fstl.lesson.getTeacher().getTimeTable().printTimeTable();
                                roomTimeTables[bestRoomId].printTimeTable();
                                //<<<<<<<<<<<<<<<<<<<<<<<<
                            }
                            else
                            {
                                //Console.WriteLine("ERROR!!!!!!!!!!!!!!!!");
                            }
                        }
                        //usuwanie z listy ustawionych lekcji
                        foreach (FreeSlotsToLesson tmp in freeSlotsToLesson)
                        {
                            // Console.WriteLine("check " + tmpSlot.ToString());

                            for (int i = 0; i < fstl.lesson.getSize(); ++i)
                            {
                                TimeSlot tmpSlot = new TimeSlot(bestSlot.day, bestSlot.hour + i);

                                if (tmp.slots.Contains(tmpSlot))
                                {
                                    int      timeSlotIndexToRemove = tmp.slots.IndexOf(tmpSlot);
                                    TimeSlot tsToRemove            = tmp.slots[timeSlotIndexToRemove];
                                    tmp.slots.Remove(tsToRemove);
                                }
                            }
                        }
                    }
                    else
                    {
                        //if (!removeLessonsAndFindNewPosition(fstl.lesson))
                        //Console.WriteLine("ERROR! \t" + fstl.lesson.ToString() + "have not free slots");
                    }
                }
            }
        }
        public Boolean removeLessonsAndFindNewPosition(Lesson lesson)
        {
            bool result = true;

            TimeTable                groupTT           = lesson.getGroup().getTimeTable();            //plan zajęć dla grupy szukanej lekcji
            TimeTable                teacherTT         = lesson.getTeacher().getTimeTable();          //plan zajec dla nauczyciela szukanej lekcji
            List <TimeSlot>          groupSlots        = groupTT.getFreeTimeSlot(lesson.getSize());   //wolne sloty grupy
            List <TimeSlot>          teacherSlots      = teacherTT.getFreeTimeSlot(lesson.getSize()); //wolne sloty nauczyciela
            List <FreeSlotsToLesson> freeSlotsToLesson = new List <FreeSlotsToLesson>();
            List <TimeSlot>          slotsToChange     = new List <TimeSlot>();
            TimeSlot bestSlot     = null;
            Lesson   lessonToMove = null;

            for (int d = 0; d < ConstVariable.NUMBER_OF_DAYS; d++)
            {
                for (int h = 0; h < ConstVariable.NUMBER_OF_SLOTS_IN_DAY; h++)
                {
                    ////pobranie wolnych sal i grupy/nauczyciela
                    ////porównanie wolnych slotów nauczyciela, grupy i sal
                    ////dodanie do listy
                    if (true)
                    {
                        slotsToChange.Add(new TimeSlot(d, h));
                    }
                }
            }

            bestSlot = slotsToChange[rand.Next(slotsToChange.Count - 1)];                         //wylosowanie z listy najlepszego slotu

            //wstawienie zmienianej lekcji
            lessonToMove = groupTT.getLesson(bestSlot.day, bestSlot.hour)[0];
            ////

            //usuniecie zmienianej lekcji z poprzedniej pozycji
            groupTT.removeLesson(lessonToMove, bestSlot.day, bestSlot.hour);
            teacherTT.removeLesson(lessonToMove, bestSlot.day, bestSlot.hour);

            ////room

            //wstawienie w wybrany slot lekcji do wstawienia
            groupTT.addLesson(lesson, bestSlot.day, bestSlot.hour);
            teacherTT.addLesson(lesson, bestSlot.day, bestSlot.hour);
            ////room


            return(result);

            /*
             * Boolean result = true;
             * Boolean isGroupHaveMoreSlots = true;
             * List<FreeSlotsToLesson> freeSlotsToLesson = new List<FreeSlotsToLesson>();
             *
             * TimeTable groupTT = lesson.getGroup().getTimeTable();                               //plan zajęć dla grupy szukanej lekcji
             * TimeTable teacherTT = lesson.getTeacher().getTimeTable();                           //plan zajec dla nauczyciela szukanej lekcji
             * List<TimeSlot> groupSlots = groupTT.getFreeTimeSlot(lesson.getSize());              //wolne sloty grupy
             * List<TimeSlot> teacherSlots = teacherTT.getFreeTimeSlot(lesson.getSize());          //wolne sloty nauczyciela
             *
             * List<FreeSlotsInRoomToLesson> freeSlotsInRoomToLesson = new List<FreeSlotsInRoomToLesson>();    //lista wolnych slotów sal pasujących do lekcji i grupy
             * foreach (TimeTable tt in roomTimeTables)
             * {
             *  if (tt.room.type.Equals(lesson.getSubject().getRoomType()) && tt.room.amount >= lesson.getGroup().amount)
             *  {
             *      freeSlotsInRoomToLesson.Add(new FreeSlotsInRoomToLesson(tt.getFreeTimeSlot(), tt.room));
             *  }
             * }
             *
             * List<TimeSlot> theSameSlots = getTheSameSlots(groupSlots, teacherSlots);                                       //generuje liste wolnych slotów dla grupt i nauczyciela i sprawdza wolne sale
             * freeSlotsToLesson.Add(new FreeSlotsToLesson(theSameSlots, lesson, freeSlotsInRoomToLesson));   //generuje lekcje wraz z listą wolnych slotów
             *
             *
             * if (getTheSameSlots(groupSlots,teacherSlots).Count == 0) {
             *  TimeTable firstTT = groupTT;
             *  TimeTable secondTT = teacherTT;
             *  isGroupHaveMoreSlots = true;
             *
             *  if (groupSlots.Count > teacherSlots.Count) {
             *      firstTT = teacherTT;
             *      secondTT = groupTT;
             *      isGroupHaveMoreSlots = false;
             *  }
             *
             *  //first - plan który posiada mniej wolnych slotów
             *  //second - plan który posiada więcej wolnych slotow
             *  //
             *  //w "second" szukamy lekcji mozliwych dozamiany w slotach  planu "first"
             *
             *
             *  List<TimeSlot> firstSlots = firstTT.getFreeTimeSlot(lesson.getSize());
             *  List<TimeSlot> secondSlots = secondTT.getFreeTimeSlot(lesson.getSize());
             *   // do poprawienia
             *   // dopisac uwzglednianie wiekszych lekcji
             *   // zwalnianie slotow przy zamianie
             *   // nie zmieniac wiekszych slotow
             *
             *  while (firstSlots.Count > 0) {
             *      int slotsIndex = rand.Next(0, firstSlots.Count);
             *      TimeSlot timeSlotToClear = firstSlots[slotsIndex];
             *      Lesson checkedLesson = secondTT.getDays()[timeSlotToClear.day].getSlots()[timeSlotToClear.hour].getLesson(0);
             *
             *      List<TimeSlot> slotsToCompare;
             *      if (isGroupHaveMoreSlots) {
             *          slotsToCompare = checkedLesson.getGroup().getTimeTable().getFreeTimeSlot();
             *      } else {
             *          slotsToCompare = checkedLesson.getTeacher().getTimeTable().getFreeTimeSlot();
             *      }
             *
             *      List<TimeSlot> freeSlots = getTheSameSlots(slotsToCompare, secondSlots);
             *      if (freeSlots.Count > 0 ) {
             *          int newSlotIndex = rand.Next(0, freeSlots.Count);
             *
             *          // wstawaimy lekcje do przeniesienia
             *          checkedLesson.getGroup().getTimeTable().addLesson(checkedLesson, freeSlots[newSlotIndex].day, freeSlots[newSlotIndex].hour);
             *          checkedLesson.getTeacher().getTimeTable().addLesson(checkedLesson, freeSlots[newSlotIndex].day, freeSlots[newSlotIndex].hour);
             *          // zwalniamy wolny slot
             *          checkedLesson.getGroup().getTimeTable().removeLesson(checkedLesson, timeSlotToClear.day, timeSlotToClear.hour);
             *          checkedLesson.getTeacher().getTimeTable().removeLesson(checkedLesson, timeSlotToClear.day, timeSlotToClear.hour);
             *          // ustawiam nowa lekcje
             *          lesson.getGroup().getTimeTable().addLesson(lesson, freeSlots[newSlotIndex].day, freeSlots[newSlotIndex].hour);
             *          lesson.getTeacher().getTimeTable().addLesson(lesson, freeSlots[newSlotIndex].day, freeSlots[newSlotIndex].hour);
             *          //Console.WriteLine("find free slots");
             *          break;
             *      }
             *
             *
             *      firstSlots.RemoveAt(slotsIndex);
             *  }
             *  if (firstSlots.Count == 0)
             *  {
             *      //Console.WriteLine("NOT FOUND");
             *
             *      // szukanie wgłąb
             *      firstSlots = firstTT.getFreeTimeSlot();
             *      while (firstSlots.Count > 0)
             *      {
             *          int slotsIndex = rand.Next(0, firstSlots.Count);
             *          TimeSlot timeSlotToClear = firstSlots[slotsIndex];
             *          Lesson checkedLesson = secondTT.getDays()[timeSlotToClear.day].getSlots()[timeSlotToClear.hour].getLesson(0);
             *
             *          if (removeLessonsAndFindNewPosition(checkedLesson))
             *          {
             *              removeLessonsAndFindNewPosition(lesson);
             *              break;
             *          }
             *
             *          firstSlots.RemoveAt(slotsIndex);
             *      }
             *
             *  }
             *
             * }
             *
             * return result;
             *
             */
        }