Changeset 7677 for trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators
- Timestamp:
- 03/30/12 22:48:32 (13 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators
- Files:
-
- 2 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveEvaluator.cs
r7672 r7677 20 20 #endregion 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; … … 54 55 } 55 56 } 57 58 [ThreadStatic] 59 private static double[] cache; 60 61 protected static void CalculateWithScaling(IEnumerable<double> targetValues, IEnumerable<double> estimatedValues, IOnlineCalculator calculator, int maxRows) { 62 if (cache == null || cache.GetLength(0) < maxRows) { 63 cache = new double[maxRows]; 64 } 65 66 //calculate linear scaling 67 //the static methods of the calculator could not be used as it performs a check if the enumerators have an equal amount of elements 68 //this is not true if the cache is used 69 int i = 0; 70 var linearScalingCalculator = new OnlineLinearScalingParameterCalculator(); 71 var targetValuesEnumerator = targetValues.GetEnumerator(); 72 var estimatedValuesEnumerator = estimatedValues.GetEnumerator(); 73 while (targetValuesEnumerator.MoveNext() && estimatedValuesEnumerator.MoveNext()) { 74 double target = targetValuesEnumerator.Current; 75 double estimated = estimatedValuesEnumerator.Current; 76 cache[i] = estimated; 77 linearScalingCalculator.Add(estimated, target); 78 i++; 79 } 80 double alpha = linearScalingCalculator.Alpha; 81 double beta = linearScalingCalculator.Beta; 82 83 //calculate the quality by using the passed online calculator 84 targetValuesEnumerator = targetValues.GetEnumerator(); 85 i = 0; 86 while (targetValuesEnumerator.MoveNext()) { 87 double target = targetValuesEnumerator.Current; 88 double estimated = cache[i] * beta + alpha; 89 calculator.Add(target, estimated); 90 i++; 91 } 92 } 56 93 } 57 94 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator.cs
r7672 r7677 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq;25 24 using HeuristicLab.Common; 26 25 using HeuristicLab.Core; … … 30 29 31 30 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression { 32 [Item("M ean squared error Evaluator", "Calculates the meansquared error of a symbolic regression solution.")]31 [Item("Maximum absolute error Evaluator", "Calculates the maximum squared error of a symbolic regression solution.")] 33 32 [StorableClass] 34 public class SymbolicRegressionSingleObjectiveM eanSquaredErrorEvaluator : SymbolicRegressionSingleObjectiveEvaluator {33 public class SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator : SymbolicRegressionSingleObjectiveEvaluator { 35 34 public override bool Maximization { get { return false; } } 36 [ThreadStatic]37 private static double[] estimatedValuesCache;38 39 35 [StorableConstructor] 40 protected SymbolicRegressionSingleObjectiveM eanSquaredErrorEvaluator(bool deserializing) : base(deserializing) { }41 protected SymbolicRegressionSingleObjectiveM eanSquaredErrorEvaluator(SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator original, Cloner cloner)36 protected SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator(bool deserializing) : base(deserializing) { } 37 protected SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator(SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator original, Cloner cloner) 42 38 : base(original, cloner) { 43 39 } 44 40 public override IDeepCloneable Clone(Cloner cloner) { 45 return new SymbolicRegressionSingleObjectiveM eanSquaredErrorEvaluator(this, cloner);41 return new SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator(this, cloner); 46 42 } 47 public SymbolicRegressionSingleObjectiveM eanSquaredErrorEvaluator() : base() { }43 public SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator() : base() { } 48 44 49 45 public override IOperation Apply() { … … 59 55 public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling) { 60 56 IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows); 61 IEnumerable<double> originalValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);57 IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows); 62 58 IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit); 63 59 OnlineCalculatorError errorState; … … 65 61 double mse; 66 62 if (applyLinearScaling) { 67 //int i = 0; 68 //int rowCount = rows.Count(); 69 //if (estimatedValuesCache == null) estimatedValuesCache = new double[problemData.Dataset.Rows]; 70 //foreach (var value in boundedEstimatedValues) { 71 // estimatedValuesCache[i] = value; 72 // i++; 73 //} 74 75 double alpha, beta; 76 OnlineLinearScalingParameterCalculator.Calculate(boundedEstimatedValues, originalValues, out alpha, out beta, out errorState); 77 mse = OnlineMeanSquaredErrorCalculator.Calculate(originalValues, boundedEstimatedValues.Select(value => value * beta + alpha), out errorState); 63 var maeCalculator = new OnlineMaxAbsoluteErrorCalculator(); 64 CalculateWithScaling(targetValues, boundedEstimatedValues, maeCalculator, problemData.Dataset.Rows); 65 errorState = maeCalculator.ErrorState; 66 mse = maeCalculator.MaxAbsoluteError; 78 67 } else 79 mse = OnlineM eanSquaredErrorCalculator.Calculate(originalValues, boundedEstimatedValues, out errorState);68 mse = OnlineMaxAbsoluteErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState); 80 69 81 70 if (errorState != OnlineCalculatorError.None) return Double.NaN; -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator.cs
r7672 r7677 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq;25 24 using HeuristicLab.Common; 26 25 using HeuristicLab.Core; … … 30 29 31 30 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression { 32 [Item("Mean squared error Evaluator", "Calculates the mean squarederror of a symbolic regression solution.")]31 [Item("Mean absolute error Evaluator", "Calculates the mean absolute error of a symbolic regression solution.")] 33 32 [StorableClass] 34 public class SymbolicRegressionSingleObjectiveMean SquaredErrorEvaluator : SymbolicRegressionSingleObjectiveEvaluator {33 public class SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator : SymbolicRegressionSingleObjectiveEvaluator { 35 34 public override bool Maximization { get { return false; } } 36 [ThreadStatic]37 private static double[] estimatedValuesCache;38 39 35 [StorableConstructor] 40 protected SymbolicRegressionSingleObjectiveMean SquaredErrorEvaluator(bool deserializing) : base(deserializing) { }41 protected SymbolicRegressionSingleObjectiveMean SquaredErrorEvaluator(SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator original, Cloner cloner)36 protected SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator(bool deserializing) : base(deserializing) { } 37 protected SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator(SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator original, Cloner cloner) 42 38 : base(original, cloner) { 43 39 } 44 40 public override IDeepCloneable Clone(Cloner cloner) { 45 return new SymbolicRegressionSingleObjectiveMean SquaredErrorEvaluator(this, cloner);41 return new SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator(this, cloner); 46 42 } 47 public SymbolicRegressionSingleObjectiveMean SquaredErrorEvaluator() : base() { }43 public SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator() : base() { } 48 44 49 45 public override IOperation Apply() { … … 59 55 public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling) { 60 56 IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows); 61 IEnumerable<double> originalValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);57 IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows); 62 58 IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit); 63 59 OnlineCalculatorError errorState; … … 65 61 double mse; 66 62 if (applyLinearScaling) { 67 //int i = 0; 68 //int rowCount = rows.Count(); 69 //if (estimatedValuesCache == null) estimatedValuesCache = new double[problemData.Dataset.Rows]; 70 //foreach (var value in boundedEstimatedValues) { 71 // estimatedValuesCache[i] = value; 72 // i++; 73 //} 74 75 double alpha, beta; 76 OnlineLinearScalingParameterCalculator.Calculate(boundedEstimatedValues, originalValues, out alpha, out beta, out errorState); 77 mse = OnlineMeanSquaredErrorCalculator.Calculate(originalValues, boundedEstimatedValues.Select(value => value * beta + alpha), out errorState); 63 var maeCalculator = new OnlineMeanAbsoluteErrorCalculator(); 64 CalculateWithScaling(targetValues, boundedEstimatedValues, maeCalculator, problemData.Dataset.Rows); 65 errorState = maeCalculator.ErrorState; 66 mse = maeCalculator.MeanAbsoluteError; 78 67 } else 79 mse = OnlineMeanSquaredErrorCalculator.Calculate( originalValues, boundedEstimatedValues, out errorState);68 mse = OnlineMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState); 80 69 81 70 if (errorState != OnlineCalculatorError.None) return Double.NaN; -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.cs
r7672 r7677 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq;25 24 using HeuristicLab.Common; 26 25 using HeuristicLab.Core; … … 34 33 public class SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator : SymbolicRegressionSingleObjectiveEvaluator { 35 34 public override bool Maximization { get { return false; } } 36 [ThreadStatic]37 private static double[] estimatedValuesCache;38 39 35 [StorableConstructor] 40 36 protected SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator(bool deserializing) : base(deserializing) { } … … 59 55 public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling) { 60 56 IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows); 61 IEnumerable<double> originalValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);57 IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows); 62 58 IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit); 63 59 OnlineCalculatorError errorState; … … 65 61 double mse; 66 62 if (applyLinearScaling) { 67 //int i = 0; 68 //int rowCount = rows.Count(); 69 //if (estimatedValuesCache == null) estimatedValuesCache = new double[problemData.Dataset.Rows]; 70 //foreach (var value in boundedEstimatedValues) { 71 // estimatedValuesCache[i] = value; 72 // i++; 73 //} 74 75 double alpha, beta; 76 OnlineLinearScalingParameterCalculator.Calculate(boundedEstimatedValues, originalValues, out alpha, out beta, out errorState); 77 mse = OnlineMeanSquaredErrorCalculator.Calculate(originalValues, boundedEstimatedValues.Select(value => value * beta + alpha), out errorState); 63 var mseCalculator = new OnlineMeanSquaredErrorCalculator(); 64 CalculateWithScaling(targetValues, boundedEstimatedValues, mseCalculator, problemData.Dataset.Rows); 65 errorState = mseCalculator.ErrorState; 66 mse = mseCalculator.MeanSquaredError; 78 67 } else 79 mse = OnlineMeanSquaredErrorCalculator.Calculate( originalValues, boundedEstimatedValues, out errorState);68 mse = OnlineMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState); 80 69 81 70 if (errorState != OnlineCalculatorError.None) return Double.NaN;
Note: See TracChangeset
for help on using the changeset viewer.