Ejemplo n.º 1
0
        public override SortedSubsetChromosome Mutate(SortedSubsetChromosome chromosome)
        {
            if (chromosome == null)
            {
                return(null);
            }
            if (chromosome.Sections.Length < 2)
            {
                return(null);
            }

            int retryCount = ParameterSet.GetInt(ParameterNames.FailedMutationRetryCount);

            while (true)
            {
                var  source  = GetSourceSectionAndPosition(chromosome);
                bool success = ReplaceOneGeneToRandomSection(chromosome, source, retryCount);

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

            CleanOutSections(chromosome);
            return(chromosome);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns a new position randomly choosen from the conflict-list
        /// </summary>
        /// <param name="chromosome"></param>
        /// <returns></returns>
        public GenePosition GetConflictedSectionAndPosition(SortedSubsetChromosome chromosome)
        {
            var conflicted = Random.GetInt(0, chromosome.ConflictList.Count);
            var source     = chromosome.ConflictList[conflicted];

            return(source);
        }
Ejemplo n.º 4
0
        private int GetNumberOfGenesToChange(SortedSubsetChromosome chromosome)
        {
            int max = chromosome.TotalCount / chromosome.Sections.Length;
            int min = Math.Min(chromosome.ConflictList.Count, max);

            return(Random.GetInt(min, max));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Returns the gene values from a chromosome section
        /// </summary>
        /// <param name="chromosome">The multi-section chromosome which the operator works within</param>
        /// <param name="sectionIndex">The index of the section</param>
        /// <param name="position">The position of the first gene to get</param>
        /// <param name="count">The number of genes to get</param>
        /// <returns>The array of gene values</returns>
        public int[] GetGenes(SortedSubsetChromosome chromosome, int sectionIndex, int position, int count)
        {
            int[] result = new int[count];

            Array.Copy(chromosome.Sections[sectionIndex], position, result, 0, count);
            return(result);
        }
Ejemplo n.º 6
0
        private GenePosition GetTargetSection(SortedSubsetChromosome chromosome, int sectionToEliminate, int geneValue)
        {
            List <GenePosition> fitSections = new List <GenePosition>();

            for (int s = 0; s < chromosome.Sections.Length; s++)
            {
                if (s != sectionToEliminate)
                {
                    var position = FindNewGenePosition(chromosome.Sections[s], geneValue);
                    if (!ConflictDetectedWithLeftNeighbor(chromosome.Sections[s], position, geneValue) &&
                        !ConflictDetectedWithRightNeighbor(chromosome.Sections[s], position, geneValue))
                    {
                        fitSections.Add(new GenePosition(s, position));
                    }
                }
            }

            if (fitSections.Count == 0)
            {
                return(null);
            }

            var randomIndex   = Random.GetInt(0, fitSections.Count);
            var chosenSection = fitSections[randomIndex];

            return(chosenSection);
        }
Ejemplo n.º 7
0
        public override SortedSubsetChromosome Mutate(SortedSubsetChromosome chromosome)
        {
            if (chromosome == null)
            {
                return(null);
            }
            if (chromosome.Sections.Length < 2)
            {
                return(null);
            }

            int retryCount = ParameterSet.GetInt(ParameterNames.FailedMutationRetryCount);

            while (true)
            {
                var source   = GetSourceSectionAndPosition(chromosome);
                var section  = chromosome.Sections[source.Section];
                var position = source.Position;

                var length = Random.GetInt(0, section.Length - source.Position);

                var targetSectionIndex = Random.GetIntWithTabu(0, chromosome.Sections.Length, source.Section);
                var targetSection      = chromosome.Sections[targetSectionIndex];

                while (position < source.Position + length)
                {
                    var geneValue      = section[position];
                    var targetPosition = FindNewGenePosition(targetSection, geneValue);
                    var insertable     = CountInsertableGenes(chromosome, targetSectionIndex, targetPosition, section, position);

                    bool conflict = ConflictDetectedWithLeftNeighbor(targetSection, targetPosition, geneValue);
                    if (!conflict)
                    {
                        var lastGeneValue = section[position + insertable - 1];
                        conflict = ConflictDetectedWithRightNeighbor(targetSection, targetPosition, lastGeneValue);
                    }

                    if (conflict)
                    {
                        break;
                    }

                    InsertGenes(chromosome, targetSectionIndex, targetPosition, section, position, insertable);
                    position += insertable;
                }

                if (position > source.Position || retryCount-- < 0)
                {
                    break;
                }
            }

            CleanOutSections(chromosome);
            return(chromosome);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Make a (mostly random) decision on the using of the conflict list
        /// </summary>
        /// <param name="chromosome"></param>
        /// <returns></returns>
        public bool ConflictShouldBeEliminated(SortedSubsetChromosome chromosome)
        {
            if (chromosome.ConflictList.Count == 0)
            {
                return(false);
            }

            var reducingConflictPossibility = ParameterSet.GetValue(ParameterNames.ConflictReducingProbability);
            var rnd = Random.GetDouble(0, 1);

            return(rnd < reducingConflictPossibility);
        }
Ejemplo n.º 9
0
        private int GetSectionToEliminate(SortedSubsetChromosome chromosome)
        {
            var provider = new StochasticProvider <int>(this.Random);

            for (int s = 0; s < chromosome.Sections.Length; s++)
            {
                var section     = chromosome.Sections[s];
                var probability = chromosome.TotalCount / (double)section.Length;
                provider.Add(s, probability);
            }

            int chosen = provider.GetOne();

            return(chosen);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Returns the count of genes can be inserted to a given section and position inside it.
        /// </summary>
        /// <param name="chromosome">The multi-section chromosome which the operator works within</param>
        /// <param name="sectionIndex">The index of the section</param>
        /// <param name="insertPosition">The position inside the section</param>
        /// <param name="genesToInsert">The gene array to insert</param>
        /// <param name="firstGeneIndex">The first gene's index inside the gene array</param>
        /// <returns>The possible count of the insertable genes</returns>
        public int CountInsertableGenes(SortedSubsetChromosome chromosome, int sectionIndex,
                                        int insertPosition, int[] genesToInsert, int firstGeneIndex)
        {
            var count = 0;

            while ((firstGeneIndex + count < genesToInsert.Length) &&
                   ((insertPosition > chromosome.Sections[sectionIndex].Length - 1) ||
                    (genesToInsert[firstGeneIndex + count] < chromosome.Sections[sectionIndex][insertPosition])
                   )
                   )
            {
                count++;
            }

            return(count);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Delete given number of genes from a chromosome section and position inside it
        /// </summary>
        /// <param name="chromosome">The multi-section chromosome which the operator works within</param>
        /// <param name="sectionIndex">The index of the section</param>
        /// <param name="position">The position inside the section</param>
        /// <param name="count">The number of genes to be deleted</param>
        public void DeleteGenesFromSection(SortedSubsetChromosome chromosome, int sectionIndex, int position, int count)
        {
            int[] section = chromosome.Sections[sectionIndex];
            int[] temp    = new int[section.Length - count];

            if (position > 0)
            {
                Array.Copy(section, 0, temp, 0, position);
            }

            if (position < section.Length - count + 1)
            {
                Array.Copy(section, position + count, temp, position, section.Length - position - count);
            }

            chromosome.Sections[sectionIndex] = temp;
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Returns a new random position
        /// </summary>
        /// <param name="chromosome"></param>
        /// <returns></returns>
        public GenePosition GetRandomSectionAndPosition(SortedSubsetChromosome chromosome)
        {
            var sectionLength      = 0;
            var sourceSectionIndex = 0;

            while (sectionLength == 0)
            {
                sourceSectionIndex = Random.GetInt(0, chromosome.Sections.Length);
                sectionLength      = chromosome.Sections[sourceSectionIndex].Length;
            }

            var sourcePosition = Random.GetInt(0, chromosome.Sections[sourceSectionIndex].Length);

            var source = new GenePosition(sourceSectionIndex, sourcePosition);

            return(source);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Deletes all the sections with length of 0
        /// </summary>
        /// <param name="chromosome">The multi-section chromosome which the operator works within</param>
        /// <returns>True if at least one chromosome is eliminated</returns>
        public bool CleanOutSections(SortedSubsetChromosome chromosome)
        {
            //TODO: simlpy move the sections?
            int[][] sections = chromosome.Sections;

            var sectionsToDeleteCount = 0;

            foreach (var section in sections)
            {
                if (section.Length == 0)
                {
                    sectionsToDeleteCount++;
                }
            }

            if (sectionsToDeleteCount == 0)
            {
                return(false);
            }

            int[][] temp = new int[sections.Length - sectionsToDeleteCount][];

            int sourceStart = 0;
            int targetStart = 0;

            for (int s = 0; s < sections.Length; s++)
            {
                if (sections[s].Length == 0)
                {
                    if (sourceStart < s)
                    {
                        Array.Copy(sections, sourceStart, temp, targetStart, s - sourceStart);
                        targetStart += s - sourceStart;
                    }
                    sourceStart = s + 1;
                }
            }
            if (sourceStart < sections.Length)
            {
                Array.Copy(sections, sourceStart, temp, targetStart, sections.Length - sourceStart);
            }

            chromosome.Sections = temp;

            return(true);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Delete one section completely from the chromosome
        /// </summary>
        /// <param name="chromosome">The multi-section chromosome which the operator works within</param>
        /// <param name="sectionIndex">The index of the section to be deleted</param>
        public void DeleteSection(SortedSubsetChromosome chromosome, int sectionIndex)
        {
            //TODO: simlpy move the sections?
            int[][] sections = chromosome.Sections;
            int[][] temp     = new int[sections.Length - 1][];

            if (sectionIndex > 0)
            {
                Array.Copy(sections, 0, temp, 0, sectionIndex);
            }

            if (sectionIndex < sections.Length)
            {
                Array.Copy(sections, sectionIndex + 1, temp, sectionIndex, sections.Length - sectionIndex - 1);
            }

            chromosome.Sections = temp;
        }
Ejemplo n.º 15
0
        public SortedSubsetChromosome IncrementNumberOfSections(SortedSubsetChromosome chromosome, LinkedList <int> sectionList)
        {
            int length      = chromosome.Sections.Length;
            var newSections = new int[length + 1][];

            Array.Copy(chromosome.Sections, newSections, length);

            newSections[length] = new int[sectionList.Count];
            var node = sectionList.First;

            for (int i = 0; i < sectionList.Count; i++)
            {
                newSections[length][i] = node.Value;
                node = node.Next;
            }

            chromosome.Sections = newSections;
            return(chromosome);
        }
Ejemplo n.º 16
0
        public override SortedSubsetChromosome Mutate(SortedSubsetChromosome chromosome)
        {
            if (chromosome == null)
            {
                return(null);
            }

            var numberOfGenesToReplace = GetNumberOfGenesToChange(chromosome);

            int retryCount = ParameterSet.GetInt(ParameterNames.FailedMutationRetryCount);
            int replaced   = 0;

            var targetList = new LinkedList <int>();

            for (int i = 0; i < numberOfGenesToReplace; i++)
            {
                var tryCount = retryCount;

                while (true)
                {
                    GenePosition source    = GetSourceSectionAndPosition(chromosome);
                    var          geneValue = chromosome.Sections[source.Section][source.Position];
                    var          success   = InsertGeneToLinkedList(targetList, geneValue);
                    if (success)
                    {
                        DeleteGenesFromSection(chromosome, source.Section, source.Position, 1);
                        replaced++;
                    }

                    if (success || tryCount-- < 0)
                    {
                        break;
                    }
                }
            }

            IncrementNumberOfSections(chromosome, targetList);

            CleanOutSections(chromosome);

            return(chromosome);
        }
Ejemplo n.º 17
0
        public override SortedSubsetChromosome Mutate(SortedSubsetChromosome chromosome)
        {
            if (chromosome == null)
            {
                return(null);
            }
            if (chromosome.Sections.Length < 2)
            {
                return(null);
            }

            var sectionToEliminate = GetSectionToEliminate(chromosome);

            for (int g = chromosome.Sections[sectionToEliminate].Length - 1; g >= 0; g--)
            {
                var geneValue = chromosome.Sections[sectionToEliminate][g];
                var target    = GetTargetSection(chromosome, sectionToEliminate, geneValue);

                if (target == null)
                {
                    continue;
                }

                InsertGenes(chromosome, target.Section, target.Position, new int[] { geneValue }, 0, 1);
                DeleteGenesFromSection(chromosome, sectionToEliminate, g, 1);
            }

            bool eliminated = CleanOutSections(chromosome);

            //TODO: Delete this
            //if (eliminated)
            //{
            //    var yepp = true;
            //}
            return(chromosome);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Insert one gene into a chromosome section and position inside it
        /// </summary>
        /// <param name="chromosome">The multi-section chromosome which the operator works within</param>
        /// <param name="sectionIndex">The index of the section</param>
        /// <param name="insertPosition">The position inside the section</param>
        /// <param name="genesToInsert">The array of genes to insert</param>
        public bool InsertGenes(SortedSubsetChromosome chromosome, int sectionIndex, int insertPosition, int[] genesToInsert, int firstGeneIndex, int count)
        {
            if (ConflictDetectedWithLeftNeighbor(chromosome.Sections[sectionIndex], insertPosition,
                                                 genesToInsert[firstGeneIndex]))
            {
                return(false);
            }

            if (ConflictDetectedWithRightNeighbor(chromosome.Sections[sectionIndex], insertPosition,
                                                  genesToInsert[firstGeneIndex + count - 1]))
            {
                return(false);
            }

            int[] section = chromosome.Sections[sectionIndex];
            int[] temp    = new int[section.Length + count];

            if (insertPosition > 0)
            {
                Array.Copy(section, 0, temp, 0, insertPosition);
            }

            if (count > 0)
            {
                Array.Copy(genesToInsert, firstGeneIndex, temp, insertPosition, count);
            }

            if (insertPosition < section.Length)
            {
                Array.Copy(section, insertPosition, temp, insertPosition + count, section.Length - insertPosition);
            }

            chromosome.Sections[sectionIndex] = temp;

            return(true);
        }
Ejemplo n.º 19
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);
        }
Ejemplo n.º 20
0
        public override IList <IChromosome> Cross(IChromosome iparent0, IChromosome iparent1)
        {
            var children = new List <IChromosome>();

            var parent1 = iparent0 as SortedSubsetChromosome;
            var parent2 = iparent1 as SortedSubsetChromosome;

            var sectionsCount = parent1.Sections.Length > parent2.Sections.Length
                ? parent1.Sections.Length
                : parent2.Sections.Length;

            var totalCount = parent1.TotalCount > parent2.TotalCount
                ? parent1.TotalCount
                : parent2.TotalCount;

            var  child0           = new int[sectionsCount][];
            var  child1           = new int[sectionsCount][];
            bool child0Conflicted = false;
            bool child1Conflicted = false;

            int retryCount = ParameterSet.GetInt(ParameterNames.FailedCrossoverRetryCount);

            while (true)
            {
                var crossoverPosition = Random.GetInt(0, totalCount);

                for (int sectionIndex = 0; sectionIndex < sectionsCount; sectionIndex++)
                {
                    bool section0Exists = (parent1.Sections.Length > sectionIndex);
                    var  section0       = section0Exists ? parent1.Sections[sectionIndex] : new int[0];
                    var  position0      = section0Exists ? FindNewGenePosition(section0, crossoverPosition) : 0;

                    bool section1Exists = (parent2.Sections.Length > sectionIndex);
                    var  section1       = section1Exists ? parent2.Sections[sectionIndex] : new int[0];
                    var  position1      = section1Exists ? FindNewGenePosition(section1, crossoverPosition) : 0;

                    var child0Section = MergeSections(section0, position0, section1, position1, ref child0Conflicted);
                    child0[sectionIndex] = child0Section;

                    var child1Section = MergeSections(section1, position1, section0, position0, ref child1Conflicted);
                    child1[sectionIndex] = child1Section;

                    //TODO: Merge conflict lists

                    if (child0Conflicted && child1Conflicted)
                    {
                        break;
                    }
                }

                if (!child0Conflicted || !child1Conflicted)
                {
                    ////TODO: Delete this
                    //var conflictedPositions0 =
                    //    child0Conflicted ? new List<GenePosition>() : SortedSubsetChromosomeValidator.SearchForConflict(child0);
                    //var conflictedPositions1 =
                    //    child1Conflicted ? new List<GenePosition>() : SortedSubsetChromosomeValidator.SearchForConflict(child1);

                    //if (conflictedPositions0.Count > 0 || conflictedPositions1.Count > 0)
                    //{
                    //    bool error = true;  //For breakpoints
                    //    throw new ApplicationException("Conflict between neighboring values! (Crossover: OnePointCrossover)");
                    //}

                    break;
                }

                if (retryCount-- < 0)
                {
                    break;
                }

                child0           = new int[sectionsCount][];
                child1           = new int[sectionsCount][];
                child0Conflicted = false;
                child1Conflicted = false;
            }

            if (!child0Conflicted)
            {
                var child0Chromosome = new SortedSubsetChromosome(child0);
                CleanOutSections(child0Chromosome);
                children.Add(child0Chromosome);
            }

            if (!child1Conflicted)
            {
                var child1Chromosome = new SortedSubsetChromosome(child1);
                CleanOutSections(child1Chromosome);
                children.Add(child1Chromosome);
            }

            return(children);
        }
Ejemplo n.º 21
0
 public int GetGeneValue(SortedSubsetChromosome chromosome, GenePosition genePosition)
 {
     return(chromosome.Sections[genePosition.Section][genePosition.Position]);
 }
Ejemplo n.º 22
0
 /// <summary>
 /// Returns a new position from the conflict-list or randomly
 /// </summary>
 /// <param name="chromosome"></param>
 /// <returns></returns>
 public GenePosition GetSourceSectionAndPosition(SortedSubsetChromosome chromosome)
 {
     return(ConflictShouldBeEliminated(chromosome)
         ? GetConflictedSectionAndPosition(chromosome)
         : GetRandomSectionAndPosition(chromosome));
 }