Fitness function for symbolic regression (function approximation) problem

The fitness function calculates fitness value of GP and GEP chromosomes with the aim of solving symbolic regression problem. The fitness function's value is computed as: 100.0 / ( error + 1 ) where error equals to the sum of absolute differences between function values (computed using the function encoded by chromosome) and input values (function to be approximated).

Sample usage:

// constants double[] constants = new double[5] { 1, 2, 3, 5, 7 }; // function to be approximated double[,] data = new double[5, 2] { {1, 1}, {2, 3}, {3, 6}, {4, 10}, {5, 15} }; // create population Population population = new Population( 100, new GPTreeChromosome( new SimpleGeneFunction( 1 + constants.Length ) ), new SymbolicRegressionFitness( data, constants ), new EliteSelection( ) ); // run one epoch of the population population.RunEpoch( );
Inheritance: IFitnessFunction
コード例 #1
0
ファイル: Approximation.cs プロジェクト: accord-net/framework
        // Worker thread
        void SearchSolution()
        {
            // create fitness function
            SymbolicRegressionFitness fitness = new SymbolicRegressionFitness(data, new double[] { 1, 2, 3, 5, 7 });
            // create gene function
            IGPGene gene = (functionsSet == 0) ?
                (IGPGene)new SimpleGeneFunction(6) :
                (IGPGene)new ExtendedGeneFunction(6);
            // 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
            int i = 1;
            // solution array
            double[,] solution = new double[50, 2];
            double[] input = new double[6] { 0, 1, 2, 3, 5, 7 };

            // calculate X values to be used with solution function
            for (int j = 0; j < 50; j++)
            {
                solution[j, 0] = chart.RangeX.Min + (double)j * chart.RangeX.Length / 49;
            }

            // loop
            while (!needToStop)
            {
                // run one epoch of genetic algorithm
                population.RunEpoch();

                try
                {
                    // get best solution
                    string bestFunction = population.BestChromosome.ToString();

                    // calculate best function
                    for (int j = 0; j < 50; j++)
                    {
                        input[0] = solution[j, 0];
                        solution[j, 1] = PolishExpression.Evaluate(bestFunction, input);
                    }
                    chart.UpdateDataSeries("solution", solution);
                    // calculate error
                    double error = 0.0;
                    for (int j = 0, k = data.GetLength(0); j < k; j++)
                    {
                        input[0] = data[j, 0];
                        error += Math.Abs(data[j, 1] - PolishExpression.Evaluate(bestFunction, input));
                    }

                    // set current iteration's info
                    SetText(currentIterationBox, i.ToString());
                    SetText(currentErrorBox, error.ToString("F3"));
                }
                catch
                {
                    // remove any solutions from chart in case of any errors
                    chart.UpdateDataSeries("solution", null);
                }

                // increase current iteration
                i++;

                //
                if ((iterations != 0) && (i > iterations))
                    break;
            }

            // show solution
            SetText(solutionBox, population.BestChromosome.ToString());

            // enable settings controls
            EnableControls(true);
        }