Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveOverfittingAnalyzer.cs @ 16829

Last change on this file since 16829 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 7.0 KB
RevLine 
[4271]1#region License Information
2/* HeuristicLab
[16565]3 * Copyright (C) 2002-2019 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
[5907]22using System.Collections.Generic;
[4271]23using System.Linq;
24using HeuristicLab.Analysis;
[4272]25using HeuristicLab.Common;
[4271]26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
[16565]30using HEAL.Attic;
[4271]31
[5747]32namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
33  [Item("SymbolicClassificationSingleObjectiveOverfittingAnalyzer", "Calculates and tracks correlation of training and validation fitness of symbolic classification models.")]
[16565]34  [StorableType("551DB3E3-56F8-4E92-B538-D453DBA23D77")]
[5747]35  public sealed class SymbolicClassificationSingleObjectiveOverfittingAnalyzer : SymbolicDataAnalysisSingleObjectiveValidationAnalyzer<ISymbolicClassificationSingleObjectiveEvaluator, IClassificationProblemData> {
36    private const string TrainingValidationCorrelationParameterName = "Training and validation fitness correlation";
37    private const string TrainingValidationCorrelationTableParameterName = "Training and validation fitness correlation table";
[5192]38    private const string LowerCorrelationThresholdParameterName = "LowerCorrelationThreshold";
39    private const string UpperCorrelationThresholdParameterName = "UpperCorrelationThreshold";
40    private const string OverfittingParameterName = "IsOverfitting";
[4271]41
42    #region parameter properties
43    public ILookupParameter<DoubleValue> TrainingValidationQualityCorrelationParameter {
[5192]44      get { return (ILookupParameter<DoubleValue>)Parameters[TrainingValidationCorrelationParameterName]; }
[4271]45    }
[5192]46    public ILookupParameter<DataTable> TrainingValidationQualityCorrelationTableParameter {
47      get { return (ILookupParameter<DataTable>)Parameters[TrainingValidationCorrelationTableParameterName]; }
[4271]48    }
[5192]49    public IValueLookupParameter<DoubleValue> LowerCorrelationThresholdParameter {
50      get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerCorrelationThresholdParameterName]; }
[4326]51    }
[5192]52    public IValueLookupParameter<DoubleValue> UpperCorrelationThresholdParameter {
53      get { return (IValueLookupParameter<DoubleValue>)Parameters[UpperCorrelationThresholdParameterName]; }
54    }
[4271]55    public ILookupParameter<BoolValue> OverfittingParameter {
[5192]56      get { return (ILookupParameter<BoolValue>)Parameters[OverfittingParameterName]; }
[4271]57    }
58    #endregion
59
[5192]60    [StorableConstructor]
[16565]61    private SymbolicClassificationSingleObjectiveOverfittingAnalyzer(StorableConstructorFlag _) : base(_) { }
[5747]62    private SymbolicClassificationSingleObjectiveOverfittingAnalyzer(SymbolicClassificationSingleObjectiveOverfittingAnalyzer original, Cloner cloner) : base(original, cloner) { }
63    public SymbolicClassificationSingleObjectiveOverfittingAnalyzer()
[4271]64      : base() {
[5192]65      Parameters.Add(new LookupParameter<DoubleValue>(TrainingValidationCorrelationParameterName, "Correlation of training and validation fitnesses"));
66      Parameters.Add(new LookupParameter<DataTable>(TrainingValidationCorrelationTableParameterName, "Data table of training and validation fitness correlation values over the whole run."));
67      Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerCorrelationThresholdParameterName, "Lower threshold for correlation value that marks the boundary from non-overfitting to overfitting.", new DoubleValue(0.65)));
68      Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperCorrelationThresholdParameterName, "Upper threshold for correlation value that marks the boundary from overfitting to non-overfitting.", new DoubleValue(0.75)));
69      Parameters.Add(new LookupParameter<BoolValue>(OverfittingParameterName, "Boolean indicator for overfitting."));
[4271]70    }
71
[5192]72    public override IDeepCloneable Clone(Cloner cloner) {
[5747]73      return new SymbolicClassificationSingleObjectiveOverfittingAnalyzer(this, cloner);
[4271]74    }
75
[5747]76    public override IOperation Apply() {
[5197]77      double[] trainingQuality = QualityParameter.ActualValue.Select(x => x.Value).ToArray();
[6728]78      var problemData = ProblemDataParameter.ActualValue;
79      var evaluator = EvaluatorParameter.ActualValue;
[5747]80      // evaluate on validation partition
[5882]81      IEnumerable<int> rows = GenerateRowsToEvaluate();
[5907]82      if (!rows.Any()) return base.Apply();
[6728]83      IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator);
84      double[] validationQuality = SymbolicExpressionTree
85        .Select(t => evaluator.Evaluate(childContext, t, problemData, rows))
86        .ToArray();
[5823]87      double r = 0.0;
88      try {
89        r = alglib.spearmancorr2(trainingQuality, validationQuality);
90      }
91      catch (alglib.alglibexception) {
92        r = 0.0;
93      }
[4271]94
[5192]95      TrainingValidationQualityCorrelationParameter.ActualValue = new DoubleValue(r);
96      if (TrainingValidationQualityCorrelationTableParameter.ActualValue == null) {
[5747]97        var dataTable = new DataTable(TrainingValidationQualityCorrelationTableParameter.Name, TrainingValidationQualityCorrelationTableParameter.Description);
98        dataTable.Rows.Add(new DataRow(TrainingValidationQualityCorrelationParameter.Name, TrainingValidationQualityCorrelationParameter.Description));
[5858]99        dataTable.Rows[TrainingValidationQualityCorrelationParameter.Name].VisualProperties.StartIndexZero = true;
[5192]100        TrainingValidationQualityCorrelationTableParameter.ActualValue = dataTable;
[5747]101        ResultCollectionParameter.ActualValue.Add(new Result(TrainingValidationQualityCorrelationTableParameter.Name, dataTable));
[4272]102      }
103
[5747]104      TrainingValidationQualityCorrelationTableParameter.ActualValue.Rows[TrainingValidationQualityCorrelationParameter.Name].Values.Add(r);
[4272]105
[5192]106      if (OverfittingParameter.ActualValue != null && OverfittingParameter.ActualValue.Value) {
[5436]107        // overfitting == true
108        // => r must reach the upper threshold to switch back to non-overfitting state
109        OverfittingParameter.ActualValue = new BoolValue(r < UpperCorrelationThresholdParameter.ActualValue.Value);
[5192]110      } else {
[5436]111        // overfitting == false
112        // => r must drop below lower threshold to switch to overfitting state
113        OverfittingParameter.ActualValue = new BoolValue(r < LowerCorrelationThresholdParameter.ActualValue.Value);
[5010]114      }
[5747]115
116      return base.Apply();
[4271]117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.