Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/28/18 11:35:13 (6 years ago)
Author:
bburlacu
Message:

#2886: implement LRU cache for storing search nodes, introduce SortedSet for handling priorities, fix serialization and cloning

File:
1 edited

Legend:

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

    r15949 r15974  
    1616  public class RSquaredEvaluator : Item, IGrammarEnumerationAnalyzer {
    1717    public static readonly string BestTrainingQualityResultName = "Best R² (Training)";
     18    public static readonly string BestTestQualityResultName = "Best R² (Test)";
    1819    public static readonly string BestTrainingModelResultName = "Best model (Training)";
    1920    public static readonly string BestTrainingSolutionResultName = "Best solution (Training)";
     21    public static readonly string BestComplexityResultName = "Best solution complexity";
    2022
    2123    private static readonly ISymbolicDataAnalysisExpressionTreeInterpreter expressionTreeLinearInterpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
     
    4244      algorithm.Started -= OnStarted;
    4345      algorithm.Stopped -= OnStopped;
    44 
    4546      algorithm.DistinctSentenceGenerated -= AlgorithmOnDistinctSentenceGenerated;
    4647    }
     
    5758    }
    5859
    59     private void OnStopped(object sender, EventArgs eventArgs) {
    60       GrammarEnumerationAlgorithm algorithm = (GrammarEnumerationAlgorithm)sender;
    61       if (algorithm.Results.ContainsKey(BestTrainingModelResultName)) {
    62         SymbolicRegressionModel model = (SymbolicRegressionModel)algorithm.Results[BestTrainingModelResultName].Value;
    63         IRegressionSolution bestTrainingSolution = new RegressionSolution(model, algorithm.Problem.ProblemData);
     60    private void OnStopped(object sender, EventArgs eventArgs) { }
    6461
    65         algorithm.Results.AddOrUpdateResult(BestTrainingSolutionResultName, bestTrainingSolution);
    66       }
     62    private T GetValue<T>(IItem value) where T : struct {
     63      var v = value as ValueTypeValue<T>;
     64      if (v == null)
     65        throw new ArgumentException(string.Format("Item is not of type {0}", typeof(ValueTypeValue<T>)));
     66      return v.Value;
    6767    }
    6868
    6969    private void EvaluateSentence(GrammarEnumerationAlgorithm algorithm, SymbolString symbolString) {
     70      var results = algorithm.Results;
     71      var grammar = algorithm.Grammar;
    7072      var problemData = algorithm.Problem.ProblemData;
    7173
     
    7476
    7577      double r2 = Evaluate(problemData, tree, OptimizeConstants);
     78      double bestR2 = results.ContainsKey(BestTrainingQualityResultName) ? GetValue<double>(results[BestTrainingQualityResultName].Value) : 0.0;
     79      if (r2 < bestR2)
     80        return;
    7681
    77       double bestR2 = 0.0;
    78       if (algorithm.Results.ContainsKey(BestTrainingQualityResultName))
    79         bestR2 = ((DoubleValue)algorithm.Results[BestTrainingQualityResultName].Value).Value;
    80       bool better = r2 > bestR2;
    81       bool equallyGood = r2.IsAlmost(bestR2);
    82       bool shorter = false;
     82      var bestComplexity = int.MaxValue;
     83      if (results.ContainsKey(BestComplexityResultName)) {
     84        bestComplexity = GetValue<int>(results[BestComplexityResultName].Value);
     85      } else if (algorithm.BestTrainingSentence != null) {
     86        bestComplexity = grammar.GetComplexity(algorithm.BestTrainingSentence);
     87        results.AddOrUpdateResult(BestComplexityResultName, new IntValue(bestComplexity));
     88      }
     89      var complexity = grammar.GetComplexity(symbolString);
    8390
    84       if (!better && equallyGood) {
    85         shorter = algorithm.BestTrainingSentence != null &&
    86           algorithm.Grammar.GetComplexity(algorithm.BestTrainingSentence) > algorithm.Grammar.GetComplexity(symbolString);
    87       }
    88       if (better || (equallyGood && shorter)) {
    89         algorithm.Results.AddOrUpdateResult(BestTrainingQualityResultName, new DoubleValue(r2));
    90 
    91         SymbolicRegressionModel model = new SymbolicRegressionModel(
    92           problemData.TargetVariable,
    93           tree,
    94           expressionTreeLinearInterpreter);
    95 
    96         algorithm.Results.AddOrUpdateResult(BestTrainingModelResultName, model);
    97 
     91      if (r2 > bestR2 || (r2.IsAlmost(bestR2) && complexity < bestComplexity)) {
     92        results.AddOrUpdateResult(BestTrainingQualityResultName, new DoubleValue(r2));
     93        results.AddOrUpdateResult(BestComplexityResultName, new IntValue(complexity));
    9894        algorithm.BestTrainingSentence = symbolString;
    9995      }
     
    121117          }
    122118        }
    123 
    124         r2 = double.IsNaN(r2) ? 0.0 : r2;
    125 
    126119      } else {
    127         var target = problemData.TargetVariableTrainingValues;
    128 
    129         SymbolicRegressionModel model = new SymbolicRegressionModel(
    130           problemData.TargetVariable,
     120        r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(expressionTreeLinearInterpreter,
    131121          tree,
    132           expressionTreeLinearInterpreter);
    133 
    134         var estVals = model.GetEstimatedValues(problemData.Dataset, problemData.TrainingIndices);
    135         OnlineCalculatorError error;
    136         r2 = OnlinePearsonsRCalculator.Calculate(target, estVals, out error);
    137         if (error != OnlineCalculatorError.None) r2 = 0.0;
     122          double.MinValue,
     123          double.MaxValue,
     124          problemData,
     125          problemData.TrainingIndices,
     126          applyLinearScaling: true);
    138127      }
    139 
    140       return r2;
     128      return double.IsNaN(r2) ? 0.0 : r2;
    141129    }
    142130  }
Note: See TracChangeset for help on using the changeset viewer.