Changeset 8664 for trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveBoundedMeanSquaredErrorEvaluator.cs
- Timestamp:
- 09/17/12 11:18:40 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveBoundedMeanSquaredErrorEvaluator.cs
r7259 r8664 47 47 IEnumerable<int> rows = GenerateRowsToEvaluate(); 48 48 var solution = SymbolicExpressionTreeParameter.ActualValue; 49 double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows );49 double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value); 50 50 QualityParameter.ActualValue = new DoubleValue(quality); 51 51 return base.Apply(); 52 52 } 53 53 54 public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IClassificationProblemData problemData, IEnumerable<int> rows ) {54 public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IClassificationProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling) { 55 55 IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows); 56 IEnumerable<double> originalValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);57 IEnumerable<double> boundedEstimationValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);56 IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows); 57 OnlineCalculatorError errorState; 58 58 59 double minClassValue = problemData.ClassValues.OrderBy(x => x).First();60 double maxClassValue = problemData.ClassValues.OrderBy(x => x).Last();59 double lowestClassValue = problemData.ClassValues.OrderBy(x => x).First(); 60 double upmostClassValue = problemData.ClassValues.OrderByDescending(x => x).First(); 61 61 62 IEnumerator<double> originalEnumerator = originalValues.GetEnumerator(); 63 IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator(); 64 double errorSum = 0.0; 65 int n = 0; 66 67 // always move forward both enumerators (do not use short-circuit evaluation!) 68 while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) { 69 double estimated = estimatedEnumerator.Current; 70 double original = originalEnumerator.Current; 71 double error = estimated - original; 72 73 if (estimated < minClassValue || estimated > maxClassValue) 74 errorSum += Math.Abs(error); 75 else 76 errorSum += Math.Pow(error, 2); 77 n++; 62 double boundedMse; 63 if (applyLinearScaling) { 64 var boundedMseCalculator = new OnlineBoundedMeanSquaredErrorCalculator(lowestClassValue, upmostClassValue); 65 CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, boundedMseCalculator, problemData.Dataset.Rows); 66 errorState = boundedMseCalculator.ErrorState; 67 boundedMse = boundedMseCalculator.BoundedMeanSquaredError; 68 } else { 69 IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit); 70 boundedMse = OnlineBoundedMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, lowestClassValue, upmostClassValue, out errorState); 78 71 } 79 80 // check if both enumerators are at the end to make sure both enumerations have the same length 81 if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext()) { 82 throw new ArgumentException("Number of elements in first and second enumeration doesn't match."); 83 } else { 84 return errorSum / n; 85 } 72 if (errorState != OnlineCalculatorError.None) return Double.NaN; 73 return boundedMse; 86 74 } 87 75 … … 89 77 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context; 90 78 EstimationLimitsParameter.ExecutionContext = context; 79 ApplyLinearScalingParameter.ExecutionContext = context; 91 80 92 double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows );81 double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value); 93 82 94 83 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null; 95 84 EstimationLimitsParameter.ExecutionContext = null; 85 ApplyLinearScalingParameter.ExecutionContext = null; 96 86 97 87 return mse;
Note: See TracChangeset
for help on using the changeset viewer.