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