Esempio n. 1
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);
        }
        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);
        }