- Timestamp:
- 09/17/12 11:18:40 (12 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4
- Files:
-
- 17 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveMeanSquaredErrorTreeSizeEvaluator.cs
r7259 r8664 47 47 IEnumerable<int> rows = GenerateRowsToEvaluate(); 48 48 var solution = SymbolicExpressionTreeParameter.ActualValue; 49 double[] qualities = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows );49 double[] qualities = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value); 50 50 QualitiesParameter.ActualValue = new DoubleArray(qualities); 51 51 return base.Apply(); 52 52 } 53 53 54 public static double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows ) {54 public static double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData 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); 58 57 OnlineCalculatorError errorState; 59 double mse = OnlineMeanSquaredErrorCalculator.Calculate(originalValues, boundedEstimationValues, out errorState); 58 59 double mse; 60 if (applyLinearScaling) { 61 var mseCalculator = new OnlineMeanSquaredErrorCalculator(); 62 CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, mseCalculator, problemData.Dataset.Rows); 63 errorState = mseCalculator.ErrorState; 64 mse = mseCalculator.MeanSquaredError; 65 } else { 66 IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit); 67 mse = OnlineMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState); 68 } 60 69 if (errorState != OnlineCalculatorError.None) mse = double.NaN; 61 70 return new double[2] { mse, solution.Length }; … … 65 74 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context; 66 75 EstimationLimitsParameter.ExecutionContext = context; 76 ApplyLinearScalingParameter.ExecutionContext = context; 67 77 68 double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows );78 double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value); 69 79 70 80 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null; 71 81 EstimationLimitsParameter.ExecutionContext = null; 82 ApplyLinearScalingParameter.ExecutionContext = null; 72 83 73 84 return quality; -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectivePearsonRSquaredTreeSizeEvaluator.cs
r7259 r8664 47 47 IEnumerable<int> rows = GenerateRowsToEvaluate(); 48 48 var solution = SymbolicExpressionTreeParameter.ActualValue; 49 double[] qualities = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows );49 double[] qualities = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value); 50 50 QualitiesParameter.ActualValue = new DoubleArray(qualities); 51 51 return base.Apply(); 52 52 } 53 53 54 public static double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows ) {54 public static double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData 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);56 IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows); 57 57 OnlineCalculatorError errorState; 58 double r2 = OnlinePearsonsRSquaredCalculator.Calculate(estimatedValues, originalValues, out errorState); 59 if (errorState != OnlineCalculatorError.None) r2 = 0.0; 60 return new double[] { r2, solution.Length }; 58 59 double r2; 60 if (applyLinearScaling) { 61 var r2Calculator = new OnlinePearsonsRSquaredCalculator(); 62 CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, r2Calculator, problemData.Dataset.Rows); 63 errorState = r2Calculator.ErrorState; 64 r2 = r2Calculator.RSquared; 65 } else { 66 IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit); 67 r2 = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState); 68 } 69 70 if (errorState != OnlineCalculatorError.None) r2 = double.NaN; 71 return new double[2] { r2, solution.Length }; 61 72 } 62 73 … … 64 75 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context; 65 76 EstimationLimitsParameter.ExecutionContext = context; 77 ApplyLinearScalingParameter.ExecutionContext = context; 66 78 67 double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows );79 double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value); 68 80 69 81 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null; 70 82 EstimationLimitsParameter.ExecutionContext = null; 83 ApplyLinearScalingParameter.ExecutionContext = null; 71 84 72 85 return quality; -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveProblem.cs
r8175 r8664 65 65 EstimationLimitsParameter.Hidden = true; 66 66 67 ApplyLinearScalingParameter.Value.Value = true; 67 68 Maximization = new BoolArray(new bool[] { true, false }); 68 69 MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth; -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer.cs
r7259 r8664 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Data;25 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 25 using HeuristicLab.Parameters; … … 38 37 private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter"; 39 38 private const string EstimationLimitsParameterName = "EstimationLimits"; 40 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";41 39 #region parameter properties 42 40 public ILookupParameter<IRegressionProblemData> ProblemDataParameter { … … 48 46 public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter { 49 47 get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; } 50 }51 public IValueParameter<BoolValue> ApplyLinearScalingParameter {52 get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }53 }54 #endregion55 56 #region properties57 public BoolValue ApplyLinearScaling {58 get { return ApplyLinearScalingParameter.Value; }59 48 } 60 49 #endregion … … 68 57 Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The symbolic data analysis tree interpreter for the symbolic expression tree.")); 69 58 Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic regression model.")); 70 Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true)));71 59 } 72 60 … … 77 65 protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality) { 78 66 var model = new SymbolicRegressionModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper); 79 if (ApplyLinearScaling.Value) 80 SymbolicRegressionModel.Scale(model, ProblemDataParameter.ActualValue); 67 if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicRegressionModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TargetVariable); 81 68 return new SymbolicRegressionSolution(model, (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone()); 82 69 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveValidationBestSolutionAnalyzer.cs
r7259 r8664 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Data;25 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 25 using HeuristicLab.Parameters; … … 36 35 ISymbolicDataAnalysisBoundedOperator { 37 36 private const string EstimationLimitsParameterName = "EstimationLimits"; 38 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";39 37 40 38 #region parameter properties … … 42 40 get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; } 43 41 } 44 public IValueParameter<BoolValue> ApplyLinearScalingParameter {45 get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }46 }47 42 #endregion 48 43 49 #region properties50 public BoolValue ApplyLinearScaling {51 get { return ApplyLinearScalingParameter.Value; }52 }53 #endregion54 44 [StorableConstructor] 55 45 private SymbolicRegressionMultiObjectiveValidationBestSolutionAnalyzer(bool deserializing) : base(deserializing) { } … … 58 48 : base() { 59 49 Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic regression model.")); 60 Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true)));61 50 } 62 51 public override IDeepCloneable Clone(Cloner cloner) { … … 66 55 protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality) { 67 56 var model = new SymbolicRegressionModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper); 68 if (ApplyLinearScaling.Value) 69 SymbolicRegressionModel.Scale(model, ProblemDataParameter.ActualValue); 57 if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicRegressionModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TargetVariable); 70 58 return new SymbolicRegressionSolution(model, (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone()); 71 59 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionConstantOptimizationEvaluator.cs
r8053 r8664 106 106 IEnumerable<int> constantOptimizationRows = GenerateRowsToEvaluate(ConstantOptimizationRowsPercentage.Value); 107 107 quality = OptimizeConstants(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, ProblemDataParameter.ActualValue, 108 constantOptimizationRows, ConstantOptimizationImprovement.Value, ConstantOptimizationIterations.Value, 0.001,108 constantOptimizationRows, ApplyLinearScalingParameter.ActualValue.Value, ConstantOptimizationImprovement.Value, ConstantOptimizationIterations.Value, 0.001, 109 109 EstimationLimitsParameter.ActualValue.Upper, EstimationLimitsParameter.ActualValue.Lower, 110 110 EvaluatedTreesParameter.ActualValue, EvaluatedTreeNodesParameter.ActualValue); 111 111 if (ConstantOptimizationRowsPercentage.Value != RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value) { 112 112 var evaluationRows = GenerateRowsToEvaluate(); 113 quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, evaluationRows );113 quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, evaluationRows, ApplyLinearScalingParameter.ActualValue.Value); 114 114 } 115 115 } else { 116 116 var evaluationRows = GenerateRowsToEvaluate(); 117 quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, evaluationRows );117 quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, evaluationRows, ApplyLinearScalingParameter.ActualValue.Value); 118 118 } 119 119 QualityParameter.ActualValue = new DoubleValue(quality); … … 145 145 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context; 146 146 EstimationLimitsParameter.ExecutionContext = context; 147 148 double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows); 147 ApplyLinearScalingParameter.ExecutionContext = context; 148 149 double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value); 149 150 150 151 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null; 151 152 EstimationLimitsParameter.ExecutionContext = null; 153 ApplyLinearScalingParameter.ExecutionContext = context; 152 154 153 155 return r2; … … 155 157 156 158 public static double OptimizeConstants(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree tree, IRegressionProblemData problemData, 157 IEnumerable<int> rows, double improvement, int iterations, double differentialStep, double upperEstimationLimit = double.MaxValue, double lowerEstimationLimit = double.MinValue, IntValue evaluatedTrees = null, IntValue evaluatedTreeNodes = null) {159 IEnumerable<int> rows, bool applyLinearScaling, double improvement, int iterations, double differentialStep, double upperEstimationLimit = double.MaxValue, double lowerEstimationLimit = double.MinValue, IntValue evaluatedTrees = null, IntValue evaluatedTreeNodes = null) { 158 160 List<SymbolicExpressionTreeTerminalNode> terminalNodes = tree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTerminalNode>().ToList(); 159 161 double[] c = new double[terminalNodes.Count]; … … 179 181 alglib.minlmcreatev(1, c, diffstep, out state); 180 182 alglib.minlmsetcond(state, epsg, epsf, epsx, maxits); 181 alglib.minlmoptimize(state, CreateCallBack(interpreter, tree, problemData, rows, upperEstimationLimit, lowerEstimationLimit, treeLength, evaluatedTrees, evaluatedTreeNodes), null, terminalNodes);183 alglib.minlmoptimize(state, CreateCallBack(interpreter, tree, problemData, rows, applyLinearScaling, upperEstimationLimit, lowerEstimationLimit, treeLength, evaluatedTrees, evaluatedTreeNodes), null, terminalNodes); 182 184 alglib.minlmresults(state, out c, out report); 183 185 … … 192 194 } 193 195 194 private static alglib.ndimensional_fvec CreateCallBack(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows, double upperEstimationLimit, double lowerEstimationLimit, int treeLength, IntValue evaluatedTrees = null, IntValue evaluatedTreeNodes = null) {196 private static alglib.ndimensional_fvec CreateCallBack(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling, double upperEstimationLimit, double lowerEstimationLimit, int treeLength, IntValue evaluatedTrees = null, IntValue evaluatedTreeNodes = null) { 195 197 return (double[] arg, double[] fi, object obj) => { 196 198 // update constants of tree … … 203 205 } 204 206 205 double quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows );207 double quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling); 206 208 207 209 fi[0] = 1 - quality; -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveEvaluator.cs
r8639 r8664 30 30 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression { 31 31 [StorableClass] 32 public abstract class SymbolicRegressionSingleObjectiveEvaluator : SymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData>, ISymbolicRegressionSingleObjectiveEvaluator { 33 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling"; 34 public IFixedValueParameter<BoolValue> ApplyLinearScalingParameter { 35 get { return (IFixedValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; } 36 } 37 public bool ApplyLinearScaling { 38 get { return ApplyLinearScalingParameter.Value.Value; } 39 set { ApplyLinearScalingParameter.Value.Value = value; } 40 } 41 32 public abstract class SymbolicRegressionSingleObjectiveEvaluator : SymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData>, ISymbolicRegressionSingleObjectiveEvaluator { 42 33 [StorableConstructor] 43 34 protected SymbolicRegressionSingleObjectiveEvaluator(bool deserializing) : base(deserializing) { } 44 35 protected SymbolicRegressionSingleObjectiveEvaluator(SymbolicRegressionSingleObjectiveEvaluator original, Cloner cloner) : base(original, cloner) { } 45 protected SymbolicRegressionSingleObjectiveEvaluator() 46 : base() { 47 Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the individual should be linearly scaled before evaluating.", new BoolValue(true))); 48 ApplyLinearScalingParameter.Hidden = true; 49 } 50 51 [StorableHook(HookType.AfterDeserialization)] 52 private void AfterDeserialization() { 53 if (!Parameters.ContainsKey(ApplyLinearScalingParameterName)) { 54 Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the individual should be linearly scaled before evaluating.", new BoolValue(false))); 55 ApplyLinearScalingParameter.Hidden = true; 56 } 57 } 58 59 [ThreadStatic] 60 private static double[] cache; 61 62 protected static void CalculateWithScaling(IEnumerable<double> targetValues, IEnumerable<double> estimatedValues, 63 double lowerEstimationLimit, double upperEstimationLimit, 64 IOnlineCalculator calculator, int maxRows) { 65 if (cache == null || cache.GetLength(0) < maxRows) { 66 cache = new double[maxRows]; 67 } 68 69 //calculate linear scaling 70 //the static methods of the calculator could not be used as it performs a check if the enumerators have an equal amount of elements 71 //this is not true if the cache is used 72 int i = 0; 73 var linearScalingCalculator = new OnlineLinearScalingParameterCalculator(); 74 var targetValuesEnumerator = targetValues.GetEnumerator(); 75 var estimatedValuesEnumerator = estimatedValues.GetEnumerator(); 76 while (targetValuesEnumerator.MoveNext() & estimatedValuesEnumerator.MoveNext()) { 77 double target = targetValuesEnumerator.Current; 78 double estimated = estimatedValuesEnumerator.Current; 79 cache[i] = estimated; 80 if (!double.IsNaN(estimated) && !double.IsInfinity(estimated)) 81 linearScalingCalculator.Add(estimated, target); 82 i++; 83 } 84 if (linearScalingCalculator.ErrorState == OnlineCalculatorError.None && (targetValuesEnumerator.MoveNext() || estimatedValuesEnumerator.MoveNext())) 85 throw new ArgumentException("Number of elements in target and estimated values enumeration do not match."); 86 87 double alpha = linearScalingCalculator.Alpha; 88 double beta = linearScalingCalculator.Beta; 89 if (linearScalingCalculator.ErrorState != OnlineCalculatorError.None) { 90 alpha = 0.0; 91 beta = 1.0; 92 } 93 94 //calculate the quality by using the passed online calculator 95 targetValuesEnumerator = targetValues.GetEnumerator(); 96 var scaledBoundedEstimatedValuesEnumerator = Enumerable.Range(0, i).Select(x => cache[x] * beta + alpha) 97 .LimitToRange(lowerEstimationLimit, upperEstimationLimit).GetEnumerator(); 98 99 while (targetValuesEnumerator.MoveNext() & scaledBoundedEstimatedValuesEnumerator.MoveNext()) { 100 calculator.Add(targetValuesEnumerator.Current, scaledBoundedEstimatedValuesEnumerator.Current); 101 } 102 } 36 protected SymbolicRegressionSingleObjectiveEvaluator(): base() {} 103 37 } 104 38 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator.cs
r8113 r8664 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 23 using HeuristicLab.Common; … … 47 46 IEnumerable<int> rows = GenerateRowsToEvaluate(); 48 47 49 double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScaling );48 double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value); 50 49 QualityParameter.ActualValue = new DoubleValue(quality); 51 50 … … 68 67 mse = OnlineMaxAbsoluteErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState); 69 68 } 70 if (errorState != OnlineCalculatorError.None) return Double.NaN;71 elsereturn mse;69 if (errorState != OnlineCalculatorError.None) return double.NaN; 70 return mse; 72 71 } 73 72 … … 75 74 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context; 76 75 EstimationLimitsParameter.ExecutionContext = context; 76 ApplyLinearScalingParameter.ExecutionContext = context; 77 77 78 double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScaling );78 double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value); 79 79 80 80 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null; 81 81 EstimationLimitsParameter.ExecutionContext = null; 82 ApplyLinearScalingParameter.ExecutionContext = null; 82 83 83 84 return mse; -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator.cs
r8634 r8664 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 23 using HeuristicLab.Common; … … 47 46 IEnumerable<int> rows = GenerateRowsToEvaluate(); 48 47 49 double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScaling );48 double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value); 50 49 QualityParameter.ActualValue = new DoubleValue(quality); 51 50 … … 68 67 mae = OnlineMeanAbsoluteErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState); 69 68 } 70 if (errorState != OnlineCalculatorError.None) return Double.NaN;71 elsereturn mae;69 if (errorState != OnlineCalculatorError.None) return double.NaN; 70 return mae; 72 71 } 73 72 … … 75 74 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context; 76 75 EstimationLimitsParameter.ExecutionContext = context; 76 ApplyLinearScalingParameter.ExecutionContext = context; 77 77 78 double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScaling );78 double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value); 79 79 80 80 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null; 81 81 EstimationLimitsParameter.ExecutionContext = null; 82 ApplyLinearScalingParameter.ExecutionContext = null; 82 83 83 84 return mse; -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.cs
r8113 r8664 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 23 using HeuristicLab.Common; … … 47 46 IEnumerable<int> rows = GenerateRowsToEvaluate(); 48 47 49 double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScaling );48 double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value); 50 49 QualityParameter.ActualValue = new DoubleValue(quality); 51 50 … … 68 67 mse = OnlineMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState); 69 68 } 70 if (errorState != OnlineCalculatorError.None) return Double.NaN;71 elsereturn mse;69 if (errorState != OnlineCalculatorError.None) return double.NaN; 70 return mse; 72 71 } 73 72 … … 75 74 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context; 76 75 EstimationLimitsParameter.ExecutionContext = context; 76 ApplyLinearScalingParameter.ExecutionContext = context; 77 77 78 double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScaling );78 double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value); 79 79 80 80 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null; 81 81 EstimationLimitsParameter.ExecutionContext = null; 82 ApplyLinearScalingParameter.ExecutionContext = null; 82 83 83 84 return mse; -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.cs
r7672 r8664 48 48 IEnumerable<int> rows = GenerateRowsToEvaluate(); 49 49 50 double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows );50 double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value); 51 51 QualityParameter.ActualValue = new DoubleValue(quality); 52 52 … … 54 54 } 55 55 56 public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows ) {56 public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling) { 57 57 IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows); 58 IEnumerable<double> originalValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);58 IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows); 59 59 OnlineCalculatorError errorState; 60 double r2 = OnlinePearsonsRSquaredCalculator.Calculate(estimatedValues, originalValues, out errorState); 61 if (errorState != OnlineCalculatorError.None) return 0.0; 62 else return r2; 60 61 double r2; 62 if (applyLinearScaling) { 63 var r2Calculator = new OnlinePearsonsRSquaredCalculator(); 64 CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, r2Calculator, problemData.Dataset.Rows); 65 errorState = r2Calculator.ErrorState; 66 r2 = r2Calculator.RSquared; 67 } else { 68 IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit); 69 r2 = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState); 70 } 71 if (errorState != OnlineCalculatorError.None) return double.NaN; 72 return r2; 63 73 } 64 74 … … 66 76 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context; 67 77 EstimationLimitsParameter.ExecutionContext = context; 78 ApplyLinearScalingParameter.ExecutionContext = context; 68 79 69 double r2 = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows );80 double r2 = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value); 70 81 71 82 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null; 72 83 EstimationLimitsParameter.ExecutionContext = null; 84 ApplyLinearScalingParameter.ExecutionContext = null; 73 85 74 86 return r2; -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveProblem.cs
r8175 r8664 61 61 EstimationLimitsParameter.Hidden = true; 62 62 63 64 ApplyLinearScalingParameter.Value.Value = true; 63 65 Maximization.Value = true; 64 66 MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth; -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveTrainingBestSolutionAnalyzer.cs
r7259 r8664 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Data;25 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 25 using HeuristicLab.Parameters; … … 38 37 private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter"; 39 38 private const string EstimationLimitsParameterName = "EstimationLimits"; 40 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";41 39 #region parameter properties 42 40 public ILookupParameter<IRegressionProblemData> ProblemDataParameter { … … 48 46 public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter { 49 47 get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; } 50 }51 public IValueParameter<BoolValue> ApplyLinearScalingParameter {52 get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }53 }54 #endregion55 56 #region properties57 public BoolValue ApplyLinearScaling {58 get { return ApplyLinearScalingParameter.Value; }59 48 } 60 49 #endregion … … 68 57 Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The symbolic data analysis tree interpreter for the symbolic expression tree.")); 69 58 Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic regression model.")); 70 Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true)));71 59 } 72 60 public override IDeepCloneable Clone(Cloner cloner) { … … 76 64 protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) { 77 65 var model = new SymbolicRegressionModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper); 78 if (ApplyLinearScaling.Value) 79 SymbolicRegressionModel.Scale(model, ProblemDataParameter.ActualValue); 66 if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicRegressionModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TargetVariable); 80 67 return new SymbolicRegressionSolution(model, (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone()); 81 68 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer.cs
r8169 r8664 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Data;25 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 using HeuristicLab.Parameters;27 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 26 … … 34 32 [StorableClass] 35 33 public sealed class SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer<IRegressionProblemData, ISymbolicRegressionSolution> { 36 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";37 #region parameter properties38 public IValueParameter<BoolValue> ApplyLinearScalingParameter {39 get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }40 }41 #endregion42 43 #region properties44 public BoolValue ApplyLinearScaling {45 get { return ApplyLinearScalingParameter.Value; }46 }47 #endregion48 34 49 35 [StorableConstructor] 50 36 private SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer(bool deserializing) : base(deserializing) { } 51 37 private SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer(SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { } 52 public SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer() 53 : base() { 54 Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true))); 55 } 38 public SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer() : base() { } 56 39 public override IDeepCloneable Clone(Cloner cloner) { 57 40 return new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer(this, cloner); … … 60 43 protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree) { 61 44 var model = new SymbolicRegressionModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper); 62 if (ApplyLinearScaling.Value) 63 SymbolicRegressionModel.Scale(model, ProblemDataParameter.ActualValue); 45 if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicRegressionModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TargetVariable); 64 46 return new SymbolicRegressionSolution(model, (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone()); 65 47 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveValidationBestSolutionAnalyzer.cs
r7259 r8664 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Data;25 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 25 using HeuristicLab.Parameters; … … 36 35 ISymbolicDataAnalysisBoundedOperator { 37 36 private const string EstimationLimitsParameterName = "EstimationLimits"; 38 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";39 37 40 38 #region parameter properties 41 39 public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter { 42 40 get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; } 43 }44 public IValueParameter<BoolValue> ApplyLinearScalingParameter {45 get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }46 }47 #endregion48 49 #region properties50 public BoolValue ApplyLinearScaling {51 get { return ApplyLinearScalingParameter.Value; }52 41 } 53 42 #endregion … … 59 48 : base() { 60 49 Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic regression model.")); 61 Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true)));62 50 } 63 51 … … 68 56 protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) { 69 57 var model = new SymbolicRegressionModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper); 70 if (ApplyLinearScaling.Value) 71 SymbolicRegressionModel.Scale(model, ProblemDataParameter.ActualValue); 58 if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicRegressionModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TargetVariable); 72 59 return new SymbolicRegressionSolution(model, (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone()); 73 60 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer.cs
r8169 r8664 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Data;25 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 using HeuristicLab.Parameters;27 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 26 … … 34 32 [StorableClass] 35 33 public sealed class SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer<ISymbolicRegressionSolution, ISymbolicRegressionSingleObjectiveEvaluator, IRegressionProblemData> { 36 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";37 #region parameter properties38 public IValueParameter<BoolValue> ApplyLinearScalingParameter {39 get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }40 }41 #endregion42 43 #region properties44 public BoolValue ApplyLinearScaling {45 get { return ApplyLinearScalingParameter.Value; }46 }47 #endregion48 49 34 [StorableConstructor] 50 35 private SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer(bool deserializing) : base(deserializing) { } 51 36 private SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer(SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { } 52 public SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer() 53 : base() { 54 Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true))); 55 } 37 public SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer() : base() { } 38 56 39 public override IDeepCloneable Clone(Cloner cloner) { 57 40 return new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer(this, cloner); … … 60 43 protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree) { 61 44 var model = new SymbolicRegressionModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper); 62 if (ApplyLinearScaling.Value) 63 SymbolicRegressionModel.Scale(model, ProblemDataParameter.ActualValue); 45 if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicRegressionModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TargetVariable); 64 46 return new SymbolicRegressionSolution(model, (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone()); 65 47 } -
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.