#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System.Collections.Generic; using System.Linq; using HeuristicLab.Analysis; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.DataAnalysis.Evaluators; using HeuristicLab.Problems.DataAnalysis.Symbolic; using System; namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers { [Item("SymbolicRegressionOverfittingAnalyzer", "Calculates and tracks correlation of training and validation fitness of symbolic regression models.")] [StorableClass] public sealed class SymbolicRegressionOverfittingAnalyzer : SymbolicRegressionValidationAnalyzer, ISymbolicRegressionAnalyzer { private const string MaximizationParameterName = "Maximization"; private const string QualityParameterName = "Quality"; private const string TrainingValidationCorrelationParameterName = "TrainingValidationCorrelation"; private const string TrainingValidationCorrelationTableParameterName = "TrainingValidationCorrelationTable"; private const string LowerCorrelationThresholdParameterName = "LowerCorrelationThreshold"; private const string UpperCorrelationThresholdParameterName = "UpperCorrelationThreshold"; private const string OverfittingParameterName = "IsOverfitting"; private const string ResultsParameterName = "Results"; #region parameter properties public ScopeTreeLookupParameter QualityParameter { get { return (ScopeTreeLookupParameter)Parameters[QualityParameterName]; } } public ILookupParameter MaximizationParameter { get { return (ILookupParameter)Parameters[MaximizationParameterName]; } } public ILookupParameter TrainingValidationQualityCorrelationParameter { get { return (ILookupParameter)Parameters[TrainingValidationCorrelationParameterName]; } } public ILookupParameter TrainingValidationQualityCorrelationTableParameter { get { return (ILookupParameter)Parameters[TrainingValidationCorrelationTableParameterName]; } } public IValueLookupParameter LowerCorrelationThresholdParameter { get { return (IValueLookupParameter)Parameters[LowerCorrelationThresholdParameterName]; } } public IValueLookupParameter UpperCorrelationThresholdParameter { get { return (IValueLookupParameter)Parameters[UpperCorrelationThresholdParameterName]; } } public ILookupParameter OverfittingParameter { get { return (ILookupParameter)Parameters[OverfittingParameterName]; } } public ILookupParameter ResultsParameter { get { return (ILookupParameter)Parameters[ResultsParameterName]; } } #endregion #region properties public BoolValue Maximization { get { return MaximizationParameter.ActualValue; } } #endregion [StorableConstructor] private SymbolicRegressionOverfittingAnalyzer(bool deserializing) : base(deserializing) { } private SymbolicRegressionOverfittingAnalyzer(SymbolicRegressionOverfittingAnalyzer original, Cloner cloner) : base(original, cloner) { } public SymbolicRegressionOverfittingAnalyzer() : base() { Parameters.Add(new ScopeTreeLookupParameter(QualityParameterName, "Training fitness")); Parameters.Add(new LookupParameter(MaximizationParameterName, "The direction of optimization.")); Parameters.Add(new LookupParameter(TrainingValidationCorrelationParameterName, "Correlation of training and validation fitnesses")); Parameters.Add(new LookupParameter(TrainingValidationCorrelationTableParameterName, "Data table of training and validation fitness correlation values over the whole run.")); Parameters.Add(new ValueLookupParameter(LowerCorrelationThresholdParameterName, "Lower threshold for correlation value that marks the boundary from non-overfitting to overfitting.", new DoubleValue(0.65))); Parameters.Add(new ValueLookupParameter(UpperCorrelationThresholdParameterName, "Upper threshold for correlation value that marks the boundary from overfitting to non-overfitting.", new DoubleValue(0.75))); Parameters.Add(new LookupParameter(OverfittingParameterName, "Boolean indicator for overfitting.")); Parameters.Add(new LookupParameter(ResultsParameterName, "The results collection.")); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { } public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicRegressionOverfittingAnalyzer(this, cloner); } protected override void Analyze(SymbolicExpressionTree[] trees, double[] validationQuality) { double[] trainingQuality = QualityParameter.ActualValue.Select(x => x.Value).ToArray(); double r = alglib.spearmancorr2(trainingQuality, validationQuality); TrainingValidationQualityCorrelationParameter.ActualValue = new DoubleValue(r); if (TrainingValidationQualityCorrelationTableParameter.ActualValue == null) { var dataTable = new DataTable("Training and validation fitness correlation table", "Data table of training and validation fitness correlation values over the whole run."); dataTable.Rows.Add(new DataRow("Training and validation fitness correlation", "Training and validation fitness correlation values")); TrainingValidationQualityCorrelationTableParameter.ActualValue = dataTable; ResultsParameter.ActualValue.Add(new Result(TrainingValidationCorrelationTableParameterName, dataTable)); } TrainingValidationQualityCorrelationTableParameter.ActualValue.Rows["Training and validation fitness correlation"].Values.Add(r); double correlationThreshold; if (OverfittingParameter.ActualValue != null && OverfittingParameter.ActualValue.Value) { // if is already overfitting => have to reach the upper threshold to switch back to non-overfitting state correlationThreshold = UpperCorrelationThresholdParameter.ActualValue.Value; } else { // if currently in non-overfitting state => have to reach to lower threshold to switch to overfitting state correlationThreshold = LowerCorrelationThresholdParameter.ActualValue.Value; } bool overfitting = r < correlationThreshold; OverfittingParameter.ActualValue = new BoolValue(overfitting); } } }