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


C# Chromosome.Count方法代码示例

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


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

示例1: BinaryCrossover

        public static Population BinaryCrossover(Chromosome x1, Chromosome x2, Random r)
        {
            var genelist1 = new List<Gene>();
            var genelist2 = new List<Gene>();

            //for each gene in the chromosomes, we crossover
            for (int i = 0; i < x1.Count(); i++)
            {
                Gene c1, c2;
                x1[i].Crossover(x2[i], out c1, out c2);
                genelist1.Add(c1);
                genelist2.Add(c2);
            }

            var x1_t = new Chromosome(genelist1);
            var x2_t = new Chromosome(genelist2);

            var l1 = new List<Chromosome>();
            l1.Add(x1_t);
            l1.Add(x2_t);

            return new Population(l1);
        }
开发者ID:ifzz,项目名称:QuantSys,代码行数:23,代码来源:Crossover.cs

示例2: Run

        public void Run()
        {
            InitializePopulation();

            double maxFitness = 0;

            while (_currentTrial++ < Trials)
            {
                
                Console.WriteLine("Starting trial " + _currentTrial + "...");

                string filename = QSConstants.DEFAULT_DATA_FILEPATH + "result" + _currentTrial + ".txt";

                using (var file = new StreamWriter(@filename))
                {
                    for (int generation = 0; generation < Generations; generation++)
                    {
                        Console.WriteLine(generation);
                        var newPop = new Population();

                        //elitism
                        Population elite = _currentPopulation.ElitismSelection(2);

                        newPop.JoinPopulation(elite);

                        //populate rest of population with children
                        while (newPop.PopulationSize < PopulationSize)
                        {
                            //selection
                            Population parents = _currentPopulation.SimpleRouletteSelection(2, _randomSeed);

                            //crossover
                            Population children = Crossover.BinaryCrossover(parents[0], parents[1], _randomSeed);

                            newPop.JoinPopulation(children);
                        }

                        newPop.MutatePopulation(MutationRate, _randomSeed);

                        double average = 0;

                        //calculate fitness for new population
                        foreach (Chromosome chromosome in newPop.Chromosomes)
                        {
                            if (chromosome.Fitness.Equals(double.NaN))
                                chromosome.CalculateFitness(_fitnessFunction);
                            if (chromosome.Fitness > maxFitness)
                            {
                                maxFitness = chromosome.Fitness;
                                maxC = chromosome;

                                file.WriteLine("Generation: {0}", generation);
                                file.WriteLine("Fitness: {0}", maxFitness);
                                for (int i = 0; i < maxC.Count(); i++)
                                {
                                    file.WriteLine("Gene " + i + ":" + maxC[i]);
                                }

                                file.WriteLine();
                                file.Flush();
                            }

                            average += chromosome.Fitness;
                            //Console.WriteLine(chromosome.Fitness + ": x:" + (double)chromosome.GeneList[0].geneValue + "; y:" + (double)chromosome.GeneList[1].geneValue);
                        }

                        //Console.WriteLine(average/newPop.Chromosomes.Count);
                        //Console.WriteLine("-----------------------------------");

                        _currentPopulation = newPop;
                    }
                }

                maxC.CalculateFitness(_fitnessFunction);
            }

            Console.WriteLine("Execution complete.");
        }
开发者ID:ifzz,项目名称:QuantSys,代码行数:78,代码来源:GeneticAlgorithm.cs


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