Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis SolutionEnsembles/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator.cs @ 6089

Last change on this file since 6089 was 5809, checked in by mkommend, 14 years ago

#1418: Reintegrated branch into trunk.

File size: 3.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8
9namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
10  [Item("Pearson R² & Tree size Evaluator", "Calculates the Pearson R² and the tree size of a symbolic classification solution.")]
11  [StorableClass]
12  public class SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator : SymbolicClassificationMultiObjectiveEvaluator {
13    [StorableConstructor]
14    protected SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator(bool deserializing) : base(deserializing) { }
15    protected SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator(SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator original, Cloner cloner)
16      : base(original, cloner) {
17    }
18    public override IDeepCloneable Clone(Cloner cloner) {
19      return new SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator(this, cloner);
20    }
21
22    public SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator() : base() { }
23
24    public override IEnumerable<bool> Maximization { get { return new bool[2] { true, false }; } }
25
26    public override IOperation Apply() {
27      IEnumerable<int> rows = GenerateRowsToEvaluate();
28      double[] qualities = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, SymbolicExpressionTreeParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows);
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) {
34      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
35      IEnumerable<double> originalValues = problemData.Dataset.GetEnumeratedVariableValues(problemData.TargetVariable, rows);
36      try {
37        double r2 = OnlinePearsonsRSquaredEvaluator.Calculate(originalValues, estimatedValues);
38        return new double[2] { r2, solution.Length };
39      }
40      catch (ArgumentException) {
41        // if R² cannot be calcualted because of infinity or NaN values => return worst possible fitness value
42        return new double[2] { 0.0, solution.Length };
43      }
44    }
45
46    public override double[] Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IClassificationProblemData problemData, IEnumerable<int> rows) {
47      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
48      EstimationLimitsParameter.ExecutionContext = context;
49
50      double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows);
51
52      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
53      EstimationLimitsParameter.ExecutionContext = null;
54
55      return quality;
56    }
57  }
58}
Note: See TracBrowser for help on using the repository browser.