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);
        }
Exemple #2
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);
        }