Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1418 fixed minor issues in symbolic data analysis classes.

File size: 7.0 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.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
32  [Item("SymbolicClassificationSingleObjectiveOverfittingAnalyzer", "Calculates and tracks correlation of training and validation fitness of symbolic classification models.")]
33  [StorableClass]
34  public sealed class SymbolicClassificationSingleObjectiveOverfittingAnalyzer : SymbolicDataAnalysisSingleObjectiveValidationAnalyzer<ISymbolicClassificationSingleObjectiveEvaluator, IClassificationProblemData> {
35    private const string TrainingValidationCorrelationParameterName = "Training and validation fitness correlation";
36    private const string TrainingValidationCorrelationTableParameterName = "Training and validation fitness correlation table";
37    private const string LowerCorrelationThresholdParameterName = "LowerCorrelationThreshold";
38    private const string UpperCorrelationThresholdParameterName = "UpperCorrelationThreshold";
39    private const string OverfittingParameterName = "IsOverfitting";
40
41    #region parameter properties
42    public ILookupParameter<DoubleValue> TrainingValidationQualityCorrelationParameter {
43      get { return (ILookupParameter<DoubleValue>)Parameters[TrainingValidationCorrelationParameterName]; }
44    }
45    public ILookupParameter<DataTable> TrainingValidationQualityCorrelationTableParameter {
46      get { return (ILookupParameter<DataTable>)Parameters[TrainingValidationCorrelationTableParameterName]; }
47    }
48    public IValueLookupParameter<DoubleValue> LowerCorrelationThresholdParameter {
49      get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerCorrelationThresholdParameterName]; }
50    }
51    public IValueLookupParameter<DoubleValue> UpperCorrelationThresholdParameter {
52      get { return (IValueLookupParameter<DoubleValue>)Parameters[UpperCorrelationThresholdParameterName]; }
53    }
54    public ILookupParameter<BoolValue> OverfittingParameter {
55      get { return (ILookupParameter<BoolValue>)Parameters[OverfittingParameterName]; }
56    }
57    #endregion
58
59    [StorableConstructor]
60    private SymbolicClassificationSingleObjectiveOverfittingAnalyzer(bool deserializing) : base(deserializing) { }
61    private SymbolicClassificationSingleObjectiveOverfittingAnalyzer(SymbolicClassificationSingleObjectiveOverfittingAnalyzer original, Cloner cloner) : base(original, cloner) { }
62    public SymbolicClassificationSingleObjectiveOverfittingAnalyzer()
63      : base() {
64      Parameters.Add(new LookupParameter<DoubleValue>(TrainingValidationCorrelationParameterName, "Correlation of training and validation fitnesses"));
65      Parameters.Add(new LookupParameter<DataTable>(TrainingValidationCorrelationTableParameterName, "Data table of training and validation fitness correlation values over the whole run."));
66      Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerCorrelationThresholdParameterName, "Lower threshold for correlation value that marks the boundary from non-overfitting to overfitting.", new DoubleValue(0.65)));
67      Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperCorrelationThresholdParameterName, "Upper threshold for correlation value that marks the boundary from overfitting to non-overfitting.", new DoubleValue(0.75)));
68      Parameters.Add(new LookupParameter<BoolValue>(OverfittingParameterName, "Boolean indicator for overfitting."));
69    }
70
71    public override IDeepCloneable Clone(Cloner cloner) {
72      return new SymbolicClassificationSingleObjectiveOverfittingAnalyzer(this, cloner);
73    }
74
75    public override IOperation Apply() {
76      double[] trainingQuality = QualityParameter.ActualValue.Select(x => x.Value).ToArray();
77      // evaluate on validation partition
78      int start = ValidationPartitionParameter.ActualValue.Start;
79      int end = ValidationPartitionParameter.ActualValue.End;
80      var rows = Enumerable.Range(start, end - start);
81      if (!rows.Any()) return base.Apply();
82
83      IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(EvaluatorParameter.ActualValue);
84      double[] validationQuality = (from tree in SymbolicExpressionTrees
85                                    select EvaluatorParameter.ActualValue.Evaluate(childContext, tree, ProblemDataParameter.ActualValue, rows))
86                                   .ToArray();
87      double r = 0.0;
88      try {
89        r = alglib.spearmancorr2(trainingQuality, validationQuality);
90      }
91      catch (alglib.alglibexception) {
92        r = 0.0;
93      }
94
95      TrainingValidationQualityCorrelationParameter.ActualValue = new DoubleValue(r);
96      if (TrainingValidationQualityCorrelationTableParameter.ActualValue == null) {
97        var dataTable = new DataTable(TrainingValidationQualityCorrelationTableParameter.Name, TrainingValidationQualityCorrelationTableParameter.Description);
98        dataTable.Rows.Add(new DataRow(TrainingValidationQualityCorrelationParameter.Name, TrainingValidationQualityCorrelationParameter.Description));
99        TrainingValidationQualityCorrelationTableParameter.ActualValue = dataTable;
100        ResultCollectionParameter.ActualValue.Add(new Result(TrainingValidationQualityCorrelationTableParameter.Name, dataTable));
101      }
102
103      TrainingValidationQualityCorrelationTableParameter.ActualValue.Rows[TrainingValidationQualityCorrelationParameter.Name].Values.Add(r);
104
105      if (OverfittingParameter.ActualValue != null && OverfittingParameter.ActualValue.Value) {
106        // overfitting == true
107        // => r must reach the upper threshold to switch back to non-overfitting state
108        OverfittingParameter.ActualValue = new BoolValue(r < UpperCorrelationThresholdParameter.ActualValue.Value);
109      } else {
110        // overfitting == false
111        // => r must drop below lower threshold to switch to overfitting state
112        OverfittingParameter.ActualValue = new BoolValue(r < LowerCorrelationThresholdParameter.ActualValue.Value);
113      }
114
115      return base.Apply();
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.