コード例 #1
0
        public override SortedSubsetChromosome Mutate(SortedSubsetChromosome chromosome)
        {
            if (chromosome == null)
            {
                return(null);
            }
            if (chromosome.Sections.Length < 2)
            {
                return(null);
            }

            bool childConflicted = false;
            int  retryCount      = ParameterSet.GetInt(ParameterNames.FailedMutationRetryCount);

            while (true)
            {
                var range1           = GetSourceRange(chromosome);
                var section1         = chromosome.Sections[range1.Section];
                var sourceGeneValue1 = section1[range1.FirstPosition];
                var sourceGeneValue2 = section1[range1.LastPosition - 1];

                var targetSectionIndex = Random.GetIntWithTabu(0, chromosome.Sections.Length, range1.Section);
                var section2           = chromosome.Sections[targetSectionIndex];
                var targetPosition1    = FindNewGenePosition(section2, sourceGeneValue1);
                var targetPosition2    = FindNewGenePosition(section2, sourceGeneValue2);
                var range2             = new GeneRange(targetSectionIndex, targetPosition1, targetPosition2);

                var temp1 = MergeSections(section1, range1, section2, range2, ref childConflicted);
                var temp2 = MergeSections(section2, range2, section1, range1, ref childConflicted);

                if (!childConflicted)
                {
                    chromosome.Sections[range1.Section] = temp1;
                    chromosome.Sections[range2.Section] = temp2;
                }

                if (!childConflicted || retryCount-- < 0)
                {
                    //TODO: Remove this (only for debugging purpose)
                    chromosome.GeneratedRandoms.Add(range1.Section);
                    chromosome.GeneratedRandoms.Add(range1.FirstPosition);
                    chromosome.GeneratedRandoms.Add(range1.LastPosition);
                    chromosome.GeneratedRandoms.Add(range2.Section);
                    chromosome.GeneratedRandoms.Add(range2.FirstPosition);
                    chromosome.GeneratedRandoms.Add(range2.LastPosition);

                    break;
                }

                childConflicted = false;
            }

            if (childConflicted)
            {
                return(null);
            }

            CleanOutSections(chromosome);
            return(chromosome);
        }
コード例 #2
0
        public GeneRange GetParentRange(int[] section, int crossoverPointLeft, int crossoverPointRight, int sectionIndex)
        {
            var positionLeft  = FindNewGenePosition(section, crossoverPointLeft);
            var positionRight = FindNewGenePosition(section, crossoverPointRight);
            var range1        = new GeneRange(sectionIndex, positionLeft, positionRight);

            return(range1);
        }
コード例 #3
0
        public GeneRange GetSourceRange(SortedSubsetChromosome chromosome)
        {
            var sourceStart  = GetSourceSectionAndPosition(chromosome);
            var sourceLength = Random.GetInt(1, chromosome.Sections[sourceStart.Section].Length - sourceStart.Position + 1);
            var sourceEnd    = sourceStart.Position + sourceLength;
            var range        = new GeneRange(sourceStart.Section, sourceStart.Position, sourceEnd);

            return(range);
        }
コード例 #4
0
        public int[] MergeSections(int[] section1, GeneRange range1, int[] section2, GeneRange range2, ref bool childConflicted)
        {
            if (childConflicted)
            {
                return(null);
            }
            int?geneValue0 = null;
            int?geneValue1 = null;

            if (range2.FirstPosition < range2.LastPosition)
            {
                geneValue0 = section2[range2.FirstPosition];
                geneValue1 = section2[range2.LastPosition - 1];
            }

            if (ConflictDetectedWithLeftNeighbor(section1, range1.FirstPosition, geneValue0))
            {
                childConflicted = true;
            }

            if (ConflictDetectedWithRightNeighbor(section1, range1.LastPosition, geneValue1))
            {
                childConflicted = true;
            }

            if (childConflicted)
            {
                return(null);
            }

            var innerLength = range2.LastPosition - range2.FirstPosition;
            var rightLength = section1.Length - range1.LastPosition;

            int[] childSection = new int[range1.FirstPosition + innerLength + rightLength];

            if (range1.FirstPosition > 0)
            {
                Array.Copy(section1, 0, childSection, 0, range1.FirstPosition);
            }

            if (innerLength > 0)
            {
                Array.Copy(section2, range2.FirstPosition, childSection, range1.FirstPosition, innerLength);
            }

            if (rightLength > 0)
            {
                Array.Copy(section1, range1.LastPosition, childSection, range1.FirstPosition + innerLength, rightLength);
            }

            return(childSection);
        }
コード例 #5
0
        public override SortedSubsetChromosome Mutate(SortedSubsetChromosome chromosome)
        {
            if (chromosome == null)
            {
                return(null);
            }
            if (chromosome.Sections.Length < 3)
            {
                return(null);
            }

            bool childConflicted = false;
            int  retryCount      = ParameterSet.GetInt(ParameterNames.FailedMutationRetryCount);

            while (true)
            {
                var firstRange      = GetSourceRange(chromosome);
                var firstSection    = chromosome.Sections[firstRange.Section];
                var firstGeneValue1 = firstSection[firstRange.FirstPosition];
                var firstGeneValue2 = firstSection[firstRange.LastPosition - 1];

                var secondSectionIndex = Random.GetIntWithTabu(0, chromosome.Sections.Length, firstRange.Section);
                var secondSection      = chromosome.Sections[secondSectionIndex];
                var secondPosition1    = FindNewGenePosition(secondSection, firstGeneValue1);
                var secondPosition2    = FindNewGenePosition(secondSection, firstGeneValue2);
                var secondRange        = new GeneRange(secondSectionIndex, secondPosition1, secondPosition2);

                if (secondRange.Length == 0)
                {
                    childConflicted = true;
                }

                if (!childConflicted)
                {
                    var secondGeneValue1 = secondSection[secondRange.FirstPosition];
                    var secondGeneValue2 = secondSection[secondRange.LastPosition - 1];

                    var thirdSectionIndex = Random.GetIntWithTabu(0, chromosome.Sections.Length, firstRange.Section,
                                                                  secondRange.Section);
                    var thirdSection   = chromosome.Sections[thirdSectionIndex];
                    var thirdPosition1 = FindNewGenePosition(thirdSection, secondGeneValue1);
                    var thirdPosition2 = FindNewGenePosition(thirdSection, secondGeneValue2);
                    var thirdRange     = new GeneRange(thirdSectionIndex, thirdPosition1, thirdPosition2);

                    var temp1 = MergeSections(firstSection, firstRange, secondSection, secondRange,
                                              ref childConflicted);
                    var temp2 = MergeSections(secondSection, secondRange, thirdSection, thirdRange,
                                              ref childConflicted);
                    var temp3 = MergeSections(thirdSection, thirdRange, firstSection, firstRange, ref childConflicted);

                    if (!childConflicted)
                    {
                        chromosome.Sections[firstRange.Section]  = temp1;
                        chromosome.Sections[secondRange.Section] = temp2;
                        chromosome.Sections[thirdRange.Section]  = temp3;
                    }
                }

                if (!childConflicted || retryCount-- < 0)
                {
                    break;
                }

                childConflicted = false;
            }

            if (childConflicted)
            {
                return(null);
            }

            CleanOutSections(chromosome);
            return(chromosome);
        }