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 #2
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());
            }
        }
Beispiel #3
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 #4
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 #5
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 #6
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);
            }
        }
        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 RenewPopulationViaManager_RenewedPopulationUpdated()
        {
            CleanChromosomesAndEvaluations();

            var renewManager = A.Fake <IPopulationRenwalManager>();

            A.CallTo(() => renewManager.ShouldRenew(A <Population> ._, A <IEnvironment> ._, A <int> ._)).Returns(1);
            var renewedPopulation = new double[] { 3, 3 };
            var initialPopulation = new double[] { 2, 2 };
            var populationManager = new TestPopulationManager(initialPopulation);

            populationManager.SetPopulationGenerated(new[] { renewedPopulation });
            var builder = CreateEngineBuilder(populationManager);

            builder.AddPopulationRenwalManager(renewManager);
            var engine = builder.Build();

            engine.OnNewGeneration += populationUpdatedOnEvent.Save;

            engine.Next();

            AssertManagersUpdated(renewedPopulation);
        }