Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/02/21 17:23:10 (2 years ago)
Author:
dpiringe
Message:

#3136

  • refactor the evaluation logic of NMSESingleObjectiveConstraintsEvaluator
  • refactor the new method Evaluate for PearsonRSquaredAverageSimilarityEvaluator
  • change the parameter order of some evaluate/calculate methods
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionConstantOptimizationEvaluator.cs

    r18099 r18103  
    149149    private static readonly object locker = new object();
    150150    public override IOperation InstrumentedApply() {
    151       var solution = SymbolicExpressionTreeParameter.ActualValue;
     151      var tree = SymbolicExpressionTreeParameter.ActualValue;
    152152      double quality;
    153153      if (RandomParameter.ActualValue.NextDouble() < ConstantOptimizationProbability.Value) {
    154154        IEnumerable<int> constantOptimizationRows = GenerateRowsToEvaluate(ConstantOptimizationRowsPercentage.Value);
    155155        var counter = new EvaluationsCounter();
    156         quality = OptimizeConstants(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, ProblemDataParameter.ActualValue,
     156        quality = OptimizeConstants(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, ProblemDataParameter.ActualValue,
    157157           constantOptimizationRows, ApplyLinearScalingParameter.ActualValue.Value, ConstantOptimizationIterations.Value, updateVariableWeights: UpdateVariableWeights, lowerEstimationLimit: EstimationLimitsParameter.ActualValue.Lower, upperEstimationLimit: EstimationLimitsParameter.ActualValue.Upper, updateConstantsInTree: UpdateConstantsInTree, counter: counter);
    158158
    159159        if (ConstantOptimizationRowsPercentage.Value != RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value) {
    160160          var evaluationRows = GenerateRowsToEvaluate();
    161           quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, evaluationRows, ApplyLinearScalingParameter.ActualValue.Value);
     161          quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(
     162            tree, ProblemDataParameter.ActualValue,
     163            evaluationRows, SymbolicDataAnalysisTreeInterpreterParameter.ActualValue,
     164            ApplyLinearScalingParameter.ActualValue.Value,
     165            EstimationLimitsParameter.ActualValue.Lower,
     166            EstimationLimitsParameter.ActualValue.Upper);
    162167        }
    163168
     
    171176      } else {
    172177        var evaluationRows = GenerateRowsToEvaluate();
    173         quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, evaluationRows, ApplyLinearScalingParameter.ActualValue.Value);
     178        quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(
     179          tree, ProblemDataParameter.ActualValue,
     180          evaluationRows, SymbolicDataAnalysisTreeInterpreterParameter.ActualValue,
     181          ApplyLinearScalingParameter.ActualValue.Value,
     182          EstimationLimitsParameter.ActualValue.Lower,
     183          EstimationLimitsParameter.ActualValue.Upper);
    174184      }
    175185      QualityParameter.ActualValue = new DoubleValue(quality);
     
    178188    }
    179189
    180     public override double Evaluate(IRegressionProblemData problemData,
    181       ISymbolicExpressionTree solution,
     190    public override double Evaluate(
     191      ISymbolicExpressionTree tree,
     192      IRegressionProblemData problemData,
     193      IEnumerable<int> rows,
    182194      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
    183       IEnumerable<int> rows = null,
    184195      bool applyLinearScaling = true,
    185196      double lowerEstimationLimit = double.MinValue,
    186197      double upperEstimationLimit = double.MaxValue) {
    187198
    188 
    189       var random = RandomParameter?.Value ?? new MersenneTwister((uint)DateTime.Now.Millisecond);
    190       double quality;
     199      var random = RandomParameter.ActualValue;
     200      double quality = double.NaN;
    191201
    192202      var propability = random.NextDouble();
    193203      if (propability < ConstantOptimizationProbability.Value) {
    194         var counter = new EvaluationsCounter();
     204        IEnumerable<int> constantOptimizationRows = GenerateRowsToEvaluate(ConstantOptimizationRowsPercentage.Value);
    195205        quality = OptimizeConstants(
    196           interpreter,
    197           solution,
    198           problemData,
    199           rows ?? problemData.TrainingIndices,
    200           applyLinearScaling,
    201           ConstantOptimizationIterations.Value,
    202           updateVariableWeights: UpdateVariableWeights,
    203           lowerEstimationLimit: lowerEstimationLimit,
    204           upperEstimationLimit: upperEstimationLimit,
    205           updateConstantsInTree: UpdateConstantsInTree,
    206           counter: counter);
    207 
    208         if (ConstantOptimizationRowsPercentage.Value != RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value) { //TODO: remove this?
    209           var evaluationRows = GenerateRowsToEvaluate();
    210           quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(
    211             interpreter,
    212             solution,
    213             lowerEstimationLimit,
    214             upperEstimationLimit,
    215             problemData,
    216             evaluationRows,
    217             applyLinearScaling);
    218         }
    219 
    220       } else {
    221         var evaluationRows = GenerateRowsToEvaluate();
     206          interpreter, tree,
     207          problemData, constantOptimizationRows,
     208          applyLinearScaling,
     209          ConstantOptimizationIterations.Value,
     210          updateVariableWeights: UpdateVariableWeights,
     211          lowerEstimationLimit: lowerEstimationLimit,
     212          upperEstimationLimit: upperEstimationLimit,
     213          updateConstantsInTree: UpdateConstantsInTree);
     214      }
     215      if (double.IsNaN(quality) || ConstantOptimizationRowsPercentage.Value != RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value) {
    222216        quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(
    223           interpreter,
    224           solution,
     217          tree, problemData,
     218          rows, interpreter,
     219          applyLinearScaling,
    225220          lowerEstimationLimit,
    226           upperEstimationLimit,
    227           problemData,
    228           evaluationRows,
    229           applyLinearScaling);
     221          upperEstimationLimit);
    230222      }
    231223      return quality;
     
    242234      // because Evaluate() is used to get the quality of evolved models on
    243235      // different partitions of the dataset (e.g., best validation model)
    244       double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value);
     236      double r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(
     237        tree, problemData, rows,
     238        SymbolicDataAnalysisTreeInterpreterParameter.ActualValue,
     239        ApplyLinearScalingParameter.ActualValue.Value,
     240        EstimationLimitsParameter.ActualValue.Lower,
     241        EstimationLimitsParameter.ActualValue.Upper);
    245242
    246243      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
     
    290287      }
    291288
    292       double originalQuality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);
     289      double originalQuality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(
     290        tree, problemData, rows,
     291        interpreter, applyLinearScaling,
     292        lowerEstimationLimit,
     293        upperEstimationLimit);
    293294
    294295      if (counter == null) counter = new EvaluationsCounter();
     
    348349        } else UpdateConstants(tree, c, updateVariableWeights);
    349350      }
    350       var quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);
     351      var quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(
     352        tree, problemData, rows,
     353        interpreter, applyLinearScaling,
     354        lowerEstimationLimit, upperEstimationLimit);
    351355
    352356      if (!updateConstantsInTree) UpdateConstants(tree, initialConstants, updateVariableWeights);
Note: See TracChangeset for help on using the changeset viewer.