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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
27 | using HEAL.Attic;
|
---|
28 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
29 | using HeuristicLab.Problems.DataAnalysis;
|
---|
30 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Algorithms.OESRALPS.Evaluators
|
---|
33 | {
|
---|
34 | [Item("Mean squared error Evaluator", "Calculates the mean squared error of a symbolic regression solution.")]
|
---|
35 | [StorableType("8D4B5243-1615-46A6-AAF9-18C9BCB725DD")]
|
---|
36 | public class SymbolicRegressionSingleObjectiveMeanSquaredErrorSlidingWindowEvaluator : SymbolicRegressionSingleObjectiveEvaluator
|
---|
37 | {
|
---|
38 | public override bool Maximization { get { return false; } }
|
---|
39 | [StorableConstructor]
|
---|
40 | protected SymbolicRegressionSingleObjectiveMeanSquaredErrorSlidingWindowEvaluator(StorableConstructorFlag _) : base(_) { }
|
---|
41 | protected SymbolicRegressionSingleObjectiveMeanSquaredErrorSlidingWindowEvaluator(SymbolicRegressionSingleObjectiveMeanSquaredErrorSlidingWindowEvaluator original, Cloner cloner)
|
---|
42 | : base(original, cloner)
|
---|
43 | {
|
---|
44 | }
|
---|
45 | public override IDeepCloneable Clone(Cloner cloner)
|
---|
46 | {
|
---|
47 | return new SymbolicRegressionSingleObjectiveMeanSquaredErrorSlidingWindowEvaluator(this, cloner);
|
---|
48 | }
|
---|
49 | public SymbolicRegressionSingleObjectiveMeanSquaredErrorSlidingWindowEvaluator() : base() { }
|
---|
50 |
|
---|
51 | public override IOperation InstrumentedApply()
|
---|
52 | {
|
---|
53 | var solution = SymbolicExpressionTreeParameter.ActualValue;
|
---|
54 | IEnumerable<int> rows = GenerateRowsToEvaluate();
|
---|
55 |
|
---|
56 | double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value);
|
---|
57 | QualityParameter.ActualValue = new DoubleValue(quality);
|
---|
58 |
|
---|
59 | return base.InstrumentedApply();
|
---|
60 | }
|
---|
61 |
|
---|
62 | public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling)
|
---|
63 | {
|
---|
64 | IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
|
---|
65 | IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
|
---|
66 | OnlineCalculatorError errorState;
|
---|
67 |
|
---|
68 | double mse;
|
---|
69 | if (applyLinearScaling)
|
---|
70 | {
|
---|
71 | var mseCalculator = new OnlineMeanSquaredErrorCalculator();
|
---|
72 | CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, mseCalculator, problemData.Dataset.Rows);
|
---|
73 | errorState = mseCalculator.ErrorState;
|
---|
74 | mse = mseCalculator.MeanSquaredError;
|
---|
75 | }
|
---|
76 | else
|
---|
77 | {
|
---|
78 | IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
|
---|
79 | mse = OnlineMeanSquaredErrorCalculator.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 | {
|
---|
87 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
|
---|
88 | EstimationLimitsParameter.ExecutionContext = context;
|
---|
89 | ApplyLinearScalingParameter.ExecutionContext = context;
|
---|
90 |
|
---|
91 | double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value);
|
---|
92 |
|
---|
93 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
|
---|
94 | EstimationLimitsParameter.ExecutionContext = null;
|
---|
95 | ApplyLinearScalingParameter.ExecutionContext = null;
|
---|
96 |
|
---|
97 | return mse;
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|