Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TreeSimplifier/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator.cs @ 8915

Last change on this file since 8915 was 8915, checked in by mkommend, 11 years ago

#1763: merged changes from trunk into the tree simplifier branch.

File size: 3.9 KB
Line 
1using System.Collections.Generic;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
5using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7
8namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
9  [Item("Pearson R² & Tree size Evaluator", "Calculates the Pearson R² and the tree size of a symbolic classification solution.")]
10  [StorableClass]
11  public class SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator : SymbolicClassificationMultiObjectiveEvaluator {
12    [StorableConstructor]
13    protected SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator(bool deserializing) : base(deserializing) { }
14    protected SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator(SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator original, Cloner cloner)
15      : base(original, cloner) {
16    }
17    public override IDeepCloneable Clone(Cloner cloner) {
18      return new SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator(this, cloner);
19    }
20
21    public SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator() : base() { }
22
23    public override IEnumerable<bool> Maximization { get { return new bool[2] { true, false }; } }
24
25    public override IOperation Apply() {
26      IEnumerable<int> rows = GenerateRowsToEvaluate();
27      var solution = SymbolicExpressionTreeParameter.ActualValue;
28      double[] qualities = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value);
29      QualitiesParameter.ActualValue = new DoubleArray(qualities);
30      return base.Apply();
31    }
32
33    public static double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IClassificationProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling) {
34      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
35      IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
36      OnlineCalculatorError errorState;
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 };
51
52    }
53
54    public override double[] Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IClassificationProblemData problemData, IEnumerable<int> rows) {
55      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
56      EstimationLimitsParameter.ExecutionContext = context;
57
58      double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value);
59
60      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
61      EstimationLimitsParameter.ExecutionContext = null;
62
63      return quality;
64    }
65  }
66}
Note: See TracBrowser for help on using the repository browser.