Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis.ComplexityAnalyzer/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer.cs @ 13176

Last change on this file since 13176 was 12130, checked in by mkommend, 10 years ago

#2175: Updated complexity branch with trunk changes.

File size: 10.2 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
46    #region parameter properties
47    public ILookupParameter<IRegressionProblemData> ProblemDataParameter {
48      get { return (ILookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
49    }
50    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
51      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
52    }
53    public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
54      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
55    }
56    public ILookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
57      get { return (ILookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
58    }
59    #endregion
60
61    [StorableConstructor]
62    private SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
63    private SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer(SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
64    public SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer()
65      : base() {
66      Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName, "The problem data for the symbolic regression solution."));
67      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The symbolic data analysis tree interpreter for the symbolic expression tree."));
68      Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic regression model."));
69      Parameters.Add(new LookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "Maximal length of the symbolic expression."));
70    }
71
72    [StorableHook(HookType.AfterDeserialization)]
73    private void AfterDeserialization() {
74      if (!Parameters.ContainsKey(MaximumSymbolicExpressionTreeLengthParameterName))
75        Parameters.Add(new LookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "Maximal length of the symbolic expression."));
76    }
77
78    public override IDeepCloneable Clone(Cloner cloner) {
79      return new SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer(this, cloner);
80    }
81
82    protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality) {
83      var model = new SymbolicRegressionModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
84      if (ApplyLinearScalingParameter.ActualValue.Value) model.Scale(ProblemDataParameter.ActualValue);
85      return new SymbolicRegressionSolution(model, (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone());
86    }
87
88    public override IOperation Apply() {
89      var operation = base.Apply();
90
91      var paretoFront = TrainingBestSolutionsParameter.ActualValue;
92
93      IResult result;
94      ScatterPlot qualityToTreeSize;
95      if (!ResultCollection.TryGetValue("Pareto Front Analysis", out result)) {
96        qualityToTreeSize = new ScatterPlot("Quality vs Tree Size", "");
97        qualityToTreeSize.VisualProperties.XAxisMinimumAuto = false;
98        qualityToTreeSize.VisualProperties.XAxisMaximumAuto = false;
99        qualityToTreeSize.VisualProperties.YAxisMinimumAuto = false;
100        qualityToTreeSize.VisualProperties.YAxisMaximumAuto = false;
101
102        qualityToTreeSize.VisualProperties.XAxisMinimumFixedValue = 0;
103        qualityToTreeSize.VisualProperties.XAxisMaximumFixedValue = MaximumSymbolicExpressionTreeLengthParameter.ActualValue.Value;
104        qualityToTreeSize.VisualProperties.YAxisMinimumFixedValue = 0;
105        qualityToTreeSize.VisualProperties.YAxisMaximumFixedValue = 2;
106        ResultCollection.Add(new Result("Pareto Front Analysis", qualityToTreeSize));
107      } else {
108        qualityToTreeSize = (ScatterPlot)result.Value;
109      }
110
111
112      int previousTreeLength = -1;
113      var sizeParetoFront = new LinkedList<ISymbolicRegressionSolution>();
114      foreach (var solution in paretoFront.OrderBy(s => s.Model.SymbolicExpressionTree.Length)) {
115        int treeLength = solution.Model.SymbolicExpressionTree.Length;
116        if (!sizeParetoFront.Any()) sizeParetoFront.AddLast(solution);
117        if (solution.TrainingNormalizedMeanSquaredError < sizeParetoFront.Last.Value.TrainingNormalizedMeanSquaredError) {
118          if (treeLength == previousTreeLength)
119            sizeParetoFront.RemoveLast();
120          sizeParetoFront.AddLast(solution);
121        }
122        previousTreeLength = treeLength;
123      }
124
125      qualityToTreeSize.Rows.Clear();
126      var trainingRow = new ScatterPlotDataRow("Training NMSE", "", sizeParetoFront.Select(x => new Point2D<double>(x.Model.SymbolicExpressionTree.Length, x.TrainingNormalizedMeanSquaredError)));
127      trainingRow.VisualProperties.PointSize = 5;
128      var testRow = new ScatterPlotDataRow("Test NMSE", "", sizeParetoFront.Select(x => new Point2D<double>(x.Model.SymbolicExpressionTree.Length, x.TestNormalizedMeanSquaredError)));
129      testRow.VisualProperties.PointSize = 5;
130      qualityToTreeSize.Rows.Add(trainingRow);
131      qualityToTreeSize.Rows.Add(testRow);
132
133      double trainingArea = sizeParetoFront.Select(s => s.Model.SymbolicExpressionTree.Length * s.TrainingNormalizedMeanSquaredError).Average();
134      double testArea = sizeParetoFront.Select(s => s.Model.SymbolicExpressionTree.Length * s.TestNormalizedMeanSquaredError).Average();
135
136      ResultCollection paretoFrontResults;
137      if (!ResultCollection.TryGetValue("Pareto Front Results", out result)) {
138        paretoFrontResults = new ResultCollection();
139        ResultCollection.Add(new Result("Pareto Front Results", paretoFrontResults));
140      } else paretoFrontResults = (ResultCollection)result.Value;
141
142      DoubleValue trainingAreaResult, testAreaResult, areaDifferenceResult, avgTrainingNMSE, avgTestNMSE;
143      if (!paretoFrontResults.TryGetValue("Non Dominated Area (training)", out result)) {
144        trainingAreaResult = new DoubleValue();
145        paretoFrontResults.Add(new Result("Non Dominated Area (training)", trainingAreaResult));
146      } else trainingAreaResult = (DoubleValue)result.Value;
147      if (!paretoFrontResults.TryGetValue("Non Dominated Area (test)", out result)) {
148        testAreaResult = new DoubleValue();
149        paretoFrontResults.Add(new Result("Non Dominated Area (test)", testAreaResult));
150      } else testAreaResult = (DoubleValue)result.Value;
151      if (!paretoFrontResults.TryGetValue("Non Dominated Area Difference", out result)) {
152        areaDifferenceResult = new DoubleValue();
153        paretoFrontResults.Add(new Result("Non Dominated Area Difference", areaDifferenceResult));
154      } else areaDifferenceResult = (DoubleValue)result.Value;
155      if (!paretoFrontResults.TryGetValue("Average Training NMSE", out result)) {
156        avgTrainingNMSE = new DoubleValue();
157        paretoFrontResults.Add(new Result("Average Training NMSE", avgTrainingNMSE));
158      } else avgTrainingNMSE = (DoubleValue)result.Value;
159      if (!paretoFrontResults.TryGetValue("Average Test NMSE", out result)) {
160        avgTestNMSE = new DoubleValue();
161        paretoFrontResults.Add(new Result("Average Test NMSE", avgTestNMSE));
162      } else avgTestNMSE = (DoubleValue)result.Value;
163
164      trainingAreaResult.Value = trainingArea;
165      testAreaResult.Value = testArea;
166      areaDifferenceResult.Value = trainingArea - testArea;
167      avgTrainingNMSE.Value = sizeParetoFront.Select(s => s.TrainingNormalizedMeanSquaredError).Average();
168      avgTestNMSE.Value = sizeParetoFront.Select(s => s.TestNormalizedMeanSquaredError).Average();
169
170      return operation;
171    }
172
173  }
174}
Note: See TracBrowser for help on using the repository browser.