private void ConductGeneticAlgorithm()
        {
            if (isConductingGA)
                return;

            isConductingGA = true;

            int[,] xVal = new int[4, DataStorage.initialPopCount];
            if (generationCtr == 0)
            {
                for (int i = 0; i < DataStorage.initialPopCount; i++)
                    xVal[0, i] = xVal[1, i] = xVal[2, i] = xVal[3, i] = rnd.Next(DataStorage.lowerBound, DataStorage.UpperBound);
            }
            else
                xVal = mutatedChromosomes;

            // Performing selection
            int[,] selectedXVal = Selection.DoSelection(xVal, ref mainSelectionTable, ref additionalSelectionTable);

            // Performing crossover
            int[,] crossOveredValue = Crossover.DoCrossover(selectedXVal, ref crossOverTable);

            // Performing mutation
            int[] maxima = new int[4];
            mutatedChromosomes = Mutation.DoMutation(crossOveredValue, ref mutationTable, ref maxima);

            for (int gen = 0; gen < 4; gen++)
                maximaSaved[generationCtr, gen] = maxima[gen];

            GenerationTB.Text = (generationCtr + 1).ToString();
            MaximaTB.Text = maxima[genIndex].ToString();

            for (int gen = 0; gen < 4; gen++)
                GenVsMaximaChart.Series[DataStorage.selectionSchemes[gen]].Points.AddXY((generationCtr + 1), DataStorage.UpperBound - maximaSaved[generationCtr, gen]);

            generationCtr++;
            isConductingGA = false;

            if (generationCtr == DataStorage.generationCount)
            {
                Form analysis = new Analysis();
                analysis.Show();

            }
        }
        private void TableAndGraph_Load(object sender, EventArgs e)
        {
            UpdateGenIndex(DataStorage.selectionType);

            // SelectionSchemeChoose combobox
            SelectionSchemeChooseCB.Items.Add("Roulette wheel selection");
            SelectionSchemeChooseCB.Items.Add("Random selection");
            SelectionSchemeChooseCB.Items.Add("Rank selection");
            SelectionSchemeChooseCB.Items.Add("Tournament selection");

            ////// Selection tables
            SelectionDGV.DataSource           = Selection.InitializeTable();
            SelectionDGV.Columns[0].Width     = 50;
            SelectionAdditionalDGV.DataSource = Selection.InitializeAdditionalTable();
            SelectionTypeLB.Text     = DataStorage.selectionType;
            mainSelectionTable       = SelectionDGV.DataSource as DataTable;
            additionalSelectionTable = SelectionAdditionalDGV.DataSource as DataTable;
            toolTip1.SetToolTip(SelectionTypeLB, DataStorage.selectionToolTip[genIndex]);


            // Crossover tables
            CrossoverDGV.DataSource       = Crossover.InitializeTable();
            CrossoverDGV.Columns[0].Width = 50;
            CrossoverTypeLB.Text          = DataStorage.crossoverType;
            CrosoverRateLB.Text           = "Crossover rate: " + DataStorage.crossoverRate.ToString() + "%";
            crossOverTable = CrossoverDGV.DataSource as DataTable;
            toolTip1.SetToolTip(CrossoverTypeLB, DataStorage.GetCrossoverToolTip());

            // Mutation tables
            MutationDGV.DataSource       = Mutation.InitializeTable();
            MutationDGV.Columns[0].Width = 50;
            MutationTypeLB.Text          = DataStorage.mutationType;
            MutationRateLB.Text          = "Mutation rate: " + DataStorage.mutationRate.ToString() + "%";
            mutationTable = MutationDGV.DataSource as DataTable;
            toolTip1.SetToolTip(MutationTypeLB, DataStorage.GetMutationToolTip());

            ShowFitnessFunLB.Text        = DataStorage.fitnessFunction;
            SelectionSchemeChooseCB.Text = DataStorage.selectionType;

            maximaSaved = new int[DataStorage.generationCount, 4];

            ConductGeneticAlgorithm();
        }
Ejemplo n.º 3
0
        private void TSPTableAndGraph_Load(object sender, EventArgs e)
        {
            UpdateGenIndex(DataStorage.selectionType);

            // SelectionSchemeChoose combobox
            this.SelectionSchemeChooseCB.Items.Add("Roulette wheel selection");
            this.SelectionSchemeChooseCB.Items.Add("Random selection");
            this.SelectionSchemeChooseCB.Items.Add("Rank selection");
            this.SelectionSchemeChooseCB.Items.Add("Tournament selection");

            // Selection tables
            SelectDGV.DataSource           = Selection.InitializeTable();
            AdditionalSelectDGV.DataSource = Selection.InitializeAdditionalTable();
            this.SelectionTypeLB.Text      = DataStorage.selectionType;
            mainSelectionTable             = SelectDGV.DataSource as DataTable;
            additionalSelectionTable       = AdditionalSelectDGV.DataSource as DataTable;
            toolTip1.SetToolTip(SelectionTypeLB, DataStorage.selectionToolTip[genIndex]);

            // Crossover tables
            CrossDGV.DataSource       = Crossover.InitializeTable();
            this.CrossoverTypeLB.Text = DataStorage.crossoverType;
            this.CrosoverRateLB.Text  = "Crossover rate: " + DataStorage.crossoverRate.ToString() + "%";
            crossOverTable            = CrossDGV.DataSource as DataTable;
            toolTip1.SetToolTip(CrossoverTypeLB, DataStorage.GetCrossoverToolTip());

            // Mutation tables
            MutDGV.DataSource        = Mutation.InitializeTable();
            this.MutationTypeLB.Text = DataStorage.mutationType;
            this.MutationRateLB.Text = "Mutation rate: " + DataStorage.mutationRate.ToString() + "%";
            mutationTable            = MutDGV.DataSource as DataTable;
            toolTip1.SetToolTip(MutationTypeLB, DataStorage.GetMutationToolTip());

            // Initializing the selection scheme choose combobox
            this.SelectionSchemeChooseCB.Text = DataStorage.selectionType;

            // Matrix containing the minimum distance from each generation for each selection scheme
            minimaSaved     = new int[DataStorage.generationCount, 4];
            minimaPathSaved = new string[DataStorage.generationCount, 4];

            // What are we waiting for, set start the games
            ConductGeneticAlgorithm();
        }