1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
|
---|
11 | [Item("Pearson R² & Tree size Evaluator", "Calculates the Pearson R² and the tree size of a symbolic classification solution.")]
|
---|
12 | [StorableClass]
|
---|
13 | public class SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator : SymbolicClassificationMultiObjectiveEvaluator {
|
---|
14 | [StorableConstructor]
|
---|
15 | protected SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator(bool deserializing) : base(deserializing) { }
|
---|
16 | protected SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator(SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator original, Cloner cloner)
|
---|
17 | : base(original, cloner) {
|
---|
18 | }
|
---|
19 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
20 | return new SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator(this, cloner);
|
---|
21 | }
|
---|
22 |
|
---|
23 | public SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator() : base() { }
|
---|
24 |
|
---|
25 | public override IEnumerable<bool> Maximization { get { return new bool[2] { true, false }; } }
|
---|
26 |
|
---|
27 | public override IOperation Apply() {
|
---|
28 | IEnumerable<int> rows = GenerateRowsToEvaluate();
|
---|
29 | var solution = SymbolicExpressionTreeParameter.ActualValue;
|
---|
30 | double[] qualities = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows);
|
---|
31 | QualitiesParameter.ActualValue = new DoubleArray(qualities);
|
---|
32 | AddEvaluatedNodes(solution.Length * rows.Count());
|
---|
33 | return base.Apply();
|
---|
34 | }
|
---|
35 |
|
---|
36 | public static double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IClassificationProblemData problemData, IEnumerable<int> rows) {
|
---|
37 | IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
|
---|
38 | IEnumerable<double> originalValues = problemData.Dataset.GetEnumeratedVariableValues(problemData.TargetVariable, rows);
|
---|
39 | double r2 = OnlinePearsonsRSquaredEvaluator.Calculate(estimatedValues, originalValues);
|
---|
40 | return new double[] { double.IsNaN(r2) ? 0.0 : r2, solution.Length };
|
---|
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 | EvaluatedNodesParameter.ExecutionContext = context;
|
---|
47 |
|
---|
48 | double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows);
|
---|
49 | AddEvaluatedNodes(tree.Length * rows.Count());
|
---|
50 |
|
---|
51 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
|
---|
52 | EstimationLimitsParameter.ExecutionContext = null;
|
---|
53 | EvaluatedNodesParameter.ExecutionContext = null;
|
---|
54 |
|
---|
55 | return quality;
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|