Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator.cs @ 18240

Last change on this file since 18240 was 18220, checked in by gkronber, 2 years ago

#3136: reintegrated structure-template GP branch into trunk

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("Maximum absolute error Evaluator", "Calculates the maximum squared error of a symbolic regression solution.")]
31  [StorableType("256A6405-D1EE-4D8D-963A-42C56FEE8571")]
32  public class SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator : SymbolicRegressionSingleObjectiveEvaluator {
33    public override bool Maximization { get { return false; } }
34    [StorableConstructor]
35    protected SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator(StorableConstructorFlag _) : base(_) { }
36    protected SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator(SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator original, Cloner cloner)
37      : base(original, cloner) {
38    }
39    public override IDeepCloneable Clone(Cloner cloner) {
40      return new SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator(this, cloner);
41    }
42    public SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator() : 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,
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 mse;
72      if (applyLinearScaling) {
73        var maeCalculator = new OnlineMaxAbsoluteErrorCalculator();
74        CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, maeCalculator, problemData.Dataset.Rows);
75        errorState = maeCalculator.ErrorState;
76        mse = maeCalculator.MaxAbsoluteError;
77      } else {
78        IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
79        mse = OnlineMaxAbsoluteErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
80      }
81      if (errorState != OnlineCalculatorError.None) return double.NaN;
82      return mse;
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.