public void SetCurrentPopulation_EngineRunning_ThrowException() { Utils.RunTimedTest(() => { using (var engine = new TestGeneticSearchEngineBuilder(2, int.MaxValue, new TestPopulationManager(new double[] { 2, 2 })).Build()) { Task.Run(() => engine.Run()); while (!engine.IsRunning) { ; } engine.SetCurrentPopulation(new double[] { 3, 3 }.ToChromosomes("Converted")); if (engine.IsRunning) { Assert.Fail("Should have thrown an exception."); } else { Assert.Fail("For some reason, the engine is no longer running."); } } }); }
public void PopulationRenwalManagerGetsRightInfoTest(RunType runType) { var generation = 0; var populationRenwalManager = A.Fake <IPopulationRenwalManager>(); A.CallTo(() => populationRenwalManager.ShouldRenew(A <Population> ._, A <IEnvironment> ._, A <int> ._)).Invokes( (Population p, IEnvironment e, int g) => { Assert.AreEqual(generation, g, "Wrong generation"); foreach (var chromosome in p.GetChromosomes()) { Assert.AreEqual(generation + 1, chromosome.Evaluate(), "Wrong chromosome"); } foreach (var evaluation in p.GetEvaluations()) { Assert.AreEqual(generation + 1, evaluation, "Wrong evaluation"); } generation++; }); var populationManager = new TestPopulationManager(new[] { new double[] { 1, 1, 1 }, new double[] { 2, 2, 2 }, new double[] { 3, 3, 3 } }); using (var engine = new TestGeneticSearchEngineBuilder(POPULATION_SIZE, 3, populationManager) .AddPopulationRenwalManager(populationRenwalManager).IncludeAllHistory().Build()) { engine.Run(runType); } }
public void RenewPopulation_EngineRunning_ThrowException() { using (var engine = new TestGeneticSearchEngineBuilder(2, int.MaxValue, new TestPopulationManager(new double[] { 2, 2 })).Build()) { Task.Run(() => engine.Run()); Thread.Sleep(50); // Give the eingine some time to start engine.RenewPopulation(0.5); Assert.Fail("Should have thrown an exception by now"); } }
public void RenewIfNoImprovmentTest2(RunType runType) { var populationManager = new TestPopulationManager(new double[] { 1, 1, 1 }); populationManager.SetPopulationGenerated(new[] { new double[] { 2, 2, 2 } }); using (var engine = new TestGeneticSearchEngineBuilder(POPULATION_SIZE, 4, populationManager) .AddPopulationRenwalManager(new RenewIfNoImprovment(2, 10, 1)).IncludeAllHistory().Build()) { var result = engine.Run(runType); Assert.AreEqual(2, result.BestChromosome.Evaluate()); } }
public void SearchTimeTest(RunType runType) { var populationManager = new TestPopulationManager(new double[] { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 }); using (var engine1 = new TestGeneticSearchEngineBuilder(10, 10, populationManager).Build()) using (var engine2 = new TestGeneticSearchEngineBuilder(10, 1000, populationManager).Build()) { var result1 = engine1.Run(runType); var result2 = engine2.Run(runType); Assert.IsTrue(result2.SearchTime > result1.SearchTime, $"engine1 ran for less time than engine2 (engine1 = {result1.SearchTime}; engine2 = {result2.SearchTime})"); } }
public void StopAtConvergenceTest(RunType runType) { var populationManager = new TestPopulationManager(new[] { new double[] { 1, 2, 1 }, new double[] { 2, 6, 2 }, new double[] { 2, 2.5, 2 } }); using (var engine = new TestGeneticSearchEngineBuilder(POPULATION_SIZE, MAX_GENERATIONS, populationManager) .AddStopManager(new StopAtConvergence(0.5)).IncludeAllHistory().Build()) { var result = engine.Run(runType); Assert.AreEqual(3, result.Generations); } }
public void HistoryIsRightTest(RunType runType) { var population = new[] { new double[] { 1, 1, 1 }, new double[] { 2, 2, 2 }, new double[] { 2, 3, 2 } }; var populationManager = new TestPopulationManager(population); using (var searchEngine = new TestGeneticSearchEngineBuilder(population[0].Length, population.Length - 1, populationManager) .IncludeAllHistory().Build()) { var result = searchEngine.Run(runType); result.History.AssertHasEvaluation(population); } }
public void RenewAtDifferenceBetweenAverageAndMaximumFitnessTest(RunType runType) { var populationManager = new TestPopulationManager(new double[] { 1, 2, 1 }); populationManager.SetPopulationGenerated(new[] { new double[] { 2, 5, 2 }, new double[] { 6, 6, 6 }, new double[] { 7, 7, 7 } }); using (var engine = new TestGeneticSearchEngineBuilder(POPULATION_SIZE, 4, populationManager) .AddPopulationRenwalManager(new RenewAtDifferenceBetweenAverageAndMaximumFitness(1, 1)) .IncludeAllHistory().Build()) { var result = engine.Run(runType); Assert.AreEqual(5, result.BestChromosome.Evaluate()); } }
public void RenewOnlySomeChromosomes(RunType runType) { var populationManager = new TestPopulationManager(new double[] { 4, 4, 4 }); populationManager.SetPopulationGenerated(new[] { new double[] { 3, 3, 3 }, new double[] { 2, 2, 2 }, new double[] { 1, 1, 1 } }); using (var engine = new TestGeneticSearchEngineBuilder(POPULATION_SIZE, 4, populationManager) .AddPopulationRenwalManager(new RenewAtConvergence(0.9, 0.5)) .IncludeAllHistory().Build()) { var result = engine.Run(runType); Assert.AreEqual(4, result.BestChromosome.Evaluate()); } }
public void GetCurrentPopulation_EngineRunning_ThrowException() { using (var engine = new TestGeneticSearchEngineBuilder(2, int.MaxValue, new TestPopulationManager(new double[] { 2, 2 })).Build()) { Task.Run(() => engine.Run()); while (!engine.IsRunning) { ; } engine.GetCurrentPopulation(); Assert.Fail("Should have thrown an exception by now"); } }
public void StopIfNoImprovmentTest2(RunType runType) { Utils.RunTimedTest(() => { var populationManager = new TestPopulationManager(new[] { new double[] { 1, 1, 1 }, new double[] { 2, 2.5, 2 }, new double[] { 3, 3, 3 } }); using (var engine = new TestGeneticSearchEngineBuilder(POPULATION_SIZE, MAX_GENERATIONS, populationManager) .AddStopManager(new StopIfNoImprovment(1, 0.9)).IncludeAllHistory().Build()) { var result = engine.Run(runType); Assert.AreEqual(3, result.Generations); } }); }
public void SearchTimeRunTest() { var populationManager = new TestPopulationManager(new double[] { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 }); using (var engine = new TestGeneticSearchEngineBuilder(10, 50, populationManager).Build()) { var stopwatch = new Stopwatch(); stopwatch.Start(); var result = engine.Run(); stopwatch.Stop(); Assert.IsTrue(stopwatch.Elapsed.TotalMilliseconds * 0.90 < stopwatch.Elapsed.TotalMilliseconds, $"time is too short. {nameof(stopwatch.Elapsed)} = {stopwatch.Elapsed}; {nameof(result.SearchTime)} = {result.SearchTime.TotalMilliseconds}"); Assert.IsTrue(stopwatch.Elapsed.TotalMilliseconds * 1.1 > stopwatch.Elapsed.TotalMilliseconds, $"time is too long. {nameof(stopwatch.Elapsed)} = {stopwatch.Elapsed}; {nameof(result.SearchTime)} = {result.SearchTime.TotalMilliseconds}"); } }
public void SetCurrentPopulation_WrongNumberOfChromosomes_ThrowException() { Utils.RunTimedTest(() => { using (var engine = new TestGeneticSearchEngineBuilder(2, int.MaxValue, new TestPopulationManager(new double[] { 2, 2 })).Build()) { Task.Run(() => engine.Run()); while (!engine.IsRunning) { ; } engine.SetCurrentPopulation(new double[] { 3, 3, 3 }.ToChromosomes("Converted")); Assert.Fail("Should have thrown an exception by now"); } }); }
public void ElitismTest(double eilentPrecentage, RunType runType) { var populationSize = 10; var populationManager = new TestPopulationManager( new double[] { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 }, c => c.Evaluate() - 1); using (var engine = new TestGeneticSearchEngineBuilder(populationSize, 10, populationManager) .SetElitePercentage(eilentPrecentage).IncludeAllHistory().Build()) { var result = engine.Run(runType); var maxEvaluation = result.BestChromosome.Evaluate(); Assert.AreEqual(10, maxEvaluation); Assert.AreEqual(eilentPrecentage * populationSize, result.Population.GetChromosomes().Count(c => c.Evaluate() == maxEvaluation)); } }
public void CancellationTokenTest(RunType runType) { var cancellationSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(200)); var populationManager = new TestPopulationManager( new double[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }); using (var engine = new TestGeneticSearchEngineBuilder(25, int.MaxValue, populationManager) .SetCancellationToken(cancellationSource.Token).Build()) { Task.Run(() => { Thread.Sleep(1000); Assert.Fail("We should have finished running by now"); }); engine.Run(runType); } }
public void OnNewGenerationEventTest(RunType runType) { var population = new[] { new double[] { 1, 1, 1 }, new double[] { 2, 2, 2 }, new double[] { 2, 3, 2 } }; var populationManager = new TestPopulationManager(population); using (var searchEngine = new TestGeneticSearchEngineBuilder(population[0].Length, population.Length - 1, populationManager) .Build()) { var actualPopulation = new List <IChromosome[]>(); var actualEvaluations = new List <double[]>(); searchEngine.OnNewGeneration += (p, e) => { actualEvaluations.Add(p.GetEvaluations()); actualPopulation.Add(p.GetChromosomes()); }; searchEngine.Run(runType); actualEvaluations.AssertAreTheSame(population); actualPopulation.AssertHasEvaluation(population); } }