private void Button_Click(object sender, RoutedEventArgs e) { var watch = new System.Diagnostics.Stopwatch(); watch.Start(); string[] lg = listOfInd.Text.Split('\n'); articles = new List <article>(); foreach (var item in lg) { articles.Add(new article() { weight = int.Parse(item.Split(',')[0]), value = int.Parse(item.Split(',')[1]) }); } var Pop = firstPopulation(); int nPar = int.Parse(numOfPar.Text); int nGen = int.Parse(numOfGen.Text); var maxChild = new indv(articles); for (int i = 0; i < nGen; i++) { var parent = rouletweelSelection(Pop, nPar); Pop = rouletweelSelection(parent, nPar); for (int j = 0; j < nPar / 2; j++) { if (Pop[j].fitness > maxChild.fitness) { maxChild = Pop[j]; } if (Pop[j + 1].fitness > maxChild.fitness) { maxChild = Pop[j + 1]; } var childs = xOver(Pop[j], Pop[j + 1]); Pop.Add(childs.Item1); if (childs.Item1.fitness > maxChild.fitness) { maxChild = childs.Item1; } Pop.Add(childs.Item2); if (childs.Item2.fitness > maxChild.fitness) { maxChild = childs.Item2; } } } watch.Stop(); MessageBox.Show(maxChild.fitness.ToString() + '\n' + $"Execution Time: {watch.ElapsedMilliseconds} ms"); }
indv SelfAdaptationMethodES() { var pop = new List <indv>(); int f = f1.IsChecked.HasValue ? 1 : (f2.IsChecked.HasValue ? 2 : 3); for (int i = 0; i < int.Parse(numPop.Text); i++) { pop.Add(new indv(int.Parse(nd.Text), f, rand, double.Parse(a.Text), double.Parse(b.Text), double.Parse(c.Text), double.Parse(initSig.Text))); } indv minF = pop[1]; var par = new List <indv>(); for (int i = 0; i < int.Parse(numGen.Text); i++) { par.Clear(); for (int j = 0; j < int.Parse(numPop.Text); j++) { par.Add(pop[rand.Next(0, pop.Count)]); } var childs = new List <indv>(); for (int k = 0; k < int.Parse(numChild.Text) / 2; k++) { var p1 = par[rand.Next(0, par.Count)]; var p2 = par[rand.Next(0, par.Count)]; if (rand.NextDouble() < 0.1) { var ch = simpleCrossover(p1, p2); p1 = ch.Item1; p2 = ch.Item2; } if (rand.NextDouble() <= 0.91) { var mch = mutation(p1); if (mch.fitness > p1.fitness) { p1 = mch; } } if (rand.NextDouble() <= 0.91) { var mch = mutation(p2); if (mch.fitness > p2.fitness) { p2 = mch; } } if (p1.fitness > minF.fitness) { minF = p1; } if (p2.fitness > minF.fitness) { minF = p2; } childs.Add(p1); childs.Add(p2); } if (rouletW.IsChecked.Value) { pop = rouletweelSelection(childs, int.Parse(numPop.Text)); } else if (sus.IsChecked.Value) { pop = SusSelection(childs, int.Parse(numPop.Text)); } else { pop = TournamentSelection(childs, int.Parse(numPop.Text)); } } return(minF); }