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.Problems.GrammaticalOptimization/RoyalSequenceProblem.cs

    r11792 r11799  
    3131      this.correctReward = correctReward;
    3232      this.incorrectReward = incorrectReward;
    33       var sentenceSymbol = 'S';
     33      const char sentenceSymbol = 'S';
    3434      var terminalSymbols = Enumerable.Range(0, alphabetSize).Select(off => (char)((byte)'a' + off)).ToArray();
    35       var nonTerminalSymbols = new char[] { 'S' };
    36       var rules = terminalSymbols.Select(t => Tuple.Create('S', t.ToString()))
    37         .Concat(terminalSymbols.Select(t => Tuple.Create('S', t + "S")));
     35      var nonTerminalSymbols = new char[] { sentenceSymbol };
     36      var rules = terminalSymbols.Select(t => Tuple.Create(sentenceSymbol, t.ToString()))
     37        .Concat(terminalSymbols.Select(t => Tuple.Create(sentenceSymbol, t + sentenceSymbol.ToString())));
    3838      //var rules = terminalSymbols.Select(t => Tuple.Create('S', t + "S"))
    3939      //  .Concat(terminalSymbols.Select(t => Tuple.Create('S', t.ToString())));
     
    6464      // sentence must contain only terminal symbols, we are not checking if the sentence is syntactically valid here because it would be too slow!
    6565      Debug.Assert(sentence.Any(c => grammar.IsTerminal(c)));
    66       // as long as only correct symbols are found we increase the reward by +1
    67       // on the first incorrect symbol we return
    6866      var reward = 0.0;
    6967      for (int i = 0; i < Math.Min(sentence.Length, sequenceLen); i++) {
     
    7169          reward += correctReward;
    7270        } else {
    73           // alternatively reduce reward by number of remaining symbols
     71          // reduce reward by number of remaining symbols
    7472          return Math.Max(0.0, reward + incorrectReward * (sentence.Length - i));
    75           // stop on first incorrect symbol and return reward
    76           //return reward;
    7773        }
    7874      }
     
    8076    }
    8177
     78    // in each position there could be multiple correct and incorrect symbols
    8279    public string CanonicalRepresentation(string terminalPhrase) {
    83       throw new NotImplementedException();
    84       return terminalPhrase;
     80      var sb = new StringBuilder();
     81      for (int i = 0; i < terminalPhrase.Length; i++) {
     82        if (optimalSymbolsForPos[i].Contains(terminalPhrase[i])) {
     83          sb.Append(optimalSymbolsForPos[i].First()); // all symbols in the set are equivalent
     84        } else {
     85          sb.Append(terminalPhrase[i]);
     86        }
     87      }
     88      return sb.ToString();
    8589    }
    8690  }
Note: See TracChangeset for help on using the changeset viewer.