Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/17/11 12:25:30 (14 years ago)
Author:
gkronber
Message:

#1418 moved liner scaling method into symbolic regression model and fixed bug in interactive solution simplifier

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolution.cs

    r5720 r5729  
    5959      return new SymbolicRegressionSolution(this, cloner);
    6060    }
     61
     62    public void ScaleModel() {
     63      var dataset = ProblemData.Dataset;
     64      var targetVariable = ProblemData.TargetVariable;
     65      var rows = ProblemData.TrainingIndizes;
     66      var estimatedValues = GetEstimatedValues(rows);
     67      var targetValues = dataset.GetEnumeratedVariableValues(targetVariable, rows);
     68      double alpha;
     69      double beta;
     70      OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta);
     71
     72      ConstantTreeNode alphaTreeNode = null;
     73      ConstantTreeNode betaTreeNode = null;
     74      // check if model has been scaled previously by analyzing the structure of the tree
     75      var startNode = Model.SymbolicExpressionTree.Root.GetSubTree(0);
     76      if (startNode.GetSubTree(0).Symbol is Addition) {
     77        var addNode = startNode.GetSubTree(0);
     78        if (addNode.SubtreesCount == 2 && addNode.GetSubTree(0).Symbol is Multiplication && addNode.GetSubTree(1).Symbol is Constant) {
     79          alphaTreeNode = addNode.GetSubTree(1) as ConstantTreeNode;
     80          var mulNode = addNode.GetSubTree(0);
     81          if (mulNode.SubtreesCount == 2 && mulNode.GetSubTree(1).Symbol is Constant) {
     82            betaTreeNode = mulNode.GetSubTree(1) as ConstantTreeNode;
     83          }
     84        }
     85      }
     86      // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
     87      if (alphaTreeNode != null && betaTreeNode != null) {
     88        betaTreeNode.Value *= beta;
     89        alphaTreeNode.Value *= beta;
     90        alphaTreeNode.Value += alpha;
     91      } else {
     92        var mainBranch = startNode.GetSubTree(0);
     93        startNode.RemoveSubTree(0);
     94        var scaledMainBranch = MakeSum(MakeProduct(beta, mainBranch), alpha);
     95        startNode.AddSubTree(scaledMainBranch);
     96      }
     97
     98      OnModelChanged(EventArgs.Empty);
     99    }
     100
     101    private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
     102      if (alpha.IsAlmost(0.0)) {
     103        return treeNode;
     104      } else {
     105        var node = (new Addition()).CreateTreeNode();
     106        var alphaConst = MakeConstant(alpha);
     107        node.AddSubTree(treeNode);
     108        node.AddSubTree(alphaConst);
     109        return node;
     110      }
     111    }
     112
     113    private static ISymbolicExpressionTreeNode MakeProduct(double beta, ISymbolicExpressionTreeNode treeNode) {
     114      if (beta.IsAlmost(1.0)) {
     115        return treeNode;
     116      } else {
     117        var node = (new Multiplication()).CreateTreeNode();
     118        var betaConst = MakeConstant(beta);
     119        node.AddSubTree(treeNode);
     120        node.AddSubTree(betaConst);
     121        return node;
     122      }
     123    }
     124
     125    private static ISymbolicExpressionTreeNode MakeConstant(double c) {
     126      var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
     127      node.Value = c;
     128      return node;
     129    }
    61130  }
    62131}
Note: See TracChangeset for help on using the changeset viewer.