ScheduleWorks.Algorithm.Schedule.CalculateRating C# (CSharp) Method

CalculateRating() public method

Calculates the rating of the current schedule
public CalculateRating ( ) : Rating
return Rating
        public Rating CalculateRating()
        {
            Rating answer = new Rating();
            Dictionary<Teacher, int> dict = new Dictionary<Teacher, int>();
            bool started;
            int lastNull;

            #region Errors calculation

            #region Teachers errors

            for (int day = 0; day < this.mDays.Count; ++day)
            {
                for (int lesson = 0; lesson < this.mDays[day].Classes[0].Lessons.Count; ++lesson)
                {
                    dict.Clear();

                    for (int classN = 0; classN < this.mDays[day].Classes.Count; ++classN)
                    {
                        if (this.mDays[day].Classes[classN].Lessons[lesson] != null)
                        {
                            foreach (var item in this.mDays[day].Classes[classN].Lessons[lesson].Groups)
                            {
                                if (item.Teacher != null)
                                {
                                    if (dict.ContainsKey(item.Teacher))
                                    {
                                        dict[item.Teacher]++;
                                    }
                                    else
                                    {
                                        dict.Add(item.Teacher, 1);
                                    }
                                }
                            }
                        }
                    }

                    //Console.WriteLine("-- {0}", dict.Count);
                    foreach (var item in dict)
                    {
                        answer.Errors -= (item.Value - 1) * (item.Value - 1);
                    }
                }
            }
            #endregion

            #region Free hours

            for (int day = 0; day < this.mDays.Count; ++day)
            {
                for (int classN = 0; classN < this.mDays[day].Classes.Count; ++classN)
                {
                    started = false;
                    lastNull = 0;
                    if (mDays[day].Classes[classN].Lessons[0] == null) answer.Errors -= 1;
                    for (int lesson = 0; lesson < this.mDays[day].Classes[0].Lessons.Count; ++lesson)
                    {
                        if (this.mDays[day].Classes[classN].Lessons[lesson] != null)
                        {
                            started = true;
                            lastNull = 0;
                        }
                        else
                        {
                            if (started)
                            {
                                answer.Errors -= 5;
                                lastNull++;
                            }
                        }
                    }
                    answer.Errors += lastNull * 5;
                }
            }

            #endregion

            #region Classroom

            Dictionary<Classroom, int> classrooms = new Dictionary<Classroom, int>();

            for (int day = 0; day < this.mDays.Count; ++day)
            {
                for (int lesson = 0; lesson < this.mDays[day].Classes[0].Lessons.Count; ++lesson)
                {
                    classrooms.Clear();

                    for (int classN = 0; classN < this.mDays[day].Classes.Count; ++classN)
                    {
                        if (this.mDays[day].Classes[classN].Lessons[lesson] != null)
                        {
                            foreach (var item in this.mDays[day].Classes[classN].Lessons[lesson].Groups)
                            {
                                if (item.Room != null)
                                {
                                    if (classrooms.ContainsKey(item.Room))
                                    {
                                        classrooms[item.Room]++;
                                    }
                                    else
                                    {
                                        classrooms.Add(item.Room, 1);
                                    }
                                }
                            }
                        }
                    }

                    //Console.WriteLine("-- {0}", dict.Count);
                    foreach (var item in classrooms)
                    {
                        answer.Errors -= (item.Value - 1) * (item.Value - 1);
                    }
                }
            }

            #endregion
            #endregion

            #region Preferences calculation

            #region Difficulty pattern

            int CountLessons = 0;
            int MatchingPattern = 0;
            int CurrentDayLessonsCount;
            int CurrentDayLesson;

            foreach (var day in mDays)
            {
                foreach (var classN in day.Classes)
                {
                    CurrentDayLessonsCount = 0;
                    CurrentDayLesson = 0;
                    if (classN.Lessons[0] == null)
                    {
                        answer.Errors -= 1;
                    }
                    for (int i = 0; i < classN.Lessons.Count; i++)
                    {
                        if (classN.Lessons[i] != null)
                        {
                            CurrentDayLessonsCount++;
                        }
                    }
                    foreach (var lesson in classN.Lessons)
                    {
                        if (lesson != null)
                        {
                            //if (CurrentDayLessonsCount < 4) answer.Errors -= 1;
                            switch (CurrentDayLessonsCount)
                            {
                                case 4:
                                    if ((DifficultyPattern.FourHoursPerDay[CurrentDayLesson] & lesson.Subject.Difficulty) == lesson.Subject.Difficulty)
                                        MatchingPattern++;
                                    break;
                                case 5:
                                    if ((DifficultyPattern.FiveHoursPerDay[CurrentDayLesson] & lesson.Subject.Difficulty) == lesson.Subject.Difficulty)
                                        MatchingPattern++;
                                    break;
                                case 6:
                                    if ((DifficultyPattern.SixHoursPerDay[CurrentDayLesson] & lesson.Subject.Difficulty) == lesson.Subject.Difficulty)
                                        MatchingPattern++;
                                    break;
                                case 7:
                                    if ((DifficultyPattern.SevenHoursPerDay[CurrentDayLesson] & lesson.Subject.Difficulty) == lesson.Subject.Difficulty)
                                        MatchingPattern++;
                                    break;
                                default:
                                    break;
                            }
                            CountLessons++;
                            CurrentDayLesson++;
                        }
                    }
                }
            }

            if (CountLessons > 0)
                answer.DifficultyPatternMatching += (1 << 10) * MatchingPattern / CountLessons;

            #endregion

            #region Teachers pattern

            #endregion

            #endregion

            return answer;
        }