Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/15/12 16:47:25 (11 years ago)
Author:
mkommend
Message:

#1763: merged changes from trunk into the tree simplifier branch.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionModel.cs

    r8139 r8915  
    6464
    6565    public ISymbolicRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
    66       return new SymbolicRegressionSolution(this, problemData);
     66      return new SymbolicRegressionSolution(this, new RegressionProblemData(problemData));
    6767    }
    6868    IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) {
    6969      return CreateRegressionSolution(problemData);
    7070    }
    71 
    72     public static void Scale(SymbolicRegressionModel model, IRegressionProblemData problemData) {
    73       var dataset = problemData.Dataset;
    74       var targetVariable = problemData.TargetVariable;
    75       var rows = problemData.TrainingIndices;
    76       var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows);
    77       var targetValues = dataset.GetDoubleValues(targetVariable, rows);
    78       double alpha;
    79       double beta;
    80       OnlineCalculatorError errorState;
    81       OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta, out errorState);
    82       if (errorState != OnlineCalculatorError.None) return;
    83 
    84       ConstantTreeNode alphaTreeNode = null;
    85       ConstantTreeNode betaTreeNode = null;
    86       // check if model has been scaled previously by analyzing the structure of the tree
    87       var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0);
    88       if (startNode.GetSubtree(0).Symbol is Addition) {
    89         var addNode = startNode.GetSubtree(0);
    90         if (addNode.SubtreeCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) {
    91           alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode;
    92           var mulNode = addNode.GetSubtree(0);
    93           if (mulNode.SubtreeCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) {
    94             betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode;
    95           }
    96         }
    97       }
    98       // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
    99       if (alphaTreeNode != null && betaTreeNode != null) {
    100         betaTreeNode.Value *= beta;
    101         alphaTreeNode.Value *= beta;
    102         alphaTreeNode.Value += alpha;
    103       } else {
    104         var mainBranch = startNode.GetSubtree(0);
    105         startNode.RemoveSubtree(0);
    106         var scaledMainBranch = MakeSum(MakeProduct(mainBranch, beta), alpha);
    107         startNode.AddSubtree(scaledMainBranch);
    108       }
    109     }
    110 
    111     private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
    112       if (alpha.IsAlmost(0.0)) {
    113         return treeNode;
    114       } else {
    115         var addition = new Addition();
    116         var node = addition.CreateTreeNode();
    117         var alphaConst = MakeConstant(alpha);
    118         node.AddSubtree(treeNode);
    119         node.AddSubtree(alphaConst);
    120         return node;
    121       }
    122     }
    123 
    124     private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode treeNode, double beta) {
    125       if (beta.IsAlmost(1.0)) {
    126         return treeNode;
    127       } else {
    128         var multipliciation = new Multiplication();
    129         var node = multipliciation.CreateTreeNode();
    130         var betaConst = MakeConstant(beta);
    131         node.AddSubtree(treeNode);
    132         node.AddSubtree(betaConst);
    133         return node;
    134       }
    135     }
    136 
    137     private static ISymbolicExpressionTreeNode MakeConstant(double c) {
    138       var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
    139       node.Value = c;
    140       return node;
    141     }
    14271  }
    14372}
Note: See TracChangeset for help on using the changeset viewer.