Skip to content

Commit

Permalink
1.3.2 content
Browse files Browse the repository at this point in the history
  • Loading branch information
ZviRosenfeld committed Jan 10, 2020
1 parent e55d1b9 commit bffc489
Show file tree
Hide file tree
Showing 37 changed files with 868 additions and 159 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using GeneticAlgorithm.Components.CrossoverManagers;
using GeneticAlgorithm.Interfaces;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GeneticAlgorithm.UnitTests.Components.CrossoverManagers
{
[TestClass]
public class AlternatingPositionCrossoverTests
{
private readonly ICrossoverManager crossoverManager = new AlternatingPositionCrossover<string>(null, null);

[TestMethod]
[DataRow(20)]
public void OrderCrossover_AllElementsInEachVector(int vectors)
{
crossoverManager.TestThatAllElementsInEachVector(vectors);
}

[TestMethod]
public void OrderCrossover_ChildChanged()
{
crossoverManager.TestThatChildChanged();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using GeneticAlgorithm.Components.Chromosomes;
using GeneticAlgorithm.Components.Chromosomes;
using GeneticAlgorithm.Components.CrossoverManagers;
using GeneticAlgorithm.Components.PopulationGenerators;
using GeneticAlgorithm.Interfaces;
using GeneticAlgorithm.UnitTests.TestUtils;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand All @@ -12,8 +9,6 @@ namespace GeneticAlgorithm.UnitTests.Components.CrossoverManagers
[TestClass]
public class CycleCrossoverManagerTests
{
private static readonly List<string> elements = new List<string>() { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
private readonly IPopulationGenerator generator = new AllElementsVectorChromosomePopulationGenerator<string>(elements, null, null);
private readonly ICrossoverManager crossoverManager = new CycleCrossoverManager<string>(null, null);

[TestMethod]
Expand Down Expand Up @@ -60,12 +55,7 @@ public void CycleCrossoverTest()
[DataRow(20)]
public void CycleCrossover_AllElementsInEachVector(int vectors)
{
for (int i = 0; i < vectors; i++)
{
var parentChromosomes = generator.GeneratePopulation(2);
var child = (VectorChromosome<string>)crossoverManager.Crossover(parentChromosomes.ElementAt(0), parentChromosomes.ElementAt(1));
child.AssertContainSameElements(elements);
}
crossoverManager.TestThatAllElementsInEachVector(vectors);
}

private void TestCycleCrossover(string[] parent1, string[] parent2, string[] expectedChild)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using GeneticAlgorithm.Components.CrossoverManagers;
using GeneticAlgorithm.Interfaces;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GeneticAlgorithm.UnitTests.Components.CrossoverManagers
{
[TestClass]
public class OrderBassedCrossoverTests
{
private readonly ICrossoverManager crossoverManager = new OrderBasedCrossover<string>(null, null);

[TestMethod]
[DataRow(20)]
public void OrderBasedCrossover_AllElementsInEachVector(int vectors)
{
crossoverManager.TestThatAllElementsInEachVector(vectors);
}

[TestMethod]
public void OrderBasedCrossover_ChildChanged()
{
crossoverManager.TestThatChildChanged();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,57 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GeneticAlgorithm.Components.Chromosomes;
using GeneticAlgorithm.Components.CrossoverManagers;
using GeneticAlgorithm.Components.PopulationGenerators;
using GeneticAlgorithm.Components.CrossoverManagers;
using GeneticAlgorithm.Interfaces;
using GeneticAlgorithm.UnitTests.TestUtils;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GeneticAlgorithm.UnitTests.Components.CrossoverManagers
{
[TestClass]
public class OrderCrossoverTests
{
private static readonly List<string> elements = new List<string>() { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
private readonly IPopulationGenerator generator = new AllElementsVectorChromosomePopulationGenerator<string>(elements, null, null);
private readonly ICrossoverManager crossoverManager = new OrderCrossover<string>(null, null);

[TestMethod]
[DataRow(20)]
public void OrderCrossover_AllElementsInEachVector(int vectors)
{
for (int i = 0; i < vectors; i++)
{
var parentChromosomes = generator.GeneratePopulation(2);
var child = (VectorChromosome<string>)crossoverManager.Crossover(parentChromosomes.ElementAt(0), parentChromosomes.ElementAt(1));
child.AssertContainSameElements(elements);
}
crossoverManager.TestThatAllElementsInEachVector(vectors);
}

[TestMethod]
public void OrderCrossover_ChildChanged()
{
// Since there's a certain chance that this test will fail, I want to run it twice
var passed = false;
for (int i = 0; i < 2; i++)
{
var parentChromosomes = generator.GeneratePopulation(2);
var child = (VectorChromosome<string>) crossoverManager.Crossover(parentChromosomes.ElementAt(0),
parentChromosomes.ElementAt(1));

try
{
((VectorChromosome<string>)parentChromosomes.ElementAt(0)).AssertAreNotTheSame(child);
((VectorChromosome<string>)parentChromosomes.ElementAt(1)).AssertAreNotTheSame(child);
passed = true;
}
catch
{
// Do nothing
}
}
Assert.IsTrue(passed);
crossoverManager.TestThatChildChanged();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,56 +1,25 @@
using System.Collections.Generic;
using System.Linq;
using GeneticAlgorithm.Components.Chromosomes;
using GeneticAlgorithm.Components.CrossoverManagers;
using GeneticAlgorithm.Components.PopulationGenerators;
using GeneticAlgorithm.Components.CrossoverManagers;
using GeneticAlgorithm.Interfaces;
using GeneticAlgorithm.UnitTests.TestUtils;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GeneticAlgorithm.UnitTests.Components.CrossoverManagers
{
[TestClass]
public class PartiallyMappedCrossoverTests
{
private static readonly List<string> elements = new List<string>() { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
private readonly IPopulationGenerator generator = new AllElementsVectorChromosomePopulationGenerator<string>(elements, null, null);
private readonly ICrossoverManager crossoverManager = new PartiallyMappedCrossover<string>(null, null);

[TestMethod]
[DataRow(20)]
public void PartiallyMatchedCrossover_AllElementsInEachVector(int vectors)
{
for (int i = 0; i < vectors; i++)
{
var parentChromosomes = generator.GeneratePopulation(2);
var child = (VectorChromosome<string>)crossoverManager.Crossover(parentChromosomes.ElementAt(0), parentChromosomes.ElementAt(1));
child.AssertContainSameElements(elements);
}
crossoverManager.TestThatAllElementsInEachVector(vectors);
}

[TestMethod]
public void PartiallyMatchedCrossover_ChildChanged()
{
// Since there's a certain chance that this test will fail, I want to run it twice
var passed = false;
for (int i = 0; i < 2; i++)
{
var parentChromosomes = generator.GeneratePopulation(2);
var child = (VectorChromosome<string>)crossoverManager.Crossover(parentChromosomes.ElementAt(0),
parentChromosomes.ElementAt(1));

try
{
((VectorChromosome<string>)parentChromosomes.ElementAt(0)).AssertAreNotTheSame(child);
((VectorChromosome<string>)parentChromosomes.ElementAt(1)).AssertAreNotTheSame(child);
passed = true;
}
catch
{
// Do nothing
}
}
Assert.IsTrue(passed);
crossoverManager.TestThatChildChanged();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using GeneticAlgorithm.Components.CrossoverManagers;
using GeneticAlgorithm.Interfaces;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GeneticAlgorithm.UnitTests.Components.CrossoverManagers
{
[TestClass]
public class PositionBasedCrossoverManagerTests
{
private readonly ICrossoverManager crossoverManager = new PositionBasedCrossoverManager<string>(null, null);

[TestMethod]
[DataRow(20)]
public void PositionBasedCrossover_AllElementsInEachVector(int vectors)
{
crossoverManager.TestThatAllElementsInEachVector(vectors);
}

[TestMethod]
public void PositionBasedCrossover_ChildChanged()
{
crossoverManager.TestThatChildChanged();
}
}
}
45 changes: 44 additions & 1 deletion GeneticAlgorithm.UnitTests/Components/CrossoverManagers/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System.Collections.Generic;
using System.Linq;
using GeneticAlgorithm.Components.Chromosomes;
using GeneticAlgorithm.Components.PopulationGenerators;
using GeneticAlgorithm.Interfaces;
using GeneticAlgorithm.UnitTests.TestUtils;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GeneticAlgorithm.UnitTests.Components.CrossoverManagers
Expand Down Expand Up @@ -30,13 +34,52 @@ public static List<int> K_CrossoverGetCrossoverPointsAndAssertThatGenomesAreRigh
takingFromFirstChromosome = !takingFromFirstChromosome;
}
else if (newChromosome[i] != chromosome2[i])
Assert.Fail($"Got Genome that dosn't seem to have came from anywhere. 1: {chromosome1}; 2 {chromosome2}; newChromosome {newChromosome} ");
Assert.Fail($"Got Genome that doesn't seem to have came from anywhere. 1: {chromosome1}; 2 {chromosome2}; newChromosome {newChromosome} ");
}
}
for (int j = chromosome1.Length; j < chromosome2.Length && j < newChromosome.GetVector().Length; j++)
Assert.AreEqual(chromosome2[j], newChromosome[j]);

return crossoverPoints;
}

public static void TestThatAllElementsInEachVector(this ICrossoverManager crossoverManager, int testRuns)
{
var elements = new List<string>() { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
var generator = new AllElementsVectorChromosomePopulationGenerator<string>(elements, null, null);
for (int i = 0; i < testRuns; i++)
{
var parentChromosomes = generator.GeneratePopulation(2);
var child = (VectorChromosome<string>)crossoverManager.Crossover(parentChromosomes.ElementAt(0), parentChromosomes.ElementAt(1));
child.AssertContainSameElements(elements);
}
}

public static void TestThatChildChanged(this ICrossoverManager crossoverManager)
{
var elements = new List<string>() { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
var generator = new AllElementsVectorChromosomePopulationGenerator<string>(elements, null, null);

// Since there's a certain chance that this test will fail, I want to run it twice
var passed = false;
for (int i = 0; i < 2; i++)
{
var parentChromosomes = generator.GeneratePopulation(2);
var child = (VectorChromosome<string>)crossoverManager.Crossover(parentChromosomes.ElementAt(0),
parentChromosomes.ElementAt(1));

try
{
((VectorChromosome<string>)parentChromosomes.ElementAt(0)).AssertAreNotTheSame(child);
((VectorChromosome<string>)parentChromosomes.ElementAt(1)).AssertAreNotTheSame(child);
passed = true;
}
catch
{
// Do nothing
}
}
Assert.IsTrue(passed);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using GeneticAlgorithm.Components.Interfaces;
using GeneticAlgorithm.Components.MutationManagers;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GeneticAlgorithm.UnitTests.Components.MutationManagers
{
[TestClass]
public class DisplacementMutationManagerTests
{
private readonly IMutationManager<string> mutationManager = new DisplacementMutationManager<string>();

[TestMethod]
[DataRow(20)]
public void DisplacementMutationManager_AllElementsInEachVector(int vectors)
{
mutationManager.TestAllElementsInEachVector(vectors);
}

// This test is probabilistic, so there's a certain chance that this test will fail even if the code is okay.
[TestMethod]
public void DisplacementMutationManager_ChromosomeChanged()
{
mutationManager.TestChromosomeChanged();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using GeneticAlgorithm.Components.Chromosomes;
using GeneticAlgorithm.Components.Interfaces;
using GeneticAlgorithm.Components.MutationManagers;
using GeneticAlgorithm.Components.PopulationGenerators;
using GeneticAlgorithm.UnitTests.TestUtils;
Expand All @@ -9,34 +11,37 @@
namespace GeneticAlgorithm.UnitTests.Components.MutationManagers
{
[TestClass]
public class SingleSwapMutationManagerTests
public class ExchangeMutationManagerTests
{
private readonly IMutationManager<string> mutationManager = new ExchangeMutationManager<string>();

[TestMethod]
[DataRow(20)]
public void SingleSwapMutationManager_AllElementsInEachVector(int vectors)
public void ExchangeMutationManager_AllElementsInEachVector(int vectors)
{
var elements = new List<string>() { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
var generator = new AllElementsVectorChromosomePopulationGenerator<string>(elements, null, null);
var mutationManager = new SingleSwapMutationManager<string>();
for (int i = 0; i < vectors; i++)
{
var before = ((VectorChromosome<string>)generator.GeneratePopulation(1).First()).GetVector();
var after = mutationManager.Mutate(before.ToArray());
before.AssertContainSameElements(after);
}
mutationManager.TestAllElementsInEachVector(vectors);
}

// This test is probabilistic, so there's a certain chance that this test will fail even if the code is okay.
[TestMethod]
public void SingleSwapMutationManager_ChromosomeChanged()
public void ExchangeMutationManager_ChromosomeChanged()
{
var elements = new List<string>() { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
var generator = new AllElementsVectorChromosomePopulationGenerator<string>(elements, null, null);
var mutationManager = new SingleSwapMutationManager<string>();
var before = ((VectorChromosome<string>)generator.GeneratePopulation(1).First()).GetVector();
var after = mutationManager.Mutate(before.ToArray());
mutationManager.TestChromosomeChanged();
}

before.AssertAreNotTheSame(after);
[TestMethod]
[DataRow(20)]
public void ExchangeMutationManager_ExactlyTwoGenomesAreDiffrent(int vectors)
{
var elements = new List<string> { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
var generator = new AllElementsVectorChromosomePopulationGenerator<string>(elements, null, null);
for (int i = 0; i < vectors; i++)
{
var before = ((VectorChromosome<string>)generator.GeneratePopulation(1).First()).GetVector();
var after = mutationManager.Mutate(before.ToArray());
var diffrentGenomes = before.Where((t, j) => t != after[j]).Count();
Assert.IsTrue(diffrentGenomes == 0 || diffrentGenomes == 2);
}
}
}
}
Loading

0 comments on commit bffc489

Please sign in to comment.