Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/27/12 11:02:09 (11 years ago)
Author:
mkommend
Message:

#1763: Merged remaining changes from the TreeSimplifier branch in the trunk and refactored impact values calculators.

Location:
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression
Files:
4 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression

  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4

    • Property svn:ignore
      •  

        old new  
         1*.user
         2Plugin.cs
        13bin
        2 *.user
        3 HeuristicLabProblemsDataAnalysisSymbolicRegressionPlugin.cs
        44obj
        5 *.vs10x
        6 Plugin.cs
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj

    r8895 r8946  
    143143    <Compile Include="SingleObjective\Evaluators\SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.cs" />
    144144    <Compile Include="SymbolicRegressionSolution.cs" />
     145    <Compile Include="SymbolicRegressionSolutionImpactValuesCalculator.cs" />
    145146    <None Include="HeuristicLab.snk" />
    146147    <None Include="Plugin.cs.frame" />
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/Properties

    • Property svn:ignore
      --- 
      +++ 
      
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolutionImpactValuesCalculator.cs

    r8942 r8946  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    24 using System.Linq;
    2523using HeuristicLab.Common;
    2624using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     
    2826namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
    2927  public class SymbolicRegressionSolutionImpactValuesCalculator : SymbolicDataAnalysisSolutionImpactValuesCalculator {
    30     public override IEnumerable<Tuple<ISymbolicExpressionTreeNode, double>> CalculateReplacementValues(ISymbolicExpressionTree tree,
    31                                                                                                        ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
    32                                                                                                        IDataAnalysisProblemData problemData) {
    33       return from node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()
    34              select new Tuple<ISymbolicExpressionTreeNode, double>(node, CalculateReplacementValue(node, tree, interpreter, problemData));
     28    public override double CalculateReplacementValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows) {
     29      var regressionModel = (ISymbolicRegressionModel)model;
     30      var regressionProblemData = (IRegressionProblemData)problemData;
     31
     32      return CalculateReplacementValue(node, regressionModel.SymbolicExpressionTree, regressionModel.Interpreter, regressionProblemData.Dataset, rows);
    3533    }
    3634
    37     public override IEnumerable<Tuple<ISymbolicExpressionTreeNode, double>> CalculateImpactValues(ISymbolicExpressionTree tree,
    38                                                                                                   ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
    39                                                                                                   IDataAnalysisProblemData regressionProblemData,
    40                                                                                                   double lowerEstimationLimit, double upperEstimationLimit) {
    41       var problemData = (IRegressionProblemData)regressionProblemData;
    42       var dataset = problemData.Dataset;
    43       var rows = problemData.TrainingIndices.ToList();
    44       string targetVariable = problemData.TargetVariable;
    45       List<ISymbolicExpressionTreeNode> nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix().ToList();
    46       var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows).LimitToRange(lowerEstimationLimit, upperEstimationLimit).ToArray();
    47       var targetValues = dataset.GetDoubleValues(targetVariable, rows).ToList();
     35    public override double CalculateImpactValue(ISymbolicDataAnalysisModel model, ISymbolicExpressionTreeNode node, IDataAnalysisProblemData problemData, IEnumerable<int> rows, double originalQuality = double.NaN) {
     36      var regressionModel = (ISymbolicRegressionModel)model;
     37      var regressionProblemData = (IRegressionProblemData)problemData;
     38
     39      var dataset = regressionProblemData.Dataset;
     40      var targetValues = dataset.GetDoubleValues(regressionProblemData.TargetVariable, rows);
     41
    4842      OnlineCalculatorError errorState;
    49       double originalR2 = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, originalOutput, out errorState);
    50       if (errorState != OnlineCalculatorError.None) originalR2 = 0.0;
     43      if (double.IsNaN(originalQuality)) {
     44        var originalClassValues = regressionModel.GetEstimatedValues(dataset, rows);
     45        originalQuality = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, originalClassValues, out errorState);
     46        if (errorState != OnlineCalculatorError.None) originalQuality = 0.0;
     47      }
    5148
    52       return from node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix().ToList()
    53              select new Tuple<ISymbolicExpressionTreeNode, double>(node, CalculateImpact(tree, originalR2, node, interpreter, problemData, lowerEstimationLimit, upperEstimationLimit));
     49      var replacementValue = CalculateReplacementValue(regressionModel, node, regressionProblemData, rows);
     50      var constantNode = new ConstantTreeNode(new Constant()) { Value = replacementValue };
     51      var cloner = new Cloner();
     52      cloner.RegisterClonedObject(node, constantNode);
     53      var tempModel = cloner.Clone(regressionModel);
     54      SymbolicDataAnalysisModel.Scale(tempModel, regressionProblemData, regressionProblemData.TargetVariable);
     55
     56      var estimatedValues = tempModel.GetEstimatedValues(dataset, rows);
     57      double newQuality = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, estimatedValues, out errorState);
     58      if (errorState != OnlineCalculatorError.None) newQuality = 0.0;
     59
     60      return originalQuality - newQuality;
    5461    }
    5562
    56     private static double CalculateImpact(ISymbolicExpressionTree tree, double originalQuality, ISymbolicExpressionTreeNode node,
    57                                           ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IRegressionProblemData problemData,
    58                                           double lowerEstimationLimit, double upperEstimationLimit) {
    59       var dataset = problemData.Dataset;
    60       var rows = problemData.TrainingIndices.ToList();
    61       string targetVariable = problemData.TargetVariable;
    62       var targetValues = dataset.GetDoubleValues(targetVariable, rows).ToList();
    63 
    64       var parent = node.Parent;
    65       var constantNode = (ConstantTreeNode)new Constant().CreateTreeNode();
    66       constantNode.Value = CalculateReplacementValue(node, tree, interpreter, problemData);
    67       SwitchNode(parent, node, constantNode);
    68       var newOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows)
    69                                  .LimitToRange(lowerEstimationLimit, upperEstimationLimit)
    70                                  .ToArray();
    71       OnlineCalculatorError errorState;
    72       double quality = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, newOutput, out errorState);
    73       if (errorState != OnlineCalculatorError.None) quality = 0.0;
    74       SwitchNode(parent, constantNode, node);
    75       // impact = 0 if no change
    76       // impact < 0 if new solution is better
    77       // impact > 0 if new solution is worse
    78       return originalQuality - quality;
    79     }
    8063  }
    8164}
Note: See TracChangeset for help on using the changeset viewer.