Changeset 8915 for branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisModel.cs
- Timestamp:
- 11/15/12 16:47:25 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisModel.cs
r7259 r8915 20 20 #endregion 21 21 22 using System; 22 23 using System.Drawing; 23 24 using HeuristicLab.Common; … … 66 67 this.interpreter = interpreter; 67 68 } 69 70 #region Scaling 71 public static void Scale(ISymbolicDataAnalysisModel model, IDataAnalysisProblemData problemData, string targetVariable) { 72 var dataset = problemData.Dataset; 73 var rows = problemData.TrainingIndices; 74 var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows); 75 var targetValues = dataset.GetDoubleValues(targetVariable, rows); 76 77 var linearScalingCalculator = new OnlineLinearScalingParameterCalculator(); 78 var targetValuesEnumerator = targetValues.GetEnumerator(); 79 var estimatedValuesEnumerator = estimatedValues.GetEnumerator(); 80 while (targetValuesEnumerator.MoveNext() & estimatedValuesEnumerator.MoveNext()) { 81 double target = targetValuesEnumerator.Current; 82 double estimated = estimatedValuesEnumerator.Current; 83 if (!double.IsNaN(estimated) && !double.IsInfinity(estimated)) 84 linearScalingCalculator.Add(estimated, target); 85 } 86 if (linearScalingCalculator.ErrorState == OnlineCalculatorError.None && (targetValuesEnumerator.MoveNext() || estimatedValuesEnumerator.MoveNext())) 87 throw new ArgumentException("Number of elements in target and estimated values enumeration do not match."); 88 89 double alpha = linearScalingCalculator.Alpha; 90 double beta = linearScalingCalculator.Beta; 91 if (linearScalingCalculator.ErrorState != OnlineCalculatorError.None) return; 92 93 ConstantTreeNode alphaTreeNode = null; 94 ConstantTreeNode betaTreeNode = null; 95 // check if model has been scaled previously by analyzing the structure of the tree 96 var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0); 97 if (startNode.GetSubtree(0).Symbol is Addition) { 98 var addNode = startNode.GetSubtree(0); 99 if (addNode.SubtreeCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) { 100 alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode; 101 var mulNode = addNode.GetSubtree(0); 102 if (mulNode.SubtreeCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) { 103 betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode; 104 } 105 } 106 } 107 // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes 108 if (alphaTreeNode != null && betaTreeNode != null) { 109 betaTreeNode.Value *= beta; 110 alphaTreeNode.Value *= beta; 111 alphaTreeNode.Value += alpha; 112 } else { 113 var mainBranch = startNode.GetSubtree(0); 114 startNode.RemoveSubtree(0); 115 var scaledMainBranch = MakeSum(MakeProduct(mainBranch, beta), alpha); 116 startNode.AddSubtree(scaledMainBranch); 117 } 118 } 119 120 private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) { 121 if (alpha.IsAlmost(0.0)) { 122 return treeNode; 123 } else { 124 var addition = new Addition(); 125 var node = addition.CreateTreeNode(); 126 var alphaConst = MakeConstant(alpha); 127 node.AddSubtree(treeNode); 128 node.AddSubtree(alphaConst); 129 return node; 130 } 131 } 132 133 private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode treeNode, double beta) { 134 if (beta.IsAlmost(1.0)) { 135 return treeNode; 136 } else { 137 var multipliciation = new Multiplication(); 138 var node = multipliciation.CreateTreeNode(); 139 var betaConst = MakeConstant(beta); 140 node.AddSubtree(treeNode); 141 node.AddSubtree(betaConst); 142 return node; 143 } 144 } 145 146 private static ISymbolicExpressionTreeNode MakeConstant(double c) { 147 var node = (ConstantTreeNode)(new Constant()).CreateTreeNode(); 148 node.Value = c; 149 return node; 150 } 151 #endregion 68 152 } 69 153 }
Note: See TracChangeset
for help on using the changeset viewer.