CalculateFitness() public method

public CalculateFitness ( ) : void
return void
Example #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);
        }
Example #2
0
        // Performed crossover operation using to chromosomes
        // and returns pointer to offspring
        public Schedule Crossover(Schedule parent2)
        {
            Random rand = new Random();

            // check probability of crossover operation
            if (rand.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;

            bool[] cp = new bool[size];

            // determine crossover point (randomly)
            for (int i = _numberOfCrossoverPoints; i > 0; i--)
            {
                while (true)
                {
                    int p = rand.Next() % size;
                    if (!cp[p])
                    {
                        cp[p] = true;
                        break;
                    }
                }
            }

            //Dictionary<CourseClass, int> it1 = _classes;
            List <KeyValuePair <CourseClass, int> > it1 = _classes.ToList <KeyValuePair <CourseClass, int> >();

            //Dictionary<CourseClass, int> it2 = parent2._classes;
            List <KeyValuePair <CourseClass, int> > it2 = parent2._classes.ToList <KeyValuePair <CourseClass, int> >();

            // make new code by combining parent codes
            bool first = (rand.Next() % 2 == 0);

            //
            for (int i = 0; i < size; i++)
            {
                if (first)
                {
                    // insert class from first parent into new chromosome's class table
                    n._classes.Add(it1[i].Key, it1[i].Value);
                    // all time-space slots of class are copied
                    for (int j = it1[i].Key.GetDuration - 1; j >= 0; j--)
                    {
                        n._slots[it1[i].Value + j].Add(it1[i].Key);
                    }
                }
                else
                {
                    // insert class from second parent into new chromosome's class table
                    n._classes.Add(it2[i].Key, it2[i].Value);
                    // all time-space slots of class are copied
                    for (int j = it2[i].Key.GetDuration - 1; j >= 0; j--)
                    {
                        n._slots[it2[i].Value + j].Add(it2[i].Key);
                    }
                }

                // crossover point
                if (cp[i])
                {
                    // change source chromosome
                    first = !first;
                }
            }

            n.CalculateFitness();

            // return smart pointer to offspring
            return(n);
        }
        // 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;
        }
        // Performed crossover operation using to chromosomes
        // and returns pointer to offspring
        public Schedule Crossover(Schedule parent2)
        {
            Random rand = new Random();

            // check probability of crossover operation
            if (rand.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;

            bool[] cp = new bool[size];

            // determine crossover point (randomly)
            for (int i = _numberOfCrossoverPoints; i > 0; i--)
            {
                while (true)
                {
                    int p = rand.Next() % size;
                    if (!cp[p])
                    {
                        cp[p] = true;
                        break;
                    }
                }
            }

            //Dictionary<CourseClass, int> it1 = _classes;
            List<KeyValuePair<CourseClass, int>> it1 = _classes.ToList<KeyValuePair<CourseClass, int>>();

            //Dictionary<CourseClass, int> it2 = parent2._classes;
            List<KeyValuePair<CourseClass, int>> it2 = parent2._classes.ToList<KeyValuePair<CourseClass, int>>();

            // make new code by combining parent codes
            bool first = (rand.Next() % 2 == 0);
            //
            for (int i = 0; i < size; i++)
            {
                if (first)
                {
                    // insert class from first parent into new chromosome's class table
                    n._classes.Add(it1[i].Key, it1[i].Value);
                    // all time-space slots of class are copied
                    for (int j = it1[i].Key.GetDuration - 1; j >= 0; j--)
                        n._slots[it1[i].Value + j].Add(it1[i].Key);
                }
                else
                {
                    // insert class from second parent into new chromosome's class table
                    n._classes.Add(it2[i].Key, it2[i].Value);
                    // all time-space slots of class are copied
                    for (int j = it2[i].Key.GetDuration - 1; j >= 0; j--)
                        n._slots[it2[i].Value + j].Add(it2[i].Key);
                }

                // crossover point
                if (cp[i])
                    // change source chromosome
                    first = !first;
            }

            n.CalculateFitness();

            // return smart pointer to offspring
            return n;
        }