Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/22/12 15:03:32 (12 years ago)
Author:
bburlacu
Message:

#1763: Bugfixes and refactoring as suggested in the comments above.

File:
1 edited

Legend:

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

    r8409 r8935  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Linq;
     25using HeuristicLab.Common;
    2426using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2527
    2628namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
    2729  public class SymbolicRegressionSolutionImpactValuesCalculator : SymbolicDataAnalysisSolutionImpactValuesCalculator {
    28     public override Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree,
    29                                                                                                ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
    30                                                                                                IDataAnalysisProblemData problemData) {
    31       var replacementValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
    32       foreach (ISymbolicExpressionTreeNode node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
    33         replacementValues[node] = CalculateReplacementValue(node, tree, interpreter, problemData);
    34       }
    35       return replacementValues;
     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));
    3635    }
    3736
    38     public override Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataAnalysisProblemData regressionProblemData, double lowerEstimationLimit, double upperEstimationLimit) {
     37    public override IEnumerable<Tuple<ISymbolicExpressionTreeNode, double>> CalculateImpactValues(ISymbolicExpressionTree tree,
     38                                                                                                  ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
     39                                                                                                  IDataAnalysisProblemData regressionProblemData,
     40                                                                                                  double lowerEstimationLimit, double upperEstimationLimit) {
    3941      var problemData = (IRegressionProblemData)regressionProblemData;
    4042      var dataset = problemData.Dataset;
    4143      var rows = problemData.TrainingIndices.ToList();
    4244      string targetVariable = problemData.TargetVariable;
    43       var impactValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
    4445      List<ISymbolicExpressionTreeNode> nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix().ToList();
    45       var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows).ToArray();
     46      var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows).LimitToRange(lowerEstimationLimit, upperEstimationLimit).ToArray();
    4647      var targetValues = dataset.GetDoubleValues(targetVariable, rows).ToList();
    4748      OnlineCalculatorError errorState;
     
    4950      if (errorState != OnlineCalculatorError.None) originalR2 = 0.0;
    5051
    51       foreach (ISymbolicExpressionTreeNode node in nodes) {
    52         var parent = node.Parent;
    53         var constantNode = ((ConstantTreeNode)new Constant().CreateTreeNode());
    54         constantNode.Value = CalculateReplacementValue(node, tree, interpreter, problemData);
    55         ISymbolicExpressionTreeNode replacementNode = constantNode;
    56         SwitchNode(parent, node, replacementNode);
    57         var newOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows);
    58         double newR2 = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, newOutput, out errorState);
    59         if (errorState != OnlineCalculatorError.None) newR2 = 0.0;
     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));
     54    }
    6055
    61         // impact = 0 if no change
    62         // impact < 0 if new solution is better
    63         // impact > 0 if new solution is worse
    64         impactValues[node] = originalR2 - newR2;
    65         SwitchNode(parent, replacementNode, node);
    66       }
    67       return impactValues;
     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;
    6879    }
    6980  }
Note: See TracChangeset for help on using the changeset viewer.