Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/07/19 23:43:00 (5 years ago)
Author:
mkommend
Message:

#2977: Merged r16978, r16981 into stable.

Location:
stable
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression

  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4

  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/PearsonRSquaredAverageSimilarityEvaluator.cs

    r17097 r17098  
    2222using System;
    2323using System.Collections.Generic;
    24 using System.Diagnostics;
    25 using System.Linq;
     24using HEAL.Attic;
    2625using HeuristicLab.Common;
    2726using HeuristicLab.Core;
     
    2928using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    3029using HeuristicLab.Parameters;
    31 using HEAL.Attic;
    3230
    3331namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
     
    3634  public class PearsonRSquaredAverageSimilarityEvaluator : SymbolicRegressionMultiObjectiveEvaluator {
    3735    private const string StrictSimilarityParameterName = "StrictSimilarity";
     36    private const string AverageSimilarityParameterName = "AverageSimilarity";
    3837
    3938    private readonly object locker = new object();
    4039
     40    private readonly SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator SimilarityCalculator = new SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator();
     41
    4142    public IFixedValueParameter<BoolValue> StrictSimilarityParameter {
    4243      get { return (IFixedValueParameter<BoolValue>)Parameters[StrictSimilarityParameterName]; }
     44    }
     45
     46    public ILookupParameter<DoubleValue> AverageSimilarityParameter {
     47      get { return (ILookupParameter<DoubleValue>)Parameters[AverageSimilarityParameterName]; }
    4348    }
    4449
     
    5863    public PearsonRSquaredAverageSimilarityEvaluator() : base() {
    5964      Parameters.Add(new FixedValueParameter<BoolValue>(StrictSimilarityParameterName, "Use strict similarity calculation.", new BoolValue(false)));
     65      Parameters.Add(new LookupParameter<DoubleValue>(AverageSimilarityParameterName));
    6066    }
    6167
    62     public override IEnumerable<bool> Maximization { get { return new bool[2] { true, false }; } } // maximize R² and minimize model complexity
     68    public override IEnumerable<bool> Maximization { get { return new bool[2] { true, false }; } } // maximize R² and minimize average similarity
    6369
    6470    public override IOperation InstrumentedApply() {
     
    7379        SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, solution, problemData, rows, applyLinearScaling, ConstantOptimizationIterations, updateVariableWeights: ConstantOptimizationUpdateVariableWeights, lowerEstimationLimit: estimationLimits.Lower, upperEstimationLimit: estimationLimits.Upper);
    7480      }
    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     }
    7981
    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);
    8483
    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);
    8986
     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);
    9092        }
    9193      }
     94      var avgSimilarity = AverageSimilarityParameter.ActualValue.Value;
    9295
    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();
    9598    }
    9699
    97100    public override double[] Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows) {
    98101      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
     102      AverageSimilarityParameter.ExecutionContext = context;
    99103      EstimationLimitsParameter.ExecutionContext = context;
    100104      ApplyLinearScalingParameter.ExecutionContext = context;
    101       // DecimalPlaces parameter is a FixedValueParameter and doesn't need the context.
    102105
    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;
    104119
    105120      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
     
    107122      ApplyLinearScalingParameter.ExecutionContext = null;
    108123
    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 };
    132125    }
    133126  }
Note: See TracChangeset for help on using the changeset viewer.