Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer.cs @ 16232

Last change on this file since 16232 was 16057, checked in by jkarder, 6 years ago

#2839:

File size: 10.3 KB
RevLine 
[5557]1#region License Information
2/* HeuristicLab
[16057]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5557]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
[13241]22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Analysis;
[5557]25using HeuristicLab.Common;
26using HeuristicLab.Core;
[13241]27using HeuristicLab.Data;
[5557]28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[13241]29using HeuristicLab.Optimization;
[5557]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]
[5685]39  public sealed class SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer : SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer<ISymbolicRegressionSolution>,
[5747]40    ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator {
[5685]41    private const string ProblemDataParameterName = "ProblemData";
42    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter";
[5770]43    private const string EstimationLimitsParameterName = "EstimationLimits";
[13241]44    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
45    private const string ValidationPartitionParameterName = "ValidationPartition";
[14718]46    private const string AnalyzeTestErrorParameterName = "Analyze Test Error";
[13241]47
[5685]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    }
[5770]55    public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
56      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
[5720]57    }
[13241]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    }
[14718]65
66    public IFixedValueParameter<BoolValue> AnalyzeTestErrorParameter {
67      get { return (IFixedValueParameter<BoolValue>)Parameters[AnalyzeTestErrorParameterName]; }
68    }
[5685]69    #endregion
70
[14718]71    public bool AnalyzeTestError {
72      get { return AnalyzeTestErrorParameter.Value.Value; }
73      set { AnalyzeTestErrorParameter.Value.Value = value; }
74    }
75
[5557]76    [StorableConstructor]
77    private SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
78    private SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer(SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
79    public SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer()
80      : base() {
[13241]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."));
[14718]86      Parameters.Add(new FixedValueParameter<BoolValue>(AnalyzeTestErrorParameterName, "Flag whether the test error should be displayed in the Pareto-Front", new BoolValue(false)));
[5557]87    }
[5685]88
[13241]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."));
[14718]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)));
[13241]97    }
98
[5557]99    public override IDeepCloneable Clone(Cloner cloner) {
100      return new SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer(this, cloner);
101    }
102
103    protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality) {
[13941]104      var model = new SymbolicRegressionModel(ProblemDataParameter.ActualValue.TargetVariable, (ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
[8972]105      if (ApplyLinearScalingParameter.ActualValue.Value) model.Scale(ProblemDataParameter.ActualValue);
[5914]106      return new SymbolicRegressionSolution(model, (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone());
[5557]107    }
[13241]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();
[14861]146      var trainingRow = new ScatterPlotDataRow("Training NMSE", "", sizeParetoFront.Select(x => new Point2D<double>(x.Model.SymbolicExpressionTree.Length, x.TrainingNormalizedMeanSquaredError, x)));
[13241]147      trainingRow.VisualProperties.PointSize = 8;
148      qualityToTreeSize.Rows.Add(trainingRow);
149
[14718]150      if (AnalyzeTestError) {
151        var testRow = new ScatterPlotDataRow("Test NMSE", "",
[14861]152          sizeParetoFront.Select(x => new Point2D<double>(x.Model.SymbolicExpressionTree.Length, x.TestNormalizedMeanSquaredError, x)));
[14718]153        testRow.VisualProperties.PointSize = 8;
154        qualityToTreeSize.Rows.Add(testRow);
155      }
156
[13241]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
[5557]173  }
174}
Note: See TracBrowser for help on using the repository browser.