Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/11/18 10:34:21 (6 years ago)
Author:
bburlacu
Message:

#2886: refactor code

File:
1 edited

Legend:

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

    r15985 r15993  
    22using System.Diagnostics;
    33using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration;
     4using HeuristicLab.Analysis;
    45using HeuristicLab.Common;
    56using HeuristicLab.Core;
     
    2021    public static readonly string BestTrainingSolutionResultName = "Best solution (Training)";
    2122    public static readonly string BestComplexityResultName = "Best solution complexity";
     23    public static readonly string BestSolutions = "Best solutions";
    2224
    2325    private static readonly ISymbolicDataAnalysisExpressionTreeInterpreter expressionTreeLinearInterpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
    24 
    25     public bool OptimizeConstants { get; set; }
    2626
    2727    public RSquaredEvaluator() { }
     
    3131
    3232    protected RSquaredEvaluator(RSquaredEvaluator original, Cloner cloner) : base(original, cloner) {
    33       this.OptimizeConstants = original.OptimizeConstants;
    3433    }
    3534
     
    5251    private void AlgorithmOnDistinctSentenceGenerated(object sender, PhraseAddedEventArgs phraseAddedEventArgs) {
    5352      GrammarEnumerationAlgorithm algorithm = (GrammarEnumerationAlgorithm)sender;
    54       EvaluateSentence(algorithm, phraseAddedEventArgs.NewPhrase);
     53      EvaluateSentence(algorithm, phraseAddedEventArgs.NewPhrase, algorithm.OptimizeConstants);
    5554    }
    5655
     
    7069    }
    7170
    72     private void EvaluateSentence(GrammarEnumerationAlgorithm algorithm, SymbolString symbolString) {
     71    private void EvaluateSentence(GrammarEnumerationAlgorithm algorithm, SymbolString symbolString, bool optimizeConstants) {
    7372      var results = algorithm.Results;
    7473      var grammar = algorithm.Grammar;
     
    7877      Debug.Assert(SymbolicRegressionConstantOptimizationEvaluator.CanOptimizeConstants(tree));
    7978
    80       double r2 = Evaluate(problemData, tree, OptimizeConstants);
     79      double r2 = Evaluate(problemData, tree, optimizeConstants);
    8180      double bestR2 = results.ContainsKey(BestTrainingQualityResultName) ? GetValue<double>(results[BestTrainingQualityResultName].Value) : 0.0;
    8281      if (r2 < bestR2)
     
    9089        results.AddOrUpdateResult(BestComplexityResultName, new IntValue(complexity));
    9190        algorithm.BestTrainingSentence = symbolString;
     91
     92        // record best sentence quality & length
     93        DataTable dt;
     94        if (!results.ContainsKey(BestSolutions)) {
     95          var names = new[] { "Quality", "Relative Length", "Complexity", "Timestamp" };
     96          dt = new DataTable();
     97          foreach (var name in names) {
     98            dt.Rows.Add(new DataRow(name) { VisualProperties = { StartIndexZero = true } });
     99          }
     100          results.AddOrUpdateResult(BestSolutions, dt);
     101        }
     102        dt = (DataTable)results[BestSolutions].Value;
     103        dt.Rows["Quality"].Values.Add(r2);
     104        dt.Rows["Relative Length"].Values.Add((double)symbolString.Count() / algorithm.MaxSentenceLength);
     105        dt.Rows["Complexity"].Values.Add(complexity);
     106        dt.Rows["Timestamp"].Values.Add(algorithm.ExecutionTime.TotalMilliseconds / 1000d);
    92107      }
    93108    }
Note: See TracChangeset for help on using the changeset viewer.