[3374] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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 System.Drawing;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.Parameters;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 32 | using HeuristicLab.PluginInfrastructure;
|
---|
| 33 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 34 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 35 | using HeuristicLab.Operators;
|
---|
| 36 | using HeuristicLab.Problems.DataAnalysis.Evaluators;
|
---|
| 37 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 38 |
|
---|
| 39 | namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic {
|
---|
| 40 | [Item("SymbolicRegressionMeanSquaredErrorEvaluator", "Calculates the mean squared error of a symbolic regression solution.")]
|
---|
| 41 | [StorableClass]
|
---|
| 42 | public class SymbolicRegressionMeanSquaredErrorEvaluator : SymbolicRegressionEvaluator {
|
---|
[3513] | 43 | private const string UpperEstimationLimitParameterName = "UpperEstimationLimit";
|
---|
| 44 | private const string LowerEstimationLimitParameterName = "LowerEstimationLimit";
|
---|
| 45 |
|
---|
| 46 | #region parameter properties
|
---|
| 47 | public IValueLookupParameter<DoubleValue> UpperEstimationLimitParameter {
|
---|
| 48 | get { return (IValueLookupParameter<DoubleValue>)Parameters[UpperEstimationLimitParameterName]; }
|
---|
| 49 | }
|
---|
| 50 | public IValueLookupParameter<DoubleValue> LowerEstimationLimitParameter {
|
---|
| 51 | get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerEstimationLimitParameterName]; }
|
---|
| 52 | }
|
---|
| 53 | #endregion
|
---|
| 54 | #region properties
|
---|
| 55 | public DoubleValue UpperEstimationLimit {
|
---|
| 56 | get { return UpperEstimationLimitParameter.ActualValue; }
|
---|
| 57 | }
|
---|
| 58 | public DoubleValue LowerEstimationLimit {
|
---|
| 59 | get { return LowerEstimationLimitParameter.ActualValue; }
|
---|
| 60 | }
|
---|
| 61 | #endregion
|
---|
| 62 | public SymbolicRegressionMeanSquaredErrorEvaluator()
|
---|
| 63 | : base() {
|
---|
| 64 | Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper limit that should be used as cut off value for the output values of symbolic expression trees."));
|
---|
| 65 | Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower limit that should be used as cut off value for the output values of symbolic expression trees."));
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[3491] | 68 | protected override double Evaluate(ISymbolicExpressionTreeInterpreter interpreter, SymbolicExpressionTree solution, Dataset dataset, StringValue targetVariable, IntValue samplesStart, IntValue samplesEnd) {
|
---|
[3513] | 69 | double mse = Calculate(interpreter, solution, LowerEstimationLimit.Value, UpperEstimationLimit.Value, dataset, targetVariable.Value, samplesStart.Value, samplesEnd.Value);
|
---|
[3374] | 70 | return mse;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[3513] | 73 | public static double Calculate(ISymbolicExpressionTreeInterpreter interpreter, SymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, Dataset dataset, string targetVariable, int start, int end) {
|
---|
[3996] | 74 | IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, dataset, Enumerable.Range(start, end - start));
|
---|
| 75 | IEnumerable<double> originalValues = dataset.GetEnumeratedVariableValues(targetVariable, start, end);
|
---|
| 76 | IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
|
---|
| 77 | IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator();
|
---|
| 78 | OnlineMeanSquaredErrorEvaluator mseEvaluator = new OnlineMeanSquaredErrorEvaluator();
|
---|
| 79 |
|
---|
| 80 | while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
|
---|
| 81 | double estimated = estimatedEnumerator.Current;
|
---|
| 82 | double original = originalEnumerator.Current;
|
---|
| 83 | if (double.IsNaN(estimated))
|
---|
| 84 | estimated = upperEstimationLimit;
|
---|
| 85 | else
|
---|
| 86 | estimated = Math.Min(upperEstimationLimit, Math.Max(lowerEstimationLimit, estimated));
|
---|
| 87 | mseEvaluator.Add(original, estimated);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext()) {
|
---|
| 91 | throw new ArgumentException("Number of elements in original and estimated enumeration doesn't match.");
|
---|
| 92 | } else {
|
---|
| 93 | return mseEvaluator.MeanSquaredError;
|
---|
| 94 | }
|
---|
[3374] | 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|