Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer.cs @ 7214

Last change on this file since 7214 was 7214, checked in by ascheibe, 13 years ago

#1706 adapted outdated plugins to changes in IAnalyzer

File size: 14.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 HeuristicLab.Analysis;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.PluginInfrastructure;
31using HeuristicLab.Problems.DataAnalysis.Symbolic;
32
33namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers {
34  /// <summary>
35  /// An operator that analyzes the validation best scaled symbolic regression solution.
36  /// </summary>
37  [Item("FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer", "An operator that analyzes the validation best scaled symbolic regression solution.")]
38  [StorableClass]
39  [NonDiscoverableType]
40  public sealed class FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer : SymbolicRegressionValidationAnalyzer, ISymbolicRegressionAnalyzer {
41    private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
42    private const string MaximizationParameterName = "Maximization";
43    private const string CalculateSolutionComplexityParameterName = "CalculateSolutionComplexity";
44    private const string BestSolutionParameterName = "Best solution (validation)";
45    private const string BestSolutionQualityParameterName = "Best solution quality (validation)";
46    private const string BestSolutionLengthParameterName = "Best solution length (validation)";
47    private const string BestSolutionHeightParameterName = "Best solution height (validiation)";
48    private const string CurrentBestValidationQualityParameterName = "Current best validation quality";
49    private const string BestSolutionQualityValuesParameterName = "Validation Quality";
50    private const string ResultsParameterName = "Results";
51    private const string VariableFrequenciesParameterName = "VariableFrequencies";
52    private const string BestKnownQualityParameterName = "BestKnownQuality";
53    private const string GenerationsParameterName = "Generations";
54
55    public bool EnabledByDefault {
56      get { return true; }
57    }
58
59    #region parameter properties
60    public ILookupParameter<BoolValue> MaximizationParameter {
61      get { return (ILookupParameter<BoolValue>)Parameters[MaximizationParameterName]; }
62    }
63    public IValueParameter<BoolValue> CalculateSolutionComplexityParameter {
64      get { return (IValueParameter<BoolValue>)Parameters[CalculateSolutionComplexityParameterName]; }
65    }
66    public ILookupParameter<SymbolicRegressionSolution> BestSolutionParameter {
67      get { return (ILookupParameter<SymbolicRegressionSolution>)Parameters[BestSolutionParameterName]; }
68    }
69    public ILookupParameter<IntValue> GenerationsParameter {
70      get { return (ILookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
71    }
72    public ILookupParameter<DoubleValue> BestSolutionQualityParameter {
73      get { return (ILookupParameter<DoubleValue>)Parameters[BestSolutionQualityParameterName]; }
74    }
75    public ILookupParameter<IntValue> BestSolutionLengthParameter {
76      get { return (ILookupParameter<IntValue>)Parameters[BestSolutionLengthParameterName]; }
77    }
78    public ILookupParameter<IntValue> BestSolutionHeightParameter {
79      get { return (ILookupParameter<IntValue>)Parameters[BestSolutionHeightParameterName]; }
80    }
81    public ILookupParameter<ResultCollection> ResultsParameter {
82      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
83    }
84    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
85      get { return (ILookupParameter<DoubleValue>)Parameters[BestKnownQualityParameterName]; }
86    }
87    public ILookupParameter<DataTable> VariableFrequenciesParameter {
88      get { return (ILookupParameter<DataTable>)Parameters[VariableFrequenciesParameterName]; }
89    }
90    public IValueLookupParameter<BoolValue> ApplyLinearScalingParameter {
91      get { return (IValueLookupParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
92    }
93    #endregion
94    #region properties
95    public BoolValue Maximization {
96      get { return MaximizationParameter.ActualValue; }
97    }
98    public BoolValue CalculateSolutionComplexity {
99      get { return CalculateSolutionComplexityParameter.Value; }
100      set { CalculateSolutionComplexityParameter.Value = value; }
101    }
102    public ResultCollection Results {
103      get { return ResultsParameter.ActualValue; }
104    }
105    public DataTable VariableFrequencies {
106      get { return VariableFrequenciesParameter.ActualValue; }
107    }
108    public IntValue Generations {
109      get { return GenerationsParameter.ActualValue; }
110    }
111    public DoubleValue BestSolutionQuality {
112      get { return BestSolutionQualityParameter.ActualValue; }
113    }
114    public IntValue BestSolutionLength {
115      get { return BestSolutionLengthParameter.ActualValue; }
116      set { BestSolutionLengthParameter.ActualValue = value; }
117    }
118    public IntValue BestSolutionHeight {
119      get { return BestSolutionHeightParameter.ActualValue; }
120      set { BestSolutionHeightParameter.ActualValue = value; }
121    }
122    public BoolValue ApplyLinearScaling {
123      get { return ApplyLinearScalingParameter.ActualValue; }
124      set { ApplyLinearScalingParameter.ActualValue = value; }
125    }
126    #endregion
127
128    [StorableConstructor]
129    private FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer(bool deserializing) : base(deserializing) { }
130    private FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer(FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
131    public FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer()
132      : base() {
133      Parameters.Add(new ValueLookupParameter<BoolValue>(ApplyLinearScalingParameterName, "The switch determines if the best solution should be linearly scaled on the whole training set.", new BoolValue(true)));
134      Parameters.Add(new LookupParameter<BoolValue>(MaximizationParameterName, "The direction of optimization."));
135      Parameters.Add(new ValueParameter<BoolValue>(CalculateSolutionComplexityParameterName, "Determines if the length and height of the validation best solution should be calculated.", new BoolValue(true)));
136      Parameters.Add(new LookupParameter<SymbolicRegressionSolution>(BestSolutionParameterName, "The best symbolic regression solution."));
137      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of generations calculated so far."));
138      Parameters.Add(new LookupParameter<DoubleValue>(BestSolutionQualityParameterName, "The quality of the best symbolic regression solution."));
139      Parameters.Add(new LookupParameter<IntValue>(BestSolutionLengthParameterName, "The length of the best symbolic regression solution."));
140      Parameters.Add(new LookupParameter<IntValue>(BestSolutionHeightParameterName, "The height of the best symbolic regression solution."));
141      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection where the best symbolic regression solution should be stored."));
142      Parameters.Add(new LookupParameter<DoubleValue>(BestKnownQualityParameterName, "The best known (validation) quality achieved on the data set."));
143      Parameters.Add(new LookupParameter<DataTable>(VariableFrequenciesParameterName, "The variable frequencies table to use for the calculation of variable impacts"));
144    }
145
146    public override IDeepCloneable Clone(Cloner cloner) {
147      return new FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer(this, cloner);
148    }
149
150    [StorableHook(HookType.AfterDeserialization)]
151    private void AfterDeserialization() {
152      #region compatibility remove before releasing 3.4
153      if (!Parameters.ContainsKey("Evaluator")) {
154        Parameters.Add(new LookupParameter<ISymbolicRegressionEvaluator>("Evaluator", "The evaluator which should be used to evaluate the solution on the validation set."));
155      }
156      if (!Parameters.ContainsKey(MaximizationParameterName)) {
157        Parameters.Add(new LookupParameter<BoolValue>(MaximizationParameterName, "The direction of optimization."));
158      }
159      if (!Parameters.ContainsKey(CalculateSolutionComplexityParameterName)) {
160        Parameters.Add(new ValueParameter<BoolValue>(CalculateSolutionComplexityParameterName, "Determines if the length and height of the validation best solution should be calculated.", new BoolValue(false)));
161      }
162      if (!Parameters.ContainsKey(BestSolutionLengthParameterName)) {
163        Parameters.Add(new LookupParameter<IntValue>(BestSolutionLengthParameterName, "The length of the best symbolic regression solution."));
164      }
165      if (!Parameters.ContainsKey(BestSolutionHeightParameterName)) {
166        Parameters.Add(new LookupParameter<IntValue>(BestSolutionHeightParameterName, "The height of the best symbolic regression solution."));
167      }
168      if (!Parameters.ContainsKey(ApplyLinearScalingParameterName)) {
169        Parameters.Add(new ValueLookupParameter<BoolValue>(ApplyLinearScalingParameterName, "The switch determines if the best solution should be linearly scaled on the whole training set.", new BoolValue(true)));
170      }
171      #endregion
172    }
173
174    protected override void Analyze(SymbolicExpressionTree[] trees, double[] validationQuality) {
175      double bestQuality = Maximization.Value ? double.NegativeInfinity : double.PositiveInfinity;
176      SymbolicExpressionTree bestTree = null;
177
178      for (int i = 0; i < trees.Length; i++) {
179        double quality = validationQuality[i];
180        if ((Maximization.Value && quality > bestQuality) ||
181            (!Maximization.Value && quality < bestQuality)) {
182          bestQuality = quality;
183          bestTree = trees[i];
184        }
185      }
186
187      // if the best validation tree is better than the current best solution => update
188      bool newBest =
189        BestSolutionQuality == null ||
190        (Maximization.Value && bestQuality > BestSolutionQuality.Value) ||
191        (!Maximization.Value && bestQuality < BestSolutionQuality.Value);
192      if (newBest) {
193        double upperEstimationLimit = UpperEstimationLimit != null ? UpperEstimationLimit.Value : double.PositiveInfinity;
194        double lowerEstimationLimit = LowerEstimationLimit != null ? LowerEstimationLimit.Value : double.NegativeInfinity;
195        string targetVariable = ProblemData.TargetVariable.Value;
196
197        if (ApplyLinearScaling.Value) {
198          // calculate scaling parameters and only for the best tree using the full training set
199          double alpha, beta;
200          SymbolicRegressionScaledMeanSquaredErrorEvaluator.Calculate(SymbolicExpressionTreeInterpreter, bestTree,
201            lowerEstimationLimit, upperEstimationLimit,
202            ProblemData.Dataset, targetVariable,
203            ProblemData.TrainingIndizes, out beta, out alpha);
204
205          // scale tree for solution
206          bestTree = SymbolicRegressionSolutionLinearScaler.Scale(bestTree, alpha, beta);
207        }
208        var model = new SymbolicRegressionModel((ISymbolicExpressionTreeInterpreter)SymbolicExpressionTreeInterpreter.Clone(),
209          bestTree);
210        var solution = new SymbolicRegressionSolution((DataAnalysisProblemData)ProblemData.Clone(), model, lowerEstimationLimit, upperEstimationLimit);
211        solution.Name = BestSolutionParameterName;
212        solution.Description = "Best solution on validation partition found over the whole run.";
213
214        BestSolutionParameter.ActualValue = solution;
215        BestSolutionQualityParameter.ActualValue = new DoubleValue(bestQuality);
216
217        if (CalculateSolutionComplexity.Value) {
218          BestSolutionLength = new IntValue(solution.Model.SymbolicExpressionTree.Size);
219          BestSolutionHeight = new IntValue(solution.Model.SymbolicExpressionTree.Height);
220          if (!Results.ContainsKey(BestSolutionLengthParameterName)) {
221            Results.Add(new Result(BestSolutionLengthParameterName, "Length of the best solution on the validation set", new IntValue()));
222            Results.Add(new Result(BestSolutionHeightParameterName, "Height of the best solution on the validation set", new IntValue()));
223          }
224          Results[BestSolutionLengthParameterName].Value = BestSolutionLength;
225          Results[BestSolutionHeightParameterName].Value = BestSolutionHeight;
226        }
227
228        BestSymbolicRegressionSolutionAnalyzer.UpdateBestSolutionResults(solution, ProblemData, Results, Generations, VariableFrequencies);
229      }
230
231      if (!Results.ContainsKey(BestSolutionQualityValuesParameterName)) {
232        Results.Add(new Result(BestSolutionQualityValuesParameterName, new DataTable(BestSolutionQualityValuesParameterName, BestSolutionQualityValuesParameterName)));
233        Results.Add(new Result(BestSolutionQualityParameterName, new DoubleValue()));
234        Results.Add(new Result(CurrentBestValidationQualityParameterName, new DoubleValue()));
235      }
236      Results[BestSolutionQualityParameterName].Value = new DoubleValue(BestSolutionQualityParameter.ActualValue.Value);
237      Results[CurrentBestValidationQualityParameterName].Value = new DoubleValue(bestQuality);
238
239      DataTable validationValues = (DataTable)Results[BestSolutionQualityValuesParameterName].Value;
240      AddValue(validationValues, BestSolutionQualityParameter.ActualValue.Value, BestSolutionQualityParameterName, BestSolutionQualityParameterName);
241      AddValue(validationValues, bestQuality, CurrentBestValidationQualityParameterName, CurrentBestValidationQualityParameterName);
242    }
243
244    private static void AddValue(DataTable table, double data, string name, string description) {
245      DataRow row;
246      table.Rows.TryGetValue(name, out row);
247      if (row == null) {
248        row = new DataRow(name, description);
249        row.Values.Add(data);
250        table.Rows.Add(row);
251      } else {
252        row.Values.Add(data);
253      }
254    }
255  }
256}
Note: See TracBrowser for help on using the repository browser.