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

Mutation() public method

public Mutation ( ) : void
return void
        public void Mutation()
        {
            Random rand = new Random();

            // check probability of mutation operation
            if (rand.Next() % 100 > _mutationProbability)
                // will not do mutation
                return;

            // number of classes
            int numberOfClasses = _classes.Count;
            // number of time-space slots
            int size = _slots.Length;

            // move selected number of classes at random position
            for (int i = _mutationSize; i > 0; i--)
            {
                // select random chromosome for movement
                int mpos = rand.Next() % numberOfClasses;
                int pos1 = 0;
                KeyValuePair<CourseClass, int> it = _classes.ToList<KeyValuePair<CourseClass, int>>()[mpos];

                // current time-space slot used by class
                pos1 = it.Value;

                CourseClass cc1 = it.Key;

                // determine position of class randomly
                int nr = Configuration.GetInstance.GetNumberOfRooms();
                int dur = cc1.GetDuration;
                int day = rand.Next() % DAYS_NUM;
                int room = rand.Next() % nr;
                int time = rand.Next() % (DAY_HOURS + 1 - dur);
                int pos2 = day * nr * DAY_HOURS + room * DAY_HOURS + time;

                // move all time-space slots
                for (int j = dur - 1; j >= 0; j--)
                {
                    // remove class hour from current time-space slot
                    List<CourseClass> cl = _slots[pos1 + j];
                    foreach (CourseClass It in cl)
                    {
                        if (It == cc1)
                        {
                            cl.Remove(It);
                            break;
                        }
                    }

                    // move class hour to new time-space slot
                    _slots[pos2 + j].Add(cc1);
                }

                // change entry of class table to point to new time-space slots
                _classes[cc1] = pos2;
            }
        }