Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2645_ContextAlgorithms/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer.cs @ 15899

Last change on this file since 15899 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 10.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
34  /// <summary>
35  /// An operator that analyzes the training best symbolic regression solution for multi objective symbolic regression problems.
36  /// </summary>
37  [Item("SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer", "An operator that analyzes the training best symbolic regression solution for multi objective symbolic regression problems.")]
38  [StorableClass]
39  public sealed class SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer : SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer<ISymbolicRegressionSolution>,
40    ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator {
41    private const string ProblemDataParameterName = "ProblemData";
42    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter";
43    private const string EstimationLimitsParameterName = "EstimationLimits";
44    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
45    private const string ValidationPartitionParameterName = "ValidationPartition";
46    private const string AnalyzeTestErrorParameterName = "Analyze Test Error";
47
48    #region parameter properties
49    public ILookupParameter<IRegressionProblemData> ProblemDataParameter {
50      get { return (ILookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
51    }
52    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
53      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
54    }
55    public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
56      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
57    }
58    public ILookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
59      get { return (ILookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
60    }
61
62    public IValueLookupParameter<IntRange> ValidationPartitionParameter {
63      get { return (IValueLookupParameter<IntRange>)Parameters[ValidationPartitionParameterName]; }
64    }
65
66    public IFixedValueParameter<BoolValue> AnalyzeTestErrorParameter {
67      get { return (IFixedValueParameter<BoolValue>)Parameters[AnalyzeTestErrorParameterName]; }
68    }
69    #endregion
70
71    public bool AnalyzeTestError {
72      get { return AnalyzeTestErrorParameter.Value.Value; }
73      set { AnalyzeTestErrorParameter.Value.Value = value; }
74    }
75
76    [StorableConstructor]
77    private SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
78    private SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer(SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
79    public SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer()
80      : base() {
81      Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName, "The problem data for the symbolic regression solution.") { Hidden = true });
82      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The symbolic data analysis tree interpreter for the symbolic expression tree.") { Hidden = true });
83      Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic regression model.") { Hidden = true });
84      Parameters.Add(new LookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "Maximal length of the symbolic expression.") { Hidden = true });
85      Parameters.Add(new ValueLookupParameter<IntRange>(ValidationPartitionParameterName, "The validation partition."));
86      Parameters.Add(new FixedValueParameter<BoolValue>(AnalyzeTestErrorParameterName, "Flag whether the test error should be displayed in the Pareto-Front", new BoolValue(false)));
87    }
88
89    [StorableHook(HookType.AfterDeserialization)]
90    private void AfterDeserialization() {
91      if (!Parameters.ContainsKey(MaximumSymbolicExpressionTreeLengthParameterName))
92        Parameters.Add(new LookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "Maximal length of the symbolic expression.") { Hidden = true });
93      if (!Parameters.ContainsKey(ValidationPartitionParameterName))
94        Parameters.Add(new ValueLookupParameter<IntRange>(ValidationPartitionParameterName, "The validation partition."));
95      if (!Parameters.ContainsKey(AnalyzeTestErrorParameterName))
96        Parameters.Add(new FixedValueParameter<BoolValue>(AnalyzeTestErrorParameterName, "Flag whether the test error should be displayed in the Pareto-Front", new BoolValue(false)));
97    }
98
99    public override IDeepCloneable Clone(Cloner cloner) {
100      return new SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer(this, cloner);
101    }
102
103    protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality) {
104      var model = new SymbolicRegressionModel(ProblemDataParameter.ActualValue.TargetVariable, (ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
105      if (ApplyLinearScalingParameter.ActualValue.Value) model.Scale(ProblemDataParameter.ActualValue);
106      return new SymbolicRegressionSolution(model, (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone());
107    }
108
109    public override IOperation Apply() {
110      var operation = base.Apply();
111      var paretoFront = TrainingBestSolutionsParameter.ActualValue;
112
113      IResult result;
114      ScatterPlot qualityToTreeSize;
115      if (!ResultCollection.TryGetValue("Pareto Front Analysis", out result)) {
116        qualityToTreeSize = new ScatterPlot("Quality vs Tree Size", "");
117        qualityToTreeSize.VisualProperties.XAxisMinimumAuto = false;
118        qualityToTreeSize.VisualProperties.XAxisMaximumAuto = false;
119        qualityToTreeSize.VisualProperties.YAxisMinimumAuto = false;
120        qualityToTreeSize.VisualProperties.YAxisMaximumAuto = false;
121
122        qualityToTreeSize.VisualProperties.XAxisMinimumFixedValue = 0;
123        qualityToTreeSize.VisualProperties.XAxisMaximumFixedValue = MaximumSymbolicExpressionTreeLengthParameter.ActualValue.Value;
124        qualityToTreeSize.VisualProperties.YAxisMinimumFixedValue = 0;
125        qualityToTreeSize.VisualProperties.YAxisMaximumFixedValue = 2;
126        ResultCollection.Add(new Result("Pareto Front Analysis", qualityToTreeSize));
127      } else {
128        qualityToTreeSize = (ScatterPlot)result.Value;
129      }
130
131
132      int previousTreeLength = -1;
133      var sizeParetoFront = new LinkedList<ISymbolicRegressionSolution>();
134      foreach (var solution in paretoFront.OrderBy(s => s.Model.SymbolicExpressionTree.Length)) {
135        int treeLength = solution.Model.SymbolicExpressionTree.Length;
136        if (!sizeParetoFront.Any()) sizeParetoFront.AddLast(solution);
137        if (solution.TrainingNormalizedMeanSquaredError < sizeParetoFront.Last.Value.TrainingNormalizedMeanSquaredError) {
138          if (treeLength == previousTreeLength)
139            sizeParetoFront.RemoveLast();
140          sizeParetoFront.AddLast(solution);
141        }
142        previousTreeLength = treeLength;
143      }
144
145      qualityToTreeSize.Rows.Clear();
146      var trainingRow = new ScatterPlotDataRow("Training NMSE", "", sizeParetoFront.Select(x => new Point2D<double>(x.Model.SymbolicExpressionTree.Length, x.TrainingNormalizedMeanSquaredError, x)));
147      trainingRow.VisualProperties.PointSize = 8;
148      qualityToTreeSize.Rows.Add(trainingRow);
149
150      if (AnalyzeTestError) {
151        var testRow = new ScatterPlotDataRow("Test NMSE", "",
152          sizeParetoFront.Select(x => new Point2D<double>(x.Model.SymbolicExpressionTree.Length, x.TestNormalizedMeanSquaredError, x)));
153        testRow.VisualProperties.PointSize = 8;
154        qualityToTreeSize.Rows.Add(testRow);
155      }
156
157      var validationPartition = ValidationPartitionParameter.ActualValue;
158      if (validationPartition.Size != 0) {
159        var problemData = ProblemDataParameter.ActualValue;
160        var validationIndizes = Enumerable.Range(validationPartition.Start, validationPartition.Size).ToList();
161        var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, validationIndizes).ToList();
162        OnlineCalculatorError error;
163        var validationRow = new ScatterPlotDataRow("Validation NMSE", "",
164          sizeParetoFront.Select(x => new Point2D<double>(x.Model.SymbolicExpressionTree.Length,
165          OnlineNormalizedMeanSquaredErrorCalculator.Calculate(targetValues, x.GetEstimatedValues(validationIndizes), out error))));
166        validationRow.VisualProperties.PointSize = 7;
167        qualityToTreeSize.Rows.Add(validationRow);
168      }
169
170      return operation;
171    }
172
173  }
174}
Note: See TracBrowser for help on using the repository browser.