private GeneticSearchEngineBuilder CreateEngineBuilder(TestPopulationManager populationManager, int generations = int.MaxValue)
        {
            var populationConverter = A.Fake <IPopulationConverter>();

            A.CallTo(() => populationConverter.AddGeneration(A <Population> ._))
            .Invokes((Population population) => populationUpdatedForPopulationConverter.Save(population));
            A.CallTo(() => populationConverter.ConvertPopulation(A <IChromosome[]> ._, A <int> ._, A <IEnvironment> ._))
            .ReturnsLazily((IChromosome[] c, int g, IEnvironment e) => c);
            var stopManager = A.Fake <IStopManager>();

            A.CallTo(() => stopManager.AddGeneration(A <Population> ._))
            .Invokes((Population p) => populationUpdatedForStopManager.Save(p));
            var populationRenwalManager = A.Fake <IPopulationRenwalManager>();

            A.CallTo(() => populationRenwalManager.AddGeneration(A <Population> ._))
            .Invokes((Population p) => populationUpdatedForRenewalManager.Save(p));
            var mutationManager = A.Fake <IMutationProbabilityManager>();

            A.CallTo(() => mutationManager.AddGeneration(A <Population> ._))
            .Invokes((Population p) => populationUpdatedForMutationManager.Save(p));
            var builder = new TestGeneticSearchEngineBuilder(2, generations, populationManager)
                          .AddStopManager(stopManager)
                          .AddPopulationRenwalManager(populationRenwalManager).SetCustomMutationProbabilityManager(mutationManager)
                          .AddPopulationConverter(populationConverter);

            return(builder);
        }
        public void GetCurrentPopulation_CheckPopulationIsRight(bool includeHistory)
        {
            var populationManager = new TestPopulationManager(new double[] { 2, 2 });

            populationManager.SetPopulationGenerated(new[] { new double[] { 3, 3 } });
            var engineBuilder =
                new TestGeneticSearchEngineBuilder(2, int.MaxValue, populationManager);

            if (includeHistory)
            {
                engineBuilder.IncludeAllHistory();
            }

            var engine = engineBuilder.Build();

            var result1 = engine.Next();
            var result2 = engine.GetCurrentPopulation();

            Assertions.AssertAreTheSame(result1, result2);

            result1 = engine.Next();
            result2 = engine.GetCurrentPopulation();

            Assertions.AssertAreTheSame(result1, result2);
        }
Beispiel #3
0
        public void ConvertPopulationViaManager_CheckWeGetTheRightPopulationAndGeneration()
        {
            var generation            = 0;
            var testPopulationManager = new TestPopulationManager(new double[] { 1, 1, 1, 1, 1 });
            var populationConverter   = A.Fake <IPopulationConverter>();

            A.CallTo(() => populationConverter.ConvertPopulation(A <IChromosome[]> ._, A <int> ._, A <IEnvironment> ._))
            .ReturnsLazily((IChromosome[] c, int g, IEnvironment e) =>
            {
                Assert.AreEqual(generation, g, "We got the wrong generation");
                foreach (var chromosome in c)
                {
                    Assert.AreEqual(1, chromosome.Evaluate(), "Got wrong chromosome");
                }
                generation++;
                return(c);
            });
            var engine = new TestGeneticSearchEngineBuilder(5, 10, testPopulationManager)
                         .AddPopulationConverter(populationConverter).Build();

            for (int i = 0; i < 5; i++)
            {
                engine.Next();
            }
        }
Beispiel #4
0
        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 BestChromosome()
        {
            var populationManager = new TestPopulationManager(new double[] { 1, 10, 14, 7, 8, 13, 11, 1, 6, 6 });
            var engine            = new TestGeneticSearchEngineBuilder(10, 10, populationManager).Build();

            var result = engine.Next();

            Assert.AreEqual(14, result.BestChromosome.Evaluate());
        }
        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);
            }
        }
Beispiel #7
0
        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 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 PopulationIsUpdatedOnNewGeneration(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 engine = CreateEngineBuilder(populationManager, population.Length).Build())
            {
                engine.OnNewGeneration += populationUpdatedOnEvent.Save;

                engine.Run(runType);

                AssertManagersUpdated(population);
            }
        }
        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 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);
                }
            });
        }
Beispiel #13
0
        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());
            }
        }
Beispiel #14
0
        public void RenewPercantagePopulation_PercentageRenewed(double percent)
        {
            var populationManager = new TestPopulationManager(new double[] { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 });

            populationManager.SetPopulationGenerated(new[] { new double[] { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 } });
            var engine =
                new TestGeneticSearchEngineBuilder(10, int.MaxValue, populationManager).Build();

            engine.Next();

            var result        = engine.RenewPopulation(percent);
            var threeCounters = result.Population.Count(chromosme => chromosme.Evaluation == 3);

            Assert.AreEqual(percent * 10, threeCounters);
        }
Beispiel #15
0
        public void ResultGetsRightEnvironment()
        {
            var populationManager = new TestPopulationManager(new double[] { 0, 0 }, c => c.Evaluate() + 1);
            var engine            = new TestGeneticSearchEngineBuilder(2, 10, populationManager).SetCustomChromosomeEvaluator(A.Fake <IChromosomeEvaluator>()).Build();

            int i;

            for (i = 0; i < 4; i++)
            {
                var result = engine.Next();
                AssertIsRightEnvironment(result.Environment, i);
            }

            Assert.AreNotEqual(0, i, "SetEnvierment was never called");
        }
Beispiel #16
0
        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());
            }
        }
Beispiel #17
0
        public void ConvertPopulation_PopulationConverted()
        {
            var newPopulationEvaluation = new double[] { 2, 2, 2 };
            var populationManager       = new TestPopulationManager(new double[] { 1, 1, 1 });
            var engine = new TestGeneticSearchEngineBuilder(3, 10, populationManager).Build();

            engine.Next();

            var newPopulation = engine.SetCurrentPopulation(newPopulationEvaluation.ToChromosomes("Converted"));

            newPopulation.Population.GetChromosomes().AssertHasEvaluation(newPopulationEvaluation);

            newPopulation = engine.GetCurrentPopulation();
            newPopulation.Population.GetChromosomes().AssertHasEvaluation(newPopulationEvaluation);
        }
Beispiel #18
0
        public void RenewPopulation_CheckPopulationRenewedSentToNextGeneration()
        {
            var populationManager = new TestPopulationManager(new double[] { 2, 2 }, c => c.Evaluate() + 1);

            populationManager.SetPopulationGenerated(new[] { new double[] { 3, 3 } });
            var engine =
                new TestGeneticSearchEngineBuilder(2, int.MaxValue, populationManager).Build();

            engine.Next();
            engine.RenewPopulation(1);
            var result = engine.Next();

            foreach (var chromosme in result.Population)
            {
                Assert.AreEqual(4, chromosme.Evaluation);
            }
        }
Beispiel #19
0
        public void ConvertPopulationViaManager_CheckPopulationConverted()
        {
            var testPopulationManager = new TestPopulationManager(new double[] { 1, 1, 1, 1, 1 });
            var populationConverter   = A.Fake <IPopulationConverter>();
            var convertedPopulation   = new double[] { 2, 2, 2, 2, 2 };

            A.CallTo(() => populationConverter.ConvertPopulation(A <IChromosome[]> ._, A <int> ._, A <IEnvironment> ._))
            .Returns(convertedPopulation.ToChromosomes("Converted Chromosomes"));
            var engine = new TestGeneticSearchEngineBuilder(convertedPopulation.Length, 10, testPopulationManager)
                         .AddPopulationConverter(populationConverter).Build();

            var result = engine.Next();

            for (var i = 0; i < result.Population.GetChromosomes().Length; i++)
            {
                Assert.AreEqual(convertedPopulation[i], result.Population.GetChromosomes()[i].Evaluate(), "Wrong chromosome");
            }
        }
        public void ConvertPopulation_NewPopulationUpdated()
        {
            Utils.RunTimedTest(() =>
            {
                var initialPopulation = new double[] { 2, 2 };
                var newPopulation     = new double[] { 3, 3 };
                var populationManager = new TestPopulationManager(initialPopulation);

                var engine              = CreateEngineBuilder(populationManager).Build();
                engine.OnNewGeneration += populationUpdatedOnEvent.Save;

                engine.Next();

                engine.SetCurrentPopulation(newPopulation.ToChromosomes("Converted"));

                AssertManagersUpdated(new[] { initialPopulation, newPopulation });
            });
        }
        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));
            }
        }
Beispiel #22
0
        public void OnNewGenerationEventGetsRightEnvironment()
        {
            var counter           = 0;
            var populationManager = new TestPopulationManager(new double[] { 0, 0 }, c => c.Evaluate() + 1);
            var engine            = new TestGeneticSearchEngineBuilder(2, 10, populationManager).SetCustomChromosomeEvaluator(A.Fake <IChromosomeEvaluator>()).Build();

            engine.OnNewGeneration += (p, en) =>
            {
                AssertIsRightEnvironment(en, counter);
                counter++;
            };

            engine.Next();
            engine.Next();
            engine.Next();

            Assert.AreNotEqual(0, counter, "SetEnvierment was never called");
        }
        public void RenewPopulation_RenewedPopulationUpdated()
        {
            var initialPopulation = new double[] { 2, 2 };
            var renewedPopulation = new double[] { 3, 3 };
            var populationManager = new TestPopulationManager(initialPopulation);

            populationManager.SetPopulationGenerated(new[] { renewedPopulation });

            var engine = CreateEngineBuilder(populationManager).Build();

            engine.OnNewGeneration += populationUpdatedOnEvent.Save;

            engine.Next();

            engine.RenewPopulation(1);

            AssertManagersUpdated(new [] { initialPopulation, renewedPopulation });
        }
        public void IsCompletedTests()
        {
            var generations       = 30;
            var populationManager = new TestPopulationManager(new double[] { 1, 2, 3 });
            var searchEngine      =
                new TestGeneticSearchEngineBuilder(3, generations, populationManager)
                .IncludeAllHistory().Build();

            for (int i = 0; i < generations - 1; i++)
            {
                var result = searchEngine.Next();
                Assert.IsFalse(result.IsCompleted, "Shouldn't have finished yet");
            }

            var finalResult = searchEngine.Next();

            Assert.IsTrue(finalResult.IsCompleted);
        }
        public void ConvertPopulationViaManager_NewPopulationUpdated()
        {
            var initialPopulation   = new double[] { 2, 2 };
            var newPopulation       = new double[] { 3, 3 };
            var populationManager   = new TestPopulationManager(initialPopulation);
            var populationConverter = A.Fake <IPopulationConverter>();

            A.CallTo(() => populationConverter.ConvertPopulation(A <IChromosome[]> ._, A <int> ._, A <IEnvironment> ._))
            .Returns(newPopulation.ToChromosomes("Converted"));
            A.CallTo(() => populationConverter.AddGeneration(A <Population> ._))
            .Invokes((Population p) => populationUpdatedForPopulationConverter.Save(p, null));

            var engine = CreateEngineBuilder(populationManager).AddPopulationConverter(populationConverter).Build();

            engine.OnNewGeneration += populationUpdatedOnEvent.Save;

            engine.Next();
            AssertManagersUpdated(newPopulation);
        }
        public void HistoryExistTest(bool includeHistory, RunType runType)
        {
            var populationManager = new TestPopulationManager(new double[] { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 });
            var enigneBuilder     = new TestGeneticSearchEngineBuilder(10, 2, populationManager);

            using (var searchEngine = includeHistory ? enigneBuilder.IncludeAllHistory().Build() : enigneBuilder.Build())
            {
                var result = searchEngine.Run(runType);

                if (includeHistory)
                {
                    Assert.AreEqual(2, result.History.Count, "There should have been history");
                }
                else
                {
                    Assert.AreEqual(0, result.History.Count, "There shouldn't be any history");
                }
            }
        }
Beispiel #27
0
        public void ChromosomeEvaluatorGetRightEnvironment()
        {
            var counter             = 0;
            var chromosomeEvaluator = A.Fake <IChromosomeEvaluator>();

            A.CallTo(() => chromosomeEvaluator.SetEnvierment(A <IEnvironment> ._)).Invokes((IEnvironment e) =>
            {
                AssertIsRightEnvironment(e, counter);
                counter++;
            });
            var populationManager = new TestPopulationManager(new double[] { 0, 0 }, c => c.Evaluate() + 1);
            var engine            = new TestGeneticSearchEngineBuilder(2, 10, populationManager).SetCustomChromosomeEvaluator(chromosomeEvaluator).Build();

            engine.Next();
            engine.Next();
            engine.Next();

            Assert.AreNotEqual(0, counter, "SetEnvierment was never called");
        }
        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);
            }
        }
Beispiel #29
0
        public void MultipleConvertPopulationManagers_ManagersCalledAndInOrder()
        {
            var testPopulationManager = new TestPopulationManager(new double[] { 1, 1, 1, 1, 1 });
            var populationConverter1  = GetOrderedPopulationManager(1);
            var populationConverter2  = GetOrderedPopulationManager(2);
            var populationConverter3  = GetOrderedPopulationManager(3);
            var engine = new TestGeneticSearchEngineBuilder(5, 10, testPopulationManager)
                         .AddPopulationConverter(populationConverter1)
                         .AddPopulationConverter(populationConverter2)
                         .AddPopulationConverter(populationConverter3)
                         .Build();

            var result = engine.Next();

            foreach (var chromosome in result.Population.GetChromosomes())
            {
                Assert.AreEqual(4, chromosome.Evaluate(), "Ended up with wrong chromosome");
            }
        }
Beispiel #30
0
        public void StopManagerGetsRightEnvironment()
        {
            var counter           = 0;
            var populationManager = new TestPopulationManager(new double[] { 0, 0 }, c => c.Evaluate() + 1);
            var stopManager       = A.Fake <IStopManager>();

            A.CallTo(() => stopManager.ShouldStop(A <Population> ._, A <IEnvironment> ._, A <int> ._)).Invokes((Population p, IEnvironment e, int g) =>
            {
                AssertIsRightEnvironment(e, counter);
                counter++;
            });
            var engine = new TestGeneticSearchEngineBuilder(2, 10, populationManager)
                         .SetEnvironment(new DefaultEnvironment()).AddStopManager(stopManager).Build();

            engine.Next();
            engine.Next();
            engine.Next();

            Assert.AreNotEqual(0, counter, "SetEnvierment was never called");
        }