Exemple #1
0
        public float Evaluate(IChromosome ch, IFunctionSet functionSet)
        {
            var expTree = ((GPChromosome)ch).expressionTree;

            double fitness    = 0;
            double rowFitness = 0.0;
            double y;

            //index of output parameter
            int indexOutput = Globals.gpterminals.NumConstants + Globals.gpterminals.NumVariables;

            for (int i = 0; i < Globals.gpterminals.RowCount; i++)
            {
                // evalue the function agains eachh rowData
                y = functionSet.Evaluate(expTree, i);

                // check for correct numeric value
                if (double.IsNaN(y) || double.IsInfinity(y))
                {
                    return(float.NaN);
                }

                //this fitness algoritm is specialized only for Binary and Classification problems
                if (m_ProblemType == ColumnDataType.Categorical || m_ProblemType == ColumnDataType.Binary)
                {
                    var valClass = Experiment.getCategoryFromNumeric(y, Globals.classCount);

                    if (double.IsNaN(valClass))
                    {
                        return(float.NaN);
                    }
                    //check is the calculated value correct
                    var val = Globals.gpterminals.TrainingData[i][indexOutput] == valClass ? 1 : 0;
                    //add the result to the fitness
                    rowFitness += val;
                }
                else//thros exception if the problems is not as expected
                {
                    throw new Exception("Problem type is unknown!");
                }
            }

            if (double.IsNaN(rowFitness) || double.IsInfinity(rowFitness))
            {
                fitness = float.NaN;
            }
            else
            {
                fitness = (float)(rowFitness / Globals.gpterminals.RowCount) * 1000.0;
            }

            return((float)Math.Round(fitness, 2));
        }
Exemple #2
0
        public float Evaluate(IChromosome ch, IFunctionSet functionSet)
        {
            var expTree = ((GPChromosome)ch).expressionTree;

            double fitness = 0;
            double rowFitness = 0.0;
            double y, SS_tot = 0;

            //index of output parameter
            int indexOutput = Globals.gpterminals.NumConstants + Globals.gpterminals.NumVariables;

            for (int i = 0; i < Globals.gpterminals.RowCount; i++)
            {
                // evalue the function agains eachh rowData
                y = functionSet.Evaluate(expTree, i);

                // check for correct numeric value
                if (double.IsNaN(y) || double.IsInfinity(y))
                {
                    return(float.NaN);
                }

                //Calculate square error
                rowFitness += Math.Pow(y - Globals.gpterminals.TrainingData[i][indexOutput], 2);
                SS_tot     += Math.Pow(Globals.gpterminals.TrainingData[i][indexOutput] - Globals.gpterminals.AverageValue, 2);
            }

            if (SS_tot != 0)
            {
                rowFitness = rowFitness / SS_tot;
            }

            if (double.IsNaN(rowFitness) || double.IsInfinity(rowFitness))
            {
                fitness = float.NaN;
            }
            else
            {
                fitness = (float)((1.0 / (1.0 + rowFitness / Globals.gpterminals.RowCount)) * 1000.0);
            }

            return((float)Math.Round(fitness, 2));
        }
Exemple #3
0
        //Calculate model agains specific data
        public static double[] CalculateGPModel(GPNode node, bool btrainingData = true)
        {
            validateFunctionSet();
            double[][] data = btrainingData ? gpterminals.TrainingData : gpterminals.TestingData;

            if (data == null)
            {
                return(null);
            }

            var model = new double[data.Length];

            for (int i = 0; i < data.Length; i++)
            {
                model[i] = functions.Evaluate(node, i, btrainingData);
            }

            return(model);
        }
Exemple #4
0
        public float Evaluate(IChromosome ch, IFunctionSet functionSet)
        {
            var expTree = ((GPChromosome)ch).expressionTree;

            double fitness    = 0;
            double rowFitness = 0.0;
            double y;

            //index of output parameter
            int indexOutput = Globals.gpterminals.NumConstants + Globals.gpterminals.NumVariables;

            for (int i = 0; i < Globals.gpterminals.RowCount; i++)
            {
                // evalue the function agains eachh rowData
                y = functionSet.Evaluate(expTree, i);

                // check for correct numeric value
                if (double.IsNaN(y) || double.IsInfinity(y))
                {
                    return(float.NaN);
                }

                var temp = y - Globals.gpterminals.TrainingData[i][indexOutput];
                rowFitness += temp * temp;
            }

            if (double.IsNaN(rowFitness) || double.IsInfinity(rowFitness))
            {
                fitness = float.NaN;
            }
            else
            {
                fitness = (float)((1.0 / (1.0 + rowFitness)) * 1000.0);
            }

            return((float)Math.Round(fitness, 2));
        }
        /// <summary>
        /// Evaluates function agains terminals
        /// </summary>
        /// <param name="chromosome"></param>
        /// <param name="functionSet"></param>
        /// <returns></returns>
        public float Evaluate(IChromosome chromosome, IFunctionSet functionSet)
        {
            GANumChromosome ch = chromosome as GANumChromosome;

            if (ch == null)
            {
                return(0);
            }
            else
            {
                //prepare terminals
                var term = Globals.gpterminals.SingleTrainingData;
                for (int i = 0; i < ch.val.Length; i++)
                {
                    term[i] = ch.val[i];
                }

                var y = functionSet.Evaluate(_funToOptimize, -1);


                if (double.IsNaN(y) || double.IsInfinity(y))
                {
                    y = float.NaN;
                }

                //Save output in to output variable
                term[term.Length - 1] = y;

                if (IsMinimize)
                {
                    y *= -1;
                }

                return((float)y);
            }
        }