示例#1
0
        public void CreateRandomGenomeThrowsErrorIfNoValidGenomesExist()
        {
            // Create genome builder that does not accept any genome.
            var genomeBuilder = GenomeBuilderTest.CreateCustomGenomeBuilder(
                mutationRate: 0,
                isValidFunction: candidate => false);

            // CreateRandomGenome will create a genome and try to repair it afterwards.
            // It should then throw an exception as that will never work.
            Assert.Throws <TimeoutException>(() => genomeBuilder.CreateRandomGenome(age: 0));
        }
示例#2
0
        public void MakeGenomeValidThrowsIfNoValidGenomeExists()
        {
            // Create genome builder that does not accept any genome.
            var genomeBuilder = GenomeBuilderTest.CreateCustomGenomeBuilder(
                mutationRate: 0,
                isValidFunction: candidate => false);

            // Try to repair a genome.
            var genome = GenomeBuilderTest.BuildFittingGenome();

            Assert.Throws <TimeoutException>(() => genomeBuilder.MakeGenomeValid(genome));
        }
示例#3
0
        public void MutateThrowsErrorIfNoValidGenomesExist()
        {
            // Create genome builder that does not accept any genome.
            var genomeBuilder = GenomeBuilderTest.CreateCustomGenomeBuilder(
                mutationRate: 0,
                isValidFunction: candidate => false);
            // Build a genome.
            var genome = GenomeBuilderTest.BuildFittingGenome();

            // Mutate will try to repair the genome and should throw an exception as that will never work.
            Assert.Throws <TimeoutException>(() => genomeBuilder.Mutate(genome));
        }
示例#4
0
        public void MakeGenomeValidRepairsInvalidGenome()
        {
            // Invalid genomes must exist. We forbid having a value of 3 for a certain gene.
            int forbiddenValue = 3;
            Func <Genome, bool> doesNotHave3ForInt1To5 = candidate =>
                                                         !object.Equals(candidate.GetGeneValue(GenomeBuilderTest.SmallValueParameter).GetValue(), forbiddenValue);

            // Create a genome builder implementing that check.
            var genomeBuilder = GenomeBuilderTest.CreateCustomGenomeBuilder(
                mutationRate: 0,
                isValidFunction: doesNotHave3ForInt1To5);

            var genome = GenomeBuilderTest.BuildFittingGenome();

            genome.SetGene(GenomeBuilderTest.SmallValueParameter, new Allele <int>(forbiddenValue));

            genomeBuilder.MakeGenomeValid(genome);
            Assert.True(genomeBuilder.IsGenomeValid(genome), $"{genome} should be valid after repairing it.");
        }
示例#5
0
        public void MutateRepairsInvalidGenomes()
        {
            // Invalid genomes must exist. We forbid having a value of 3 for a certain gene.
            int forbiddenValue = 3;
            Func <Genome, bool> doesNotHave3ForInt1To5 = candidate =>
                                                         !object.Equals(candidate.GetGeneValue(GenomeBuilderTest.SmallValueParameter).GetValue(), forbiddenValue);

            // Create a genome builder implementing that check. It also should never mutate randomly
            // so we can be sure the repair was not due to the mutation itself.
            var genomeBuilder = GenomeBuilderTest.CreateCustomGenomeBuilder(
                mutationRate: 0,
                isValidFunction: doesNotHave3ForInt1To5);

            // Build an invalid genome.
            var genome = GenomeBuilderTest.BuildFittingGenome();

            genome.SetGene(GenomeBuilderTest.SmallValueParameter, new Allele <int>(forbiddenValue));

            // Mutate and check the broken gene was fixed.
            genomeBuilder.Mutate(genome);
            Assert.NotEqual(
                forbiddenValue,
                genome.GetGeneValue(GenomeBuilderTest.SmallValueParameter).GetValue());
        }