private void btn_run_Click(object sender, EventArgs e) { if (txtBx_target.Text.Trim().Length > 0) { target = txtBx_target.Text.Trim(); numberOfGenes = txtBx_target.Text.Trim().Length; maxGenerations = trkbar_maxGenerations.Value; populationSize = trkbar_populationSize.Value; crossoverRate = (float)trkBar_crossoverRate.Value / 100; mutationRate = (float)trkBar_mutationRate.Value / 100; numberOfMates = trkBar_numMates.Value; lstBx_output.Items.Clear(); btn_run.Enabled = false; btn_reset.Enabled = false; btn_about.Enabled = false; var timer = Stopwatch.StartNew(); // Initialize population List<Individual> population = new List<Individual>(); for (int i = 0; i < populationSize; i++) { population.Add(new Individual()); population[i].GenerateRandomGenes(numberOfGenes, ref generator); } // evolve generations int generation = 1; while (generation <= maxGenerations) { // evaluate fitness int elite = 0; for (int i = 0; i < populationSize; i++) { population[i].CalculateFitness(target); if (population[i].GetFitness() >= population[elite].GetFitness()) elite = i; } // display most fit individual string result = "Generation: " + generation + "\tBest Fit: " + population[elite].GetPhenotype(); lstBx_output.Items.Add(result); // evaluate fitness of most fit individual if (population[elite].GetPhenotype().Equals(target)) break; // evolve current generation List<Individual> newPopulation = new List<Individual>(); for (int i = 0; i < populationSize; i++) { Evolution selection = new Evolution(); int firstParent = selection.FindParent(numberOfMates, populationSize, ref generator, population); int secondParent = selection.FindParent(numberOfMates, populationSize, ref generator, population); if (secondParent == firstParent) secondParent = (secondParent + 1) % populationSize; Individual child = selection.BreedParents(numberOfGenes, crossoverRate, population[firstParent], population[firstParent]); child.Mutate(numberOfGenes, mutationRate, ref generator); newPopulation.Add(child); } // kill off current generation population.Clear(); population.AddRange(newPopulation); generation++; } timer.Stop(); var runTime = timer.ElapsedMilliseconds; lbl_time.Text = "Run time: " + runTime + "ms"; } else { MessageBox.Show("Please enter text in the target control", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtBx_target.Clear(); txtBx_target.Focus(); } btn_run.Enabled = true; btn_reset.Enabled = true; btn_about.Enabled = true; }
private void btn_run_Click(object sender, EventArgs e) { if (txtBx_target.Text.Trim().Length > 0) { target = txtBx_target.Text.Trim(); numberOfGenes = txtBx_target.Text.Trim().Length; maxGenerations = trkbar_maxGenerations.Value; populationSize = trkbar_populationSize.Value; crossoverRate = (float)trkBar_crossoverRate.Value / 100; mutationRate = (float)trkBar_mutationRate.Value / 100; numberOfMates = trkBar_numMates.Value; lstBx_output.Items.Clear(); btn_run.Enabled = false; btn_reset.Enabled = false; btn_about.Enabled = false; var timer = Stopwatch.StartNew(); // Initialize population List <Individual> population = new List <Individual>(); for (int i = 0; i < populationSize; i++) { population.Add(new Individual()); population[i].GenerateRandomGenes(numberOfGenes, ref generator); } // evolve generations int generation = 1; while (generation <= maxGenerations) { // evaluate fitness int elite = 0; for (int i = 0; i < populationSize; i++) { population[i].CalculateFitness(target); if (population[i].GetFitness() >= population[elite].GetFitness()) { elite = i; } } // display most fit individual string result = "Generation: " + generation + "\tBest Fit: " + population[elite].GetPhenotype(); lstBx_output.Items.Add(result); // evaluate fitness of most fit individual if (population[elite].GetPhenotype().Equals(target)) { break; } // evolve current generation List <Individual> newPopulation = new List <Individual>(); for (int i = 0; i < populationSize; i++) { Evolution selection = new Evolution(); int firstParent = selection.FindParent(numberOfMates, populationSize, ref generator, population); int secondParent = selection.FindParent(numberOfMates, populationSize, ref generator, population); if (secondParent == firstParent) { secondParent = (secondParent + 1) % populationSize; } Individual child = selection.BreedParents(numberOfGenes, crossoverRate, population[firstParent], population[firstParent]); child.Mutate(numberOfGenes, mutationRate, ref generator); newPopulation.Add(child); } // kill off current generation population.Clear(); population.AddRange(newPopulation); generation++; } timer.Stop(); var runTime = timer.ElapsedMilliseconds; lbl_time.Text = "Run time: " + runTime + "ms"; } else { MessageBox.Show("Please enter text in the target control", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); txtBx_target.Clear(); txtBx_target.Focus(); } btn_run.Enabled = true; btn_reset.Enabled = true; btn_about.Enabled = true; }