[5500] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5500] | 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;
|
---|
[16565] | 27 | using HEAL.Attic;
|
---|
[5500] | 28 |
|
---|
| 29 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
[7677] | 30 | [Item("Maximum absolute error Evaluator", "Calculates the maximum squared error of a symbolic regression solution.")]
|
---|
[16565] | 31 | [StorableType("256A6405-D1EE-4D8D-963A-42C56FEE8571")]
|
---|
[7677] | 32 | public class SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator : SymbolicRegressionSingleObjectiveEvaluator {
|
---|
[7672] | 33 | public override bool Maximization { get { return false; } }
|
---|
[5500] | 34 | [StorableConstructor]
|
---|
[16565] | 35 | protected SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator(StorableConstructorFlag _) : base(_) { }
|
---|
[7677] | 36 | protected SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator(SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator original, Cloner cloner)
|
---|
[5500] | 37 | : base(original, cloner) {
|
---|
| 38 | }
|
---|
| 39 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[7677] | 40 | return new SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator(this, cloner);
|
---|
[5500] | 41 | }
|
---|
[7677] | 42 | public SymbolicRegressionSingleObjectiveMaxAbsoluteErrorEvaluator() : base() { }
|
---|
[5505] | 43 |
|
---|
[10291] | 44 | public override IOperation InstrumentedApply() {
|
---|
[18220] | 45 | var tree = SymbolicExpressionTreeParameter.ActualValue;
|
---|
[5500] | 46 | IEnumerable<int> rows = GenerateRowsToEvaluate();
|
---|
[5851] | 47 |
|
---|
[18220] | 48 | double quality = Calculate(
|
---|
| 49 | tree, ProblemDataParameter.ActualValue,
|
---|
| 50 | rows, SymbolicDataAnalysisTreeInterpreterParameter.ActualValue,
|
---|
| 51 | ApplyLinearScalingParameter.ActualValue.Value,
|
---|
| 52 | EstimationLimitsParameter.ActualValue.Lower,
|
---|
| 53 | EstimationLimitsParameter.ActualValue.Upper);
|
---|
[5851] | 54 | QualityParameter.ActualValue = new DoubleValue(quality);
|
---|
| 55 |
|
---|
[10291] | 56 | return base.InstrumentedApply();
|
---|
[5500] | 57 | }
|
---|
| 58 |
|
---|
[18220] | 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);
|
---|
[7677] | 68 | IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
|
---|
[5942] | 69 | OnlineCalculatorError errorState;
|
---|
[7672] | 70 |
|
---|
| 71 | double mse;
|
---|
| 72 | if (applyLinearScaling) {
|
---|
[7677] | 73 | var maeCalculator = new OnlineMaxAbsoluteErrorCalculator();
|
---|
[8113] | 74 | CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, maeCalculator, problemData.Dataset.Rows);
|
---|
[7677] | 75 | errorState = maeCalculator.ErrorState;
|
---|
| 76 | mse = maeCalculator.MaxAbsoluteError;
|
---|
[8113] | 77 | } else {
|
---|
| 78 | IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
|
---|
[7677] | 79 | mse = OnlineMaxAbsoluteErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
|
---|
[8113] | 80 | }
|
---|
[8664] | 81 | if (errorState != OnlineCalculatorError.None) return double.NaN;
|
---|
| 82 | return mse;
|
---|
[5500] | 83 | }
|
---|
[5607] | 84 |
|
---|
[5613] | 85 | public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows) {
|
---|
[5722] | 86 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
|
---|
[5770] | 87 | EstimationLimitsParameter.ExecutionContext = context;
|
---|
[8664] | 88 | ApplyLinearScalingParameter.ExecutionContext = context;
|
---|
[5722] | 89 |
|
---|
[18220] | 90 | double mse = Calculate(
|
---|
| 91 | tree, problemData, rows,
|
---|
| 92 | SymbolicDataAnalysisTreeInterpreterParameter.ActualValue,
|
---|
| 93 | ApplyLinearScalingParameter.ActualValue.Value,
|
---|
| 94 | EstimationLimitsParameter.ActualValue.Lower,
|
---|
| 95 | EstimationLimitsParameter.ActualValue.Upper);
|
---|
[5722] | 96 |
|
---|
| 97 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
|
---|
[5770] | 98 | EstimationLimitsParameter.ExecutionContext = null;
|
---|
[8664] | 99 | ApplyLinearScalingParameter.ExecutionContext = null;
|
---|
[5722] | 100 |
|
---|
| 101 | return mse;
|
---|
[5607] | 102 | }
|
---|
[18220] | 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 | }
|
---|
[5500] | 114 | }
|
---|
[8664] | 115 | } |
---|