Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveOverfittingAnalyzer.cs @ 6284

Last change on this file since 6284 was 5907, checked in by mkommend, 14 years ago

#1418: Removed calls to IEnumerable.Count() in performance critical sections.

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.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
33  [Item("SymbolicRegressionSingleObjectiveOverfittingAnalyzer", "Calculates and tracks correlation of training and validation fitness of symbolic regression models.")]
34  [StorableClass]
35  public sealed class SymbolicRegressionSingleObjectiveOverfittingAnalyzer : SymbolicDataAnalysisSingleObjectiveValidationAnalyzer<ISymbolicRegressionSingleObjectiveEvaluator, IRegressionProblemData> {
36    private const string TrainingValidationCorrelationParameterName = "Training and validation fitness correlation";
37    private const string TrainingValidationCorrelationTableParameterName = "Training and validation fitness correlation table";
38    private const string LowerCorrelationThresholdParameterName = "LowerCorrelationThreshold";
39    private const string UpperCorrelationThresholdParameterName = "UpperCorrelationThreshold";
40    private const string OverfittingParameterName = "IsOverfitting";
41
42    #region parameter properties
43    public ILookupParameter<DoubleValue> TrainingValidationQualityCorrelationParameter {
44      get { return (ILookupParameter<DoubleValue>)Parameters[TrainingValidationCorrelationParameterName]; }
45    }
46    public ILookupParameter<DataTable> TrainingValidationQualityCorrelationTableParameter {
47      get { return (ILookupParameter<DataTable>)Parameters[TrainingValidationCorrelationTableParameterName]; }
48    }
49    public IValueLookupParameter<DoubleValue> LowerCorrelationThresholdParameter {
50      get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerCorrelationThresholdParameterName]; }
51    }
52    public IValueLookupParameter<DoubleValue> UpperCorrelationThresholdParameter {
53      get { return (IValueLookupParameter<DoubleValue>)Parameters[UpperCorrelationThresholdParameterName]; }
54    }
55    public ILookupParameter<BoolValue> OverfittingParameter {
56      get { return (ILookupParameter<BoolValue>)Parameters[OverfittingParameterName]; }
57    }
58    #endregion
59
60    [StorableConstructor]
61    private SymbolicRegressionSingleObjectiveOverfittingAnalyzer(bool deserializing) : base(deserializing) { }
62    private SymbolicRegressionSingleObjectiveOverfittingAnalyzer(SymbolicRegressionSingleObjectiveOverfittingAnalyzer original, Cloner cloner) : base(original, cloner) { }
63    public SymbolicRegressionSingleObjectiveOverfittingAnalyzer()
64      : base() {
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."));
70    }
71
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new SymbolicRegressionSingleObjectiveOverfittingAnalyzer(this, cloner);
74    }
75
76    public override IOperation Apply() {
77      IEnumerable<int> rows = GenerateRowsToEvaluate();
78      if (!rows.Any()) return base.Apply();
79
80      double[] trainingQuality = QualityParameter.ActualValue.Select(x => x.Value).ToArray();
81      // evaluate on validation partition
82      IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(EvaluatorParameter.ActualValue);
83      double[] validationQuality = (from tree in SymbolicExpressionTree
84                                    select EvaluatorParameter.ActualValue.Evaluate(childContext, tree, ProblemDataParameter.ActualValue, rows))
85                                   .ToArray();
86      double r = 0.0;
87      try {
88        r = alglib.spearmancorr2(trainingQuality, validationQuality);
89      }
90      catch (alglib.alglibexception) {
91        r = 0.0;
92      }
93
94      TrainingValidationQualityCorrelationParameter.ActualValue = new DoubleValue(r);
95
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        dataTable.Rows[TrainingValidationQualityCorrelationParameter.Name].VisualProperties.StartIndexZero = true;
100        TrainingValidationQualityCorrelationTableParameter.ActualValue = dataTable;
101        ResultCollectionParameter.ActualValue.Add(new Result(TrainingValidationQualityCorrelationTableParameter.Name, dataTable));
102      }
103
104      TrainingValidationQualityCorrelationTableParameter.ActualValue.Rows[TrainingValidationQualityCorrelationParameter.Name].Values.Add(r);
105
106      if (OverfittingParameter.ActualValue != null && OverfittingParameter.ActualValue.Value) {
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);
110      } else {
111        // overfitting == false
112        // => r must drop below lower threshold to switch to overfitting state
113        OverfittingParameter.ActualValue = new BoolValue(r < LowerCorrelationThresholdParameter.ActualValue.Value);
114      }
115
116      return base.Apply();
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.