Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5991 was 5863, checked in by mkommend, 14 years ago

#1418: Added NonDiscoverableType attribute to outdated analyzers.

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