コード例 #1
0
 public void Reproduction()
 {
     chromoGens = Crossover.doCrossover(crossoverType, chromoGens, probabilityCrossover, random);
     //chromoGens = Mutation.doMutation(1, chromoGens, probabilityMutation, random);
     chromoValue   = Chromossome.getPopulationValues(chromoGens, rangeMin, rangeMax, granularity, random);
     chromoFitness = Chromossome.getPopulationFitness(functionType, optimizationType, chromoValue);
 }
コード例 #2
0
        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();
            }
        }
コード例 #3
0
        private void UpdateTableAndGraphValues()
        {
            SelectionTypeLB.Text = DataStorage.selectionSchemes[genIndex];
            MaximaTB.Text        = maximaSaved[generationCtr - 1, genIndex].ToString();

            Selection.UpdateTableValue(ref mainSelectionTable, ref additionalSelectionTable);
            Crossover.UpdateTableValue(ref crossOverTable);
            Mutation.UpdateTableValue(ref mutationTable);

            toolTip1.SetToolTip(SelectionTypeLB, DataStorage.selectionToolTip[genIndex]);
        }
コード例 #4
0
        private void ConductGeneticAlgorithm()
        {
            string[,] initialPath = new string[4, DataStorage.initialPopCount];
            if (generationCtr == 0)
            {
                for (int i = 0; i < DataStorage.initialPopCount; i++)
                {
                    initialPath[0, i] = initialPath[1, i] = initialPath[2, i] = initialPath[3, i] = Utility.GetRandomPath(DataStorage.cityCount);
                }
            }
            else
            {
                initialPath = mutatedPath;
            }

            // Performing selection
            string[,] selectedpath = Selection.DoSelection(initialPath, ref mainSelectionTable, ref additionalSelectionTable);

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

            // Performing mutation
            int[]    minima     = new int[4];
            string[] minimaPath = new string[4];
            mutatedPath = Mutation.DoMutation(crossOveredValue, ref mutationTable, ref minima, ref minimaPath);

            for (int gen = 0; gen < 4; gen++)
            {
                minimaSaved[generationCtr, gen]     = minima[gen];
                minimaPathSaved[generationCtr, gen] = minimaPath[gen];
            }

            GenerationTB.Text = (generationCtr + 1).ToString();
            MinimaTB.Text     = minima[genIndex].ToString();
            MinimaPathTB.Text = minimaPath[genIndex].ToString();

            for (int gen = 0; gen < 4; gen++)
            {
                DistanceChart.Series[DataStorage.selectionSchemes[gen]].Points.AddXY((generationCtr + 1), minimaSaved[generationCtr, gen]);
            }

            generationCtr++;

            if (generationCtr == DataStorage.generationCount)
            {
                Form analysis = new Analysis();
                analysis.Show();
            }
        }
コード例 #5
0
        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();
        }
コード例 #6
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();
        }
コード例 #7
0
 private void button2_Click(object sender, EventArgs e)
 {
     if (initflag == true)
     {
         chromoC = Crossover.TwoPoints(chromoP, 50, _random);
         richTextBox1.Clear();
         richTextBox2.Clear();
         for (int i = 0; i < chromoP.Count(); i++)
         {
             richTextBox1.AppendText(chromoP[i] + "\n");
             richTextBox2.AppendText(chromoC[i] + "\n");
         }
         chromoP = chromoC;
     }
     else
     {
         chromoP  = Chromossome.Creates(6, 0, 63, 0, _random);
         initflag = true;
     }
 }