[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 {
|
---|
[5618] | 30 | [Item("Mean squared error Evaluator", "Calculates the mean squared error of a symbolic regression solution.")]
|
---|
[16565] | 31 | [StorableType("8D4B5243-1635-46A6-AEF9-18C9BCB725DD")]
|
---|
[5500] | 32 | public class SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator : SymbolicRegressionSingleObjectiveEvaluator {
|
---|
[7672] | 33 | public override bool Maximization { get { return false; } }
|
---|
[5500] | 34 | [StorableConstructor]
|
---|
[16565] | 35 | protected SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator(StorableConstructorFlag _) : base(_) { }
|
---|
[5500] | 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 | }
|
---|
[5505] | 42 | public SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator() : base() { }
|
---|
| 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, double upperEstimationLimit) {
|
---|
| 66 | IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, rows);
|
---|
[7677] | 67 | IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
|
---|
[5942] | 68 | OnlineCalculatorError errorState;
|
---|
[7672] | 69 |
|
---|
| 70 | double mse;
|
---|
| 71 | if (applyLinearScaling) {
|
---|
[7677] | 72 | var mseCalculator = new OnlineMeanSquaredErrorCalculator();
|
---|
[8113] | 73 | CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, mseCalculator, problemData.Dataset.Rows);
|
---|
[7677] | 74 | errorState = mseCalculator.ErrorState;
|
---|
| 75 | mse = mseCalculator.MeanSquaredError;
|
---|
[8113] | 76 | } else {
|
---|
| 77 | IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
|
---|
[7677] | 78 | mse = OnlineMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
|
---|
[8113] | 79 | }
|
---|
[8664] | 80 | if (errorState != OnlineCalculatorError.None) return double.NaN;
|
---|
| 81 | return mse;
|
---|
[5500] | 82 | }
|
---|
[5607] | 83 |
|
---|
[5613] | 84 | public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows) {
|
---|
[5722] | 85 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
|
---|
[5770] | 86 | EstimationLimitsParameter.ExecutionContext = context;
|
---|
[8664] | 87 | ApplyLinearScalingParameter.ExecutionContext = context;
|
---|
[5722] | 88 |
|
---|
[18220] | 89 | double mse = Calculate(
|
---|
| 90 | tree, problemData, rows,
|
---|
| 91 | SymbolicDataAnalysisTreeInterpreterParameter.ActualValue,
|
---|
| 92 | ApplyLinearScalingParameter.ActualValue.Value,
|
---|
| 93 | EstimationLimitsParameter.ActualValue.Lower,
|
---|
| 94 | EstimationLimitsParameter.ActualValue.Upper);
|
---|
[5722] | 95 |
|
---|
| 96 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
|
---|
[5770] | 97 | EstimationLimitsParameter.ExecutionContext = null;
|
---|
[8664] | 98 | ApplyLinearScalingParameter.ExecutionContext = null;
|
---|
[5722] | 99 |
|
---|
| 100 | return mse;
|
---|
[5607] | 101 | }
|
---|
[18220] | 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 | }
|
---|
[5500] | 113 | }
|
---|
| 114 | }
|
---|