Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/SymbolicRegressionOverfittingAnalyzer.cs @ 6452

Last change on this file since 6452 was 6228, checked in by epitzer, 14 years ago

check hooks by method name only (#1530)

File size: 7.3 KB
RevLine 
[4271]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4271]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;
[4272]24using HeuristicLab.Common;
[4271]25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[5863]31using HeuristicLab.PluginInfrastructure;
[4271]32
33namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers {
[5192]34  [Item("SymbolicRegressionOverfittingAnalyzer", "Calculates and tracks correlation of training and validation fitness of symbolic regression models.")]
[4271]35  [StorableClass]
[5863]36  [NonDiscoverableType]
[5197]37  public sealed class SymbolicRegressionOverfittingAnalyzer : SymbolicRegressionValidationAnalyzer, ISymbolicRegressionAnalyzer {
[5192]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";
[4271]46
47    #region parameter properties
48    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
[5192]49      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
[4271]50    }
[5192]51    public ILookupParameter<BoolValue> MaximizationParameter {
52      get { return (ILookupParameter<BoolValue>)Parameters[MaximizationParameterName]; }
53    }
[4271]54    public ILookupParameter<DoubleValue> TrainingValidationQualityCorrelationParameter {
[5192]55      get { return (ILookupParameter<DoubleValue>)Parameters[TrainingValidationCorrelationParameterName]; }
[4271]56    }
[5192]57    public ILookupParameter<DataTable> TrainingValidationQualityCorrelationTableParameter {
58      get { return (ILookupParameter<DataTable>)Parameters[TrainingValidationCorrelationTableParameterName]; }
[4271]59    }
[5192]60    public IValueLookupParameter<DoubleValue> LowerCorrelationThresholdParameter {
61      get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerCorrelationThresholdParameterName]; }
[4326]62    }
[5192]63    public IValueLookupParameter<DoubleValue> UpperCorrelationThresholdParameter {
64      get { return (IValueLookupParameter<DoubleValue>)Parameters[UpperCorrelationThresholdParameterName]; }
65    }
[4271]66    public ILookupParameter<BoolValue> OverfittingParameter {
[5192]67      get { return (ILookupParameter<BoolValue>)Parameters[OverfittingParameterName]; }
[4271]68    }
69    public ILookupParameter<ResultCollection> ResultsParameter {
[5192]70      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
[4271]71    }
72    #endregion
73    #region properties
[5192]74    public BoolValue Maximization {
75      get { return MaximizationParameter.ActualValue; }
[4271]76    }
77    #endregion
78
[5192]79    [StorableConstructor]
80    private SymbolicRegressionOverfittingAnalyzer(bool deserializing) : base(deserializing) { }
81    private SymbolicRegressionOverfittingAnalyzer(SymbolicRegressionOverfittingAnalyzer original, Cloner cloner) : base(original, cloner) { }
82    public SymbolicRegressionOverfittingAnalyzer()
[4271]83      : base() {
[5192]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."));
[6228]92    }   
[4271]93
[5192]94    public override IDeepCloneable Clone(Cloner cloner) {
95      return new SymbolicRegressionOverfittingAnalyzer(this, cloner);
[4271]96    }
97
[5197]98    protected override void Analyze(SymbolicExpressionTree[] trees, double[] validationQuality) {
99      double[] trainingQuality = QualityParameter.ActualValue.Select(x => x.Value).ToArray();
[4271]100
[5197]101      double r = alglib.spearmancorr2(trainingQuality, validationQuality);
[4271]102
[5192]103      TrainingValidationQualityCorrelationParameter.ActualValue = new DoubleValue(r);
[4275]104
[5192]105      if (TrainingValidationQualityCorrelationTableParameter.ActualValue == null) {
106        var dataTable = new DataTable("Training and validation fitness correlation table", "Data table of training and validation fitness correlation values over the whole run.");
107        dataTable.Rows.Add(new DataRow("Training and validation fitness correlation", "Training and validation fitness correlation values"));
108        TrainingValidationQualityCorrelationTableParameter.ActualValue = dataTable;
109        ResultsParameter.ActualValue.Add(new Result(TrainingValidationCorrelationTableParameterName, dataTable));
[4272]110      }
111
[5192]112      TrainingValidationQualityCorrelationTableParameter.ActualValue.Rows["Training and validation fitness correlation"].Values.Add(r);
[4272]113
[5192]114      if (OverfittingParameter.ActualValue != null && OverfittingParameter.ActualValue.Value) {
[5436]115        // overfitting == true
116        // => r must reach the upper threshold to switch back to non-overfitting state
117        OverfittingParameter.ActualValue = new BoolValue(r < UpperCorrelationThresholdParameter.ActualValue.Value);
[5192]118      } else {
[5436]119        // overfitting == false
120        // => r must drop below lower threshold to switch to overfitting state
121        OverfittingParameter.ActualValue = new BoolValue(r < LowerCorrelationThresholdParameter.ActualValue.Value);
[5010]122      }
[4271]123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.