示例#1
0
        // Initializes genetic algorithm
        public Algorithm(int numberOfChromosomes, int replaceByGeneration, int trackBest,
                         Schedule prototype, Schedule.ScheduleObserver observer)
        {
            NumberOfChromosomes = numberOfChromosomes;
            TrackBest           = trackBest;
            ReplaceByGeneration = replaceByGeneration;
            _currentBestSize    = 0;
            _prototype          = prototype;
            _observer           = observer;
            _currentGeneration  = 0;
            _state = AlgorithmState.AS_USER_STOPPED;

            // reserve space for population
            _chromosomes = new Schedule[NumberOfChromosomes];
            _bestFlags   = new bool[NumberOfChromosomes];

            // reserve space for best chromosome group
            _bestChromosomes = new int[TrackBest];

            // clear population
            for (int i = _chromosomes.Length - 1; i >= 0; --i)
            {
                _chromosomes[i] = null;
                _bestFlags[i]   = false;
            }
            _instance = this;

            #region Find number of Active CPU or CPU core's for this Programs
            long   Affinity_Dec = System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity.ToInt64();
            string Affinity_Bin = Convert.ToString(Affinity_Dec, 2); // toBase 2
            foreach (char anyOne in Affinity_Bin.ToCharArray())
            {
                if (anyOne == '1')
                {
                    numCore++;
                }
            }
            #endregion
        }
        // Initializes genetic algorithm
        public Algorithm(int numberOfChromosomes, int replaceByGeneration, int trackBest,
            Schedule prototype, Schedule.ScheduleObserver observer)
        {
            NumberOfChromosomes = numberOfChromosomes;
            TrackBest = trackBest;
            ReplaceByGeneration = replaceByGeneration;
            _currentBestSize = 0;
            _prototype = prototype;
            _observer = observer;
            _currentGeneration = 0;
            _state =  AlgorithmState.AS_USER_STOPPED;

            // reserve space for population
            _chromosomes = new Schedule[NumberOfChromosomes];
            _bestFlags = new bool[NumberOfChromosomes];

            // reserve space for best chromosome group
            _bestChromosomes = new int[TrackBest];

            // clear population
            for (int i = _chromosomes.Length - 1; i >= 0; --i)
            {
                _chromosomes[i] = null;
                _bestFlags[i] = false;
            }
            _instance = this;

            #region Find number of Active CPU or CPU core's for this Programs
            long Affinity_Dec = System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity.ToInt64();
            string Affinity_Bin = Convert.ToString(Affinity_Dec, 2); // toBase 2
            foreach (char anyOne in Affinity_Bin.ToCharArray())
                if (anyOne == '1') numCore++;
            #endregion
            }
        private void ResultForm_Load(object sender, EventArgs e)
        {
            Configuration.GetInstance.ParseFile(new LINQDataContext());
            btnPause.Enabled = false;
            btnStop.Enabled = false;

            if (Configuration.GetInstance.GetNumberOfRooms() > 0)
            {
                create_GridView = new CreateDataGridViews(Configuration.GetInstance.Rooms, this);
                Schedule prototype = new Schedule(5, 5, 90, 10);
                Schedule.ScheduleObserver sso = new Schedule.ScheduleObserver();
                sso.SetWindow(create_GridView);

                AA = new MakeClassSchedule.Algorithm.Algorithm(1000, 180, 50, prototype, sso);

                state = ThreadState.Unstarted;
                timerWorkingSet.Start();
            }
            else
            {
                MessageBox.Show("Number of rooms is less than the limit!", "Number of Rooms Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                timerWorkingSet.Stop();
                AA = null;
                Dispose();
                return;
            }

            if (Configuration.GetInstance.GetNumberOfCourseClasses() <= 0)
            {
                btnStart.Enabled = false;
            }
        }