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

            //// make new chromosome, copy chromosome setup
            Schedule newChromosome = new Schedule(this, true);
            // place classes at random position

            List<CourseClass> c = Configuration.Instance.CourseClasses;
            foreach (var courseClass in c)
            {
                // determine random position of class
                int nr = Configuration.Instance.GetNumberOfRooms();
                int duration = courseClass.LessonDuration;
                int day = random.Next(0, Consts.DayCount);
                int room = random.Next(0, nr);
                int time = random.Next(0, Consts.DayHours + 1 - duration);
                int pos = day * nr * Consts.DayHours + room * Consts.DayHours + time;
                // fill time-space slots, for each hour of class
                for (int i = duration - 1; i >= 0; i--)
                    newChromosome.Slots[pos + i].Add(courseClass);
                // insert in class table of chromosome
                newChromosome.Classes.Add(courseClass, pos);

            }
            newChromosome.CalculateFitness();
            return newChromosome;
        }
示例#2
0
        // Performes crossover operation using to chromosomes and returns pointer to offspring
        public Schedule Crossover(Schedule parent2)
        {
            //        // check probability of crossover operation
            if (random.Next(100) > CrossoverProbability)
                // no crossover, just copy first parent
                return new Schedule(this, false);

            // new chromosome object, copy chromosome setup
            Schedule n = new Schedule(this, true);

            //// number of classes
            int size = Classes.Count;

            List<bool> cp = new List<bool>(size);
            for (int i = 0; i < size; ++i)
                cp.Add(false);

            // determine crossover point (randomly)
            for (int i = NumberOfCrossoverPoints; i > 0; i--)
            {
                while (true)
                {
                    int p = random.Next(size);
                    if (!cp[p])
                    {
                        cp[p] = true;
                        break;
                    }
                }
            }
            // make new code by combining parent codes
            bool first = random.Next(1000) % 2 == 0;

            for (int i = 0, k = 0, j = 0; i < size; i++, j++, k++)
            {
                if (first)
                {
                    // insert class from first parent into new chromosome's calss table
                    var classValue = Classes.ElementAt(k);
                    n.Classes.Add(classValue.Key, classValue.Value);
                    // all time-space slots of class are copied
                    for (int x = classValue.Key.LessonDuration - 1; x >= 0; x--)
                    {
                        n.Slots[classValue.Value + x].Add(classValue.Key);
                    }

                }
                else
                {
                    // insert class from second parent into new chromosome's calss table
                    var classValue = parent2.Classes.ElementAt(j);
                    n.Classes.Add(classValue.Key, classValue.Value);
                    // all time-space slots of class are copied
                    for (int x = classValue.Key.LessonDuration - 1; x >= 0; x--)
                    {
                        n.Slots[classValue.Value + x].Add(classValue.Key);
                    }
                }
                //crossover point
                if (cp[i])
                    first = !first;
            }
            n.CalculateFitness();
            return n;
        }