当前位置: 首页>>代码示例>>C#>>正文


C# IChromosome.GetGenes方法代码示例

本文整理汇总了C#中IChromosome.GetGenes方法的典型用法代码示例。如果您正苦于以下问题:C# IChromosome.GetGenes方法的具体用法?C# IChromosome.GetGenes怎么用?C# IChromosome.GetGenes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IChromosome的用法示例。


在下文中一共展示了IChromosome.GetGenes方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Draw

        /// <summary>
        /// Draws the sample.
        /// </summary>
        /// <param name="bestChromosome">The current best chromosome</param>
        public override void Draw(IChromosome bestChromosome)
        {
            var c = bestChromosome as TspChromosome;
            Console.WriteLine("Cities: {0:n0}", c.Length);
            Console.WriteLine("Distance: {0:n2}", c.Distance);

            var cities = bestChromosome.GetGenes().Select(g => g.Value.ToString()).ToArray();
            Console.WriteLine("City tour: {0}", string.Join(", ", cities));
        }
开发者ID:mahmoud-samy-symbyo,项目名称:GeneticSharp,代码行数:13,代码来源:TspSampleController.cs

示例2: CreateOffspring

        private static IChromosome CreateOffspring(IChromosome leftParent, IChromosome rightParent, int leftParentPoint, int rightParentPoint)
        {
            var offspring = leftParent.CreateNew();

            offspring.Resize(leftParentPoint + (rightParent.Length - rightParentPoint));
            offspring.ReplaceGenes(0, leftParent.GetGenes().Take(leftParentPoint).ToArray());
            offspring.ReplaceGenes(leftParentPoint, rightParent.GetGenes().Skip(rightParentPoint).ToArray());

            return offspring;
        }
开发者ID:denmerc,项目名称:GeneticSharp,代码行数:10,代码来源:CutAndSpliceCrossover.cs

示例3: Evaluate

        public double Evaluate(IChromosome chromosome)
        {
            if (SupportsParallel) {
                Thread.Sleep (ParallelSleep);
            }

            var genes = chromosome.GetGenes();
            double f = genes.Sum(g => (int) g.Value)  / 20f;

            if(f > 1)
            {
                f = 0;
            }

            return f;
        }
开发者ID:denisbarboni,项目名称:Projeto-AG,代码行数:16,代码来源:FitnessStub.cs

示例4: PerformMutate

        /// <summary>
        /// Mutate the specified chromosome.
        /// </summary>
        /// <param name="chromosome">The chromosome.</param>
        /// <param name="probability">The probability to mutate each chromosome.</param>
        protected override void PerformMutate(IChromosome chromosome, float probability)
        {
            if (chromosome.Length < 3)
            {
                throw new MutationException(this, "A chromosome should have, at least, 3 genes. {0} has only {1} gene.".With(chromosome.GetType().Name, chromosome.Length));
            }

            if (RandomizationProvider.Current.GetDouble() <= probability)
            {
                var indexes = RandomizationProvider.Current.GetUniqueInts(2, 0, chromosome.Length).OrderBy(i => i).ToArray();
                var firstIndex = indexes[0];
                var secondIndex = indexes[1];

                var revertedSequence = chromosome.GetGenes().Skip(firstIndex).Take((secondIndex - firstIndex) + 1).Reverse().ToArray();

                chromosome.ReplaceGenes(firstIndex, revertedSequence);
            }
        }
开发者ID:denmerc,项目名称:GeneticSharp,代码行数:23,代码来源:ReverseSequenceMutation.cs

示例5: CreateChild

        /// <summary>
        /// Creates the child.
        /// </summary>
        /// <returns>The child.</returns>
        /// <param name="leftParent">Left parent.</param>
        /// <param name="rightParent">Right parent.</param>
        private IChromosome CreateChild(IChromosome leftParent, IChromosome rightParent)
        {
            var cutGenesCount = SwapPointIndex + 1;
            var child = leftParent.CreateNew();
            child.ReplaceGenes(0, leftParent.GetGenes().Take(cutGenesCount).ToArray());
            child.ReplaceGenes(cutGenesCount, rightParent.GetGenes().Skip(cutGenesCount).ToArray());

            return child;
        }
开发者ID:denisbarboni,项目名称:Projeto-AG,代码行数:15,代码来源:OnePointCrossover.cs

示例6: Evaluate

        /// <summary>
        /// Performs the evaluation against the specified chromosome.
        /// </summary>
        /// <param name="chromosome">The chromosome to be evaluated.</param>
        /// <returns>The fitness of the chromosome.</returns>
        public double Evaluate(IChromosome chromosome)
        {
            var genes = chromosome.GetGenes ();
            var distanceSum = 0.0;
            var lastCityIndex = Convert.ToInt32 (genes [0].Value);
            var citiesIndexes = new List<int>();
            citiesIndexes.Add(lastCityIndex);

            foreach (var g in genes) {
                var currentCityIndex = Convert.ToInt32 (g.Value);
                distanceSum += CalcDistanceTwoCities(Cities[currentCityIndex], Cities[lastCityIndex]);
                lastCityIndex = currentCityIndex;

                citiesIndexes.Add(lastCityIndex);
            }

            distanceSum += CalcDistanceTwoCities(Cities[citiesIndexes.Last()], Cities[citiesIndexes.First()]);

            var fitness = 1.0 - (distanceSum / (Cities.Count * 1000.0));

            ((TspChromosome)chromosome).Distance = distanceSum;

            // There is repeated cities on the indexes?
            var diff =  Cities.Count - citiesIndexes.Distinct ().Count ();

            if (diff > 0) {
                fitness /= diff;
            }

            if (fitness < 0)
            {
                fitness = 0;
            }

            return fitness;
        }
开发者ID:rfrerebe,项目名称:GeneticSharp,代码行数:41,代码来源:TspFitness.cs

示例7: CreateChild

        /// <summary>
        /// Creates the child.
        /// </summary>
        /// <returns>The child.</returns>
        /// <param name="leftParent">Left parent.</param>
        /// <param name="rightParent">Right parent.</param>
        protected override IChromosome CreateChild(IChromosome leftParent, IChromosome rightParent)
        {
            var firstCutGenesCount = SwapPointOneGeneIndex + 1;
            var secondCutGenesCount = SwapPointTwoGeneIndex + 1;
            var child = leftParent.CreateNew();
            child.ReplaceGenes(0, leftParent.GetGenes().Take(firstCutGenesCount).ToArray());
            child.ReplaceGenes(firstCutGenesCount, rightParent.GetGenes().Skip(firstCutGenesCount).Take(secondCutGenesCount - firstCutGenesCount).ToArray());
            child.ReplaceGenes(secondCutGenesCount, leftParent.GetGenes().Skip(secondCutGenesCount).ToArray());

            return child;
        }
开发者ID:denmerc,项目名称:GeneticSharp,代码行数:17,代码来源:TwoPointCrossover.cs

示例8: CreateChild

        /// <summary>
        /// Creates the child.
        /// </summary>
        /// <returns>The child.</returns>
        /// <param name="firstParent">First parent.</param>
        /// <param name="secondParent">Second parent.</param>
        /// <param name="middleSectionBeginIndex">Middle section begin index.</param>
        /// <param name="middleSectionEndIndex">Middle section end index.</param>
        private static IChromosome CreateChild(IChromosome firstParent, IChromosome secondParent, int middleSectionBeginIndex, int middleSectionEndIndex)
        {
            var middleSectionGenes = firstParent.GetGenes().Skip(middleSectionBeginIndex).Take((middleSectionEndIndex - middleSectionBeginIndex) + 1);
            var secondParentRemainingGenes = secondParent.GetGenes().Except(middleSectionGenes).GetEnumerator();
            var child = firstParent.CreateNew();

            for (int i = 0; i < firstParent.Length; i++)
            {
                var firstParentGene = firstParent.GetGene(i);

                if (i >= middleSectionBeginIndex && i <= middleSectionEndIndex)
                {
                    child.ReplaceGene(i, firstParentGene);
                }
                else
                {
                    secondParentRemainingGenes.MoveNext();
                    child.ReplaceGene(i, secondParentRemainingGenes.Current);
                }
            }

            return child;
        }
开发者ID:denmerc,项目名称:GeneticSharp,代码行数:31,代码来源:OrderedCrossover.cs


注:本文中的IChromosome.GetGenes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。