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

Crossover() public method

public Crossover ( Schedule parent2 ) : Schedule
parent2 Schedule
return Schedule
        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;
        }

Usage Example

        // new Thread for Start GA
        private void GA_Start(object Parallel_Mutex_On)
        {
            if ((Boolean)Parallel_Mutex_On) // Parallel Process Requirement's
            {
                #region GA for Mutex On

                while (true) //------------------------------------------------------------------------
                {
                    // user has stopped execution?
                    if (_state == AlgorithmState.AS_CRITERIA_STOPPED || _state == AlgorithmState.AS_USER_STOPPED)
                    {
                        break;
                    }
                    else if (_state == AlgorithmState.AS_SUSPENDED)
                    {
                        if (Thread.CurrentThread.IsAlive)
                        {
                            Thread.CurrentThread.Suspend();
                        }
                    }

                    // Save a Elite Chromosome for protection in Mutation and etc.
                    Schedule best = GetBestChromosome();

                    // algorithm has reached criteria?
                    if (best.GetFitness() >= 1)
                    {
                        _state = AlgorithmState.AS_CRITERIA_STOPPED;
                        break;
                    }


                    // produce offspring
                    Schedule[] offspring;
                    offspring = new Schedule[_replaceByGeneration];
                    Random rand = new Random();
                    for (int j = 0; j < _replaceByGeneration; j++)
                    {
                        Schedule p1;
                        Schedule p2;
                        // selects parent randomly
                        lock (Locker1)
                        {
                            p1 = _chromosomes[(rand.Next() % _chromosomes.Length)].MakeCopy(false);
                        }
                        lock (Locker1)
                        {
                            p2 = _chromosomes[(rand.Next() % _chromosomes.Length)].MakeCopy(false);
                        }

                        offspring[j] = p1.Crossover(p2);
                        lock (Locker1)
                        {
                            offspring[j].Mutation();
                            offspring[j].CalculateFitness();
                        }
                    }

                    // replace chromosomes of current operation with offspring
                    for (int j = 0; j < _replaceByGeneration; j++)
                    {
                        int ci;
                        do
                        {
                            // select chromosome for replacement randomly
                            ci = rand.Next() % _chromosomes.Length;

                            // protect best chromosomes from replacement
                        } while (IsInBest(ci));

                        lock (Locker1)
                        {
                            // replace chromosomes
                            _chromosomes[ci] = null;
                            _chromosomes[ci] = offspring[j];
                        }
                        // try to add new chromosomes in best chromosome group
                        AddToBest_Parallel(ci);
                    }

                    // algorithm has found new best chromosome
                    if (best != GetBestChromosome())
                    // notify observer
                    {
                        lock (Locker1)
                        {
                            _observer.NewBestChromosome(GetBestChromosome(), ResultControls.ResultForm._setting.Display_RealTime);
                        }
                    }
                    _currentGeneration++;
                }

                // The GA job's is Complete!
                if (_observer != null)
                {
                    lock (Locker0)
                    {
                        // notify observer that execution of algorithm has changed it state
                        _observer.EvolutionStateChanged(_state);
                    }
                }
                Thread.CurrentThread.Abort();
                #endregion
            }
            else
            {
                #region GA for Mutex Off

                while (true) //------------------------------------------------------------------------
                {
                    // user has stopped execution?
                    if (_state == AlgorithmState.AS_CRITERIA_STOPPED || _state == AlgorithmState.AS_USER_STOPPED)
                    {
                        break;
                    }
                    else if (_state == AlgorithmState.AS_SUSPENDED)
                    {
                        if (Thread.CurrentThread.IsAlive)
                        {
                            Thread.CurrentThread.Suspend();
                        }
                    }


                    // Save a Elite Chromosome for protection in Mutation and etc.
                    Schedule best = GetBestChromosome();

                    // algorithm has reached criteria?
                    if (best.GetFitness() >= 1)
                    {
                        _state = AlgorithmState.AS_CRITERIA_STOPPED;
                        break;
                    }

                    // produce offspring
                    Schedule[] offspring;
                    offspring = new Schedule[_replaceByGeneration];
                    Random rand = new Random();
                    for (int j = 0; j < _replaceByGeneration; j++)
                    {
                        // selects parent randomly
                        Schedule p1 = _chromosomes[(rand.Next() % _chromosomes.Length)];
                        Schedule p2 = _chromosomes[(rand.Next() % _chromosomes.Length)];

                        offspring[j] = p1.Crossover(p2);
                        offspring[j].Mutation();
                        offspring[j].CalculateFitness();
                    }

                    // replace chromosomes of current operation with offspring
                    for (int j = 0; j < _replaceByGeneration; j++)
                    {
                        int ci;
                        do
                        {
                            // select chromosome for replacement randomly
                            ci = rand.Next() % _chromosomes.Length;

                            // protect best chromosomes from replacement
                        } while (IsInBest(ci));

                        // replace chromosomes
                        _chromosomes[ci] = null;
                        _chromosomes[ci] = offspring[j];

                        // try to add new chromosomes in best chromosome group
                        AddToBest_Sequence(ci);
                    }

                    // algorithm has found new best chromosome
                    if (best != GetBestChromosome())
                    // notify observer
                    {
                        _observer.NewBestChromosome(GetBestChromosome(), ResultControls.ResultForm._setting.Display_RealTime);
                    }
                    _currentGeneration++;
                }

                // The GA job's is Complete!
                if (_observer != null)
                {
                    // notify observer that execution of algorithm has changed it state
                    _observer.EvolutionStateChanged(_state);
                }

                Thread.CurrentThread.Abort();
                #endregion
            }
        }