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 |
|
---|
22 | using System.Linq;
|
---|
23 | using HeuristicLab.Analysis;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace 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 = alglib.spearmancorr2(trainingQuality, validationQuality);
|
---|
88 |
|
---|
89 | TrainingValidationQualityCorrelationParameter.ActualValue = new DoubleValue(r);
|
---|
90 |
|
---|
91 | if (TrainingValidationQualityCorrelationTableParameter.ActualValue == null) {
|
---|
92 | var dataTable = new DataTable(TrainingValidationQualityCorrelationTableParameter.Name, TrainingValidationQualityCorrelationTableParameter.Description);
|
---|
93 | dataTable.Rows.Add(new DataRow(TrainingValidationQualityCorrelationParameter.Name, TrainingValidationQualityCorrelationParameter.Description));
|
---|
94 | TrainingValidationQualityCorrelationTableParameter.ActualValue = dataTable;
|
---|
95 | ResultCollectionParameter.ActualValue.Add(new Result(TrainingValidationQualityCorrelationTableParameter.Name, dataTable));
|
---|
96 | }
|
---|
97 |
|
---|
98 | TrainingValidationQualityCorrelationTableParameter.ActualValue.Rows[TrainingValidationQualityCorrelationParameter.Name].Values.Add(r);
|
---|
99 |
|
---|
100 | if (OverfittingParameter.ActualValue != null && OverfittingParameter.ActualValue.Value) {
|
---|
101 | // overfitting == true
|
---|
102 | // => r must reach the upper threshold to switch back to non-overfitting state
|
---|
103 | OverfittingParameter.ActualValue = new BoolValue(r < UpperCorrelationThresholdParameter.ActualValue.Value);
|
---|
104 | } else {
|
---|
105 | // overfitting == false
|
---|
106 | // => r must drop below lower threshold to switch to overfitting state
|
---|
107 | OverfittingParameter.ActualValue = new BoolValue(r < LowerCorrelationThresholdParameter.ActualValue.Value);
|
---|
108 | }
|
---|
109 |
|
---|
110 | return base.Apply();
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|