Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/SymbolicRegressionOverfittingAnalyzer.cs @ 6284

Last change on this file since 6284 was 5863, checked in by mkommend, 14 years ago

#1418: Added NonDiscoverableType attribute to outdated analyzers.

File size: 7.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Linq;
23using HeuristicLab.Analysis;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.PluginInfrastructure;
32
33namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers {
34  [Item("SymbolicRegressionOverfittingAnalyzer", "Calculates and tracks correlation of training and validation fitness of symbolic regression models.")]
35  [StorableClass]
36  [NonDiscoverableType]
37  public sealed class SymbolicRegressionOverfittingAnalyzer : SymbolicRegressionValidationAnalyzer, ISymbolicRegressionAnalyzer {
38    private const string MaximizationParameterName = "Maximization";
39    private const string QualityParameterName = "Quality";
40    private const string TrainingValidationCorrelationParameterName = "TrainingValidationCorrelation";
41    private const string TrainingValidationCorrelationTableParameterName = "TrainingValidationCorrelationTable";
42    private const string LowerCorrelationThresholdParameterName = "LowerCorrelationThreshold";
43    private const string UpperCorrelationThresholdParameterName = "UpperCorrelationThreshold";
44    private const string OverfittingParameterName = "IsOverfitting";
45    private const string ResultsParameterName = "Results";
46
47    #region parameter properties
48    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
49      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
50    }
51    public ILookupParameter<BoolValue> MaximizationParameter {
52      get { return (ILookupParameter<BoolValue>)Parameters[MaximizationParameterName]; }
53    }
54    public ILookupParameter<DoubleValue> TrainingValidationQualityCorrelationParameter {
55      get { return (ILookupParameter<DoubleValue>)Parameters[TrainingValidationCorrelationParameterName]; }
56    }
57    public ILookupParameter<DataTable> TrainingValidationQualityCorrelationTableParameter {
58      get { return (ILookupParameter<DataTable>)Parameters[TrainingValidationCorrelationTableParameterName]; }
59    }
60    public IValueLookupParameter<DoubleValue> LowerCorrelationThresholdParameter {
61      get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerCorrelationThresholdParameterName]; }
62    }
63    public IValueLookupParameter<DoubleValue> UpperCorrelationThresholdParameter {
64      get { return (IValueLookupParameter<DoubleValue>)Parameters[UpperCorrelationThresholdParameterName]; }
65    }
66    public ILookupParameter<BoolValue> OverfittingParameter {
67      get { return (ILookupParameter<BoolValue>)Parameters[OverfittingParameterName]; }
68    }
69    public ILookupParameter<ResultCollection> ResultsParameter {
70      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
71    }
72    #endregion
73    #region properties
74    public BoolValue Maximization {
75      get { return MaximizationParameter.ActualValue; }
76    }
77    #endregion
78
79    [StorableConstructor]
80    private SymbolicRegressionOverfittingAnalyzer(bool deserializing) : base(deserializing) { }
81    private SymbolicRegressionOverfittingAnalyzer(SymbolicRegressionOverfittingAnalyzer original, Cloner cloner) : base(original, cloner) { }
82    public SymbolicRegressionOverfittingAnalyzer()
83      : base() {
84      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "Training fitness"));
85      Parameters.Add(new LookupParameter<BoolValue>(MaximizationParameterName, "The direction of optimization."));
86      Parameters.Add(new LookupParameter<DoubleValue>(TrainingValidationCorrelationParameterName, "Correlation of training and validation fitnesses"));
87      Parameters.Add(new LookupParameter<DataTable>(TrainingValidationCorrelationTableParameterName, "Data table of training and validation fitness correlation values over the whole run."));
88      Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerCorrelationThresholdParameterName, "Lower threshold for correlation value that marks the boundary from non-overfitting to overfitting.", new DoubleValue(0.65)));
89      Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperCorrelationThresholdParameterName, "Upper threshold for correlation value that marks the boundary from overfitting to non-overfitting.", new DoubleValue(0.75)));
90      Parameters.Add(new LookupParameter<BoolValue>(OverfittingParameterName, "Boolean indicator for overfitting."));
91      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection."));
92    }
93
94    [StorableHook(HookType.AfterDeserialization)]
95    private void AfterDeserialization() {
96    }
97
98    public override IDeepCloneable Clone(Cloner cloner) {
99      return new SymbolicRegressionOverfittingAnalyzer(this, cloner);
100    }
101
102    protected override void Analyze(SymbolicExpressionTree[] trees, double[] validationQuality) {
103      double[] trainingQuality = QualityParameter.ActualValue.Select(x => x.Value).ToArray();
104
105      double r = alglib.spearmancorr2(trainingQuality, validationQuality);
106
107      TrainingValidationQualityCorrelationParameter.ActualValue = new DoubleValue(r);
108
109      if (TrainingValidationQualityCorrelationTableParameter.ActualValue == null) {
110        var dataTable = new DataTable("Training and validation fitness correlation table", "Data table of training and validation fitness correlation values over the whole run.");
111        dataTable.Rows.Add(new DataRow("Training and validation fitness correlation", "Training and validation fitness correlation values"));
112        TrainingValidationQualityCorrelationTableParameter.ActualValue = dataTable;
113        ResultsParameter.ActualValue.Add(new Result(TrainingValidationCorrelationTableParameterName, dataTable));
114      }
115
116      TrainingValidationQualityCorrelationTableParameter.ActualValue.Rows["Training and validation fitness correlation"].Values.Add(r);
117
118      if (OverfittingParameter.ActualValue != null && OverfittingParameter.ActualValue.Value) {
119        // overfitting == true
120        // => r must reach the upper threshold to switch back to non-overfitting state
121        OverfittingParameter.ActualValue = new BoolValue(r < UpperCorrelationThresholdParameter.ActualValue.Value);
122      } else {
123        // overfitting == false
124        // => r must drop below lower threshold to switch to overfitting state
125        OverfittingParameter.ActualValue = new BoolValue(r < LowerCorrelationThresholdParameter.ActualValue.Value);
126      }
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.