Пример #1
0
        public void CreateInstancesReadsInstancesCorrectly()
        {
            AcLibUtilsTest.WriteFile(new[] { "330 ba", "2147483648 foo/bar" }, AcLibUtilsTest.InstanceSpecificationFile);
            var instances = AcLibUtils.CreateInstances(AcLibUtilsTest.InstanceSpecificationFile);

            Assert.Equal(2, instances.Count);
            Assert.Equal("ba", instances[0].Path);
            Assert.Equal(330, instances[0].Seed);
            Assert.Equal("foo/bar", instances[1].Path);
            Assert.Equal(int.MinValue, instances[1].Seed);
        }
Пример #2
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();
        }
Пример #3
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());
            }
        }
Пример #4
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());
        }
Пример #5
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));
        }
Пример #6
0
        public void CreateParameterTreeCreatesFlatTree()
        {
            var parameters = new List <IParameterNode>
            {
                new ValueNode <string>("a", new CategoricalDomain <string>(new List <string> {
                    "0", "1"
                })),
                new ValueNode <int>("b", new DiscreteLogDomain(1, 1024)),
                new ValueNode <double>("c", new ContinuousDomain(-1.02, 2.6)),
            };
            var specification = new ParameterConfigurationSpaceSpecification(
                parameters,
                new Dictionary <string, List <EqualsCondition> >(),
                new List <ForbiddenParameterCombination>());
            var tree = AcLibUtils.CreateParameterTree(specification);

            Assert.Equal(3, tree.GetParameters().Count());
            foreach (var parameter in parameters)
            {
                Assert.True(
                    tree.Root.Children.Contains(parameter),
                    $"{parameter} was not placed directly below the parameter tree root.");
            }
        }
Пример #7
0
 public void CreateParameterTreeThrowsForMissingSpecification()
 {
     Assert.Throws <ArgumentNullException>(() => AcLibUtils.CreateParameterTree(parameterConfiguration: null));
 }
Пример #8
0
 public void CreateInstancesThrowsForNegativeSeed()
 {
     AcLibUtilsTest.WriteFile(new[] { "0 a", "-1 b" }, AcLibUtilsTest.InstanceSpecificationFile);
     Assert.Throws <OverflowException>(() => AcLibUtils.CreateInstances(AcLibUtilsTest.InstanceSpecificationFile));
 }
Пример #9
0
 public void CreateInstancesThrowsForWrongSeedFormat()
 {
     AcLibUtilsTest.WriteFile(new[] { "0 a", "4f b" }, AcLibUtilsTest.InstanceSpecificationFile);
     Assert.Throws <FormatException>(() => AcLibUtils.CreateInstances(AcLibUtilsTest.InstanceSpecificationFile));
 }
Пример #10
0
 public void CreateInstancesThrowsForTooMuchInformation()
 {
     AcLibUtilsTest.WriteFile(new[] { "0 a", "330 b x" }, AcLibUtilsTest.InstanceSpecificationFile);
     Assert.Throws <ArgumentException>(() => AcLibUtils.CreateInstances(AcLibUtilsTest.InstanceSpecificationFile));
 }
Пример #11
0
 public void CreateInstancesThrowsForMissingInstanceFile()
 {
     Assert.Throws <FileNotFoundException>(() => AcLibUtils.CreateInstances(AcLibUtilsTest.InstanceSpecificationFile));
 }