GetFitness() public method

public GetFitness ( ) : float
return float
Esempio n. 1
0
        // 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
            }
        }
        public void SetSchedule(Schedule schedule, bool showGraphical)
        {
            _schedule = schedule.MakeCopy(false);
            if (Monitor.TryEnter(Locker, 500))
            {
                //_resultWindow.Controls["lblFitness"].Text = schedule.GetFitness().ToString();
                SetText("Fitness: " + schedule.GetFitness().ToString());
                Monitor.Exit(Locker);
            }
            else return;
            if (showGraphical)
            {
                //
                // ReSet to New DataGridView
                //
                foreach (KeyValuePair<int, DataGridView> it in dgvList)
                {
                    ClearDataGridView(it.Value);
                }
                //
                int numberOfRooms = Configuration.GetInstance.GetNumberOfRooms();
                int daySize = schedule.day_Hours * numberOfRooms;
                Random rand = new Random();
                foreach (KeyValuePair<CourseClass, int> it in schedule.GetClasses().ToList())
                {
                    // 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 / schedule.day_Hours;
                    time = time % schedule.day_Hours;  // this is a time now!

                    int dur = it.Key.GetDuration;

                    CourseClass cc = it.Key;
                    Room r = Configuration.GetInstance.GetRoomById(room);
                    string groups_Name = "";
                    foreach (var gs in cc.GetGroups)
                    {
                        groups_Name += gs.GetName + "\r\n";
                    }
                    groups_Name = groups_Name.Trim();

                    ((DataGridViewTextBoxCellEx)dgvList[r.GetId][day + 1, time]).RowSpan = cc.GetDuration;
                    dgvList[r.GetId][day + 1, time].Style.BackColor =
                                Color.FromArgb(rand.Next(70, 250), rand.Next(70, 250), rand.Next(70, 250));

                    dgvList[r.GetId][day + 1, time].Value = string.Format(CultureInfo.CurrentCulture,
                        "{0}\r\n{1}\r\n{2}\r\n{3}", cc.GetCourse.GetName, cc.GetProfessor.GetName, groups_Name, cc.Lab);

                        //(cc.GetCourse.GetName + Environment.NewLine +
                        // cc.GetProfessor.GetName + Environment.NewLine +
                        // groups_Name + Environment.NewLine + cc.Lab);
                }
            }
        }