[17621] | 1 | using System.Collections.Generic;
|
---|
| 2 | using HEAL.Attic;
|
---|
| 3 | using HeuristicLab.Common;
|
---|
| 4 | using HeuristicLab.Core;
|
---|
| 5 | using HeuristicLab.Data;
|
---|
| 6 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 7 |
|
---|
| 8 | namespace 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.GetReadonlyDictionary();
|
---|
| 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 | } |
---|