1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
32 | [Item("Mean squared error Evaluator", "Calculates the mean squared error of a symbolic regression solution.")]
|
---|
33 | [StorableClass]
|
---|
34 | public class SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator : SymbolicRegressionSingleObjectiveEvaluator {
|
---|
35 | public override bool Maximization { get { return false; } }
|
---|
36 | [ThreadStatic]
|
---|
37 | private static double[] estimatedValuesCache;
|
---|
38 |
|
---|
39 | [StorableConstructor]
|
---|
40 | protected SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator(bool deserializing) : base(deserializing) { }
|
---|
41 | protected SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator(SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator original, Cloner cloner)
|
---|
42 | : base(original, cloner) {
|
---|
43 | }
|
---|
44 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
45 | return new SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator(this, cloner);
|
---|
46 | }
|
---|
47 | public SymbolicRegressionSingleObjectiveMeanSquaredErrorEvaluator() : base() { }
|
---|
48 |
|
---|
49 | public override IOperation Apply() {
|
---|
50 | var solution = SymbolicExpressionTreeParameter.ActualValue;
|
---|
51 | IEnumerable<int> rows = GenerateRowsToEvaluate();
|
---|
52 |
|
---|
53 | double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScaling);
|
---|
54 | QualityParameter.ActualValue = new DoubleValue(quality);
|
---|
55 |
|
---|
56 | return base.Apply();
|
---|
57 | }
|
---|
58 |
|
---|
59 | public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling) {
|
---|
60 | IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
|
---|
61 | IEnumerable<double> originalValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
|
---|
62 | IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
|
---|
63 | OnlineCalculatorError errorState;
|
---|
64 |
|
---|
65 | double mse;
|
---|
66 | if (applyLinearScaling) {
|
---|
67 | //int i = 0;
|
---|
68 | //int rowCount = rows.Count();
|
---|
69 | //if (estimatedValuesCache == null) estimatedValuesCache = new double[problemData.Dataset.Rows];
|
---|
70 | //foreach (var value in boundedEstimatedValues) {
|
---|
71 | // estimatedValuesCache[i] = value;
|
---|
72 | // i++;
|
---|
73 | //}
|
---|
74 |
|
---|
75 | double alpha, beta;
|
---|
76 | OnlineLinearScalingParameterCalculator.Calculate(boundedEstimatedValues, originalValues, out alpha, out beta, out errorState);
|
---|
77 | mse = OnlineMeanSquaredErrorCalculator.Calculate(originalValues, boundedEstimatedValues.Select(value => value * beta + alpha), out errorState);
|
---|
78 | } else
|
---|
79 | mse = OnlineMeanSquaredErrorCalculator.Calculate(originalValues, boundedEstimatedValues, out errorState);
|
---|
80 |
|
---|
81 | if (errorState != OnlineCalculatorError.None) return Double.NaN;
|
---|
82 | else 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 |
|
---|
89 | double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScaling);
|
---|
90 |
|
---|
91 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
|
---|
92 | EstimationLimitsParameter.ExecutionContext = null;
|
---|
93 |
|
---|
94 | return mse;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|