示例#1
0
        public void WhenMutationIsRequested_Double_ContextMethod_AllValuesChangedWithinVariance()
        {
            var population = new Population <double> ();

            (InitialiserContextProvider.GetContext <double>() as DoubleBasedGenomeInitialiserContext).Min = 100d;
            (InitialiserContextProvider.GetContext <double>() as DoubleBasedGenomeInitialiserContext).Max = 200d;

            population.Initialise(1, 10000);

            ConfigurationProvider.Mutation.ChanceOfMutation        = 1;
            ConfigurationProvider.Mutation.MutationMethod          = MutationConfiguration.MutationStyle.Variance;
            ConfigurationProvider.Mutation.ContextMutationVariance = .25;

            var    mutator      = MutationProvider.GetMutator <double> ();
            double initialState = 150d;

            population[0].Code.For((item, i) => {
                population[0].Code [i] = initialState;
            });

            mutator.Mutate(population);

            double variance = 25d;

            population[0].Code.For((item, i) => {
                Assert.GreaterOrEqual(item, initialState - variance);
                Assert.LessOrEqual(item, initialState + variance);
            });
        }
示例#2
0
        public void WhenMutationIsRequested_Int_ContextMethod_VarianceIsApprox50pcNegativeVariance()
        {
            Population <int> population = new Population <int> ();

            int genomeCount = 1000;

            population.Initialise(1, genomeCount);

            int[] original = population [0].Code.ToArray();

            Mutator <int> mutator = MutationProvider.GetMutator <int> ();

            ConfigurationProvider.Mutation.ChanceOfMutation        = 1;
            ConfigurationProvider.Mutation.ContextMutationVariance = .33;
            ConfigurationProvider.Mutation.MutationMethod          = MutationConfiguration.MutationStyle.Variance;

            mutator.Mutate(population);

            int negativeCount = 0;

            population[0].Code.For((item, i) => {
                negativeCount += population[0].Code[i] < original[i] ? 1 : 0;
            });

            double acceptableMin = (double)genomeCount * ((double)4.5 / 10);
            double acceptableMax = (double)genomeCount * ((double)5.5 / 10);

            Assert.GreaterOrEqual(negativeCount, acceptableMin);
            Assert.LessOrEqual(negativeCount, acceptableMax);
        }
示例#3
0
        public void WhenMutationIsRequested_Double_Chance50pc_ApproxHalfValuesChanged()
        {
            Enumerable.Range(0, 100).ForEach(loop => {
                Population <double> population = new Population <double> ();
                int genomeCount = 10000;

                population.Initialise(1, genomeCount);

                //assume between 45% and 55%
                double acceptableMin = (double)genomeCount * ((double)4.5 / 10);
                double acceptableMax = (double)genomeCount * ((double)5.5 / 10);

                double[] beforeMutation = population[0].Code.Select(i => i).ToArray();

                Mutator <double> mutator = MutationProvider.GetMutator <double> ();

                ConfigurationProvider.Mutation.ChanceOfMutation = 0.5d;

                mutator.Mutate(population);

                int changedCount = 0;

                population[0].Code.For((item, i) => {
                    changedCount += beforeMutation [i] == item ? 0 : 1;
                });

                Assert.GreaterOrEqual(changedCount, acceptableMin);
                Assert.LessOrEqual(changedCount, acceptableMax);
            });
        }
示例#4
0
        public void WhenMutationIsRequested_Bool_Chance50pc_ApproxHalfValuesChanged()
        {
            Population <bool> population = new Population <bool> ();

            int genomeLength = 1000;

            population.Initialise(1, genomeLength);

            bool[] beforeMutation = population [0].Code.Select(i => i).ToArray();

            ConfigurationProvider.Mutation.ChanceOfMutation = .5d;

            Mutator <bool> mutator = MutationProvider.GetMutator <bool> ();

            mutator.Mutate(population);

            int mutationCount = 0;

            population [0].Code.For((item, i) => {
                mutationCount += beforeMutation [i] == item ? 0 : 1;
            });

            double acceptableMin = (double)genomeLength * ((double)4.5d / 10);
            double acceptableMax = (double)genomeLength * ((double)5.5d / 10);

            Assert.LessOrEqual(mutationCount, acceptableMax);
            Assert.GreaterOrEqual(mutationCount, acceptableMin);
        }
示例#5
0
        public void Setup()
        {
            ConfigurationProvider.Reset();

            InitialiserProvider.Reset();
            InitialiserContextProvider.Reset();
            MutationProvider.Reset();
        }
示例#6
0
        public void WhenMutationIsRequested_Bool_Chance100pc_AllValuesChanged()
        {
            Population <bool> population = new Population <bool> ();

            population.Initialise(1, 1000);

            bool[] beforeMutation = population [0].Code.Select(i => i).ToArray();

            ConfigurationProvider.Mutation.ChanceOfMutation = 0;

            Mutator <bool> mutator = MutationProvider.GetMutator <bool> ();

            mutator.Mutate(population);

            population [0].Code.For((item, i) => {
                Assert.AreEqual(beforeMutation[i], item);
            });
        }
示例#7
0
        public void WhenMutationIsRequested_Double_FullRandomMethod_AllValuesChanged()
        {
            Population <double> population = new Population <double> ();

            population.Initialise(1, 100000);

            double[] beforeMutation = population[0].Code.Select(i => i).ToArray();

            Mutator <double> mutator = MutationProvider.GetMutator <double> ();

            ConfigurationProvider.Mutation.ChanceOfMutation = 1;

            mutator.Mutate(population);

            population[0].Code.For((item, i) =>
            {
                Assert.AreNotEqual(beforeMutation[i], item);
            });
        }
示例#8
0
        public void WhenMutationIsRequested_Int_ContextMethod_AllValuesChangedWithinVariance()
        {
            Population <int> population = new Population <int> ();

            (InitialiserContextProvider.GetContext <int>() as IntBasedGenomeInitialiserContext).Min = 100;
            (InitialiserContextProvider.GetContext <int>() as IntBasedGenomeInitialiserContext).Max = 200;

            population.Initialise(1, 10000);

            ConfigurationProvider.Mutation.ChanceOfMutation        = 1;
            ConfigurationProvider.Mutation.MutationMethod          = MutationConfiguration.MutationStyle.Variance;
            ConfigurationProvider.Mutation.ContextMutationVariance = .25;

            Mutator <int> mutator = MutationProvider.GetMutator <int> ();

            population[0].Code.For((item, i) => {
                int mod5 = i % 5;

                population[0].Code [i] = 10 + ((mod5 % 5) * 10)
                                         + (InitialiserContextProvider.GetContext <int>() as IntBasedGenomeInitialiserContext).Min;
            });



            mutator.Mutate(population);

            int variance = 25;

            population[0].Code.For((item, i) => {
                int mod5 = i % 5;

                int original = 10 + ((mod5 % 5) * 10)
                               + (InitialiserContextProvider.GetContext <int>() as IntBasedGenomeInitialiserContext).Min;

                int min = original - variance;
                int max = original + variance;

                Assert.GreaterOrEqual(item, min);
                Assert.LessOrEqual(item, max);
            });
        }
示例#9
0
        public void WhenMutationIsRequested_Double_ContextMethod_VarianceIsApprox50pcNegativeVariance()
        {
            var population = new Population <double> ();

            (InitialiserContextProvider.GetContext <double> () as DoubleBasedGenomeInitialiserContext).Min = 100d;
            (InitialiserContextProvider.GetContext <double> () as DoubleBasedGenomeInitialiserContext).Max = 200d;

            var genomeCount = 10000;

            population.Initialise(1, genomeCount);

            ConfigurationProvider.Mutation.ChanceOfMutation        = 1;
            ConfigurationProvider.Mutation.MutationMethod          = MutationConfiguration.MutationStyle.Variance;
            ConfigurationProvider.Mutation.ContextMutationVariance = .25;

            Mutator <double> mutator = MutationProvider.GetMutator <double> ();

            population [0].Code.For((item, i) => {
                population [0].Code [i] = 150;
            });

            mutator.Mutate(population);

            int negativeVarianceCount = 0;

            population [0].Code.For((item, i) => {
                if (item < 150)
                {
                    negativeVarianceCount += 1;
                }
            });

            //assume between 45% and 55%
            double acceptableMin = (double)genomeCount * ((double)4.5 / 10);
            double acceptableMax = (double)genomeCount * ((double)5.5 / 10);

            Assert.GreaterOrEqual(negativeVarianceCount, acceptableMin);
            Assert.LessOrEqual(negativeVarianceCount, acceptableMax);
        }
示例#10
0
        public void WhenMutationIsSetToVariance_Double_CheckedOverflow_MutationLessThanMinFloorAtMin()
        {
            var population = new Population <double> ();

            (InitialiserContextProvider.GetContext <double> () as DoubleBasedGenomeInitialiserContext).Min = 10d;
            (InitialiserContextProvider.GetContext <double> () as DoubleBasedGenomeInitialiserContext).Max = 110d;

            DoubleBasedSingleInitialiser initialiser = new DoubleBasedSingleInitialiser(
                (InitialiserContextProvider.GetContext <double> () as DoubleBasedGenomeInitialiserContext).Min);

            InitialiserProvider.AddInitialiser(initialiser);

            var genomeLength = 100000;

            population.Initialise(1, genomeLength);

            ConfigurationProvider.Mutation.AllowUncheckedVariance  = false;
            ConfigurationProvider.Mutation.ChanceOfMutation        = 1;
            ConfigurationProvider.Mutation.ContextMutationVariance = .1;
            ConfigurationProvider.Mutation.MutationMethod          = MutationConfiguration.MutationStyle.Variance;

            Mutator <double> mutator = MutationProvider.GetMutator <double> ();

            mutator.Mutate(population);

            int underflowCount = 0;

            population [0].Code.ForEach(i => {
                underflowCount +=
                    i == (InitialiserContextProvider.GetContext <double> () as DoubleBasedGenomeInitialiserContext).Min ? 1 : 0;
            });

            double acceptableMin = (double)genomeLength * ((double)4.5d / 10);
            double acceptableMax = (double)genomeLength * ((double)5.5d / 10);

            Assert.LessOrEqual(underflowCount, acceptableMax);
            Assert.GreaterOrEqual(underflowCount, acceptableMin);
        }
示例#11
0
        public void WhenMutationIsSetToVariance_Byte_UncheckedOverflow_MutationLessThanMinUnderflowToMax()
        {
            Population <byte> population = new Population <byte> ();

            (InitialiserContextProvider.GetContext <byte> () as ByteBasedGenomeInitialiserContext).Min = 0;
            (InitialiserContextProvider.GetContext <byte> () as ByteBasedGenomeInitialiserContext).Max = 100;

            ByteBasedSingleInitialiser initialiser = new ByteBasedSingleInitialiser(
                (InitialiserContextProvider.GetContext <byte> () as ByteBasedGenomeInitialiserContext).Min);

            InitialiserProvider.AddInitialiser(initialiser);

            var genomeLength = 1000;

            population.Initialise(1, genomeLength);

            ConfigurationProvider.Mutation.AllowUncheckedVariance  = true;
            ConfigurationProvider.Mutation.ChanceOfMutation        = 1;
            ConfigurationProvider.Mutation.ContextMutationVariance = .1;
            ConfigurationProvider.Mutation.MutationMethod          = MutationConfiguration.MutationStyle.Variance;

            Mutator <byte> mutator = MutationProvider.GetMutator <byte> ();

            mutator.Mutate(population);

            int overflowCount = 0;

            population [0].Code.ForEach(i => {
                overflowCount += i >= 90 ? 1 : 0;
            });

            double acceptableMin = (double)genomeLength * ((double)4.5d / 10);
            double acceptableMax = (double)genomeLength * ((double)5.5d / 10);

            Assert.LessOrEqual(overflowCount, acceptableMax);
            Assert.GreaterOrEqual(overflowCount, acceptableMin);
        }
示例#12
0
        public void WhenMutationIsRequested_Int_NoExceptionThrown()
        {
            MutationProvider.GetMutator <int> ();

            Assert.Pass();
        }
示例#13
0
        public void WhenMutationIsRequested_Double_NoExceptionThrown()
        {
            MutationProvider.GetMutator <double> ();

            Assert.Pass();
        }
示例#14
0
        public void WhenMutatorIsRequested_Int_CorrectMutatorShouldReturn()
        {
            Mutator mutator = MutationProvider.GetMutator <int> ();

            Assert.AreEqual(typeof(int), mutator.GenomeType);
        }
示例#15
0
        public void WhenMutatorIsRequested_Double_CorrectMutatorShouldReturn()
        {
            Mutator <double> mutator = MutationProvider.GetMutator <double> ();

            Assert.AreEqual(typeof(double), mutator.GenomeType);
        }