1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using HeuristicLab.Common;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
7 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
8 |
|
---|
9 | namespace 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 | }
|
---|