// 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
            }
 // Frees memory used by global instance
 public static void FreeInstance()
 {
     // free memory used by global instance if it exists
     if (_instance != null)
     {
         _instance._prototype = null;
         _instance._observer = null;
         _instance = null;
     }
 }
        // Returns reference to global instance of algorithm
        public static Algorithm GetInstance()
        {
            // global instance doesn't exist?
            if (_instance == null)
            {
                // make prototype of chromosomes
                Schedule prototype = new Schedule(5, 5, 90, 10);

                // make new global instance of algorithm using chromosome prototype
                _instance = new Algorithm(1000, 180, 50, prototype, new Schedule.ScheduleObserver());
            }

            return _instance;
        }
        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;
            }
        }