Skip to content

Commit 266a2b5

Browse files
Added SelectionStrategyUtilsTests
1 parent 114e200 commit 266a2b5

File tree

8 files changed

+87
-29
lines changed

8 files changed

+87
-29
lines changed

GeneticAlgorithm.UnitTests/MutationManagersTests.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ public void ConvergenceMutationManagerTest()
1212
{
1313
var convergenceMutationManager = new ConvergenceMutationManager();
1414

15-
var homogeneousPopulation = new Population(new double[] {2, 2, 2, 2, 2 }.ToChromosomes());
16-
homogeneousPopulation.Evaluate();
17-
18-
var diversifiedPopulation = new Population(new double[] { 1, 2, 3, 4, 5 }.ToChromosomes());
19-
diversifiedPopulation.Evaluate();
15+
var homogeneousPopulation = new double[] {2, 2, 2, 2, 2 }.ToPopulation().Evaluate();
16+
var diversifiedPopulation = new double[] { 1, 2, 3, 4, 5 }.ToPopulation().Evaluate();
2017

2118
convergenceMutationManager.AddGeneration(homogeneousPopulation);
2219

GeneticAlgorithm.UnitTests/SearchUtilsTests.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ public void ChooseBestTest()
2727
A.CallTo(() => chromosome2.Evaluate()).Returns(8);
2828
A.CallTo(() => chromosome3.Evaluate()).Returns(0.5);
2929

30-
var population = new Population(new[] {chromosome1, chromosome2, chromosome3});
31-
population.Evaluate();
30+
var population = new Population(new[] {chromosome1, chromosome2, chromosome3}).Evaluate();
3231
var best = population.ChooseBest();
3332

3433
Assert.AreEqual(chromosome2, best);
@@ -74,13 +73,9 @@ public void CombineTest_SecondArrayEmty()
7473
}
7574

7675
[TestMethod]
77-
public void ClonePopulation_IfPopulationCloned()
76+
public void ClonePopulation_PopulationCloned()
7877
{
79-
var evaluations = new double[] {1, 2, 1, 3};
80-
var chromosomes = evaluations.ToChromosomes("Chromosomes");
81-
var population = new Population(chromosomes);
82-
population.Evaluate();
83-
78+
var population = (new double[] { 1, 2, 1, 3 }).ToPopulation("Chromosomes").Evaluate();
8479
var populationClone = population.Clone();
8580

8681
population.AssertIsSame(populationClone);
@@ -89,9 +84,7 @@ public void ClonePopulation_IfPopulationCloned()
8984
[TestMethod]
9085
public void ClonePopulation_ChangingCloneDosntChangeOriginal()
9186
{
92-
var evaluations = new double[] { 1, 1, 1, 1 };
93-
var chromosomes = evaluations.ToChromosomes("Chromosomes");
94-
var population = new Population(chromosomes);
87+
var population = (new double[] { 1, 1, 1, 1 }).ToPopulation("Chromosomes");
9588

9689
var populationClone = population.Clone();
9790
var fakeChromosome = ChromosomeFactory.CreateChromosome(10, "New");

GeneticAlgorithm.UnitTests/SelectionStrategyTests/SelectionStrategyTests.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ public class SelectionStrategyTests
1717
[TestInitialize]
1818
public void TestInitialize()
1919
{
20-
population = ChromosomeFactory.ToPopulation(new [] { chromosome1Probability * 2, chromosome2Probability * 2, chromosome3Probability * 2});
21-
Utils.Evaluate(population);
20+
population = new [] { chromosome1Probability * 2, chromosome2Probability * 2, chromosome3Probability * 2}.ToPopulation().Evaluate();
2221
}
2322

2423
[TestMethod]
@@ -183,8 +182,7 @@ private void AssertSelectionStrategyUsesLatestPopulation(ISelectionStrategy sele
183182
/// </summary>
184183
private void AssertChromosomesAreScattered(ISelectionStrategy selectionStrategy)
185184
{
186-
var population = new[] { 0.23, 0.24, 0.26, 0.27 }.ToPopulation();
187-
population.Evaluate();
185+
var population = new[] { 0.23, 0.24, 0.26, 0.27 }.ToPopulation().Evaluate();
188186
selectionStrategy.SetPopulation(population, 100);
189187

190188
bool chromosome1 = false, chromosome2 = false, chromosome3 = false, chromosome4 = false;
@@ -209,8 +207,7 @@ private void AssertChromosomesAreScattered(ISelectionStrategy selectionStrategy)
209207

210208
private void AssertLowestChromosomesAreIgnored(ISelectionStrategy selectionStrategy, int chromosomesToIgnore)
211209
{
212-
var population = new double[] {1, 2, 3, 4}.ToPopulation();
213-
population.Evaluate();
210+
var population = new double[] {1, 2, 3, 4}.ToPopulation().Evaluate();
214211
selectionStrategy.SetPopulation(population, 100);
215212

216213
bool chromosome1 = false, chromosome2 = false, chromosome3 = false, chromosome4 = false;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System.Linq;
2+
using GeneticAlgorithm.Exceptions;
3+
using GeneticAlgorithm.SelectionStrategies;
4+
using GeneticAlgorithm.UnitTests.TestUtils;
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
7+
namespace GeneticAlgorithm.UnitTests.SelectionStrategyTests
8+
{
9+
[TestClass]
10+
public class SelectionStrategyUtilsTests
11+
{
12+
[TestMethod]
13+
public void GetNormilizeEvaluationsTest()
14+
{
15+
var population = (new double[] { 1, 2, 6, 1 }).ToPopulation().Evaluate();
16+
var normilizeEvaluations = population.GetNormilizeEvaluations();
17+
18+
new []{0.1, 0.2, 0.6, 0.1}.AssertAreTheSame(normilizeEvaluations.ToArray());
19+
}
20+
21+
[TestMethod]
22+
public void GetNormilizeEvaluations_OriginalPopulationNotChanged()
23+
{
24+
var originalEvaluations = new double[] {1, 2, 6, 1};
25+
var population = originalEvaluations.ToPopulation().Evaluate();
26+
population.GetNormilizeEvaluations();
27+
28+
originalEvaluations.AssertAreTheSame(population.GetEvaluations());
29+
}
30+
31+
[TestMethod]
32+
[DataRow(1)]
33+
[DataRow(5)]
34+
[DataRow(10)]
35+
public void GetBestChromosomesTest(int numberOfBestChromosomes)
36+
{
37+
var population = new double[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}.ToPopulation().Evaluate();
38+
var newPopulation = population.GetBestChromosomes(numberOfBestChromosomes);
39+
40+
var newEvaluations = newPopulation.GetEvaluations();
41+
var minChromosomeInNewPopulation = 10 - numberOfBestChromosomes;
42+
for (int i = minChromosomeInNewPopulation; i >= minChromosomeInNewPopulation; i--)
43+
Assert.IsTrue(newEvaluations.Any(v => v == i), $"{nameof(newPopulation)} doesn't contain {i}");
44+
45+
Assert.AreEqual(numberOfBestChromosomes ,newPopulation.Count(), $"To many chromosome in {nameof(newPopulation)}");
46+
}
47+
48+
[TestMethod]
49+
public void GetBestChromosomes_OriginalPopulationNotChanged()
50+
{
51+
var originalEvaluations = new double[] { 1, 2, 6, 1 };
52+
var population = originalEvaluations.ToPopulation().Evaluate();
53+
population.GetBestChromosomes(1);
54+
55+
Assert.AreEqual(4, population.Count());
56+
}
57+
58+
[TestMethod]
59+
[DataRow(0)]
60+
[DataRow(-2)]
61+
[DataRow(4)]
62+
[ExpectedException(typeof(InternalSearchException), "Code 1006")]
63+
public void GetBestChromosomes_BadNumberOfChromosomes_ThrowException(int numberOfBestChromosomes)
64+
{
65+
var population = new double[] { 1, 1, 1 }.ToPopulation().Evaluate();
66+
population.GetBestChromosomes(numberOfBestChromosomes);
67+
}
68+
}
69+
}

GeneticAlgorithm.UnitTests/TestUtils/Assertions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public static void AssertAreTheSame(this double[] collection1, double[] collecti
3333

3434
public static void AssertAreTheSame(GeneticSearchResult result1, GeneticSearchResult result2)
3535
{
36-
Assert.AreEqual(result1.Generations, result2.Generations, "Diffrent number of generations");
37-
Assert.AreEqual(result1.IsCompleted, result2.IsCompleted, "Diffrent 'IsComplate' value");
38-
Assert.AreEqual(result1.SearchTime, result2.SearchTime, "Diffrent searchTime");
36+
Assert.AreEqual(result1.Generations, result2.Generations, "Different number of generations");
37+
Assert.AreEqual(result1.IsCompleted, result2.IsCompleted, "Different 'IsComplate' value");
38+
Assert.AreEqual(result1.SearchTime, result2.SearchTime, "Different searchTime");
3939
result1.Population.AssertIsSame(result2.Population);
4040

4141
for (int i = 0; i < result1.History.Count; i++)

GeneticAlgorithm.UnitTests/TestUtils/Utils.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ public static GeneticSearchResult Run(this GeneticSearchEngine engine, RunType r
1616
return result;
1717
}
1818

19-
public static void Evaluate(this Population population)
19+
public static Population Evaluate(this Population population)
2020
{
2121
foreach (var chromosome in population)
2222
chromosome.Evaluation = chromosome.Chromosome.Evaluate();
23+
24+
return population;
2325
}
2426

2527
public static GeneticSearchEngine GetBassicEngine()

GeneticAlgorithm/GeneticAlgorithm.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package >
33
<metadata>
44
<id>GeneticAlgorithm</id>
5-
<version>1.2.1</version>
5+
<version>1.2.2</version>
66
<title>A parallel Genetic Algorithm Engine</title>
77
<description>A parallel Genetic Algorithm Engine. GeneticAlgorithm was created to be easily customized and simple to use.</description>
88
<authors>Zvi Rosenfeld</authors>

GeneticAlgorithm/SelectionStrategies/SelectionStrategyUtils.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace GeneticAlgorithm.SelectionStrategies
77
{
8-
static class SelectionStrategyUtils
8+
public static class SelectionStrategyUtils
99
{
1010
public static double[] GetNormilizeEvaluations(this Population population)
1111
{
@@ -19,7 +19,7 @@ public static double[] GetNormilizeEvaluations(this Population population)
1919
}
2020

2121
/// <summary>
22-
/// Returns a new population object with only the best n chromosomes
22+
/// Returns a population object with only the best n chromosomes
2323
/// </summary>
2424
public static Population GetBestChromosomes(this Population population, int n)
2525
{

0 commit comments

Comments
 (0)