Changeset 13582


Ignore:
Timestamp:
02/01/16 17:27:53 (4 years ago)
Author:
gkronber
Message:

#2572: added parameters to SymbolicRegressionSolutionsAnalyzer to write R² (training & test) for all solutions to the scope

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSolutionsAnalyzer.cs

    r13395 r13582  
    2727using HeuristicLab.Common;
    2828using HeuristicLab.Core;
     29using HeuristicLab.Data;
    2930using HeuristicLab.Operators;
    3031using HeuristicLab.Optimization;
     
    3738    private const string ResultCollectionParameterName = "Results";
    3839    private const string RegressionSolutionQualitiesResultName = "Regression Solution Qualities";
     40    private const string TrainingQualityParameterName = "TrainingRSquared";
     41    private const string TestQualityParameterName = "TestRSquared";
    3942
    4043    public ILookupParameter<ResultCollection> ResultCollectionParameter {
    4144      get { return (ILookupParameter<ResultCollection>)Parameters[ResultCollectionParameterName]; }
     45    }
     46    public ILookupParameter<DoubleValue> TrainingQualityParameter {
     47      get { return (ILookupParameter<DoubleValue>)Parameters[TrainingQualityParameterName]; }
     48    }
     49    public ILookupParameter<DoubleValue> TestQualityParameter {
     50      get { return (ILookupParameter<DoubleValue>)Parameters[TestQualityParameterName]; }
    4251    }
    4352
     
    5665    public SymbolicRegressionSolutionsAnalyzer() {
    5766      Parameters.Add(new LookupParameter<ResultCollection>(ResultCollectionParameterName, "The result collection to store the analysis results."));
     67      Parameters.Add(new LookupParameter<DoubleValue>(TrainingQualityParameterName));
     68      Parameters.Add(new LookupParameter<DoubleValue>(TestQualityParameterName));
     69    }
     70
     71    [StorableHook(HookType.AfterDeserialization)]
     72    private void AfterDeserialization() {
     73      // BackwardsCompatibility3.3
     74
     75      #region Backwards compatible code, remove with 3.4
     76      if (!Parameters.ContainsKey(TrainingQualityParameterName))
     77        Parameters.Add(new LookupParameter<DoubleValue>(TrainingQualityParameterName));
     78      if (!Parameters.ContainsKey(TestQualityParameterName))
     79        Parameters.Add(new LookupParameter<DoubleValue>(TestQualityParameterName));
     80      #endregion
    5881    }
    5982
     
    6790
    6891      var dataTable = (DataTable)results[RegressionSolutionQualitiesResultName].Value;
     92
     93      // only if the parameters are available (not available in old persisted code)
     94      ILookupParameter<DoubleValue> trainingQualityParam = null;
     95      ILookupParameter<DoubleValue> testQualityParam = null;
     96      // store actual names of parameter because it is changed below
     97      string prevTrainingQualityParamName = TrainingQualityParameterName;
     98      string prevTestQualityParamName = TestQualityParameterName;
     99      if (Parameters.ContainsKey(TrainingQualityParameterName)) {
     100        trainingQualityParam = TrainingQualityParameter;
     101        prevTrainingQualityParamName = trainingQualityParam.ActualName;
     102      }
     103      if (Parameters.ContainsKey(TestQualityParameterName)) {
     104        testQualityParam = TestQualityParameter;
     105        prevTestQualityParamName = testQualityParam.ActualName;
     106      }
     107
    69108      foreach (var result in results.Where(r => r.Value is IRegressionSolution)) {
    70109        var solution = (IRegressionSolution)result.Value;
    71110
    72         var trainingR2 = result.Name + Environment.NewLine + "Training R²";
    73         if (!dataTable.Rows.ContainsKey(trainingR2))
    74           dataTable.Rows.Add(new DataRow(trainingR2));
     111        var trainingR2Name = result.Name + " Training R²";
     112        if (!dataTable.Rows.ContainsKey(trainingR2Name))
     113          dataTable.Rows.Add(new DataRow(trainingR2Name));
    75114
    76         var testR2 = result.Name + Environment.NewLine + " Test R²";
    77         if (!dataTable.Rows.ContainsKey(testR2))
    78           dataTable.Rows.Add(new DataRow(testR2));
     115        var testR2Name = result.Name + " Test R²";
     116        if (!dataTable.Rows.ContainsKey(testR2Name))
     117          dataTable.Rows.Add(new DataRow(testR2Name));
    79118
    80         dataTable.Rows[trainingR2].Values.Add(solution.TrainingRSquared);
    81         dataTable.Rows[testR2].Values.Add(solution.TestRSquared);
     119        dataTable.Rows[trainingR2Name].Values.Add(solution.TrainingRSquared);
     120        dataTable.Rows[testR2Name].Values.Add(solution.TestRSquared);
     121
     122        // also add training and test R² to the scope using the parameters
     123        // HACK: we change the ActualName of the parameter to write two variables for each solution in the results collection
     124        if (trainingQualityParam != null) {
     125          trainingQualityParam.ActualName = trainingR2Name;
     126          trainingQualityParam.ActualValue = new DoubleValue(solution.TrainingRSquared);
     127        }
     128        if (testQualityParam != null) {
     129          testQualityParam.ActualName = testR2Name;
     130          testQualityParam.ActualValue = new DoubleValue(solution.TestRSquared);
     131        }
    82132      }
     133
     134      if (trainingQualityParam != null) trainingQualityParam.ActualName = prevTrainingQualityParamName;
     135      if (testQualityParam != null) testQualityParam.ActualName = prevTestQualityParamName;
    83136
    84137      return base.Apply();
Note: See TracChangeset for help on using the changeset viewer.