Changeset 8935 for branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression
- Timestamp:
- 11/22/12 15:03:32 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolutionImpactValuesCalculator.cs
r8409 r8935 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using System.Linq; 25 using HeuristicLab.Common; 24 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 27 26 28 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression { 27 29 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)); 36 35 } 37 36 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) { 39 41 var problemData = (IRegressionProblemData)regressionProblemData; 40 42 var dataset = problemData.Dataset; 41 43 var rows = problemData.TrainingIndices.ToList(); 42 44 string targetVariable = problemData.TargetVariable; 43 var impactValues = new Dictionary<ISymbolicExpressionTreeNode, double>();44 45 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(); 46 47 var targetValues = dataset.GetDoubleValues(targetVariable, rows).ToList(); 47 48 OnlineCalculatorError errorState; … … 49 50 if (errorState != OnlineCalculatorError.None) originalR2 = 0.0; 50 51 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 } 60 55 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; 68 79 } 69 80 }
Note: See TracChangeset
for help on using the changeset viewer.