public static ISymbolicRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData, string modelStructure, int maxIterations)
        {
            var parser     = new InfixExpressionParser();
            var tree       = parser.Parse(modelStructure);
            var simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier();

            if (!SymbolicRegressionConstantOptimizationEvaluator.CanOptimizeConstants(tree))
            {
                throw new ArgumentException("The optimizer does not support the specified model structure.");
            }

            var interpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();

            SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, tree, problemData, problemData.TrainingIndices,
                                                                              applyLinearScaling: false, maxIterations: maxIterations,
                                                                              updateVariableWeights: false, updateConstantsInTree: true);


            var scaledModel = new SymbolicRegressionModel(problemData.TargetVariable, tree, (ISymbolicDataAnalysisExpressionTreeInterpreter)interpreter.Clone());

            scaledModel.Scale(problemData);
            SymbolicRegressionSolution solution = new SymbolicRegressionSolution(scaledModel, (IRegressionProblemData)problemData.Clone());

            solution.Model.Name = "Regression Model";
            solution.Name       = "Regression Solution";
            return(solution);
        }
Пример #2
0
        public void InfixExpressionParserTestFormatting()
        {
            var formatter = new InfixExpressionFormatter();
            var parser    = new InfixExpressionParser();

            Console.WriteLine(formatter.Format(parser.Parse("3")));
            Console.WriteLine(formatter.Format(parser.Parse("3*3")));
            Console.WriteLine(formatter.Format(parser.Parse("3 * 4")));
            Console.WriteLine(formatter.Format(parser.Parse("123E-03")));
            Console.WriteLine(formatter.Format(parser.Parse("123e-03")));
            Console.WriteLine(formatter.Format(parser.Parse("123e+03")));
            Console.WriteLine(formatter.Format(parser.Parse("123E+03")));
            Console.WriteLine(formatter.Format(parser.Parse("123.0E-03")));
            Console.WriteLine(formatter.Format(parser.Parse("123.0e-03")));
            Console.WriteLine(formatter.Format(parser.Parse("123.0e+03")));
            Console.WriteLine(formatter.Format(parser.Parse("123.0E+03")));
            Console.WriteLine(formatter.Format(parser.Parse("123.0E-3")));
            Console.WriteLine(formatter.Format(parser.Parse("123.0e-3")));
            Console.WriteLine(formatter.Format(parser.Parse("123.0e+3")));
            Console.WriteLine(formatter.Format(parser.Parse("123.0E+3")));

            Console.WriteLine(formatter.Format(parser.Parse("3.1415+2.0")));
            Console.WriteLine(formatter.Format(parser.Parse("3.1415/2.0")));
            Console.WriteLine(formatter.Format(parser.Parse("3.1415*2.0")));
            Console.WriteLine(formatter.Format(parser.Parse("3.1415-2.0")));
            // round-trip
            Console.WriteLine(formatter.Format(parser.Parse(formatter.Format(parser.Parse("3.1415-2.0")))));
            Console.WriteLine(formatter.Format(parser.Parse("3.1415+(2.0)")));
            Console.WriteLine(formatter.Format(parser.Parse("(3.1415+(2.0))")));


            Console.WriteLine(formatter.Format(parser.Parse("log(3)")));
            Console.WriteLine(formatter.Format(parser.Parse("log(-3)")));
            Console.WriteLine(formatter.Format(parser.Parse("exp(3)")));
            Console.WriteLine(formatter.Format(parser.Parse("exp(-3)")));
            Console.WriteLine(formatter.Format(parser.Parse("sqrt(3)")));

            Console.WriteLine(formatter.Format(parser.Parse("sqr((-3))")));

            Console.WriteLine(formatter.Format(parser.Parse("3/3+2/2+1/1")));
            Console.WriteLine(formatter.Format(parser.Parse("-3+30-2+20-1+10")));
            // round trip
            Console.WriteLine(formatter.Format(parser.Parse(formatter.Format(parser.Parse("-3+30-2+20-1+10")))));

            Console.WriteLine(formatter.Format(parser.Parse("\"x1\"")));
            Console.WriteLine(formatter.Format(parser.Parse("\'var name\'")));
            Console.WriteLine(formatter.Format(parser.Parse("\"var name\"")));
            Console.WriteLine(formatter.Format(parser.Parse("\"1\"")));

            Console.WriteLine(formatter.Format(parser.Parse("'var \" name\'")));
            Console.WriteLine(formatter.Format(parser.Parse("\"var \' name\"")));


            Console.WriteLine(formatter.Format(parser.Parse("\"x1\"*\"x2\"")));
            Console.WriteLine(formatter.Format(parser.Parse("\"x1\"*\"x2\"+\"x3\"*\"x4\"")));
            Console.WriteLine(formatter.Format(parser.Parse("x1*x2+x3*x4")));
        }
    public void InfixExpressionParserTestFormatting() {
      var formatter = new InfixExpressionFormatter();
      var parser = new InfixExpressionParser();
      Console.WriteLine(formatter.Format(parser.Parse("3")));
      Console.WriteLine(formatter.Format(parser.Parse("3*3")));
      Console.WriteLine(formatter.Format(parser.Parse("3 * 4")));
      Console.WriteLine(formatter.Format(parser.Parse("123E-03")  ));
      Console.WriteLine(formatter.Format(parser.Parse("123e-03")));
      Console.WriteLine(formatter.Format(parser.Parse("123e+03")));
      Console.WriteLine(formatter.Format(parser.Parse("123E+03")));
      Console.WriteLine(formatter.Format(parser.Parse("123.0E-03")));
      Console.WriteLine(formatter.Format(parser.Parse("123.0e-03")));
      Console.WriteLine(formatter.Format(parser.Parse("123.0e+03")));
      Console.WriteLine(formatter.Format(parser.Parse("123.0E+03")));
      Console.WriteLine(formatter.Format(parser.Parse("123.0E-3")));
      Console.WriteLine(formatter.Format(parser.Parse("123.0e-3")));
      Console.WriteLine(formatter.Format(parser.Parse("123.0e+3")));
      Console.WriteLine(formatter.Format(parser.Parse("123.0E+3")));

      Console.WriteLine(formatter.Format(parser.Parse("3.1415+2.0")));
      Console.WriteLine(formatter.Format(parser.Parse("3.1415/2.0")));
      Console.WriteLine(formatter.Format(parser.Parse("3.1415*2.0")));
      Console.WriteLine(formatter.Format(parser.Parse("3.1415-2.0")));
      // round-trip
      Console.WriteLine(formatter.Format(parser.Parse(formatter.Format(parser.Parse("3.1415-2.0")))));
      Console.WriteLine(formatter.Format(parser.Parse("3.1415+(2.0)")));
      Console.WriteLine(formatter.Format(parser.Parse("(3.1415+(2.0))")));


      Console.WriteLine(formatter.Format(parser.Parse("log(3)")));
      Console.WriteLine(formatter.Format(parser.Parse("log(-3)")));
      Console.WriteLine(formatter.Format(parser.Parse("exp(3)")));
      Console.WriteLine(formatter.Format(parser.Parse("exp(-3)")));
      Console.WriteLine(formatter.Format(parser.Parse("sqrt(3)")));

      Console.WriteLine(formatter.Format(parser.Parse("sqr((-3))")));

      Console.WriteLine(formatter.Format(parser.Parse("3/3+2/2+1/1")));
      Console.WriteLine(formatter.Format(parser.Parse("-3+30-2+20-1+10")));
      // round trip
      Console.WriteLine(formatter.Format(parser.Parse(formatter.Format(parser.Parse("-3+30-2+20-1+10")))));

      Console.WriteLine(formatter.Format(parser.Parse("\"x1\"")));
      Console.WriteLine(formatter.Format(parser.Parse("\'var name\'")));
      Console.WriteLine(formatter.Format(parser.Parse("\"var name\"")));
      Console.WriteLine(formatter.Format(parser.Parse("\"1\"")));

      Console.WriteLine(formatter.Format(parser.Parse("'var \" name\'")));
      Console.WriteLine(formatter.Format(parser.Parse("\"var \' name\"")));


      Console.WriteLine(formatter.Format(parser.Parse("\"x1\"*\"x2\"")));
      Console.WriteLine(formatter.Format(parser.Parse("\"x1\"*\"x2\"+\"x3\"*\"x4\"")));
      Console.WriteLine(formatter.Format(parser.Parse("x1*x2+x3*x4")));

    }
Пример #4
0
        private string Derive(string expr, string variable)
        {
            var parser    = new InfixExpressionParser();
            var formatter = new InfixExpressionFormatter();

            var t      = parser.Parse(expr);
            var tPrime = DerivativeCalculator.Derive(t, variable);

            return(formatter.Format(tPrime));
        }
        private void EvaluateTest(string expression, Interval expectedResult, Dictionary <string, Interval> variableRanges = null, double lowerDelta = 0, double upperDelta = 0)
        {
            var      parser      = new InfixExpressionParser();
            var      tree        = parser.Parse(expression);
            var      interpreter = new IntervalInterpreter();
            Interval result;

            if (variableRanges == null)
            {
                result = interpreter.GetSymbolicExpressionTreeInterval(tree, problemData.Dataset, problemData.AllIndices);
            }
            else
            {
                result = interpreter.GetSymbolicExpressionTreeInterval(tree, variableRanges);
            }

            Assert.AreEqual(expectedResult.LowerBound, result.LowerBound, lowerDelta);
            Assert.AreEqual(expectedResult.UpperBound, result.UpperBound, upperDelta);
        }
Пример #6
0
        /*
         * Creates an ISymbolicExpressionTree out of the list of a model
         * It does so by creating a string which will then get parsed by the InfixExpressionParser
         */
        private ISymbolicExpressionTree Tree(IEnumerable <BasisFunction> basisFunctions, double[] coeffs, double offset)
        {
            if (basisFunctions.Count(val => true) != coeffs.Length)
            {
                throw new ArgumentException();
            }
            var culture           = new CultureInfo("en-US");
            var numNumeratorFuncs = ConsiderDenominations ? basisFunctions.Count() / 2 : basisFunctions.Count();

            // true if there exists at least 1 coefficient value in the model that is part of the denominator
            // (i.e. if there exists at least 1 non-zero value in the second half of the array)
            bool   withDenom = coeffs.OrderByDescending(val => val).Take(coeffs.Length / 2).ToArray().Any(val => !val.IsAlmost(0.0));
            string model     = "(" + offset.ToString(culture);

            for (int i = 0; i < numNumeratorFuncs; i++)
            {
                var func = basisFunctions.ElementAt(i);
                // only generate nodes for relevant basis functions (those with non-zero coeffs)
                if (coeffs[i] != 0)
                {
                    model += " + (" + coeffs[i].ToString(culture) + ") * " + func.Var;
                }
            }
            if (ConsiderDenominations && withDenom)
            {
                model += ") / (1";
                for (int i = numNumeratorFuncs; i < basisFunctions.Count(); i++)
                {
                    var func = basisFunctions.ElementAt(i);
                    // only generate nodes for relevant basis functions (those with non-zero coeffs)
                    if (coeffs[i] != 0)
                    {
                        model += " + (" + coeffs[i].ToString(culture) + ") * " + func.Var;
                    }
                }
            }
            model += ")";
            InfixExpressionParser p = new InfixExpressionParser();

            return(p.Parse(model));
        }
        public void InfixExpressionParserTestFormatting()
        {
            var formatter = new InfixExpressionFormatter();
            var parser    = new InfixExpressionParser();

            Console.WriteLine(formatter.Format(parser.Parse("3")));
            Console.WriteLine(formatter.Format(parser.Parse("3*3")));
            Console.WriteLine(formatter.Format(parser.Parse("3 * 4")));
            Console.WriteLine(formatter.Format(parser.Parse("123E-03")));
            Console.WriteLine(formatter.Format(parser.Parse("123e-03")));
            Console.WriteLine(formatter.Format(parser.Parse("123e+03")));
            Console.WriteLine(formatter.Format(parser.Parse("123E+03")));
            Console.WriteLine(formatter.Format(parser.Parse("123.0E-03")));
            Console.WriteLine(formatter.Format(parser.Parse("123.0e-03")));
            Console.WriteLine(formatter.Format(parser.Parse("123.0e+03")));
            Console.WriteLine(formatter.Format(parser.Parse("123.0E+03")));
            Console.WriteLine(formatter.Format(parser.Parse("123.0E-3")));
            Console.WriteLine(formatter.Format(parser.Parse("123.0e-3")));
            Console.WriteLine(formatter.Format(parser.Parse("123.0e+3")));
            Console.WriteLine(formatter.Format(parser.Parse("123.0E+3")));

            Console.WriteLine(formatter.Format(parser.Parse("3.1415+2.0")));
            Console.WriteLine(formatter.Format(parser.Parse("3.1415/2.0")));
            Console.WriteLine(formatter.Format(parser.Parse("3.1415*2.0")));
            Console.WriteLine(formatter.Format(parser.Parse("3.1415-2.0")));
            // round-trip
            Console.WriteLine(formatter.Format(parser.Parse(formatter.Format(parser.Parse("3.1415-2.0")))));
            Console.WriteLine(formatter.Format(parser.Parse("3.1415+(2.0)")));
            Console.WriteLine(formatter.Format(parser.Parse("(3.1415+(2.0))")));


            Console.WriteLine(formatter.Format(parser.Parse("log(3)")));
            Console.WriteLine(formatter.Format(parser.Parse("log(-3)")));
            Console.WriteLine(formatter.Format(parser.Parse("exp(3)")));
            Console.WriteLine(formatter.Format(parser.Parse("exp(-3)")));
            Console.WriteLine(formatter.Format(parser.Parse("sqrt(3)")));

            Console.WriteLine(formatter.Format(parser.Parse("sqr((-3))")));

            Console.WriteLine(formatter.Format(parser.Parse("3/3+2/2+1/1")));
            Console.WriteLine(formatter.Format(parser.Parse("-3+30-2+20-1+10")));
            // round trip
            Console.WriteLine(formatter.Format(parser.Parse(formatter.Format(parser.Parse("-3+30-2+20-1+10")))));

            Console.WriteLine(formatter.Format(parser.Parse("\"x1\"")));
            Console.WriteLine(formatter.Format(parser.Parse("\'var name\'")));
            Console.WriteLine(formatter.Format(parser.Parse("\"var name\"")));
            Console.WriteLine(formatter.Format(parser.Parse("\"1\"")));

            Console.WriteLine(formatter.Format(parser.Parse("'var \" name\'")));
            Console.WriteLine(formatter.Format(parser.Parse("\"var \' name\"")));


            Console.WriteLine(formatter.Format(parser.Parse("\"x1\"*\"x2\"")));
            Console.WriteLine(formatter.Format(parser.Parse("\"x1\"*\"x2\"+\"x3\"*\"x4\"")));
            Console.WriteLine(formatter.Format(parser.Parse("x1*x2+x3*x4")));


            Console.WriteLine(formatter.Format(parser.Parse("POW(3, 2)")));
            Console.WriteLine(formatter.Format(parser.Parse("POW(3.1, 2.1)")));
            Console.WriteLine(formatter.Format(parser.Parse("POW(3.1 , 2.1)")));
            Console.WriteLine(formatter.Format(parser.Parse("POW(3.1 ,2.1)")));
            Console.WriteLine(formatter.Format(parser.Parse("POW(-3.1 , - 2.1)")));
            Console.WriteLine(formatter.Format(parser.Parse("ROOT(3, 2)")));
            Console.WriteLine(formatter.Format(parser.Parse("ROOT(3.1, 2.1)")));
            Console.WriteLine(formatter.Format(parser.Parse("ROOT(3.1 , 2.1)")));
            Console.WriteLine(formatter.Format(parser.Parse("ROOT(3.1 ,2.1)")));
            Console.WriteLine(formatter.Format(parser.Parse("ROOT(-3.1 , - 2.1)")));

            Console.WriteLine(formatter.Format(parser.Parse("IF(GT( 0, 1), 1, 0)")));
            Console.WriteLine(formatter.Format(parser.Parse("IF(LT(0,1), 1 , 0)")));

            Console.WriteLine(formatter.Format(parser.Parse("LAG(x, 1)")));
            Console.WriteLine(formatter.Format(parser.Parse("LAG(x, -1)")));
            Console.WriteLine(formatter.Format(parser.Parse("LAG(x, +1)")));
            Console.WriteLine(formatter.Format(parser.Parse("x * LAG('x', +1)")));

            Console.WriteLine(formatter.Format(parser.Parse("x [1.0] * y")));
            Console.WriteLine(formatter.Format(parser.Parse("x [1.0, 2.0] * y [1.0, 2.0]")));
            Console.WriteLine(formatter.Format(parser.Parse("x[1] * y")));
            Console.WriteLine(formatter.Format(parser.Parse("x[1, 2] * y [1, 2]")));

            Console.WriteLine(formatter.Format(parser.Parse("x [+1.0] * y")));
            Console.WriteLine(formatter.Format(parser.Parse("x [-1.0] * y")));
            Console.WriteLine(formatter.Format(parser.Parse("x [-1.0, -2.0] * y [+1.0, +2.0]")));

            Console.WriteLine(formatter.Format(parser.Parse("x='bla' * y")));
            Console.WriteLine(formatter.Format(parser.Parse("x = 'bla'")));
            Console.WriteLine(formatter.Format(parser.Parse("x = \"bla\"")));
            Console.WriteLine(formatter.Format(parser.Parse("1.0 * x = bla")));
            Console.WriteLine(formatter.Format(parser.Parse("-1.0 * x = bla")));
            Console.WriteLine(formatter.Format(parser.Parse("+1.0 * \"x\" = bla + y = \"bla2\"")));
        }
Пример #8
0
        public void DeriveExpressions()
        {
            var formatter = new InfixExpressionFormatter();
            var parser    = new InfixExpressionParser();

            Assert.AreEqual("0", Derive("3", "x"));
            Assert.AreEqual("1", Derive("x", "x"));
            Assert.AreEqual("10", Derive("10*x", "x"));
            Assert.AreEqual("10", Derive("x*10", "x"));
            Assert.AreEqual("(2*'x')", Derive("x*x", "x"));
            Assert.AreEqual("((('x' * 'x') * 2) + ('x' * 'x'))", Derive("x*x*x", "x")); // simplifier does not merge (x*x)*2 + x*x  to 3*x*x
            Assert.AreEqual("0", Derive("10*x", "y"));
            Assert.AreEqual("20", Derive("10*x+20*y", "y"));
            Assert.AreEqual("6", Derive("2*3*x", "x"));
            Assert.AreEqual("(10*'y')", Derive("10*x*y+20*y", "x"));
            Assert.AreEqual("(1 / (SQR('x') * (-1)))", Derive("1/x", "x"));
            Assert.AreEqual("('y' / (SQR('x') * (-1)))", Derive("y/x", "x"));
            Assert.AreEqual("((((-2*'x') + (-1)) * ('a' + 'b')) / SQR(('x' + ('x' * 'x'))))",
                            Derive("(a+b)/(x+x*x)", "x"));
            Assert.AreEqual("((((-2*'x') + (-1)) * ('a' + 'b')) / SQR(('x' + SQR('x'))))", Derive("(a+b)/(x+SQR(x))", "x"));
            Assert.AreEqual("EXP('x')", Derive("exp(x)", "x"));
            Assert.AreEqual("(EXP((3*'x')) * 3)", Derive("exp(3*x)", "x"));
            Assert.AreEqual("(1 / 'x')", Derive("log(x)", "x"));
            Assert.AreEqual("(1 / 'x')", Derive("log(3*x)", "x"));                                            // 3 * 1/(3*x)
            Assert.AreEqual("(1 / ('x' + (0.333333333333333*'y')))", Derive("log(3*x+y)", "x"));              // simplifier does not try to keep fractions
            Assert.AreEqual("(1 / (SQRT(((3*'x') + 'y')) * 0.666666666666667))", Derive("sqrt(3*x+y)", "x")); // 3 / (2 * sqrt(3*x+y)) = 1 / ((2/3) * sqrt(3*x+y))
            Assert.AreEqual("(COS((3*'x')) * 3)", Derive("sin(3*x)", "x"));
            Assert.AreEqual("(SIN((3*'x')) * (-3))", Derive("cos(3*x)", "x"));
            Assert.AreEqual("(1 / (SQR(COS((3*'x'))) * 0.333333333333333))", Derive("tan(3*x)", "x")); // diff(tan(f(x)), x) = 1.0 / cos²(f(x)), simplifier puts constant factor into the denominator

            Assert.AreEqual("((9*'x') / ABS((3*'x')))", Derive("abs(3*x)", "x"));
            Assert.AreEqual("(SQR('x') * 3)", Derive("cube(x)", "x"));
            Assert.AreEqual("(1 / (SQR(CUBEROOT('x')) * 3))", Derive("cuberoot(x)", "x"));

            Assert.AreEqual("0", Derive("(a+b)/(x+SQR(x))", "y")); // df(a,b,x) / dy = 0


            Assert.AreEqual("('a' * 'b' * 'c')", Derive("a*b*c*d", "d"));
            Assert.AreEqual("('a' / ('b' * 'c' * SQR('d') * (-1)))", Derive("a/b/c/d", "d"));

            Assert.AreEqual("('x' * ((SQR(TANH(SQR('x'))) * (-1)) + 1) * 2)", Derive("tanh(sqr(x))", "x")); // (2*'x'*(1 - SQR(TANH(SQR('x'))))

            {
                // special case: Inv(x) using only one argument to the division symbol
                // f(x) = 1/x
                var root    = new ProgramRootSymbol().CreateTreeNode();
                var start   = new StartSymbol().CreateTreeNode();
                var div     = new Division().CreateTreeNode();
                var varNode = (VariableTreeNode)(new Variable().CreateTreeNode());
                varNode.Weight       = 1.0;
                varNode.VariableName = "x";
                div.AddSubtree(varNode);
                start.AddSubtree(div);
                root.AddSubtree(start);
                var t = new SymbolicExpressionTree(root);
                Assert.AreEqual("(1 / (SQR('x') * (-1)))",
                                formatter.Format(DerivativeCalculator.Derive(t, "x")));
            }

            {
                // special case: multiplication with only one argument
                var root    = new ProgramRootSymbol().CreateTreeNode();
                var start   = new StartSymbol().CreateTreeNode();
                var mul     = new Multiplication().CreateTreeNode();
                var varNode = (VariableTreeNode)(new Variable().CreateTreeNode());
                varNode.Weight       = 3.0;
                varNode.VariableName = "x";
                mul.AddSubtree(varNode);
                start.AddSubtree(mul);
                root.AddSubtree(start);
                var t = new SymbolicExpressionTree(root);
                Assert.AreEqual("3",
                                formatter.Format(DerivativeCalculator.Derive(t, "x")));
            }

            {
                // division with multiple arguments
                // div(x, y, z) is interpreted as (x / y) / z
                var root     = new ProgramRootSymbol().CreateTreeNode();
                var start    = new StartSymbol().CreateTreeNode();
                var div      = new Division().CreateTreeNode();
                var varNode1 = (VariableTreeNode)(new Variable().CreateTreeNode());
                varNode1.Weight       = 3.0;
                varNode1.VariableName = "x";
                var varNode2 = (VariableTreeNode)(new Variable().CreateTreeNode());
                varNode2.Weight       = 4.0;
                varNode2.VariableName = "y";
                var varNode3 = (VariableTreeNode)(new Variable().CreateTreeNode());
                varNode3.Weight       = 5.0;
                varNode3.VariableName = "z";
                div.AddSubtree(varNode1); div.AddSubtree(varNode2); div.AddSubtree(varNode3);
                start.AddSubtree(div);
                root.AddSubtree(start);
                var t = new SymbolicExpressionTree(root);

                Assert.AreEqual("(('y' * 'z' * 60) / (SQR('y') * SQR('z') * 400))",    // actually 3 / (4y  5z) but simplifier is not smart enough to cancel numerator and denominator
                                                                                       // 60 y z / y² z² 20² == 6 / y z 40 == 3 / y z 20
                                formatter.Format(DerivativeCalculator.Derive(t, "x")));
                Assert.AreEqual("(('x' * 'z' * (-60)) / (SQR('y') * SQR('z') * 400))", // actually 3x * -(4 5 z) / (4y 5z)² = -3x / (20 y² z)
                                                                                       // -3 4 5 x z / 4² y² 5² z² = -60 x z / 20² z² y² ==    -60 x z / y² z² 20²
                                formatter.Format(DerivativeCalculator.Derive(t, "y")));
                Assert.AreEqual("(('x' * 'y' * (-60)) / (SQR('y') * SQR('z') * 400))",
                                formatter.Format(DerivativeCalculator.Derive(t, "z")));
            }
        }
Пример #9
0
        /// <summary>
        /// Fits a model to the data by optimizing the numeric constants.
        /// Model is specified as infix expression containing variable names and numbers.
        /// The starting point for the numeric constants is initialized randomly if a random number generator is specified (~N(0,1)). Otherwise the user specified constants are
        /// used as a starting point.
        /// </summary>-
        /// <param name="problemData">Training and test data</param>
        /// <param name="modelStructure">The function as infix expression</param>
        /// <param name="maxIterations">Number of constant optimization iterations (using Levenberg-Marquardt algorithm)</param>
        /// <param name="random">Optional random number generator for random initialization of numeric constants.</param>
        /// <returns></returns>
        public static ISymbolicRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData, string modelStructure, int maxIterations, bool applyLinearScaling, IRandom rand = null)
        {
            var parser = new InfixExpressionParser();
            var tree   = parser.Parse(modelStructure);
            // parser handles double and string variables equally by creating a VariableTreeNode
            // post-process to replace VariableTreeNodes by FactorVariableTreeNodes for all string variables
            var factorSymbol = new FactorVariable();

            factorSymbol.VariableNames =
                problemData.AllowedInputVariables.Where(name => problemData.Dataset.VariableHasType <string>(name));
            factorSymbol.AllVariableNames = factorSymbol.VariableNames;
            factorSymbol.VariableValues   =
                factorSymbol.VariableNames.Select(name =>
                                                  new KeyValuePair <string, Dictionary <string, int> >(name,
                                                                                                       problemData.Dataset.GetReadOnlyStringValues(name).Distinct()
                                                                                                       .Select((n, i) => Tuple.Create(n, i))
                                                                                                       .ToDictionary(tup => tup.Item1, tup => tup.Item2)));

            foreach (var parent in tree.IterateNodesPrefix().ToArray())
            {
                for (int i = 0; i < parent.SubtreeCount; i++)
                {
                    var varChild       = parent.GetSubtree(i) as VariableTreeNode;
                    var factorVarChild = parent.GetSubtree(i) as FactorVariableTreeNode;
                    if (varChild != null && factorSymbol.VariableNames.Contains(varChild.VariableName))
                    {
                        parent.RemoveSubtree(i);
                        var factorTreeNode = (FactorVariableTreeNode)factorSymbol.CreateTreeNode();
                        factorTreeNode.VariableName = varChild.VariableName;
                        factorTreeNode.Weights      =
                            factorTreeNode.Symbol.GetVariableValues(factorTreeNode.VariableName).Select(_ => 1.0).ToArray();
                        // weight = 1.0 for each value
                        parent.InsertSubtree(i, factorTreeNode);
                    }
                    else if (factorVarChild != null && factorSymbol.VariableNames.Contains(factorVarChild.VariableName))
                    {
                        if (factorSymbol.GetVariableValues(factorVarChild.VariableName).Count() != factorVarChild.Weights.Length)
                        {
                            throw new ArgumentException(
                                      string.Format("Factor variable {0} needs exactly {1} weights",
                                                    factorVarChild.VariableName,
                                                    factorSymbol.GetVariableValues(factorVarChild.VariableName).Count()));
                        }
                        parent.RemoveSubtree(i);
                        var factorTreeNode = (FactorVariableTreeNode)factorSymbol.CreateTreeNode();
                        factorTreeNode.VariableName = factorVarChild.VariableName;
                        factorTreeNode.Weights      = factorVarChild.Weights;
                        parent.InsertSubtree(i, factorTreeNode);
                    }
                }
            }

            if (!SymbolicRegressionConstantOptimizationEvaluator.CanOptimizeConstants(tree))
            {
                throw new ArgumentException("The optimizer does not support the specified model structure.");
            }

            // initialize constants randomly
            if (rand != null)
            {
                foreach (var node in tree.IterateNodesPrefix().OfType <ConstantTreeNode>())
                {
                    double f = Math.Exp(NormalDistributedRandom.NextDouble(rand, 0, 1));
                    double s = rand.NextDouble() < 0.5 ? -1 : 1;
                    node.Value = s * node.Value * f;
                }
            }
            var interpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();

            SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, tree, problemData, problemData.TrainingIndices,
                                                                              applyLinearScaling: applyLinearScaling, maxIterations: maxIterations,
                                                                              updateVariableWeights: false, updateConstantsInTree: true);

            var model = new SymbolicRegressionModel(problemData.TargetVariable, tree, (ISymbolicDataAnalysisExpressionTreeInterpreter)interpreter.Clone());

            if (applyLinearScaling)
            {
                model.Scale(problemData);
            }

            SymbolicRegressionSolution solution = new SymbolicRegressionSolution(model, (IRegressionProblemData)problemData.Clone());

            solution.Model.Name = "Regression Model";
            solution.Name       = "Regression Solution";
            return(solution);
        }
Пример #10
0
 public BottomUpSimilarityCalculatorTest()
 {
     var parser = new InfixExpressionParser();
 }