Example #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);
            });
        }
Example #2
0
        public void WhenIntBasedContextRequested_DefaultValues_MaxValueShouldBe_255()
        {
            var context = InitialiserContextProvider.GetContext <int> ();

            JObject properties = context.Serialise();

            Assert.AreEqual(256, properties.Value <int> ("Max"));
        }
Example #3
0
        public void Setup()
        {
            ConfigurationProvider.Reset();

            InitialiserProvider.Reset();
            InitialiserContextProvider.Reset();
            MutationProvider.Reset();
        }
        public void WhenDoubleBasedPopulationInitialised_ValuesShouldBeBetweenMinAndMax()
        {
            var context = new DoubleBasedGenomeInitialiserContext();

            context.Min = 15d;
            context.Max = 192d;

            InitialiserContextProvider.AddContext(context);

            Population <double> population = new Population <double> ();

            population.Initialise(1000, 5000);

            population.Individuals.ForEach(i => {
                foreach (double x in i.Code)
                {
                    Assert.IsTrue(x >= context.Min && x <= context.Max);                //probably inclusive, but hey
                }
            });
        }
        public void WhenIntBasedPopulationInInitialised_MinAndMaxEqual_ValuesShouldBeCorrect()
        {
            int min = 186, max = min;

            Population <int> population = new Population <int> ();

            var context = InitialiserContextProvider.GetContext <int> () as IntBasedGenomeInitialiserContext;

            context.Min = min;
            context.Max = max;

            population.Initialise(context, 100, 5000);

            population.Individuals.ForEach(i => {
                foreach (int x in i.Code)
                {
                    Assert.AreEqual(186, x);
                }
            });
        }
Example #6
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);
            });
        }
        public void WhenIntBasedPopulationInInitialised_ValuesShouldBeCorrect()
        {
            int min = 10, max = 47;

            Population <int> population = new Population <int> ();

            IntBasedGenomeInitialiserContext context = new IntBasedGenomeInitialiserContext {
                Min = min,
                Max = max
            };

            InitialiserContextProvider.AddContext(context);

            population.Initialise(100, 5000);

            population.Individuals.ForEach(i => {
                foreach (int x in i.Code)
                {
                    Assert.IsTrue(x >= min && x < max);
                }
            });
        }
Example #8
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);
        }
Example #9
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);
        }
Example #10
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);
        }
 public void TestSetup()
 {
     InitialiserProvider.Reset();
     InitialiserContextProvider.Reset();
 }
Example #12
0
        public void WhenIntBasedContextRequested_ShouldReturn()
        {
            var context = InitialiserContextProvider.GetContext <int> ();

            Assert.IsNotNull(context);
        }
Example #13
0
        public void WhenInitialiserContextProviderCalled_ContextDoesNotExist_ShouldThrowException()
        {
            InitialiserContextProvider.GetContext <UIntPtr> ();

            Assert.Fail();
        }
Example #14
0
        public void WhenInitialiserContextProviderCalled_ContextExists_ContextShouldBeOfCorrectType()
        {
            var context = InitialiserContextProvider.GetContext <int> ();

            Assert.AreEqual(typeof(int), context.GenomeType);
        }
Example #15
0
 public void WhenInitialiserContextProviderCalled_ContextExists_ShouldNotReturnNull()
 {
     Assert.IsNotNull(InitialiserContextProvider.GetContext <int> ());
 }
Example #16
0
        public void WhenInitialiserContextProviderCalled_ContextExists_ShouldNotThrowException()
        {
            InitialiserContextProvider.GetContext <byte> ();

            Assert.Pass();
        }