Пример #1
0
        public async Task ElitismStrategy_GetElitistGeneticEntities()
        {
            double           elitismRatio         = .1;
            int              totalGeneticEntities = 100;
            GeneticAlgorithm algorithm            = GetGeneticAlgorithm(elitismRatio);
            await algorithm.InitializeAsync();

            MockPopulation population = new MockPopulation();

            population.Initialize(algorithm);
            for (int i = 0; i < totalGeneticEntities; i++)
            {
                MockEntity entity = new MockEntity();
                entity.Initialize(algorithm);
                population.Entities.Add(entity);
            }
            algorithm.Environment.Populations.Add(population);
            MockElitismStrategy strategy = (MockElitismStrategy)algorithm.ElitismStrategy;

            strategy.Initialize(algorithm);

            IList <GeneticEntity> geneticEntities = strategy.GetEliteEntities(population);

            Assert.Equal(Convert.ToInt32(Math.Round(elitismRatio * totalGeneticEntities)), geneticEntities.Count);
        }
Пример #2
0
        public void BoltzmannSelectionOperator_Select()
        {
            double           initialTemp = 10;
            GeneticAlgorithm algorithm   = GetMockAlgorithm(initialTemp);

            FakeBoltzmannSelectionOperator op = (FakeBoltzmannSelectionOperator)algorithm.SelectionOperator;

            op.Initialize(algorithm);
            MockPopulation population = new MockPopulation();

            population.Initialize(algorithm);

            MockEntity entity1 = new MockEntity();

            entity1.Initialize(algorithm);
            population.Entities.Add(entity1);

            MockEntity entity2 = new MockEntity();

            entity2.Initialize(algorithm);
            population.Entities.Add(entity2);

            IEnumerable <GeneticEntity> entities = op.SelectEntities(2, population);

            Assert.NotNull(entities);
            Assert.True(entities.Count() > 0, "An entity should have been selected.");
        }
Пример #3
0
        public async Task Population_Initialize_Async()
        {
            GeneticAlgorithm algorithm = new MockGeneticAlgorithm
            {
                FitnessEvaluator  = new MockFitnessEvaluator(),
                SelectionOperator = new MockSelectionOperator(),
                GeneticEntitySeed = new MockEntity
                {
                    TestProperty = 3
                },
                PopulationSeed = new MockPopulation
                {
                    MinimumPopulationSize = 10
                }
            };

            algorithm.GeneticEntitySeed.Initialize(algorithm);
            MockPopulation population = new MockPopulation();

            population.Initialize(algorithm);
            await population.InitializeAsync();

            Assert.Equal(algorithm.PopulationSeed.MinimumPopulationSize, population.Entities.Count);
            Assert.Equal(3, ((MockEntity)population.Entities[0]).TestProperty);
        }
Пример #4
0
        public void SelectionOperator_Select_EmptyPopulation()
        {
            GeneticAlgorithm      algorithm = GetAlgorithm();
            MockSelectionOperator op        = new MockSelectionOperator();

            op.Initialize(algorithm);
            MockPopulation population = new MockPopulation();

            population.Initialize(algorithm);
            Assert.Throws <ArgumentException>(() => op.SelectEntities(1, population));
        }
Пример #5
0
        public void ElitismStrategy_GetElitistGeneticEntities_EmptyPopulation()
        {
            GeneticAlgorithm    algorithm = GetGeneticAlgorithm(.1);
            MockElitismStrategy strategy  = new MockElitismStrategy();

            strategy.Initialize(algorithm);
            MockPopulation pop = new MockPopulation();

            pop.Initialize(algorithm);
            Assert.Throws <ArgumentException>(() => strategy.GetEliteEntities(pop));
        }
Пример #6
0
        public void FitnessScalingStrategy_Scale_EmptyPopulation()
        {
            GeneticAlgorithm            algorithm = GetAlgorithm();
            FakeFitnessScalingStrategy2 strategy  = new FakeFitnessScalingStrategy2();

            strategy.Initialize(algorithm);
            MockPopulation population = new MockPopulation();

            population.Initialize(algorithm);
            Assert.Throws <ArgumentException>(() => strategy.Scale(population));
        }
Пример #7
0
        public void Population_Ctor()
        {
            GeneticAlgorithm algorithm = new MockGeneticAlgorithm
            {
                SelectionOperator = new MockSelectionOperator(),
                FitnessEvaluator  = new MockFitnessEvaluator(),
                GeneticEntitySeed = new MockEntity(),
                PopulationSeed    = new MockPopulation(),
            };
            Population population = new MockPopulation();

            population.Initialize(algorithm);
            PrivateObject accessor = new PrivateObject(population);

            Assert.Same(algorithm, accessor.GetProperty("Algorithm"));
        }
Пример #8
0
        public void BoltzmannSelectionOperator_Select_Overflow()
        {
            double initialTemp = .0000001;
            MockGeneticAlgorithm           algorithm = GetMockAlgorithm(initialTemp);
            FakeBoltzmannSelectionOperator op        = new FakeBoltzmannSelectionOperator();

            op.Initialize(algorithm);
            MockPopulation population = new MockPopulation();

            population.Initialize(algorithm);
            MockEntity entity = new MockEntity();

            entity.Initialize(algorithm);
            entity.ScaledFitnessValue = 1;
            population.Entities.Add(entity);
            Assert.Throws <OverflowException>(() => op.SelectEntities(1, population));
        }
Пример #9
0
        public void FitnessScalingStrategy_Scale()
        {
            GeneticAlgorithm            algorithm = GetAlgorithm();
            FakeFitnessScalingStrategy2 strategy  = new FakeFitnessScalingStrategy2();

            strategy.Initialize(algorithm);
            MockPopulation population = new MockPopulation();

            population.Initialize(algorithm);
            MockEntity entity = new MockEntity();

            entity.Initialize(algorithm);
            population.Entities.Add(entity);
            strategy.Scale(population);

            Assert.True(strategy.OnScaleCalled, "ScaleCore was not called.");
        }
Пример #10
0
        private static MockPopulation GetPopulation(GeneticAlgorithm algorithm)
        {
            MockPopulation population = new MockPopulation();

            population.Initialize(algorithm);
            MockEntity entity1 = new MockEntity();

            entity1.Initialize(algorithm);
            entity1.Identifier = "5";
            MockEntity entity2 = new MockEntity();

            entity2.Initialize(algorithm);
            entity2.Identifier = "2";
            population.Entities.Add(entity1);
            population.Entities.Add(entity2);
            return(population);
        }
Пример #11
0
        public void SelectionOperator_Select()
        {
            GeneticAlgorithm      algorithm = GetAlgorithm();
            MockSelectionOperator op        = new MockSelectionOperator();

            op.Initialize(algorithm);
            MockPopulation population = new MockPopulation();

            population.Initialize(algorithm);
            GeneticEntity entity1 = new MockEntity();

            entity1.Initialize(algorithm);
            GeneticEntity entity2 = new MockEntity();

            entity2.Initialize(algorithm);
            population.Entities.Add(entity1);
            population.Entities.Add(entity2);
            IList <GeneticEntity> selectedEntities = op.SelectEntities(1, population);

            Assert.Same(entity1, selectedEntities[0]);
            Assert.Equal(1, op.DoSelectCallCount);
        }
Пример #12
0
        public void Population_Ctor_NullAlgorithm()
        {
            MockPopulation population = new MockPopulation();

            Assert.Throws <ArgumentNullException>(() => population.Initialize(null));
        }
Пример #13
0
        private static async Task TestEvaluateFitnessAsync(bool useScaling, bool useMetric)
        {
            GeneticAlgorithm algorithm = new MockGeneticAlgorithm
            {
                SelectionOperator = new MockSelectionOperator(),
                GeneticEntitySeed = new MockEntity(),
                PopulationSeed    = new MockPopulation(),
                FitnessEvaluator  = new MockFitnessEvaluator(),
            };

            if (useScaling)
            {
                algorithm.FitnessScalingStrategy = new FakeFitnessScalingStrategy();
            }

            if (useMetric)
            {
                algorithm.Metrics.Add(new MockMetric());
            }

            algorithm.FitnessEvaluator = new MockFitnessEvaluator();
            algorithm.FitnessEvaluator.Initialize(algorithm);
            if (useScaling)
            {
                algorithm.FitnessScalingStrategy = new FakeFitnessScalingStrategy();
                algorithm.FitnessScalingStrategy.Initialize(algorithm);
            }

            MockPopulation population = new MockPopulation();

            population.Initialize(algorithm);
            MockEntity entity1 = new MockEntity();

            entity1.Initialize(algorithm);
            entity1.Identifier = "123";
            population.Entities.Add(entity1);

            MockEntity entity2 = new MockEntity();

            entity2.Initialize(algorithm);
            entity2.Identifier = "456";
            population.Entities.Add(entity2);

            await population.EvaluateFitnessAsync();

            Assert.Equal((double)123, entity1.RawFitnessValue);
            Assert.Equal((double)456, entity2.RawFitnessValue);

            if (useScaling)
            {
                Assert.Equal(entity1.RawFitnessValue - 1, entity1.ScaledFitnessValue);
                Assert.Equal(entity2.RawFitnessValue - 1, entity2.ScaledFitnessValue);
            }
            else
            {
                Assert.Equal(entity1.RawFitnessValue, entity1.ScaledFitnessValue);
                Assert.Equal(entity2.RawFitnessValue, entity2.ScaledFitnessValue);
            }

            if (!useMetric && !useScaling)
            {
                Assert.False(population.RawMax.HasValue, "RawMax not set correctly.");
                Assert.False(population.RawMin.HasValue, "RawMax not set correctly.");
                Assert.False(population.RawMean.HasValue, "RawMean not set correctly.");
                Assert.False(population.RawStandardDeviation.HasValue, "RawStandardDeviation not set correctly.");
            }
            else
            {
                Assert.Equal(entity2.RawFitnessValue, population.RawMax);
                Assert.Equal(entity1.RawFitnessValue, population.RawMin);
                Assert.Equal((entity1.RawFitnessValue + entity2.RawFitnessValue) / 2, population.RawMean);
            }
        }