Changeset 11799 for branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization
- Timestamp:
- 01/19/15 20:09:12 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization/SequentialSearch.cs
r11793 r11799 70 70 Reset(); 71 71 72 for (int i = 0; bestQuality < 1.0 &&!Done() && i < maxIterations; i++) {72 for (int i = 0; /*!bestQuality.IsAlmost(1.0) && */!Done() && i < maxIterations; i++) { 73 73 var phrase = SampleSentence(problem.Grammar); 74 74 // can fail on the last sentence … … 97 97 stateChain.Clear(); 98 98 phrase = new Sequence(rootNode.phrase); 99 //var startPhrase = new Sequence("a*b+c*d+e*f+E");100 99 } while (!Done() && !TryCompleteSentence(grammar, ref phrase)); 101 100 return phrase; … … 110 109 111 110 while (!phrase.IsTerminal) { 112 113 114 //if (n.randomTries < randomTries) { 115 // n.randomTries++; 116 // curDepth = Math.Max(curDepth, curDepth); 117 // g.CompleteSentenceRandomly(random, phrase, maxLen); 118 // return true; 119 //} else { 120 // => select using bandit policy 121 // failure means we simply restart 122 GenerateFollowStates(n); // creates child nodes for node n 123 124 int selectedChildIdx; 125 if (!behaviourPolicy.TrySelect(random, n.phrase, n.children.Select(ch => ch.phrase), out selectedChildIdx)) { 126 return false; 127 } 128 phrase.ReplaceAt(phrase.FirstNonTerminalIndex, 1, n.children[selectedChildIdx].alternative); 129 130 // prepare for next iteration 131 n = n.children[selectedChildIdx]; 132 stateChain.Add(n.phrase); 133 curDepth++; 134 //} 111 if (n.randomTries < randomTries) { 112 n.randomTries++; 113 maxSearchDepth = Math.Max(maxSearchDepth, curDepth); 114 g.CompleteSentenceRandomly(random, phrase, maxLen); 115 return true; 116 } else { 117 // => select using bandit policy 118 // failure means we simply restart 119 GenerateFollowStates(n); // creates child nodes for node n 120 121 int selectedChildIdx; 122 if (!behaviourPolicy.TrySelect(random, n.phrase, n.children.Select(ch => ch.phrase), out selectedChildIdx)) { 123 return false; 124 } 125 phrase.ReplaceAt(phrase.FirstNonTerminalIndex, 1, n.children[selectedChildIdx].alternative); 126 127 // prepare for next iteration 128 n = n.children[selectedChildIdx]; 129 stateChain.Add(n.phrase); 130 curDepth++; 131 } 135 132 } // while 136 133 … … 157 154 int idx = 0; 158 155 foreach (var alt in alts) { 159 var newPhrase = new Sequence(phrase); // clone 160 newPhrase.ReplaceAt(newPhrase.FirstNonTerminalIndex, 1, alt); 161 children[idx++] = new TreeNode(newPhrase.ToString(), alt); 156 // var newPhrase = new Sequence(phrase); // clone 157 // newPhrase.ReplaceAt(newPhrase.FirstNonTerminalIndex, 1, alt); 158 // children[idx++] = new TreeNode(newPhrase.ToString(), alt); 159 160 // since we are not using a sequence later on we might directly transform the current sequence to a string and replace there 161 var phraseStr = phrase.ToString(); 162 var sb = new StringBuilder(phraseStr); 163 sb.Remove(phrase.FirstNonTerminalIndex, 1).Insert(phrase.FirstNonTerminalIndex, alt.ToString()); 164 children[idx++] = new TreeNode(sb.ToString(), alt); 162 165 } 163 166 n.children = children;
Note: See TracChangeset
for help on using the changeset viewer.