Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator.cs @ 5759

Last change on this file since 5759 was 5759, checked in by mkommend, 13 years ago

#1418:

  • Worked on IntRange and DoubleRange
  • Updated evaluators, analyzers, problems and problem data to use IntRanges
  • Removed properties to access the value of LookupParameter
  • Corrected files.txt
File size: 3.5 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, LowerEstimationLimitParameter.ActualValue.Value, UpperEstimationLimitParameter.ActualValue.Value, 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      LowerEstimationLimitParameter.ExecutionContext = context;
49      UpperEstimationLimitParameter.ExecutionContext = context;
50
51      double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, LowerEstimationLimitParameter.ActualValue.Value, UpperEstimationLimitParameter.ActualValue.Value, problemData, rows);
52
53      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
54      LowerEstimationLimitParameter.ExecutionContext = null;
55      UpperEstimationLimitParameter.ExecutionContext = null;
56
57      return quality;
58    }
59  }
60}
Note: See TracBrowser for help on using the repository browser.