Changeset 17687 for branches/1837_Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer.cs
- Timestamp:
- 07/19/20 19:07:40 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/1837_Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer.cs
r9456 r17687 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-2013Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 20 20 #endregion 21 21 22 using System.Collections.Generic; 23 using System.Linq; 24 using HeuristicLab.Analysis; 22 25 using HeuristicLab.Common; 23 26 using HeuristicLab.Core; 27 using HeuristicLab.Data; 24 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 29 using HeuristicLab.Optimization; 25 30 using HeuristicLab.Parameters; 26 using H euristicLab.Persistence.Default.CompositeSerializers.Storable;31 using HEAL.Attic; 27 32 28 33 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression { … … 31 36 /// </summary> 32 37 [Item("SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer", "An operator that analyzes the training best symbolic regression solution for multi objective symbolic regression problems.")] 33 [Storable Class]38 [StorableType("431B0046-3115-493F-BD15-E3DA98E3E9C7")] 34 39 public sealed class SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer : SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer<ISymbolicRegressionSolution>, 35 40 ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator { … … 37 42 private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter"; 38 43 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 39 48 #region parameter properties 40 49 public ILookupParameter<IRegressionProblemData> ProblemDataParameter { … … 47 56 get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; } 48 57 } 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 } 49 69 #endregion 50 70 71 public bool AnalyzeTestError { 72 get { return AnalyzeTestErrorParameter.Value.Value; } 73 set { AnalyzeTestErrorParameter.Value.Value = value; } 74 } 75 51 76 [StorableConstructor] 52 private SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer( bool deserializing) : base(deserializing) { }77 private SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer(StorableConstructorFlag _) : base(_) { } 53 78 private SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer(SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { } 54 79 public SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer() 55 80 : 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))); 59 97 } 60 98 … … 64 102 65 103 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); 67 105 if (ApplyLinearScalingParameter.ActualValue.Value) model.Scale(ProblemDataParameter.ActualValue); 68 106 return new SymbolicRegressionSolution(model, (IRegressionProblemData)ProblemDataParameter.ActualValue.Clone()); 69 107 } 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 70 173 } 71 174 }
Note: See TracChangeset
for help on using the changeset viewer.