Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/SymbolicRegressionOverfittingAnalyzer.cs @ 7214

Last change on this file since 7214 was 7214, checked in by ascheibe, 12 years ago

#1706 adapted outdated plugins to changes in IAnalyzer

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    public bool EnabledByDefault {
48      get { return true; }
49    }
50
51    #region parameter properties
52    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
53      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
54    }
55    public ILookupParameter<BoolValue> MaximizationParameter {
56      get { return (ILookupParameter<BoolValue>)Parameters[MaximizationParameterName]; }
57    }
58    public ILookupParameter<DoubleValue> TrainingValidationQualityCorrelationParameter {
59      get { return (ILookupParameter<DoubleValue>)Parameters[TrainingValidationCorrelationParameterName]; }
60    }
61    public ILookupParameter<DataTable> TrainingValidationQualityCorrelationTableParameter {
62      get { return (ILookupParameter<DataTable>)Parameters[TrainingValidationCorrelationTableParameterName]; }
63    }
64    public IValueLookupParameter<DoubleValue> LowerCorrelationThresholdParameter {
65      get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerCorrelationThresholdParameterName]; }
66    }
67    public IValueLookupParameter<DoubleValue> UpperCorrelationThresholdParameter {
68      get { return (IValueLookupParameter<DoubleValue>)Parameters[UpperCorrelationThresholdParameterName]; }
69    }
70    public ILookupParameter<BoolValue> OverfittingParameter {
71      get { return (ILookupParameter<BoolValue>)Parameters[OverfittingParameterName]; }
72    }
73    public ILookupParameter<ResultCollection> ResultsParameter {
74      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
75    }
76    #endregion
77    #region properties
78    public BoolValue Maximization {
79      get { return MaximizationParameter.ActualValue; }
80    }
81    #endregion
82
83    [StorableConstructor]
84    private SymbolicRegressionOverfittingAnalyzer(bool deserializing) : base(deserializing) { }
85    private SymbolicRegressionOverfittingAnalyzer(SymbolicRegressionOverfittingAnalyzer original, Cloner cloner) : base(original, cloner) { }
86    public SymbolicRegressionOverfittingAnalyzer()
87      : base() {
88      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "Training fitness"));
89      Parameters.Add(new LookupParameter<BoolValue>(MaximizationParameterName, "The direction of optimization."));
90      Parameters.Add(new LookupParameter<DoubleValue>(TrainingValidationCorrelationParameterName, "Correlation of training and validation fitnesses"));
91      Parameters.Add(new LookupParameter<DataTable>(TrainingValidationCorrelationTableParameterName, "Data table of training and validation fitness correlation values over the whole run."));
92      Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerCorrelationThresholdParameterName, "Lower threshold for correlation value that marks the boundary from non-overfitting to overfitting.", new DoubleValue(0.65)));
93      Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperCorrelationThresholdParameterName, "Upper threshold for correlation value that marks the boundary from overfitting to non-overfitting.", new DoubleValue(0.75)));
94      Parameters.Add(new LookupParameter<BoolValue>(OverfittingParameterName, "Boolean indicator for overfitting."));
95      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The results collection."));
96    }
97
98    [StorableHook(HookType.AfterDeserialization)]
99    private void AfterDeserialization() {
100    }
101
102    public override IDeepCloneable Clone(Cloner cloner) {
103      return new SymbolicRegressionOverfittingAnalyzer(this, cloner);
104    }
105
106    protected override void Analyze(SymbolicExpressionTree[] trees, double[] validationQuality) {
107      double[] trainingQuality = QualityParameter.ActualValue.Select(x => x.Value).ToArray();
108
109      double r = alglib.spearmancorr2(trainingQuality, validationQuality);
110
111      TrainingValidationQualityCorrelationParameter.ActualValue = new DoubleValue(r);
112
113      if (TrainingValidationQualityCorrelationTableParameter.ActualValue == null) {
114        var dataTable = new DataTable("Training and validation fitness correlation table", "Data table of training and validation fitness correlation values over the whole run.");
115        dataTable.Rows.Add(new DataRow("Training and validation fitness correlation", "Training and validation fitness correlation values"));
116        TrainingValidationQualityCorrelationTableParameter.ActualValue = dataTable;
117        ResultsParameter.ActualValue.Add(new Result(TrainingValidationCorrelationTableParameterName, dataTable));
118      }
119
120      TrainingValidationQualityCorrelationTableParameter.ActualValue.Rows["Training and validation fitness correlation"].Values.Add(r);
121
122      if (OverfittingParameter.ActualValue != null && OverfittingParameter.ActualValue.Value) {
123        // overfitting == true
124        // => r must reach the upper threshold to switch back to non-overfitting state
125        OverfittingParameter.ActualValue = new BoolValue(r < UpperCorrelationThresholdParameter.ActualValue.Value);
126      } else {
127        // overfitting == false
128        // => r must drop below lower threshold to switch to overfitting state
129        OverfittingParameter.ActualValue = new BoolValue(r < LowerCorrelationThresholdParameter.ActualValue.Value);
130      }
131    }
132  }
133}
Note: See TracBrowser for help on using the repository browser.