1 | using System.Collections.Generic;
|
---|
2 | using HeuristicLab.Common;
|
---|
3 | using HeuristicLab.Core;
|
---|
4 | using HeuristicLab.Data;
|
---|
5 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 |
|
---|
8 | namespace 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);
|
---|
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 | 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 };
|
---|
40 |
|
---|
41 | }
|
---|
42 |
|
---|
43 | public override double[] Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IClassificationProblemData problemData, IEnumerable<int> rows) {
|
---|
44 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
|
---|
45 | EstimationLimitsParameter.ExecutionContext = context;
|
---|
46 |
|
---|
47 | double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows);
|
---|
48 |
|
---|
49 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
|
---|
50 | EstimationLimitsParameter.ExecutionContext = null;
|
---|
51 |
|
---|
52 | return quality;
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|