static bool PerGenerationCallback(EngineProgress p) { string bestSuffix = " ", avgSuffix = " "; if (p.BestFitnessSoFar < bestOverallScoreSoFar) { bestOverallScoreSoFar = p.BestFitnessSoFar; bestSuffix = "*"; } if (p.AvgFitnessThisGen < bestAverageScoreSoFar) { bestAverageScoreSoFar = p.AvgFitnessThisGen; avgSuffix = "*"; } Console.WriteLine("Gen " + p.GenerationNumber.ToString().PadLeft(3) + " avg: " + FormatNumber(p.AvgFitnessThisGen) + avgSuffix + " best: " + FormatNumber(p.BestFitnessThisGen) + bestSuffix + " t: " + p.TimeForGeneration.TotalSeconds.ToString("0.00")); // save stats: date, gen#, best-this-gen, avg-this-gen, settings var writer = File.AppendText("per-gen-stats.csv"); writer.WriteLine( "\"" + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + "\"," + p.GenerationNumber + "," + p.BestFitnessThisGen.ToString() + "," + p.AvgFitnessThisGen.ToString() + "," + printableParams); writer.Close(); return(true); // keep going }
//------------------------------------------------------------------------- // For each generation, we get information about what's going on //------------------------------------------------------------------------- private bool PerGenerationCallback(EngineProgress progress, Strategy bestThisGen) { string bestSuffix = " ", avgSuffix = " "; if (progress.BestFitnessSoFar > bestOverallScoreSoFar) { bestOverallScoreSoFar = progress.BestFitnessSoFar; bestSuffix = "*"; } if (progress.AvgFitnessThisGen > bestAverageScoreSoFar) { bestAverageScoreSoFar = progress.AvgFitnessThisGen; avgSuffix = "*"; } string summary = "Gen " + progress.GenerationNumber.ToString().PadLeft(4) + " best: " + progress.BestFitnessThisGen.ToString("0").PadLeft(7) + bestSuffix + " avg: " + progress.AvgFitnessThisGen.ToString("0").PadLeft(7) + avgSuffix + " " + progress.TimeForGeneration.TotalSeconds.ToString("0") + "s"; DisplayCurrentStatus(summary); Debug.WriteLine("Generation " + progress.GenerationNumber + " took " + progress.TimeForGeneration.TotalSeconds.ToString("0") + "s"); // all settings in one column string settings = "P: " + ProgramConfiguration.GAsettings.PopulationSize + " " + "G: " + ProgramConfiguration.GAsettings.MinGenerations + " - " + ProgramConfiguration.GAsettings.MaxGenerations + " " + "Stgn: " + ProgramConfiguration.GAsettings.MaxStagnantGenerations + " " + "Sel: " + ProgramConfiguration.GAsettings.SelectionStyle + " " + "Trny: " + ProgramConfiguration.GAsettings.TourneySize + " " + "Mut: " + ProgramConfiguration.GAsettings.MutationRate + " " + "MI: " + ProgramConfiguration.GAsettings.MutationImpact + " " + "Elit: " + ProgramConfiguration.GAsettings.ElitismRate + " "; // save stats: date, gen#, best-this-gen, avg-this-gen, settings var writer = File.AppendText("per-gen-stats.csv"); writer.WriteLine( "\"" + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + "\"," + progress.GenerationNumber + "," + progress.BestFitnessThisGen.ToString("0") + "," + progress.AvgFitnessThisGen.ToString("0") + "," + settings); writer.Close(); // keep track of how many gens we've searched totalGenerations = progress.GenerationNumber; // then display this generation's best DisplayStrategyGrids(bestThisGen, "Best from generation " + totalGenerations); // return true to keep going, false to halt the system bool keepRunning = true; return(keepRunning); }