Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSolutionsAnalyzer.cs @ 17564

Last change on this file since 17564 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 5.8 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 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.Linq;
25using HeuristicLab.Analysis;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HEAL.Attic;
33
34namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
35  [StorableType("789E0217-6DDC-44E8-85CC-A51A976A8FB8")]
36  public class SymbolicRegressionSolutionsAnalyzer : SingleSuccessorOperator, IAnalyzer {
37    private const string ResultCollectionParameterName = "Results";
38    private const string RegressionSolutionQualitiesResultName = "Regression Solution Qualities";
39    private const string TrainingQualityParameterName = "TrainingRSquared";
40    private const string TestQualityParameterName = "TestRSquared";
41
42    public ILookupParameter<ResultCollection> ResultCollectionParameter {
43      get { return (ILookupParameter<ResultCollection>)Parameters[ResultCollectionParameterName]; }
44    }
45    public ILookupParameter<DoubleValue> TrainingQualityParameter {
46      get { return (ILookupParameter<DoubleValue>)Parameters[TrainingQualityParameterName]; }
47    }
48    public ILookupParameter<DoubleValue> TestQualityParameter {
49      get { return (ILookupParameter<DoubleValue>)Parameters[TestQualityParameterName]; }
50    }
51
52    public virtual bool EnabledByDefault {
53      get { return false; }
54    }
55
56    [StorableConstructor]
57    protected SymbolicRegressionSolutionsAnalyzer(StorableConstructorFlag _) : base(_) { }
58    protected SymbolicRegressionSolutionsAnalyzer(SymbolicRegressionSolutionsAnalyzer original, Cloner cloner)
59      : base(original, cloner) { }
60    public override IDeepCloneable Clone(Cloner cloner) {
61      return new SymbolicRegressionSolutionsAnalyzer(this, cloner);
62    }
63
64    public SymbolicRegressionSolutionsAnalyzer() {
65      Parameters.Add(new LookupParameter<ResultCollection>(ResultCollectionParameterName, "The result collection to store the analysis results."));
66      Parameters.Add(new LookupParameter<DoubleValue>(TrainingQualityParameterName));
67      Parameters.Add(new LookupParameter<DoubleValue>(TestQualityParameterName));
68    }
69
70    [StorableHook(HookType.AfterDeserialization)]
71    private void AfterDeserialization() {
72      // BackwardsCompatibility3.3
73
74      #region Backwards compatible code, remove with 3.4
75      if (!Parameters.ContainsKey(TrainingQualityParameterName))
76        Parameters.Add(new LookupParameter<DoubleValue>(TrainingQualityParameterName));
77      if (!Parameters.ContainsKey(TestQualityParameterName))
78        Parameters.Add(new LookupParameter<DoubleValue>(TestQualityParameterName));
79      #endregion
80    }
81
82    public override IOperation Apply() {
83      var results = ResultCollectionParameter.ActualValue;
84
85      if (!results.ContainsKey(RegressionSolutionQualitiesResultName)) {
86        var newDataTable = new DataTable(RegressionSolutionQualitiesResultName);
87        results.Add(new Result(RegressionSolutionQualitiesResultName, "Chart displaying the training and test qualities of the regression solutions.", newDataTable));
88      }
89
90      var dataTable = (DataTable)results[RegressionSolutionQualitiesResultName].Value;
91
92      // only if the parameters are available (not available in old persisted code)
93      ILookupParameter<DoubleValue> trainingQualityParam = null;
94      ILookupParameter<DoubleValue> testQualityParam = null;
95      // store actual names of parameter because it is changed below
96      trainingQualityParam = TrainingQualityParameter;
97      string prevTrainingQualityParamName = trainingQualityParam.ActualName;
98      testQualityParam = TestQualityParameter;
99      string prevTestQualityParamName = testQualityParam.ActualName;
100      foreach (var result in results.Where(r => r.Value is IRegressionSolution)) {
101        var solution = (IRegressionSolution)result.Value;
102
103        var trainingR2Name = result.Name + " Training R²";
104        if (!dataTable.Rows.ContainsKey(trainingR2Name))
105          dataTable.Rows.Add(new DataRow(trainingR2Name));
106
107        var testR2Name = result.Name + " Test R²";
108        if (!dataTable.Rows.ContainsKey(testR2Name))
109          dataTable.Rows.Add(new DataRow(testR2Name));
110
111        dataTable.Rows[trainingR2Name].Values.Add(solution.TrainingRSquared);
112        dataTable.Rows[testR2Name].Values.Add(solution.TestRSquared);
113
114        // also add training and test R² to the scope using the parameters
115        // HACK: we change the ActualName of the parameter to write two variables for each solution in the results collection
116        trainingQualityParam.ActualName = trainingR2Name;
117        trainingQualityParam.ActualValue = new DoubleValue(solution.TrainingRSquared);
118        testQualityParam.ActualName = testR2Name;
119        testQualityParam.ActualValue = new DoubleValue(solution.TestRSquared);
120      }
121
122      trainingQualityParam.ActualName = prevTrainingQualityParamName;
123      testQualityParam.ActualName = prevTestQualityParamName;
124
125      return base.Apply();
126    }
127  }
128}
Note: See TracBrowser for help on using the repository browser.