Changeset 15987


Ignore:
Timestamp:
07/05/18 17:52:31 (3 years ago)
Author:
bburlacu
Message:

#2886: Make sure to clear search data structures before returning in GrammarEnumerationAlgorithm.OnStopped()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/GrammarEnumerationAlgorithm.cs

    r15979 r15987  
    374374          } else if (!OpenPhrases.Contains(phraseHash) && !ArchivedPhrases.Contains(phraseHash)) {
    375375
    376             double r2 = GetR2(newPhrase, fetchedSearchNode.R2);
     376            bool isCompleteSentence = IsCompleteSentence(newPhrase);
     377            double r2 = isCompleteSentence ? Grammar.EvaluatePhrase(newPhrase, Problem.ProblemData, OptimizeConstants) : fetchedSearchNode.R2;
    377378            double phrasePriority = GetPriority(newPhrase, r2, maxSentenceLength, errorWeight, variableImportanceWeight);
    378379
     
    396397      double relLength = (double)phrase.Count() / maxSentenceLength;
    397398      double error = 1.0 - r2;
    398 
    399       return relLength + errorWeight * error + variableImportanceWeight * phraseVariableImportance;
    400     }
    401 
    402     private double GetR2(SymbolString phrase, double parentR2) {
    403       int length = phrase.Count();
    404 
    405       // If the only nonterminal symbol is Expr, we can need to evaluate the sentence. Otherwise
    406       // the phrase has the same r2 as its parent, from which it was derived.
    407       for (int i = 0; i < length; i++) {
    408         if (phrase[i] is NonterminalSymbol && phrase[i] != Grammar.Expr) {
    409           return parentR2;
    410         }
    411       }
    412 
    413       return Grammar.EvaluatePhrase(phrase, Problem.ProblemData, OptimizeConstants);
     399      return error * relLength;
     400    }
     401
     402    private bool IsCompleteSentence(SymbolString phrase) {
     403      return !phrase.Any(x => x is NonterminalSymbol && x != Grammar.Expr);
    414404    }
    415405
     
    461451    protected override void OnStopped() {
    462452      previousExecutionState = this.ExecutionState;
    463 
    464       if (BestTrainingSentence == null) {
    465         base.OnStopped();
    466         return;
    467       }
    468 
    469453      // free memory at the end of the run (this saves a lot of memory)
    470454      ArchivedPhrases.Clear();
    471455      OpenPhrases.Clear();
    472456      DistinctSentencesComplexity.Clear();
     457
     458      if (BestTrainingSentence == null) {
     459        base.OnStopped();
     460        return;
     461      }
    473462
    474463      var tree = Grammar.ParseSymbolicExpressionTree(BestTrainingSentence);
Note: See TracChangeset for help on using the changeset viewer.