private void button1_Click(object sender, EventArgs e) { richTextBox1.Text = ""; desc.Clear(); alg = new PGA(); OptimizationModel optModel = new OptimizationModel(func); AlgorithmSettings settings = new AlgorithmSettings() { InitialLoadType = (InitialLoadType)comboBox1.SelectedItem, OptModel = optModel, InitialPointCount = (int)numericUpDown1.Value, SelectionType = (SelectionType)comboBox2.SelectedItem, EndCondition = (EndCondition)comboBox3.SelectedItem, MaxGenerationCount = (int)numericUpDown2.Value, SurvivedCount = (int)numericUpDown3.Value, MutationChance = (double)numericUpDown4.Value, CrossingGenNumber = (int)numericUpDown5.Value, Tolerance = (double)numericUpDown6.Value, MutationChanceAfterCrossing = (double)numericUpDown7.Value, MutationType = (MutationType)comboBox4.SelectedItem }; alg.Run(settings); DrawRezult(alg); WriteRezult(alg); }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { int progress = 0; optrez.Clear(); finalRez.Clear(); alg = new PGA(); OptimizationModel optModel = new OptimizationModel(func); EnumConverter InitialLoadTypeCollection = new EnumConverter(typeof(InitialLoadType)); EnumConverter EndConditionTypeCollecton = new EnumConverter(typeof(EndCondition)); EnumConverter MutationTypeCollecton = new EnumConverter(typeof(MutationType)); EnumConverter SelectionTypeCollection = new EnumConverter(typeof(SelectionType)); foreach (InitialLoadType il in InitialLoadTypeCollection.GetStandardValues()) { foreach (EndCondition ec in EndConditionTypeCollecton.GetStandardValues()) { foreach (MutationType mt in MutationTypeCollecton.GetStandardValues()) { foreach (SelectionType st in SelectionTypeCollection.GetStandardValues()) { double[] f1m = new double[(int)numericUpDown8.Value]; double[] f2m = new double[(int)numericUpDown8.Value]; double[] fm = new double[(int)numericUpDown8.Value]; double[] x1m = new double[(int)numericUpDown8.Value]; double[] x2m = new double[(int)numericUpDown8.Value]; for (int i = 0; i < (int)numericUpDown8.Value; i++) { AlgorithmSettings settings = new AlgorithmSettings() { InitialLoadType = il, OptModel = optModel, InitialPointCount = (int)numericUpDown1.Value, SelectionType = st, EndCondition = ec, MaxGenerationCount = (int)numericUpDown2.Value, SurvivedCount = (int)numericUpDown3.Value, MutationChance = (double)numericUpDown4.Value, CrossingGenNumber = (int)numericUpDown5.Value, Tolerance = (double)numericUpDown6.Value, MutationChanceAfterCrossing = (double)numericUpDown7.Value, MutationType = mt }; alg.Run(settings); double x1 = alg.Best.X1; double x2 = alg.Best.X2; double f1 = alg.Best.F; double f2 = alg.CallCount; double f = GetCriterion(f1, f2); f1m[i] = f1; f2m[i] = f2; fm[i] = f; x1m[i] = x1; x2m[i] = x2; optrez.Add(new OptRezult() { I = il, E = ec, S = st, M = mt, F1 = f1, F2 = f2, X1 = x1, X2 = x2, F = f }); } progress++; backgroundWorker1.ReportProgress(progress * 100 / 24); finalRez.Add(new OptRezult() { I = il, E = ec, S = st, M = mt, X1 = x1m.Average(), X2 = x2m.Average(), F1 = f1m.Average(), F2 = f2m.Average(), F = fm.Average() }); } } } } }
private void WriteRezult(PGA alg) { int i = 0; int[] clr = new int[100]; foreach (var item in alg.Generations) { WriteLine(string.Format("Покаление {0}", item.Id)); WriteLine("На входе"); if (item.InitialChromosomes != null) foreach (var ch in item.InitialChromosomes) { WriteChomosome(ch); } WriteLine("Первая мутация"); if (item.FirstMutation != null) foreach (var ch in item.FirstMutation) { WriteChomosome(ch.Key, "Родитель"); WriteChomosome(ch.Value, "Потомок"); WriteLine(); } WriteLine("Перед скрещиванием"); if (item.AfterFirstMutation != null) foreach (var ch in item.AfterFirstMutation) { WriteChomosome(ch); } WriteLine("Пары1"); if (item.Pairs1 != null) foreach (var ch in item.Pairs1) { WriteChomosome(ch.Chr1); WriteChomosome(ch.Chr2); WriteLine(); } WriteLine("Победители в парах1"); if (item.Winners1 != null) foreach (var ch in item.Winners1) { WriteChomosome(ch); } WriteLine("Пары2"); if (item.Pairs2 != null) foreach (var ch in item.Pairs2) { WriteChomosome(ch.Chr1); WriteChomosome(ch.Chr2); WriteLine(); } WriteLine("Победители в парах2"); if (item.Winners2 != null) foreach (var ch in item.Winners2) { WriteChomosome(ch); } WriteLine("Пары для генетических операций"); if (item.PairsForGeneticOperation != null) foreach (var ch in item.PairsForGeneticOperation) { WriteChomosome(ch.Chr1); WriteChomosome(ch.Chr2); WriteLine(); } WriteLine("Результат генетических операций"); if (item.GeneticOperationRezults != null) foreach (var ch in item.GeneticOperationRezults) { WriteChomosome(ch); } WriteLine("Все хромосомы покаления"); if (item.AllFinishChromosome != null) foreach (var ch in item.AllFinishChromosome) { WriteChomosome(ch); } WriteLine("Лучшие"); if (item.Output != null) foreach (var ch in item.Output) { WriteChomosome(ch); } WriteLine("Лучшая"); WriteChomosome(item.BestChromosome); i++; clr[i] = richTextBox1.Text.Length; } WriteLine(string.Format("Количество вычислений целевой ф-и {0}", alg.CallCount)); WriteLine("Лучшая хромосома"); WriteChomosome(alg.Best); for (int j = 0; j < i; j++) { richTextBox1.Select(clr[j], clr[j + 1] - clr[j]); richTextBox1.SelectionColor = pens[j].Color; } }
private void DrawRezult(PGA alg) { numericUpDown8.Maximum = alg.Generations.Count - 1; desc.Clear(); desc.DrawMesh(alg.InitialMesh, Pens.Red); for (int i = 0; i < alg.Generations.Count; i++) { if (alg.Generations[i].AllFinishChromosome != null) foreach (var item in alg.Generations[i].AllFinishChromosome) { SolidBrush b = new SolidBrush(pens[item.GId].Color); desc.AddCircle((float)item.X1, (float)item.X2, 3, pens[item.GId], true); desc.AddText(item.GId + "." + item.Id, (float)item.X1, (float)item.X2, SystemFonts.SmallCaptionFont, b); } } desc.Update(); }