From c0173a0c2a4ece1d10e7eada5669957cede1c1eb Mon Sep 17 00:00:00 2001 From: Zvi Rosenfeld Date: Thu, 25 Jul 2019 15:56:25 +0300 Subject: [PATCH] Fixed a bug --- .gitignore | 2 ++ GeneticAlgorithm.UnitTests/SearchTests.cs | 2 +- .../SearchUtilsTests.cs | 4 ++- GeneticAlgorithm/GeneticSearchEngine.cs | 27 ++++++++++++------- GeneticAlgorithm/GeneticSearchResult.cs | 4 +-- GeneticAlgorithm/SearchUtils.cs | 6 ++--- 6 files changed, 28 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index d764238..98f40a9 100644 --- a/.gitignore +++ b/.gitignore @@ -264,3 +264,5 @@ paket-files/ __pycache__/ *.pyc *.zip + +*nuget.exe diff --git a/GeneticAlgorithm.UnitTests/SearchTests.cs b/GeneticAlgorithm.UnitTests/SearchTests.cs index aa2889d..64f94df 100644 --- a/GeneticAlgorithm.UnitTests/SearchTests.cs +++ b/GeneticAlgorithm.UnitTests/SearchTests.cs @@ -73,7 +73,7 @@ public void HistoryExistTest(bool includeHistory) var result = searchEngine.Search(); if (includeHistory) - Assert.AreEqual(3, result.History.Count, "There should have been history"); + Assert.AreEqual(2, result.History.Count, "There should have been history"); else Assert.AreEqual(0, result.History.Count, "There shouldn't be any history"); } diff --git a/GeneticAlgorithm.UnitTests/SearchUtilsTests.cs b/GeneticAlgorithm.UnitTests/SearchUtilsTests.cs index f1096cb..8fb533e 100644 --- a/GeneticAlgorithm.UnitTests/SearchUtilsTests.cs +++ b/GeneticAlgorithm.UnitTests/SearchUtilsTests.cs @@ -26,7 +26,9 @@ public void ChooseBestTest() A.CallTo(() => chromosome2.Evaluate()).Returns(8); A.CallTo(() => chromosome3.Evaluate()).Returns(0.5); - var population = new[] {chromosome1, chromosome2, chromosome3}; + var population = new Population(new[] {chromosome1, chromosome2, chromosome3}); + foreach (var chromosome in population) + chromosome.Evaluation = chromosome.Chromosome.Evaluate(); var best = population.ChooseBest(); Assert.AreEqual(chromosome2, best); diff --git a/GeneticAlgorithm/GeneticSearchEngine.cs b/GeneticAlgorithm/GeneticSearchEngine.cs index eb61e54..6d9e6ff 100644 --- a/GeneticAlgorithm/GeneticSearchEngine.cs +++ b/GeneticAlgorithm/GeneticSearchEngine.cs @@ -15,7 +15,7 @@ public class GeneticSearchEngine private readonly GeneticSearchOptions options; /// - /// This even is risen for every new generation. It's arguments are the population and their evaluations. + /// This even is risen once for every new generation. It's arguments are the population and their evaluations. /// public event Action OnNewGeneration; @@ -34,17 +34,20 @@ public GeneticSearchResult Search() var stopwatch = new Stopwatch(); stopwatch.Start(); history = new List(); - population = new Population(populationGenerator.GeneratePopulation(options.PopulationSize).ToArray()); - if (options.IncludeAllHistory) - history.Add(population.GetChromosomes()); - int generation; for (generation = 0; generation < options.MaxGenerations; generation++) { + if (generation == 0) + population = new Population(populationGenerator.GeneratePopulation(options.PopulationSize).ToArray()); + else + GenerateChildren(); EvaluatePopulation(); if (options.StopManagers.Any(stopManager => stopManager.ShouldStop(population.GetChromosomes(), population.GetEvaluations(), generation))) + { + UpdateEventsAndHistory(population.GetChromosomes(), population.GetEvaluations()); break; + } var populationToRenew = GetPopulationToRenew(generation); if (populationToRenew > 0) @@ -54,15 +57,11 @@ public GeneticSearchResult Search() } UpdateNewGeneration(); - NormilizeEvaluations(); - GenerateChildren(); - if (options.IncludeAllHistory) - history.Add(population.GetChromosomes()); } stopwatch.Stop(); - return new GeneticSearchResult(population.GetChromosomes(), history, stopwatch.Elapsed, generation); + return new GeneticSearchResult(population.ChooseBest(), population.GetChromosomes(), history, stopwatch.Elapsed, generation); } /// @@ -78,7 +77,15 @@ private void UpdateNewGeneration() foreach (var populationRenwalManager in options.PopulationRenwalManagers) populationRenwalManager.AddGeneration(chromosomes, evaluations); + UpdateEventsAndHistory(chromosomes, evaluations); + } + + private void UpdateEventsAndHistory(IChromosome[] chromosomes, double[] evaluations) + { OnNewGeneration?.Invoke(chromosomes, evaluations); + + if (options.IncludeAllHistory) + history.Add(population.GetChromosomes()); } private void GenerateChildren() diff --git a/GeneticAlgorithm/GeneticSearchResult.cs b/GeneticAlgorithm/GeneticSearchResult.cs index 3ff598d..615f0b4 100644 --- a/GeneticAlgorithm/GeneticSearchResult.cs +++ b/GeneticAlgorithm/GeneticSearchResult.cs @@ -6,13 +6,13 @@ namespace GeneticAlgorithm { public class GeneticSearchResult { - public GeneticSearchResult(IChromosome[] population, List history, TimeSpan searchTime, int generations) + public GeneticSearchResult(IChromosome bestChromosome, IChromosome[] population, List history, TimeSpan searchTime, int generations) { Population = population; History = history; SearchTime = searchTime; Generations = generations; - BestChromosome = population.ChooseBest(); + BestChromosome = bestChromosome; } public IChromosome[] Population { get; } diff --git a/GeneticAlgorithm/SearchUtils.cs b/GeneticAlgorithm/SearchUtils.cs index 6f7c409..cb3085a 100644 --- a/GeneticAlgorithm/SearchUtils.cs +++ b/GeneticAlgorithm/SearchUtils.cs @@ -4,17 +4,17 @@ namespace GeneticAlgorithm { public static class SearchUtils { - public static IChromosome ChooseBest(this IChromosome[] population) + public static IChromosome ChooseBest(this Population population) { double bestEvaluation = -1; IChromosome bestChromosome = null; foreach (var chromosome in population) { - var evaluation = chromosome.Evaluate(); + var evaluation = chromosome.Evaluation; if (evaluation > bestEvaluation) { bestEvaluation = evaluation; - bestChromosome = chromosome; + bestChromosome = chromosome.Chromosome; } }