Changeset 15915 for branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/GrammarEnumerationAlgorithm.cs
- Timestamp:
- 04/24/18 13:11:59 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/GrammarEnumerationAlgorithm.cs
r15910 r15915 174 174 int maxSentenceLength = GetMaxSentenceLength(); 175 175 176 OpenPhrases.Store( phrase0Hash, 0.0, phrase0);176 OpenPhrases.Store(new SearchNode(phrase0Hash, 0.0, 0.0, phrase0)); 177 177 while (OpenPhrases.Count > 0) { 178 178 if (cancellationToken.IsCancellationRequested) break; 179 179 180 S toredSymbolString fetchedPhrase = OpenPhrases.GetNext();181 SymbolString currPhrase = fetched Phrase.SymbolString;182 183 OnPhraseFetched(fetched Phrase.Hash, currPhrase);184 185 ArchivedPhrases.Add(fetched Phrase.Hash);180 SearchNode fetchedSearchNode = OpenPhrases.GetNext(); 181 SymbolString currPhrase = fetchedSearchNode.SymbolString; 182 183 OnPhraseFetched(fetchedSearchNode.Hash, currPhrase); 184 185 ArchivedPhrases.Add(fetchedSearchNode.Hash); 186 186 187 187 // expand next nonterminal symbols … … 199 199 var phraseHash = Grammar.Hasher.CalcHashCode(newPhrase); 200 200 201 OnPhraseDerived(fetched Phrase.Hash, fetchedPhrase.SymbolString, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]);201 OnPhraseDerived(fetchedSearchNode.Hash, fetchedSearchNode.SymbolString, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]); 202 202 203 203 if (newPhrase.IsSentence()) { 204 204 AllGeneratedSentencesCount++; 205 205 206 OnSentenceGenerated(fetched Phrase.Hash, fetchedPhrase.SymbolString, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]);206 OnSentenceGenerated(fetchedSearchNode.Hash, fetchedSearchNode.SymbolString, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]); 207 207 208 208 // Is the best solution found? (only if RSquaredEvaluator is activated) … … 219 219 220 220 DistinctSentencesComplexity[phraseHash] = newPhraseComplexity; 221 OnDistinctSentenceGenerated(fetched Phrase.Hash, fetchedPhrase.SymbolString, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]);221 OnDistinctSentenceGenerated(fetchedSearchNode.Hash, fetchedSearchNode.SymbolString, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]); 222 222 } 223 223 UpdateView(); 224 224 225 225 } 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); 228 232 } 229 233 } … … 233 237 } 234 238 235 protected double GetPriority(SymbolString phrase, int maxSentenceLength) {239 protected double GetPriority(SymbolString phrase, double r2, int maxSentenceLength) { 236 240 double relLength = (double)phrase.Count() / maxSentenceLength; 237 238 double r2 = Grammar.EvaluatePhrase(phrase, Problem.ProblemData, OptimizeConstants);239 241 double error = 1.0 - r2; 240 242 241 243 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); 242 258 } 243 259
Note: See TracChangeset
for help on using the changeset viewer.