source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveOverfittingAnalyzer.cs @ 5747

Last change on this file since 5747 was 5747, checked in by gkronber, 11 years ago

#1418 renamed bounded evaluator, added base classes for single objective and multi objective validation analzers, added overfitting analyzers for symbolic regression and classification.

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