Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/ValidationBestScaledSymbolicRegressionSolutionAnalyzer.cs @ 3915

Last change on this file since 3915 was 3806, checked in by gkronber, 14 years ago

Fixed a bug in symbolic regression analyzers. (MSE, R² and relative errors were evaluated on the non-scaled solution). #938

File size: 17.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
31using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
32using HeuristicLab.Problems.DataAnalysis.Symbolic;
33using System.Collections.Generic;
34using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
35using HeuristicLab.Problems.DataAnalysis;
36using HeuristicLab.Analysis;
37using System;
38using HeuristicLab.Optimization.Operators;
39
40namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers {
41  /// <summary>
42  /// An operator that analyzes the validation best scaled symbolic regression solution.
43  /// </summary>
44  [Item("ValidationBestScaledSymbolicRegressionSolutionAnalyzer", "An operator that analyzes the validation best scaled symbolic regression solution.")]
45  [StorableClass]
46  public sealed class ValidationBestScaledSymbolicRegressionSolutionAnalyzer : AlgorithmOperator, ISymbolicRegressionAnalyzer {
47    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
48    private const string ScaledSymbolicExpressionTreeParameterName = "ScaledSymbolicExpressionTree";
49    private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
50    private const string ProblemDataParameterName = "ProblemData";
51    private const string TrainingSamplesStartParameterName = "TrainingSamplesStart";
52    private const string TrainingSamplesEndParameterName = "TrainingSamplesEnd";
53    private const string ValidationSamplesStartParameterName = "ValidationSamplesStart";
54    private const string ValidationSamplesEndParameterName = "ValidationSamplesEnd";
55    private const string TestSamplesStartParameterName = "TestSamplesStart";
56    private const string TestSamplesEndParameterName = "TestSamplesEnd";
57    private const string QualityParameterName = "Quality";
58    private const string ScaledQualityParameterName = "ScaledQuality";
59    private const string UpperEstimationLimitParameterName = "UpperEstimationLimit";
60    private const string LowerEstimationLimitParameterName = "LowerEstimationLimit";
61    private const string AlphaParameterName = "Alpha";
62    private const string BetaParameterName = "Beta";
63    private const string BestSolutionParameterName = "Best solution (validation)";
64    private const string BestSolutionQualityParameterName = "Best solution quality (validation)";
65    private const string CurrentBestValidationQualityParameterName = "Current best validation quality";
66    private const string ResultsParameterName = "Results";
67    private const string BestKnownQualityParameterName = "BestKnownQuality";
68
69    public ScopeTreeLookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
70      get { return (ScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
71    }
72    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
73      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
74    }
75    public IValueLookupParameter<ISymbolicExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
76      get { return (IValueLookupParameter<ISymbolicExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
77    }
78    public IValueLookupParameter<DataAnalysisProblemData> ProblemDataParameter {
79      get { return (IValueLookupParameter<DataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
80    }
81    public IValueLookupParameter<IntValue> TrainingSamplesStartParameter {
82      get { return (IValueLookupParameter<IntValue>)Parameters[TrainingSamplesStartParameterName]; }
83    }
84    public IValueLookupParameter<IntValue> TrainingSamplesEndParameter {
85      get { return (IValueLookupParameter<IntValue>)Parameters[TrainingSamplesEndParameterName]; }
86    }
87    public IValueLookupParameter<IntValue> ValidationSamplesStartParameter {
88      get { return (IValueLookupParameter<IntValue>)Parameters[ValidationSamplesStartParameterName]; }
89    }
90    public IValueLookupParameter<IntValue> ValidationSamplesEndParameter {
91      get { return (IValueLookupParameter<IntValue>)Parameters[ValidationSamplesEndParameterName]; }
92    }
93    public IValueLookupParameter<IntValue> TestSamplesStartParameter {
94      get { return (IValueLookupParameter<IntValue>)Parameters[TestSamplesStartParameterName]; }
95    }
96    public IValueLookupParameter<IntValue> TestSamplesEndParameter {
97      get { return (IValueLookupParameter<IntValue>)Parameters[TestSamplesEndParameterName]; }
98    }
99    public IValueLookupParameter<DoubleValue> UpperEstimationLimitParameter {
100      get { return (IValueLookupParameter<DoubleValue>)Parameters[UpperEstimationLimitParameterName]; }
101    }
102    public IValueLookupParameter<DoubleValue> LowerEstimationLimitParameter {
103      get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerEstimationLimitParameterName]; }
104    }
105    public ILookupParameter<SymbolicRegressionSolution> BestSolutionParameter {
106      get { return (ILookupParameter<SymbolicRegressionSolution>)Parameters[BestSolutionParameterName]; }
107    }
108    public ILookupParameter<DoubleValue> BestSolutionQualityParameter {
109      get { return (ILookupParameter<DoubleValue>)Parameters[BestSolutionQualityParameterName]; }
110    }
111    public ILookupParameter<ResultCollection> ResultsParameter {
112      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
113    }
114    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
115      get { return (ILookupParameter<DoubleValue>)Parameters[BestKnownQualityParameterName]; }
116    }
117
118    [Storable]
119    private BestSymbolicRegressionSolutionAnalyzer bestSolutionAnalyzer;
120    [Storable]
121    private UniformSubScopesProcessor subScopesProcessor;
122    [Storable]
123    private BestAverageWorstQualityCalculator bestAvgWorstValidationQualityCalculator;
124    [Storable]
125    private BestQualityMemorizer bestKnownQualityMemorizer;
126    [Storable]
127    private SymbolicRegressionModelQualityAnalyzer modelQualityAnalyzer;
128
129    public ValidationBestScaledSymbolicRegressionSolutionAnalyzer()
130      : base() {
131      Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
132      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The quality of the symbolic expression trees to analyze."));
133      Parameters.Add(new ValueLookupParameter<ISymbolicExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, "The interpreter that should be used for the analysis of symbolic expression trees."));
134      Parameters.Add(new ValueLookupParameter<DataAnalysisProblemData>(ProblemDataParameterName, "The problem data for which the symbolic expression tree is a solution."));
135      Parameters.Add(new ValueLookupParameter<IntValue>(TrainingSamplesStartParameterName, "The first index of the training partition of the data set."));
136      Parameters.Add(new ValueLookupParameter<IntValue>(TrainingSamplesEndParameterName, "The last index of the training partition of the data set."));
137      Parameters.Add(new ValueLookupParameter<IntValue>(ValidationSamplesStartParameterName, "The first index of the validation partition of the data set."));
138      Parameters.Add(new ValueLookupParameter<IntValue>(ValidationSamplesEndParameterName, "The last index of the validation partition of the data set."));
139      Parameters.Add(new ValueLookupParameter<IntValue>(TestSamplesStartParameterName, "The first index of the test partition of the data set."));
140      Parameters.Add(new ValueLookupParameter<IntValue>(TestSamplesEndParameterName, "The last index of the test partition of the data set."));
141      Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper estimation limit that was set for the evaluation of the symbolic expression trees."));
142      Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower estimation limit that was set for the evaluation of the symbolic expression trees."));
143      Parameters.Add(new LookupParameter<SymbolicRegressionSolution>(BestSolutionParameterName, "The best symbolic regression solution."));
144      Parameters.Add(new LookupParameter<DoubleValue>(BestSolutionQualityParameterName, "The quality of the best symbolic regression solution."));
145      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection where the best symbolic regression solution should be stored."));
146      Parameters.Add(new LookupParameter<DoubleValue>(BestKnownQualityParameterName, "The best known (validation) quality achieved on the data set."));
147
148      #region operator initialization
149      subScopesProcessor = new UniformSubScopesProcessor();
150      SymbolicRegressionSolutionLinearScaler linearScaler = new SymbolicRegressionSolutionLinearScaler();
151      modelQualityAnalyzer = new SymbolicRegressionModelQualityAnalyzer();
152      SymbolicRegressionMeanSquaredErrorEvaluator validationMseEvaluator = new SymbolicRegressionMeanSquaredErrorEvaluator();
153      bestSolutionAnalyzer = new BestSymbolicRegressionSolutionAnalyzer();
154      bestKnownQualityMemorizer = new BestQualityMemorizer();
155      bestAvgWorstValidationQualityCalculator = new BestAverageWorstQualityCalculator();
156      DataTableValuesCollector validationValuesCollector = new DataTableValuesCollector();
157      ResultsCollector resultsCollector = new ResultsCollector();
158      #endregion
159
160      #region parameter wiring
161      subScopesProcessor.Depth.Value = SymbolicExpressionTreeParameter.Depth;
162
163      linearScaler.AlphaParameter.ActualName = AlphaParameterName;
164      linearScaler.BetaParameter.ActualName = BetaParameterName;
165      linearScaler.SymbolicExpressionTreeParameter.ActualName = SymbolicExpressionTreeParameter.Name;
166      linearScaler.ScaledSymbolicExpressionTreeParameter.ActualName = ScaledSymbolicExpressionTreeParameterName;
167
168      modelQualityAnalyzer.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
169      modelQualityAnalyzer.SymbolicExpressionTreeParameter.ActualName = ScaledSymbolicExpressionTreeParameterName;
170      modelQualityAnalyzer.SymbolicExpressionTreeParameter.Depth = SymbolicExpressionTreeParameter.Depth;
171      modelQualityAnalyzer.UpperEstimationLimitParameter.ActualName = UpperEstimationLimitParameter.Name;
172      modelQualityAnalyzer.LowerEstimationLimitParameter.ActualName = LowerEstimationLimitParameter.Name;
173      modelQualityAnalyzer.SymbolicExpressionTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
174      modelQualityAnalyzer.TrainingSamplesStartParameter.ActualName = TrainingSamplesStartParameter.Name;
175      modelQualityAnalyzer.TrainingSamplesEndParameter.ActualName = TrainingSamplesEndParameter.Name;
176      modelQualityAnalyzer.TestSamplesStartParameter.ActualName = TestSamplesStartParameter.Name;
177      modelQualityAnalyzer.TestSamplesEndParameter.ActualName = TestSamplesEndParameter.Name;
178
179      validationMseEvaluator.LowerEstimationLimitParameter.ActualName = LowerEstimationLimitParameter.Name;
180      validationMseEvaluator.UpperEstimationLimitParameter.ActualName = UpperEstimationLimitParameter.Name;
181      validationMseEvaluator.SymbolicExpressionTreeParameter.ActualName = ScaledSymbolicExpressionTreeParameterName;
182      validationMseEvaluator.SymbolicExpressionTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
183      validationMseEvaluator.QualityParameter.ActualName = ScaledQualityParameterName;
184      validationMseEvaluator.RegressionProblemDataParameter.ActualName = ProblemDataParameter.Name;
185      validationMseEvaluator.SamplesStartParameter.ActualName = ValidationSamplesStartParameter.Name;
186      validationMseEvaluator.SamplesEndParameter.ActualName = ValidationSamplesEndParameter.Name;
187
188      bestSolutionAnalyzer.BestSolutionParameter.ActualName = BestSolutionParameter.Name;
189      bestSolutionAnalyzer.BestSolutionQualityParameter.ActualName = BestSolutionQualityParameter.Name;
190      bestSolutionAnalyzer.LowerEstimationLimitParameter.ActualName = LowerEstimationLimitParameter.Name;
191      bestSolutionAnalyzer.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
192      bestSolutionAnalyzer.QualityParameter.ActualName = ScaledQualityParameterName;
193      bestSolutionAnalyzer.ResultsParameter.ActualName = ResultsParameter.Name;
194      bestSolutionAnalyzer.SymbolicExpressionTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
195      bestSolutionAnalyzer.SymbolicExpressionTreeParameter.ActualName = ScaledSymbolicExpressionTreeParameterName;
196      bestSolutionAnalyzer.SymbolicExpressionTreeParameter.Depth = SymbolicExpressionTreeParameter.Depth;
197      bestSolutionAnalyzer.UpperEstimationLimitParameter.ActualName = UpperEstimationLimitParameter.Name;
198
199      bestAvgWorstValidationQualityCalculator.AverageQualityParameter.ActualName = "Current average validation quality";
200      bestAvgWorstValidationQualityCalculator.BestQualityParameter.ActualName = CurrentBestValidationQualityParameterName;
201      bestAvgWorstValidationQualityCalculator.MaximizationParameter.Value = new BoolValue(false);
202      bestAvgWorstValidationQualityCalculator.QualityParameter.ActualName = ScaledQualityParameterName;
203      bestAvgWorstValidationQualityCalculator.QualityParameter.Depth = SymbolicExpressionTreeParameter.Depth;
204      bestAvgWorstValidationQualityCalculator.WorstQualityParameter.ActualName = "Current worst validation quality";
205
206      bestKnownQualityMemorizer.BestQualityParameter.ActualName = BestKnownQualityParameterName;
207      bestKnownQualityMemorizer.MaximizationParameter.Value = new BoolValue(false);
208      bestKnownQualityMemorizer.QualityParameter.ActualName = QualityParameter.Name;
209      bestKnownQualityMemorizer.QualityParameter.Depth = QualityParameter.Depth;
210
211      validationValuesCollector.DataTableParameter.ActualName = "Validation quality";
212      validationValuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(CurrentBestValidationQualityParameterName, null, CurrentBestValidationQualityParameterName));
213      validationValuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(BestSolutionQualityParameter.Name, null, BestSolutionQualityParameter.Name));
214
215      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(CurrentBestValidationQualityParameterName, null, CurrentBestValidationQualityParameterName));
216      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(BestSolutionQualityParameter.Name, null, BestSolutionQualityParameter.Name));
217      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Validation quality"));
218      resultsCollector.ResultsParameter.ActualName = ResultsParameter.Name;
219      #endregion
220
221      #region operator graph
222      OperatorGraph.InitialOperator = subScopesProcessor;
223      subScopesProcessor.Operator = linearScaler;
224      linearScaler.Successor = validationMseEvaluator;
225      validationMseEvaluator.Successor = null;
226      subScopesProcessor.Successor = modelQualityAnalyzer;
227      modelQualityAnalyzer.Successor = bestSolutionAnalyzer;
228      bestSolutionAnalyzer.Successor = bestAvgWorstValidationQualityCalculator;
229      bestAvgWorstValidationQualityCalculator.Successor = bestKnownQualityMemorizer;
230      bestKnownQualityMemorizer.Successor = validationValuesCollector;
231      validationValuesCollector.Successor = resultsCollector;
232      resultsCollector.Successor = null;
233      #endregion
234
235      Initialize();
236    }
237
238    [StorableConstructor]
239    private ValidationBestScaledSymbolicRegressionSolutionAnalyzer(bool deserializing) : base() { }
240
241    [StorableHook(HookType.AfterDeserialization)]
242    private void Initialize() {
243      SymbolicExpressionTreeParameter.DepthChanged += new EventHandler(SymbolicExpressionTreeParameter_DepthChanged);
244    }
245
246    public override IDeepCloneable Clone(Cloner cloner) {
247      ValidationBestScaledSymbolicRegressionSolutionAnalyzer clone = (ValidationBestScaledSymbolicRegressionSolutionAnalyzer)base.Clone(cloner);
248      clone.Initialize();
249      return clone;
250    }
251
252    private void SymbolicExpressionTreeParameter_DepthChanged(object sender, EventArgs e) {
253      subScopesProcessor.Depth.Value = SymbolicExpressionTreeParameter.Depth;
254      bestSolutionAnalyzer.SymbolicExpressionTreeParameter.Depth = SymbolicExpressionTreeParameter.Depth;
255      bestSolutionAnalyzer.QualityParameter.Depth = SymbolicExpressionTreeParameter.Depth;
256      bestAvgWorstValidationQualityCalculator.QualityParameter.Depth = SymbolicExpressionTreeParameter.Depth;
257      bestKnownQualityMemorizer.QualityParameter.Depth = SymbolicExpressionTreeParameter.Depth;
258      modelQualityAnalyzer.SymbolicExpressionTreeParameter.Depth = SymbolicExpressionTreeParameter.Depth;
259    }
260  }
261}
Note: See TracBrowser for help on using the repository browser.