Ejemplo n.º 1
0
        /// <summary>
        /// Inserts one gene value into a randomly choosen section and deletes it from its original position
        /// </summary>
        /// <param name="chromosome"></param>
        /// <param name="source"></param>
        /// <returns></returns>
        public bool ReplaceOneGeneToRandomSection(SortedSubsetChromosome chromosome, GenePosition source, int retryCount)
        {
            var geneValue = chromosome.Sections[source.Section][source.Position];

            bool success = false;

            while (true)
            {
                var targetSectionIndex = Random.GetIntWithTabu(0, chromosome.Sections.Length, source.Section);
                var targetSection      = chromosome.Sections[targetSectionIndex];
                var targetPos          = FindNewGenePosition(targetSection, geneValue);

                success = InsertGenes(chromosome, targetSectionIndex, targetPos,
                                      chromosome.Sections[source.Section], source.Position, 1);
                if (success)
                {
                    DeleteGenesFromSection(chromosome, source.Section, source.Position, 1);
                }

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

            return(success);
        }
Ejemplo n.º 2
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.º 3
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.º 4
0
 public int GetGeneValue(SortedSubsetChromosome chromosome, GenePosition genePosition)
 {
     return(chromosome.Sections[genePosition.Section][genePosition.Position]);
 }