Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/19/15 20:09:12 (9 years ago)
Author:
gkronber
Message:

#2283: performance tuning and reactivated random-roll-out policy in sequential search

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization/SequentialSearch.cs

    r11793 r11799  
    7070      Reset();
    7171
    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++) {
    7373        var phrase = SampleSentence(problem.Grammar);
    7474        // can fail on the last sentence
     
    9797        stateChain.Clear();
    9898        phrase = new Sequence(rootNode.phrase);
    99         //var startPhrase = new Sequence("a*b+c*d+e*f+E");
    10099      } while (!Done() && !TryCompleteSentence(grammar, ref phrase));
    101100      return phrase;
     
    110109
    111110      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        }
    135132      } // while
    136133
     
    157154        int idx = 0;
    158155        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);
    162165        }
    163166        n.children = children;
Note: See TracChangeset for help on using the changeset viewer.