Example #1
0
        public void GivenRandom_WhenGetIntWithTabu_ThenShouldReturnDifferent()
        {
            var random = new PredeterminedRandom(10, 10, 25, 30);
            var result = random.GetIntWithTabu(0, 50, 10);

            result.Should().Be(25);
        }
        public void GivenSortedSubsetChromosomes_WhenOnePointCrossOver_WithConflict_ThenShouldReply()
        {
            var chromosomes = SortedSubsetTestData.CreateChromosomes();

            var random       = new PredeterminedRandom(5, 7); //Crossover gene value before and after conflicts
            var parameterSet = new ParameterSet();

            parameterSet.SetValue(ParameterNames.FailedCrossoverRetryCount, 1);
            var conflictDetectors = new List <INeighborhoodConflictDetector>()
            {
                new PredeterminedConflictDetector(false, true, true, false, false, false, false, false, false, false)
            };
            var crossover = new OnePointCrossover(random, parameterSet, conflictDetectors);

            var results = crossover.Cross(chromosomes[0], chromosomes[1]);

            var result1 = results[0] as SortedSubsetChromosome;
            var result2 = results[1] as SortedSubsetChromosome;

            result1.Sections[0].Should().BeEquivalentTo(new int[] { 1, 4, 5, 9 });
            result1.Sections[1].Should().BeEquivalentTo(new int[] { 3, 6, 7 });
            result1.Sections[2].Should().BeEquivalentTo(new int[] { 2, 8 });
            result2.Sections[0].Should().BeEquivalentTo(new int[] { 3, 4, 7 });
            result2.Sections[1].Should().BeEquivalentTo(new int[] { 2, 5, 8 });
            result2.Sections[2].Should().BeEquivalentTo(new int[] { 1, 6, 9 });
        }
        public void GivenSortedSubsetChromosomes_WhenOnePointCrossOver_ThenShouldBeProper()
        {
            var chromosomes  = SortedSubsetTestData.CreateChromosomes();
            var random       = new PredeterminedRandom(5); //Crossover gene value
            var parameterSet = new ParameterSet();

            parameterSet.SetValue(ParameterNames.FailedCrossoverRetryCount, 0);
            var conflictDetectors = new List <INeighborhoodConflictDetector>()
            {
                AllRightConflictDetector.Instance
            };
            var crossover = new OnePointCrossover(random, parameterSet, conflictDetectors);

            var results = crossover.Cross(chromosomes[0], chromosomes[1]);

            var result1 = results[0] as SortedSubsetChromosome;
            var result2 = results[1] as SortedSubsetChromosome;

            result1.Sections[0].Should().BeEquivalentTo(new int[] { 1, 4, 9 });
            result1.Sections[1].Should().BeEquivalentTo(new int[] { 3, 5, 7 });
            result1.Sections[2].Should().BeEquivalentTo(new int[] { 2, 6, 8 });
            result2.Sections[0].Should().BeEquivalentTo(new int[] { 3, 4, 5, 7 });
            result2.Sections[1].Should().BeEquivalentTo(new int[] { 2, 6, 8 });
            result2.Sections[2].Should().BeEquivalentTo(new int[] { 1, 9 });
        }
        public void NonEmptyProbabilityDistribution_ChooseOne_ShouldReturnOne()
        {
            var random       = new PredeterminedRandom(80, 30);
            var distribution = new StochasticProvider <string>(random)
                               .Add("First", 50)
                               .Add("Second", 40);

            distribution.GetOne().Should().Be("Second");
            distribution.GetOne().Should().Be("First");
        }
Example #5
0
        public void SwapTwoRangeMutation_Mutate_ReturnMutated()
        {
            var random       = new PredeterminedRandom(new double[] { 5, 3, 2, 2 });
            var parameterSet = new ParameterSet();
            var chromosome   = PermutationTestData.CreateTestChromosome1(); //{ 7, 2, 8, 4, 1, 6, 3, 5, 9, 0 }
            var mutation     = new SwapTwoRangeMutation(random, parameterSet, null);

            var result   = mutation.Mutate(chromosome);
            var expected = new int[] { 7, 2, 6, 3, 5, 1, 8, 4, 9, 0 };

            result.Genes.Should().BeEquivalentTo(expected);
        }
        public void PMXCrossover_Cross_ReturnChildren(int position, int length, int[] expected)
        {
            var random       = new PredeterminedRandom(new double[] { position, length });
            var parameterSet = new ParameterSet();
            var crossover    = new PMXCrossover(random, parameterSet, null);
            var parents      = PermutationTestData.CreateTestChromosomes();

            var children = crossover.Cross(parents[0], parents[1]);

            children.Count.Should().Be(2);
            ((PermutationChromosome)children[0]).Genes.Should().BeEquivalentTo(expected);
        }
        public void PMXCrossover_CreateGeneMap_GetUniqueValue_ReturnUnique(int position, int expected)
        {
            var random       = new PredeterminedRandom(new double[0]);
            var parameterSet = new ParameterSet();
            var crossover    = new PMXCrossover(random, parameterSet, null);
            var chromosomes  = PermutationTestData.CreateTestChromosomes();
            var range        = new GeneRange(3, 4);
            var geneMap      = crossover.GenerateGeneMap(((PermutationChromosome)chromosomes[0]).Genes, range);

            var result = crossover.GetUniqueGeneValue(((PermutationChromosome)chromosomes[1]).Genes, geneMap, position);

            result.Should().Be(expected);
        }
Example #8
0
        public void DoubleFieldOperator_Set_ShouldSet(double change, double expected)
        {
            var testStruct = new TestStruct()
            {
                DoubleField  = 4.0,
                BitField     = false,
                IntegerField = 0
            };

            var random       = new PredeterminedRandom(new double[] { 1, 1 });
            var parameterSet = new ParameterSet(new List <PeaSettingsNamedValue>()
            {
                new PeaSettingsNamedValue(ParameterNames.MutationIntensity, change)
            });
            var fieldOperator = new DoubleFieldOperator <TestStruct>("DoubleField", random, parameterSet);

            fieldOperator.Mutate(testStruct);
        }
        public void GivenSortedSubsetChromosomes_WhenOnePointCrossOver_WithConflict_ThenShouldReturnsZero()
        {
            var chromosomes = SortedSubsetTestData.CreateChromosomes();

            var random       = new PredeterminedRandom(5); //Crossover gene value
            var parameterSet = new ParameterSet();

            parameterSet.SetValue(ParameterNames.FailedCrossoverRetryCount, 0);
            var conflictDetectors = new List <INeighborhoodConflictDetector>()
            {
                new PredeterminedConflictDetector(false, true, true)
            };
            var crossover = new OnePointCrossover(random, parameterSet, conflictDetectors);

            var results = crossover.Cross(chromosomes[0], chromosomes[1]);

            results.Count.Should().Be(0);
        }
        public void GivenSortedSubsetOnePointCrossover_WhenMergeSections_ThenShouldBeProper(
            int[] sectionForLeft, int leftEndPosition,
            int[] sectionForRight, int rightStartPosition, int[] expected)
        {
            bool childAlreadyConflicted = false;
            var  random            = new PredeterminedRandom(5);
            var  parameterSet      = new ParameterSet();
            var  conflictDetectors = new List <INeighborhoodConflictDetector>()
            {
                AllRightConflictDetector.Instance
            };
            var crossover = new OnePointCrossover(random, parameterSet, conflictDetectors);

            var result = crossover.MergeSections(sectionForLeft, leftEndPosition, sectionForRight, rightStartPosition,
                                                 ref childAlreadyConflicted);

            result.Should().BeEquivalentTo(expected);
        }
        public void PMXCrossover_CopyWithDuplicationElimination_CopyData(int begin, int end, int[] expected)
        {
            var random       = new PredeterminedRandom(new double[0]);
            var parameterSet = new ParameterSet();
            var crossover    = new PMXCrossover(random, parameterSet, null);
            var chromosomes  = PermutationTestData.CreateTestChromosomes();
            var range        = new GeneRange(3, 3);
            var geneMap      = crossover.GenerateGeneMap(((PermutationChromosome)chromosomes[0]).Genes, range);
            var childGenes   = new int[((PermutationChromosome)chromosomes[0]).Genes.Length];

            var length = end - begin;

            crossover.CopyWithDuplicationElimination(((PermutationChromosome)chromosomes[1]).Genes, childGenes, geneMap, begin, end);
            var result = new int[length];

            Array.Copy(childGenes, begin, result, 0, length);

            result.Should().BeEquivalentTo(expected);
        }
        public void GivenChromosome_WithoutConflict_ThenShouldBeReplaceRandom()
        {
            var random       = new PredeterminedRandom(1, 2, 1, 2); //possibility, sourceSection, sourcePosition, wrongTargetSection, goodTargetSection
            var parameterSet = new ParameterSet();

            parameterSet.SetValue(ParameterNames.FailedMutationRetryCount, 0);

            var chromosome        = SortedSubsetTestData.CreateChromosome();
            var conflictDetectors = new List <INeighborhoodConflictDetector>()
            {
                AllRightConflictDetector.Instance
            };
            var mutation = new ReplaceOneGeneMutation(random, parameterSet, conflictDetectors);

            var result = mutation.Mutate(chromosome);

            result.Sections[0].Should().BeEquivalentTo(new int[] { 1, 4, 5, 7 });
            result.Sections[1].Should().BeEquivalentTo(new int[] { 3, 6 });
            result.Sections[2].Should().BeEquivalentTo(new int[] { 2, 8, 9 });
        }
        public void Order1Crossover_Cross_ReturnChildren()
        {
            var position     = 3;
            var length       = 5;
            var random       = new PredeterminedRandom(new double[] { position, length });
            var parameterSet = new ParameterSet();
            var crossover    = new Order1Crossover(random, parameterSet, null);

            var parent1Genes = new int[] { 8, 4, 7, 3, 6, 2, 5, 1, 9, 0 };
            var parent2Genes = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            var parent1      = new PermutationChromosome(parent1Genes);
            var parent2      = new PermutationChromosome(parent2Genes);

            var children = crossover.Cross(parent1, parent2);

            children.Count.Should().Be(2);

            var expected = new int[] { 0, 4, 7, 3, 6, 2, 5, 1, 8, 9 };

            ((PermutationChromosome)children[0]).Genes.Should().BeEquivalentTo(expected);
        }
        public void GivenChromosome_WithConflict_WhenReplaceOneGene_ThenShouldBeReplaceConflicted()
        {
            var random       = new PredeterminedRandom(0.5, 3, 1); //possibility, conflictIndex, targetSection
            var parameterSet = new ParameterSet();

            parameterSet.SetValue(ParameterNames.ConflictReducingProbability, 0.6);
            parameterSet.SetValue(ParameterNames.FailedMutationRetryCount, 0);
            var chromosome = SortedSubsetTestData.CreateChromosome();

            chromosome.ConflictList.Add(new GenePosition(0, 1));
            var conflictDetectors = new List <INeighborhoodConflictDetector>()
            {
                AllRightConflictDetector.Instance
            };
            var mutation = new ReplaceOneGeneMutation(random, parameterSet, conflictDetectors);

            var result = mutation.Mutate(chromosome);

            result.Sections[0].Should().BeEquivalentTo(new int[] { 1, 5, 7 });
            result.Sections[1].Should().BeEquivalentTo(new int[] { 3, 4, 6, 8 });
            result.Sections[2].Should().BeEquivalentTo(new int[] { 2, 9 });
        }
Example #15
0
        public void GivenPredeterminedRandom_WhenGetDoubleWithMinMax_ThenShouldReturnProper(double minValue, double maxValue)
        {
            var random = new PredeterminedRandom(100.17, 101.33, 3.141592654, 2.71828);

            TestRandomGetDouble(random, minValue, maxValue);
        }
Example #16
0
        public void GivenPredeterminedRandom_WhenGetIntWithMinMax_ThenShouldReturnProper(int minValue, int maxValue)
        {
            var random = new PredeterminedRandom(100, 101, 102, 103);

            TestRandomGetInt(random, minValue, maxValue);
        }