Ejemplo n.º 1
0
        private static void TestIsValid(int crossoverPointCount, bool requiresUniqueElementValues, bool expectedIsValid)
        {
            MultiPointCrossoverOperator op = new MultiPointCrossoverOperator
            {
                CrossoverPointCount = crossoverPointCount
            };
            MockGeneticAlgorithm algorithm = new MockGeneticAlgorithm
            {
                GeneticEntitySeed = new IntegerListEntity
                {
                    RequiresUniqueElementValues = requiresUniqueElementValues
                }
            };

            op.Initialize(algorithm);

            MultiPointCrossoverOperatorCrossoverPointValidator validator =
                new MultiPointCrossoverOperatorCrossoverPointValidator();
            bool result = validator.IsValid(op, out string errorMessage);

            Assert.Equal(expectedIsValid, result);
            if (expectedIsValid)
            {
                Assert.Null(errorMessage);
            }
            else
            {
                Assert.NotNull(errorMessage);
            }
        }
Ejemplo n.º 2
0
        public void MultiPointCrossoverOperator_GenerateCrossover_TwoPointsDifferentLength_UniqueValues()
        {
            MultiPointCrossoverOperator op = new MultiPointCrossoverOperator
            {
                CrossoverPointCount = 2,
                CrossoverRate       = 1
            };
            MockGeneticAlgorithm algorithm = new MockGeneticAlgorithm
            {
                GeneticEntitySeed = new TestEntity
                {
                    RequiresUniqueElementValues = true
                }
            };

            op.Initialize(algorithm);

            TestEntity entity1 = new TestEntity();

            entity1.InnerList.AddRange(new object[] { 0, 1, 2, 3 });

            TestEntity entity2 = new TestEntity();

            entity2.InnerList.AddRange(new object[] { 3, 5, 4, 1, 6, 2, 0 });

            List <GeneticEntity> parents = new List <GeneticEntity>
            {
                entity1,
                entity2
            };

            RandomNumberService.Instance = new FakeRandomNumberService(4,
                                                                       new Queue <int>(new int[] { 2, 3 }));

            PrivateObject accessor             = new PrivateObject(op);
            IEnumerable <GeneticEntity> result = (IEnumerable <GeneticEntity>)accessor.Invoke(
                "GenerateCrossover", parents);

            Assert.Equal(2, result.Count());
            TestEntity resultEntity1 = (TestEntity)result.ElementAt(0);
            TestEntity resultEntity2 = (TestEntity)result.ElementAt(1);

            Assert.Equal(new int[] { 0, 1, 4, 3 }, (IEnumerable)resultEntity1);
            Assert.Equal(new int[] { 3, 5, 2, 1, 6, 4, 0 }, (IEnumerable)resultEntity2);
        }
Ejemplo n.º 3
0
        public void MultiPointCrossoverOperator_Validation()
        {
            MultiPointCrossoverOperator op = new MultiPointCrossoverOperator
            {
                CrossoverPointCount = 3,
            };
            MockGeneticAlgorithm algorithm = new MockGeneticAlgorithm
            {
                GeneticEntitySeed = new TestEntity
                {
                    RequiresUniqueElementValues = true
                }
            };

            op.Initialize(algorithm);

            Assert.Throws <ValidationException>(() => op.Validate());
        }
Ejemplo n.º 4
0
        public void MultiPointCrossoverOperator_GenerateCrossover_ThreePointsSameLength()
        {
            MultiPointCrossoverOperator op = new MultiPointCrossoverOperator
            {
                CrossoverPointCount = 3,
                CrossoverRate       = 1
            };
            MockGeneticAlgorithm algorithm = new MockGeneticAlgorithm
            {
                GeneticEntitySeed = new TestEntity()
            };

            op.Initialize(algorithm);

            TestEntity entity1 = new TestEntity();

            entity1.InnerList.AddRange(new object[] { 0, 1, 2, 3, 4 });

            TestEntity entity2 = new TestEntity();

            entity2.InnerList.AddRange(new object[] { 4, 2, 1, 0, 0 });

            List <GeneticEntity> parents = new List <GeneticEntity>
            {
                entity1,
                entity2
            };

            RandomNumberService.Instance = new FakeRandomNumberService(5,
                                                                       new Queue <int>(new int[] { 0, 3, 4 }));

            PrivateObject accessor             = new PrivateObject(op);
            IEnumerable <GeneticEntity> result = (IEnumerable <GeneticEntity>)accessor.Invoke(
                "GenerateCrossover", parents);

            Assert.Equal(2, result.Count());
            TestEntity resultEntity1 = (TestEntity)result.ElementAt(0);
            TestEntity resultEntity2 = (TestEntity)result.ElementAt(1);

            Assert.Equal(new int[] { 4, 2, 1, 3, 0 }, (IEnumerable)resultEntity1);
            Assert.Equal(new int[] { 0, 1, 2, 0, 4 }, (IEnumerable)resultEntity2);
        }