public override void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node,
      IDataAnalysisProblemData problemData, IEnumerable<int> rows, out double impactValue, out double replacementValue, out double newQualityForImpactsCalculation,
      double qualityForImpactsCalculation = Double.NaN) {
      var classificationModel = (ISymbolicClassificationModel)model;
      var classificationProblemData = (IClassificationProblemData)problemData;

      if (double.IsNaN(qualityForImpactsCalculation))
        qualityForImpactsCalculation = CalculateQualityForImpacts(classificationModel, classificationProblemData, rows);

      replacementValue = CalculateReplacementValue(classificationModel, node, classificationProblemData, rows);
      var constantNode = new ConstantTreeNode(new Constant()) { Value = replacementValue };

      var cloner = new Cloner();
      var tempModel = cloner.Clone(classificationModel);
      var tempModelNode = (ISymbolicExpressionTreeNode)cloner.GetClone(node);

      var tempModelParentNode = tempModelNode.Parent;
      int i = tempModelParentNode.IndexOfSubtree(tempModelNode);
      tempModelParentNode.RemoveSubtree(i);
      tempModelParentNode.InsertSubtree(i, constantNode);

      OnlineCalculatorError errorState;
      var dataset = classificationProblemData.Dataset;
      var targetClassValues = dataset.GetDoubleValues(classificationProblemData.TargetVariable, rows);
      var estimatedClassValues = tempModel.GetEstimatedClassValues(dataset, rows);
      newQualityForImpactsCalculation = OnlineAccuracyCalculator.Calculate(targetClassValues, estimatedClassValues, out errorState);
      if (errorState != OnlineCalculatorError.None) newQualityForImpactsCalculation = 0.0;

      impactValue = qualityForImpactsCalculation - newQualityForImpactsCalculation;
    }
 public ConstantNodeEditDialog(ISymbolicExpressionTreeNode node)
 {
     InitializeComponent();
     oldValueTextBox.TabStop = false; // cannot receive focus using tab key
     NewNode = (ConstantTreeNode)node;
     InitializeFields();
 }
        private static void UpdateConstants(ISymbolicExpressionTree tree, double[] constants, bool updateVariableWeights)
        {
            int i = 0;

            foreach (var node in tree.Root.IterateNodesPrefix().OfType <SymbolicExpressionTreeTerminalNode>())
            {
                ConstantTreeNode       constantTreeNode     = node as ConstantTreeNode;
                VariableTreeNodeBase   variableTreeNodeBase = node as VariableTreeNodeBase;
                FactorVariableTreeNode factorVarTreeNode    = node as FactorVariableTreeNode;
                if (constantTreeNode != null)
                {
                    constantTreeNode.Value = constants[i++];
                }
                else if (updateVariableWeights && variableTreeNodeBase != null)
                {
                    variableTreeNodeBase.Weight = constants[i++];
                }
                else if (factorVarTreeNode != null)
                {
                    for (int j = 0; j < factorVarTreeNode.Weights.Length; j++)
                    {
                        factorVarTreeNode.Weights[j] = constants[i++];
                    }
                }
            }
        }
Exemplo n.º 4
0
        private ISymbolicExpressionTreeNode MakeProduct(ConstantTreeNode c, double weight)
        {
            var mul  = new Multiplication();
            var prod = mul.CreateTreeNode();

            prod.AddSubtree(MakeConstantTreeNode(weight));
            prod.AddSubtree(c);
            return(prod);
        }
Exemplo n.º 5
0
        private ConstantTreeNode MakeConstantTreeNode(double value)
        {
            Constant constant = new Constant();

            constant.MinValue = value - 1;
            constant.MaxValue = value + 1;
            ConstantTreeNode constantTreeNode = (ConstantTreeNode)constant.CreateTreeNode();

            constantTreeNode.Value = value;
            return(constantTreeNode);
        }
    public InteractiveSymbolicTimeSeriesPrognosisSolutionSimplifierView()
      : base() {
      InitializeComponent();
      this.Caption = "Interactive Time-Series Prognosis Solution Simplifier";

      constantNode = ((ConstantTreeNode)new Constant().CreateTreeNode());
      ISymbolicExpressionTreeNode root = new ProgramRootSymbol().CreateTreeNode();
      ISymbolicExpressionTreeNode start = new StartSymbol().CreateTreeNode();
      root.AddSubtree(start);
      tempTree = new SymbolicExpressionTree(root);
    }
        public InteractiveSymbolicTimeSeriesPrognosisSolutionSimplifierView()
            : base()
        {
            InitializeComponent();
            this.Caption = "Interactive Time-Series Prognosis Solution Simplifier";

            constantNode = ((ConstantTreeNode) new Constant().CreateTreeNode());
            ISymbolicExpressionTreeNode root  = new ProgramRootSymbol().CreateTreeNode();
            ISymbolicExpressionTreeNode start = new StartSymbol().CreateTreeNode();

            root.AddSubtree(start);
            tempTree = new SymbolicExpressionTree(root);
        }
Exemplo n.º 8
0
        private static void UpdateConstants(ISymbolicExpressionTree tree, double[] constants)
        {
            int i = 0;

            foreach (var node in tree.Root.IterateNodesPrefix().OfType <SymbolicExpressionTreeTerminalNode>())
            {
                ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
                VariableTreeNode variableTreeNode = node as VariableTreeNode;
                if (constantTreeNode != null)
                {
                    constantTreeNode.Value = constants[i++];
                }
                else if (variableTreeNode != null)
                {
                    variableTreeNode.Weight = constants[i++];
                }
            }
        }
Exemplo n.º 9
0
        public override void CalculateImpactAndReplacementValues(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node,
                                                                 IDataAnalysisProblemData problemData, IEnumerable <int> rows, out double impactValue, out double replacementValue, out double newQualityForImpactsCalculation,
                                                                 double qualityForImpactsCalculation = Double.NaN)
        {
            var regressionModel       = (ISymbolicRegressionModel)model;
            var regressionProblemData = (IRegressionProblemData)problemData;

            var dataset      = regressionProblemData.Dataset;
            var targetValues = dataset.GetDoubleValues(regressionProblemData.TargetVariable, rows);

            OnlineCalculatorError errorState;

            if (double.IsNaN(qualityForImpactsCalculation))
            {
                qualityForImpactsCalculation = CalculateQualityForImpacts(regressionModel, regressionProblemData, rows);
            }

            replacementValue = CalculateReplacementValue(regressionModel, node, regressionProblemData, rows);
            var constantNode = new ConstantTreeNode(new Constant())
            {
                Value = replacementValue
            };

            var cloner        = new Cloner();
            var tempModel     = cloner.Clone(regressionModel);
            var tempModelNode = (ISymbolicExpressionTreeNode)cloner.GetClone(node);

            var tempModelParentNode = tempModelNode.Parent;
            int i = tempModelParentNode.IndexOfSubtree(tempModelNode);

            tempModelParentNode.RemoveSubtree(i);
            tempModelParentNode.InsertSubtree(i, constantNode);

            var    estimatedValues = tempModel.GetEstimatedValues(dataset, rows);
            double r = OnlinePearsonsRCalculator.Calculate(targetValues, estimatedValues, out errorState);

            if (errorState != OnlineCalculatorError.None)
            {
                r = 0.0;
            }
            newQualityForImpactsCalculation = r * r;

            impactValue = qualityForImpactsCalculation - newQualityForImpactsCalculation;
        }
        private TreeNode FillTreeNode(Expression expression)
        {
            TreeNode treeNode = new NullTreeNode();

            if (expression is BinaryExpression binary)
            {
                treeNode = new BinaryTreeNode()
                {
                    Operator = expression.NodeType,
                    Left     = FillTreeNode(binary.Left),
                    Right    = FillTreeNode(binary.Right),
                };
            }
            else if (expression is MemberExpression)
            {
                treeNode = new AccessorTreeNode()
                {
                    Accessor = expression.ToString()
                };
            }
            else if (expression is ConstantExpression)
            {
                treeNode = new ConstantTreeNode()
                {
                    Constant = expression.ToString()
                };
            }
            else if (expression is MethodCallExpression methodCall)
            {
                treeNode = new MethodCallTreeNode()
                {
                    Method    = methodCall.Method.Name,
                    Arguments = methodCall.Arguments.Select(x => x.ToString()).ToArray(),
                    Accessor  = methodCall.Object.ToString()
                };
            }
            else if (expression is LambdaExpression lambdaExpr)
            {
                treeNode = FillTreeNode(lambdaExpr.Body);
            }

            return(treeNode);
        }
        public static double OptimizeConstants(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable <int> rows, bool applyLinearScaling, int maxIterations, bool updateVariableWeights = true, double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue, bool updateConstantsInTree = true)
        {
            List <AutoDiff.Variable> variables     = new List <AutoDiff.Variable>();
            List <AutoDiff.Variable> parameters    = new List <AutoDiff.Variable>();
            List <string>            variableNames = new List <string>();

            AutoDiff.Term func;
            if (!TryTransformToAutoDiff(tree.Root.GetSubtree(0), variables, parameters, variableNames, updateVariableWeights, out func))
            {
                throw new NotSupportedException("Could not optimize constants of symbolic expression tree due to not supported symbols used in the tree.");
            }
            if (variableNames.Count == 0)
            {
                return(0.0);
            }

            AutoDiff.IParametricCompiledTerm compiledFunc = func.Compile(variables.ToArray(), parameters.ToArray());

            List <SymbolicExpressionTreeTerminalNode> terminalNodes = null;

            if (updateVariableWeights)
            {
                terminalNodes = tree.Root.IterateNodesPrefix().OfType <SymbolicExpressionTreeTerminalNode>().ToList();
            }
            else
            {
                terminalNodes = new List <SymbolicExpressionTreeTerminalNode>(tree.Root.IterateNodesPrefix().OfType <ConstantTreeNode>());
            }

            //extract inital constants
            double[] c = new double[variables.Count];
            {
                c[0] = 0.0;
                c[1] = 1.0;
                int i = 2;
                foreach (var node in terminalNodes)
                {
                    ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
                    VariableTreeNode variableTreeNode = node as VariableTreeNode;
                    if (constantTreeNode != null)
                    {
                        c[i++] = constantTreeNode.Value;
                    }
                    else if (updateVariableWeights && variableTreeNode != null)
                    {
                        c[i++] = variableTreeNode.Weight;
                    }
                }
            }
            double[] originalConstants = (double[])c.Clone();
            double   originalQuality   = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);

            alglib.lsfitstate  state;
            alglib.lsfitreport rep;
            int info;

            IDataset ds = problemData.Dataset;

            double[,] x = new double[rows.Count(), variableNames.Count];
            int row = 0;

            foreach (var r in rows)
            {
                for (int col = 0; col < variableNames.Count; col++)
                {
                    x[row, col] = ds.GetDoubleValue(variableNames[col], r);
                }
                row++;
            }
            double[] y = ds.GetDoubleValues(problemData.TargetVariable, rows).ToArray();
            int      n = x.GetLength(0);
            int      m = x.GetLength(1);
            int      k = c.Length;

            alglib.ndimensional_pfunc function_cx_1_func = CreatePFunc(compiledFunc);
            alglib.ndimensional_pgrad function_cx_1_grad = CreatePGrad(compiledFunc);

            try {
                alglib.lsfitcreatefg(x, y, c, n, m, k, false, out state);
                alglib.lsfitsetcond(state, 0.0, 0.0, maxIterations);
                //alglib.lsfitsetgradientcheck(state, 0.001);
                alglib.lsfitfit(state, function_cx_1_func, function_cx_1_grad, null, null);
                alglib.lsfitresults(state, out info, out c, out rep);
            }
            catch (ArithmeticException) {
                return(originalQuality);
            }
            catch (alglib.alglibexception) {
                return(originalQuality);
            }

            //info == -7  => constant optimization failed due to wrong gradient
            if (info != -7)
            {
                UpdateConstants(tree, c.Skip(2).ToArray(), updateVariableWeights);
            }
            var quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);

            if (!updateConstantsInTree)
            {
                UpdateConstants(tree, originalConstants.Skip(2).ToArray(), updateVariableWeights);
            }
            if (originalQuality - quality > 0.001 || double.IsNaN(quality))
            {
                UpdateConstants(tree, originalConstants.Skip(2).ToArray(), updateVariableWeights);
                return(originalQuality);
            }
            return(quality);
        }
 public ConstantNodeEditDialog(ISymbolicExpressionTreeNode node) {
   InitializeComponent();
   oldValueTextBox.TabStop = false; // cannot receive focus using tab key
   NewNode = (ConstantTreeNode)node;
   InitializeFields();
 }
 private ISymbolicExpressionTreeNode MakeProduct(ConstantTreeNode c, double weight) {
   var mul = new Multiplication();
   var prod = mul.CreateTreeNode();
   prod.AddSubtree(MakeConstantTreeNode(weight));
   prod.AddSubtree(c);
   return prod;
 }
        private static ITimeSeriesPrognosisSolution CreateAutoRegressiveSolution(ITimeSeriesPrognosisProblemData problemData, int timeOffset, out double rmsError, out double cvRmsError)
        {
            string targetVariable = problemData.TargetVariable;

            double[,] inputMatrix = new double[problemData.TrainingPartition.Size, timeOffset + 1];
            var targetValues = problemData.Dataset.GetDoubleValues(targetVariable).ToList();

            for (int i = 0, row = problemData.TrainingPartition.Start; i < problemData.TrainingPartition.Size; i++, row++)
            {
                for (int col = 0; col < timeOffset; col++)
                {
                    inputMatrix[i, col] = targetValues[row - col - 1];
                }
            }
            // set target values in last column
            for (int i = 0; i < inputMatrix.GetLength(0); i++)
            {
                inputMatrix[i, timeOffset] = targetValues[i + problemData.TrainingPartition.Start];
            }

            if (inputMatrix.Cast <double>().Any(x => double.IsNaN(x) || double.IsInfinity(x)))
            {
                throw new NotSupportedException("Linear regression does not support NaN or infinity values in the input dataset.");
            }


            alglib.linearmodel lm = new alglib.linearmodel();
            alglib.lrreport    ar = new alglib.lrreport();
            int nRows             = inputMatrix.GetLength(0);
            int nFeatures         = inputMatrix.GetLength(1) - 1;

            double[] coefficients = new double[nFeatures + 1]; // last coefficient is for the constant

            int retVal = 1;

            alglib.lrbuild(inputMatrix, nRows, nFeatures, out retVal, out lm, out ar);
            if (retVal != 1)
            {
                throw new ArgumentException("Error in calculation of linear regression solution");
            }
            rmsError   = ar.rmserror;
            cvRmsError = ar.cvrmserror;

            alglib.lrunpack(lm, out coefficients, out nFeatures);


            ISymbolicExpressionTree     tree      = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode());
            ISymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode();

            tree.Root.AddSubtree(startNode);
            ISymbolicExpressionTreeNode addition = new Addition().CreateTreeNode();

            startNode.AddSubtree(addition);

            for (int i = 0; i < timeOffset; i++)
            {
                LaggedVariableTreeNode node = (LaggedVariableTreeNode) new LaggedVariable().CreateTreeNode();
                node.VariableName = targetVariable;
                node.Weight       = coefficients[i];
                node.Lag          = (i + 1) * -1;
                addition.AddSubtree(node);
            }

            ConstantTreeNode cNode = (ConstantTreeNode) new Constant().CreateTreeNode();

            cNode.Value = coefficients[coefficients.Length - 1];
            addition.AddSubtree(cNode);

            var interpreter = new SymbolicTimeSeriesPrognosisExpressionTreeInterpreter(problemData.TargetVariable);
            var model       = new SymbolicTimeSeriesPrognosisModel(problemData.TargetVariable, tree, interpreter);
            var solution    = model.CreateTimeSeriesPrognosisSolution((ITimeSeriesPrognosisProblemData)problemData.Clone());

            return(solution);
        }
Exemplo n.º 15
0
        public static ISymbolicRegressionSolution CreateLinearRegressionSolution(IRegressionProblemData problemData, out double rmsError, out double cvRmsError)
        {
            var    dataset        = problemData.Dataset;
            string targetVariable = problemData.TargetVariable;
            IEnumerable <string> allowedInputVariables = problemData.AllowedInputVariables;
            IEnumerable <int>    rows = problemData.TrainingIndices;

            double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables.Concat(new string[] { targetVariable }), rows);
            if (inputMatrix.Cast <double>().Any(x => double.IsNaN(x) || double.IsInfinity(x)))
            {
                throw new NotSupportedException("Linear regression does not support NaN or infinity values in the input dataset.");
            }

            alglib.linearmodel lm = new alglib.linearmodel();
            alglib.lrreport    ar = new alglib.lrreport();
            int nRows             = inputMatrix.GetLength(0);
            int nFeatures         = inputMatrix.GetLength(1) - 1;

            double[] coefficients = new double[nFeatures + 1]; // last coefficient is for the constant

            int retVal = 1;

            alglib.lrbuild(inputMatrix, nRows, nFeatures, out retVal, out lm, out ar);
            if (retVal != 1)
            {
                throw new ArgumentException("Error in calculation of linear regression solution");
            }
            rmsError   = ar.rmserror;
            cvRmsError = ar.cvrmserror;

            alglib.lrunpack(lm, out coefficients, out nFeatures);

            ISymbolicExpressionTree     tree      = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode());
            ISymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode();

            tree.Root.AddSubtree(startNode);
            ISymbolicExpressionTreeNode addition = new Addition().CreateTreeNode();

            startNode.AddSubtree(addition);

            int col = 0;

            foreach (string column in allowedInputVariables)
            {
                VariableTreeNode vNode = (VariableTreeNode) new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable().CreateTreeNode();
                vNode.VariableName = column;
                vNode.Weight       = coefficients[col];
                addition.AddSubtree(vNode);
                col++;
            }

            ConstantTreeNode cNode = (ConstantTreeNode) new Constant().CreateTreeNode();

            cNode.Value = coefficients[coefficients.Length - 1];
            addition.AddSubtree(cNode);

            SymbolicRegressionSolution solution = new SymbolicRegressionSolution(new SymbolicRegressionModel(tree, new SymbolicDataAnalysisExpressionTreeInterpreter()), (IRegressionProblemData)problemData.Clone());

            solution.Model.Name = "Linear Regression Model";
            solution.Name       = "Linear Regression Solution";
            return(solution);
        }