Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSolutionsAnalyzer.cs @ 13656

Last change on this file since 13656 was 13582, checked in by gkronber, 9 years ago

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

File size: 6.3 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Linq;
26using HeuristicLab.Analysis;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Operators;
31using HeuristicLab.Optimization;
32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34
35namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
36  [StorableClass]
37  public class SymbolicRegressionSolutionsAnalyzer : SingleSuccessorOperator, IAnalyzer {
38    private const string ResultCollectionParameterName = "Results";
39    private const string RegressionSolutionQualitiesResultName = "Regression Solution Qualities";
40    private const string TrainingQualityParameterName = "TrainingRSquared";
41    private const string TestQualityParameterName = "TestRSquared";
42
43    public ILookupParameter<ResultCollection> ResultCollectionParameter {
44      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]; }
51    }
52
53    public virtual bool EnabledByDefault {
54      get { return false; }
55    }
56
57    [StorableConstructor]
58    protected SymbolicRegressionSolutionsAnalyzer(bool deserializing) : base(deserializing) { }
59    protected SymbolicRegressionSolutionsAnalyzer(SymbolicRegressionSolutionsAnalyzer original, Cloner cloner)
60      : base(original, cloner) { }
61    public override IDeepCloneable Clone(Cloner cloner) {
62      return new SymbolicRegressionSolutionsAnalyzer(this, cloner);
63    }
64
65    public SymbolicRegressionSolutionsAnalyzer() {
66      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
81    }
82
83    public override IOperation Apply() {
84      var results = ResultCollectionParameter.ActualValue;
85
86      if (!results.ContainsKey(RegressionSolutionQualitiesResultName)) {
87        var newDataTable = new DataTable(RegressionSolutionQualitiesResultName);
88        results.Add(new Result(RegressionSolutionQualitiesResultName, "Chart displaying the training and test qualities of the regression solutions.", newDataTable));
89      }
90
91      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
108      foreach (var result in results.Where(r => r.Value is IRegressionSolution)) {
109        var solution = (IRegressionSolution)result.Value;
110
111        var trainingR2Name = result.Name + " Training R²";
112        if (!dataTable.Rows.ContainsKey(trainingR2Name))
113          dataTable.Rows.Add(new DataRow(trainingR2Name));
114
115        var testR2Name = result.Name + " Test R²";
116        if (!dataTable.Rows.ContainsKey(testR2Name))
117          dataTable.Rows.Add(new DataRow(testR2Name));
118
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        }
132      }
133
134      if (trainingQualityParam != null) trainingQualityParam.ActualName = prevTrainingQualityParamName;
135      if (testQualityParam != null) testQualityParam.ActualName = prevTestQualityParamName;
136
137      return base.Apply();
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.