Skip to content

Commit

Permalink
Added OnNewGeneration event
Browse files Browse the repository at this point in the history
  • Loading branch information
ZviRosenfeldCx committed Jul 25, 2019
1 parent 2df95a8 commit e8b158c
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 10 deletions.
43 changes: 40 additions & 3 deletions GeneticAlgorithm.UnitTests/SearchTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using GeneticAlgorithm.Interfaces;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GeneticAlgorithm.UnitTests
Expand Down Expand Up @@ -86,10 +88,45 @@ public void HistoryIsRightTest()
.IncludeAllHistory().Build();

var result = searchEngine.Search();

AssertHasEvaluation(result.History, population);
}

for (int i = 0; i < population.Length; i++)
for (int j = 0; j < population[i].Length; j++)
Assert.AreEqual(population[i][j], result.History[i][j].Evaluate());
[TestMethod]
public void OnNewGenerationEventTest()
{
var population = new[] { new double[] { 1, 1, 1 }, new double[] { 2, 2, 2 }, new double[] { 2, 3, 2 } };
var populationManager = new TestPopulationManager(population);
var searchEngine =
new TestGeneticSearchEngineBuilder(population[0].Length, population.Length - 1, populationManager)
.Build();

var actualPopulation = new List<IChromosome[]>();
var actualEvaluations = new List<double[]>();
searchEngine.OnNewGeneration += (c, d) =>
{
actualEvaluations.Add(d);
actualPopulation.Add(c);
};

searchEngine.Search();

AssertAreTheSame(actualEvaluations, population);
AssertHasEvaluation(actualPopulation, population);
}

private void AssertHasEvaluation(List<IChromosome[]> chromosomes, double[][] evaluations)
{
for (int i = 0; i < chromosomes.Count; i++)
for (int j = 0; j < chromosomes[0].Length; j++)
Assert.AreEqual(evaluations[i][j], chromosomes[i][j].Evaluate());
}

private void AssertAreTheSame(List<double[]> collection1, double[][] collection2)
{
for (int i = 0; i < collection1.Count; i++)
for (int j = 0; j < collection1[0].Length; j++)
Assert.AreEqual(collection1[i][j], collection2[i][j]);
}
}
}
21 changes: 17 additions & 4 deletions GeneticAlgorithm/GeneticSearchEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public class GeneticSearchEngine
private readonly IChildrenGenerator childrenGenerator;
private readonly GeneticSearchOptions options;

/// <summary>
/// This even is risen for every new generation. It's arguments are the population and their evaluations.
/// </summary>
public event Action<IChromosome[], double[]> OnNewGeneration;

public GeneticSearchEngine(GeneticSearchOptions options, IPopulationGenerator populationGenerator, IChildrenGenerator childrenGenerator)
{
this.options = options;
Expand Down Expand Up @@ -48,7 +53,7 @@ public GeneticSearchResult Search()
EvaluatePopulation();
}

UpdateStopAndRenewalManagers();
UpdateNewGeneration();

NormilizeEvaluations();
GenerateChildren();
Expand All @@ -60,12 +65,20 @@ public GeneticSearchResult Search()
return new GeneticSearchResult(population.GetChromosomes(), history, stopwatch.Elapsed, generation);
}

private void UpdateStopAndRenewalManagers()
/// <summary>
/// Update everyone that needs to know about the new generation
/// </summary>
private void UpdateNewGeneration()
{
var chromosomes = population.GetChromosomes();
var evaluations = population.GetEvaluations();

foreach (var stopManager in options.StopManagers)
stopManager.AddGeneration(population.GetChromosomes(), population.GetEvaluations());
stopManager.AddGeneration(chromosomes, evaluations);
foreach (var populationRenwalManager in options.PopulationRenwalManagers)
populationRenwalManager.AddGeneration(population.GetChromosomes(), population.GetEvaluations());
populationRenwalManager.AddGeneration(chromosomes, evaluations);

OnNewGeneration?.Invoke(chromosomes, evaluations);
}

private void GenerateChildren()
Expand Down
2 changes: 1 addition & 1 deletion GreatestVectorTests/BassicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace GreatestVectorTests
[TestClass]
public class BassicTests
{
public const int POPULATION_SIZE = 100;
private const int POPULATION_SIZE = 100;

[TestMethod]
public void BassicTest()
Expand Down
4 changes: 2 additions & 2 deletions GreatestVectorTests/NumberVectorBassicPopulationGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class NumberVectorBassicPopulationGenerator : IPopulationGenerator

public IEnumerable<IChromosome> GeneratePopulation(int size)
{
var population = new IChromosome[BassicTests.POPULATION_SIZE];
var population = new IChromosome[size];

for (int i = 0; i < BassicTests.POPULATION_SIZE; i++)
for (int i = 0; i < size; i++)
population[i] = GetChromosome();

return population;
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Your chromosomes will need to implement the IChromosome classes.
### ICrossoverManager

You'll need to implement the ICrossoverManager class. This tells the engine how to perform crossovers for your chromosomes.
You can read more about corossovers [here](https://en.wikipedia.org/wiki/Crossover_(genetic_algorithm)).

```CSharp
public interface ICrossoverManager
Expand Down Expand Up @@ -57,6 +58,23 @@ var searchEngine = new GeneticSearchEngineBuilder(POPULATION_SIZE, MAX_GENERATIO
var result = searchEngine.Search();
```

## Events

### OnNewGeneration

This event is called once for every new generations. It's arguments are the generation's population, and their evaluations.
This is a good way for GUIs to visually show the argument's progress, or just show the search progress.

Example:
```CSharp
var searchEngine = new GeneticSearchEngineBuilder(POPULATION_SIZE, MAX_GENERATIONS, crossoverManager, populationGenerator).Build();
searchEngine.OnNewGeneration += (IChromosome[] c,double[] d) =>
{
// Do some work here
};
var result = searchEngine.Search();
```

## Search Options
Let's see how we can configure our search engine to better match our needs.

Expand Down

0 comments on commit e8b158c

Please sign in to comment.