Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3076_IA_evaluators_analyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveConstraintPearsonRSquaredEvaluator.cs @ 17632

Last change on this file since 17632 was 17632, checked in by chaider, 4 years ago

#3076 Changed readonly dictionary to normal dicitionary to set new interval in VariableRanges after splitting the intervals

File size: 4.3 KB
Line 
1using System.Collections.Generic;
2using HEAL.Attic;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
7
8namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
9  [Item("Pearson R² Constraint Evaluator",
10    "Calculates the square of the pearson correlation coefficient (also known as coefficient of determination) of a symbolic regression solution.")]
11  [StorableType("D61462E4-2032-4790-B63D-5E6512987F64")]
12  public class SymbolicRegressionSingleObjectiveConstraintPearsonRSquaredEvaluator : SymbolicRegressionSingleObjectiveEvaluator {
13    [StorableConstructor]
14    protected SymbolicRegressionSingleObjectiveConstraintPearsonRSquaredEvaluator(StorableConstructorFlag _) :
15      base(_) { }
16
17    protected SymbolicRegressionSingleObjectiveConstraintPearsonRSquaredEvaluator(
18      SymbolicRegressionSingleObjectiveConstraintPearsonRSquaredEvaluator original, Cloner cloner)
19      : base(original, cloner) { }
20
21    public override IDeepCloneable Clone(Cloner cloner) {
22      return new SymbolicRegressionSingleObjectiveConstraintPearsonRSquaredEvaluator(this, cloner);
23    }
24
25    public SymbolicRegressionSingleObjectiveConstraintPearsonRSquaredEvaluator() { }
26
27    public override bool Maximization => true;
28
29    public override IOperation InstrumentedApply() {
30      var solution = SymbolicExpressionTreeParameter.ActualValue;
31      var rows     = GenerateRowsToEvaluate();
32
33      var quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution,
34                              EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper,
35                              ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value);
36      QualityParameter.ActualValue = new DoubleValue(quality);
37      return base.InstrumentedApply();
38    }
39
40    public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
41                                   ISymbolicExpressionTree solution, double lowerEstimationLimit,
42                                   double upperEstimationLimit, IRegressionProblemData problemData,
43                                   IEnumerable<int> rows, bool applyLinearScaling) {
44      var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
45      var targetValues    = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
46      var errorState      = OnlineCalculatorError.None;
47
48      var constraints    = problemData.IntervalConstraints.EnabledConstraints;
49      var variableRanges = problemData.VariableRanges.GetDictionary();
50      var tree           = solution;
51
52      if (!SymbolicRegressionConstraintAnalyzer.ConstraintsSatisfied(constraints, variableRanges, tree)) return 0;
53
54      var r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, solution,
55                                                                                   lowerEstimationLimit,
56                                                                                   upperEstimationLimit, problemData,
57                                                                                   rows, applyLinearScaling);
58
59      return r2;
60    }
61
62
63    public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree,
64                                    IRegressionProblemData problemData, IEnumerable<int> rows) {
65      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
66      EstimationLimitsParameter.ExecutionContext                    = context;
67      ApplyLinearScalingParameter.ExecutionContext                  = context;
68
69      var r2 = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree,
70                         EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper,
71                         problemData, rows,
72                         ApplyLinearScalingParameter.ActualValue.Value);
73
74      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
75      EstimationLimitsParameter.ExecutionContext                    = null;
76      ApplyLinearScalingParameter.ExecutionContext                  = null;
77
78      return r2;
79    }
80  }
81}
Note: See TracBrowser for help on using the repository browser.