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


C# Population.FindBestChromosome方法代码示例

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


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

示例1: Migrate

        /// <summary>
        /// Perform migration between two populations.
        /// </summary>
        /// 
        /// <param name="anotherPopulation">Population to do migration with.</param>
        /// <param name="numberOfMigrants">Number of chromosomes from each population to migrate.</param>
        /// <param name="migrantsSelector">Selection algorithm used to select chromosomes to migrate.</param>
        /// 
        /// <remarks><para>The method performs migration between two populations - current and the
        /// <paramref name="anotherPopulation">specified one</paramref>. During migration
        /// <paramref name="numberOfMigrants">specified number</paramref> of chromosomes is choosen from
        /// each population using <paramref name="migrantsSelector">specified selection algorithms</paramref>
        /// and put into another population replacing worst members there.</para></remarks>
        /// 
        public void Migrate( Population anotherPopulation, int numberOfMigrants, ISelectionMethod migrantsSelector )
        {
            int currentSize = this.size;
            int anotherSize = anotherPopulation.Size;

            // create copy of current population
            List<IChromosome> currentCopy = new List<IChromosome>( );

            for ( int i = 0; i < currentSize; i++ )
            {
                currentCopy.Add( population[i].Clone( ) );
            }

            // create copy of another population
            List<IChromosome> anotherCopy = new List<IChromosome>( );

            for ( int i = 0; i < anotherSize; i++ )
            {
                anotherCopy.Add( anotherPopulation.population[i].Clone( ) );
            }

            // apply selection to both populations' copies - select members to migrate
            migrantsSelector.ApplySelection( currentCopy, numberOfMigrants );
            migrantsSelector.ApplySelection( anotherCopy, numberOfMigrants );

            // sort original populations, so the best chromosomes are in the beginning
            population.Sort( );
            anotherPopulation.population.Sort( );

            // remove worst chromosomes from both populations to free space for new members
            population.RemoveRange( currentSize - numberOfMigrants, numberOfMigrants );
            anotherPopulation.population.RemoveRange( anotherSize - numberOfMigrants, numberOfMigrants );

            // put migrants to corresponding populations
            population.AddRange( anotherCopy );
            anotherPopulation.population.AddRange( currentCopy );

            // find best chromosomes in each population
            FindBestChromosome( );
            anotherPopulation.FindBestChromosome( );
        }
开发者ID:alexanderlimasilva,项目名称:GeradorExpressoes,代码行数:55,代码来源:Population.cs

示例2: SearchSolution

        public void SearchSolution()
        {
            WriteData saida = new WriteData();

            // create fitness function
            MMREFitness fitness = new MMREFitness(data);
            //SymbolicRegressionFitness fitness = new SymbolicRegressionFitness(data, new double[] { 1, 2, 3, 5, 7, 9});

            // create gene function
            IGPGene gene = (functionsSet == 0) ? (IGPGene)new SimpleGeneFunction(data.GetLength(0)) : (IGPGene)new ExpressionGeneFunction(data.GetLength(0));

            // create population
            Population population = new Population(populationSize, (geneticMethod == 0) ?
                    (IChromosome)new GPTreeChromosome(gene) :
                    (IChromosome)new GEPChromosome(gene, 15),
                fitness,
                (selectionMethod == 0) ? (ISelectionMethod)new EliteSelection() :
                (selectionMethod == 1) ? (ISelectionMethod)new RankSelection() :
                                         (ISelectionMethod)new RouletteWheelSelection()
                );

            // iterations ou numero de gerações a serem utilizadas
            int i = 1;

            // solution array
            double[,] solution = new double[data.GetLength(0), 2];
            double[] input = new double[data.GetLength(0)];
            double[] output = new double[data.GetLength(0)];
            //double[] input = new double[1];

            // Alexander - Define o valor de entrada X com base no dataset
            for (int j = 0; j < data.GetLength(0); j++)
            {
                input[j] = data[j, 1];
            }

            // Alexander - Define se saida sera baseada na função ou nos dados existentes no dataset
            for (int j = 0; j < data.GetLength(0); j++)
            {
                if (useFunction)
                   output[j] = func(input[j]);
                else
                   output[j] = data[j, 0];

                System.Console.WriteLine(String.Format("{0:N}", input[j]) + ", " + String.Format("{0:N}", output[j]));
            }

            // loop
            while (!needToStop)
            {
                System.Console.WriteLine("Geração: " + i.ToString());

                hits = 0;

                if (i <= 1) {
                   //Grava a população gerada e seu fitness em cada rodada
                   nomearq = "Populacao" + ((geneticMethod == 0) ? "_GP_" : "_GEP_") + dataset + "_" + executions.ToString() + "_" + "Geracao_" + i.ToString() + "_";
                   saida.escreveArquivo("Geracao: " + i.ToString() + "\r\n" + population.toString(), nomearq, 1);

                   nomearq = "AnalisedeTempo" + ((geneticMethod == 0) ? "_GP_" : "_GEP_") + dataset + "_" + executions.ToString() + "_" + iterations.ToString();
                   saida.escreveArquivo("DataSet" + "\t" + "Metodo" + "\t" + "Geracao" + "\t" + "MMRE" + "\r\n", nomearq, 0);
                }

                // run one epoch of genetic algorithm
                population.ActualGeneration = i;
                //population.RunEpoch();
                //population.FindBestChromosome();

                try
                {
                    // get best solution
                    population.FindBestChromosome();

                    string bestFunction = population.BestChromosome.ToString().Trim();
                    System.Console.WriteLine("Função: " + RPN2Infix.PostfixToInfix(bestFunction));

                    npred = 0;
                    sum = 0.0;
                    error = 0.0;
                    pred = 0.00;
                    double result = 0.0;
                    double resultgerado = 0.0;

                    // calculate best function
                    for (int j = 0; j < data.GetLength(0); j++)
                    {
                        double HIT_LEVEL = 0.01;
                        double PROBABLY_ZERO = 1.11E-15;
                        double BIG_NUMBER = 1.0e15;

                        System.Console.WriteLine("Valor de entrada: " + input[j]);

                        resultgerado = PolishGerExpression.Evaluate(bestFunction, input, j);

                        // fitness (atual - estimado) / atual
                        result = Math.Abs((output[j] - resultgerado) / output[j]);

                        if (!(result < BIG_NUMBER))       // *NOT* (input.x >= BIG_NUMBER)
                            result = BIG_NUMBER;

//.........这里部分代码省略.........
开发者ID:alexanderlimasilva,项目名称:GeradorExpressoes,代码行数:101,代码来源:Solution.cs


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