Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer.cs @ 13310

Last change on this file since 13310 was 13310, checked in by mkommend, 8 years ago

#2175: Merged r13241, r13300, r13307, r13308 into stable.

File size: 9.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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
47    #region parameter properties
48    public ILookupParameter<IRegressionProblemData> ProblemDataParameter {
49      get { return (ILookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
50    }
51    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
52      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
53    }
54    public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
55      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
56    }
57    public ILookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
58      get { return (ILookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
59    }
60
61    public IValueLookupParameter<IntRange> ValidationPartitionParameter {
62      get { return (IValueLookupParameter<IntRange>)Parameters[ValidationPartitionParameterName]; }
63    }
64    #endregion
65
66    [StorableConstructor]
67    private SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
68    private SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer(SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
69    public SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer()
70      : base() {
71      Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName, "The problem data for the symbolic regression solution.") { Hidden = true });
72      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The symbolic data analysis tree interpreter for the symbolic expression tree.") { Hidden = true });
73      Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic regression model.") { Hidden = true });
74      Parameters.Add(new LookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "Maximal length of the symbolic expression.") { Hidden = true });
75      Parameters.Add(new ValueLookupParameter<IntRange>(ValidationPartitionParameterName, "The validation partition."));
76    }
77
78    [StorableHook(HookType.AfterDeserialization)]
79    private void AfterDeserialization() {
80      if (!Parameters.ContainsKey(MaximumSymbolicExpressionTreeLengthParameterName))
81        Parameters.Add(new LookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "Maximal length of the symbolic expression.") { Hidden = true });
82      if (!Parameters.ContainsKey(ValidationPartitionParameterName))
83        Parameters.Add(new ValueLookupParameter<IntRange>(ValidationPartitionParameterName, "The validation partition."));
84    }
85
86    public override IDeepCloneable Clone(Cloner cloner) {
87      return new SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer(this, cloner);
88    }
89
90    protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality) {
91      var model = new SymbolicRegressionModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
92      if (ApplyLinearScalingParameter.ActualValue.Value) model.Scale(ProblemDataParameter.ActualValue);
93      return new SymbolicRegressionSolution(model, (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone());
94    }
95
96    public override IOperation Apply() {
97      var operation = base.Apply();
98      var paretoFront = TrainingBestSolutionsParameter.ActualValue;
99
100      IResult result;
101      ScatterPlot qualityToTreeSize;
102      if (!ResultCollection.TryGetValue("Pareto Front Analysis", out result)) {
103        qualityToTreeSize = new ScatterPlot("Quality vs Tree Size", "");
104        qualityToTreeSize.VisualProperties.XAxisMinimumAuto = false;
105        qualityToTreeSize.VisualProperties.XAxisMaximumAuto = false;
106        qualityToTreeSize.VisualProperties.YAxisMinimumAuto = false;
107        qualityToTreeSize.VisualProperties.YAxisMaximumAuto = false;
108
109        qualityToTreeSize.VisualProperties.XAxisMinimumFixedValue = 0;
110        qualityToTreeSize.VisualProperties.XAxisMaximumFixedValue = MaximumSymbolicExpressionTreeLengthParameter.ActualValue.Value;
111        qualityToTreeSize.VisualProperties.YAxisMinimumFixedValue = 0;
112        qualityToTreeSize.VisualProperties.YAxisMaximumFixedValue = 2;
113        ResultCollection.Add(new Result("Pareto Front Analysis", qualityToTreeSize));
114      } else {
115        qualityToTreeSize = (ScatterPlot)result.Value;
116      }
117
118
119      int previousTreeLength = -1;
120      var sizeParetoFront = new LinkedList<ISymbolicRegressionSolution>();
121      foreach (var solution in paretoFront.OrderBy(s => s.Model.SymbolicExpressionTree.Length)) {
122        int treeLength = solution.Model.SymbolicExpressionTree.Length;
123        if (!sizeParetoFront.Any()) sizeParetoFront.AddLast(solution);
124        if (solution.TrainingNormalizedMeanSquaredError < sizeParetoFront.Last.Value.TrainingNormalizedMeanSquaredError) {
125          if (treeLength == previousTreeLength)
126            sizeParetoFront.RemoveLast();
127          sizeParetoFront.AddLast(solution);
128        }
129        previousTreeLength = treeLength;
130      }
131
132      qualityToTreeSize.Rows.Clear();
133      var trainingRow = new ScatterPlotDataRow("Training NMSE", "", sizeParetoFront.Select(x => new Point2D<double>(x.Model.SymbolicExpressionTree.Length, x.TrainingNormalizedMeanSquaredError)));
134      trainingRow.VisualProperties.PointSize = 8;
135      qualityToTreeSize.Rows.Add(trainingRow);
136
137      var validationPartition = ValidationPartitionParameter.ActualValue;
138      if (validationPartition.Size != 0) {
139        var problemData = ProblemDataParameter.ActualValue;
140        var validationIndizes = Enumerable.Range(validationPartition.Start, validationPartition.Size).ToList();
141        var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, validationIndizes).ToList();
142        OnlineCalculatorError error;
143        var validationRow = new ScatterPlotDataRow("Validation NMSE", "",
144          sizeParetoFront.Select(x => new Point2D<double>(x.Model.SymbolicExpressionTree.Length,
145          OnlineNormalizedMeanSquaredErrorCalculator.Calculate(targetValues, x.GetEstimatedValues(validationIndizes), out error))));
146        validationRow.VisualProperties.PointSize = 7;
147        qualityToTreeSize.Rows.Add(validationRow);
148      }
149
150      return operation;
151    }
152
153  }
154}
Note: See TracBrowser for help on using the repository browser.