Fitness function for times series prediction problem

The fitness function calculates fitness value of GP and GEP chromosomes with the aim of solving times series prediction problem using sliding window method. The fitness function's value is computed as: 100.0 / ( error + 1 ) where error equals to the sum of absolute differences between predicted value and actual future value.

Sample usage:

// number of points from the past used to predict new one int windowSize = 5; // time series to predict double[] data = new double[13] { 1, 2, 4, 7, 11, 16, 22, 29, 37, 46, 56, 67, 79 }; // constants double[] constants = new double[10] { 1, 2, 3, 5, 7, 11, 13, 17, 19, 23 }; // create population Population population = new Population( 100, new GPTreeChromosome( new SimpleGeneFunction( windowSize + constants.Length ) ), new TimeSeriesPredictionFitness( data, windowSize, 1, constants ), new EliteSelection( ) ); // run one epoch of the population population.RunEpoch( );
Inheritance: IFitnessFunction
Example #1
0
        // Worker thread
        void SearchSolution()
        {
            // constants
            double[] constants = new double[10] { 1, 2, 3, 5, 7, 11, 13, 17, 19, 23 };
            // create fitness function
            TimeSeriesPredictionFitness fitness = new TimeSeriesPredictionFitness(
                data, windowSize, predictionSize, constants);
            // create gene function
            IGPGene gene = (functionsSet == 0) ?
                (IGPGene)new SimpleGeneFunction(windowSize + constants.Length) :
                (IGPGene)new ExtendedGeneFunction(windowSize + constants.Length);
            // create population
            Population population = new Population(populationSize,
                (geneticMethod == 0) ?
                (IChromosome)new GPTreeChromosome(gene) :
                (IChromosome)new GEPChromosome(gene, headLength),
                fitness,
                (selectionMethod == 0) ? (ISelectionMethod)new EliteSelection() :
                (selectionMethod == 1) ? (ISelectionMethod)new RankSelection() :
                (ISelectionMethod)new RouletteWheelSelection()
                );
            // iterations
            int i = 1;
            // solution array
            int solutionSize = data.Length - windowSize;
            double[,] solution = new double[solutionSize, 2];
            double[] input = new double[windowSize + constants.Length];

            // calculate X values to be used with solution function
            for (int j = 0; j < solutionSize; j++)
            {
                solution[j, 0] = j + windowSize;
            }
            // prepare input
            Array.Copy(constants, 0, input, windowSize, constants.Length);

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

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

                    // calculate best function and prediction error
                    double learningError = 0.0;
                    double predictionError = 0.0;
                    // go through all the data
                    for (int j = 0, n = data.Length - windowSize; j < n; j++)
                    {
                        // put values from current window as variables
                        for (int k = 0, b = j + windowSize - 1; k < windowSize; k++)
                        {
                            input[k] = data[b - k];
                        }

                        // evalue the function
                        solution[j, 1] = PolishExpression.Evaluate(bestFunction, input);

                        // calculate prediction error
                        if (j >= n - predictionSize)
                        {
                            predictionError += Math.Abs(solution[j, 1] - data[windowSize + j]);
                        }
                        else
                        {
                            learningError += Math.Abs(solution[j, 1] - data[windowSize + j]);
                        }
                    }
                    // update solution on the chart
                    chart.UpdateDataSeries("solution", solution);

                    // set current iteration's info
                    SetText(currentIterationBox, i.ToString());
                    SetText(currentLearningErrorBox, learningError.ToString("F3"));
                    SetText(currentPredictionErrorBox, predictionError.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());
            for (int j = windowSize, k = 0, n = data.Length; j < n; j++, k++)
            {
                AddSubItem(dataList, j, solution[k, 1].ToString());
            }

            // enable settings controls
            EnableControls(true);
        }