Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectiveTrainingBestSolutionAnalyzer.cs @ 17514

Last change on this file since 17514 was 17496, checked in by mkommend, 5 years ago

#3019: Merged r17450 into stable.

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