Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.Regression/3.3/Symbolic/Analyzer/ValidationBestScaledSymbolicVectorRegressionSolutionAnalyzer.cs @ 4194

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

Created a feature/exploration branch for new data analysis features #1142

File size: 13.5 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Analysis;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.DataAnalysis.MultiVariate.Regression.Symbolic.Evaluators;
33using HeuristicLab.Problems.DataAnalysis.Symbolic;
34using HeuristicLab.Problems.DataAnalysis.MultiVariate.Regression.Symbolic.Interfaces;
35
36namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.Regression.Symbolic.Analyzers {
37  /// <summary>
38  /// An operator that analyzes the validation best scaled symbolic vector regression solution.
39  /// </summary>
40  [Item("ValidationBestScaledSymbolicVectorRegressionSolutionAnalyzer", "An operator that analyzes the validation best scaled symbolic vector regression solution.")]
41  [StorableClass]
42  public sealed class ValidationBestScaledSymbolicVectorRegressionSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
43    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
44    private const string ScaledSymbolicExpressionTreeParameterName = "ScaledSymbolicExpressionTree";
45    private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
46    private const string ProblemDataParameterName = "ProblemData";
47    private const string ValidationSamplesStartParameterName = "ValidationSamplesStart";
48    private const string ValidationSamplesEndParameterName = "ValidationSamplesEnd";
49    private const string EvaluatorParameterName = "Evaluator";
50    private const string MaximizationParameterName = "Maximization";
51    private const string UpperEstimationLimitParameterName = "UpperEstimationLimit";
52    private const string LowerEstimationLimitParameterName = "LowerEstimationLimit";
53    private const string AlphaParameterName = "Alpha";
54    private const string BetaParameterName = "Beta";
55    private const string BestSolutionParameterName = "Best solution (validation)";
56    private const string BestSolutionQualityParameterName = "Best solution quality (validation)";
57    private const string CurrentBestValidationQualityParameterName = "Current best validation quality";
58    private const string BestSolutionQualityValuesParameterName = "Validation Quality";
59    private const string ResultsParameterName = "Results";
60    private const string BestKnownQualityParameterName = "BestKnownQuality";
61
62    #region parameter properties
63    public ScopeTreeLookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
64      get { return (ScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
65    }
66    public ScopeTreeLookupParameter<DoubleArray> AlphaParameter {
67      get { return (ScopeTreeLookupParameter<DoubleArray>)Parameters[AlphaParameterName]; }
68    }
69    public ScopeTreeLookupParameter<DoubleArray> BetaParameter {
70      get { return (ScopeTreeLookupParameter<DoubleArray>)Parameters[BetaParameterName]; }
71    }
72    public IValueLookupParameter<ISymbolicExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
73      get { return (IValueLookupParameter<ISymbolicExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
74    }
75    public IValueLookupParameter<MultiVariateDataAnalysisProblemData> ProblemDataParameter {
76      get { return (IValueLookupParameter<MultiVariateDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
77    }
78    public IValueLookupParameter<IntValue> ValidationSamplesStartParameter {
79      get { return (IValueLookupParameter<IntValue>)Parameters[ValidationSamplesStartParameterName]; }
80    }
81    public IValueLookupParameter<IntValue> ValidationSamplesEndParameter {
82      get { return (IValueLookupParameter<IntValue>)Parameters[ValidationSamplesEndParameterName]; }
83    }
84    public IValueLookupParameter<ISingleObjectiveSymbolicVectorRegressionEvaluator> EvaluatorParameter {
85      get { return (IValueLookupParameter<ISingleObjectiveSymbolicVectorRegressionEvaluator>)Parameters[EvaluatorParameterName]; }
86    }
87    public IValueLookupParameter<BoolValue> MaximizationParameter {
88      get { return (IValueLookupParameter<BoolValue>)Parameters[MaximizationParameterName]; }
89    }
90    public IValueLookupParameter<DoubleArray> UpperEstimationLimitParameter {
91      get { return (IValueLookupParameter<DoubleArray>)Parameters[UpperEstimationLimitParameterName]; }
92    }
93    public IValueLookupParameter<DoubleArray> LowerEstimationLimitParameter {
94      get { return (IValueLookupParameter<DoubleArray>)Parameters[LowerEstimationLimitParameterName]; }
95    }
96    public ILookupParameter<SymbolicExpressionTree> BestSolutionParameter {
97      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[BestSolutionParameterName]; }
98    }
99    public ILookupParameter<DoubleValue> BestSolutionQualityParameter {
100      get { return (ILookupParameter<DoubleValue>)Parameters[BestSolutionQualityParameterName]; }
101    }
102    public ILookupParameter<ResultCollection> ResultsParameter {
103      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
104    }
105    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
106      get { return (ILookupParameter<DoubleValue>)Parameters[BestKnownQualityParameterName]; }
107    }
108    #endregion
109    #region properties
110    public MultiVariateDataAnalysisProblemData ProblemData {
111      get { return ProblemDataParameter.ActualValue; }
112    }
113    public ItemArray<DoubleArray> Alpha {
114      get { return AlphaParameter.ActualValue; }
115    }
116    public ItemArray<DoubleArray> Beta {
117      get { return BetaParameter.ActualValue; }
118    }
119    public DoubleArray LowerEstimationLimit {
120      get { return LowerEstimationLimitParameter.ActualValue; }
121    }
122    public DoubleArray UpperEstimationLimit {
123      get { return UpperEstimationLimitParameter.ActualValue; }
124    }
125    public ISingleObjectiveSymbolicVectorRegressionEvaluator Evaluator {
126      get { return EvaluatorParameter.ActualValue; }
127    }
128    public BoolValue Maximization {
129      get { return MaximizationParameter.ActualValue; }
130    }
131    public DoubleValue BestSolutionQuality {
132      get { return BestSolutionQualityParameter.ActualValue; }
133    }
134    #endregion
135
136    public ValidationBestScaledSymbolicVectorRegressionSolutionAnalyzer()
137      : base() {
138      Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
139      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>(AlphaParameterName, "The alpha parameter for linear scaling."));
140      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>(BetaParameterName, "The beta parameter for linear scaling."));
141      Parameters.Add(new ValueLookupParameter<ISymbolicExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, "The interpreter that should be used for the analysis of symbolic expression trees."));
142      Parameters.Add(new ValueLookupParameter<MultiVariateDataAnalysisProblemData>(ProblemDataParameterName, "The problem data for which the symbolic expression tree is a solution."));
143      Parameters.Add(new ValueLookupParameter<IntValue>(ValidationSamplesStartParameterName, "The first index of the validation partition of the data set."));
144      Parameters.Add(new ValueLookupParameter<IntValue>(ValidationSamplesEndParameterName, "The last index of the validation partition of the data set."));
145      Parameters.Add(new ValueLookupParameter<ISingleObjectiveSymbolicVectorRegressionEvaluator>(EvaluatorParameterName, "The evaluator which should be used to evaluate the solution on the validation set."));
146      Parameters.Add(new ValueLookupParameter<BoolValue>(MaximizationParameterName, "The direction of optimization."));
147      Parameters.Add(new ValueLookupParameter<DoubleArray>(UpperEstimationLimitParameterName, "The upper estimation limit that was set for the evaluation of the symbolic expression trees."));
148      Parameters.Add(new ValueLookupParameter<DoubleArray>(LowerEstimationLimitParameterName, "The lower estimation limit that was set for the evaluation of the symbolic expression trees."));
149      Parameters.Add(new LookupParameter<SymbolicExpressionTree>(BestSolutionParameterName, "The best symbolic regression solution."));
150      Parameters.Add(new LookupParameter<DoubleValue>(BestSolutionQualityParameterName, "The quality of the best symbolic regression solution."));
151      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection where the best symbolic regression solution should be stored."));
152      Parameters.Add(new LookupParameter<DoubleValue>(BestKnownQualityParameterName, "The best known (validation) quality achieved on the data set."));
153
154    }
155
156    public override IOperation Apply() {
157      var trees = SymbolicExpressionTreeParameter.ActualValue;
158      IEnumerable<SymbolicExpressionTree> scaledTrees;
159      if (Alpha.Length == trees.Length) {
160        scaledTrees = from i in Enumerable.Range(0, trees.Length)
161                      select SymbolicVectorRegressionSolutionLinearScaler.Scale(trees[i], Beta[i].ToArray(), Alpha[i].ToArray());
162      } else {
163        scaledTrees = trees;
164      }
165      IEnumerable<string> selectedTargetVariables = from item in ProblemData.TargetVariables.CheckedItems
166                                                    select item.Value.Value;
167      var interpreter = SymbolicExpressionTreeInterpreterParameter.ActualValue;
168      int validationStart = ValidationSamplesStartParameter.ActualValue.Value;
169      int validationEnd = ValidationSamplesEndParameter.ActualValue.Value;
170      IEnumerable<int> rows = Enumerable.Range(validationStart, validationEnd - validationStart);
171      SymbolicExpressionTree bestTree = null;
172      double bestQuality = Maximization.Value ? double.NegativeInfinity : double.PositiveInfinity;
173      foreach (var tree in scaledTrees) {
174        // calculate quality on validation set
175        double quality = Evaluator.Evaluate(tree, interpreter, ProblemData, selectedTargetVariables, rows, LowerEstimationLimit, UpperEstimationLimit);
176        if ((Maximization.Value && quality > bestQuality) ||
177            (!Maximization.Value && quality < bestQuality)) {
178          bestQuality = quality;
179          bestTree = tree;
180        }
181      }
182      bool newBest =
183        BestSolutionQualityParameter.ActualValue == null ||
184        (Maximization.Value && bestQuality > BestSolutionQuality.Value) ||
185                     (!Maximization.Value && bestQuality < BestSolutionQuality.Value);
186      if (newBest) {
187        var bestSolution = bestTree;
188
189        //bestSolution.Name = BestSolutionParameterName;
190        //solution.Description = "Best solution on validation partition found over the whole run.";
191
192        BestSolutionParameter.ActualValue = bestSolution;
193        BestSolutionQualityParameter.ActualValue = new DoubleValue(bestQuality);
194      }
195
196      // update results
197      var results = ResultsParameter.ActualValue;
198      if (!results.ContainsKey(BestSolutionQualityValuesParameterName)) {
199        results.Add(new Result(BestSolutionParameterName, BestSolutionParameter.ActualValue));
200        results.Add(new Result(BestSolutionQualityValuesParameterName, new DataTable(BestSolutionQualityValuesParameterName, BestSolutionQualityValuesParameterName)));
201        results.Add(new Result(BestSolutionQualityParameterName, new DoubleValue()));
202        results.Add(new Result(CurrentBestValidationQualityParameterName, new DoubleValue()));
203      }
204      results[BestSolutionParameterName].Value = BestSolutionParameter.ActualValue;
205      results[BestSolutionQualityParameterName].Value = new DoubleValue(BestSolutionQualityParameter.ActualValue.Value);
206      results[CurrentBestValidationQualityParameterName].Value = new DoubleValue(bestQuality);
207
208      DataTable validationValues = (DataTable)results[BestSolutionQualityValuesParameterName].Value;
209      AddValue(validationValues, BestSolutionQualityParameter.ActualValue.Value, BestSolutionQualityParameterName, BestSolutionQualityParameterName);
210      AddValue(validationValues, bestQuality, CurrentBestValidationQualityParameterName, CurrentBestValidationQualityParameterName);
211
212      return base.Apply();
213    }
214
215    private static void AddValue(DataTable table, double data, string name, string description) {
216      DataRow row;
217      table.Rows.TryGetValue(name, out row);
218      if (row == null) {
219        row = new DataRow(name, description);
220        row.Values.Add(data);
221        table.Rows.Add(row);
222      } else {
223        row.Values.Add(data);
224      }
225    }
226  }
227}
Note: See TracBrowser for help on using the repository browser.