- Timestamp:
- 04/24/18 13:11:59 (7 years ago)
- Location:
- branches/2886_SymRegGrammarEnumeration
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Analysis/RSquaredEvaluator.cs ¶
r15910 r15915 110 110 problemData.TrainingIndices, 111 111 applyLinearScaling: false, 112 maxIterations: 50,112 maxIterations: 10, 113 113 updateVariableWeights: true, 114 114 updateConstantsInTree: true); -
TabularUnified 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 -
TabularUnified branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/SearchDataStructure.cs ¶
r15907 r15915 5 5 namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration { 6 6 7 public class S toredSymbolString{7 public class SearchNode { 8 8 public readonly int Hash; 9 9 public readonly SymbolString SymbolString; 10 public readonly double Priority; 11 public readonly double R2; 10 12 11 public S toredSymbolString(int hash, SymbolString symbolString) {13 public SearchNode(int hash, double priority, double r2, SymbolString symbolString) { 12 14 Hash = hash; 15 Priority = priority; 13 16 SymbolString = symbolString; 17 R2 = r2; 14 18 } 15 19 } … … 19 23 } 20 24 21 class SearchDataStore : IEnumerable<S ymbolString> {25 class SearchDataStore : IEnumerable<SearchNode> { 22 26 23 private Dictionary<int, S ymbolString> storedValues; // Store hash-references and associated, actual values27 private Dictionary<int, SearchNode> storedValues; // Store hash-references and associated, actual values 24 28 private Action<int, double> storeInternal; // Store hash-references 25 29 private Func<int> fetchInternal; // Fetch hash-reference 26 30 27 31 public SearchDataStore(StorageType storageType) { 28 storedValues = new Dictionary<int, S ymbolString>();32 storedValues = new Dictionary<int, SearchNode>(); 29 33 30 34 switch (storageType) { … … 89 93 #region Interface 90 94 91 public S toredSymbolStringGetNext() {95 public SearchNode GetNext() { 92 96 int hash = fetchInternal.Invoke(); 93 S ymbolStringresult = storedValues[hash];97 SearchNode result = storedValues[hash]; 94 98 storedValues.Remove(hash); 95 return new StoredSymbolString(hash, result);99 return result; 96 100 } 97 101 98 public void Store( int hash, double priority, SymbolString s) {99 storeInternal( hash, priority);100 storedValues[ hash] = s;102 public void Store(SearchNode sn) { 103 storeInternal(sn.Hash, sn.Priority); 104 storedValues[sn.Hash] = sn; 101 105 } 102 106 … … 113 117 } 114 118 115 public IEnumerator<S ymbolString> GetEnumerator() {119 public IEnumerator<SearchNode> GetEnumerator() { 116 120 return storedValues.Values.GetEnumerator(); 117 121 } -
TabularUnified branches/2886_SymRegGrammarEnumeration/Test/GrammarEnumerationTest.cs ¶
r15907 r15915 210 210 public void Constants_Nguyen7() { 211 211 // log(x+1) + log(x*x + 1) 212 alg.MaxComplexity = 3; 212 alg.MaxComplexity = 4; 213 alg.OptimizeConstants = true; 213 214 alg.Problem.ProblemData = new NguyenFunctionSeven().GenerateRegressionData(); 214 215 … … 242 243 // x*x*x*x - x*x*x + y*y/2 -y 243 244 alg.MaxComplexity = 10; 245 alg.OptimizeConstants = true; 244 246 alg.Problem.ProblemData = new NguyenFunctionTwelve().GenerateRegressionData(); 245 247 … … 286 288 // (30*x*z) / ((x - 10)*y*y) 287 289 alg.MaxComplexity = 5; 290 alg.OptimizeConstants = true; 288 291 alg.Problem.ProblemData = new KeijzerFunctionFive().GenerateRegressionData(); 289 292 … … 357 360 [TestProperty("Goal", "structure search + const op")] 358 361 public void Constants_Keijzer14() { 359 // 8 / (2 + x*x + y*y 362 // 8 / (2 + x*x + y*y) 360 363 alg.MaxComplexity = 4; 364 alg.OptimizeConstants = true; 361 365 alg.Problem.ProblemData = new KeijzerFunctionFourteen().GenerateRegressionData(); 362 366 … … 396 400 // x*x*x / 5 + y*y*y / 2 - y - x 397 401 alg.MaxComplexity = 8; 402 alg.OptimizeConstants = true; 398 403 alg.Problem.ProblemData = new KeijzerFunctionFifteen().GenerateRegressionData(); 399 404 … … 428 433 [TestProperty("Goal", "Poly-10 derivatives")] 429 434 public void MctsSymbReg_NoConstants_Poly10_Part1() { 430 alg.MaxComplexity = 12; 435 alg.MaxComplexity = 12; 431 436 alg.OptimizeConstants = false; 432 437 var regProblem = new PolyTen(123).GenerateRegressionData();
Note: See TracChangeset
for help on using the changeset viewer.