public void Mutate_AllGenesMutablesTrue_AllGenesMutaed()
        {
            var target     = new UniformMutation(true);
            var chromosome = MockRepository.GenerateStub <ChromosomeBase>(3);

            chromosome.ReplaceGenes(0, new Gene[]
            {
                new Gene(1),
                new Gene(1),
                new Gene(1)
            });

            chromosome.Expect(c => c.GenerateGene(0)).Return(new Gene(0));
            chromosome.Expect(c => c.GenerateGene(1)).Return(new Gene(10));
            chromosome.Expect(c => c.GenerateGene(2)).Return(new Gene(20));
            RandomizationProvider.Current = MockRepository.GenerateMock <IRandomization>();

            target.Mutate(chromosome, 1);
            Assert.AreEqual(0, chromosome.GetGene(0).Value);
            Assert.AreEqual(10, chromosome.GetGene(1).Value);
            Assert.AreEqual(20, chromosome.GetGene(2).Value);

            chromosome.VerifyAllExpectations();
            RandomizationProvider.Current.VerifyAllExpectations();
        }
        public IMutation UniformMutation()
        {
            var target = new UniformMutation();

            target.Mutate(new TspChromosome(_numberOfCities), _probability);

            return(target);
        }
Exemple #3
0
        public void Should_Perform_Mutation()
        {
            var algorithm = new UniformMutation(1, -0.1, 0.1);
            var a         = new Element(new double[] { 8D, 3D, 2D, 9D, 1D, 0D, 7D, 6D, 5D, 4D });
            var index     = 1;

            algorithm.Mutate(ref a, index);
            Assert.NotEqual(3D, a.Data[1]);
            Assert.InRange(a.Data[1], 2.9, 3.1);
        }
        public void Mutate_Indexes_RandomIndexes()
        {
            var target     = new UniformMutation(0, 2);
            var chromosome = Substitute.For <ChromosomeBase>(3);

            chromosome.ReplaceGenes(0, new Gene[]
            {
                new Gene(1),
                new Gene(1),
                new Gene(1)
            });

            chromosome.GenerateGene(0).Returns(new Gene(0));
            chromosome.GenerateGene(2).Returns(new Gene(10));
            RandomizationProvider.Current = Substitute.For <IRandomization>();

            target.Mutate(chromosome, 1);
            Assert.AreEqual(0, chromosome.GetGene(0).Value);
            Assert.AreEqual(1, chromosome.GetGene(1).Value);
            Assert.AreEqual(10, chromosome.GetGene(2).Value);
        }
        public void Mutate_InvalidIndexes_Exception()
        {
            var target     = new UniformMutation(0, 3);
            var chromosome = Substitute.For <ChromosomeBase>(3);

            chromosome.ReplaceGenes(0, new Gene[]
            {
                new Gene(1),
                new Gene(1),
                new Gene(1)
            });

            chromosome.GenerateGene(0).Returns(new Gene(0));

            RandomizationProvider.Current = Substitute.For <IRandomization>();

            Assert.Catch <MutationException>(() =>
            {
                target.Mutate(chromosome, 1);
            }, "The chromosome has no gene on index 3. The chromosome genes length is 3.");
        }
Exemple #6
0
        public void Mutate_InvalidIndexes_Exception()
        {
            var target     = new UniformMutation(0, 3);
            var chromosome = MockRepository.GenerateStub <ChromosomeBase>(3);

            chromosome.ReplaceGenes(0, new Gene[]
            {
                new Gene(1),
                new Gene(1),
                new Gene(1)
            });

            chromosome.Expect(c => c.GenerateGene(0)).Return(new Gene(0));

            RandomizationProvider.Current = MockRepository.GenerateMock <IRandomization> ();

            ExceptionAssert.IsThrowing(new MutationException(target, "The chromosome has no gene on index 3. The chromosome genes lenght is 3."), () => {
                target.Mutate(chromosome, 1);
            });

            chromosome.VerifyAllExpectations();
            RandomizationProvider.Current.VerifyAllExpectations();
        }
        public void Mutate_NoIndexes_RandomOneIndex()
        {
            var target     = new UniformMutation();
            var chromosome = MockRepository.GenerateStub <ChromosomeBase>(3);

            chromosome.ReplaceGenes(0, new Gene[]
            {
                new Gene(1),
                new Gene(1),
                new Gene(1)
            });

            chromosome.Expect(c => c.GenerateGene(1)).Return(new Gene(0));
            RandomizationProvider.Current = MockRepository.GenerateMock <IRandomization>();
            RandomizationProvider.Current.Expect(r => r.GetInts(1, 0, 3)).Return(new int[] { 1 });

            target.Mutate(chromosome, 1);
            Assert.AreEqual(1, chromosome.GetGene(0).Value);
            Assert.AreEqual(0, chromosome.GetGene(1).Value);
            Assert.AreEqual(1, chromosome.GetGene(2).Value);

            chromosome.VerifyAllExpectations();
            RandomizationProvider.Current.VerifyAllExpectations();
        }