Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer.cs @ 16628

Last change on this file since 16628 was 16628, checked in by gkronber, 5 years ago

#2971: made branch compile with current version of trunk

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