Skip to content

Commit

Permalink
Added some more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
JanPalasek committed May 5, 2019
1 parent 778d45a commit 53b8605
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
81 changes: 81 additions & 0 deletions JobShopScheduling.Tests/JobShopGeneticAlgorithmTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
namespace JobShopScheduling.Tests
{
using System.Linq;
using GeneticAlgorithm;
using GeneticSharp.Domain;
using GeneticSharp.Domain.Populations;
using GeneticSharp.Domain.Randomizations;
using JobShopStructures;
using Moq;
using NUnit.Framework;
using Serilog;

[TestFixture]
public class JobShopGeneticAlgorithmTests
{
private JobShopGeneticAlgorithm ga;

[SetUp]
public void SetUp()
{
var jobShop = new JobShopLoader().Load("TestExamples/ft06.in");
var mockLogger = new Mock<ILogger>();

Global.Config.MinPopulationSize = 100;
Global.Config.MaxPopulationSize = 100;
ga = new JobShopGeneticAlgorithm(jobShop, 1, mockLogger.Object, adaptive: false);
ga.GenerationRan += AssertUnique;
}

[Test]
public void RunTest1()
{
Global.Config.GenerationsCount = 20;
Global.Config.CrossoverProbability = 1f;
Global.Config.MutationProbability = 1f;
Global.Config.ElitismPercent = 0.5f;

ga.Run();
}
[Test]
public void RunTest2()
{
Global.Config.GenerationsCount = 20;
Global.Config.CrossoverProbability = 0f;
Global.Config.MutationProbability = 0f;
Global.Config.ElitismPercent = 0.5f;

ga.Run();
}


/// <summary>
/// Asserts that all <see cref="ScheduleChromosome"/> in specified generation are represented by a different instance
/// and all its genes are represented by different instances as well.
///
/// If two members of generation would share same instance, they would be mutated potentially twice (mutation doesn't create
/// a new instance) and thus breaking the GA.
/// </summary>
/// <param name="generation"></param>
public void AssertUnique(Generation generation)
{
var chromosomes = generation.Chromosomes.Cast<ScheduleChromosome>().ToArray();
for (int i = 0; i < chromosomes.Length; i++)
{
for (int j = 0; j < chromosomes.Length && j != i; j++)
{
Assert.That(chromosomes[i], Is.Not.SameAs(chromosomes[j]));

var iMachines = chromosomes[i].GetGenes().Select(x => x.Value).ToList();
var jMachines = chromosomes[j].GetGenes().Select(x => x.Value).ToList();

// no machines in second chromosome is same as machine in the first chromosome
foreach (var iMachine in iMachines)
{
Assert.That(jMachines.Any(x => object.ReferenceEquals(x, iMachine)), Is.False);
}
}
}
}
}
}
3 changes: 3 additions & 0 deletions JobShopScheduling.Tests/JobShopScheduling.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
</ItemGroup>

<ItemGroup>
<None Update="TestExamples\ft06.in">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestExamples\test1.in">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
7 changes: 7 additions & 0 deletions JobShopScheduling.Tests/TestExamples/ft06.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
6 6
2 1 0 3 1 6 3 7 5 3 4 6
1 8 2 5 4 10 5 10 0 10 3 4
2 5 3 4 5 8 0 9 1 1 4 7
1 5 0 5 2 5 3 3 4 8 5 9
2 9 1 3 4 5 5 4 0 3 3 1
1 3 3 3 5 9 0 10 4 4 2 1
8 changes: 7 additions & 1 deletion JobShopScheduling/JobShopGeneticAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public class JobShopGeneticAlgorithm
/// </summary>
public PlotModel PlotModel { get; set; }

/// <summary>
/// Event that will be invoked after every generation.
/// </summary>
public event Action<Generation> GenerationRan;


/// <summary>
/// Creates instance of <see cref="JobShopGeneticAlgorithm"/> from parameters.
Expand Down Expand Up @@ -132,7 +137,8 @@ private ScheduleChromosome RunOnce(LineSeries lineSeries = null)
((ScheduleChromosome)geneticAlgorithm.Population.BestChromosome).ScheduleLength.Value));
};
}


geneticAlgorithm.GenerationRan += (o, e) => GenerationRan?.Invoke(geneticAlgorithm.Population.CurrentGeneration);
geneticAlgorithm.TaskExecutor = new ParallelTaskExecutor()
{
MinThreads = 1,
Expand Down

0 comments on commit 53b8605

Please sign in to comment.