示例#1
0
        public void MutateMayChangeEveryParameter()
        {
            // Create genome builder with a mutation rate of 1.
            var configuration = new AlgorithmTunerConfiguration.AlgorithmTunerConfigurationBuilder()
                                .SetMutationRate(1)
                                .Build(maximumNumberParallelEvaluations: 1);
            var genomeBuilder = new GenomeBuilder(GenomeBuilderTest.BuildParameterTree(), configuration);

            // Create genome.
            var genome = GenomeBuilderTest.BuildFittingGenome();

            // Prepare structure to store which values have been mutated.
            string[] identifiers =
            {
                GenomeBuilderTest.DecisionParameter,
                GenomeBuilderTest.SmallValueParameter,
                GenomeBuilderTest.DiscreteParameter,
                GenomeBuilderTest.ContinuousParameter,
            };
            Dictionary <string, bool> hasBeenMutated = identifiers.ToDictionary(
                identifier => identifier,
                identifier => false);
            Dictionary <string, IAllele> originalValues = identifiers.ToDictionary(
                identifier => identifier,
                identifier => genome.GetGeneValue(identifier));

            // Do a lot of mutations.
            for (int i = 0; i < GenomeBuilderTest.loopCountForRandomTests; i++)
            {
                genomeBuilder.Mutate(genome);

                // After each one, check which genes have changed...
                foreach (string identifier in identifiers)
                {
                    bool valueChanged = !object.Equals(originalValues[identifier], genome.GetGeneValue(identifier));
                    if (valueChanged)
                    {
                        // ...and store them.
                        hasBeenMutated[identifier] = true;
                    }
                }
            }

            // Finally check that everything has been changed at least once.
            Assert.True(
                hasBeenMutated.All(keyValuePair => keyValuePair.Value),
                $"Genes {TestUtils.PrintList(hasBeenMutated.Where(keyValuePair => !keyValuePair.Value).Select(keyValuePair => keyValuePair.Key))} have not been mutated.");
        }
示例#2
0
        public void MutateChangesGivenGenomeIfMutationRateIs1()
        {
            // Create genome builder with a mutation rate of 1.
            var configuration =
                new AlgorithmTunerConfiguration.AlgorithmTunerConfigurationBuilder()
                .SetMutationRate(1)
                .Build(maximumNumberParallelEvaluations: 1);
            var genomeBuilder = new GenomeBuilder(GenomeBuilderTest.BuildParameterTree(), configuration);

            // Build a fitting genome and store original description.
            var    genome = GenomeBuilderTest.BuildFittingGenome();
            string originalDescription = genome.ToString();

            // Call mutate and check genome changed.
            genomeBuilder.Mutate(genome);
            Assert.NotEqual(
                originalDescription,
                genome.ToString());
        }
示例#3
0
        public void MutateRespectsMutationRate()
        {
            // Create genome builder with a mutation rate of 0.35.
            double mutationRate  = 0.35;
            var    configuration =
                new AlgorithmTunerConfiguration.AlgorithmTunerConfigurationBuilder()
                .SetMutationRate(mutationRate)
                .Build(maximumNumberParallelEvaluations: 1);
            var genomeBuilder = new GenomeBuilder(GenomeBuilderTest.BuildParameterTree(), configuration);

            // Create genome.
            var genome = GenomeBuilderTest.BuildFittingGenome();

            // For a lot of iterations:
            IAllele oldGeneValue = genome.GetGeneValue(GenomeBuilderTest.ContinuousParameter);
            int     changeCount  = 0;
            int     numberLoops  = 1000;

            for (int i = 0; i < numberLoops; i++)
            {
                // Mutate the genome ...
                genomeBuilder.Mutate(genome);
                // ... compare the continuous parameter gene...
                IAllele newGeneValue = genome.GetGeneValue(GenomeBuilderTest.ContinuousParameter);
                if (!object.Equals(newGeneValue, oldGeneValue))
                {
                    changeCount++;
                }

                // ... and count the number of times it changes.
                oldGeneValue = newGeneValue;
            }

            // Finally compare the number of mutations with the expected number.
            double expectedNumberMutations = mutationRate * numberLoops;

            Assert.True(
                Math.Abs(expectedNumberMutations - changeCount) <= 0.1 * expectedNumberMutations,
                "Number of mutations was not as expected.");
        }