Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/23/18 15:41:02 (7 years ago)
Author:
lkammere
Message:

#2886: Add simple constant optimization to evaluation of solutions.

File:
1 edited

Legend:

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

    r15824 r15859  
    11using System;
     2using System.Diagnostics;
    23using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration;
    34using HeuristicLab.Common;
     
    6364
    6465    private void EvaluateSentence(GrammarEnumerationAlgorithm algorithm, SymbolString symbolString) {
     66      var problemData = algorithm.Problem.ProblemData;
     67
    6568      SymbolicExpressionTree tree = algorithm.Grammar.ParseSymbolicExpressionTree(symbolString);
     69      Debug.Assert(SymbolicRegressionConstantOptimizationEvaluator.CanOptimizeConstants(tree));
    6670
    67       var problemData = algorithm.Problem.ProblemData;
     71      // TODO: Initialize constant values randomly
     72      // TODO: Restarts
     73
     74      double r2 = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(expressionTreeLinearInterpreter,
     75        tree,
     76        problemData,
     77        problemData.TrainingIndices,
     78        applyLinearScaling: false,
     79        maxIterations: 200,
     80        updateVariableWeights: true,
     81        updateConstantsInTree: true);
     82
     83      foreach (var symbolicExpressionTreeNode in tree.IterateNodesPostfix()) {
     84        ConstantTreeNode constTreeNode = symbolicExpressionTreeNode as ConstantTreeNode;
     85        if (constTreeNode != null && constTreeNode.Value.IsAlmost(0.0)) {
     86          constTreeNode.Value = 0.0;
     87        }
     88      }
     89
    6890      SymbolicRegressionModel model = new SymbolicRegressionModel(
    6991        problemData.TargetVariable,
     
    7193        expressionTreeLinearInterpreter);
    7294
    73       var target = problemData.TargetVariableTrainingValues;
    74       var estVals = model.GetEstimatedValues(problemData.Dataset, problemData.TrainingIndices);
    75       OnlineCalculatorError error;
    76       var r2 = OnlinePearsonsRCalculator.Calculate(target, estVals, out error);
    77       if (error != OnlineCalculatorError.None) r2 = 0.0;
     95      //var target = problemData.TargetVariableTrainingValues;
     96      //var estVals = model.GetEstimatedValues(problemData.Dataset, problemData.TrainingIndices);
     97      //OnlineCalculatorError error;
     98      //r2 = OnlinePearsonsRCalculator.Calculate(target, estVals, out error);
     99      //if (error != OnlineCalculatorError.None) r2 = 0.0;
    78100
    79101      var bestR2Result = (DoubleValue)algorithm.Results[BestTrainingQualityResultName].Value;
    80       if (r2 > bestR2Result.Value) {
     102      bool better = r2 > bestR2Result.Value;
     103      bool equallyGood = r2.IsAlmost(bestR2Result.Value);
     104      bool shorter = algorithm.BestTrainingSentence != null && symbolString.Count() < algorithm.BestTrainingSentence.Count();
     105      if (better || (equallyGood && shorter)) {
    81106        bestR2Result.Value = r2;
    82107        algorithm.Results.AddOrUpdateResult(BestTrainingModelResultName, model);
Note: See TracChangeset for help on using the changeset viewer.