public static Pop Sort(Pop p) { IList <Gen> list = new List <Gen>(); list = p.Kromozon; IEnumerable <Gen> sortedEnum = list.OrderBy(f => f.Score); IList <Gen> sortedList = sortedEnum.ToList(); p.Kromozon = sortedList; return(p); }
public static Pop Speed(Pop p) { foreach (Gen h in p.Kromozon) { double rand1 = Math.Round(RandomNumber(0, 1), 1); double rand2 = Math.Round(RandomNumber(0, 1), 1); if (h.Speed == null) { h.Speed = new double[h.Genes.Length]; for (int i = 0; i < h.Genes.Length; i++) { h.Speed[i] = 0; } } for (int i = 0; i < h.Genes.Length; i++) { double a = h.Pbest[i] - h.Genes[i]; double b = p.BestGen.Pbest[i] - h.Genes[i]; double son = h.Speed[i] + (Pop.C * rand1 * a) + (Pop.C * rand2 * b); h.Speed[i] = son; double sonuc = h.Genes[i] + son; if (sonuc >= Pop.max) // -3 > 5 { sonuc = Pop.max; } if (sonuc <= Pop.min) { sonuc = Pop.min; } sonuc = Math.Round(sonuc, Pop.penalty); h.Genes[i] = sonuc; } } return(p); }
private void btn_basla_Click(object sender, EventArgs e) { int pop = (int)num_pop.Value; double min = decimal.ToDouble(num_min.Value); double max = decimal.ToDouble(num_max.Value); double pen = decimal.ToDouble(num_pen.Value); double gen = decimal.ToDouble(num_gen.Value); double c = decimal.ToDouble(num_c.Value); double jen = decimal.ToDouble(num_jen.Value); bool kon_min = rad_min.Checked; while (chart_pso.Series.Count > 0) { chart_pso.Series.RemoveAt(0); } // Resetle Series series = this.chart_pso.Series.Add("Total"); series.ChartType = SeriesChartType.Spline; chart_pso.Series["Total"].BorderWidth = 2; chart_pso.Series["Total"].Color = Color.Red; Pop p = new Pop(pop); //Bilgileri al Pop.penalty = (int)pen; Pop.max = (int)max; Pop.min = (int)min; Pop.gensize = (int)gen; Pop.C = (int)c; //Bilgileri al p.Generate(); for (int i = 0; i < jen; i++) { p.ScoreAll(); if (i % (jen / 10) == 0) { series.Points.AddXY(i, Math.Round(p.BestGen.Pscore, 0)); } p = PSO.Speed(p); } p.ScoreAll(); listBox1.Items.Add("----------------------"); for (int i = 0; i < p.Kromozon.Count; i++) { listBox1.Items.Add("Krom_" + (i + 1) + " Score =" + p.Kromozon[i].Score); listBox1.Items.Add("Krom_" + (i + 1) + " PScore = " + p.Kromozon[i].Pscore); listBox1.Items.Add("----------------------"); } int j = 0; listBox1.Items.Add("----------------------"); listBox1.Items.Add("BestGen_Score = " + p.BestGen.Score); foreach (double g in p.BestGen.Genes) { j++; listBox1.Items.Add("BestGen_Gen " + j + "=" + g); } listBox1.Items.Add("----------------------"); listBox1.Items.Add("----------------------"); listBox1.Items.Add("BestGen_PScore = " + p.BestGen.Pscore); j = 0; foreach (double g in p.BestGen.Pbest) { j++; listBox1.Items.Add("BestGen_PGen " + j + "=" + g); } }