Changeset 17005 for branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression
- Timestamp:
- 06/11/19 20:59:44 (5 years ago)
- Location:
- branches/2994-AutoDiffForIntervals
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2994-AutoDiffForIntervals
- Property svn:mergeinfo changed
/trunk merged: 16944-16945,16978-16983,16992,16997
- Property svn:mergeinfo changed
-
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression
- Property svn:mergeinfo changed
/trunk/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression merged: 16978
- Property svn:mergeinfo changed
-
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4
- Property svn:mergeinfo changed
/trunk/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4 merged: 16978
- Property svn:mergeinfo changed
-
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/PearsonRSquaredAverageSimilarityEvaluator.cs
r16565 r17005 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Diagnostics; 25 using System.Linq; 24 using HEAL.Attic; 26 25 using HeuristicLab.Common; 27 26 using HeuristicLab.Core; … … 29 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 30 29 using HeuristicLab.Parameters; 31 using HEAL.Attic;32 30 33 31 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression { … … 36 34 public class PearsonRSquaredAverageSimilarityEvaluator : SymbolicRegressionMultiObjectiveEvaluator { 37 35 private const string StrictSimilarityParameterName = "StrictSimilarity"; 36 private const string AverageSimilarityParameterName = "AverageSimilarity"; 38 37 39 38 private readonly object locker = new object(); 40 39 40 private readonly SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator SimilarityCalculator = new SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator(); 41 41 42 public IFixedValueParameter<BoolValue> StrictSimilarityParameter { 42 43 get { return (IFixedValueParameter<BoolValue>)Parameters[StrictSimilarityParameterName]; } 44 } 45 46 public ILookupParameter<DoubleValue> AverageSimilarityParameter { 47 get { return (ILookupParameter<DoubleValue>)Parameters[AverageSimilarityParameterName]; } 43 48 } 44 49 … … 58 63 public PearsonRSquaredAverageSimilarityEvaluator() : base() { 59 64 Parameters.Add(new FixedValueParameter<BoolValue>(StrictSimilarityParameterName, "Use strict similarity calculation.", new BoolValue(false))); 65 Parameters.Add(new LookupParameter<DoubleValue>(AverageSimilarityParameterName)); 60 66 } 61 67 62 public override IEnumerable<bool> Maximization { get { return new bool[2] { true, false }; } } // maximize R² and minimize model complexity68 public override IEnumerable<bool> Maximization { get { return new bool[2] { true, false }; } } // maximize R² and minimize average similarity 63 69 64 70 public override IOperation InstrumentedApply() { … … 73 79 SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, solution, problemData, rows, applyLinearScaling, ConstantOptimizationIterations, updateVariableWeights: ConstantOptimizationUpdateVariableWeights, lowerEstimationLimit: estimationLimits.Lower, upperEstimationLimit: estimationLimits.Upper); 74 80 } 75 double[] qualities = Calculate(interpreter, solution, estimationLimits.Lower, estimationLimits.Upper, problemData, rows, applyLinearScaling, DecimalPlaces);76 QualitiesParameter.ActualValue = new DoubleArray(qualities);77 return base.InstrumentedApply();78 }79 81 80 public double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling, int decimalPlaces) { 81 double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling); 82 if (decimalPlaces >= 0) 83 r2 = Math.Round(r2, decimalPlaces); 82 double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, solution, estimationLimits.Lower, estimationLimits.Upper, problemData, rows, applyLinearScaling); 84 83 85 var variables = ExecutionContext.Scope.Variables; 86 if (!variables.ContainsKey("AverageSimilarity")) { 87 lock (locker) { 88 CalculateAverageSimilarities(ExecutionContext.Scope.Parent.SubScopes.Where(x => x.Variables.ContainsKey("SymbolicExpressionTree")).ToArray(), StrictSimilarity); 84 if (DecimalPlaces >= 0) 85 r2 = Math.Round(r2, DecimalPlaces); 89 86 87 lock (locker) { 88 if (AverageSimilarityParameter.ActualValue == null) { 89 var context = new ExecutionContext(null, SimilarityCalculator, ExecutionContext.Scope.Parent); 90 SimilarityCalculator.StrictSimilarity = StrictSimilarity; 91 SimilarityCalculator.Execute(context, CancellationToken); 90 92 } 91 93 } 94 var avgSimilarity = AverageSimilarityParameter.ActualValue.Value; 92 95 93 double avgSim = ((DoubleValue)variables["AverageSimilarity"].Value).Value;94 return new double[2] { r2, avgSim };96 QualitiesParameter.ActualValue = new DoubleArray(new[] { r2, avgSimilarity }); 97 return base.InstrumentedApply(); 95 98 } 96 99 97 100 public override double[] Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows) { 98 101 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context; 102 AverageSimilarityParameter.ExecutionContext = context; 99 103 EstimationLimitsParameter.ExecutionContext = context; 100 104 ApplyLinearScalingParameter.ExecutionContext = context; 101 // DecimalPlaces parameter is a FixedValueParameter and doesn't need the context.102 105 103 double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value, DecimalPlaces); 106 var estimationLimits = EstimationLimitsParameter.ActualValue; 107 var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value; 108 109 double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, estimationLimits.Lower, estimationLimits.Upper, problemData, rows, applyLinearScaling); 110 111 lock (locker) { 112 if (AverageSimilarityParameter.ActualValue == null) { 113 var ctx = new ExecutionContext(null, SimilarityCalculator, context.Scope.Parent); 114 SimilarityCalculator.StrictSimilarity = StrictSimilarity; 115 SimilarityCalculator.Execute(context, CancellationToken); 116 } 117 } 118 var avgSimilarity = AverageSimilarityParameter.ActualValue.Value; 104 119 105 120 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null; … … 107 122 ApplyLinearScalingParameter.ExecutionContext = null; 108 123 109 return quality; 110 } 111 112 private readonly Stopwatch sw = new Stopwatch(); 113 public void CalculateAverageSimilarities(IScope[] treeScopes, bool strict) { 114 var trees = treeScopes.Select(x => (ISymbolicExpressionTree)x.Variables["SymbolicExpressionTree"].Value).ToArray(); 115 var similarityMatrix = SymbolicExpressionTreeHash.ComputeSimilarityMatrix(trees, simplify: false, strict: strict); 116 117 for (int i = 0; i < treeScopes.Length; ++i) { 118 var scope = treeScopes[i]; 119 var avgSimilarity = 0d; 120 for (int j = 0; j < trees.Length; ++j) { 121 if (i == j) continue; 122 avgSimilarity += similarityMatrix[i, j]; 123 } 124 avgSimilarity /= trees.Length - 1; 125 126 if (scope.Variables.ContainsKey("AverageSimilarity")) { 127 ((DoubleValue)scope.Variables["AverageSimilarity"].Value).Value = avgSimilarity; 128 } else { 129 scope.Variables.Add(new Core.Variable("AverageSimilarity", new DoubleValue(avgSimilarity))); 130 } 131 } 124 return new[] { r2, avgSimilarity }; 132 125 } 133 126 }
Note: See TracChangeset
for help on using the changeset viewer.