Skip to content

Commit e8da071

Browse files
Some fixes
1 parent 553d2d2 commit e8da071

File tree

7 files changed

+62
-7
lines changed

7 files changed

+62
-7
lines changed

GeneticAlgorithm.UnitTests/PopulationRenwalManagerTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ public void RenewAtConvergenceTest()
2424
Assert.AreEqual(4, result.BestChromosome.Evaluate());
2525
}
2626

27+
[TestMethod]
28+
public void RenewOnlySomeChromosomes()
29+
{
30+
var populationManager = new TestPopulationManager(new double[] { 4, 4, 4 }, false);
31+
populationManager.SetPopulationGenerated(new[]
32+
{new double[] {3, 3, 3}, new double[] {2, 2, 2}, new double[] {1, 1, 1}});
33+
var engine = new TestGeneticSearchEngineBuilder(POPULATION_SIZE, MAX_GENERATIONS, populationManager)
34+
.AddPopulationRenwalManager(new RenewAtConvergence(0.9, 0.5))
35+
.IncludeAllHistory().Build();
36+
37+
var result = engine.Search();
38+
39+
Assert.AreEqual(4, result.BestChromosome.Evaluate());
40+
}
41+
2742
[TestMethod]
2843
public void RenewIfNoImprovmentTest1()
2944
{
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace GeneticAlgorithm.Exceptions
2+
{
3+
class InternalSearchException : GeneticAlgorithmException
4+
{
5+
public InternalSearchException(string message) : base(message)
6+
{
7+
}
8+
}
9+
}

GeneticAlgorithm/GeneticSearchEngine.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Concurrent;
32
using System.Collections.Generic;
43
using System.Diagnostics;
54
using System.Linq;
@@ -80,8 +79,34 @@ private int GetPopulationToRenew(int generation)
8079
private void RenewPopulation(int populationToRenew)
8180
{
8281
var newPopulation = populationGenerator.GeneratePopulation(populationToRenew).ToArray();
83-
for (int i = 0; i < populationToRenew; i++)
82+
var oldPopulation = GetBestChromosomes(population.Length - populationToRenew);
83+
var i = 0;
84+
for (; i < populationToRenew; i++)
8485
population[i] = newPopulation[i];
86+
for (; i < population.Length; i++)
87+
population[i] = oldPopulation[i - populationToRenew];
88+
}
89+
90+
private IChromosome[] GetBestChromosomes(int n)
91+
{
92+
if (n == 0)
93+
return new IChromosome[0];
94+
95+
var min = evaluations.OrderByDescending(x => x).Take(n).Last();
96+
var bestChromosomes = new IChromosome[n];
97+
int index = 0;
98+
for (int i = 0; i < population.Length; i++)
99+
{
100+
if (evaluations[i] >= min)
101+
{
102+
bestChromosomes[index] = population[i];
103+
index++;
104+
}
105+
if (index >= n)
106+
return bestChromosomes;
107+
}
108+
109+
throw new InternalSearchException("Code 1000 (not enough best chromosomes found)");
85110
}
86111

87112
private void EvaluatePopulation()

GeneticAlgorithm/GeneticSearchEngineBuilder.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public GeneticSearchEngineBuilder SetMutationProbability(double probability)
2929
return this;
3030
}
3131

32+
/// <summary>
33+
/// The GeneticSearchResult will include the entire history of the population
34+
/// </summary>
3235
public GeneticSearchEngineBuilder IncludeAllHistory()
3336
{
3437
includeAllHistory = true;

GeneticAlgorithm/GeneticSearchOptions.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ public GeneticSearchOptions(int populationSize, int maxGenerations, double mutat
3131
public int MaxGenerations { get; }
3232

3333
public double MutationProbability { get; }
34-
35-
/// <summary>
36-
/// If this is true, the GeneticSearchResult will include the entire history of the population
37-
/// </summary>
34+
3835
public bool IncludeAllHistory { get; }
3936

4037
public List<IStopManager> StopManagers { get; }

GeneticAlgorithm/Interfaces/IPopulationGenerator.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ namespace GeneticAlgorithm.Interfaces
44
{
55
public interface IPopulationGenerator
66
{
7+
/// <summary>
8+
/// size is the number of chromosomes we want to generate
9+
/// </summary>
710
IEnumerable<IChromosome> GeneratePopulation(int size);
811
}
912
}

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ You'll need to implement the ICrossoverManager class. This tells the engine how
3333

3434
### IPopulationGenerator
3535

36-
You'll also need to implement the IPopulationGenerator class. The engine uses this class to create its initial population. the PopulationGeneratorwill also renew the population when needed (see the secssion on IPopulationRenwalManagers).
36+
You'll also need to implement the IPopulationGenerator class. The engine uses this class to create its initial population.
37+
The PopulationGenerator will also renew the population when needed (see [IPopulationRenwalManagers](https://github.com/ZviRosenfeld/GeneticAlgorithmEngine/blob/master/README.md#ipopulationrenwalmanagers)).
3738

3839
```CSharp
3940
public interface IPopulationGenerator
@@ -58,6 +59,8 @@ var result = searchEngine.Search();
5859

5960
## Search Options
6061

62+
Let's see how we can configure our search engine to better match our needs.
63+
6164
### Mutations
6265

6366
By defualt, the probability of mutations is 0. You can change this be using the GeneticSearchEngineBuilder.SetMutationProbability(double probability) method.

0 commit comments

Comments
 (0)