Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/04/18 16:23:55 (6 years ago)
Author:
lkammere
Message:

#2886: Priorize phrases whose (fully expanded) terms result in high R².

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Analysis/RSquaredEvaluator.cs

    r15861 r15883  
    1313namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
    1414  public class RSquaredEvaluator : Item, IGrammarEnumerationAnalyzer {
    15     private readonly string BestTrainingQualityResultName = "Best R² (Training)";
    16     private readonly string BestTrainingModelResultName = "Best model (Training)";
    17     private readonly string BestTrainingSolutionResultName = "Best solution (Training)";
     15    public static readonly string BestTrainingQualityResultName = "Best R² (Training)";
     16    public static readonly string BestTrainingModelResultName = "Best model (Training)";
     17    public static readonly string BestTrainingSolutionResultName = "Best solution (Training)";
    1818
    19     private readonly ISymbolicDataAnalysisExpressionTreeInterpreter expressionTreeLinearInterpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
     19    private static readonly ISymbolicDataAnalysisExpressionTreeInterpreter expressionTreeLinearInterpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
    2020
    2121    public bool OptimizeConstants { get; set; }
     
    7373      Debug.Assert(SymbolicRegressionConstantOptimizationEvaluator.CanOptimizeConstants(tree));
    7474
    75       // TODO: Initialize constant values randomly
    76       // TODO: Restarts
     75      double r2 = Evaluate(problemData, tree, OptimizeConstants);
    7776
    78       double r2;
     77      var bestR2Result = (DoubleValue)algorithm.Results[BestTrainingQualityResultName].Value;
     78      bool better = r2 > bestR2Result.Value;
     79      bool equallyGood = r2.IsAlmost(bestR2Result.Value);
     80      bool shorter = false;
    7981
    80       SymbolicRegressionModel model = new SymbolicRegressionModel(
     82      if (!better && equallyGood) {
     83        shorter = algorithm.BestTrainingSentence != null &&
     84          algorithm.Grammar.GetComplexity(algorithm.BestTrainingSentence) > algorithm.Grammar.GetComplexity(symbolString);
     85      }
     86      if (better || (equallyGood && shorter)) {
     87        bestR2Result.Value = r2;
     88
     89        SymbolicRegressionModel model = new SymbolicRegressionModel(
    8190          problemData.TargetVariable,
    8291          tree,
    8392          expressionTreeLinearInterpreter);
    8493
    85       if (OptimizeConstants) {
     94        algorithm.Results.AddOrUpdateResult(BestTrainingModelResultName, model);
     95
     96        algorithm.BestTrainingSentence = symbolString;
     97      }
     98    }
     99
     100    public static double Evaluate(IRegressionProblemData problemData, SymbolicExpressionTree tree, bool optimizeConstants = true) {
     101      double r2;
     102
     103      // TODO: Initialize constant values randomly
     104      // TODO: Restarts
     105      if (optimizeConstants) {
    86106        r2 = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(expressionTreeLinearInterpreter,
    87107          tree,
     
    100120        }
    101121
    102 
    103122      } else {
    104123        var target = problemData.TargetVariableTrainingValues;
     124
     125        SymbolicRegressionModel model = new SymbolicRegressionModel(
     126          problemData.TargetVariable,
     127          tree,
     128          expressionTreeLinearInterpreter);
     129
    105130        var estVals = model.GetEstimatedValues(problemData.Dataset, problemData.TrainingIndices);
    106131        OnlineCalculatorError error;
     
    109134      }
    110135
    111       var bestR2Result = (DoubleValue)algorithm.Results[BestTrainingQualityResultName].Value;
    112       bool better = r2 > bestR2Result.Value;
    113       bool equallyGood = r2.IsAlmost(bestR2Result.Value);
    114       bool shorter = false;
    115 
    116       if (!better && equallyGood) {
    117         shorter = algorithm.BestTrainingSentence != null &&
    118           algorithm.Grammar.GetComplexity(algorithm.BestTrainingSentence) > algorithm.Grammar.GetComplexity(symbolString);
    119       }
    120       if (better || (equallyGood && shorter)) {
    121         bestR2Result.Value = r2;
    122         algorithm.Results.AddOrUpdateResult(BestTrainingModelResultName, model);
    123 
    124         algorithm.BestTrainingSentence = symbolString;
    125       }
     136      return r2;
    126137    }
    127138  }
Note: See TracChangeset for help on using the changeset viewer.