Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/19/20 19:07:40 (4 years ago)
Author:
fbaching
Message:

#1837: merged changes from trunk

  • apply changes from Attic release to all SlidingWindow specific code files (replace StorableClass with StorableType)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/1837_Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer.cs

    r9456 r17687  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2020#endregion
    2121
     22using System.Collections.Generic;
     23using System.Linq;
     24using HeuristicLab.Analysis;
    2225using HeuristicLab.Common;
    2326using HeuristicLab.Core;
     27using HeuristicLab.Data;
    2428using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     29using HeuristicLab.Optimization;
    2530using HeuristicLab.Parameters;
    26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     31using HEAL.Attic;
    2732
    2833namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
     
    3136  /// </summary>
    3237  [Item("SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer", "An operator that analyzes the training best symbolic regression solution for multi objective symbolic regression problems.")]
    33   [StorableClass]
     38  [StorableType("431B0046-3115-493F-BD15-E3DA98E3E9C7")]
    3439  public sealed class SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer : SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer<ISymbolicRegressionSolution>,
    3540    ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator {
     
    3742    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter";
    3843    private const string EstimationLimitsParameterName = "EstimationLimits";
     44    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
     45    private const string ValidationPartitionParameterName = "ValidationPartition";
     46    private const string AnalyzeTestErrorParameterName = "Analyze Test Error";
     47
    3948    #region parameter properties
    4049    public ILookupParameter<IRegressionProblemData> ProblemDataParameter {
     
    4756      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
    4857    }
     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    }
     65
     66    public IFixedValueParameter<BoolValue> AnalyzeTestErrorParameter {
     67      get { return (IFixedValueParameter<BoolValue>)Parameters[AnalyzeTestErrorParameterName]; }
     68    }
    4969    #endregion
    5070
     71    public bool AnalyzeTestError {
     72      get { return AnalyzeTestErrorParameter.Value.Value; }
     73      set { AnalyzeTestErrorParameter.Value.Value = value; }
     74    }
     75
    5176    [StorableConstructor]
    52     private SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
     77    private SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer(StorableConstructorFlag _) : base(_) { }
    5378    private SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer(SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
    5479    public SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer()
    5580      : base() {
    56       Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName, "The problem data for the symbolic regression solution."));
    57       Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The symbolic data analysis tree interpreter for the symbolic expression tree."));
    58       Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic regression model."));
     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."));
     86      Parameters.Add(new FixedValueParameter<BoolValue>(AnalyzeTestErrorParameterName, "Flag whether the test error should be displayed in the Pareto-Front", new BoolValue(false)));
     87    }
     88
     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."));
     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)));
    5997    }
    6098
     
    64102
    65103    protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality) {
    66       var model = new SymbolicRegressionModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
     104      var model = new SymbolicRegressionModel(ProblemDataParameter.ActualValue.TargetVariable, (ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
    67105      if (ApplyLinearScalingParameter.ActualValue.Value) model.Scale(ProblemDataParameter.ActualValue);
    68106      return new SymbolicRegressionSolution(model, (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone());
    69107    }
     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();
     146      var trainingRow = new ScatterPlotDataRow("Training NMSE", "", sizeParetoFront.Select(x => new Point2D<double>(x.Model.SymbolicExpressionTree.Length, x.TrainingNormalizedMeanSquaredError, x)));
     147      trainingRow.VisualProperties.PointSize = 8;
     148      qualityToTreeSize.Rows.Add(trainingRow);
     149
     150      if (AnalyzeTestError) {
     151        var testRow = new ScatterPlotDataRow("Test NMSE", "",
     152          sizeParetoFront.Select(x => new Point2D<double>(x.Model.SymbolicExpressionTree.Length, x.TestNormalizedMeanSquaredError, x)));
     153        testRow.VisualProperties.PointSize = 8;
     154        qualityToTreeSize.Rows.Add(testRow);
     155      }
     156
     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
    70173  }
    71174}
Note: See TracChangeset for help on using the changeset viewer.