Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 18103 was 18103, checked in by dpiringe, 2 years 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 absolute error Evaluator", "Calculates the mean absolute error of a symbolic regression solution.")]
31  [StorableType("8ABB1AC3-0ACE-43AB-8E6B-B0FC925429DC")]
32  public class SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator : SymbolicRegressionSingleObjectiveEvaluator {
33    public override bool Maximization { get { return false; } }
34    [StorableConstructor]
35    protected SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator(StorableConstructorFlag _) : base(_) { }
36    protected SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator(SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator original, Cloner cloner)
37      : base(original, cloner) {
38    }
39    public override IDeepCloneable Clone(Cloner cloner) {
40      return new SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator(this, cloner);
41    }
42    public SymbolicRegressionSingleObjectiveMeanAbsoluteErrorEvaluator() : base() { }
43
44    public override IOperation InstrumentedApply() {
45      var tree = SymbolicExpressionTreeParameter.ActualValue;
46      IEnumerable<int> rows = GenerateRowsToEvaluate();
47
48      double quality = Calculate(tree,
49        ProblemDataParameter.ActualValue, rows,
50        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,
66      double upperEstimationLimit) {
67      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, rows);
68      IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
69      OnlineCalculatorError errorState;
70
71      double mae;
72      if (applyLinearScaling) {
73        var maeCalculator = new OnlineMeanAbsoluteErrorCalculator();
74        CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, maeCalculator, problemData.Dataset.Rows);
75        errorState = maeCalculator.ErrorState;
76        mae = maeCalculator.MeanAbsoluteError;
77      } else {
78        IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
79        mae = OnlineMeanAbsoluteErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
80      }
81      if (errorState != OnlineCalculatorError.None) return double.NaN;
82      return mae;
83    }
84
85    public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows) {
86      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
87      EstimationLimitsParameter.ExecutionContext = context;
88      ApplyLinearScalingParameter.ExecutionContext = context;
89
90      double mse = Calculate(
91        tree, problemData, rows,
92        SymbolicDataAnalysisTreeInterpreterParameter.ActualValue,
93        ApplyLinearScalingParameter.ActualValue.Value,
94        EstimationLimitsParameter.ActualValue.Lower,
95        EstimationLimitsParameter.ActualValue.Upper);
96
97      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
98      EstimationLimitsParameter.ExecutionContext = null;
99      ApplyLinearScalingParameter.ExecutionContext = null;
100
101      return mse;
102    }
103
104    public override double Evaluate(
105      ISymbolicExpressionTree tree,
106      IRegressionProblemData problemData,
107      IEnumerable<int> rows,
108      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
109      bool applyLinearScaling = true,
110      double lowerEstimationLimit = double.MinValue,
111      double upperEstimationLimit = double.MaxValue) {
112      return Calculate(tree, problemData, rows, interpreter, applyLinearScaling, lowerEstimationLimit, upperEstimationLimit);
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.