source: branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator.cs @ 18103

Last change on this file since 18103 was 18103, checked in by dpiringe, 6 months ago

#3136

  • refactor the evaluation logic of NMSESingleObjectiveConstraintsEvaluator
  • refactor the new method Evaluate for PearsonRSquaredAverageSimilarityEvaluator
  • change the parameter order of some evaluate/calculate methods
File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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
22using System.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HEAL.Attic;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
30  [Item("Mean squared error Evaluator", "Calculates the mean squared error of a symbolic regression solution.")]
31  [StorableType("8D4B5243-1635-46A6-AEF9-18C9BCB725DD")]
32  public class SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator : SymbolicRegressionSingleObjectiveEvaluator {
33    public override bool Maximization { get { return false; } }
34    [StorableConstructor]
35    protected SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator(StorableConstructorFlag _) : base(_) { }
36    protected SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator(SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator original, Cloner cloner)
37      : base(original, cloner) {
38    }
39    public override IDeepCloneable Clone(Cloner cloner) {
40      return new SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator(this, cloner);
41    }
42    public SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator() : base() { }
43
44    public override IOperation InstrumentedApply() {
45      var tree = SymbolicExpressionTreeParameter.ActualValue;
46      IEnumerable<int> rows = GenerateRowsToEvaluate();
47
48      double quality = Calculate(
49        tree, ProblemDataParameter.ActualValue,
50        rows, SymbolicDataAnalysisTreeInterpreterParameter.ActualValue,
51        ApplyLinearScalingParameter.ActualValue.Value,         
52        EstimationLimitsParameter.ActualValue.Lower,
53        EstimationLimitsParameter.ActualValue.Upper);
54      QualityParameter.ActualValue = new DoubleValue(quality);
55
56      return base.InstrumentedApply();
57    }
58
59    public static double Calculate(
60      ISymbolicExpressionTree tree,
61      IRegressionProblemData problemData,
62      IEnumerable<int> rows,
63      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
64      bool applyLinearScaling,
65      double lowerEstimationLimit, double upperEstimationLimit) {
66      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, rows);
67      IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
68      OnlineCalculatorError errorState;
69
70      double mse;
71      if (applyLinearScaling) {
72        var mseCalculator = new OnlineMeanSquaredErrorCalculator();
73        CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, mseCalculator, problemData.Dataset.Rows);
74        errorState = mseCalculator.ErrorState;
75        mse = mseCalculator.MeanSquaredError;
76      } else {
77        IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
78        mse = OnlineMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
79      }
80      if (errorState != OnlineCalculatorError.None) return double.NaN;
81      return mse;
82    }
83
84    public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows) {
85      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
86      EstimationLimitsParameter.ExecutionContext = context;
87      ApplyLinearScalingParameter.ExecutionContext = context;
88
89      double mse = Calculate(       
90        tree, problemData, rows,
91        SymbolicDataAnalysisTreeInterpreterParameter.ActualValue,
92        ApplyLinearScalingParameter.ActualValue.Value,
93        EstimationLimitsParameter.ActualValue.Lower,
94        EstimationLimitsParameter.ActualValue.Upper);
95
96      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
97      EstimationLimitsParameter.ExecutionContext = null;
98      ApplyLinearScalingParameter.ExecutionContext = null;
99
100      return mse;
101    }
102
103    public override double Evaluate(
104      ISymbolicExpressionTree tree,
105      IRegressionProblemData problemData,
106      IEnumerable<int> rows,
107      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
108      bool applyLinearScaling = true,
109      double lowerEstimationLimit = double.MinValue,
110      double upperEstimationLimit = double.MaxValue) {
111      return Calculate(tree, problemData, rows, interpreter, applyLinearScaling, lowerEstimationLimit, upperEstimationLimit);
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.