Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionMeanSquaredErrorEvaluator.cs @ 3996

Last change on this file since 3996 was 3996, checked in by gkronber, 14 years ago

Improved efficiency of analyzers and evaluators for regression problems. #1074

File size: 4.9 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Drawing;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
33using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
34using HeuristicLab.Problems.DataAnalysis;
35using HeuristicLab.Operators;
36using HeuristicLab.Problems.DataAnalysis.Evaluators;
37using HeuristicLab.Problems.DataAnalysis.Symbolic;
38
39namespace 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 {
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
68    protected override double Evaluate(ISymbolicExpressionTreeInterpreter interpreter, SymbolicExpressionTree solution, Dataset dataset, StringValue targetVariable, IntValue samplesStart, IntValue samplesEnd) {
69      double mse = Calculate(interpreter, solution, LowerEstimationLimit.Value, UpperEstimationLimit.Value, dataset, targetVariable.Value, samplesStart.Value, samplesEnd.Value);
70      return mse;
71    }
72
73    public static double Calculate(ISymbolicExpressionTreeInterpreter interpreter, SymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, Dataset dataset, string targetVariable, int start, int end) {
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      }
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.