[10072] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[10073] | 28 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 29 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
[10072] | 30 |
|
---|
[10073] | 31 | namespace HeuristicLab.Problems.GrammaticalEvolution {
|
---|
[10072] | 32 | [Item("Pearson R² Evaluator", "Calculates the square of the pearson correlation coefficient (also known as coefficient of determination) of a symbolic regression solution.")]
|
---|
| 33 | [StorableClass]
|
---|
[10073] | 34 | public class GESymbolicRegressionSingleObjectivePearsonRSquaredEvaluator : GESymbolicRegressionSingleObjectiveEvaluator {
|
---|
[10072] | 35 | [StorableConstructor]
|
---|
[10073] | 36 | protected GESymbolicRegressionSingleObjectivePearsonRSquaredEvaluator(bool deserializing) : base(deserializing) { }
|
---|
| 37 | protected GESymbolicRegressionSingleObjectivePearsonRSquaredEvaluator(GESymbolicRegressionSingleObjectivePearsonRSquaredEvaluator original, Cloner cloner)
|
---|
[10072] | 38 | : base(original, cloner) {
|
---|
| 39 | }
|
---|
| 40 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[10073] | 41 | return new GESymbolicRegressionSingleObjectivePearsonRSquaredEvaluator(this, cloner);
|
---|
[10072] | 42 | }
|
---|
| 43 |
|
---|
[10073] | 44 | public GESymbolicRegressionSingleObjectivePearsonRSquaredEvaluator() : base() { }
|
---|
[10072] | 45 |
|
---|
| 46 | public override bool Maximization { get { return true; } }
|
---|
| 47 |
|
---|
| 48 | public override IOperation Apply() {
|
---|
[10073] | 49 | var solution = GenotypeToPhenotypeMapperParameter.ActualValue.Map(
|
---|
| 50 | SymbolicExpressionTreeGrammarParameter.ActualValue,
|
---|
| 51 | IntegerVectorParameter.ActualValue
|
---|
| 52 | );
|
---|
| 53 | SymbolicExpressionTreeParameter.ActualValue = solution;
|
---|
[10072] | 54 | IEnumerable<int> rows = GenerateRowsToEvaluate();
|
---|
| 55 |
|
---|
[10073] | 56 | double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue,
|
---|
| 57 | solution, EstimationLimitsParameter.ActualValue.Lower,
|
---|
| 58 | EstimationLimitsParameter.ActualValue.Upper,
|
---|
| 59 | ProblemDataParameter.ActualValue, rows,
|
---|
| 60 | ApplyLinearScalingParameter.ActualValue.Value);
|
---|
[10072] | 61 | QualityParameter.ActualValue = new DoubleValue(quality);
|
---|
| 62 |
|
---|
| 63 | return base.Apply();
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[10073] | 66 | public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
|
---|
| 67 | ISymbolicExpressionTree solution,
|
---|
| 68 | double lowerEstimationLimit, double upperEstimationLimit,
|
---|
| 69 | IRegressionProblemData problemData,
|
---|
| 70 | IEnumerable<int> rows, bool applyLinearScaling) {
|
---|
[10072] | 71 | IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
|
---|
| 72 | IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
|
---|
| 73 | OnlineCalculatorError errorState;
|
---|
| 74 |
|
---|
| 75 | double r2;
|
---|
| 76 | if (applyLinearScaling) {
|
---|
| 77 | var r2Calculator = new OnlinePearsonsRSquaredCalculator();
|
---|
| 78 | CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, r2Calculator, problemData.Dataset.Rows);
|
---|
| 79 | errorState = r2Calculator.ErrorState;
|
---|
| 80 | r2 = r2Calculator.RSquared;
|
---|
| 81 | } else {
|
---|
| 82 | IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
|
---|
| 83 | r2 = OnlinePearsonsRSquaredCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
|
---|
| 84 | }
|
---|
| 85 | if (errorState != OnlineCalculatorError.None) return double.NaN;
|
---|
| 86 | return r2;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[10073] | 89 | public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree,
|
---|
| 90 | IRegressionProblemData problemData, IEnumerable<int> rows) {
|
---|
[10072] | 91 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
|
---|
| 92 | EstimationLimitsParameter.ExecutionContext = context;
|
---|
| 93 | ApplyLinearScalingParameter.ExecutionContext = context;
|
---|
| 94 |
|
---|
[10073] | 95 | double r2 = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue,
|
---|
| 96 | tree, EstimationLimitsParameter.ActualValue.Lower,
|
---|
| 97 | EstimationLimitsParameter.ActualValue.Upper,
|
---|
| 98 | problemData, rows,
|
---|
| 99 | ApplyLinearScalingParameter.ActualValue.Value);
|
---|
[10072] | 100 |
|
---|
| 101 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
|
---|
| 102 | EstimationLimitsParameter.ExecutionContext = null;
|
---|
| 103 | ApplyLinearScalingParameter.ExecutionContext = null;
|
---|
| 104 |
|
---|
| 105 | return r2;
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | }
|
---|