Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/24/18 13:11:59 (7 years ago)
Author:
lkammere
Message:

#2886: Add separate data structure for storing phrases in the queue.

File:
1 edited

Legend:

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

    r15910 r15915  
    174174      int maxSentenceLength = GetMaxSentenceLength();
    175175
    176       OpenPhrases.Store(phrase0Hash, 0.0, phrase0);
     176      OpenPhrases.Store(new SearchNode(phrase0Hash, 0.0, 0.0, phrase0));
    177177      while (OpenPhrases.Count > 0) {
    178178        if (cancellationToken.IsCancellationRequested) break;
    179179
    180         StoredSymbolString fetchedPhrase = OpenPhrases.GetNext();
    181         SymbolString currPhrase = fetchedPhrase.SymbolString;
    182 
    183         OnPhraseFetched(fetchedPhrase.Hash, currPhrase);
    184 
    185         ArchivedPhrases.Add(fetchedPhrase.Hash);
     180        SearchNode fetchedSearchNode = OpenPhrases.GetNext();
     181        SymbolString currPhrase = fetchedSearchNode.SymbolString;
     182
     183        OnPhraseFetched(fetchedSearchNode.Hash, currPhrase);
     184
     185        ArchivedPhrases.Add(fetchedSearchNode.Hash);
    186186
    187187        // expand next nonterminal symbols
     
    199199            var phraseHash = Grammar.Hasher.CalcHashCode(newPhrase);
    200200
    201             OnPhraseDerived(fetchedPhrase.Hash, fetchedPhrase.SymbolString, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]);
     201            OnPhraseDerived(fetchedSearchNode.Hash, fetchedSearchNode.SymbolString, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]);
    202202
    203203            if (newPhrase.IsSentence()) {
    204204              AllGeneratedSentencesCount++;
    205205
    206               OnSentenceGenerated(fetchedPhrase.Hash, fetchedPhrase.SymbolString, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]);
     206              OnSentenceGenerated(fetchedSearchNode.Hash, fetchedSearchNode.SymbolString, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]);
    207207
    208208              // Is the best solution found? (only if RSquaredEvaluator is activated)
     
    219219
    220220                DistinctSentencesComplexity[phraseHash] = newPhraseComplexity;
    221                 OnDistinctSentenceGenerated(fetchedPhrase.Hash, fetchedPhrase.SymbolString, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]);
     221                OnDistinctSentenceGenerated(fetchedSearchNode.Hash, fetchedSearchNode.SymbolString, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]);
    222222              }
    223223              UpdateView();
    224224
    225225            } else if (!OpenPhrases.Contains(phraseHash) && !ArchivedPhrases.Contains(phraseHash)) {
    226               double phrasePriority = GetPriority(newPhrase, maxSentenceLength);
    227               OpenPhrases.Store(phraseHash, phrasePriority, newPhrase);
     226
     227              double r2 = GetR2(newPhrase, fetchedSearchNode.R2);
     228              double phrasePriority = GetPriority(newPhrase, r2, maxSentenceLength);
     229
     230              SearchNode newSearchNode = new SearchNode(phraseHash, phrasePriority, r2, newPhrase);
     231              OpenPhrases.Store(newSearchNode);
    228232            }
    229233          }
     
    233237    }
    234238
    235     protected double GetPriority(SymbolString phrase, int maxSentenceLength) {
     239    protected double GetPriority(SymbolString phrase, double r2, int maxSentenceLength) {
    236240      double relLength = (double)phrase.Count() / maxSentenceLength;
    237 
    238       double r2 = Grammar.EvaluatePhrase(phrase, Problem.ProblemData, OptimizeConstants);
    239241      double error = 1.0 - r2;
    240242
    241243      return relLength + ErrorWeight * error;
     244    }
     245
     246    private double GetR2(SymbolString phrase, double parentR2) {
     247      int length = phrase.Count();
     248
     249      // If the only nonterminal symbol is Expr, we can need to evaluate the sentence. Otherwise
     250      // the phrase has the same r2 as its parent, from which it was derived.
     251      for (int i = 0; i < length; i++) {
     252        if (phrase[i] is NonterminalSymbol && phrase[i] != Grammar.Expr) {
     253          return parentR2;
     254        }
     255      }
     256
     257      return Grammar.EvaluatePhrase(phrase, Problem.ProblemData, OptimizeConstants);
    242258    }
    243259
Note: See TracChangeset for help on using the changeset viewer.