Changeset 17098
- Timestamp:
- 07/07/19 23:43:00 (5 years ago)
- Location:
- stable
- Files:
-
- 6 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/trunk merged: 16978,16981
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
/trunk/HeuristicLab.Problems.DataAnalysis.Symbolic merged: 16978,16981
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression
- Property svn:mergeinfo changed
/trunk/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression merged: 16978
- Property svn:mergeinfo changed
-
stable/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
-
stable/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/PearsonRSquaredAverageSimilarityEvaluator.cs
r17097 r17098 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 } -
stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r17097 r17098 176 176 <Compile Include="Interpreter\SymbolicDataAnalysisExpressionTreeBatchInterpreter.cs" /> 177 177 <Compile Include="Interpreter\SymbolicDataAnalysisExpressionTreeNativeInterpreter.cs" /> 178 <Compile Include="SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator.cs" /> 178 179 <Compile Include="SymbolicDataAnalysisExpressionTreeSimplificationOperator.cs" /> 179 180 <Compile Include="SymbolicDataAnalysisModelComplexityCalculator.cs" /> -
stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator.cs
r16978 r17098 28 28 using HeuristicLab.Operators; 29 29 using HeuristicLab.Parameters; 30 using HeuristicLab.PluginInfrastructure; 30 31 31 32 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 32 33 [StorableType("F30B6562-ECEC-42A0-B7D6-E55390FC0412")] 34 [NonDiscoverableType] 33 35 public class SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator : InstrumentedOperator { 34 36 private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
Note: See TracChangeset
for help on using the changeset viewer.