コード例 #1
0
        private void TestMutation(int firstRandomValue, int secondRandomValue, int[] expectedValues)
        {
            ListShiftMutationOperator op = new ListShiftMutationOperator
            {
                MutationRate = 1
            };

            RandomNumberService.Instance = new FakeRandomNumberService(firstRandomValue, secondRandomValue);

            IntegerListEntity entity = new IntegerListEntity
            {
                MinimumStartingLength = 5,
                MaximumStartingLength = 5,
            };

            entity.Initialize(new MockGeneticAlgorithm());
            for (int i = 0; i < 5; i++)
            {
                entity[i] = i;
            }

            IntegerListEntity result = (IntegerListEntity)op.Mutate(entity);

            Assert.Equal(expectedValues, result);
        }
コード例 #2
0
        public void UniformIntegerMutationOperatorTest_Mutate()
        {
            MockGeneticAlgorithm algorithm = new MockGeneticAlgorithm
            {
                PopulationSeed    = new MockPopulation(),
                SelectionOperator = new MockSelectionOperator(),
                FitnessEvaluator  = new MockFitnessEvaluator(),
                GeneticEntitySeed = new IntegerListEntity
                {
                    MinimumStartingLength = 4,
                    MaximumStartingLength = 4,
                    MaxElementValue       = 2,
                    MinElementValue       = 1
                },
                MutationOperator = new UniformIntegerMutationOperator
                {
                    MutationRate = 1
                }
            };
            UniformIntegerMutationOperator op = new UniformIntegerMutationOperator {
                MutationRate = 1
            };

            op.Initialize(algorithm);
            IntegerListEntity entity = new IntegerListEntity {
                MinimumStartingLength = 4, MaximumStartingLength = 4, MaxElementValue = 2, MinElementValue = 1
            };

            entity.Age = 10;
            entity.Initialize(algorithm);
            entity[0] = 1;
            entity[1] = 1;
            entity[2] = 2;
            entity[3] = 1;
            GeneticEntity mutant = op.Mutate(entity);

            Assert.Equal("2, 2, 1, 2", mutant.Representation);
            Assert.Equal(0, mutant.Age);
        }