Example #1
0
        public string DecodeExpression(GPNode treeExpression, int langOption)
        {
            //Prepare chromoseme for evaluation
            var tokens = treeExpression.ToList();
            int countT = tokens.Count;

            //Stack fr evaluation
            Stack <string> expression = new Stack <string>();

            for (int i = countT - 1; i >= 0; i--)
            {
                if (tokens[i] >= 1000 && tokens[i] < 2000)
                {
                    string varaiable = terminals[tokens[i] - 1000].Name;
                    if (!varaiable.EndsWith(" "))//we add extre space at the end to recognize variable index and value
                    {
                        varaiable += " ";
                    }
                    expression.Push(varaiable);
                }
                else
                {
                    //prepare function arguments for evaluation
                    int    count    = functions[tokens[i] - 2000].Aritry;
                    string function = "";

                    if (langOption == 1)
                    {
                        function = functions[tokens[i] - 2000].MathematicaDefinition;
                    }
                    else if (langOption == 2)
                    {
                        function = functions[tokens[i] - 2000].ExcelDefinition;
                    }
                    else if (langOption == 3)
                    {
                        function = functions[tokens[i] - 2000].RDefinition;
                    }
                    else
                    {
                        function = functions[tokens[i] - 2000].Definition;
                    }

                    for (int j = 1; j <= count; j++)
                    {
                        string oldStr = "x" + (j).ToString();
                        string newStr = expression.Pop();
                        function = function.Replace(oldStr, newStr);
                    }

                    //Izracunavanje rezultata
                    expression.Push("(" + function + ")");
                }
            }
            // return the only value from stack
            Debug.Assert(expression.Count == 1);
            // return arguments.Pop();
            return(expression.Pop());
        }
Example #2
0
        /// <summary>
        /// Decoding treeExpression to polishnotation
        /// </summary>
        /// <param name="treeExpression"></param>
        /// <returns></returns>
        public string DecodeExpression(GPNode treeExpression, bool bExcel = false)
        {
            //Prepare chromoseme for evaluation
            var tokens = treeExpression.ToList();
            int countT = tokens.Count;

            //Stack fr evaluation
            Stack <string> expression = new Stack <string>();

            for (int i = countT - 1; i >= 0; i--)
            {
                if (tokens[i] >= 1000 && tokens[i] < 2000)
                {
                    string varaiable = terminals[tokens[i] - 1000].Name;
                    expression.Push(varaiable);
                }
                else
                {
                    //prepare function arguments for evaluation
                    int    count    = functions[tokens[i] - 2000].Aritry;
                    string function = bExcel ? functions[tokens[i] - 2000].ExcelDefinition : functions[tokens[i] - 2000].Definition;


                    for (int j = 1; j <= count; j++)
                    {
                        string oldStr = "x" + (j).ToString();
                        string newStr = expression.Pop();
                        if (bExcel)
                        {
                            newStr += " ";
                        }
                        function = function.Replace(oldStr, newStr);
                    }

                    //Izracunavanje rezultata
                    expression.Push("(" + function + ")");
                }
            }
            // return the only value from stack
            Debug.Assert(expression.Count == 1);
            // return arguments.Pop();
            return(expression.Pop());
        }
Example #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);
        }
Example #4
0
        /// <summary>
        /// Converts GPNode in to expression
        /// </summary>
        /// <param name="treeExpression">chromosome in S-expression</param>
        /// <param name="rowIndex"> current row to evaluate choromosme</param>
        /// <param name="btrainingData">whether is training or testing data</param>
        /// <returns></returns>
        public double Evaluate(GPNode treeExpression, int rowIndex, bool btrainingData = true)
        {
            //get the array from tree nodes
            treeExpression.ToList(_tokens.Value);
            var tokens = _tokens.Value;
            //count all tokens
            int countT = tokens.Count();

            //get terminal for specific position
            double[] terminalRow = Globals.GetTerminalRow(rowIndex, btrainingData);

            //Stack for evaluation
            Stack <double> arguments = _arguments.Value;//new Stack<double>();

            //the maximum aritry is 4
            // double[] val = new double[5];

            for (int i = countT - 1; i >= 0; i--)
            {
                //skip invalid tokens
                if (tokens[i] < 1000)
                {
                    continue;
                }

                // Put terminal in to Stack for leter function evaluation
                if (tokens[i] >= 1000 && tokens[i] < 2000)
                {
                    arguments.Push(terminalRow[tokens[i] - 1000]);
                    //reset node
                    tokens[i] = 0;
                }
                else
                {
                    //create real index value
                    int ind = tokens[i] - 2000;

                    //make current token invalid
                    //or reset token position
                    tokens[i] = 0;

                    //prepare function arguments for evaluation
                    int count = functions[ind].Aritry;

                    //Extract variables
                    for (int j = 0; j < count; j++)
                    {
                        var num = arguments.Pop();
                        if (double.IsNaN(num) || double.IsInfinity(num))
                        {
                            ResetTokensList(tokens, arguments, i);
                            return(double.NaN);
                        }
                        _args.Value[j] = num;
                    }

                    double result = Evaluate(functions[ind], _args.Value);
                    _args.Value[0] = 0;
                    _args.Value[1] = 0;
                    _args.Value[2] = 0;
                    _args.Value[3] = 0;
                    _args.Value[4] = 0;

                    //check if number is valid
                    if (double.IsNaN(result) || double.IsInfinity(result))
                    {
                        //reset the rest of elements in the list
                        ResetTokensList(tokens, arguments, i);
                        return(double.NaN);
                    }

                    //inser subresult in to stack
                    arguments.Push(result);
                }
            }
            // return the only value from stack
            Debug.Assert(arguments.Count == 1);
            return(arguments.Pop());
        }