Skip to content

Commit

Permalink
Fixed a bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ZviRosenfeldCx committed Jul 25, 2019
1 parent e8b158c commit c0173a0
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,5 @@ paket-files/
__pycache__/
*.pyc
*.zip

*nuget.exe
2 changes: 1 addition & 1 deletion GeneticAlgorithm.UnitTests/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
4 changes: 3 additions & 1 deletion GeneticAlgorithm.UnitTests/SearchUtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
27 changes: 17 additions & 10 deletions GeneticAlgorithm/GeneticSearchEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class GeneticSearchEngine
private readonly GeneticSearchOptions options;

/// <summary>
/// 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.
/// </summary>
public event Action<IChromosome[], double[]> OnNewGeneration;

Expand All @@ -34,17 +34,20 @@ public GeneticSearchResult Search()
var stopwatch = new Stopwatch();
stopwatch.Start();
history = new List<IChromosome[]>();
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)
Expand All @@ -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);
}

/// <summary>
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions GeneticAlgorithm/GeneticSearchResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ namespace GeneticAlgorithm
{
public class GeneticSearchResult
{
public GeneticSearchResult(IChromosome[] population, List<IChromosome[]> history, TimeSpan searchTime, int generations)
public GeneticSearchResult(IChromosome bestChromosome, IChromosome[] population, List<IChromosome[]> history, TimeSpan searchTime, int generations)
{
Population = population;
History = history;
SearchTime = searchTime;
Generations = generations;
BestChromosome = population.ChooseBest();
BestChromosome = bestChromosome;
}

public IChromosome[] Population { get; }
Expand Down
6 changes: 3 additions & 3 deletions GeneticAlgorithm/SearchUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down

0 comments on commit c0173a0

Please sign in to comment.