Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/25/19 23:14:06 (5 years ago)
Author:
mkommend
Message:

#2435: Updated branch with most recent trunk changes.

Location:
branches/2435-alglib_3_15
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/2435-alglib_3_15

  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression

  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4

  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/PearsonRSquaredAverageSimilarityEvaluator.cs

    r16565 r17034  
    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  }
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionConstantOptimizationEvaluator.cs

    r16565 r17034  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using HEAL.Attic;
    2526using HeuristicLab.Common;
    2627using HeuristicLab.Core;
     
    2930using HeuristicLab.Optimization;
    3031using HeuristicLab.Parameters;
    31 using HEAL.Attic;
    3232
    3333namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
     
    114114    public SymbolicRegressionConstantOptimizationEvaluator()
    115115      : base() {
    116       Parameters.Add(new FixedValueParameter<IntValue>(ConstantOptimizationIterationsParameterName, "Determines how many iterations should be calculated while optimizing the constant of a symbolic expression tree (0 indicates other or default stopping criterion).", new IntValue(10), true));
    117       Parameters.Add(new FixedValueParameter<DoubleValue>(ConstantOptimizationImprovementParameterName, "Determines the relative improvement which must be achieved in the constant optimization to continue with it (0 indicates other or default stopping criterion).", new DoubleValue(0), true) { Hidden = true });
    118       Parameters.Add(new FixedValueParameter<PercentValue>(ConstantOptimizationProbabilityParameterName, "Determines the probability that the constants are optimized", new PercentValue(1), true));
    119       Parameters.Add(new FixedValueParameter<PercentValue>(ConstantOptimizationRowsPercentageParameterName, "Determines the percentage of the rows which should be used for constant optimization", new PercentValue(1), true));
     116      Parameters.Add(new FixedValueParameter<IntValue>(ConstantOptimizationIterationsParameterName, "Determines how many iterations should be calculated while optimizing the constant of a symbolic expression tree (0 indicates other or default stopping criterion).", new IntValue(10)));
     117      Parameters.Add(new FixedValueParameter<DoubleValue>(ConstantOptimizationImprovementParameterName, "Determines the relative improvement which must be achieved in the constant optimization to continue with it (0 indicates other or default stopping criterion).", new DoubleValue(0)) { Hidden = true });
     118      Parameters.Add(new FixedValueParameter<PercentValue>(ConstantOptimizationProbabilityParameterName, "Determines the probability that the constants are optimized", new PercentValue(1)));
     119      Parameters.Add(new FixedValueParameter<PercentValue>(ConstantOptimizationRowsPercentageParameterName, "Determines the percentage of the rows which should be used for constant optimization", new PercentValue(1)));
    120120      Parameters.Add(new FixedValueParameter<BoolValue>(UpdateConstantsInTreeParameterName, "Determines if the constants in the tree should be overwritten by the optimized constants.", new BoolValue(true)) { Hidden = true });
    121121      Parameters.Add(new FixedValueParameter<BoolValue>(UpdateVariableWeightsParameterName, "Determines if the variable weights in the tree should be  optimized.", new BoolValue(true)) { Hidden = true });
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionModel.cs

    r16565 r17034  
    9090      var regressionProblemData = problemData as IRegressionProblemData;
    9191      if (regressionProblemData == null)
    92         throw new ArgumentException("The problem data is not a regression problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
     92        throw new ArgumentException("The problem data is not compatible with this symbolic regression model. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
    9393      return IsProblemDataCompatible(regressionProblemData, out errorMessage);
    9494    }
Note: See TracChangeset for help on using the changeset viewer.