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