Exemple #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ParameterConfigurationSpaceGenomeBuilderTest"/> class.
        /// </summary>
        public ParameterConfigurationSpaceGenomeBuilderTest()
        {
            this._parameterSpecification = ParameterConfigurationSpaceGenomeBuilderTest.CreateParameterConfigurationSpaceSpecification();
            this._parameterTree          = AcLibUtils.CreateParameterTree(this._parameterSpecification);
            this._genomeBuilder          = new ParameterConfigurationSpaceGenomeBuilder(
                this._parameterTree,
                this._parameterSpecification,
                this._configuration);

            Randomizer.Reset();
            Randomizer.Configure();
        }
Exemple #2
0
        public void MakeGenomeValidOnlyChangesRelevantParameters()
        {
            // Create a specification with several parameters.
            var parameters = new List <IParameterNode>
            {
                new ValueNode <int>("a", new IntegerDomain(-2, 4)),
                new ValueNode <int>("Forbidden", new IntegerDomain(-30, 60)),
                new ValueNode <int>("c", new IntegerDomain(-2, 4)),
            };
            var forbiddenValue = new Dictionary <string, IAllele> {
                { "Forbidden", new Allele <int>(0) }
            };
            var inactiveForbiddenValue = new Dictionary <string, IAllele> {
                { "a", new Allele <int>(2) }
            };
            var forbiddenCombinations = new List <ForbiddenParameterCombination>
            {
                new ForbiddenParameterCombination(inactiveForbiddenValue),
                new ForbiddenParameterCombination(forbiddenValue),
            };
            var multipleParameterSpecification = new ParameterConfigurationSpaceSpecification(
                parameters,
                new Dictionary <string, List <EqualsCondition> >(),
                forbiddenCombinations);

            this._genomeBuilder = new ParameterConfigurationSpaceGenomeBuilder(
                AcLibUtils.CreateParameterTree(multipleParameterSpecification),
                multipleParameterSpecification,
                this._configuration);

            // Several times:
            int numberTests = 20;

            for (int i = 0; i < numberTests; i++)
            {
                // Create an invalid genome.
                var genome = new Genome();
                genome.SetGene("a", new Allele <int>(-1));
                genome.SetGene("Forbidden", new Allele <int>(0));
                genome.SetGene("c", new Allele <int>(2));

                // Repair it.
                this._genomeBuilder.MakeGenomeValid(genome);

                // Check it is repaired with only the forbidden value changed.
                Assert.NotEqual(
                    0,
                    genome.GetGeneValue("Forbidden").GetValue());
                Assert.Equal(-1, genome.GetGeneValue("a").GetValue());
                Assert.Equal(2, genome.GetGeneValue("c").GetValue());
            }
        }
Exemple #3
0
        public void MakeGenomeValidCanHandleMultipleIssues()
        {
            // Create a specification with dependent rules.
            var parameters = new List <IParameterNode>
            {
                new ValueNode <int>("a", new IntegerDomain(-2, 4)),
                new ValueNode <int>("Forbidden", new CategoricalDomain <int>(new List <int> {
                    6, 89
                })),
                new ValueNode <int>("c", new IntegerDomain(-2, 4)),
            };
            var forbiddenValue = new Dictionary <string, IAllele> {
                { "Forbidden", new Allele <int>(6) }
            };
            var dependentRule = new Dictionary <string, IAllele> {
                { "a", new Allele <int>(2) }, { "Forbidden", new Allele <int>(89) }
            };
            var forbiddenCombinations = new List <ForbiddenParameterCombination>
            {
                new ForbiddenParameterCombination(dependentRule),
                new ForbiddenParameterCombination(forbiddenValue),
            };
            var multipleParameterSpecification = new ParameterConfigurationSpaceSpecification(
                parameters,
                new Dictionary <string, List <EqualsCondition> >(),
                forbiddenCombinations);

            this._genomeBuilder = new ParameterConfigurationSpaceGenomeBuilder(
                AcLibUtils.CreateParameterTree(multipleParameterSpecification),
                multipleParameterSpecification,
                this._configuration);

            // Create an genome which is invalid and needs at least two mutations to get valid.
            var genome = new Genome();

            genome.SetGene("a", new Allele <int>(2));
            genome.SetGene("Forbidden", new Allele <int>(6));
            genome.SetGene("c", new Allele <int>(-1));

            this._genomeBuilder.MakeGenomeValid(genome);

            Assert.Equal(89, genome.GetGeneValue("Forbidden").GetValue());
            Assert.NotEqual(2, genome.GetGeneValue("a").GetValue());
            Assert.Equal(-1, genome.GetGeneValue("c").GetValue());
        }
Exemple #4
0
        public void MakeGenomeValidThrowsForImpossibleTasks()
        {
            // Create a specification in which every value is forbidden.
            var parameters = new List <IParameterNode>
            {
                new ValueNode <int>("a", new CategoricalDomain <int>(new List <int> {
                    7, -12
                }))
            };

            var forbiddenValue = new Dictionary <string, IAllele> {
                { "a", new Allele <int>(7) }
            };
            var secondForbidden = new Dictionary <string, IAllele> {
                { "a", new Allele <int>(-12) }
            };
            var forbiddenCombinations = new List <ForbiddenParameterCombination>
            {
                new ForbiddenParameterCombination(forbiddenValue),
                new ForbiddenParameterCombination(secondForbidden),
            };

            var impossibleSpecification = new ParameterConfigurationSpaceSpecification(
                parameters,
                new Dictionary <string, List <EqualsCondition> >(),
                forbiddenCombinations);

            this._genomeBuilder = new ParameterConfigurationSpaceGenomeBuilder(
                AcLibUtils.CreateParameterTree(impossibleSpecification),
                impossibleSpecification,
                this._configuration);

            // Try to repair a genome.
            var genome = new Genome();

            genome.SetGene("a", new Allele <int>(7));
            Assert.Throws <TimeoutException>(() => this._genomeBuilder.MakeGenomeValid(genome));
        }