MakeClassSchedule.Algorithm.Schedule.CalculateFitness C# (CSharp) Method

CalculateFitness() public method

public CalculateFitness ( ) : void
return void
        public void CalculateFitness()
        {
            // chromosome's score
            int score = 0;

            int numberOfRooms = Configuration.GetInstance.GetNumberOfRooms();
            int daySize = DAY_HOURS * numberOfRooms;

            int ci = 0;

            // check criteria and calculate scores for each class in schedule
            foreach (KeyValuePair<CourseClass, int> it in _classes.ToList())
            {                                           //_classes.ToList<KeyValuePair<CourseClass, int>>())
                // coordinate of time-space slot
                int pos = it.Value; // int pos of _slot array
                int day = pos / daySize;
                int time = pos % daySize; // this is not time now!
                int room = time / DAY_HOURS;
                time = time % DAY_HOURS;  // this is a time now!

                int dur = it.Key.GetDuration;

                CourseClass cc = it.Key;
                Room r = Configuration.GetInstance.GetRoomById(room);

                #region Score 1 (check for room overlapping of classes)  [+3]

                // check for room overlapping of classes
                bool overlapping = false;
                for (int i = dur - 1; i >= 0; i--)
                {
                    if (_slots[pos + i].Count > 1)
                    {
                        overlapping = true;
                        break;
                    }
                }

                // on room overlapping
                if (!overlapping)
                    score += 3;

                _criteria[ci + 0] = !overlapping;

                #endregion

                #region Score 2 (does current room have enough seats)  [+2]
                // does current room have enough seats
                _criteria[ci + 1] = r.GetNumberOfSeats >= cc.GetNumberOfSeats;
                if (_criteria[ci + 1])
                    score += 2;
                #endregion

                #region Score 3 (does current room have computers if they are required)  [+12]
                // does current room have computers if they are required
                _criteria[ci + 2] = (cc.Lab == r.GetLab) ? true : false;
                if (_criteria[ci + 2])
                    score += 10;
                #endregion

                #region Score 4 and 5 (check overlapping of classes for professors and student groups)  [+4][+4]

                bool prof = false, gro = false;
                // check overlapping of classes for professors and student groups
                for (int i = numberOfRooms, t = (day * daySize + time); i > 0; i--, t += DAY_HOURS)
                {
                    // for each hour of class
                    for (int j = dur - 1; j >= 0; j--)
                    {
                        // check for overlapping with other classes at same time
                        List<CourseClass> cl = _slots[t + j];
                        foreach (CourseClass it_cc in cl)
                        {
                            if (cc != it_cc)
                            {
                                // professor overlaps?
                                if (!prof && cc.ProfessorOverlaps(it_cc))
                                    prof = true;

                                // student group overlaps?
                                if (!gro && cc.GroupsOverlap(it_cc))
                                    gro = true;

                                // both type of overlapping? no need to check more
                                if (prof && gro)
                                    goto total_overlap;
                            }
                        }
                    }
                }

            total_overlap:

                // professors have no overlapping classes?
                if (!prof)
                    score += 4;
                _criteria[ci + 3] = !prof;

                // student groups has no overlapping classes?
                if (!gro)
                    score += 4;
                _criteria[ci + 4] = !gro;

                #endregion

                #region Score 6 (check this class time by professor's free TimeTable)  [+8]
                _criteria[ci + 5] = true;
                for (int i = 0; i < dur; i++)
                {
                    if (!cc.GetProfessor.GetSchedule[time + i, day + 1])
                    {
                        _criteria[ci + 5] = false;
                        break;
                    }
                }
                if (_criteria[ci + 5])
                    score += 8;

                #endregion

                #region Score 7 (doesn't current class in 13-14 DayHours or it TimeSlot is 5)  [+1]
                // All Time-Slot is 8~19 in 12 Hours
                // Time-Slot 5 is in 13-14 Hour's
                _criteria[ci + 6] = true;
                for (int i = 0; i < dur; i++)
                {
                    if ((time + i) == 5)
                    {
                        _criteria[ci + 6] = false;
                        break;
                    }
                }
                if (_criteria[ci + 6]) score++;
                #endregion

                ci += numberOfScores;
            }

            // calculate fitness value based on score
            _fitness = (float)score / (Configuration.GetInstance.GetNumberOfCourseClasses() * numberOfScores);
        }

Usage Example

Beispiel #1
0
        // Makes new chromosome with same setup but with randomly chosen code
        public Schedule MakeNewFromPrototype()
        {
            // number of time-space slots
            int size = _slots.Length;

            // make new chromosome, copy chromosome setup
            Schedule newChromosome = new Schedule(this, true);

            // place classes at random position
            List <CourseClass> cc = Configuration.GetInstance.GetCourseClasses();

            foreach (CourseClass it in cc)
            {
                // determine random position of class
                int    num_rooms = Configuration.GetInstance.GetNumberOfRooms();
                int    dur       = it.GetDuration;
                Random rand      = new Random();
                int    day       = rand.Next() % DAYS_NUM;
                int    room      = rand.Next() % num_rooms;
                int    time      = rand.Next() % (DAY_HOURS + 1 - dur);
                int    pos       = (day * num_rooms * DAY_HOURS) + (room * DAY_HOURS + time); // (Base) + (offset) time's

                // fill time-space slots, for each hour of class
                for (int i = dur - 1; i >= 0; i--)
                {
                    newChromosome._slots[pos + i].Add(it);
                }

                // insert in class table of chromosome
                newChromosome._classes.Add(it, pos);
            }

            newChromosome.CalculateFitness();

            // return smart pointer
            return(newChromosome);
        }
All Usage Examples Of MakeClassSchedule.Algorithm.Schedule::CalculateFitness