Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/17/12 11:18:40 (12 years ago)
Author:
mkommend
Message:

#1951:

  • Added linear scaling parameter to data analysis problems.
  • Adapted interfaces, evaluators and analyzers accordingly.
  • Added OnlineBoundedMeanSquaredErrorCalculator.
  • Adapted symbolic regression sample unit test.
Location:
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectiveMeanSquaredErrorTreeSizeEvaluator.cs

    r7259 r8664  
    6565      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
    6666      EstimationLimitsParameter.ExecutionContext = context;
     67      ApplyLinearScalingParameter.ExecutionContext = context;
    6768
    6869      double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows);
     
    7071      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
    7172      EstimationLimitsParameter.ExecutionContext = null;
     73      ApplyLinearScalingParameter.ExecutionContext = null;
    7274
    7375      return quality;
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator.cs

    r6740 r8664  
    2626      IEnumerable<int> rows = GenerateRowsToEvaluate();
    2727      var solution = SymbolicExpressionTreeParameter.ActualValue;
    28       double[] qualities = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows);
     28      double[] qualities = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value);
    2929      QualitiesParameter.ActualValue = new DoubleArray(qualities);
    3030      return base.Apply();
    3131    }
    3232
    33     public static double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IClassificationProblemData problemData, IEnumerable<int> rows) {
     33    public static double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IClassificationProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling) {
    3434      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
    35       IEnumerable<double> originalValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
     35      IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
    3636      OnlineCalculatorError errorState;
    37       double r2 = OnlinePearsonsRSquaredCalculator.Calculate(estimatedValues, originalValues, out errorState);
    38       if (errorState != OnlineCalculatorError.None) r2 = 0.0;
    39       return new double[] { r2, solution.Length };
     37
     38      double r2;
     39      if (applyLinearScaling) {
     40        var r2Calculator = new OnlinePearsonsRSquaredCalculator();
     41        CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, r2Calculator, problemData.Dataset.Rows);
     42        errorState = r2Calculator.ErrorState;
     43        r2 = r2Calculator.RSquared;
     44      } else {
     45        IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
     46        r2 = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
     47      }
     48
     49      if (errorState != OnlineCalculatorError.None) r2 = double.NaN;
     50      return new double[2] { r2, solution.Length };
    4051
    4152    }
     
    4556      EstimationLimitsParameter.ExecutionContext = context;
    4657
    47       double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows);
     58      double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value);
    4859
    4960      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectiveProblem.cs

    r8594 r8664  
    6868      Parameters.Add(new ValueParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "", new AccuracyMaximizingThresholdsModelCreator()));
    6969
     70      ApplyLinearScalingParameter.Value.Value = false;
    7071      EstimationLimitsParameter.Hidden = true;
    7172
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectiveTrainingBestSolutionAnalyzer.cs

    r8594 r8664  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Data;
    2524using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2625using HeuristicLab.Parameters;
     
    3938    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter";
    4039    private const string EstimationLimitsParameterName = "EstimationLimits";
    41     private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
    4240
    4341    #region parameter properties
     
    5755      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
    5856    }
    59     public IValueParameter<BoolValue> ApplyLinearScalingParameter {
    60       get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
    61     }
    62     #endregion
    63 
    64     #region properties
    65     public BoolValue ApplyLinearScaling {
    66       get { return ApplyLinearScalingParameter.Value; }
    67     }
    6857    #endregion
    6958
     
    7766      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The symbolic data analysis tree interpreter for the symbolic expression tree."));
    7867      Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic classification model."));
    79       Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic classification solution should be linearly scaled.", new BoolValue(false)));
    8068    }
    8169    public override IDeepCloneable Clone(Cloner cloner) {
     
    9179    protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality) {
    9280      var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
    93       if (ApplyLinearScaling.Value) SymbolicClassificationModel.Scale(model, ProblemDataParameter.ActualValue);
     81      if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicClassificationModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TargetVariable);
    9482
    9583      model.RecalculateModelParameters(ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TrainingIndices);
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectiveValidationBestSolutionAnalyzer.cs

    r8594 r8664  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Data;
    2524using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2625using HeuristicLab.Parameters;
     
    3736    private const string ModelCreatorParameterName = "ModelCreator";
    3837    private const string EstimationLimitsParameterName = "EstimationLimits";
    39     private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
    4038
    4139    #region parameter properties
    4240    public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
    4341      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
    44     }
    45     public IValueParameter<BoolValue> ApplyLinearScalingParameter {
    46       get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
    4742    }
    4843    public IValueLookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
     
    5449    #endregion
    5550
    56     #region properties
    57     public BoolValue ApplyLinearScaling {
    58       get { return ApplyLinearScalingParameter.Value; }
    59     }
    60     #endregion
    6151    [StorableConstructor]
    6252    private SymbolicClassificationMultiObjectiveValidationBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
     
    6555      : base() {
    6656      Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The loewr and upper limit for the estimated values produced by the symbolic classification model."));
    67       Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic classification solution should be linearly scaled.", new BoolValue(false)));
    6857      Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, ""));
    6958    }
     
    7261    }
    7362
    74     [StorableHook(HookType.AfterDeserialization)]
    75     private void AfterDeserialization() {
    76       if (!Parameters.ContainsKey(ModelCreatorParameterName))
    77         Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, ""));
    78     }
    7963
    8064    protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQualities) {
    8165      var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
    82       if (ApplyLinearScaling.Value) SymbolicClassificationModel.Scale(model, ProblemDataParameter.ActualValue);
     66      if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicClassificationModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TargetVariable);
    8367
    8468      model.RecalculateModelParameters(ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TrainingIndices);
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveBoundedMeanSquaredErrorEvaluator.cs

    r7259 r8664  
    4747      IEnumerable<int> rows = GenerateRowsToEvaluate();
    4848      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);
    5050      QualityParameter.ActualValue = new DoubleValue(quality);
    5151      return base.Apply();
    5252    }
    5353
    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) {
    5555      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;
    5858
    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();
    6161
    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);
    7871      }
    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;
    8674    }
    8775
     
    8977      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
    9078      EstimationLimitsParameter.ExecutionContext = context;
     79      ApplyLinearScalingParameter.ExecutionContext = context;
    9180
    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);
    9382
    9483      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
    9584      EstimationLimitsParameter.ExecutionContext = null;
     85      ApplyLinearScalingParameter.ExecutionContext = null;
    9686
    9787      return mse;
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveMeanSquaredErrorEvaluator.cs

    r7259 r8664  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using HeuristicLab.Common;
     
    4748      IEnumerable<int> rows = GenerateRowsToEvaluate();
    4849      var solution = SymbolicExpressionTreeParameter.ActualValue;
    49       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);
    5051      QualityParameter.ActualValue = new DoubleValue(quality);
    5152      return base.Apply();
    5253    }
    5354
    54     public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IClassificationProblemData problemData, IEnumerable<int> rows) {
     55    public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IClassificationProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling) {
    5556      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);
     57      IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
    5858      OnlineCalculatorError errorState;
    59       double mse = OnlineMeanSquaredErrorCalculator.Calculate(originalValues, boundedEstimationValues, out errorState);
    60       if (errorState != OnlineCalculatorError.None) return double.NaN;
    61       else return mse;
     59
     60      double mse;
     61      if (applyLinearScaling) {
     62        var mseCalculator = new OnlineMeanSquaredErrorCalculator();
     63        CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, mseCalculator, problemData.Dataset.Rows);
     64        errorState = mseCalculator.ErrorState;
     65        mse = mseCalculator.MeanSquaredError;
     66      } else {
     67        IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
     68        mse = OnlineMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
     69      }
     70      if (errorState != OnlineCalculatorError.None) return Double.NaN;
     71      return mse;
    6272    }
    6373
     
    6575      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
    6676      EstimationLimitsParameter.ExecutionContext = context;
     77      ApplyLinearScalingParameter.ExecutionContext = context;
    6778
    68       double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows);
     79      double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value);
    6980
    7081      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
    7182      EstimationLimitsParameter.ExecutionContext = null;
     83      ApplyLinearScalingParameter.ExecutionContext = null;
    7284
    7385      return mse;
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectivePearsonRSquaredEvaluator.cs

    r8646 r8664  
    4747      IEnumerable<int> rows = GenerateRowsToEvaluate();
    4848      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);
    5050      QualityParameter.ActualValue = new DoubleValue(quality);
    5151      return base.Apply();
    5252    }
    5353
    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) {
    5555      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);
    5857      OnlineCalculatorError errorState;
    59       double r2 = OnlinePearsonsRSquaredCalculator.Calculate(boundedEstimationValues, originalValues, out errorState);
    60       if (errorState != OnlineCalculatorError.None) return 0.0;
    61       else return r2;
     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      if (errorState != OnlineCalculatorError.None) return double.NaN;
     70      return r2;
    6271    }
    6372
     
    6574      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
    6675      EstimationLimitsParameter.ExecutionContext = context;
     76      ApplyLinearScalingParameter.ExecutionContext = context;
    6777
    68       double r2 = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows);
     78      double r2 = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value);
    6979
    7080      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
    7181      EstimationLimitsParameter.ExecutionContext = null;
     82      ApplyLinearScalingParameter.ExecutionContext = null;
    7283
    7384      return r2;
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectivePenaltyScoreEvaluator.cs

    r8594 r8664  
    8686      EstimationLimitsParameter.ExecutionContext = context;
    8787      ModelCreatorParameter.ExecutionContext = context;
     88      ApplyLinearScalingParameter.ExecutionContext = context;
    8889
    8990      var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel(tree, SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
     91      if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicClassificationModel.Scale(model, problemData, problemData.TargetVariable);
    9092      model.RecalculateModelParameters(problemData, rows);
    9193      double penalty = Calculate(model, problemData, rows);
     
    9496      EstimationLimitsParameter.ExecutionContext = null;
    9597      ModelCreatorParameter.ExecutionContext = null;
     98      ApplyLinearScalingParameter.ExecutionContext = null;
    9699
    97100      return penalty;
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveProblem.cs

    r8594 r8664  
    6666      Parameters.Add(new ValueParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "", new AccuracyMaximizingThresholdsModelCreator()));
    6767
     68      ApplyLinearScalingParameter.Value.Value = false;
    6869      EstimationLimitsParameter.Hidden = true;
    6970
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveTrainingBestSolutionAnalyzer.cs

    r8594 r8664  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Data;
    2524using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2625using HeuristicLab.Parameters;
     
    3938    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter";
    4039    private const string EstimationLimitsParameterName = "UpperEstimationLimit";
    41     private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
    4240    #region parameter properties
    4341    public ILookupParameter<IClassificationProblemData> ProblemDataParameter {
     
    5654      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
    5755    }
    58     public IValueParameter<BoolValue> ApplyLinearScalingParameter {
    59       get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
    60     }
    6156    #endregion
    62     #region properties
    63     public BoolValue ApplyLinearScaling {
    64       get { return ApplyLinearScalingParameter.Value; }
    65     }
    66     #endregion
     57
    6758
    6859    [StorableConstructor]
     
    7566      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The symbolic data analysis tree interpreter for the symbolic expression tree."));
    7667      Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic classification model."));
    77       Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic classification solution should be linearly scaled.", new BoolValue(false)));
    7868    }
    7969
     
    8979    protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) {
    9080      var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
    91       if (ApplyLinearScaling.Value) SymbolicClassificationModel.Scale(model, ProblemDataParameter.ActualValue);
     81      if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicClassificationModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TargetVariable);
    9282
    9383      model.RecalculateModelParameters(ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TrainingIndices);
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer.cs

    r8594 r8664  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Data;
    2524using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2625using HeuristicLab.Parameters;
     
    3433  [StorableClass]
    3534  public sealed class SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer<IClassificationProblemData, ISymbolicClassificationSolution>, ISymbolicClassificationModelCreatorOperator {
    36     private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
    3735    private const string ModelCreatorParameterName = "ModelCreator";
    3836    #region parameter properties
    39     public IValueParameter<BoolValue> ApplyLinearScalingParameter {
    40       get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
    41     }
    4237    public IValueLookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
    4338      get { return (IValueLookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
     
    4843    #endregion
    4944
    50     #region properties
    51     public BoolValue ApplyLinearScaling {
    52       get { return ApplyLinearScalingParameter.Value; }
    53     }
    54     #endregion
    55 
    5645    [StorableConstructor]
    5746    private SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
     
    5948    public SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer()
    6049      : base() {
    61       Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic classification solution should be linearly scaled.", new BoolValue(false)));
    6250      Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, ""));
    6351    }
     
    7462    protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree) {
    7563      var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
    76       if (ApplyLinearScaling.Value) SymbolicClassificationModel.Scale(model, ProblemDataParameter.ActualValue);
     64      if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicClassificationModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TargetVariable);
    7765
    7866      model.RecalculateModelParameters(ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TrainingIndices);
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveValidationBestSolutionAnalyzer.cs

    r8594 r8664  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Data;
    2524using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2625using HeuristicLab.Parameters;
     
    3635  ISymbolicDataAnalysisBoundedOperator, ISymbolicClassificationModelCreatorOperator {
    3736    private const string EstimationLimitsParameterName = "EstimationLimits";
    38     private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
    3937    private const string ModelCreatorParameterName = "ModelCreator";
    4038
     
    4240    public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
    4341      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
    44     }
    45     public IValueParameter<BoolValue> ApplyLinearScalingParameter {
    46       get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
    4742    }
    4843    public IValueLookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
     
    5449    #endregion
    5550
    56     #region properties
    57     public BoolValue ApplyLinearScaling {
    58       get { return ApplyLinearScalingParameter.Value; }
    59     }
    60     #endregion
    6151    [StorableConstructor]
    6252    private SymbolicClassificationSingleObjectiveValidationBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
     
    6555      : base() {
    6656      Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic classification model."));
    67       Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic classification solution should be linearly scaled.", new BoolValue(false)));
    6857      Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, ""));
    6958    }
     
    8069    protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) {
    8170      var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
    82       if (ApplyLinearScaling.Value) SymbolicClassificationModel.Scale(model, ProblemDataParameter.ActualValue);
     71      if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicClassificationModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TargetVariable);
    8372
    8473      model.RecalculateModelParameters(ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TrainingIndices);
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer.cs

    r8594 r8664  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Data;
    2524using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2625using HeuristicLab.Parameters;
     
    3433  [StorableClass]
    3534  public sealed class SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer<ISymbolicClassificationSolution, ISymbolicClassificationSingleObjectiveEvaluator, IClassificationProblemData>, ISymbolicClassificationModelCreatorOperator {
    36     private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
    3735    private const string ModelCreatorParameterName = "ModelCreator";
    3836    #region parameter properties
    39     public IValueParameter<BoolValue> ApplyLinearScalingParameter {
    40       get { return (IValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
    41     }
    4237    public IValueLookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
    4338      get { return (IValueLookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
     
    4843    #endregion
    4944
    50     #region properties
    51     public BoolValue ApplyLinearScaling {
    52       get { return ApplyLinearScalingParameter.Value; }
    53     }
    54     #endregion
    55 
    5645    [StorableConstructor]
    5746    private SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
     
    5948    public SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer()
    6049      : base() {
    61       Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic classification solution should be linearly scaled.", new BoolValue(false)));
    6250      Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, ""));
    6351    }
     
    7462    protected override ISymbolicClassificationSolution CreateSolution(ISymbolicExpressionTree bestTree) {
    7563      var model = ModelCreatorParameter.ActualValue.CreateSymbolicClassificationModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
    76       if (ApplyLinearScaling.Value) SymbolicClassificationModel.Scale(model, ProblemDataParameter.ActualValue);
     64      if (ApplyLinearScalingParameter.ActualValue.Value) SymbolicClassificationModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TargetVariable);
    7765
    7866      model.RecalculateModelParameters(ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TrainingIndices);
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicClassificationModel.cs

    r8623 r8664  
    6262      return CreateClassificationSolution(problemData);
    6363    }
    64 
    65     #region scaling
    66     public static void Scale(ISymbolicClassificationModel model, IClassificationProblemData problemData) {
    67       var dataset = problemData.Dataset;
    68       var targetVariable = problemData.TargetVariable;
    69       var rows = problemData.TrainingIndices;
    70       var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows);
    71       var targetValues = dataset.GetDoubleValues(targetVariable, rows);
    72       double alpha;
    73       double beta;
    74       OnlineCalculatorError errorState;
    75       OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta, out errorState);
    76       if (errorState != OnlineCalculatorError.None) return;
    77 
    78       ConstantTreeNode alphaTreeNode = null;
    79       ConstantTreeNode betaTreeNode = null;
    80       // check if model has been scaled previously by analyzing the structure of the tree
    81       var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0);
    82       if (startNode.GetSubtree(0).Symbol is Addition) {
    83         var addNode = startNode.GetSubtree(0);
    84         if (addNode.SubtreeCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) {
    85           alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode;
    86           var mulNode = addNode.GetSubtree(0);
    87           if (mulNode.SubtreeCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) {
    88             betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode;
    89           }
    90         }
    91       }
    92       // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
    93       if (alphaTreeNode != null && betaTreeNode != null) {
    94         betaTreeNode.Value *= beta;
    95         alphaTreeNode.Value *= beta;
    96         alphaTreeNode.Value += alpha;
    97       } else {
    98         var mainBranch = startNode.GetSubtree(0);
    99         startNode.RemoveSubtree(0);
    100         var scaledMainBranch = MakeSum(MakeProduct(mainBranch, beta), alpha);
    101         startNode.AddSubtree(scaledMainBranch);
    102       }
    103     }
    104 
    105     private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
    106       if (alpha.IsAlmost(0.0)) {
    107         return treeNode;
    108       } else {
    109         var addition = new Addition();
    110         var node = addition.CreateTreeNode();
    111         var alphaConst = MakeConstant(alpha);
    112         node.AddSubtree(treeNode);
    113         node.AddSubtree(alphaConst);
    114         return node;
    115       }
    116     }
    117 
    118     private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode treeNode, double beta) {
    119       if (beta.IsAlmost(1.0)) {
    120         return treeNode;
    121       } else {
    122         var multipliciation = new Multiplication();
    123         var node = multipliciation.CreateTreeNode();
    124         var betaConst = MakeConstant(beta);
    125         node.AddSubtree(treeNode);
    126         node.AddSubtree(betaConst);
    127         return node;
    128       }
    129     }
    130 
    131     private static ISymbolicExpressionTreeNode MakeConstant(double c) {
    132       var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
    133       node.Value = c;
    134       return node;
    135     }
    136     #endregion
    13764  }
    13865}
Note: See TracChangeset for help on using the changeset viewer.