示例#1
0
        private void button1_Click(object sender, EventArgs e)
        {
            //初始化
            int p           = int.Parse(this.textBox2.Text);
            int Generations = int.Parse(this.textBox4.Text);//迭代次数

            if (p <= Parameter.m)
            {
                Parameter.p = p;
                GA GAAlgo = new GA();
                System.Collections.ArrayList bestFitness = new System.Collections.ArrayList();
                GAAlgo.MutationRate = double.Parse(this.textBox5.Text);
                GAAlgo.CrossRate    = double.Parse(this.textBox6.Text);
                if (comboBox1.Text == (string)comboBox1.Items[1])
                {
                    GAAlgo.Selection = GA.SelectionType.Tournment;
                }
                if (comboBox2.Text == (string)comboBox2.Items[1])
                {
                    GAAlgo.Crosstype = 2;
                }
                //寻优
                GAAlgo.Initialize();//产生第一代群体
                progressBar1.Value = 0;
                GAChromosome GAChr = new GAChromosome();
                bestFitness.Clear();
                while (GAAlgo.GenerationNum < Generations)//产生下一代群体
                {
                    GAAlgo.CreateNextGeneration();
                    GAChr = GAAlgo.GetBestChromosome();
                    bestFitness.Add(GAChr.Fitness);
                }
                progressBar1.Value = 100;
                //记录结果
                Parameter.x    = GAChr.ToArray();
                Parameter.opti = 100 / GAChr.Fitness;
                //显示结果
                label10.Text = (100 / GAChr.Fitness).ToString();
                //图像分析
                GAChartForm.chartControl1.Series.Clear();
                if (checkBox1.Checked)
                {
                    ChartSeries fitnessSeries = new ChartSeries("bestFitness");
                    fitnessSeries.SeriesIndexedModelImpl = new StringIndexedModel(fitnessSeries, (double[])bestFitness.ToArray(typeof(double)));
                    GAChartForm.chartControl1.Series.Add(fitnessSeries);
                }
                if (checkBox2.Checked)
                {
                    ChartSeries total_fitnessSeries = new ChartSeries("totalFitness");
                    total_fitnessSeries.SeriesIndexedModelImpl = new StringIndexedModel(total_fitnessSeries, (double[])GAAlgo.TotalFitness.ToArray(typeof(double)));
                    GAChartForm.chartControl1.Series.Add(total_fitnessSeries);
                }
                for (int i = 0; i < GAChartForm.chartControl1.Series.Count; i++)
                {
                    GAChartForm.chartControl1.Series[i].Type = ChartSeriesType.Line;
                }
            }
        }
示例#2
0
        private void btn_Run_Click(object sender, System.EventArgs e)
        {
            try
            {
                if (Points.Count < 2)
                {
                    MessageBox.Show("No Enough cities");
                    return;
                }
                this.m_chro = null;
                Refresh();
                //新加的部分
                points.Clear();
                temps.Clear();
                DrawPoints();
                GALib.Initializer newItializer = new GALib.Initializer(this.Initializer);
                GALib.Mutate      mutater      = new GALib.Mutate(this.ChromoseCompraror);
                GALib.Fitness     fitmethod    = new GALib.Fitness(this.Fitness);
                GALib.CrossOver   CrossMethod  = new GALib.CrossOver(this.CrossOver);

                GALib.GA GAAlgo = new GA(newItializer, fitmethod, mutater, CrossMethod);
                GAAlgo.Generations    = long.Parse(this.num_Gnr.Value.ToString());
                GAAlgo.PopulationSize = ushort.Parse(this.num_PopSiz.Value.ToString());
                GAAlgo.Mutation       = double.Parse(this.num_Mutation.Value.ToString());
                GAAlgo.CrossOver      = double.Parse(this.numCO.Value.ToString());

                if (Tool_MainBar.Buttons[1].Pushed)
                {
                    GAAlgo.EnableLogging = true;
                    GAAlgo.LogFilePath   = this.openFileDialog.FileName;
                }

                GAAlgo.Initialize();

                while (!GAAlgo.IsDone())
                {
                    GAAlgo.CreateNextGeneration();
                }

                m_chro = GAAlgo.GetBestChromosome();
                DrawCitiesPath();
            }
            catch (System.FormatException exp)
            {
                MessageBox.Show("Please check your Input Parameters " + exp);
            }
        }
示例#3
0
        private void btnSearch_Click(object sender, EventArgs e)
        {
            isInSearch = true;
            if (!isValidNumberOfCities())
            {
                MessageBox.Show("Số thành phố tối thiểu phải bằng 4  !", "Cảnh báo");
                return;
            }
            distanceMatrix = ComputeDistances(points);         // tính ma trận khoảng cách từ ma trận điểm đã cho

            Initializer khoitaomoi    = new Initializer(InitialGA.khoitaoNST);
            Mutate      dotbien       = new Mutate(InitialGA.Compraror_NST);
            Fitness     phepthichnghi = new Fitness(InitialGA.Thichnghi);
            CrossOver   pheplaighep   = new CrossOver(InitialGA.Laighep);
            //tao doi tuong moi
            GA GeneticSolver = new GA(khoitaomoi, phepthichnghi, dotbien, pheplaighep);

            GeneticSolver.Generations    = (long)numGenLimit.Value;
            GeneticSolver.PopulationSize = (ushort)numPopSize.Value;
            GeneticSolver.Mutation       = (double)numMutation.Value;
            GeneticSolver.CrossOver      = (double)numCrossOver.Value;

            // Khoi tao thuat toan
            GeneticSolver.Initialize();

            // kiem tra dieu kien dung
            // chua thoa man thuc hien tao the he moi
            while (!GeneticSolver.IsStop())
            {
                GeneticSolver.CreateNextGeneration();
            }

            // Lay ra phan tu co do thich nghi tot nhat trong quan the
            solutionChromo = GeneticSolver.GetBestChromosome();

            // Vẽ phương án lên bản đồ
            ptbMap.Refresh();
            DrawPath(solutionChromo);
            DrawPoints();
        }