private void bgw_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; // Get settings int numGenerations = (int)numericNumGenerations.Value; eaLoop.Initialize(); for (int i = 0; i < numGenerations; i++) { if (worker.CancellationPending) { return; } // Perform one iteration of the loop eaLoop.Iterate(); lock (plotPopulation) { plotPopulation.Clear(); plotPopulation.AddRange(eaLoop.AdultPopulation); } bgw.ReportProgress(1, i); } }
private void bgw_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; // Get settings int numGenerations = (int)numericNumGenerations.Value; int numRuns = (int)numericNumRuns.Value; maxValues = new float[numGenerations, numRuns]; averageValues = new float[numGenerations, numRuns]; sdValues = new float[numGenerations, numRuns]; bool plotSd = checkBoxSd.Checked; for (int n = 0; n < numRuns; n++) { eaLoop.Initialize(); for (int i = 0; i < numGenerations; i++) { if (worker.CancellationPending) { return; } // Perform one iteration of the loop eaLoop.Iterate(); // Sample data max = eaLoop.max; average = eaLoop.average; maxValues[i, n] = max; averageValues[i, n] = average; // Calculate standard deviations sd = 0; foreach (Individual individual in eaLoop.AdultPopulation) { sd += (float)Math.Pow(individual.Fitness - average, 2); } sd /= eaLoop.AdultPopulation.Count; sd = (float)Math.Sqrt(sd); sdValues[i, n] = sd; if (numRuns == 1) { worker.ReportProgress(i); if (max == eaLoop.goal) { Debug.WriteLine("Reached goal"); return; } } } } // Find the maximal individual Debug.WriteLine(max); return; }