Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/23/11 13:52:29 (14 years ago)
Author:
gkronber
Message:

#1418 Fixed a problem with scaling of regression and classification solutions (moved scale method out of solution into the model because of leaky abstraction).

Location:
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectiveTrainingBestSolutionAnalyzer.cs

    r5809 r5818  
    7777    protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality) {
    7878      var model = new SymbolicDiscriminantFunctionClassificationModel(bestTree, SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
    79       var solution = new SymbolicDiscriminantFunctionClassificationSolution(model, ProblemDataParameter.ActualValue);
    8079      if (ApplyLinearScaling.Value) {
    81         solution.ScaleModel();
     80        SymbolicDiscriminantFunctionClassificationModel.Scale(model, ProblemDataParameter.ActualValue);
    8281      }
    83       return solution;
     82      return new SymbolicDiscriminantFunctionClassificationSolution(model, ProblemDataParameter.ActualValue);
    8483    }
    8584  }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectiveValidationBestSolutionAnalyzer.cs

    r5809 r5818  
    6666    protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQualities) {
    6767      var model = new SymbolicDiscriminantFunctionClassificationModel(bestTree, SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
    68       var solution = new SymbolicDiscriminantFunctionClassificationSolution(model, ProblemDataParameter.ActualValue);
    6968      if (ApplyLinearScaling.Value) {
    70         solution.ScaleModel();
     69        SymbolicDiscriminantFunctionClassificationModel.Scale(model, ProblemDataParameter.ActualValue);
    7170      }
    72       return solution;
     71      return new SymbolicDiscriminantFunctionClassificationSolution(model, ProblemDataParameter.ActualValue);
    7372    }
    7473  }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveTrainingBestSolutionAnalyzer.cs

    r5809 r5818  
    7575    protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) {
    7676      var model = new SymbolicDiscriminantFunctionClassificationModel(bestTree, SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
    77       var solution = new SymbolicDiscriminantFunctionClassificationSolution(model, ProblemDataParameter.ActualValue);
    7877      if (ApplyLinearScaling.Value) {
    79         solution.ScaleModel();
     78        SymbolicDiscriminantFunctionClassificationModel.Scale(model, ProblemDataParameter.ActualValue);
    8079      }
    81       return solution;
     80      return new SymbolicDiscriminantFunctionClassificationSolution(model, ProblemDataParameter.ActualValue);
    8281    }
    8382  }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveValidationBestSolutionAnalyzer.cs

    r5809 r5818  
    6666    protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) {
    6767      var model = new SymbolicDiscriminantFunctionClassificationModel(bestTree, SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
    68       var solution = new SymbolicDiscriminantFunctionClassificationSolution(model, ProblemDataParameter.ActualValue);
    6968      if (ApplyLinearScaling.Value) {
    70         solution.ScaleModel();
     69        SymbolicDiscriminantFunctionClassificationModel.Scale(model, ProblemDataParameter.ActualValue);
    7170      }
    72       return solution;
     71      return new SymbolicDiscriminantFunctionClassificationSolution(model, ProblemDataParameter.ActualValue);
    7372    }
    7473  }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicDiscriminantFunctionClassificationModel.cs

    r5809 r5818  
    115115    }
    116116    #endregion
     117
     118    public static void Scale(SymbolicDiscriminantFunctionClassificationModel model, IClassificationProblemData problemData) {
     119      var dataset = problemData.Dataset;
     120      var targetVariable = problemData.TargetVariable;
     121      var rows = problemData.TrainingIndizes;
     122      var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows);
     123      var targetValues = dataset.GetEnumeratedVariableValues(targetVariable, rows);
     124      double alpha;
     125      double beta;
     126      OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta);
     127
     128      ConstantTreeNode alphaTreeNode = null;
     129      ConstantTreeNode betaTreeNode = null;
     130      // check if model has been scaled previously by analyzing the structure of the tree
     131      var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0);
     132      if (startNode.GetSubtree(0).Symbol is Addition) {
     133        var addNode = startNode.GetSubtree(0);
     134        if (addNode.SubtreesCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) {
     135          alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode;
     136          var mulNode = addNode.GetSubtree(0);
     137          if (mulNode.SubtreesCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) {
     138            betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode;
     139          }
     140        }
     141      }
     142      // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
     143      if (alphaTreeNode != null && betaTreeNode != null) {
     144        betaTreeNode.Value *= beta;
     145        alphaTreeNode.Value *= beta;
     146        alphaTreeNode.Value += alpha;
     147      } else {
     148        var mainBranch = startNode.GetSubtree(0);
     149        startNode.RemoveSubtree(0);
     150        var scaledMainBranch = MakeSum(MakeProduct(beta, mainBranch), alpha);
     151        startNode.AddSubtree(scaledMainBranch);
     152      }
     153    }
     154
     155    private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
     156      if (alpha.IsAlmost(0.0)) {
     157        return treeNode;
     158      } else {
     159        var node = (new Addition()).CreateTreeNode();
     160        var alphaConst = MakeConstant(alpha);
     161        node.AddSubtree(treeNode);
     162        node.AddSubtree(alphaConst);
     163        return node;
     164      }
     165    }
     166
     167    private static ISymbolicExpressionTreeNode MakeProduct(double beta, ISymbolicExpressionTreeNode treeNode) {
     168      if (beta.IsAlmost(1.0)) {
     169        return treeNode;
     170      } else {
     171        var node = (new Multiplication()).CreateTreeNode();
     172        var betaConst = MakeConstant(beta);
     173        node.AddSubtree(treeNode);
     174        node.AddSubtree(betaConst);
     175        return node;
     176      }
     177    }
     178
     179    private static ISymbolicExpressionTreeNode MakeConstant(double c) {
     180      var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
     181      node.Value = c;
     182      return node;
     183    }
    117184  }
    118185}
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicDiscriminantFunctionClassificationSolution.cs

    r5809 r5818  
    8787      ModelLength = Model.SymbolicExpressionTree.Length;
    8888      ModelDepth = Model.SymbolicExpressionTree.Depth;
    89     }
    90 
    91     public void ScaleModel() {
    92       var dataset = ProblemData.Dataset;
    93       var targetVariable = ProblemData.TargetVariable;
    94       var rows = ProblemData.TrainingIndizes;
    95       var estimatedValues = GetEstimatedValues(rows);
    96       var targetValues = dataset.GetEnumeratedVariableValues(targetVariable, rows);
    97       double alpha;
    98       double beta;
    99       OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta);
    100 
    101       ConstantTreeNode alphaTreeNode = null;
    102       ConstantTreeNode betaTreeNode = null;
    103       // check if model has been scaled previously by analyzing the structure of the tree
    104       var startNode = Model.SymbolicExpressionTree.Root.GetSubtree(0);
    105       if (startNode.GetSubtree(0).Symbol is Addition) {
    106         var addNode = startNode.GetSubtree(0);
    107         if (addNode.SubtreesCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) {
    108           alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode;
    109           var mulNode = addNode.GetSubtree(0);
    110           if (mulNode.SubtreesCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) {
    111             betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode;
    112           }
    113         }
    114       }
    115       // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
    116       if (alphaTreeNode != null && betaTreeNode != null) {
    117         betaTreeNode.Value *= beta;
    118         alphaTreeNode.Value *= beta;
    119         alphaTreeNode.Value += alpha;
    120       } else {
    121         var mainBranch = startNode.GetSubtree(0);
    122         startNode.RemoveSubtree(0);
    123         var scaledMainBranch = MakeSum(MakeProduct(beta, mainBranch), alpha);
    124         startNode.AddSubtree(scaledMainBranch);
    125       }
    126 
    127       OnModelChanged(EventArgs.Empty);
    128     }
    129 
    130     private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
    131       if (alpha.IsAlmost(0.0)) {
    132         return treeNode;
    133       } else {
    134         var node = (new Addition()).CreateTreeNode();
    135         var alphaConst = MakeConstant(alpha);
    136         node.AddSubtree(treeNode);
    137         node.AddSubtree(alphaConst);
    138         return node;
    139       }
    140     }
    141 
    142     private static ISymbolicExpressionTreeNode MakeProduct(double beta, ISymbolicExpressionTreeNode treeNode) {
    143       if (beta.IsAlmost(1.0)) {
    144         return treeNode;
    145       } else {
    146         var node = (new Multiplication()).CreateTreeNode();
    147         var betaConst = MakeConstant(beta);
    148         node.AddSubtree(treeNode);
    149         node.AddSubtree(betaConst);
    150         return node;
    151       }
    152     }
    153 
    154     private static ISymbolicExpressionTreeNode MakeConstant(double c) {
    155       var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
    156       node.Value = c;
    157       return node;
    158     }
     89    }   
    15990  }
    16091}
Note: See TracChangeset for help on using the changeset viewer.